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