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
24#include <xrpl/basics/Log.h>
25#include <xrpl/protocol/Feature.h>
26
27namespace ripple {
28
29// These are the same flags defined as SF_PRIVATE1-4 in HashRouter.h
30#define SF_SIGBAD SF_PRIVATE1 // Signature is bad
31#define SF_SIGGOOD SF_PRIVATE2 // Signature is good
32#define SF_LOCALBAD SF_PRIVATE3 // Local checks failed
33#define SF_LOCALGOOD SF_PRIVATE4 // Local checks passed
34
35//------------------------------------------------------------------------------
36
39 HashRouter& router,
40 STTx const& tx,
41 Rules const& rules,
42 Config const& config)
43{
44 auto const id = tx.getTransactionID();
45 auto const flags = router.getFlags(id);
46 if (flags & SF_SIGBAD)
47 // Signature is known bad
48 return {Validity::SigBad, "Transaction has bad signature."};
49
50 if (!(flags & SF_SIGGOOD))
51 {
52 // Don't know signature state. Check it.
53 auto const requireCanonicalSig =
54 rules.enabled(featureRequireFullyCanonicalSig)
57
58 auto const sigVerify = tx.checkSign(requireCanonicalSig, rules);
59 if (!sigVerify)
60 {
61 router.setFlags(id, SF_SIGBAD);
62 return {Validity::SigBad, sigVerify.error()};
63 }
64 router.setFlags(id, SF_SIGGOOD);
65 }
66
67 // Signature is now known good
68 if (flags & SF_LOCALBAD)
69 // ...but the local checks
70 // are known bad.
71 return {Validity::SigGoodOnly, "Local checks failed."};
72
73 if (flags & SF_LOCALGOOD)
74 // ...and the local checks
75 // are known good.
76 return {Validity::Valid, ""};
77
78 // Do the local checks
79 std::string reason;
80 if (!passesLocalChecks(tx, reason))
81 {
82 router.setFlags(id, SF_LOCALBAD);
83 return {Validity::SigGoodOnly, reason};
84 }
85 router.setFlags(id, SF_LOCALGOOD);
86 return {Validity::Valid, ""};
87}
88
89void
90forceValidity(HashRouter& router, uint256 const& txid, Validity validity)
91{
92 int flags = 0;
93 switch (validity)
94 {
95 case Validity::Valid:
96 flags |= SF_LOCALGOOD;
97 [[fallthrough]];
99 flags |= SF_SIGGOOD;
100 [[fallthrough]];
101 case Validity::SigBad:
102 // would be silly to call directly
103 break;
104 }
105 if (flags)
106 router.setFlags(txid, flags);
107}
108
109ApplyResult
111 Application& app,
112 OpenView& view,
113 STTx const& tx,
114 ApplyFlags flags,
116{
117 STAmountSO stAmountSO{view.rules().enabled(fixSTAmountCanonicalize)};
118 NumberSO stNumberSO{view.rules().enabled(fixUniversalNumber)};
119
120 auto pfresult = preflight(app, view.rules(), tx, flags, j);
121 auto pcresult = preclaim(pfresult, app, view);
122 return doApply(pcresult, app, view);
123}
124
127 Application& app,
128 OpenView& view,
129 STTx const& txn,
130 bool retryAssured,
131 ApplyFlags flags,
133{
134 // Returns false if the transaction has need not be retried.
135 if (retryAssured)
136 flags = flags | tapRETRY;
137
138 JLOG(j.debug()) << "TXN " << txn.getTransactionID()
139 << (retryAssured ? "/retry" : "/final");
140
141 try
142 {
143 auto const result = apply(app, view, txn, flags, j);
144 if (result.applied)
145 {
146 JLOG(j.debug())
147 << "Transaction applied: " << transHuman(result.ter);
149 }
150
151 if (isTefFailure(result.ter) || isTemMalformed(result.ter) ||
152 isTelLocal(result.ter))
153 {
154 // failure
155 JLOG(j.debug())
156 << "Transaction failure: " << transHuman(result.ter);
158 }
159
160 JLOG(j.debug()) << "Transaction retry: " << transHuman(result.ter);
162 }
163 catch (std::exception const& ex)
164 {
165 JLOG(j.warn()) << "Throws: " << ex.what();
167 }
168}
169
170} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:60
Stream debug() const
Definition: Journal.h:328
Stream warn() const
Definition: Journal.h:340
Routing table for objects identified by hash.
Definition: HashRouter.h:57
int getFlags(uint256 const &key)
Definition: HashRouter.cpp:95
bool setFlags(uint256 const &key, int flags)
Set the flags on a hash.
Definition: HashRouter.cpp:103
RAII class to set and restore the Number switchover.
Definition: IOUAmount.h:206
Writable ledger view that accumulates state and tx changes.
Definition: OpenView.h:57
Rules const & rules() const override
Returns the tx processing rules.
Definition: OpenView.cpp:153
Rules controlling protocol behavior.
Definition: Rules.h:35
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:130
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:239
uint256 getTransactionID() const
Definition: STTx.h:194
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:266
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:303
ApplyResult doApply(PreclaimResult const &preclaimResult, Application &app, OpenView &view)
Apply a prechecked transaction to an OpenView.
Definition: applySteps.cpp:381
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:323
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:110
void forceValidity(HashRouter &router, uint256 const &txid, Validity validity)
Sets the validity of a given transaction in the cache.
Definition: apply.cpp:90
bool passesLocalChecks(STObject const &st, std::string &)
Definition: STTx.cpp:604
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:31
@ tapRETRY
Definition: ApplyView.h:40
ApplyTransactionResult applyTransaction(Application &app, OpenView &view, STTx const &tx, bool retryAssured, ApplyFlags flags, beast::Journal journal)
Transaction application helper.
Definition: apply.cpp:126
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:38
T what(T... args)