rippled
Loading...
Searching...
No Matches
OpenLedger.h
1#pragma once
2
3#include <xrpld/core/Config.h>
4
5#include <xrpl/basics/Log.h>
6#include <xrpl/basics/UnorderedContainers.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/beast/utility/instrumentation.h>
9#include <xrpl/ledger/CachedSLEs.h>
10#include <xrpl/ledger/CanonicalTXSet.h>
11#include <xrpl/ledger/Ledger.h>
12#include <xrpl/ledger/OpenView.h>
13
14#include <mutex>
15
16namespace xrpl {
17
18// How many total extra passes we make
19// We must ensure we make at least one non-retriable pass
20#define LEDGER_TOTAL_PASSES 3
21
22// How many extra retry passes we
23// make if the previous retry pass made changes
24#define LEDGER_RETRY_PASSES 1
25
27
28//------------------------------------------------------------------------------
29
32{
33private:
39
40public:
53
54 OpenLedger() = delete;
55 OpenLedger(OpenLedger const&) = delete;
57 operator=(OpenLedger const&) = delete;
58
63 explicit OpenLedger(
65 CachedSLEs& cache,
66 beast::Journal journal);
67
81 bool
82 empty() const;
83
95 current() const;
96
107 bool
108 modify(modify_type const& f);
109
143 void
144 accept(
145 Application& app,
146 Rules const& rules,
147 std::shared_ptr<Ledger const> const& ledger,
148 OrderedTxs const& locals,
149 bool retriesFirst,
150 OrderedTxs& retries,
151 ApplyFlags flags,
152 std::string const& suffix = "",
153 modify_type const& f = {});
154
155private:
161 template <class FwdRange>
162 static void
163 apply(
164 Application& app,
165 OpenView& view,
166 ReadView const& check,
167 FwdRange const& txs,
168 OrderedTxs& retries,
169 ApplyFlags flags,
171
173
175 create(Rules const& rules, std::shared_ptr<Ledger const> const& ledger);
176
177 static Result
178 apply_one(
179 Application& app,
180 OpenView& view,
182 bool retry,
183 ApplyFlags flags,
185};
186
187//------------------------------------------------------------------------------
188
189template <class FwdRange>
190void
192 Application& app,
193 OpenView& view,
194 ReadView const& check,
195 FwdRange const& txs,
196 OrderedTxs& retries,
197 ApplyFlags flags,
199{
200 for (auto iter = txs.begin(); iter != txs.end(); ++iter)
201 {
202 try
203 {
204 // Dereferencing the iterator can throw since it may be transformed.
205 auto const tx = *iter;
206 auto const txId = tx->getTransactionID();
207 if (check.txExists(txId))
208 continue;
209 auto const result = apply_one(app, view, tx, true, flags, j);
210 if (result == Result::retry)
211 retries.insert(tx);
212 }
213 catch (std::exception const& e)
214 {
215 JLOG(j.error()) << "OpenLedger::apply: Caught exception: " << e.what();
216 }
217 }
218 bool retry = true;
219 for (int pass = 0; pass < LEDGER_TOTAL_PASSES; ++pass)
220 {
221 int changes = 0;
222 auto iter = retries.begin();
223 while (iter != retries.end())
224 {
225 switch (apply_one(app, view, iter->second, retry, flags, j))
226 {
227 case Result::success:
228 ++changes;
229 [[fallthrough]];
230 case Result::failure:
231 iter = retries.erase(iter);
232 break;
233 case Result::retry:
234 ++iter;
235 }
236 }
237 // A non-retry pass made no changes
238 if (!changes && !retry)
239 return;
240 // Stop retriable passes
241 if (!changes || (pass >= LEDGER_RETRY_PASSES))
242 retry = false;
243 }
244
245 // If there are any transactions left, we must have
246 // tried them in at least one final pass
247 XRPL_ASSERT(retries.empty() || !retry, "xrpl::OpenLedger::apply : valid retries");
248}
249
250//------------------------------------------------------------------------------
251
252// For debug logging
253
256
258debugTostr(OrderedTxs const& set);
259
261debugTostr(SHAMap const& set);
262
265
266} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream error() const
Definition Journal.h:319
Holds transactions which were deferred to the next pass of consensus.
void insert(std::shared_ptr< STTx const > const &txn)
const_iterator begin() const
const_iterator erase(const_iterator const &it)
const_iterator end() const
Represents the open ledger.
Definition OpenLedger.h:32
beast::Journal const j_
Definition OpenLedger.h:34
bool modify(modify_type const &f)
Modify the open ledger.
OpenLedger(OpenLedger const &)=delete
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.
bool empty() const
Returns true if there are no transactions.
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
std::shared_ptr< OpenView > create(Rules const &rules, std::shared_ptr< Ledger const > const &ledger)
std::shared_ptr< OpenView const > current_
Definition OpenLedger.h:38
OpenLedger()=delete
OpenLedger & operator=(OpenLedger const &)=delete
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:191
static Result apply_one(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, bool retry, ApplyFlags flags, beast::Journal j)
CachedSLEs & cache_
Definition OpenLedger.h:35
std::mutex current_mutex_
Definition OpenLedger.h:37
std::mutex modify_mutex_
Definition OpenLedger.h:36
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:45
A view into a ledger.
Definition ReadView.h:31
Rules controlling protocol behavior.
Definition Rules.h:18
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition SHAMap.h:77
Map/cache combination.
Definition TaggedCache.h:42
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string debugTxstr(std::shared_ptr< STTx const > const &tx)
std::string debugTostr(OrderedTxs const &set)
ApplyFlags
Definition ApplyView.h:10
boost::outcome_v2::result< T, std::error_code > Result
Definition b58_utils.h:17
T what(T... args)