xrpld
Loading...
Searching...
No Matches
SponsorHelpers.cpp
1#include <xrpl/ledger/helpers/SponsorHelpers.h>
2
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/ledger/ApplyView.h>
5#include <xrpl/ledger/ReadView.h>
6#include <xrpl/ledger/helpers/AccountRootHelpers.h>
7#include <xrpl/ledger/helpers/EscrowHelpers.h>
8#include <xrpl/ledger/helpers/OracleHelpers.h>
9#include <xrpl/ledger/helpers/ProposalHelpers.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/LedgerFormats.h>
14#include <xrpl/protocol/Rules.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STArray.h>
17#include <xrpl/protocol/STLedgerEntry.h>
18#include <xrpl/protocol/STTx.h>
19#include <xrpl/protocol/TER.h>
20#include <xrpl/protocol/TxFormats.h>
21
22#include <cstdint>
23#include <expected>
24#include <optional>
25#include <unordered_set>
26
27namespace xrpl {
28
29bool
31{
32 // Transaction types explicitly allow-listed for reserve sponsorship, for
33 // v1. Lazily-initialized function-local static: constructed once on first
34 // use, with no startup cost paid by clients that never call this.
35 static std::unordered_set<TxType> const kReserveSponsorAllowed = {
36 ttDELEGATE_SET,
37 ttDEPOSIT_PREAUTH,
38 ttPAYMENT,
39 ttSIGNER_LIST_SET,
40 ttCHECK_CANCEL,
41 ttCHECK_CASH,
42 ttCHECK_CREATE,
43 ttESCROW_CANCEL,
44 ttESCROW_CREATE,
45 ttESCROW_FINISH,
46 ttPAYCHAN_CLAIM,
47 ttPAYCHAN_CREATE,
48 ttPAYCHAN_FUND,
49 ttCLAWBACK,
50 ttMPTOKEN_AUTHORIZE,
51 ttMPTOKEN_ISSUANCE_CREATE,
52 ttMPTOKEN_ISSUANCE_DESTROY,
53 ttMPTOKEN_ISSUANCE_SET,
54 ttTRUST_SET,
55 ttCREDENTIAL_ACCEPT,
56 ttCREDENTIAL_CREATE,
57 ttCREDENTIAL_DELETE,
58 ttACCOUNT_SET,
59 ttREGULAR_KEY_SET,
60 ttSPONSORSHIP_TRANSFER,
61 ttTRANSACTION_PROPOSAL_CREATE,
62 };
63 return kReserveSponsorAllowed.contains(txType);
64}
65
68{
69 if (tx.isFieldPresent(sfSponsor) && isReserveSponsored(tx))
70 {
71 XRPL_ASSERT(
72 getCurrentTransactionRules()->enabled( // NOLINT(bugprone-unchecked-optional-access)
73 featureSponsor),
74 "xrpl::getTxReserveSponsorID : sponsor exists + Sponsor enabled");
75 return tx.getAccountID(sfSponsor);
76 }
77 return {};
78}
79
80std::expected<SLE::pointer, TER>
82{
83 auto const sponsorID = getTxReserveSponsorID(ctx.tx);
84 if (sponsorID)
85 {
86 XRPL_ASSERT(
87 ctx.view.rules().enabled(featureSponsor),
88 "xrpl::getTxReserveSponsor : sponsor exists + Sponsor enabled");
89 auto sle = ctx.view.peek(keylet::account(*sponsorID));
90
91 // already checked in Transactor::checkSponsor
92 if (!sle)
93 return std::unexpected(tecINTERNAL); // LCOV_EXCL_LINE
94 return sle;
95 }
96 return SLE::pointer();
97}
98
99std::expected<SLE::const_pointer, TER>
100getTxReserveSponsor(ReadView const& view, STTx const& tx)
101{
102 auto const sponsorID = getTxReserveSponsorID(tx);
103 if (sponsorID)
104 {
105 XRPL_ASSERT(
106 view.rules().enabled(featureSponsor),
107 "xrpl::getTxReserveSponsor : sponsor exists + Sponsor enabled");
108 auto sle = view.read(keylet::account(*sponsorID));
109
110 // already checked in Transactor::checkSponsor
111 if (!sle)
112 return std::unexpected(tecINTERNAL); // LCOV_EXCL_LINE
113 return sle;
114 }
115 return SLE::pointer();
116}
117
118std::expected<SLE::pointer, TER>
120{
121 // A reserve sponsor only covers tx.Account's own objects.
122 if (ctx.view.rules().enabled(fixCleanup3_2_0))
123 {
124 XRPL_ASSERT(
125 accountSle && accountSle->getType() == ltACCOUNT_ROOT,
126 "xrpl::getEffectiveTxReserveSponsor : accountSle exists and is account type");
127 }
128 else
129 {
130 XRPL_ASSERT(
131 accountSle &&
132 ((accountSle->getType() == ltACCOUNT_ROOT) || (accountSle->getType() == ltESCROW)),
133 "xrpl::getEffectiveTxReserveSponsor : accountSle exists and is account type");
134 }
135
136 if (isPseudoAccount(accountSle) || accountSle->getAccountID(sfAccount) != ctx.tx[sfAccount])
137 return SLE::pointer();
138 return getTxReserveSponsor(ctx);
139}
140
143{
144 XRPL_ASSERT(
145 (sle &&
146 ((sle->getType() == ltRIPPLE_STATE && (field == sfHighSponsor || field == sfLowSponsor)) ||
147 (sle->getType() != ltRIPPLE_STATE && field == sfSponsor))),
148 "xrpl::getLedgerEntryReserveSponsorID : correct sfield");
149
150 if (sle->isFieldPresent(field))
151 return sle->getAccountID(field);
152 return {};
153}
154
157{
158 auto const sponsorID = getLedgerEntryReserveSponsorID(sle, field);
159 if (sponsorID)
160 {
161 XRPL_ASSERT(
162 view.rules().enabled(featureSponsor),
163 "xrpl::getLedgerEntryReserveSponsor : sponsor exists + Sponsor enabled");
164 return view.peek(keylet::account(*sponsorID));
165 }
166 return {};
167}
168
169void
171{
172 XRPL_ASSERT(
173 (sle->getType() == ltRIPPLE_STATE && (field == sfHighSponsor || field == sfLowSponsor)) ||
174 (sle->getType() != ltRIPPLE_STATE && field == sfSponsor),
175 "addSponsorToLedgerEntry : Invalid field to the LedgerEntry");
176 if (sponsorSle)
177 {
178 XRPL_ASSERT(
179 getCurrentTransactionRules()->enabled( // NOLINT(bugprone-unchecked-optional-access)
180 featureSponsor),
181 "xrpl::addSponsorToLedgerEntry : sponsor exists + Sponsor enabled");
182 sle->setAccountID(field, sponsorSle->getAccountID(sfAccount));
183 }
184}
185
186void
188{
189 // getTxReserveSponsor yields a null pointer when the tx is not
190 // reserve-sponsored, so addSponsorToLedgerEntry becomes a no-op then. The
191 // error case (tecINTERNAL) is an already-checked invariant; skip stamping.
192 auto const sponsorSle = getTxReserveSponsor(ctx);
193 if (sponsorSle && *sponsorSle)
194 {
195 XRPL_ASSERT(
196 ctx.view.rules().enabled(featureSponsor),
197 "xrpl::addSponsorToLedgerEntry : sponsor exists + Sponsor enabled");
198 addSponsorToLedgerEntry(sle, *sponsorSle, field);
199 }
200}
201
202void
204{
205 XRPL_ASSERT(
206 (sle->getType() == ltRIPPLE_STATE && (field == sfHighSponsor || field == sfLowSponsor)) ||
207 (sle->getType() != ltRIPPLE_STATE && field == sfSponsor),
208 "removeSponsorFromLedgerEntry : Invalid field to the LedgerEntry");
209 if (sle->isFieldPresent(field))
210 {
211 XRPL_ASSERT(
212 getCurrentTransactionRules()->enabled( // NOLINT(bugprone-unchecked-optional-access)
213 featureSponsor),
214 "xrpl::removeSponsorFromLedgerEntry : sponsor exists + Sponsor enabled");
215 sle->makeFieldAbsent(field);
216 }
217}
218
219bool
220isLedgerEntryOwner(ReadView const& view, SLE const& sle, AccountID const& account)
221{
222 switch (sle.getType())
223 {
224 case ltCHECK:
225 case ltESCROW:
226 case ltPAYCHAN:
227 case ltMPTOKEN:
228 case ltDELEGATE:
229 case ltDEPOSIT_PREAUTH:
230 return sle.getAccountID(sfAccount) == account;
231 case ltMPTOKEN_ISSUANCE:
232 return sle.getAccountID(sfIssuer) == account;
233 case ltSIGNER_LIST: {
234 auto const signerList = view.read(keylet::signerList(account));
235 if (!signerList)
236 return false;
237 return signerList->key() == sle.key();
238 }
239 case ltCREDENTIAL: {
240 auto const& ownerField = sle.isFlag(lsfAccepted) ? sfSubject : sfIssuer;
241 return sle.getAccountID(ownerField) == account;
242 }
243 case ltRIPPLE_STATE: {
244 if (sle.isFlag(lsfHighReserve))
245 {
246 auto const highAccount = sle.getFieldAmount(sfHighLimit).getIssuer();
247 if (highAccount == account)
248 return true;
249 }
250 if (sle.isFlag(lsfLowReserve))
251 {
252 auto const lowAccount = sle.getFieldAmount(sfLowLimit).getIssuer();
253 if (lowAccount == account)
254 return true;
255 }
256 // Reachable: the sponsee may be a third party or the side of the
257 // line that holds no reserve (e.g. the issuer). Callers map this
258 // to tecNO_PERMISSION.
259 return false;
260 }
261 case ltTRANSACTION_PROPOSAL:
262 return sle.getAccountID(sfOwner) == account;
263 default:
264 // LCOV_EXCL_START
265 UNREACHABLE("xrpl::isLedgerEntryOwner : object is not supported by sponsorship.");
266 return false;
267 // LCOV_EXCL_STOP
268 };
269}
270
271bool
273{
274 switch (sle.getType())
275 {
276 case ltCHECK:
277 case ltESCROW:
278 case ltPAYCHAN:
279 case ltMPTOKEN:
280 case ltDELEGATE:
281 case ltDEPOSIT_PREAUTH:
282 case ltMPTOKEN_ISSUANCE:
283 case ltSIGNER_LIST:
284 case ltCREDENTIAL:
285 case ltRIPPLE_STATE:
286 case ltTRANSACTION_PROPOSAL:
287 return true;
288 default:
289 return false;
290 };
291}
292
295{
296 switch (sle.getType())
297 {
298 case ltORACLE: {
299 return calculateOracleReserve(sle.getFieldArray(sfPriceDataSeries));
300 }
301 // Vaults require 2 owner counts (the vault and a pseudo-account)
302 case ltVAULT:
303 return 2;
304 case ltSIGNER_LIST: {
305 // Mirror SignerListSet's owner-count accounting so that create and
306 // delete agree. Modern lists (post-MultiSignReserve) carry the
307 // lsfOneOwnerCount flag and cost a single owner count. Legacy
308 // pre-MultiSignReserve lists cost 2 + signer_count owner counts
309 if (sle.isFlag(lsfOneOwnerCount))
310 return 1;
311 return 2 + static_cast<std::uint32_t>(sle.getFieldArray(sfSignerEntries).size());
312 }
313 case ltTRANSACTION_PROPOSAL:
314 // Mirror TransactionProposalCreate's own reserve sizing so that
315 // creation and sponsorship accounting agree: a proposed Batch
316 // reserves more than an ordinary proposal.
317 return proposal::proposalOwnerCount(sle.getFieldObject(sfProposedTransaction));
318 case ltACCOUNT_ROOT:
319 // LCOV_EXCL_START
320 UNREACHABLE("AccountRoots are not supported by object sponsorship.");
321 return 0;
322 // LCOV_EXCL_STOP
323 case ltESCROW:
324 return calculateAdditionalReserve(sle[~sfBytecode]);
325 default:
326 return 1;
327 }
328}
329
330SF_ACCOUNT const&
331getLedgerEntrySponsorField(SLE const& sle, AccountID const& owner)
332{
333 switch (sle.getType())
334 {
335 case ltRIPPLE_STATE: {
336 if (sle.isFlag(lsfHighReserve))
337 {
338 auto const highAccount = sle.getFieldAmount(sfHighLimit).getIssuer();
339 if (highAccount == owner)
340 return sfHighSponsor;
341 }
342 if (sle.isFlag(lsfLowReserve))
343 {
344 auto const lowAccount = sle.getFieldAmount(sfLowLimit).getIssuer();
345 if (lowAccount == owner)
346 return sfLowSponsor;
347 }
348 // LCOV_EXCL_START
349 UNREACHABLE("xrpl::getLedgerEntrySponsorField : unknown owner for RippleState");
350 return sfSponsor;
351 // LCOV_EXCL_STOP
352 }
353 default:
354 return sfSponsor;
355 }
356}
357
358} // namespace xrpl
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:141
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
A view into a ledger.
Definition ReadView.h:41
virtual Rules const & rules() const =0
Returns the tx processing rules.
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:182
AccountID const & getIssuer() const
Definition STAmount.h:516
UInt256 const & key() const
Returns the 'key' (or 'index') of this item.
std::shared_ptr< STLedgerEntry > pointer
LedgerEntryType getType() const
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::shared_ptr< STLedgerEntry > const & Ref
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:688
bool isFlag(std::uint32_t) const
Definition STObject.cpp:511
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
STObject getFieldObject(SField const &field) const
Definition STObject.cpp:678
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:643
STAmount const & getFieldAmount(SField const &field) const
Definition STObject.cpp:657
T contains(T... args)
Keylet signerList(AccountID const &account) noexcept
A SignerList.
Definition Indexes.cpp:360
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
std::uint32_t proposalOwnerCount(STObject const &proposedTx)
Owner-reserve increments held by a proposal of the given transaction.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::uint32_t calculateOracleReserve(T const &priceDataSeries)
void removeSponsorFromLedgerEntry(SLE::Ref sle, SF_ACCOUNT const &field=sfSponsor)
Remove the reserve sponsor field from a ledger entry.
TxType
Transaction type identifiers.
Definition TxFormats.h:45
int32_t calculateAdditionalReserve(T const &finishFunction)
Owner count an escrow costs.
std::optional< AccountID > getLedgerEntryReserveSponsorID(SLE::ConstRef sle, SF_ACCOUNT const &field=sfSponsor)
Return the AccountID stored in the given sponsor field of a ledger entry, or nullopt if absent.
std::optional< Rules > const & getCurrentTransactionRules()
Definition Rules.cpp:30
std::expected< SLE::pointer, TER > getEffectiveTxReserveSponsor(ApplyViewContext ctx, SLE::ConstRef accountSle)
The transaction's reserve sponsor for the given account, if applicable.
void addSponsorToLedgerEntry(SLE::Ref sle, SLE::ConstRef sponsorSle, SF_ACCOUNT const &field=sfSponsor)
Stamp a reserve sponsor onto a ledger entry using an explicit sponsor SLE.
bool isReserveSponsored(STTx const &tx)
Whether the transaction's reserve is sponsored (sfSponsor present + spfSponsorReserve set).
STLedgerEntry SLE
bool isReserveSponsorAllowed(TxType txType)
Whether the given transaction type may use reserve sponsorship (v1).
std::expected< SLE::pointer, TER > getTxReserveSponsor(ApplyViewContext ctx)
Return a mutable SLE for the transaction's reserve sponsor account.
std::uint32_t getLedgerEntryOwnerCount(SLE const &sle)
Return the number of owner-count units the ledger entry consumes.
bool isPseudoAccount(SLE::const_pointer sleAcct)
Returns true if and only if sleAcct is a pseudo-account of any kind (i.e.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
SLE::pointer getLedgerEntryReserveSponsor(ApplyView &view, SLE::ConstRef sle, SF_ACCOUNT const &field=sfSponsor)
Return a mutable SLE for the reserve sponsor recorded on a ledger entry.
@ tecINTERNAL
Definition TER.h:321
std::optional< AccountID > getTxReserveSponsorID(STTx const &tx)
Return the AccountID of the transaction's reserve sponsor, or nullopt if unsponsored.
TypedField< STAccount > SF_ACCOUNT
Definition SField.h:356
bool isLedgerEntryOwner(ReadView const &view, SLE const &sle, AccountID const &account)
Whether account is the owner of a ledger entry for sponsorship purposes.
SF_ACCOUNT const & getLedgerEntrySponsorField(SLE const &sle, AccountID const &owner)
Return the SField used to store the reserve sponsor for owner on sle.
bool isLedgerEntrySupportedBySponsorship(SLE const &sle)
Whether this ledger entry type can have a reserve sponsor attached to it.
Bundles the mutable ledger view and the transaction being applied.
Definition ApplyView.h:451
UInt256 key
Definition Keylet.h:21
T unexpected(T... args)