xrpld
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#include <string_view>
16
17namespace xrpl {
18
19// How many total extra passes we make
20// We must ensure we make at least one non-retriable pass
21#define LEDGER_TOTAL_PASSES 3
22
23// How many extra retry passes we
24// make if the previous retry pass made changes
25#define LEDGER_RETRY_PASSES 1
26
28
29//------------------------------------------------------------------------------
30
33{
34private:
40
41public:
54
55 OpenLedger() = delete;
56 OpenLedger(OpenLedger const&) = delete;
58 operator=(OpenLedger const&) = delete;
59
64 explicit OpenLedger(
66 CachedSLEs& cache,
67 beast::Journal journal);
68
82 bool
83 empty() const;
84
96 current() const;
97
108 bool
109 modify(modify_type const& f);
110
144 void
145 accept(
146 Application& app,
147 Rules const& rules,
148 std::shared_ptr<Ledger const> const& ledger,
149 OrderedTxs const& locals,
150 bool retriesFirst,
151 OrderedTxs& retries,
152 ApplyFlags flags,
153 std::string_view suffix = "",
154 modify_type const& f = {});
155
156private:
162 template <class FwdRange>
163 static void
164 apply(
165 Application& app,
166 OpenView& view,
167 ReadView const& check,
168 FwdRange const& txs,
169 OrderedTxs& retries,
170 ApplyFlags flags,
172
173 enum class Result { Success, Failure, Retry };
174
176 create(Rules const& rules, std::shared_ptr<Ledger const> const& ledger);
177
178 static Result
179 applyOne(
180 Application& app,
181 OpenView& view,
183 bool retry,
184 ApplyFlags flags,
186};
187
188//------------------------------------------------------------------------------
189
190template <class FwdRange>
191void
193 Application& app,
194 OpenView& view,
195 ReadView const& check,
196 FwdRange const& txs,
197 OrderedTxs& retries,
198 ApplyFlags flags,
200{
201 for (auto iter = txs.begin(); iter != txs.end(); ++iter)
202 {
203 try
204 {
205 // Dereferencing the iterator can throw since it may be transformed.
206 auto const tx = *iter;
207 auto const txId = tx->getTransactionID();
208 if (check.txExists(txId))
209 continue;
210 auto const result = applyOne(app, view, tx, true, flags, j);
211 if (result == Result::Retry)
212 retries.insert(tx);
213 }
214 catch (std::exception const& e)
215 {
216 JLOG(j.error()) << "OpenLedger::apply: Caught exception: " << e.what();
217 }
218 }
219 bool retry = true;
220 for (int pass = 0; pass < LEDGER_TOTAL_PASSES; ++pass)
221 {
222 int changes = 0;
223 auto iter = retries.begin();
224 while (iter != retries.end())
225 {
226 switch (applyOne(app, view, iter->second, retry, flags, j))
227 {
228 case Result::Success:
229 ++changes;
230 [[fallthrough]];
231 case Result::Failure:
232 iter = retries.erase(iter);
233 break;
234 case Result::Retry:
235 ++iter;
236 }
237 }
238 // A non-retry pass made no changes
239 if (!changes && !retry)
240 return;
241 // Stop retriable passes
242 if (!changes || (pass >= LEDGER_RETRY_PASSES))
243 retry = false;
244 }
245
246 // If there are any transactions left, we must have
247 // tried them in at least one final pass
248 XRPL_ASSERT(retries.empty() || !retry, "xrpl::OpenLedger::apply : valid retries");
249}
250
251//------------------------------------------------------------------------------
252
253// For debug logging
254
257
260
262debugTostr(SHAMap const& set);
263
266
267} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Stream error() const
Definition Journal.h:315
Holds transactions which were deferred to the next pass of consensus.
const_iterator begin() const
const_iterator erase(const_iterator const &it)
void insert(std::shared_ptr< STTx const > txn)
const_iterator end() const
static Result applyOne(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, bool retry, ApplyFlags flags, beast::Journal j)
beast::Journal const j_
Definition OpenLedger.h:35
bool modify(modify_type const &f)
Modify the open ledger.
std::function< bool(OpenView &, beast::Journal)> modify_type
Signature for modification functions.
Definition OpenLedger.h:53
OpenLedger(OpenLedger const &)=delete
bool empty() const
Returns true if there are no transactions.
std::mutex currentMutex_
Definition OpenLedger.h:38
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
std::mutex modifyMutex_
Definition OpenLedger.h:37
std::shared_ptr< OpenView > create(Rules const &rules, std::shared_ptr< Ledger const > const &ledger)
std::shared_ptr< OpenView const > current_
Definition OpenLedger.h:39
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:192
CachedSLEs & cache_
Definition OpenLedger.h:36
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_view suffix="", modify_type const &f={})
Accept a new ledger.
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:33
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition SHAMap.h:77
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
boost::outcome_v2::result< T, std::error_code > Result
Definition b58_utils.h:17
std::string debugTxstr(std::shared_ptr< STTx const > const &tx)
TaggedCache< uint256, SLE const > CachedSLEs
std::string debugTostr(OrderedTxs const &set)
ApplyFlags
Definition ApplyView.h:12
CanonicalTXSet OrderedTxs
Definition OpenLedger.h:27
T what(T... args)