rippled
Loading...
Searching...
No Matches
PermissionedDomainSet.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/misc/CredentialHelpers.h>
21#include <xrpld/app/tx/detail/PermissionedDomainSet.h>
22#include <xrpld/ledger/View.h>
23#include <xrpl/protocol/STObject.h>
24#include <xrpl/protocol/TxFlags.h>
25
26#include <optional>
27
28namespace ripple {
29
32{
33 if (!ctx.rules.enabled(featurePermissionedDomains))
34 return temDISABLED;
35 if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
36 return ret;
37
38 if (ctx.tx.getFlags() & tfUniversalMask)
39 {
40 JLOG(ctx.j.debug()) << "PermissionedDomainSet: invalid flags.";
41 return temINVALID_FLAG;
42 }
43
44 if (auto err = credentials::checkArray(
45 ctx.tx.getFieldArray(sfAcceptedCredentials),
47 ctx.j);
48 !isTesSuccess(err))
49 return err;
50
51 auto const domain = ctx.tx.at(~sfDomainID);
52 if (domain && *domain == beast::zero)
53 return temMALFORMED;
54
55 return preflight2(ctx);
56}
57
58TER
60{
61 auto const account = ctx.tx.getAccountID(sfAccount);
62
63 if (!ctx.view.exists(keylet::account(account)))
64 return tefINTERNAL; // LCOV_EXCL_LINE
65
66 auto const& credentials = ctx.tx.getFieldArray(sfAcceptedCredentials);
67 for (auto const& credential : credentials)
68 {
69 if (!ctx.view.exists(
70 keylet::account(credential.getAccountID(sfIssuer))))
71 return tecNO_ISSUER;
72 }
73
74 if (ctx.tx.isFieldPresent(sfDomainID))
75 {
76 auto const sleDomain = ctx.view.read(
78 if (!sleDomain)
79 return tecNO_ENTRY;
80 if (sleDomain->getAccountID(sfOwner) != account)
81 return tecNO_PERMISSION;
82 }
83
84 return tesSUCCESS;
85}
86
88TER
90{
91 auto const ownerSle = view().peek(keylet::account(account_));
92 if (!ownerSle)
93 return tefINTERNAL; // LCOV_EXCL_LINE
94
95 auto const sortedTxCredentials =
96 credentials::makeSorted(ctx_.tx.getFieldArray(sfAcceptedCredentials));
97 STArray sortedLE(sfAcceptedCredentials, sortedTxCredentials.size());
98 for (auto const& p : sortedTxCredentials)
99 {
100 auto cred = STObject::makeInnerObject(sfCredential);
101 cred.setAccountID(sfIssuer, p.first);
102 cred.setFieldVL(sfCredentialType, p.second);
103 sortedLE.push_back(std::move(cred));
104 }
105
106 if (ctx_.tx.isFieldPresent(sfDomainID))
107 {
108 // Modify existing permissioned domain.
109 auto slePd = view().peek(
111 if (!slePd)
112 return tefINTERNAL; // LCOV_EXCL_LINE
113 slePd->peekFieldArray(sfAcceptedCredentials) = std::move(sortedLE);
114 view().update(slePd);
115 }
116 else
117 {
118 // Create new permissioned domain.
119 // Check reserve availability for new object creation
120 auto const balance = STAmount((*ownerSle)[sfBalance]).xrp();
121 auto const reserve =
122 ctx_.view().fees().accountReserve((*ownerSle)[sfOwnerCount] + 1);
123 if (balance < reserve)
125
126 Keylet const pdKeylet = keylet::permissionedDomain(
127 account_, ctx_.tx.getFieldU32(sfSequence));
128 auto slePd = std::make_shared<SLE>(pdKeylet);
129 if (!slePd)
130 return tefINTERNAL; // LCOV_EXCL_LINE
131
132 slePd->setAccountID(sfOwner, account_);
133 slePd->setFieldU32(sfSequence, ctx_.tx.getFieldU32(sfSequence));
134 slePd->peekFieldArray(sfAcceptedCredentials) = std::move(sortedLE);
135 auto const page = view().dirInsert(
137 if (!page)
138 return tecDIR_FULL; // LCOV_EXCL_LINE
139
140 slePd->setFieldU64(sfOwnerNode, *page);
141 // If we succeeded, the new entry counts against the creator's reserve.
142 adjustOwnerCount(view(), ownerSle, 1, ctx_.journal);
143 view().insert(slePd);
144 }
145
146 return tesSUCCESS;
147}
148
149} // namespace ripple
Stream debug() const
Definition: Journal.h:317
ApplyView & view()
Definition: ApplyContext.h:54
beast::Journal const journal
Definition: ApplyContext.h:51
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
Definition: ApplyView.h:314
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
TER doApply() override
Attempt to create the Permissioned Domain.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:122
XRPAmount xrp() const
Definition: STAmount.cpp:273
void push_back(STObject const &object)
Definition: STArray.h:212
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:621
const STArray & getFieldArray(SField const &field) const
Definition: STObject.cpp:656
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
Definition: STObject.h:1025
std::uint32_t getFieldU32(SField const &field) const
Definition: STObject.cpp:585
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:454
static STObject makeInnerObject(SField const &name)
Definition: STObject.cpp:65
std::uint32_t getFlags() const
Definition: STObject.cpp:507
uint256 getFieldH256(SField const &field) const
Definition: STObject.cpp:615
AccountID const account_
Definition: Transactor.h:91
ApplyView & view()
Definition: Transactor.h:107
ApplyContext & ctx_
Definition: Transactor.h:88
NotTEC checkArray(STArray const &credentials, unsigned maxSize, beast::Journal j)
std::set< std::pair< AccountID, Slice > > makeSorted(STArray const &credentials)
Keylet permissionedDomain(AccountID const &account, std::uint32_t seq) noexcept
Definition: Indexes.cpp:532
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:160
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:350
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::size_t constexpr maxPermissionedDomainCredentialsArraySize
The maximum number of credentials can be passed in array for permissioned domain.
Definition: Protocol.h:110
bool isTesSuccess(TER x)
Definition: TER.h:656
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:887
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:82
@ tefINTERNAL
Definition: TER.h:173
static bool adjustOwnerCount(ApplyContext &ctx, int count)
Definition: SetOracle.cpp:186
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:134
@ tecNO_ENTRY
Definition: TER.h:293
@ tecNO_ISSUER
Definition: TER.h:286
@ tecDIR_FULL
Definition: TER.h:274
@ tecNO_PERMISSION
Definition: TER.h:292
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:294
@ tesSUCCESS
Definition: TER.h:242
constexpr std::uint32_t tfUniversalMask
Definition: TxFlags.h:62
@ credential
Credentials signature.
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:587
@ temMALFORMED
Definition: TER.h:87
@ temINVALID_FLAG
Definition: TER.h:111
@ temDISABLED
Definition: TER.h:114
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: protocol/Fees.h:49
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:39
State information when determining if a tx is likely to claim a fee.
Definition: Transactor.h:53
ReadView const & view
Definition: Transactor.h:56
State information when preflighting a tx.
Definition: Transactor.h:32
beast::Journal const j
Definition: Transactor.h:38