rippled
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 <ripple/app/tx/impl/ApplyContext.h>
21 #include <ripple/app/tx/impl/InvariantCheck.h>
22 #include <ripple/app/tx/impl/Transactor.h>
23 #include <ripple/basics/Log.h>
24 #include <ripple/json/to_string.h>
25 #include <ripple/protocol/Indexes.h>
26 #include <ripple/protocol/Feature.h>
27 #include <cassert>
28 
29 namespace ripple {
30 
32  OpenView& base, STTx const& tx_, TER preclaimResult_,
33  FeeUnit64 baseFee_, ApplyFlags flags,
34  beast::Journal journal_)
35  : app(app_)
36  , tx(tx_)
37  , preclaimResult(preclaimResult_)
38  , baseFee(baseFee_)
39  , journal(journal_)
40  , base_ (base)
41  , flags_(flags)
42 {
43  view_.emplace(&base_, flags_);
44 }
45 
46 void
48 {
49  view_.emplace(&base_, flags_);
50 }
51 
52 void
54 {
55  view_->apply(base_, tx, ter, journal);
56 }
57 
60 {
61  return view_->size();
62 }
63 
64 void
66  uint256 const&, bool,
68  std::shared_ptr<SLE const> const&)> const& func)
69 {
70  view_->visit(base_, func);
71 }
72 
73 TER
75 {
76  // If we already failed invariant checks before and we are now attempting to
77  // only charge a fee, and even that fails the invariant checks something is
78  // very wrong. We switch to tefINVARIANT_FAILED, which does NOT get included
79  // in a ledger.
80 
81  return (result == tecINVARIANT_FAILED || result == tefINVARIANT_FAILED)
84 }
85 
86 template<std::size_t... Is>
87 TER
89  TER const result,
90  XRPAmount const fee,
92 {
93  try
94  {
95  auto checkers = getInvariantChecks();
96 
97  // call each check's per-entry method
98  visit (
99  [&checkers](
100  uint256 const& index,
101  bool isDelete,
102  std::shared_ptr <SLE const> const& before,
104  {
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>(
115  checkers).finalize(tx, result, fee, *view_, journal)...}};
116 
117  // call each check's finalizer to see that it passes
118  if (! std::all_of( finalizers.cbegin(), finalizers.cend(),
119  [](auto const& b) { return b; }))
120  {
121  JLOG(journal.fatal()) <<
122  "Transaction has failed one or more invariants: " <<
124 
125  return failInvariantCheck (result);
126  }
127  }
128  catch(std::exception const& ex)
129  {
130  JLOG(journal.fatal()) <<
131  "Transaction caused an exception in an invariant" <<
132  ", ex: " << ex.what() <<
133  ", tx: " << to_string(tx.getJson (JsonOptions::none));
134 
135  return failInvariantCheck (result);
136  }
137 
138  return result;
139 }
140 
141 TER
143 {
144  assert (isTesSuccess(result) || isTecClaim(result));
145 
146  return checkInvariantsHelper(result, fee,
147  std::make_index_sequence<std::tuple_size<InvariantChecks>::value>{});
148 }
149 
150 } // ripple
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:312
ripple::ApplyContext::ApplyContext
ApplyContext(Application &app, OpenView &base, STTx const &tx, TER preclaimResult, FeeUnit64 baseFee, ApplyFlags flags, beast::Journal=beast::Journal{beast::Journal::getNullSink()})
Definition: ApplyContext.cpp:31
ripple::Application
Definition: Application.h:85
ripple::ApplyContext::view_
boost::optional< ApplyViewImpl > view_
Definition: ApplyContext.h:121
ripple::tecINVARIANT_FAILED
@ tecINVARIANT_FAILED
Definition: TER.h:278
std::shared_ptr
STL class.
ripple::ApplyContext::checkInvariants
TER checkInvariants(TER const result, XRPAmount const fee)
Applies all invariant checkers one by one.
Definition: ApplyContext.cpp:142
std::exception
STL class.
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:501
ripple::tefINVARIANT_FAILED
@ tefINVARIANT_FAILED
Definition: TER.h:163
std::index_sequence
ripple::OpenView
Writable ledger view that accumulates state and tx changes.
Definition: OpenView.h:52
ripple::ApplyContext::flags_
ApplyFlags flags_
Definition: ApplyContext.h:120
ripple::ApplyFlags
ApplyFlags
Definition: ApplyView.h:30
ripple::ApplyContext::journal
const beast::Journal journal
Definition: ApplyContext.h:48
std::function
std::all_of
T all_of(T... args)
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:41
ripple::ApplyContext::base_
OpenView & base_
Definition: ApplyContext.h:119
ripple::isTecClaim
bool isTecClaim(TER x)
Definition: TER.h:506
ripple::base_uint< 256 >
ripple::JsonOptions::none
@ none
ripple::TERSubset< CanCvtToTER >
std::array
STL class.
ripple::ApplyContext::discard
void discard()
Discard changes and start fresh.
Definition: ApplyContext.cpp:47
ripple::STTx
Definition: STTx.h:43
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple::feeunit::TaggedFee
Definition: FeeUnits.h:72
ripple::STTx::getJson
Json::Value getJson(JsonOptions options) const override
Definition: STTx.cpp:202
ripple::ApplyContext::apply
void apply(TER)
Apply the transaction result to the base.
Definition: ApplyContext.cpp:53
ripple::ApplyContext::visit
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.
Definition: ApplyContext.cpp:65
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
cassert
ripple::ApplyContext::checkInvariantsHelper
TER checkInvariantsHelper(TER const result, XRPAmount const fee, std::index_sequence< Is... >)
Definition: ApplyContext.cpp:88
ripple::after
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition: Escrow.cpp:87
std::size_t
ripple::getInvariantChecks
InvariantChecks getInvariantChecks()
get a tuple of all invariant checks
Definition: InvariantCheck.h:345
ripple::ApplyContext::size
std::size_t size()
Get the number of unapplied changes.
Definition: ApplyContext.cpp:59
ripple::ApplyContext::tx
STTx const & tx
Definition: ApplyContext.h:45
std::exception::what
T what(T... args)
ripple::ApplyContext::failInvariantCheck
TER failInvariantCheck(TER const result)
Definition: ApplyContext.cpp:74
ripple::XRPAmount
Definition: XRPAmount.h:46