rippled
Loading...
Searching...
No Matches
apply.cpp
1#include <xrpld/app/misc/HashRouter.h>
2#include <xrpld/app/tx/apply.h>
3#include <xrpld/app/tx/applySteps.h>
4
5#include <xrpl/basics/Log.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/TxFlags.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, Config const& config)
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(Application& app, OpenView& view, PreflightChecks&& preflightChecks)
111{
112 NumberSO stNumberSO{view.rules().enabled(fixUniversalNumber)};
113 return doApply(preclaim(preflightChecks(), app, view), app, view);
114}
115
116ApplyResult
117apply(Application& app, OpenView& view, STTx const& tx, ApplyFlags flags, beast::Journal j)
118{
119 return apply(app, view, [&]() mutable { return preflight(app, view.rules(), tx, flags, j); });
120}
121
122ApplyResult
124 Application& app,
125 OpenView& view,
126 uint256 const& parentBatchId,
127 STTx const& tx,
128 ApplyFlags flags,
130{
131 return apply(app, view, [&]() mutable { return preflight(app, view.rules(), parentBatchId, tx, flags, j); });
132}
133
134static bool
135applyBatchTransactions(Application& app, OpenView& batchView, STTx const& batchTxn, beast::Journal j)
136{
137 XRPL_ASSERT(
138 batchTxn.getTxnType() == ttBATCH && batchTxn.getFieldArray(sfRawTransactions).size() != 0,
139 "Batch transaction missing sfRawTransactions");
140
141 auto const parentBatchId = batchTxn.getTransactionID();
142 auto const mode = batchTxn.getFlags();
143
144 auto applyOneTransaction = [&app, &j, &parentBatchId, &batchView](STTx&& tx) {
145 OpenView perTxBatchView(batch_view, batchView);
146
147 auto const ret = apply(app, perTxBatchView, parentBatchId, tx, tapBATCH, j);
148 XRPL_ASSERT(
149 ret.applied == (isTesSuccess(ret.ter) || isTecClaim(ret.ter)), "Inner transaction should not be applied");
150
151 JLOG(j.debug()) << "BatchTrace[" << parentBatchId << "]: " << tx.getTransactionID() << " "
152 << (ret.applied ? "applied" : "failure") << ": " << transToken(ret.ter);
153
154 // If the transaction should be applied push its changes to the
155 // whole-batch view.
156 if (ret.applied && (isTesSuccess(ret.ter) || isTecClaim(ret.ter)))
157 perTxBatchView.apply(batchView);
158
159 return ret;
160 };
161
162 int applied = 0;
163
164 for (STObject rb : batchTxn.getFieldArray(sfRawTransactions))
165 {
166 auto const result = applyOneTransaction(STTx{std::move(rb)});
167 XRPL_ASSERT(
168 result.applied == (isTesSuccess(result.ter) || isTecClaim(result.ter)),
169 "Outer Batch failure, inner transaction should not be applied");
170
171 if (result.applied)
172 ++applied;
173
174 if (!isTesSuccess(result.ter))
175 {
176 if (mode & tfAllOrNothing)
177 return false;
178
179 if (mode & tfUntilFailure)
180 break;
181 }
182 else if (mode & tfOnlyOne)
183 break;
184 }
185
186 return applied != 0;
187}
188
191 Application& app,
192 OpenView& view,
193 STTx const& txn,
194 bool retryAssured,
195 ApplyFlags flags,
197{
198 // Returns false if the transaction has need not be retried.
199 if (retryAssured)
200 flags = flags | tapRETRY;
201
202 JLOG(j.debug()) << "TXN " << txn.getTransactionID() << (retryAssured ? "/retry" : "/final");
203
204 try
205 {
206 auto const result = apply(app, view, txn, flags, j);
207
208 if (result.applied)
209 {
210 JLOG(j.debug()) << "Transaction applied: " << transToken(result.ter);
211
212 // The batch transaction was just applied; now we need to apply
213 // its inner transactions as necessary.
214 if (isTesSuccess(result.ter) && txn.getTxnType() == ttBATCH)
215 {
216 OpenView wholeBatchView(batch_view, view);
217
218 if (applyBatchTransactions(app, wholeBatchView, txn, j))
219 wholeBatchView.apply(view);
220 }
221
223 }
224
225 if (isTefFailure(result.ter) || isTemMalformed(result.ter) || isTelLocal(result.ter))
226 {
227 // failure
228 JLOG(j.debug()) << "Transaction failure: " << transHuman(result.ter);
230 }
231
232 JLOG(j.debug()) << "Transaction retry: " << transHuman(result.ter);
234 }
235 catch (std::exception const& ex)
236 {
237 JLOG(j.warn()) << "Throws: " << ex.what();
239 }
240}
241
242} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
Stream debug() const
Definition Journal.h:301
Stream warn() const
Definition Journal.h:313
Routing table for objects identified by hash.
Definition HashRouter.h:78
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:191
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:46
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:19
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:118
size_type size() const
Definition STArray.h:224
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:181
Blob getSigningPubKey() const
Definition STTx.h:187
uint256 getTransactionID() const
Definition STTx.h:193
T empty(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
constexpr struct xrpl::batch_view_t batch_view
ApplyResult apply(Application &app, OpenView &view, STTx const &tx, ApplyFlags flags, beast::Journal journal)
Apply a transaction to an OpenView.
Definition apply.cpp:117
Validity
Describes the pre-processing validity of a transaction.
Definition apply.h:22
@ 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.
constexpr std::uint32_t tfInnerBatchTxn
Definition TxFlags.h:42
constexpr HashRouterFlags SF_LOCALBAD
Definition apply.cpp:15
ApplyTransactionResult applyTransaction(Application &app, OpenView &view, STTx const &tx, bool retryAssured, ApplyFlags flags, beast::Journal journal)
Transaction application helper.
Definition apply.cpp:190
PreflightResult preflight(Application &app, Rules const &rules, STTx const &tx, ApplyFlags flags, beast::Journal j)
Gate a transaction based on static information.
ApplyTransactionResult
Enum class for return value from applyTransaction
Definition apply.h:107
@ 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:257
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:638
bool passesLocalChecks(STObject const &st, std::string &)
Definition STTx.cpp:737
HashRouterFlags
Definition HashRouter.h:15
ApplyResult doApply(PreclaimResult const &preclaimResult, Application &app, OpenView &view)
Apply a prechecked transaction to an OpenView.
constexpr HashRouterFlags SF_LOCALGOOD
Definition apply.cpp:16
std::pair< Validity, std::string > checkValidity(HashRouter &router, STTx const &tx, Rules const &rules, Config const &config)
Checks transaction signature and local checks.
Definition apply.cpp:21
constexpr std::uint32_t tfOnlyOne
Definition TxFlags.h:258
ApplyFlags
Definition ApplyView.h:11
@ tapRETRY
Definition ApplyView.h:20
@ tapBATCH
Definition ApplyView.h:26
bool isTelLocal(TER x) noexcept
Definition TER.h:626
static bool applyBatchTransactions(Application &app, OpenView &batchView, STTx const &batchTxn, beast::Journal j)
Definition apply.cpp:135
bool isTesSuccess(TER x) noexcept
Definition TER.h:650
PreclaimResult preclaim(PreflightResult const &preflightResult, Application &app, OpenView const &view)
Gate a transaction based on static ledger information.
constexpr std::uint32_t tfUntilFailure
Definition TxFlags.h:259
bool isTecClaim(TER x) noexcept
Definition TER.h:657
void forceValidity(HashRouter &router, uint256 const &txid, Validity validity)
Sets the validity of a given transaction in the cache.
Definition apply.cpp:89
bool isTemMalformed(TER x) noexcept
Definition TER.h:632
T what(T... args)