rippled
Loading...
Searching...
No Matches
DelegateSet.cpp
1#include <xrpld/app/tx/detail/DelegateSet.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/ledger/View.h>
5#include <xrpl/protocol/Feature.h>
6#include <xrpl/protocol/Indexes.h>
7#include <xrpl/protocol/st.h>
8
9namespace xrpl {
10
13{
14 auto const& permissions = ctx.tx.getFieldArray(sfPermissions);
15 if (permissions.size() > permissionMaxSize)
16 return temARRAY_TOO_LARGE;
17
18 // can not authorize self
19 if (ctx.tx[sfAccount] == ctx.tx[sfAuthorize])
20 return temMALFORMED;
21
23
24 for (auto const& permission : permissions)
25 {
26 if (!permissionSet.insert(permission[sfPermissionValue]).second)
27 return temMALFORMED;
28
29 if (!Permission::getInstance().isDelegable(permission[sfPermissionValue], ctx.rules))
30 return temMALFORMED;
31 }
32
33 return tesSUCCESS;
34}
35
36TER
38{
39 if (!ctx.view.exists(keylet::account(ctx.tx[sfAccount])))
40 return terNO_ACCOUNT; // LCOV_EXCL_LINE
41
42 if (!ctx.view.exists(keylet::account(ctx.tx[sfAuthorize])))
43 return tecNO_TARGET;
44
45 return tesSUCCESS;
46}
47
48TER
50{
51 auto const sleOwner = ctx_.view().peek(keylet::account(account_));
52 if (!sleOwner)
53 return tefINTERNAL; // LCOV_EXCL_LINE
54
55 auto const& authAccount = ctx_.tx[sfAuthorize];
56 auto const delegateKey = keylet::delegate(account_, authAccount);
57
58 auto sle = ctx_.view().peek(delegateKey);
59 if (sle)
60 {
61 auto const& permissions = ctx_.tx.getFieldArray(sfPermissions);
62 if (permissions.empty())
63 // if permissions array is empty, delete the ledger object.
64 return deleteDelegate(view(), sle, account_, j_);
65
66 sle->setFieldArray(sfPermissions, permissions);
67 ctx_.view().update(sle);
68 return tesSUCCESS;
69 }
70
71 STAmount const reserve{ctx_.view().fees().accountReserve(sleOwner->getFieldU32(sfOwnerCount) + 1)};
72
73 if (mPriorBalance < reserve)
75
76 auto const& permissions = ctx_.tx.getFieldArray(sfPermissions);
77 if (!permissions.empty())
78 {
79 sle = std::make_shared<SLE>(delegateKey);
80 sle->setAccountID(sfAccount, account_);
81 sle->setAccountID(sfAuthorize, authAccount);
82
83 sle->setFieldArray(sfPermissions, permissions);
84 auto const page = ctx_.view().dirInsert(keylet::ownerDir(account_), delegateKey, describeOwnerDir(account_));
85
86 if (!page)
87 return tecDIR_FULL; // LCOV_EXCL_LINE
88
89 (*sle)[sfOwnerNode] = *page;
90 ctx_.view().insert(sle);
91 adjustOwnerCount(ctx_.view(), sleOwner, 1, ctx_.journal);
92 }
93
94 return tesSUCCESS;
95}
96
97TER
99 ApplyView& view,
100 std::shared_ptr<SLE> const& sle,
101 AccountID const& account,
103{
104 if (!sle)
105 return tecINTERNAL; // LCOV_EXCL_LINE
106
107 if (!view.dirRemove(keylet::ownerDir(account), (*sle)[sfOwnerNode], sle->key(), false))
108 {
109 // LCOV_EXCL_START
110 JLOG(j.fatal()) << "Unable to delete Delegate from owner.";
111 return tefBAD_LEDGER;
112 // LCOV_EXCL_STOP
113 }
114
115 auto const sleOwner = view.peek(keylet::account(account));
116 if (!sleOwner)
117 return tecINTERNAL; // LCOV_EXCL_LINE
118
119 adjustOwnerCount(view, sleOwner, -1, j);
120
121 view.erase(sle);
122
123 return tesSUCCESS;
124}
125
126} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
Stream fatal() const
Definition Journal.h:325
STTx const & tx
beast::Journal const journal
ApplyView & view()
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:115
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove 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.
static TER deleteDelegate(ApplyView &view, std::shared_ptr< SLE > const &sle, AccountID const &account, beast::Journal j)
static NotTEC preflight(PreflightContext const &ctx)
TER doApply() override
static TER preclaim(PreclaimContext const &ctx)
bool isDelegable(std::uint32_t const &permissionValue, Rules const &rules) const
static Permission const & getInstance()
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.
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:663
AccountID const account_
Definition Transactor.h:113
beast::Journal const j_
Definition Transactor.h:111
ApplyView & view()
Definition Transactor.h:129
XRPAmount mPriorBalance
Definition Transactor.h:114
ApplyContext & ctx_
Definition Transactor.h:109
T insert(T... args)
T is_same_v
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:325
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
Definition Indexes.cpp:406
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:160
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ terNO_ACCOUNT
Definition TER.h:198
@ tefBAD_LEDGER
Definition TER.h:151
@ 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
@ temARRAY_TOO_LARGE
Definition TER.h:122
@ temMALFORMED
Definition TER.h:68
@ tecDIR_FULL
Definition TER.h:269
@ tecNO_TARGET
Definition TER.h:286
@ tecINTERNAL
Definition TER.h:292
@ tecINSUFFICIENT_RESERVE
Definition TER.h:289
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:581
@ tesSUCCESS
Definition TER.h:226
std::size_t constexpr permissionMaxSize
The maximum number of delegate permissions an account can grant.
Definition Protocol.h:295
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
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