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
23#include <xrpl/basics/Log.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/json/to_string.h>
26
27namespace ripple {
28
30 Application& app_,
31 OpenView& base,
32 STTx const& tx_,
33 TER preclaimResult_,
34 XRPAmount baseFee_,
35 ApplyFlags flags,
36 beast::Journal journal_)
37 : app(app_)
38 , tx(tx_)
39 , preclaimResult(preclaimResult_)
40 , baseFee(baseFee_)
41 , journal(journal_)
42 , base_(base)
43 , flags_(flags)
44{
45 view_.emplace(&base_, flags_);
46}
47
48void
50{
51 view_.emplace(&base_, flags_);
52}
53
56{
57 return view_->apply(base_, tx, ter, flags_ & tapDRY_RUN, journal);
58}
59
62{
63 return view_->size();
64}
65
66void
68 uint256 const&,
69 bool,
71 std::shared_ptr<SLE const> const&)> const& func)
72{
73 view_->visit(base_, func);
74}
75
76TER
78{
79 // If we already failed invariant checks before and we are now attempting to
80 // only charge a fee, and even that fails the invariant checks something is
81 // very wrong. We switch to tefINVARIANT_FAILED, which does NOT get included
82 // in a ledger.
83
84 return (result == tecINVARIANT_FAILED || result == tefINVARIANT_FAILED)
87}
88
89template <std::size_t... Is>
90TER
92 TER const result,
93 XRPAmount const fee,
95{
96 try
97 {
98 auto checkers = getInvariantChecks();
99
100 // call each check's per-entry method
101 visit([&checkers](
102 uint256 const& index,
103 bool isDelete,
104 std::shared_ptr<SLE const> const& before,
106 (..., std::get<Is>(checkers).visitEntry(isDelete, before, after));
107 });
108
109 // Note: do not replace this logic with a `...&&` fold expression.
110 // The fold expression will only run until the first check fails (it
111 // short-circuits). While the logic is still correct, the log
112 // message won't be. Every failed invariant should write to the log,
113 // not just the first one.
114 std::array<bool, sizeof...(Is)> finalizers{
115 {std::get<Is>(checkers).finalize(
116 tx, result, fee, *view_, journal)...}};
117
118 // call each check's finalizer to see that it passes
119 if (!std::all_of(
120 finalizers.cbegin(), finalizers.cend(), [](auto const& b) {
121 return b;
122 }))
123 {
124 JLOG(journal.fatal())
125 << "Transaction has failed one or more invariants: "
127
128 return failInvariantCheck(result);
129 }
130 }
131 catch (std::exception const& ex)
132 {
133 JLOG(journal.fatal())
134 << "Transaction caused an exception in an invariant"
135 << ", ex: " << ex.what()
136 << ", tx: " << to_string(tx.getJson(JsonOptions::none));
137
138 return failInvariantCheck(result);
139 }
140
141 return result;
142}
143
144TER
146{
147 XRPL_ASSERT(
148 isTesSuccess(result) || isTecClaim(result),
149 "ripple::ApplyContext::checkInvariants : is tesSUCCESS or tecCLAIM");
150
152 result,
153 fee,
154 std::make_index_sequence<std::tuple_size<InvariantChecks>::value>{});
155}
156
157} // namespace ripple
T all_of(T... args)
A generic endpoint for log messages.
Definition: Journal.h:60
Stream fatal() const
Definition: Journal.h:352
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:52
std::size_t size()
Get the number of unapplied changes.
std::optional< ApplyViewImpl > view_
Definition: ApplyContext.h:133
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:57
Json::Value getJson(JsonOptions options) const override
Definition: STTx.cpp:260
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:630
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition: View.cpp:2129
InvariantChecks getInvariantChecks()
get a tuple of all invariant checks
ApplyFlags
Definition: ApplyView.h:31
@ tapDRY_RUN
Definition: ApplyView.h:47
T what(T... args)