rippled
Loading...
Searching...
No Matches
OpenLedger.h
1#ifndef XRPL_APP_LEDGER_OPENLEDGER_H_INCLUDED
2#define XRPL_APP_LEDGER_OPENLEDGER_H_INCLUDED
3
4#include <xrpld/app/ledger/Ledger.h>
5#include <xrpld/app/misc/CanonicalTXSet.h>
6#include <xrpld/core/Config.h>
7
8#include <xrpl/basics/Log.h>
9#include <xrpl/basics/UnorderedContainers.h>
10#include <xrpl/beast/utility/Journal.h>
11#include <xrpl/beast/utility/instrumentation.h>
12#include <xrpl/ledger/CachedSLEs.h>
13#include <xrpl/ledger/OpenView.h>
14
15#include <mutex>
16
17namespace ripple {
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 const& 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
174
176 create(Rules const& rules, std::shared_ptr<Ledger const> const& ledger);
177
178 static Result
179 apply_one(
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 = apply_one(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())
217 << "OpenLedger::apply: Caught exception: " << e.what();
218 }
219 }
220 bool retry = true;
221 for (int pass = 0; pass < LEDGER_TOTAL_PASSES; ++pass)
222 {
223 int changes = 0;
224 auto iter = retries.begin();
225 while (iter != retries.end())
226 {
227 switch (apply_one(app, view, iter->second, retry, flags, j))
228 {
229 case Result::success:
230 ++changes;
231 [[fallthrough]];
232 case Result::failure:
233 iter = retries.erase(iter);
234 break;
235 case Result::retry:
236 ++iter;
237 }
238 }
239 // A non-retry pass made no changes
240 if (!changes && !retry)
241 return;
242 // Stop retriable passes
243 if (!changes || (pass >= LEDGER_RETRY_PASSES))
244 retry = false;
245 }
246
247 // If there are any transactions left, we must have
248 // tried them in at least one final pass
249 XRPL_ASSERT(
250 retries.empty() || !retry, "ripple::OpenLedger::apply : valid retries");
251}
252
253//------------------------------------------------------------------------------
254
255// For debug logging
256
259
261debugTostr(OrderedTxs const& set);
262
264debugTostr(SHAMap const& set);
265
268
269} // namespace ripple
270
271#endif
A generic endpoint for log messages.
Definition Journal.h:41
Stream error() const
Definition Journal.h:327
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:33
bool modify(modify_type const &f)
Modify the open ledger.
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.
CachedSLEs & cache_
Definition OpenLedger.h:36
OpenLedger(OpenLedger const &)=delete
std::shared_ptr< OpenView > create(Rules const &rules, std::shared_ptr< Ledger const > const &ledger)
bool empty() const
Returns true if there are no transactions.
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
std::mutex modify_mutex_
Definition OpenLedger.h:37
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)
std::shared_ptr< OpenView const > current_
Definition OpenLedger.h:39
beast::Journal const j_
Definition OpenLedger.h:35
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
std::mutex current_mutex_
Definition OpenLedger.h:38
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:46
A view into a ledger.
Definition ReadView.h:32
Rules controlling protocol behavior.
Definition Rules.h:19
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition SHAMap.h:78
Map/cache combination.
Definition TaggedCache.h:43
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
boost::outcome_v2::result< T, std::error_code > Result
Definition b58_utils.h:18
std::string debugTostr(OrderedTxs const &set)
std::string debugTxstr(std::shared_ptr< STTx const > const &tx)
T what(T... args)