rippled
Loading...
Searching...
No Matches
OpenLedger.h
1#pragma once
2
3#include <xrpld/app/ledger/Ledger.h>
4#include <xrpld/app/misc/CanonicalTXSet.h>
5#include <xrpld/core/Config.h>
6
7#include <xrpl/basics/Log.h>
8#include <xrpl/basics/UnorderedContainers.h>
9#include <xrpl/beast/utility/Journal.h>
10#include <xrpl/beast/utility/instrumentation.h>
11#include <xrpl/ledger/CachedSLEs.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(std::shared_ptr<Ledger const> const& ledger, CachedSLEs& cache, beast::Journal journal);
64
78 bool
79 empty() const;
80
92 current() const;
93
104 bool
105 modify(modify_type const& f);
106
140 void
141 accept(
142 Application& app,
143 Rules const& rules,
144 std::shared_ptr<Ledger const> const& ledger,
145 OrderedTxs const& locals,
146 bool retriesFirst,
147 OrderedTxs& retries,
148 ApplyFlags flags,
149 std::string const& suffix = "",
150 modify_type const& f = {});
151
152private:
158 template <class FwdRange>
159 static void
160 apply(
161 Application& app,
162 OpenView& view,
163 ReadView const& check,
164 FwdRange const& txs,
165 OrderedTxs& retries,
166 ApplyFlags flags,
168
170
172 create(Rules const& rules, std::shared_ptr<Ledger const> const& ledger);
173
174 static Result
175 apply_one(
176 Application& app,
177 OpenView& view,
179 bool retry,
180 ApplyFlags flags,
182};
183
184//------------------------------------------------------------------------------
185
186template <class FwdRange>
187void
189 Application& app,
190 OpenView& view,
191 ReadView const& check,
192 FwdRange const& txs,
193 OrderedTxs& retries,
194 ApplyFlags flags,
196{
197 for (auto iter = txs.begin(); iter != txs.end(); ++iter)
198 {
199 try
200 {
201 // Dereferencing the iterator can throw since it may be transformed.
202 auto const tx = *iter;
203 auto const txId = tx->getTransactionID();
204 if (check.txExists(txId))
205 continue;
206 auto const result = apply_one(app, view, tx, true, flags, j);
207 if (result == Result::retry)
208 retries.insert(tx);
209 }
210 catch (std::exception const& e)
211 {
212 JLOG(j.error()) << "OpenLedger::apply: Caught exception: " << e.what();
213 }
214 }
215 bool retry = true;
216 for (int pass = 0; pass < LEDGER_TOTAL_PASSES; ++pass)
217 {
218 int changes = 0;
219 auto iter = retries.begin();
220 while (iter != retries.end())
221 {
222 switch (apply_one(app, view, iter->second, retry, flags, j))
223 {
224 case Result::success:
225 ++changes;
226 [[fallthrough]];
227 case Result::failure:
228 iter = retries.erase(iter);
229 break;
230 case Result::retry:
231 ++iter;
232 }
233 }
234 // A non-retry pass made no changes
235 if (!changes && !retry)
236 return;
237 // Stop retriable passes
238 if (!changes || (pass >= LEDGER_RETRY_PASSES))
239 retry = false;
240 }
241
242 // If there are any transactions left, we must have
243 // tried them in at least one final pass
244 XRPL_ASSERT(retries.empty() || !retry, "xrpl::OpenLedger::apply : valid retries");
245}
246
247//------------------------------------------------------------------------------
248
249// For debug logging
250
253
255debugTostr(OrderedTxs const& set);
256
258debugTostr(SHAMap const& set);
259
262
263} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream error() const
Definition Journal.h:318
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:188
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)