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