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 ripple {
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
30 permission[sfPermissionValue], ctx.rules))
31 return temMALFORMED;
32 }
33
34 return tesSUCCESS;
35}
36
37TER
39{
40 if (!ctx.view.exists(keylet::account(ctx.tx[sfAccount])))
41 return terNO_ACCOUNT; // LCOV_EXCL_LINE
42
43 if (!ctx.view.exists(keylet::account(ctx.tx[sfAuthorize])))
44 return tecNO_TARGET;
45
46 return tesSUCCESS;
47}
48
49TER
51{
52 auto const sleOwner = ctx_.view().peek(keylet::account(account_));
53 if (!sleOwner)
54 return tefINTERNAL; // LCOV_EXCL_LINE
55
56 auto const& authAccount = ctx_.tx[sfAuthorize];
57 auto const delegateKey = keylet::delegate(account_, authAccount);
58
59 auto sle = ctx_.view().peek(delegateKey);
60 if (sle)
61 {
62 auto const& permissions = ctx_.tx.getFieldArray(sfPermissions);
63 if (permissions.empty())
64 // if permissions array is empty, delete the ledger object.
65 return deleteDelegate(view(), sle, account_, j_);
66
67 sle->setFieldArray(sfPermissions, permissions);
68 ctx_.view().update(sle);
69 return tesSUCCESS;
70 }
71
72 STAmount const reserve{ctx_.view().fees().accountReserve(
73 sleOwner->getFieldU32(sfOwnerCount) + 1)};
74
75 if (mPriorBalance < reserve)
77
78 auto const& permissions = ctx_.tx.getFieldArray(sfPermissions);
79 if (!permissions.empty())
80 {
81 sle = std::make_shared<SLE>(delegateKey);
82 sle->setAccountID(sfAccount, account_);
83 sle->setAccountID(sfAuthorize, authAccount);
84
85 sle->setFieldArray(sfPermissions, permissions);
86 auto const page = ctx_.view().dirInsert(
88 delegateKey,
90
91 if (!page)
92 return tecDIR_FULL; // LCOV_EXCL_LINE
93
94 (*sle)[sfOwnerNode] = *page;
95 ctx_.view().insert(sle);
96 adjustOwnerCount(ctx_.view(), sleOwner, 1, ctx_.journal);
97 }
98
99 return tesSUCCESS;
100}
101
102TER
104 ApplyView& view,
105 std::shared_ptr<SLE> const& sle,
106 AccountID const& account,
108{
109 if (!sle)
110 return tecINTERNAL; // LCOV_EXCL_LINE
111
112 if (!view.dirRemove(
113 keylet::ownerDir(account), (*sle)[sfOwnerNode], sle->key(), false))
114 {
115 // LCOV_EXCL_START
116 JLOG(j.fatal()) << "Unable to delete Delegate from owner.";
117 return tefBAD_LEDGER;
118 // LCOV_EXCL_STOP
119 }
120
121 auto const sleOwner = view.peek(keylet::account(account));
122 if (!sleOwner)
123 return tecINTERNAL; // LCOV_EXCL_LINE
124
125 adjustOwnerCount(view, sleOwner, -1, j);
126
127 view.erase(sle);
128
129 return tesSUCCESS;
130}
131
132} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:41
Stream fatal() const
Definition Journal.h:333
ApplyView & view()
beast::Journal const journal
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:124
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 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:300
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TER deleteDelegate(ApplyView &view, std::shared_ptr< SLE > const &sle, AccountID const &account, beast::Journal j)
TER doApply() override
static Permission const & getInstance()
bool isDelegatable(std::uint32_t const &permissionValue, Rules const &rules) const
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:683
AccountID const account_
Definition Transactor.h:128
ApplyView & view()
Definition Transactor.h:144
beast::Journal const j_
Definition Transactor.h:126
XRPAmount mPriorBalance
Definition Transactor.h:129
ApplyContext & ctx_
Definition Transactor.h:124
T insert(T... args)
T is_same_v
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
Definition Indexes.cpp:446
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:355
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::size_t constexpr permissionMaxSize
The maximum number of delegate permissions an account can grant.
Definition Protocol.h:160
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:1013
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:1031
@ tefBAD_LEDGER
Definition TER.h:151
@ tefINTERNAL
Definition TER.h:154
@ tecNO_TARGET
Definition TER.h:286
@ tecDIR_FULL
Definition TER.h:269
@ tecINTERNAL
Definition TER.h:292
@ tecINSUFFICIENT_RESERVE
Definition TER.h:289
@ tesSUCCESS
Definition TER.h:226
@ terNO_ACCOUNT
Definition TER.h:198
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:590
@ temMALFORMED
Definition TER.h:68
@ temARRAY_TOO_LARGE
Definition TER.h:122
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:61
ReadView const & view
Definition Transactor.h:64
State information when preflighting a tx.
Definition Transactor.h:16