xrpld
Loading...
Searching...
No Matches
BuildLedger.cpp
1#include <xrpld/app/ledger/BuildLedger.h>
2
3#include <xrpld/app/ledger/LedgerReplay.h>
4#include <xrpld/app/ledger/OpenLedger.h>
5#include <xrpld/app/main/Application.h>
6
7#include <xrpl/basics/Log.h>
8#include <xrpl/basics/chrono.h>
9#include <xrpl/basics/contract.h>
10#include <xrpl/beast/utility/Journal.h>
11#include <xrpl/beast/utility/instrumentation.h>
12#include <xrpl/ledger/ApplyView.h>
13#include <xrpl/ledger/CanonicalTXSet.h>
14#include <xrpl/ledger/Ledger.h>
15#include <xrpl/ledger/OpenView.h>
16#include <xrpl/ledger/entries/FeeSettingsEntry.h>
17#include <xrpl/nodestore/NodeObject.h>
18#include <xrpl/protocol/Indexes.h> // IWYU pragma: keep
19#include <xrpl/protocol/LedgerHeader.h>
20#include <xrpl/protocol/Protocol.h>
21#include <xrpl/protocol/SystemParameters.h> // IWYU pragma: keep
22#include <xrpl/protocol/TxFlags.h>
23#include <xrpl/tx/apply.h>
24
25#include <cstddef>
26#include <exception>
27#include <memory>
28#include <set>
29
30namespace xrpl {
31
32/* Generic buildLedgerImpl that dispatches to ApplyTxs invocable with signature
33 void(OpenView&, std::shared_ptr<Ledger> const&)
34 It is responsible for adding transactions to the open view to generate the
35 new ledger. It is generic since the mechanics differ for consensus
36 generated ledgers versus replayed ledgers.
37*/
38template <class ApplyTxs>
39std::shared_ptr<Ledger>
42 NetClock::time_point closeTime,
43 bool const closeTimeCorrect,
44 NetClock::duration closeResolution,
45 Application& app,
47 ApplyTxs&& applyTxs)
48{
49 auto built = std::make_shared<Ledger>(*parent, closeTime);
50
51 if (built->isFlagLedger())
52 {
53 built->updateNegativeUNL();
54 }
55
56 // Set up to write SHAMap changes to our database,
57 // perform updates, extract changes
58
59 {
60 OpenView accum(&*built);
61 XRPL_ASSERT(!accum.open(), "xrpl::buildLedgerImpl : valid ledger state");
62 applyTxs(accum, built);
63 accum.apply(*built);
64 }
65
66 built->updateSkipList();
67 {
68 // Write the final version of all modified SHAMap
69 // nodes to the node store to preserve the new LCL
70
71 int const asf = built->stateMap().flushDirty(NodeObjectType::AccountNode);
72 int const tmf = built->txMap().flushDirty(NodeObjectType::TransactionNode);
73 JLOG(j.debug()) << "Flushed " << asf << " accounts and " << tmf << " transaction nodes";
74 }
75 built->unshare();
76
77 // Accept ledger
78 XRPL_ASSERT(
79 built->header().seq < kXrpLedgerEarliestFees || FeeSettingsEntryR(*built),
80 "xrpl::buildLedgerImpl : valid ledger fees");
81 // The invariant: a ledger this function returns has both maps immutable, which is what
82 // setAccepted() reports on (see Ledger::setImmutable()). Nothing downstream re-checks it.
83 //
84 // logicError() rather than UNREACHABLE(): the consensus caller acts on this ledger straight
85 // away, so the stop has to name this site in every build rather than only where assertions
86 // are on. See RCLConsensus::Adaptor::buildLCL().
87 if (!built->setAccepted(closeTime, closeResolution, closeTimeCorrect))
88 {
89 // LCOV_EXCL_START
90 logicError("buildLedgerImpl: accepted ledger map is invalid");
91 // LCOV_EXCL_STOP
92 }
93
94 return built;
95}
96
107
110 Application& app,
112 CanonicalTXSet& txns,
113 std::set<TxID>& failed,
114 OpenView& view,
116{
117 bool certainRetry = true;
118 std::size_t count = 0;
119
120 // Attempt to apply all of the retriable transactions
121 for (int pass = 0; pass < LEDGER_TOTAL_PASSES; ++pass)
122 {
123 JLOG(j.debug()) << (certainRetry ? "Pass: " : "Final pass: ") << pass << " begins ("
124 << txns.size() << " transactions)";
125 int changes = 0;
126
127 auto it = txns.begin();
128
129 while (it != txns.end())
130 {
131 auto const txid = it->first.getTXID();
132
133 try
134 {
135 if (pass == 0 && built->txExists(txid))
136 {
137 it = txns.erase(it);
138 continue;
139 }
140
141 switch (applyTransaction(app, view, *it->second, certainRetry, TapNone, j))
142 {
144 it = txns.erase(it);
145 ++changes;
146 break;
147
149 failed.insert(txid);
150 it = txns.erase(it);
151 break;
152
154 ++it;
155 }
156 }
157 catch (std::exception const& ex)
158 {
159 JLOG(j.warn()) << "Transaction " << txid << " throws: " << ex.what();
160 failed.insert(txid);
161 it = txns.erase(it);
162 }
163 }
164
165 JLOG(j.debug()) << (certainRetry ? "Pass: " : "Final pass: ") << pass << " completed ("
166 << changes << " changes)";
167
168 // Accumulate changes.
169 count += changes;
170
171 // A non-retry pass made no changes
172 if ((changes == 0) && !certainRetry)
173 break;
174
175 // Stop retriable passes
176 if ((changes == 0) || (pass >= LEDGER_RETRY_PASSES))
177 certainRetry = false;
178 }
179
180 // If there are any transactions left, we must have
181 // tried them in at least one final pass
182 XRPL_ASSERT(txns.empty() || !certainRetry, "xrpl::applyTransactions : retry transactions");
183 return count;
184}
185
186// Build a ledger from consensus transactions
189 std::shared_ptr<Ledger const> const& parent,
190 NetClock::time_point closeTime,
191 bool const closeTimeCorrect,
192 NetClock::duration closeResolution,
193 Application& app,
194 CanonicalTXSet& txns,
195 std::set<TxID>& failedTxns,
197{
198 JLOG(j.debug()) << "Report: Transaction Set = " << txns.key() << ", close "
199 << closeTime.time_since_epoch().count()
200 << (closeTimeCorrect ? "" : " (incorrect)");
201
202 return buildLedgerImpl(
203 parent,
204 closeTime,
205 closeTimeCorrect,
206 closeResolution,
207 app,
208 j,
209 [&](OpenView& accum, std::shared_ptr<Ledger> const& built) {
210 JLOG(j.debug()) << "Attempting to apply " << txns.size() << " transactions";
211
212 auto const applied = applyTransactions(app, built, txns, failedTxns, accum, j);
213
214 if (!txns.empty() || !failedTxns.empty())
215 {
216 JLOG(j.debug()) << "Applied " << applied << " transactions; " << failedTxns.size()
217 << " failed and " << txns.size() << " will be retried. "
218 << "Total transactions in ledger (including Inner Batch): "
219 << accum.txCount();
220 }
221 else
222 {
223 JLOG(j.debug()) << "Applied " << applied << " transactions. "
224 << "Total transactions in ledger (including Inner Batch): "
225 << accum.txCount();
226 }
227 });
228}
229
230// Build a ledger by replaying
233 LedgerReplay const& replayData,
234 ApplyFlags applyFlags,
235 Application& app,
237{
238 auto const& replayLedger = replayData.replay();
239
240 JLOG(j.debug()) << "Report: Replay Ledger " << replayLedger->header().hash;
241
242 return buildLedgerImpl(
243 replayData.parent(),
244 replayLedger->header().closeTime,
245 ((replayLedger->header().closeFlags & kSLcfNoConsensusTime) == 0),
246 replayLedger->header().closeTimeResolution,
247 app,
248 j,
249 [&](OpenView& accum, std::shared_ptr<Ledger> const& built) {
250 for (auto& tx : replayData.orderedTxns())
251 {
252 // Inner batch transactions are applied as part of their outer
253 // Batch transaction, never on their own. Skip them here so they
254 // are not re-applied a second time outside of the batch during
255 // replay.
256 if (tx.second->isFlag(tfInnerBatchTxn))
257 continue;
258 applyTransaction(app, accum, *tx.second, false, applyFlags, j);
259 }
260 });
261}
262
263} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream debug() const
Definition Journal.h:344
Stream warn() const
Definition Journal.h:356
Holds transactions which were deferred to the next pass of consensus.
UInt256 const & key() const
const_iterator begin() const
const_iterator erase(const_iterator const &it)
const_iterator end() const
std::shared_ptr< Ledger const > const & replay() const
std::shared_ptr< Ledger const > const & parent() const
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
std::size_t txCount() const
Return the number of tx inserted since creation.
Definition OpenView.cpp:123
bool open() const override
Returns true if this reflects an open ledger.
Definition OpenView.h:187
void apply(TxsRawView &to) const
Apply changes.
Definition OpenView.cpp:129
T empty(T... args)
T insert(T... args)
T make_shared(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::size_t applyTransactions(Application &app, std::shared_ptr< Ledger const > const &built, CanonicalTXSet &txns, std::set< TxID > &failed, OpenView &view, beast::Journal j)
Apply a set of consensus transactions to a ledger.
ApplyTransactionResult applyTransaction(ServiceRegistry &registry, OpenView &view, STTx const &tx, bool retryAssured, ApplyFlags flags, beast::Journal journal)
Transaction application helper.
Definition apply.cpp:267
@ Success
Applied to this ledger.
Definition apply.h:128
@ Retry
Should be retried in this ledger.
Definition apply.h:136
@ Fail
Should not be retried in this ledger.
Definition apply.h:132
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
static constexpr std::uint32_t kXrpLedgerEarliestFees
The XRP Ledger mainnet's earliest ledger with a FeeSettings object.
std::shared_ptr< Ledger > buildLedger(std::shared_ptr< Ledger const > const &parent, NetClock::time_point closeTime, bool const closeTimeCorrect, NetClock::duration closeResolution, Application &app, CanonicalTXSet &txns, std::set< TxID > &failedTxs, beast::Journal j)
Build a new ledger by applying consensus transactions.
ApplyFlags
Definition ApplyView.h:27
@ TapNone
Definition ApplyView.h:28
static std::uint32_t const kSLcfNoConsensusTime
FeeSettingsEntry< ReadView > FeeSettingsEntryR
std::shared_ptr< Ledger > buildLedgerImpl(std::shared_ptr< Ledger const > const &parent, NetClock::time_point closeTime, bool const closeTimeCorrect, NetClock::duration closeResolution, Application &app, beast::Journal j, ApplyTxs &&applyTxs)
T size(T... args)
T time_since_epoch(T... args)
T what(T... args)