rippled
Loading...
Searching...
No Matches
PermissionedDomainSet.cpp
1#include <xrpld/app/tx/detail/PermissionedDomainSet.h>
2
3#include <xrpl/ledger/CredentialHelpers.h>
4#include <xrpl/ledger/View.h>
5#include <xrpl/protocol/STObject.h>
6#include <xrpl/protocol/TxFlags.h>
7
8#include <optional>
9
10namespace xrpl {
11
12bool
14{
15 return ctx.rules.enabled(featureCredentials);
16}
17
20{
21 if (auto err = credentials::checkArray(
22 ctx.tx.getFieldArray(sfAcceptedCredentials), maxPermissionedDomainCredentialsArraySize, ctx.j);
23 !isTesSuccess(err))
24 return err;
25
26 auto const domain = ctx.tx.at(~sfDomainID);
27 if (domain && *domain == beast::zero)
28 return temMALFORMED;
29
30 return tesSUCCESS;
31}
32
33TER
35{
36 auto const account = ctx.tx.getAccountID(sfAccount);
37
38 if (!ctx.view.exists(keylet::account(account)))
39 return tefINTERNAL; // LCOV_EXCL_LINE
40
41 auto const& credentials = ctx.tx.getFieldArray(sfAcceptedCredentials);
42 for (auto const& credential : credentials)
43 {
44 if (!ctx.view.exists(keylet::account(credential.getAccountID(sfIssuer))))
45 return tecNO_ISSUER;
46 }
47
48 if (ctx.tx.isFieldPresent(sfDomainID))
49 {
50 auto const sleDomain = ctx.view.read(keylet::permissionedDomain(ctx.tx.getFieldH256(sfDomainID)));
51 if (!sleDomain)
52 return tecNO_ENTRY;
53 if (sleDomain->getAccountID(sfOwner) != account)
54 return tecNO_PERMISSION;
55 }
56
57 return tesSUCCESS;
58}
59
61TER
63{
64 auto const ownerSle = view().peek(keylet::account(account_));
65 if (!ownerSle)
66 return tefINTERNAL; // LCOV_EXCL_LINE
67
68 auto const sortedTxCredentials = credentials::makeSorted(ctx_.tx.getFieldArray(sfAcceptedCredentials));
69 STArray sortedLE(sfAcceptedCredentials, sortedTxCredentials.size());
70 for (auto const& p : sortedTxCredentials)
71 {
72 auto cred = STObject::makeInnerObject(sfCredential);
73 cred.setAccountID(sfIssuer, p.first);
74 cred.setFieldVL(sfCredentialType, p.second);
75 sortedLE.push_back(std::move(cred));
76 }
77
78 if (ctx_.tx.isFieldPresent(sfDomainID))
79 {
80 // Modify existing permissioned domain.
81 auto slePd = view().peek(keylet::permissionedDomain(ctx_.tx.getFieldH256(sfDomainID)));
82 if (!slePd)
83 return tefINTERNAL; // LCOV_EXCL_LINE
84 slePd->peekFieldArray(sfAcceptedCredentials) = std::move(sortedLE);
85 view().update(slePd);
86 }
87 else
88 {
89 // Create new permissioned domain.
90 // Check reserve availability for new object creation
91 auto const balance = STAmount((*ownerSle)[sfBalance]).xrp();
92 auto const reserve = ctx_.view().fees().accountReserve((*ownerSle)[sfOwnerCount] + 1);
93 if (balance < reserve)
95
96 Keylet const pdKeylet = keylet::permissionedDomain(account_, ctx_.tx.getFieldU32(sfSequence));
97 auto slePd = std::make_shared<SLE>(pdKeylet);
98 if (!slePd)
99 return tefINTERNAL; // LCOV_EXCL_LINE
100
101 slePd->setAccountID(sfOwner, account_);
102 slePd->setFieldU32(sfSequence, ctx_.tx.getFieldU32(sfSequence));
103 slePd->peekFieldArray(sfAcceptedCredentials) = std::move(sortedLE);
104 auto const page = view().dirInsert(keylet::ownerDir(account_), pdKeylet, describeOwnerDir(account_));
105 if (!page)
106 return tecDIR_FULL; // LCOV_EXCL_LINE
107
108 slePd->setFieldU64(sfOwnerNode, *page);
109 // If we succeeded, the new entry counts against the creator's reserve.
110 adjustOwnerCount(view(), ownerSle, 1, ctx_.journal);
111 view().insert(slePd);
112 }
113
114 return tesSUCCESS;
115}
116
117} // namespace xrpl
STTx const & tx
beast::Journal const journal
ApplyView & view()
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:284
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
TER doApply() override
Attempt to create the Permissioned Domain.
static bool checkExtraFeatures(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
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.
virtual std::shared_ptr< SLE const > 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:118
XRPAmount xrp() const
Definition STAmount.cpp:249
void push_back(STObject const &object)
Definition STArray.h:188
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
Definition STObject.h:1042
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:576
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:663
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:439
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:606
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:72
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:618
AccountID const account_
Definition Transactor.h:113
ApplyView & view()
Definition Transactor.h:129
ApplyContext & ctx_
Definition Transactor.h:109
T is_same_v
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:325
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:160
Keylet permissionedDomain(AccountID const &account, std::uint32_t seq) noexcept
Definition Indexes.cpp:510
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ tefINTERNAL
Definition TER.h:154
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
Definition View.cpp:941
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:955
std::size_t constexpr maxPermissionedDomainCredentialsArraySize
The maximum number of credentials can be passed in array for permissioned domain.
Definition Protocol.h:228
@ temMALFORMED
Definition TER.h:68
bool isTesSuccess(TER x) noexcept
Definition TER.h:650
@ tecDIR_FULL
Definition TER.h:269
@ tecNO_ENTRY
Definition TER.h:288
@ tecINSUFFICIENT_RESERVE
Definition TER.h:289
@ tecNO_PERMISSION
Definition TER.h:287
@ tecNO_ISSUER
Definition TER.h:281
@ tesSUCCESS
Definition TER.h:226
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:54
ReadView const & view
Definition Transactor.h:57
State information when preflighting a tx.
Definition Transactor.h:16
beast::Journal const j
Definition Transactor.h:23