xrpld
Loading...
Searching...
No Matches
PermissionedDomainSet.cpp
1#include <xrpl/tx/transactors/permissioned_domain/PermissionedDomainSet.h>
2
3#include <xrpl/beast/utility/Zero.h>
4#include <xrpl/core/ServiceRegistry.h>
5#include <xrpl/ledger/helpers/AccountRootHelpers.h>
6#include <xrpl/ledger/helpers/CredentialHelpers.h>
7#include <xrpl/ledger/helpers/DirectoryHelpers.h>
8#include <xrpl/protocol/Feature.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/Keylet.h>
11#include <xrpl/protocol/Protocol.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/STLedgerEntry.h>
14#include <xrpl/protocol/STObject.h>
15#include <xrpl/protocol/STTx.h>
16#include <xrpl/protocol/TER.h>
17#include <xrpl/protocol/XRPAmount.h>
18#include <xrpl/tx/Transactor.h>
19
20#include <memory>
21#include <utility>
22
23namespace xrpl {
24
25bool
27{
28 return ctx.rules.enabled(featureCredentials);
29}
30
33{
34 if (auto err = credentials::checkArray(
35 ctx.tx.getFieldArray(sfAcceptedCredentials),
37 ctx.j);
38 !isTesSuccess(err))
39 return err;
40
41 auto const domain = ctx.tx.at(~sfDomainID);
42 if (domain && *domain == beast::kZero)
43 return temMALFORMED;
44
45 return tesSUCCESS;
46}
47
48TER
50{
51 auto const account = ctx.tx.getAccountID(sfAccount);
52
53 if (!ctx.view.exists(keylet::account(account)))
54 return tefINTERNAL; // LCOV_EXCL_LINE
55
56 auto const& credentials = ctx.tx.getFieldArray(sfAcceptedCredentials);
57 for (auto const& credential : credentials)
58 {
59 if (!ctx.view.exists(keylet::account(credential.getAccountID(sfIssuer))))
60 return tecNO_ISSUER;
61 }
62
63 if (ctx.tx.isFieldPresent(sfDomainID))
64 {
65 auto const sleDomain =
67 if (!sleDomain)
68 return tecNO_ENTRY;
69 if (sleDomain->getAccountID(sfOwner) != account)
70 return tecNO_PERMISSION;
71 }
72
73 return tesSUCCESS;
74}
75
77TER
79{
80 auto const ownerSle = view().peek(keylet::account(accountID_));
81 if (!ownerSle)
82 return tefINTERNAL; // LCOV_EXCL_LINE
83
84 auto const sortedTxCredentials =
85 credentials::makeSorted(ctx_.tx.getFieldArray(sfAcceptedCredentials));
86 STArray sortedLE(sfAcceptedCredentials, sortedTxCredentials.size());
87 for (auto const& p : sortedTxCredentials)
88 {
89 auto cred = STObject::makeInnerObject(sfCredential);
90 cred.setAccountID(sfIssuer, p.first);
91 cred.setFieldVL(sfCredentialType, p.second);
92 sortedLE.pushBack(std::move(cred));
93 }
94
95 if (ctx_.tx.isFieldPresent(sfDomainID))
96 {
97 // Modify existing permissioned domain.
98 auto slePd = view().peek(keylet::permissionedDomain(ctx_.tx.getFieldH256(sfDomainID)));
99 if (!slePd)
100 return tefINTERNAL; // LCOV_EXCL_LINE
101 slePd->peekFieldArray(sfAcceptedCredentials) = std::move(sortedLE);
102 view().update(slePd);
103 }
104 else
105 {
106 // Create new permissioned domain.
107 // Check reserve availability for new object creation
108 auto const balance = STAmount((*ownerSle)[sfBalance]).xrp();
109 auto const reserve = ctx_.view().fees().accountReserve((*ownerSle)[sfOwnerCount] + 1);
110 if (balance < reserve)
112
113 bool const fixEnabled = view().rules().enabled(fixCleanup3_1_3);
114 auto const seq = fixEnabled ? ctx_.tx.getSeqValue() : ctx_.tx.getFieldU32(sfSequence);
115 Keylet const pdKeylet = keylet::permissionedDomain(accountID_, seq);
116 auto slePd = std::make_shared<SLE>(pdKeylet);
117
118 slePd->setAccountID(sfOwner, accountID_);
119 slePd->setFieldU32(sfSequence, seq);
120 slePd->peekFieldArray(sfAcceptedCredentials) = std::move(sortedLE);
121 auto const page =
123 if (!page)
124 return tecDIR_FULL; // LCOV_EXCL_LINE
125
126 slePd->setFieldU64(sfOwnerNode, *page);
127 // If we succeeded, the new entry counts against the creator's reserve.
128 adjustOwnerCount(view(), ownerSle, 1, ctx_.journal);
129 view().insert(slePd);
130 }
131
132 return tesSUCCESS;
133}
134
135void
137{
138 // No transaction-specific invariants yet (future work).
139}
140
141bool
143 STTx const&,
144 TER,
145 XRPAmount,
146 ReadView const&,
147 beast::Journal const&)
148{
149 // No transaction-specific invariants yet (future work).
150 return true;
151}
152
153} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void insert(SLE::ref sle)=0
Insert a new state SLE.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(SLE::ref)> const &describe)
Insert an entry to a directory.
Definition ApplyView.h:340
virtual void update(SLE::ref sle)=0
Indicate changes to a peeked SLE.
TER doApply() override
Attempt to create the Permissioned Domain.
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static bool checkExtraFeatures(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
A view into a ledger.
Definition ReadView.h:31
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:171
XRPAmount xrp() const
Definition STAmount.cpp:271
void pushBack(STObject const &object)
Definition STArray.h:204
std::shared_ptr< STLedgerEntry const > const & const_ref
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
Definition STObject.h:1069
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:678
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:454
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:621
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:74
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:633
ApplyView & view()
Definition Transactor.h:136
AccountID const accountID_
Definition Transactor.h:120
ApplyContext & ctx_
Definition Transactor.h:116
T make_shared(T... args)
std::set< std::pair< AccountID, Slice > > makeSorted(STArray const &credentials)
NotTEC checkArray(STArray const &credentials, unsigned maxSize, beast::Journal j)
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:357
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Keylet permissionedDomain(AccountID const &account, std::uint32_t seq) noexcept
Definition Indexes.cpp:569
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ tefINTERNAL
Definition TER.h:163
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
void adjustOwnerCount(ApplyView &view, SLE::ref sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
@ temMALFORMED
Definition TER.h:73
bool isTesSuccess(TER x) noexcept
Definition TER.h:663
TERSubset< CanCvtToTER > TER
Definition TER.h:634
constexpr std::size_t kMaxPermissionedDomainCredentialsArraySize
The maximum number of credentials can be passed in array for permissioned domain.
Definition Protocol.h:232
@ tecDIR_FULL
Definition TER.h:285
@ tecNO_ENTRY
Definition TER.h:304
@ tecINSUFFICIENT_RESERVE
Definition TER.h:305
@ tecNO_PERMISSION
Definition TER.h:303
@ tecNO_ISSUER
Definition TER.h:297
@ tesSUCCESS
Definition TER.h:240
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
State information when preflighting a tx.
Definition Transactor.h:18
beast::Journal const j
Definition Transactor.h:25