rippled
Loading...
Searching...
No Matches
apply.cpp
1#include <xrpl/basics/Log.h>
2#include <xrpl/core/HashRouter.h>
3#include <xrpl/core/ServiceRegistry.h>
4#include <xrpl/protocol/Feature.h>
5#include <xrpl/protocol/TxFlags.h>
6#include <xrpl/tx/apply.h>
7#include <xrpl/tx/applySteps.h>
8
9namespace xrpl {
10
11// These are the same flags defined as HashRouterFlags::PRIVATE1-4 in
12// HashRouter.h
13constexpr HashRouterFlags SF_SIGBAD = HashRouterFlags::PRIVATE1; // Signature is bad
14constexpr HashRouterFlags SF_SIGGOOD = HashRouterFlags::PRIVATE2; // Signature is good
15constexpr HashRouterFlags SF_LOCALBAD = HashRouterFlags::PRIVATE3; // Local checks failed
16constexpr HashRouterFlags SF_LOCALGOOD = HashRouterFlags::PRIVATE4; // Local checks passed
17
18//------------------------------------------------------------------------------
19
21checkValidity(HashRouter& router, STTx const& tx, Rules const& rules)
22{
23 auto const id = tx.getTransactionID();
24 auto const flags = router.getFlags(id);
25
26 // Ignore signature check on batch inner transactions
27 if (tx.isFlag(tfInnerBatchTxn) && rules.enabled(featureBatch))
28 {
29 // Defensive Check: These values are also checked in Batch::preflight
30 if (tx.isFieldPresent(sfTxnSignature) || !tx.getSigningPubKey().empty() || tx.isFieldPresent(sfSigners))
31 return {Validity::SigBad, "Malformed: Invalid inner batch transaction."};
32
33 // This block should probably have never been included in the
34 // original `Batch` implementation. An inner transaction never
35 // has a valid signature.
36 bool const neverValid = rules.enabled(fixBatchInnerSigs);
37 if (!neverValid)
38 {
39 std::string reason;
40 if (!passesLocalChecks(tx, reason))
41 {
42 router.setFlags(id, SF_LOCALBAD);
43 return {Validity::SigGoodOnly, reason};
44 }
45
46 router.setFlags(id, SF_SIGGOOD);
47 return {Validity::Valid, ""};
48 }
49 }
50
51 if (any(flags & SF_SIGBAD))
52 // Signature is known bad
53 return {Validity::SigBad, "Transaction has bad signature."};
54
55 if (!any(flags & SF_SIGGOOD))
56 {
57 auto const sigVerify = tx.checkSign(rules);
58 if (!sigVerify)
59 {
60 router.setFlags(id, SF_SIGBAD);
61 return {Validity::SigBad, sigVerify.error()};
62 }
63 router.setFlags(id, SF_SIGGOOD);
64 }
65
66 // Signature is now known good
67 if (any(flags & SF_LOCALBAD))
68 // ...but the local checks
69 // are known bad.
70 return {Validity::SigGoodOnly, "Local checks failed."};
71
72 if (any(flags & SF_LOCALGOOD))
73 // ...and the local checks
74 // are known good.
75 return {Validity::Valid, ""};
76
77 // Do the local checks
78 std::string reason;
79 if (!passesLocalChecks(tx, reason))
80 {
81 router.setFlags(id, SF_LOCALBAD);
82 return {Validity::SigGoodOnly, reason};
83 }
84 router.setFlags(id, SF_LOCALGOOD);
85 return {Validity::Valid, ""};
86}
87
88void
89forceValidity(HashRouter& router, uint256 const& txid, Validity validity)
90{
92 switch (validity)
93 {
94 case Validity::Valid:
95 flags |= SF_LOCALGOOD;
96 [[fallthrough]];
98 flags |= SF_SIGGOOD;
99 [[fallthrough]];
100 case Validity::SigBad:
101 // would be silly to call directly
102 break;
103 }
104 if (any(flags))
105 router.setFlags(txid, flags);
106}
107
108template <typename PreflightChecks>
109ApplyResult
110apply(ServiceRegistry& registry, OpenView& view, PreflightChecks&& preflightChecks)
111{
112 NumberSO stNumberSO{view.rules().enabled(fixUniversalNumber)};
113 return doApply(preclaim(preflightChecks(), registry, view), registry, view);
114}
115
116ApplyResult
117apply(ServiceRegistry& registry, OpenView& view, STTx const& tx, ApplyFlags flags, beast::Journal j)
118{
119 return apply(registry, view, [&]() mutable { return preflight(registry, view.rules(), tx, flags, j); });
120}
121
122ApplyResult
124 ServiceRegistry& registry,
125 OpenView& view,
126 uint256 const& parentBatchId,
127 STTx const& tx,
128 ApplyFlags flags,
130{
131 return apply(
132 registry, view, [&]() mutable { return preflight(registry, view.rules(), parentBatchId, tx, flags, j); });
133}
134
135static bool
136applyBatchTransactions(ServiceRegistry& registry, OpenView& batchView, STTx const& batchTxn, beast::Journal j)
137{
138 XRPL_ASSERT(
139 batchTxn.getTxnType() == ttBATCH && batchTxn.getFieldArray(sfRawTransactions).size() != 0,
140 "Batch transaction missing sfRawTransactions");
141
142 auto const parentBatchId = batchTxn.getTransactionID();
143 auto const mode = batchTxn.getFlags();
144
145 auto applyOneTransaction = [&registry, &j, &parentBatchId, &batchView](STTx&& tx) {
146 OpenView perTxBatchView(batch_view, batchView);
147
148 auto const ret = apply(registry, perTxBatchView, parentBatchId, tx, tapBATCH, j);
149 XRPL_ASSERT(
150 ret.applied == (isTesSuccess(ret.ter) || isTecClaim(ret.ter)), "Inner transaction should not be applied");
151
152 JLOG(j.debug()) << "BatchTrace[" << parentBatchId << "]: " << tx.getTransactionID() << " "
153 << (ret.applied ? "applied" : "failure") << ": " << transToken(ret.ter);
154
155 // If the transaction should be applied push its changes to the
156 // whole-batch view.
157 if (ret.applied && (isTesSuccess(ret.ter) || isTecClaim(ret.ter)))
158 perTxBatchView.apply(batchView);
159
160 return ret;
161 };
162
163 int applied = 0;
164
165 for (STObject rb : batchTxn.getFieldArray(sfRawTransactions))
166 {
167 auto const result = applyOneTransaction(STTx{std::move(rb)});
168 XRPL_ASSERT(
169 result.applied == (isTesSuccess(result.ter) || isTecClaim(result.ter)),
170 "Outer Batch failure, inner transaction should not be applied");
171
172 if (result.applied)
173 ++applied;
174
175 if (!isTesSuccess(result.ter))
176 {
177 if (mode & tfAllOrNothing)
178 return false;
179
180 if (mode & tfUntilFailure)
181 break;
182 }
183 else if (mode & tfOnlyOne)
184 break;
185 }
186
187 return applied != 0;
188}
189
192 ServiceRegistry& registry,
193 OpenView& view,
194 STTx const& txn,
195 bool retryAssured,
196 ApplyFlags flags,
198{
199 // Returns false if the transaction has need not be retried.
200 if (retryAssured)
201 flags = flags | tapRETRY;
202
203 JLOG(j.debug()) << "TXN " << txn.getTransactionID() << (retryAssured ? "/retry" : "/final");
204
205 try
206 {
207 auto const result = apply(registry, view, txn, flags, j);
208
209 if (result.applied)
210 {
211 JLOG(j.debug()) << "Transaction applied: " << transToken(result.ter);
212
213 // The batch transaction was just applied; now we need to apply
214 // its inner transactions as necessary.
215 if (isTesSuccess(result.ter) && txn.getTxnType() == ttBATCH)
216 {
217 OpenView wholeBatchView(batch_view, view);
218
219 if (applyBatchTransactions(registry, wholeBatchView, txn, j))
220 wholeBatchView.apply(view);
221 }
222
224 }
225
226 if (isTefFailure(result.ter) || isTemMalformed(result.ter) || isTelLocal(result.ter))
227 {
228 // failure
229 JLOG(j.debug()) << "Transaction failure: " << transHuman(result.ter);
231 }
232
233 JLOG(j.debug()) << "Transaction retry: " << transHuman(result.ter);
235 }
236 catch (std::exception const& ex)
237 {
238 JLOG(j.warn()) << "Throws: " << ex.what();
240 }
241}
242
243} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream debug() const
Definition Journal.h:300
Stream warn() const
Definition Journal.h:312
Routing table for objects identified by hash.
Definition HashRouter.h:77
HashRouterFlags getFlags(uint256 const &key)
bool setFlags(uint256 const &key, HashRouterFlags flags)
Set the flags on a hash.
RAII class to set and restore the Number switchover.
Definition IOUAmount.h:192
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:45
void apply(TxsRawView &to) const
Apply changes.
Definition OpenView.cpp:101
Rules const & rules() const override
Returns the tx processing rules.
Definition OpenView.cpp:123
Rules controlling protocol behavior.
Definition Rules.h:18
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:118
size_type size() const
Definition STArray.h:223
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:663
bool isFlag(std::uint32_t) const
Definition STObject.cpp:486
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:439
std::uint32_t getFlags() const
Definition STObject.cpp:492
Expected< void, std::string > checkSign(Rules const &rules) const
Check the signature.
Definition STTx.cpp:254
TxType getTxnType() const
Definition STTx.h:180
Blob getSigningPubKey() const
Definition STTx.h:186
uint256 getTransactionID() const
Definition STTx.h:192
Service registry for dependency injection.
T empty(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr struct xrpl::batch_view_t batch_view
PreflightResult preflight(ServiceRegistry &registry, Rules const &rules, STTx const &tx, ApplyFlags flags, beast::Journal j)
Gate a transaction based on static information.
PreclaimResult preclaim(PreflightResult const &preflightResult, ServiceRegistry &registry, OpenView const &view)
Gate a transaction based on static ledger information.
Validity
Describes the pre-processing validity of a transaction.
Definition apply.h:19
@ SigBad
Signature is bad. Didn't do local checks.
@ Valid
Signature and local checks are good / passed.
@ SigGoodOnly
Signature is good, but local checks fail.
ApplyResult apply(ServiceRegistry &registry, OpenView &view, STTx const &tx, ApplyFlags flags, beast::Journal journal)
Apply a transaction to an OpenView.
Definition apply.cpp:117
constexpr std::uint32_t tfInnerBatchTxn
Definition TxFlags.h:41
constexpr HashRouterFlags SF_LOCALBAD
Definition apply.cpp:15
ApplyTransactionResult applyTransaction(ServiceRegistry &registry, OpenView &view, STTx const &tx, bool retryAssured, ApplyFlags flags, beast::Journal journal)
Transaction application helper.
Definition apply.cpp:191
std::pair< Validity, std::string > checkValidity(HashRouter &router, STTx const &tx, Rules const &rules)
Checks transaction signature and local checks.
Definition apply.cpp:21
ApplyTransactionResult
Enum class for return value from applyTransaction
Definition apply.h:104
@ Success
Applied to this ledger.
@ Retry
Should be retried in this ledger.
@ Fail
Should not be retried in this ledger.
std::string transHuman(TER code)
Definition TER.cpp:252
constexpr HashRouterFlags SF_SIGBAD
Definition apply.cpp:13
constexpr std::uint32_t tfAllOrNothing
Definition TxFlags.h:256
std::string transToken(TER code)
Definition TER.cpp:243
constexpr HashRouterFlags SF_SIGGOOD
Definition apply.cpp:14
bool isTefFailure(TER x) noexcept
Definition TER.h:631
bool passesLocalChecks(STObject const &st, std::string &)
Definition STTx.cpp:737
HashRouterFlags
Definition HashRouter.h:14
constexpr HashRouterFlags SF_LOCALGOOD
Definition apply.cpp:16
static bool applyBatchTransactions(ServiceRegistry &registry, OpenView &batchView, STTx const &batchTxn, beast::Journal j)
Definition apply.cpp:136
constexpr std::uint32_t tfOnlyOne
Definition TxFlags.h:257
ApplyFlags
Definition ApplyView.h:10
@ tapRETRY
Definition ApplyView.h:19
@ tapBATCH
Definition ApplyView.h:25
bool isTelLocal(TER x) noexcept
Definition TER.h:619
bool isTesSuccess(TER x) noexcept
Definition TER.h:643
constexpr std::uint32_t tfUntilFailure
Definition TxFlags.h:258
bool isTecClaim(TER x) noexcept
Definition TER.h:650
void forceValidity(HashRouter &router, uint256 const &txid, Validity validity)
Sets the validity of a given transaction in the cache.
Definition apply.cpp:89
ApplyResult doApply(PreclaimResult const &preclaimResult, ServiceRegistry &registry, OpenView &view)
Apply a prechecked transaction to an OpenView.
bool isTemMalformed(TER x) noexcept
Definition TER.h:625
T what(T... args)