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/beast/utility/Journal.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/core/PerfLog.h>
9#include <xrpl/ledger/ApplyView.h>
10#include <xrpl/ledger/CachedSLEs.h>
11#include <xrpl/ledger/CanonicalTXSet.h>
12#include <xrpl/ledger/Ledger.h>
13#include <xrpl/ledger/OpenView.h>
14#include <xrpl/ledger/ReadView.h>
15#include <xrpl/protocol/STTx.h>
16#include <xrpl/shamap/SHAMap.h>
17
18#include <exception>
19#include <functional>
20#include <memory>
21#include <mutex>
22#include <string>
23#include <string_view>
24
25namespace xrpl {
26
27// How many total extra passes we make
28// We must ensure we make at least one non-retriable pass
29#define LEDGER_TOTAL_PASSES 3
30
31// How many extra retry passes we
32// make if the previous retry pass made changes
33#define LEDGER_RETRY_PASSES 1
34
36
37//------------------------------------------------------------------------------
38
43{
44private:
50
51public:
65
66 OpenLedger() = delete;
67 OpenLedger(OpenLedger const&) = delete;
69 operator=(OpenLedger const&) = delete;
70
76 explicit OpenLedger(
78 CachedSLEs& cache,
79 beast::Journal journal);
80
95 bool
96 empty() const;
97
110 current() const;
111
123 bool
124 modify(modify_type const& f);
125
160 void
161 accept(
162 Application& app,
163 Rules const& rules,
164 std::shared_ptr<Ledger const> const& ledger,
165 OrderedTxs const& locals,
166 bool retriesFirst,
167 OrderedTxs& retries,
168 ApplyFlags flags,
169 std::string_view suffix = "",
170 modify_type const& f = {});
171
172private:
179 template <class FwdRange>
180 static void
181 apply(
182 Application& app,
183 OpenView& view,
184 ReadView const& check,
185 FwdRange const& txs,
186 OrderedTxs& retries,
187 ApplyFlags flags,
189
190 enum class Result { Success, Failure, Retry };
191
193 create(Rules const& rules, std::shared_ptr<Ledger const> const& ledger);
194
195 static Result
196 applyOne(
197 Application& app,
198 OpenView& view,
200 bool retry,
201 ApplyFlags flags,
203};
204
205//------------------------------------------------------------------------------
206
207template <class FwdRange>
208void
210 Application& app,
211 OpenView& view,
212 ReadView const& check,
213 FwdRange const& txs,
214 OrderedTxs& retries,
215 ApplyFlags flags,
217{
218 for (auto iter = txs.begin(); iter != txs.end(); ++iter)
219 {
220 try
221 {
222 // Dereferencing the iterator can throw since it may be transformed.
223 auto const tx = *iter;
224 auto const txId = tx->getTransactionID();
225 if (check.txExists(txId))
226 continue;
227 auto const result = applyOne(app, view, tx, true, flags, j);
228 if (result == Result::Retry)
229 retries.insert(tx);
230 }
231 catch (std::exception const& e)
232 {
233 JLOG(j.error()) << "OpenLedger::apply: Caught exception: " << e.what();
234 }
235 }
236 bool retry = true;
237 for (int pass = 0; pass < LEDGER_TOTAL_PASSES; ++pass)
238 {
239 int changes = 0;
240 auto iter = retries.begin();
241 while (iter != retries.end())
242 {
243 switch (applyOne(app, view, iter->second, retry, flags, j))
244 {
245 case Result::Success:
246 ++changes;
247 [[fallthrough]];
248 case Result::Failure:
249 iter = retries.erase(iter);
250 break;
251 case Result::Retry:
252 ++iter;
253 }
254 }
255 // A non-retry pass made no changes
256 if (!changes && !retry)
257 return;
258 // Stop retriable passes
259 if (!changes || (pass >= LEDGER_RETRY_PASSES))
260 retry = false;
261 }
262
263 // If there are any transactions left, we must have
264 // tried them in at least one final pass
265 XRPL_ASSERT(retries.empty() || !retry, "xrpl::OpenLedger::apply : valid retries");
266}
267
268//------------------------------------------------------------------------------
269
270// For debug logging
271
274
277
279debugTostr(SHAMap const& set);
280
283
284} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream error() const
Definition Journal.h:362
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:45
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:64
OpenLedger(OpenLedger const &)=delete
bool empty() const
Returns true if there are no transactions.
std::mutex currentMutex_
Definition OpenLedger.h:48
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
std::mutex modifyMutex_
Definition OpenLedger.h:47
std::shared_ptr< OpenView > create(Rules const &rules, std::shared_ptr< Ledger const > const &ledger)
std::shared_ptr< OpenView const > current_
Definition OpenLedger.h:49
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:209
CachedSLEs & cache_
Definition OpenLedger.h:46
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:59
A view into a ledger.
Definition ReadView.h:41
Rules controlling protocol behavior.
Definition Rules.h:40
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:19
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:27
CanonicalTXSet OrderedTxs
Definition OpenLedger.h:35
T what(T... args)