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 // Don't know signature state. Check it.
62 auto const requireCanonicalSig =
63 rules.enabled(featureRequireFullyCanonicalSig)
66
67 auto const sigVerify = tx.checkSign(requireCanonicalSig, rules);
68 if (!sigVerify)
69 {
70 router.setFlags(id, SF_SIGBAD);
71 return {Validity::SigBad, sigVerify.error()};
72 }
73 router.setFlags(id, SF_SIGGOOD);
74 }
75
76 // Signature is now known good
77 if (any(flags & SF_LOCALBAD))
78 // ...but the local checks
79 // are known bad.
80 return {Validity::SigGoodOnly, "Local checks failed."};
81
82 if (any(flags & SF_LOCALGOOD))
83 // ...and the local checks
84 // are known good.
85 return {Validity::Valid, ""};
86
87 // Do the local checks
88 std::string reason;
89 if (!passesLocalChecks(tx, reason))
90 {
91 router.setFlags(id, SF_LOCALBAD);
92 return {Validity::SigGoodOnly, reason};
93 }
94 router.setFlags(id, SF_LOCALGOOD);
95 return {Validity::Valid, ""};
96}
97
98void
99forceValidity(HashRouter& router, uint256 const& txid, Validity validity)
100{
102 switch (validity)
103 {
104 case Validity::Valid:
105 flags |= SF_LOCALGOOD;
106 [[fallthrough]];
108 flags |= SF_SIGGOOD;
109 [[fallthrough]];
110 case Validity::SigBad:
111 // would be silly to call directly
112 break;
113 }
114 if (any(flags))
115 router.setFlags(txid, flags);
116}
117
118template <typename PreflightChecks>
119ApplyResult
120apply(Application& app, OpenView& view, PreflightChecks&& preflightChecks)
121{
122 NumberSO stNumberSO{view.rules().enabled(fixUniversalNumber)};
123 return doApply(preclaim(preflightChecks(), app, view), app, view);
124}
125
126ApplyResult
128 Application& app,
129 OpenView& view,
130 STTx const& tx,
131 ApplyFlags flags,
133{
134 return apply(app, view, [&]() mutable {
135 return preflight(app, view.rules(), tx, flags, j);
136 });
137}
138
139ApplyResult
141 Application& app,
142 OpenView& view,
143 uint256 const& parentBatchId,
144 STTx const& tx,
145 ApplyFlags flags,
147{
148 return apply(app, view, [&]() mutable {
149 return preflight(app, view.rules(), parentBatchId, tx, flags, j);
150 });
151}
152
153static bool
155 Application& app,
156 OpenView& batchView,
157 STTx const& batchTxn,
159{
160 XRPL_ASSERT(
161 batchTxn.getTxnType() == ttBATCH &&
162 batchTxn.getFieldArray(sfRawTransactions).size() != 0,
163 "Batch transaction missing sfRawTransactions");
164
165 auto const parentBatchId = batchTxn.getTransactionID();
166 auto const mode = batchTxn.getFlags();
167
168 auto applyOneTransaction =
169 [&app, &j, &parentBatchId, &batchView](STTx&& tx) {
170 OpenView perTxBatchView(batch_view, batchView);
171
172 auto const ret =
173 apply(app, perTxBatchView, parentBatchId, tx, tapBATCH, j);
174 XRPL_ASSERT(
175 ret.applied == (isTesSuccess(ret.ter) || isTecClaim(ret.ter)),
176 "Inner transaction should not be applied");
177
178 JLOG(j.debug()) << "BatchTrace[" << parentBatchId
179 << "]: " << tx.getTransactionID() << " "
180 << (ret.applied ? "applied" : "failure") << ": "
181 << transToken(ret.ter);
182
183 // If the transaction should be applied push its changes to the
184 // whole-batch view.
185 if (ret.applied && (isTesSuccess(ret.ter) || isTecClaim(ret.ter)))
186 perTxBatchView.apply(batchView);
187
188 return ret;
189 };
190
191 int applied = 0;
192
193 for (STObject rb : batchTxn.getFieldArray(sfRawTransactions))
194 {
195 auto const result = applyOneTransaction(STTx{std::move(rb)});
196 XRPL_ASSERT(
197 result.applied ==
198 (isTesSuccess(result.ter) || isTecClaim(result.ter)),
199 "Outer Batch failure, inner transaction should not be applied");
200
201 if (result.applied)
202 ++applied;
203
204 if (!isTesSuccess(result.ter))
205 {
206 if (mode & tfAllOrNothing)
207 return false;
208
209 if (mode & tfUntilFailure)
210 break;
211 }
212 else if (mode & tfOnlyOne)
213 break;
214 }
215
216 return applied != 0;
217}
218
221 Application& app,
222 OpenView& view,
223 STTx const& txn,
224 bool retryAssured,
225 ApplyFlags flags,
227{
228 // Returns false if the transaction has need not be retried.
229 if (retryAssured)
230 flags = flags | tapRETRY;
231
232 JLOG(j.debug()) << "TXN " << txn.getTransactionID()
233 << (retryAssured ? "/retry" : "/final");
234
235 try
236 {
237 auto const result = apply(app, view, txn, flags, j);
238
239 if (result.applied)
240 {
241 JLOG(j.debug())
242 << "Transaction applied: " << transToken(result.ter);
243
244 // The batch transaction was just applied; now we need to apply
245 // its inner transactions as necessary.
246 if (isTesSuccess(result.ter) && txn.getTxnType() == ttBATCH)
247 {
248 OpenView wholeBatchView(batch_view, view);
249
250 if (applyBatchTransactions(app, wholeBatchView, txn, j))
251 wholeBatchView.apply(view);
252 }
253
255 }
256
257 if (isTefFailure(result.ter) || isTemMalformed(result.ter) ||
258 isTelLocal(result.ter))
259 {
260 // failure
261 JLOG(j.debug())
262 << "Transaction failure: " << transHuman(result.ter);
264 }
265
266 JLOG(j.debug()) << "Transaction retry: " << transHuman(result.ter);
268 }
269 catch (std::exception const& ex)
270 {
271 JLOG(j.warn()) << "Throws: " << ex.what();
273 }
274}
275
276} // 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:215
Expected< void, std::string > checkSign(RequireFullyCanonicalSig requireCanonicalSig, Rules const &rules) const
Check the signature.
Definition STTx.cpp:263
TxType getTxnType() const
Definition STTx.h:209
uint256 getTransactionID() const
Definition STTx.h:221
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:154
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:127
void forceValidity(HashRouter &router, uint256 const &txid, Validity validity)
Sets the validity of a given transaction in the cache.
Definition apply.cpp:99
bool passesLocalChecks(STObject const &st, std::string &)
Definition STTx.cpp:812
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:220
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)