rippled
Loading...
Searching...
No Matches
OpenLedger.h
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#ifndef RIPPLE_APP_LEDGER_OPENLEDGER_H_INCLUDED
21#define RIPPLE_APP_LEDGER_OPENLEDGER_H_INCLUDED
22
23#include <xrpld/app/ledger/Ledger.h>
24#include <xrpld/app/misc/CanonicalTXSet.h>
25#include <xrpld/core/Config.h>
26#include <xrpld/ledger/CachedSLEs.h>
27#include <xrpld/ledger/OpenView.h>
28
29#include <xrpl/basics/Log.h>
30#include <xrpl/basics/UnorderedContainers.h>
31#include <xrpl/beast/utility/Journal.h>
32#include <xrpl/beast/utility/instrumentation.h>
33
34#include <mutex>
35
36namespace ripple {
37
38// How many total extra passes we make
39// We must ensure we make at least one non-retriable pass
40#define LEDGER_TOTAL_PASSES 3
41
42// How many extra retry passes we
43// make if the previous retry pass made changes
44#define LEDGER_RETRY_PASSES 1
45
47
48//------------------------------------------------------------------------------
49
52{
53private:
59
60public:
73
74 OpenLedger() = delete;
75 OpenLedger(OpenLedger const&) = delete;
77 operator=(OpenLedger const&) = delete;
78
83 explicit OpenLedger(
85 CachedSLEs& cache,
86 beast::Journal journal);
87
101 bool
102 empty() const;
103
115 current() const;
116
127 bool
128 modify(modify_type const& f);
129
163 void
164 accept(
165 Application& app,
166 Rules const& rules,
167 std::shared_ptr<Ledger const> const& ledger,
168 OrderedTxs const& locals,
169 bool retriesFirst,
170 OrderedTxs& retries,
172 std::string const& suffix = "",
173 modify_type const& f = {});
174
175private:
181 template <class FwdRange>
182 static void
183 apply(
184 Application& app,
185 OpenView& view,
186 ReadView const& check,
187 FwdRange const& txs,
188 OrderedTxs& retries,
191
193
195 create(Rules const& rules, std::shared_ptr<Ledger const> const& ledger);
196
197 static Result
198 apply_one(
199 Application& app,
200 OpenView& view,
202 bool retry,
205};
206
207//------------------------------------------------------------------------------
208
209template <class FwdRange>
210void
212 Application& app,
213 OpenView& view,
214 ReadView const& check,
215 FwdRange const& txs,
216 OrderedTxs& retries,
219{
220 for (auto iter = txs.begin(); iter != txs.end(); ++iter)
221 {
222 try
223 {
224 // Dereferencing the iterator can throw since it may be transformed.
225 auto const tx = *iter;
226 auto const txId = tx->getTransactionID();
227 if (check.txExists(txId))
228 continue;
229 auto const result = apply_one(app, view, tx, true, flags, j);
230 if (result == Result::retry)
231 retries.insert(tx);
232 }
233 catch (std::exception const& e)
234 {
235 JLOG(j.error())
236 << "OpenLedger::apply: Caught exception: " << e.what();
237 }
238 }
239 bool retry = true;
240 for (int pass = 0; pass < LEDGER_TOTAL_PASSES; ++pass)
241 {
242 int changes = 0;
243 auto iter = retries.begin();
244 while (iter != retries.end())
245 {
246 switch (apply_one(app, view, iter->second, retry, flags, j))
247 {
248 case Result::success:
249 ++changes;
250 [[fallthrough]];
251 case Result::failure:
252 iter = retries.erase(iter);
253 break;
254 case Result::retry:
255 ++iter;
256 }
257 }
258 // A non-retry pass made no changes
259 if (!changes && !retry)
260 return;
261 // Stop retriable passes
262 if (!changes || (pass >= LEDGER_RETRY_PASSES))
263 retry = false;
264 }
265
266 // If there are any transactions left, we must have
267 // tried them in at least one final pass
268 XRPL_ASSERT(
269 retries.empty() || !retry, "ripple::OpenLedger::apply : valid retries");
270}
271
272//------------------------------------------------------------------------------
273
274// For debug logging
275
278
280debugTostr(OrderedTxs const& set);
281
283debugTostr(SHAMap const& set);
284
287
288} // namespace ripple
289
290#endif
A generic endpoint for log messages.
Definition: Journal.h:60
Stream error() const
Definition: Journal.h:346
Holds transactions which were deferred to the next pass of consensus.
void insert(std::shared_ptr< STTx const > const &txn)
const_iterator end() const
const_iterator begin() const
const_iterator erase(const_iterator const &it)
Represents the open ledger.
Definition: OpenLedger.h:52
bool modify(modify_type const &f)
Modify the open ledger.
Definition: OpenLedger.cpp:58
void accept(Application &app, Rules const &rules, std::shared_ptr< Ledger const > const &ledger, OrderedTxs const &locals, bool retriesFirst, OrderedTxs &retries, ApplyFlags flags, std::string const &suffix="", modify_type const &f={})
Accept a new ledger.
Definition: OpenLedger.cpp:72
CachedSLEs & cache_
Definition: OpenLedger.h:55
OpenLedger(OpenLedger const &)=delete
std::shared_ptr< OpenView > create(Rules const &rules, std::shared_ptr< Ledger const > const &ledger)
Definition: OpenLedger.cpp:160
bool empty() const
Returns true if there are no transactions.
Definition: OpenLedger.cpp:44
static void apply(Application &app, OpenView &view, ReadView const &check, FwdRange const &txs, OrderedTxs &retries, ApplyFlags flags, beast::Journal j)
Algorithm for applying transactions.
Definition: OpenLedger.h:211
std::mutex modify_mutex_
Definition: OpenLedger.h:56
OpenLedger & operator=(OpenLedger const &)=delete
static Result apply_one(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, bool retry, ApplyFlags flags, beast::Journal j)
Definition: OpenLedger.cpp:171
std::shared_ptr< OpenView const > current_
Definition: OpenLedger.h:58
beast::Journal const j_
Definition: OpenLedger.h:54
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
Definition: OpenLedger.cpp:51
std::mutex current_mutex_
Definition: OpenLedger.h:57
Writable ledger view that accumulates state and tx changes.
Definition: OpenView.h:66
A view into a ledger.
Definition: ReadView.h:52
Rules controlling protocol behavior.
Definition: Rules.h:35
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition: SHAMap.h:98
Map/cache combination.
Definition: TaggedCache.h:62
Match set account flags.
Definition: flags.h:125
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
boost::outcome_v2::result< T, std::error_code > Result
Definition: b58_utils.h:38
std::string debugTostr(OrderedTxs const &set)
Definition: OpenLedger.cpp:202
ApplyFlags
Definition: ApplyView.h:31
std::string debugTxstr(std::shared_ptr< STTx const > const &tx)
Definition: OpenLedger.cpp:194
T what(T... args)