xrpld
Loading...
Searching...
No Matches
LedgerStateFix.cpp
1#include <xrpl/tx/transactors/system/LedgerStateFix.h>
2
3#include <xrpl/beast/utility/Journal.h>
4#include <xrpl/ledger/ReadView.h>
5#include <xrpl/ledger/helpers/NFTokenHelpers.h>
6#include <xrpl/protocol/AccountID.h>
7#include <xrpl/protocol/Feature.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/LedgerFormats.h>
10#include <xrpl/protocol/SField.h>
11#include <xrpl/protocol/STLedgerEntry.h>
12#include <xrpl/protocol/STTx.h>
13#include <xrpl/protocol/TER.h>
14#include <xrpl/protocol/XRPAmount.h>
15#include <xrpl/tx/Transactor.h>
16
17#include <algorithm>
18#include <array>
19#include <utility>
20
21namespace xrpl {
22
23namespace {
24
25using FixType = LedgerStateFix::FixType;
26
27std::array<std::pair<FixType, SField const*>, 2> const kLedgerFixFields = {
28 {
29 {FixType::NfTokenPageLink, &sfOwner},
30 {FixType::BookExchangeRate, &sfBookDirectory},
31 },
32};
33
34[[nodiscard]] SField const*
35fixField(FixType const fixType)
36{
37 auto const iter = std::ranges::find_if(
38 kLedgerFixFields, [fixType](auto const& entry) { return entry.first == fixType; });
39
40 if (iter == kLedgerFixFields.end())
41 return nullptr; // LCOV_EXCL_LINE
42
43 return iter->second;
44}
45
46[[nodiscard]] bool
47hasUnexpectedFixField(STTx const& tx, SField const& expected)
48{
49 return std::ranges::any_of(kLedgerFixFields, [&tx, &expected](auto const& entry) {
50 auto const field = entry.second;
51 return field != &expected && tx.isFieldPresent(*field);
52 });
53}
54
55} // namespace
56
59{
60 auto const fixType = static_cast<FixType>(ctx.tx[sfLedgerFixType]);
61
62 switch (fixType)
63 {
65 break;
66
68 if (!ctx.rules.enabled(fixCleanup3_2_0))
69 return temDISABLED;
70 break;
71
72 default:
74 }
75
76 auto const expectedField = fixField(fixType);
77 if (expectedField == nullptr)
78 return tefINVALID_LEDGER_FIX_TYPE; // LCOV_EXCL_LINE
79
80 // Each fix type allows exactly one fix-specific field.
81 if (!ctx.tx.isFieldPresent(*expectedField) || hasUnexpectedFixField(ctx.tx, *expectedField))
82 return temINVALID;
83
84 return tesSUCCESS;
85}
86
89{
90 // The fee required for LedgerStateFix is one owner reserve, just like
91 // the fee for AccountDelete.
93}
94
95TER
97{
98 if (static_cast<FixType>(ctx.tx[sfLedgerFixType]) == FixType::NfTokenPageLink)
99 {
100 AccountID const owner{ctx.tx[sfOwner]};
101 if (!ctx.view.read(keylet::account(owner)))
102 return tecOBJECT_NOT_FOUND;
103
104 return tesSUCCESS;
105 }
106
107 if (static_cast<FixType>(ctx.tx[sfLedgerFixType]) == FixType::BookExchangeRate)
108 {
109 auto const dirKey = ctx.tx.getFieldH256(sfBookDirectory);
110 auto const sle = ctx.view.read(Keylet(ltDIR_NODE, dirKey));
111 if (!sle)
112 return tecOBJECT_NOT_FOUND;
113
114 // Must be the first page of a book directory (has sfExchangeRate).
115 if (!sle->isFieldPresent(sfExchangeRate))
116 return tecNO_PERMISSION;
117
118 // ExchangeRate is already correct, nothing to fix.
119 if (getQuality(sle->key()) == sle->getFieldU64(sfExchangeRate))
120 return tecNO_PERMISSION;
121
122 return tesSUCCESS;
123 }
124
125 // preflight is supposed to verify that only valid FixTypes get to preclaim.
126 return tecINTERNAL; // LCOV_EXCL_LINE
127}
128
129TER
131{
132 if (static_cast<FixType>(ctx_.tx[sfLedgerFixType]) == FixType::NfTokenPageLink)
133 {
134 if (!nft::repairNFTokenDirectoryLinks(view(), ctx_.tx[sfOwner]))
136
137 return tesSUCCESS;
138 }
139
140 if (static_cast<FixType>(ctx_.tx[sfLedgerFixType]) == FixType::BookExchangeRate)
141 {
142 auto const dirKey = ctx_.tx.getFieldH256(sfBookDirectory);
143 auto const sle = view().peek(Keylet(ltDIR_NODE, dirKey));
144 if (!sle)
145 return tecINTERNAL; // LCOV_EXCL_LINE
146
147 sle->setFieldU64(sfExchangeRate, getQuality(sle->key()));
148 view().update(sle);
149 return tesSUCCESS;
150 }
151
152 // preflight is supposed to verify that only valid FixTypes get to doApply.
153 return tecINTERNAL; // LCOV_EXCL_LINE
154}
155
156void
158{
159 // No transaction-specific invariants yet (future work).
160}
161
162bool
164 STTx const&,
165 TER,
166 XRPAmount,
167 ReadView const&,
168 beast::Journal const&)
169{
170 // No transaction-specific invariants yet (future work).
171 return true;
172}
173
174} // namespace xrpl
T any_of(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void update(SLE::Ref sle)=0
Indicate changes to a peeked SLE.
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.
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
A view into a ledger.
Definition ReadView.h:41
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
Identifies fields.
Definition SField.h:131
std::shared_ptr< STLedgerEntry const > const & ConstRef
UInt256 getFieldH256(SField const &field) const
Definition STObject.cpp:631
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
static XRPAmount calculateOwnerReserveFee(ReadView const &view, STTx const &tx)
ApplyView & view()
Definition Transactor.h:184
ApplyContext & ctx_
Definition Transactor.h:162
T end(T... args)
T find_if(T... args)
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
bool repairNFTokenDirectoryLinks(ApplyView &view, AccountID const &owner)
Repairs the links in an NFTokenPage directory.
json::Value entry(jtx::Env &env, jtx::Account const &account, jtx::Account const &authorize)
Definition delegate.cpp:41
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::uint64_t getQuality(UInt256 const &uBase)
Definition Indexes.cpp:207
@ tefINVALID_LEDGER_FIX_TYPE
Definition TER.h:181
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
@ temINVALID
Definition TER.h:98
@ temDISABLED
Definition TER.h:102
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecOBJECT_NOT_FOUND
Definition TER.h:337
@ tecINTERNAL
Definition TER.h:321
@ tecFAILED_PROCESSING
Definition TER.h:297
@ tecNO_PERMISSION
Definition TER.h:316
@ tesSUCCESS
Definition TER.h:253
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:92
ReadView const & view
Definition Transactor.h:95
State information when preflighting a tx.
Definition Transactor.h:39