xrpld
Loading...
Searching...
No Matches
LedgerReplayTask.cpp
1#include <xrpld/app/ledger/LedgerReplayTask.h>
2
3#include <xrpld/app/ledger/InboundLedger.h>
4#include <xrpld/app/ledger/InboundLedgers.h>
5#include <xrpld/app/ledger/LedgerMaster.h>
6#include <xrpld/app/ledger/LedgerReplayer.h>
7#include <xrpld/app/ledger/detail/LedgerDeltaAcquire.h>
8#include <xrpld/app/ledger/detail/SkipListAcquire.h>
9#include <xrpld/app/ledger/detail/TimeoutCounter.h>
10#include <xrpld/app/main/Application.h>
11
12#include <xrpl/basics/Log.h>
13#include <xrpl/basics/base_uint.h>
14#include <xrpl/beast/utility/instrumentation.h>
15#include <xrpl/core/Job.h>
16
17#include <algorithm>
18#include <cstdint>
19#include <memory>
20#include <stdexcept>
21#include <vector>
22
23namespace xrpl {
24
27 UInt256 const& finishLedgerHash,
28 std::uint32_t totalNumLedgers)
29 : reason(r), finishHash(finishLedgerHash), totalLedgers(totalNumLedgers)
30{
31 XRPL_ASSERT(
32 finishLedgerHash.isNonZero() && totalNumLedgers > 0,
33 "xrpl::LedgerReplayTask::TaskParameter::TaskParameter : valid "
34 "inputs");
35}
36
37bool
39 UInt256 const& hash,
40 std::uint32_t seq,
41 std::vector<UInt256> const& sList)
42{
43 if (finishHash != hash || sList.size() + 1 < totalLedgers || full)
44 return false;
45
46 finishSeq = seq;
47 skipList = sList;
48 skipList.emplace_back(finishHash);
50 XRPL_ASSERT(
51 startHash.isNonZero(),
52 "xrpl::LedgerReplayTask::TaskParameter::update : nonzero start hash");
54 full = true;
55 return true;
56}
57
58bool
60{
61 if (reason == existingTask.reason)
62 {
63 if (finishHash == existingTask.finishHash && totalLedgers <= existingTask.totalLedgers)
64 {
65 return true;
66 }
67
68 if (existingTask.full)
69 {
70 auto const& exList = existingTask.skipList;
71 if (auto const i = std::ranges::find(exList, finishHash); i != exList.end())
72 {
73 return existingTask.totalLedgers >= totalLedgers + (exList.end() - i) - 1;
74 }
75 }
76 }
77
78 return false;
79}
80
82 Application& app,
83 InboundLedgers& inboundLedgers,
84 LedgerReplayer& replayer,
85 std::shared_ptr<SkipListAcquire>& skipListAcquirer,
86 TaskParameter const& parameter)
88 app,
89 parameter.finishHash,
90 ledger_replay_parameters::kTaskTimeout,
91 {
92 .jobType = JtReplayTask,
93 .jobName = "LedReplTask",
95 },
96 app.getJournal("LedgerReplayTask"))
97 , inboundLedgers_(inboundLedgers)
98 , replayer_(replayer)
99 , parameter_(parameter)
100 , maxTimeouts_(
101 std::max(
104 , skipListAcquirer_(skipListAcquirer)
105{
106 JLOG(journal_.trace()) << "Create " << hash_;
107}
108
110{
111 JLOG(journal_.trace()) << "Destroy " << hash_;
112}
113
114void
116{
117 JLOG(journal_.debug()) << "Task start " << hash_;
118
120 skipListAcquirer_->addDataCallback([wptr](bool good, UInt256 const& hash) {
121 if (auto const sptr = wptr.lock(); sptr)
122 {
123 if (!good)
124 {
125 sptr->cancel();
126 }
127 else
128 {
129 auto const skipListData = sptr->skipListAcquirer_->getData();
130 sptr->updateSkipList(hash, skipListData->ledgerSeq, skipListData->skipList);
131 }
132 }
133 });
134
136 if (!isDone())
137 {
138 trigger(sl);
139 setTimer(sl);
140 }
141}
142
143void
145{
146 JLOG(journal_.trace()) << "trigger " << hash_;
147 if (!parameter_.full)
148 return;
149
150 if (!parent_)
151 {
152 parent_ = app_.getLedgerMaster().getLedgerByHash(parameter_.startHash);
153 if (!parent_)
154 {
155 parent_ = inboundLedgers_.acquire(
157 }
158 if (parent_)
159 {
160 JLOG(journal_.trace())
161 << "Got start ledger " << parameter_.startHash << " for task " << hash_;
162 }
163 }
164
165 tryAdvance(sl);
166}
167
168void
170{
171 JLOG(journal_.trace()) << "Delta " << deltaHash << " ready for task " << hash_;
173 if (!isDone())
174 tryAdvance(sl);
175}
176
177void
179{
180 JLOG(journal_.trace()) << "tryAdvance task " << hash_
181 << (parameter_.full ? ", full parameter" : ", waiting to fill parameter")
182 << ", deltaIndex=" << deltaToBuild_ << ", totalDeltas=" << deltas_.size()
183 << ", parent " << (parent_ ? parent_->header().hash : UInt256());
184
185 bool const shouldTry =
186 parent_ && parameter_.full && parameter_.totalLedgers - 1 == deltas_.size();
187 if (!shouldTry)
188 return;
189
190 try
191 {
192 for (; deltaToBuild_ < deltas_.size(); ++deltaToBuild_)
193 {
194 auto const& delta = deltas_[deltaToBuild_];
195 XRPL_ASSERT(
196 parent_->seq() + 1 == delta->ledgerSeq_,
197 "xrpl::LedgerReplayTask::tryAdvance : consecutive sequence");
198 if (auto const l = delta->tryBuild(parent_); l)
199 {
200 JLOG(journal_.debug())
201 << "Task " << hash_ << " got ledger " << l->header().hash
202 << " deltaIndex=" << deltaToBuild_ << " totalDeltas=" << deltas_.size();
203 parent_ = l;
204 }
205 else
206 {
207 return;
208 }
209 }
210
211 complete_ = true;
212 JLOG(journal_.info()) << "Completed " << hash_;
213 }
214 catch (std::runtime_error const&)
215 {
216 failed_ = true;
217 }
218}
219
220void
222 UInt256 const& hash,
223 std::uint32_t seq,
224 std::vector<UInt256> const& sList)
225{
226 {
227 ScopedLockType const sl(mtx_);
228 if (isDone())
229 return;
230 if (!parameter_.update(hash, seq, sList))
231 {
232 JLOG(journal_.error()) << "Parameter update failed " << hash_;
233 failed_ = true;
234 return;
235 }
236 }
237
238 replayer_.createDeltas(shared_from_this());
240 if (!isDone())
241 trigger(sl);
242}
243
244void
246{
247 JLOG(journal_.trace()) << "timeouts_=" << timeouts_ << " for " << hash_;
249 {
250 failed_ = true;
251 JLOG(journal_.debug()) << "LedgerReplayTask Failed, too many timeouts " << hash_;
252 }
253 else
254 {
255 trigger(sl);
256 }
257}
258
264
265void
267{
269 delta->addDataCallback(parameter_.reason, [wptr](bool good, UInt256 const& hash) {
270 if (auto const sptr = wptr.lock(); sptr)
271 {
272 if (!good)
273 {
274 sptr->cancel();
275 }
276 else
277 {
278 sptr->deltaReady(hash);
279 }
280 }
281 });
282
283 ScopedLockType const sl(mtx_);
284 if (!isDone())
285 {
286 JLOG(journal_.trace()) << "addDelta task " << hash_ << " deltaIndex=" << deltaToBuild_
287 << " totalDeltas=" << deltas_.size();
288 XRPL_ASSERT(
289 deltas_.empty() || deltas_.back()->ledgerSeq_ + 1 == delta->ledgerSeq_,
290 "xrpl::LedgerReplayTask::addDelta : no deltas or consecutive "
291 "sequence");
292 deltas_.push_back(delta);
293 }
294}
295
296bool
298{
299 ScopedLockType const sl(mtx_);
300 return isDone();
301}
302
303} // namespace xrpl
bool isNonZero() const
Definition base_uint.h:568
Manages the lifetime of inbound ledgers.
bool update(UInt256 const &hash, std::uint32_t seq, std::vector< UInt256 > const &sList)
fill all the fields that was not filled during construction
bool canMergeInto(TaskParameter const &existingTask) const
check if this task can be merged into an existing task
TaskParameter(InboundLedger::Reason r, UInt256 const &finishLedgerHash, std::uint32_t totalNumLedgers)
constructor
void trigger(ScopedLockType &sl)
Trigger another round.
void onTimer(bool progress, ScopedLockType &sl) override
Hook called from invokeOnTimer().
LedgerReplayer & replayer_
LedgerReplayTask(Application &app, InboundLedgers &inboundLedgers, LedgerReplayer &replayer, std::shared_ptr< SkipListAcquire > &skipListAcquirer, TaskParameter const &parameter)
Constructor.
void updateSkipList(UInt256 const &hash, std::uint32_t seq, std::vector< UInt256 > const &sList)
Update this task (by a SkipListAcquire subtask) when skip list is ready.
InboundLedgers & inboundLedgers_
void tryAdvance(ScopedLockType &sl)
Try to build more ledgers.
std::weak_ptr< TimeoutCounter > pmDowncast() override
Return a weak pointer to this.
std::vector< std::shared_ptr< LedgerDeltaAcquire > > deltas_
bool finished() const
return if the task is finished
void deltaReady(UInt256 const &deltaHash)
Notify this task (by a LedgerDeltaAcquire subtask) that a delta is ready.
void addDelta(std::shared_ptr< LedgerDeltaAcquire > const &delta)
add a new LedgerDeltaAcquire subtask
std::shared_ptr< Ledger const > parent_
std::shared_ptr< SkipListAcquire > skipListAcquirer_
void init()
Start the task.
Manages the lifetime of ledger replay tasks.
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_.
T find(T... args)
T lock(T... args)
T max(T... args)
constexpr std::uint32_t kTaskMaxTimeoutsMultiplier
constexpr std::uint32_t kMaxQueuedTasks
constexpr std::uint32_t kTaskMaxTimeoutsMinimum
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
T size(T... args)