rippled
Loading...
Searching...
No Matches
apply.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/misc/HashRouter.h>
21#include <xrpld/app/tx/apply.h>
22#include <xrpld/app/tx/applySteps.h>
23#include <xrpl/basics/Log.h>
24#include <xrpl/protocol/Feature.h>
25
26namespace ripple {
27
28// These are the same flags defined as SF_PRIVATE1-4 in HashRouter.h
29#define SF_SIGBAD SF_PRIVATE1 // Signature is bad
30#define SF_SIGGOOD SF_PRIVATE2 // Signature is good
31#define SF_LOCALBAD SF_PRIVATE3 // Local checks failed
32#define SF_LOCALGOOD SF_PRIVATE4 // Local checks passed
33
34//------------------------------------------------------------------------------
35
38 HashRouter& router,
39 STTx const& tx,
40 Rules const& rules,
41 Config const& config)
42{
43 auto const id = tx.getTransactionID();
44 auto const flags = router.getFlags(id);
45 if (flags & SF_SIGBAD)
46 // Signature is known bad
47 return {Validity::SigBad, "Transaction has bad signature."};
48
49 if (!(flags & SF_SIGGOOD))
50 {
51 // Don't know signature state. Check it.
52 auto const requireCanonicalSig =
53 rules.enabled(featureRequireFullyCanonicalSig)
56
57 auto const sigVerify = tx.checkSign(requireCanonicalSig, 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 (flags & SF_LOCALBAD)
68 // ...but the local checks
69 // are known bad.
70 return {Validity::SigGoodOnly, "Local checks failed."};
71
72 if (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{
91 int flags = 0;
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 (flags)
105 router.setFlags(txid, flags);
106}
107
108ApplyResult
110 Application& app,
111 OpenView& view,
112 STTx const& tx,
113 ApplyFlags flags,
115{
116 STAmountSO stAmountSO{view.rules().enabled(fixSTAmountCanonicalize)};
117 NumberSO stNumberSO{view.rules().enabled(fixUniversalNumber)};
118
119 auto pfresult = preflight(app, view.rules(), tx, flags, j);
120 auto pcresult = preclaim(pfresult, app, view);
121 return doApply(pcresult, app, view);
122}
123
126 Application& app,
127 OpenView& view,
128 STTx const& txn,
129 bool retryAssured,
130 ApplyFlags flags,
132{
133 // Returns false if the transaction has need not be retried.
134 if (retryAssured)
135 flags = flags | tapRETRY;
136
137 JLOG(j.debug()) << "TXN " << txn.getTransactionID()
138 << (retryAssured ? "/retry" : "/final");
139
140 try
141 {
142 auto const result = apply(app, view, txn, flags, j);
143 if (result.applied)
144 {
145 JLOG(j.debug())
146 << "Transaction applied: " << transHuman(result.ter);
148 }
149
150 if (isTefFailure(result.ter) || isTemMalformed(result.ter) ||
151 isTelLocal(result.ter))
152 {
153 // failure
154 JLOG(j.debug())
155 << "Transaction failure: " << transHuman(result.ter);
157 }
158
159 JLOG(j.debug()) << "Transaction retry: " << transHuman(result.ter);
161 }
162 catch (std::exception const& ex)
163 {
164 JLOG(j.warn()) << "Throws: " << ex.what();
166 }
167}
168
169} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
Stream debug() const
Definition: Journal.h:317
Stream warn() const
Definition: Journal.h:329
Routing table for objects identified by hash.
Definition: HashRouter.h:54
int getFlags(uint256 const &key)
Definition: HashRouter.cpp:94
bool setFlags(uint256 const &key, int flags)
Set the flags on a hash.
Definition: HashRouter.cpp:102
RAII class to set and restore the Number switchover.
Definition: IOUAmount.h:204
Writable ledger view that accumulates state and tx changes.
Definition: OpenView.h:56
Rules const & rules() const override
Returns the tx processing rules.
Definition: OpenView.cpp:152
Rules controlling protocol behavior.
Definition: Rules.h:35
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:122
RAII class to set and restore the STAmount canonicalize switchover.
Definition: STAmount.h:702
Expected< void, std::string > checkSign(RequireFullyCanonicalSig requireCanonicalSig, Rules const &rules) const
Definition: STTx.cpp:212
uint256 getTransactionID() const
Definition: STTx.h:193
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::string transHuman(TER code)
Definition: TER.cpp:260
bool isTelLocal(TER x)
Definition: TER.h:632
PreflightResult preflight(Application &app, Rules const &rules, STTx const &tx, ApplyFlags flags, beast::Journal j)
Gate a transaction based on static information.
Definition: applySteps.cpp:296
ApplyResult doApply(PreclaimResult const &preclaimResult, Application &app, OpenView &view)
Apply a prechecked transaction to an OpenView.
Definition: applySteps.cpp:374
ApplyTransactionResult
Enum class for return value from applyTransaction
Definition: apply.h:135
@ Success
Applied to this ledger.
@ Retry
Should be retried in this ledger.
@ Fail
Should not be retried in this ledger.
PreclaimResult preclaim(PreflightResult const &preflightResult, Application &app, OpenView const &view)
Gate a transaction based on static ledger information.
Definition: applySteps.cpp:316
bool isTemMalformed(TER x)
Definition: TER.h:638
ApplyResult apply(Application &app, OpenView &view, STTx const &tx, ApplyFlags flags, beast::Journal journal)
Apply a transaction to an OpenView.
Definition: apply.cpp:109
void forceValidity(HashRouter &router, uint256 const &txid, Validity validity)
Sets the validity of a given transaction in the cache.
Definition: apply.cpp:89
bool passesLocalChecks(STObject const &st, std::string &)
Definition: STTx.cpp:577
Validity
Describes the pre-processing validity of a transaction.
Definition: apply.h:41
@ 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 isTefFailure(TER x)
Definition: TER.h:644
ApplyFlags
Definition: ApplyView.h:30
@ tapRETRY
Definition: ApplyView.h:39
ApplyTransactionResult applyTransaction(Application &app, OpenView &view, STTx const &tx, bool retryAssured, ApplyFlags flags, beast::Journal journal)
Transaction application helper.
Definition: apply.cpp:125
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:37
T what(T... args)