rippled
Loading...
Searching...
No Matches
ApplyContext.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/tx/detail/ApplyContext.h>
21#include <xrpld/app/tx/detail/InvariantCheck.h>
22#include <xrpld/app/tx/detail/Transactor.h>
23#include <xrpl/basics/Log.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/json/to_string.h>
26#include <xrpl/protocol/Feature.h>
27#include <xrpl/protocol/Indexes.h>
28
29namespace ripple {
30
32 Application& app_,
33 OpenView& base,
34 STTx const& tx_,
35 TER preclaimResult_,
36 XRPAmount baseFee_,
37 ApplyFlags flags,
38 beast::Journal journal_)
39 : app(app_)
40 , tx(tx_)
41 , preclaimResult(preclaimResult_)
42 , baseFee(baseFee_)
43 , journal(journal_)
44 , base_(base)
45 , flags_(flags)
46{
47 view_.emplace(&base_, flags_);
48}
49
50void
52{
53 view_.emplace(&base_, flags_);
54}
55
58{
59 return view_->apply(base_, tx, ter, flags_ & tapDRY_RUN, journal);
60}
61
64{
65 return view_->size();
66}
67
68void
70 uint256 const&,
71 bool,
73 std::shared_ptr<SLE const> const&)> const& func)
74{
75 view_->visit(base_, func);
76}
77
78TER
80{
81 // If we already failed invariant checks before and we are now attempting to
82 // only charge a fee, and even that fails the invariant checks something is
83 // very wrong. We switch to tefINVARIANT_FAILED, which does NOT get included
84 // in a ledger.
85
86 return (result == tecINVARIANT_FAILED || result == tefINVARIANT_FAILED)
89}
90
91template <std::size_t... Is>
92TER
94 TER const result,
95 XRPAmount const fee,
97{
98 try
99 {
100 auto checkers = getInvariantChecks();
101
102 // call each check's per-entry method
103 visit([&checkers](
104 uint256 const& index,
105 bool isDelete,
106 std::shared_ptr<SLE const> const& before,
108 (..., std::get<Is>(checkers).visitEntry(isDelete, before, after));
109 });
110
111 // Note: do not replace this logic with a `...&&` fold expression.
112 // The fold expression will only run until the first check fails (it
113 // short-circuits). While the logic is still correct, the log
114 // message won't be. Every failed invariant should write to the log,
115 // not just the first one.
116 std::array<bool, sizeof...(Is)> finalizers{
117 {std::get<Is>(checkers).finalize(
118 tx, result, fee, *view_, journal)...}};
119
120 // call each check's finalizer to see that it passes
121 if (!std::all_of(
122 finalizers.cbegin(), finalizers.cend(), [](auto const& b) {
123 return b;
124 }))
125 {
126 JLOG(journal.fatal())
127 << "Transaction has failed one or more invariants: "
129
130 return failInvariantCheck(result);
131 }
132 }
133 catch (std::exception const& ex)
134 {
135 JLOG(journal.fatal())
136 << "Transaction caused an exception in an invariant"
137 << ", ex: " << ex.what()
138 << ", tx: " << to_string(tx.getJson(JsonOptions::none));
139
140 return failInvariantCheck(result);
141 }
142
143 return result;
144}
145
146TER
148{
149 XRPL_ASSERT(
150 isTesSuccess(result) || isTecClaim(result),
151 "ripple::ApplyContext::checkInvariants : is tesSUCCESS or tecCLAIM");
152
154 result,
155 fee,
156 std::make_index_sequence<std::tuple_size<InvariantChecks>::value>{});
157}
158
159} // namespace ripple
T all_of(T... args)
A generic endpoint for log messages.
Definition: Journal.h:59
Stream fatal() const
Definition: Journal.h:341
ApplyContext(Application &app, OpenView &base, STTx const &tx, TER preclaimResult, XRPAmount baseFee, ApplyFlags flags, beast::Journal=beast::Journal{beast::Journal::getNullSink()})
void visit(std::function< void(uint256 const &key, bool isDelete, std::shared_ptr< SLE const > const &before, std::shared_ptr< SLE const > const &after)> const &func)
Visit unapplied changes.
std::optional< TxMeta > apply(TER)
Apply the transaction result to the base.
void discard()
Discard changes and start fresh.
TER failInvariantCheck(TER const result)
beast::Journal const journal
Definition: ApplyContext.h:51
std::size_t size()
Get the number of unapplied changes.
std::optional< ApplyViewImpl > view_
Definition: ApplyContext.h:132
TER checkInvariants(TER const result, XRPAmount const fee)
Applies all invariant checkers one by one.
TER checkInvariantsHelper(TER const result, XRPAmount const fee, std::index_sequence< Is... >)
Writable ledger view that accumulates state and tx changes.
Definition: OpenView.h:56
Json::Value getJson(JsonOptions options) const override
Definition: STTx.cpp:233
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
bool isTecClaim(TER x)
Definition: TER.h:662
bool isTesSuccess(TER x)
Definition: TER.h:656
@ tefINVARIANT_FAILED
Definition: TER.h:183
@ tecINVARIANT_FAILED
Definition: TER.h:300
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
InvariantChecks getInvariantChecks()
get a tuple of all invariant checks
ApplyFlags
Definition: ApplyView.h:30
@ tapDRY_RUN
Definition: ApplyView.h:46
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition: Escrow.cpp:89
T what(T... args)