xrpld
Loading...
Searching...
No Matches
SkipListAcquire.cpp
1#include <xrpld/app/ledger/detail/SkipListAcquire.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/TimeoutCounter.h>
8#include <xrpld/app/main/Application.h>
9#include <xrpld/overlay/Peer.h>
10#include <xrpld/overlay/PeerSet.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#include <xrpl/ledger/entries/LedgerHashesEntry.h>
17#include <xrpl/protocol/Indexes.h>
18#include <xrpl/protocol/SField.h>
19#include <xrpl/shamap/SHAMapItem.h>
20
21#include <boost/smart_ptr/intrusive_ptr.hpp>
22
23#include <xrpl.pb.h>
24
25#include <cstddef>
26#include <cstdint>
27#include <memory>
28#include <utility>
29#include <vector>
30
31namespace xrpl {
32
34 Application& app,
35 InboundLedgers& inboundLedgers,
36 UInt256 const& ledgerHash,
39 app,
40 ledgerHash,
41 ledger_replay_parameters::kSubTaskTimeout,
42 {
43 .jobType = JtReplayTask,
44 .jobName = "SkipListAcq",
46 },
47 app.getJournal("LedgerReplaySkipList"))
48 , inboundLedgers_(inboundLedgers)
49 , peerSet_(std::move(peerSet))
50{
51 JLOG(journal_.trace()) << "Create " << hash_;
52}
53
55{
56 JLOG(journal_.trace()) << "Destroy " << hash_;
57}
58
59void
61{
63 if (!isDone())
64 {
65 trigger(numPeers, sl);
66 setTimer(sl);
67 }
68}
69
70void
72{
73 if (auto const l = app_.getLedgerMaster().getLedgerByHash(hash_); l)
74 {
75 JLOG(journal_.trace()) << "existing ledger " << hash_;
76 retrieveSkipList(l, sl);
77 return;
78 }
79
80 if (!fallBack_)
81 {
82 peerSet_->addPeers(
83 limit,
84 [this](auto peer) {
85 return peer->supportsFeature(ProtocolFeature::LedgerReplay) &&
86 peer->hasLedger(hash_, 0);
87 },
88 [this](auto peer) {
89 if (peer->supportsFeature(ProtocolFeature::LedgerReplay))
90 {
91 JLOG(journal_.trace()) << "Add a peer " << peer->id() << " for " << hash_;
92 protocol::TMProofPathRequest request;
93 request.set_ledgerhash(hash_.data(), hash_.size());
94 request.set_key(keylet::skip().key.data(), keylet::skip().key.size());
95 request.set_type(protocol::TMLedgerMapType::lmACCOUNT_STATE);
96 peerSet_->sendRequest(request, peer);
97 }
98 else
99 {
100 JLOG(journal_.trace())
101 << "Add a no feature peer " << peer->id() << " for " << hash_;
103 {
104 JLOG(journal_.debug()) << "Fall back for " << hash_;
106 fallBack_ = true;
107 }
108 }
109 });
110 }
111
112 if (fallBack_)
114}
115
116void
118{
119 JLOG(journal_.trace()) << "timeouts_=" << timeouts_ << " for " << hash_;
121 {
122 failed_ = true;
123 JLOG(journal_.debug()) << "too many timeouts " << hash_;
124 notify(sl);
125 }
126 else
127 {
128 trigger(1, sl);
129 }
130}
131
137
138void
140 std::uint32_t ledgerSeq,
141 boost::intrusive_ptr<SHAMapItem const> const& item)
142{
143 XRPL_ASSERT(ledgerSeq != 0 && item, "xrpl::SkipListAcquire::processData : valid inputs");
145 if (isDone())
146 return;
147
148 JLOG(journal_.trace()) << "got data for " << hash_;
149 try
150 {
151 if (auto const sle = std::make_shared<SLE>(SerialIter{item->slice()}, item->key()); sle)
152 {
153 if (auto const& skipList = sle->getFieldV256(sfHashes).value(); !skipList.empty())
154 onSkipListAcquired(skipList, ledgerSeq, sl);
155 return;
156 }
157 }
158 catch (...) // NOLINT(bugprone-empty-catch)
159 {
160 }
161
162 failed_ = true;
163 JLOG(journal_.error()) << "failed to retrieve Skip list from verified data " << hash_;
164 notify(sl);
165}
166
167void
169{
171 dataReadyCallbacks_.emplace_back(std::move(cb));
172 if (isDone())
173 {
174 JLOG(journal_.debug()) << "task added to a finished SkipListAcquire " << hash_;
175 notify(sl);
176 }
177}
178
181{
182 ScopedLockType const sl(mtx_);
183 return data_;
184}
185
186void
188{
189 if (LedgerHashesEntryR const hashIndex(*ledger, journal_);
190 hashIndex && hashIndex->isFieldPresent(sfHashes))
191 {
192 auto const& slist = hashIndex->getFieldV256(sfHashes).value();
193 if (!slist.empty())
194 {
195 onSkipListAcquired(slist, ledger->seq(), sl);
196 return;
197 }
198 }
199
200 failed_ = true;
201 JLOG(journal_.error()) << "failed to retrieve Skip list from a ledger " << hash_;
202 notify(sl);
203}
204
205void
207 std::vector<UInt256> const& skipList,
208 std::uint32_t ledgerSeq,
209 ScopedLockType& sl)
210{
211 complete_ = true;
212 data_ = std::make_shared<SkipListData>(ledgerSeq, skipList);
213 JLOG(journal_.debug()) << "Skip list acquired " << hash_;
214 notify(sl);
215}
216
217void
219{
220 XRPL_ASSERT(isDone(), "xrpl::SkipListAcquire::notify : is done");
223 auto const good = !failed_;
224 sl.unlock();
225
226 for (auto const& cb : toCall)
227 {
228 cb(good, hash_);
229 }
230
231 sl.lock();
232}
233
234} // namespace xrpl
Manages the lifetime of inbound ledgers.
void addDataCallback(OnSkipListDataCB &&cb)
Add a callback that will be called when the skipList is ready or failed.
std::uint32_t noFeaturePeerCount_
std::weak_ptr< TimeoutCounter > pmDowncast() override
Return a weak pointer to this.
std::unique_ptr< PeerSet > peerSet_
std::shared_ptr< SkipListData const > data_
void trigger(std::size_t limit, ScopedLockType &sl)
Trigger another round.
void notify(ScopedLockType &sl)
Call the OnSkipListDataCB callbacks.
std::function< void(bool successful, UInt256 const &hash)> OnSkipListDataCB
A callback used to notify that the SkipList is ready or failed.
std::vector< OnSkipListDataCB > dataReadyCallbacks_
std::shared_ptr< SkipListData const > getData() const
void onTimer(bool progress, ScopedLockType &peerSetLock) override
Hook called from invokeOnTimer().
InboundLedgers & inboundLedgers_
void onSkipListAcquired(std::vector< UInt256 > const &skipList, std::uint32_t ledgerSeq, ScopedLockType &sl)
Process the skip list.
SkipListAcquire(Application &app, InboundLedgers &inboundLedgers, UInt256 const &ledgerHash, std::unique_ptr< PeerSet > peerSet)
Constructor.
void retrieveSkipList(std::shared_ptr< Ledger const > const &ledger, ScopedLockType &sl)
Retrieve the skip list from the ledger.
void processData(std::uint32_t ledgerSeq, boost::intrusive_ptr< SHAMapItem const > const &item)
Process the data extracted from a peer's reply.
void init(int numPeers)
Start the SkipListAcquire task.
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)
Keylet const & skip() noexcept
The index of the "short" skip list.
Definition Indexes.cpp:245
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
LedgerHashesEntry< ReadView > LedgerHashesEntryR
T swap(T... args)
T unlock(T... args)