xrpld
Loading...
Searching...
No Matches
LedgerDeltaAcquire.cpp
1#include <xrpld/app/ledger/detail/LedgerDeltaAcquire.h>
2
3#include <xrpld/app/ledger/BuildLedger.h>
4#include <xrpld/app/ledger/InboundLedger.h>
5#include <xrpld/app/ledger/InboundLedgers.h>
6#include <xrpld/app/ledger/LedgerMaster.h>
7#include <xrpld/app/ledger/LedgerReplay.h>
8#include <xrpld/app/ledger/LedgerReplayer.h>
9#include <xrpld/app/ledger/detail/TimeoutCounter.h>
10#include <xrpld/app/main/Application.h>
11#include <xrpld/overlay/Peer.h>
12#include <xrpld/overlay/PeerSet.h>
13
14#include <xrpl/basics/Log.h>
15#include <xrpl/basics/base_uint.h>
16#include <xrpl/basics/contract.h>
17#include <xrpl/beast/utility/instrumentation.h>
18#include <xrpl/core/Job.h>
19#include <xrpl/core/JobQueue.h>
20#include <xrpl/ledger/ApplyView.h>
21#include <xrpl/protocol/LedgerHeader.h>
22#include <xrpl/protocol/Rules.h>
23
24#include <xrpl.pb.h>
25
26#include <cstddef>
27#include <cstdint>
28#include <map>
29#include <memory>
30#include <optional>
31#include <stdexcept>
32#include <utility>
33#include <vector>
34
35namespace xrpl {
36
38 Application& app,
39 InboundLedgers& inboundLedgers,
40 UInt256 const& ledgerHash,
41 std::uint32_t ledgerSeq,
44 app,
45 ledgerHash,
46 ledger_replay_parameters::kSubTaskTimeout,
47 {
48 .jobType = JtReplayTask,
49 .jobName = "LedReplDelta",
51 },
52 app.getJournal("LedgerReplayDelta"))
53 , inboundLedgers_(inboundLedgers)
54 , ledgerSeq_(ledgerSeq)
55 , peerSet_(std::move(peerSet))
56{
57 JLOG(journal_.trace()) << "Create " << hash_ << " Seq " << ledgerSeq;
58}
59
61{
62 JLOG(journal_.trace()) << "Destroy " << hash_;
63}
64
65void
67{
69 if (!isDone())
70 {
71 trigger(numPeers, sl);
72 setTimer(sl);
73 }
74}
75
76void
78{
79 fullLedger_ = app_.getLedgerMaster().getLedgerByHash(hash_);
80 if (fullLedger_)
81 {
82 complete_ = true;
83 JLOG(journal_.trace()) << "existing ledger " << hash_;
84 notify(sl);
85 return;
86 }
87
88 if (!fallBack_)
89 {
90 peerSet_->addPeers(
91 limit,
92 [this](auto peer) {
93 return peer->supportsFeature(ProtocolFeature::LedgerReplay) &&
94 peer->hasLedger(hash_, ledgerSeq_);
95 },
96 [this](auto peer) {
97 if (peer->supportsFeature(ProtocolFeature::LedgerReplay))
98 {
99 JLOG(journal_.trace()) << "Add a peer " << peer->id() << " for " << hash_;
100 protocol::TMReplayDeltaRequest request;
101 request.set_ledgerhash(hash_.data(), hash_.size());
102 peerSet_->sendRequest(request, peer);
103 }
104 else
105 {
107 {
108 JLOG(journal_.debug()) << "Fall back for " << hash_;
110 fallBack_ = true;
111 }
112 }
113 });
114 }
115
116 if (fallBack_)
118}
119
120void
122{
123 JLOG(journal_.trace()) << "timeouts_=" << timeouts_ << " for " << hash_;
125 {
126 failed_ = true;
127 JLOG(journal_.debug()) << "too many timeouts " << hash_;
128 notify(sl);
129 }
130 else
131 {
132 trigger(1, sl);
133 }
134}
135
141
142void
144 LedgerHeader const& info,
146{
148 JLOG(journal_.trace()) << "got data for " << hash_;
149 if (isDone())
150 return;
151
152 if (info.seq == ledgerSeq_)
153 {
154 // create a temporary ledger for building a LedgerReplay object later
155 Rules const rules{app_.config().features};
156 replayTemp_ = std::make_shared<Ledger>(info, rules, app_.getNodeFamily());
157 if (replayTemp_)
158 {
159 complete_ = true;
160 orderedTxns_ = std::move(orderedTxns);
161 JLOG(journal_.debug()) << "ready to replay " << hash_;
162 notify(sl);
163 return;
164 }
165 }
166
167 failed_ = true;
168 JLOG(journal_.error()) << "failed to create a (info only) ledger from verified data " << hash_;
169 notify(sl);
170}
171
172void
174{
176 dataReadyCallbacks_.emplace_back(std::move(cb));
177 if (!reasons_.contains(reason))
178 {
179 reasons_.emplace(reason);
180 if (fullLedger_)
181 onLedgerBuilt(sl, reason);
182 }
183
184 if (isDone())
185 {
186 JLOG(journal_.debug()) << "task added to a finished LedgerDeltaAcquire " << hash_;
187 notify(sl);
188 }
189}
190
193{
195
196 if (fullLedger_)
197 return fullLedger_;
198
199 if (failed_ || !complete_ || !replayTemp_)
200 return {};
201
202 XRPL_ASSERT(
203 parent->seq() + 1 == replayTemp_->seq(),
204 "xrpl::LedgerDeltaAcquire::tryBuild : parent sequence match");
205 XRPL_ASSERT(
206 parent->header().hash == replayTemp_->header().parentHash,
207 "xrpl::LedgerDeltaAcquire::tryBuild : parent hash match");
208 // build ledger
209 LedgerReplay const replayData(parent, replayTemp_, std::move(orderedTxns_));
211 if (fullLedger_ && fullLedger_->header().hash == hash_)
212 {
213 JLOG(journal_.info()) << "Built " << hash_;
214 onLedgerBuilt(sl);
215 return fullLedger_;
216 }
217
218 failed_ = true;
219 complete_ = false;
220 JLOG(journal_.error()) << "tryBuild failed " << hash_ << " with parent "
221 << parent->header().hash;
222 Throw<std::runtime_error>("Cannot replay ledger");
223}
224
225void
227{
228 JLOG(journal_.debug()) << "onLedgerBuilt " << hash_ << (reason ? " for a new reason" : "");
229
231 bool firstTime = true;
232 if (reason) // small chance
233 {
234 reasons.clear();
235 reasons.push_back(*reason);
236 firstTime = false;
237 }
238 app_.getJobQueue().addJob(
239 JtReplayTask, "OnLedBuilt", [=, ledger = this->fullLedger_, &app = this->app_] {
240 for (auto const reason : reasons)
241 {
242 switch (reason)
243 {
245 app.getLedgerMaster().storeLedger(ledger);
246 break;
247 default:
248 // TODO for other use cases
249 break;
250 }
251 }
252
253 if (firstTime)
254 app.getLedgerMaster().tryAdvance();
255 });
256}
257
258void
260{
261 XRPL_ASSERT(isDone(), "xrpl::LedgerDeltaAcquire::notify : is done");
264 auto const good = !failed_;
265 sl.unlock();
266
267 for (auto const& cb : toCall)
268 {
269 cb(good, hash_);
270 }
271
272 sl.lock();
273}
274
275} // namespace xrpl
Manages the lifetime of inbound ledgers.
std::uint32_t const ledgerSeq_
void trigger(std::size_t limit, ScopedLockType &sl)
Trigger another round.
void init(int numPeers)
Start the LedgerDeltaAcquire task.
std::shared_ptr< Ledger const > fullLedger_
void onLedgerBuilt(ScopedLockType &sl, std::optional< InboundLedger::Reason > reason={})
Process a newly built ledger, such as store it.
std::vector< OnDeltaDataCB > dataReadyCallbacks_
std::function< void(bool successful, UInt256 const &hash)> OnDeltaDataCB
A callback used to notify that the delta's data is ready or failed.
void processData(LedgerHeader const &info, std::map< std::uint32_t, std::shared_ptr< STTx const > > &&orderedTxns)
Process the data extracted from a peer's reply.
std::unique_ptr< PeerSet > peerSet_
std::set< InboundLedger::Reason > reasons_
void notify(ScopedLockType &sl)
Call the OnDeltaDataCB callbacks.
std::shared_ptr< Ledger const > replayTemp_
void onTimer(bool progress, ScopedLockType &peerSetLock) override
Hook called from invokeOnTimer().
std::weak_ptr< TimeoutCounter > pmDowncast() override
Return a weak pointer to this.
std::shared_ptr< Ledger const > tryBuild(std::shared_ptr< Ledger const > const &parent)
Try to build the ledger if not already.
LedgerDeltaAcquire(Application &app, InboundLedgers &inboundLedgers, UInt256 const &ledgerHash, std::uint32_t ledgerSeq, std::unique_ptr< PeerSet > peerSet)
Constructor.
std::map< std::uint32_t, std::shared_ptr< STTx const > > orderedTxns_
void addDataCallback(InboundLedger::Reason reason, OnDeltaDataCB &&cb)
Add a reason and a callback to the LedgerDeltaAcquire subtask.
Rules controlling protocol behavior.
Definition Rules.h:40
std::recursive_mutex mtx_
std::unique_lock< std::recursive_mutex > ScopedLockType
UInt256 const hash_
The hash of the object (in practice, always a ledger) we are trying to fetch.
TimeoutCounter(Application &app, UInt256 const &targetHash, std::chrono::milliseconds timeoutInterval, QueueJobParameter &&jobParameter, beast::Journal journal)
beast::Journal journal_
void setTimer(ScopedLockType &)
Schedule a call to queueJob() after timerInterval_.
std::chrono::milliseconds timerInterval_
The minimum time to wait between calls to execute().
T lock(T... args)
T make_shared(T... args)
constexpr std::uint32_t kSubTaskMaxTimeouts
constexpr std::uint32_t kMaxQueuedTasks
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 256 > UInt256
Definition base_uint.h:581
@ JtReplayTask
Definition Job.h:47
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.
@ TapNone
Definition ApplyView.h:28
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
Information about the notional ledger backing the view.
T swap(T... args)
T unlock(T... args)