xrpld
Loading...
Searching...
No Matches
TransactionAcquire.cpp
1#include <xrpld/app/ledger/detail/TransactionAcquire.h>
2
3#include <xrpld/app/ledger/ConsensusTransSetSF.h>
4#include <xrpld/app/ledger/InboundTransactions.h>
5#include <xrpld/app/ledger/detail/TimeoutCounter.h>
6#include <xrpld/app/main/Application.h>
7#include <xrpld/overlay/PeerSet.h>
8
9#include <xrpl/basics/Log.h>
10#include <xrpl/basics/base_uint.h>
11#include <xrpl/beast/utility/instrumentation.h>
12#include <xrpl/core/Job.h>
13#include <xrpl/server/NetworkOPs.h>
14#include <xrpl/shamap/SHAMap.h>
15#include <xrpl/shamap/SHAMapAddNode.h>
16#include <xrpl/shamap/SHAMapMissingNode.h>
17#include <xrpl/shamap/SHAMapTreeNode.h>
18
19#include <xrpl.pb.h>
20
21#include <algorithm>
22#include <chrono>
23#include <cstddef>
24#include <exception>
25#include <memory>
26#include <utility>
27#include <vector>
28
29namespace xrpl {
30
31static constexpr auto kNormTimeouts = 4;
32static constexpr auto kMaxTimeouts = 20;
33
35 Application& app,
36 UInt256 const& hash,
38 std::chrono::milliseconds retryInterval)
40 app,
41 hash,
42 retryInterval,
43 {.jobType = JtTxnData, .jobName = "TxAcq", .jobLimit = {}},
44 app.getJournal("TransactionAcquire"))
45 , peerSet_(std::move(peerSet))
46{
48 map_->setUnbacked();
49}
50
51void
53{
54 // mtx_ is held, so this may only post real work rather than do it.
55
56 if (failed_)
57 {
58 JLOG(journal_.debug()) << "Failed to acquire TX set " << hash_;
59 }
60 else if (!map_->setImmutable())
61 {
62 // trigger() verified the map before setting complete_ and mtx_ has been held since, and
63 // nothing walks this map with the lock released, so it is still valid. UNREACHABLE for
64 // that reason, with the flags still withdrawn, since UNREACHABLE need not stop here.
65 // LCOV_EXCL_START
66 // Withdraw complete_ alongside the failure, for trigger() and takeNodes(), which both
67 // check complete_ before failed_.
68 complete_ = false;
69 failed_ = true;
70 JLOG(journal_.debug()) << "Failed to acquire TX set " << hash_;
71 UNREACHABLE("xrpl::TransactionAcquire::done : map is invalid");
72 // LCOV_EXCL_STOP
73 }
74 else
75 {
76 JLOG(journal_.debug()) << "Acquired TX set " << hash_;
77
78 UInt256 const& hash(hash_);
79 std::shared_ptr<SHAMap> const& map(map_);
80 auto const pap = &app_;
81 // Note that, when we're in the process of shutting down, addJob()
82 // may reject the request. If that happens then giveSet() will
83 // not be called. That's fine. According to David the giveSet() call
84 // just updates the consensus and related structures when we acquire
85 // a transaction set. No need to update them if we're shutting down.
86 app_.getJobQueue().addJob(JtTxnData, "ComplAcquire", [pap, hash, map] {
87 pap->getInboundTransactions().giveSet(hash, map, true);
88 });
89 }
90}
91
92void
94{
96 {
97 failed_ = true;
98 done();
99 return;
100 }
101
103 trigger(nullptr);
104
105 addPeers(1);
106}
107
113
114void
116{
117 if (complete_)
118 {
119 JLOG(journal_.info()) << "trigger after complete";
120 return;
121 }
122 if (failed_)
123 {
124 JLOG(journal_.info()) << "trigger after fail";
125 return;
126 }
127
128 if (!haveRoot_)
129 {
130 JLOG(journal_.trace()) << "TransactionAcquire::trigger " << (peer ? "havePeer" : "noPeer")
131 << " no root";
132 protocol::TMGetLedger tmGL;
133 tmGL.set_ledgerhash(hash_.begin(), hash_.size());
134 tmGL.set_itype(protocol::liTS_CANDIDATE);
135 tmGL.set_querydepth(3); // We probably need the whole thing
136
137 if (timeouts_ != 0)
138 tmGL.set_querytype(protocol::qtINDIRECT);
139
140 *tmGL.add_nodeids() = SHAMapNodeID().getRawString();
141 peerSet_->sendRequest(tmGL, peer);
142 }
143 else if (!map_->isValid())
144 {
145 failed_ = true;
146 done();
147 }
148 else
149 {
150 ConsensusTransSetSF sf(app_, app_.getTempNodeCache());
151 auto const nodes = map_->getMissingNodes(256, &sf);
152
153 if (nodes.empty())
154 {
155 if (map_->isValid())
156 {
157 complete_ = true;
158 }
159 else
160 {
161 failed_ = true;
162 }
163
164 done();
165 return;
166 }
167
168 protocol::TMGetLedger tmGL;
169 tmGL.set_ledgerhash(hash_.begin(), hash_.size());
170 tmGL.set_itype(protocol::liTS_CANDIDATE);
171
172 if (timeouts_ != 0)
173 tmGL.set_querytype(protocol::qtINDIRECT);
174
175 for (auto const& node : nodes)
176 {
177 *tmGL.add_nodeids() = node.first.getRawString();
178 }
179 peerSet_->sendRequest(tmGL, peer);
180 }
181}
182
186 std::shared_ptr<Peer> const& peer)
187{
188 ScopedLockType const sl(mtx_);
189
190 if (complete_)
191 {
192 JLOG(journal_.trace()) << "TX set complete";
193 return SHAMapAddNode();
194 }
195
196 if (failed_)
197 {
198 JLOG(journal_.trace()) << "TX set failed";
199 return SHAMapAddNode();
200 }
201
202 try
203 {
204 if (data.empty())
205 return SHAMapAddNode::invalid();
206
207 ConsensusTransSetSF sf(app_, app_.getTempNodeCache());
208
209 for (auto& d : data)
210 {
211 if (d.first.isRoot())
212 {
213 if (haveRoot_)
214 {
215 JLOG(journal_.debug()) << "Got root TXS node, already have it";
216 }
217 else if (!map_->addRootNode(SHAMapHash{hash_}, std::move(d.second), nullptr)
218 .isGood())
219 {
220 JLOG(journal_.warn()) << "TX acquire got bad root node for TX set " << hash_
221 << " from peer " << peer->id();
222 return SHAMapAddNode::invalid();
223 }
224 else
225 {
226 haveRoot_ = true;
227 }
228 }
229 else if (!map_->addKnownNode(d.first, std::move(d.second), &sf).isGood())
230 {
231 JLOG(journal_.warn()) << "TX acquire got bad non-root node " << d.first
232 << " for TX set " << hash_ << " from peer " << peer->id();
233 return SHAMapAddNode::invalid();
234 }
235 }
236
237 trigger(peer);
238 progress_ = true;
239 return SHAMapAddNode::useful();
240 }
241 catch (std::exception const& ex)
242 {
243 JLOG(journal_.error()) << "Peer " << peer->id()
244 << " sent us junky transaction node data: " << ex.what();
245 return SHAMapAddNode::invalid();
246 }
247}
248
249void
251{
252 peerSet_->addPeers(
253 limit,
254 [this](auto peer) { return peer->hasTxSet(hash_); },
255 [this](auto peer) { trigger(peer); });
256}
257
258void
260{
262
263 addPeers(numPeers);
264
265 setTimer(sl);
266}
267
268void
276
277} // namespace xrpl
static SHAMapAddNode useful()
static SHAMapAddNode invalid()
Identifies a node inside a SHAMap.
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)
bool progress_
Whether forward progress has been made.
beast::Journal journal_
void setTimer(ScopedLockType &)
Schedule a call to queueJob() after timerInterval_.
void done()
Settle the acquired set and hand it on, or report the failure.
SHAMapAddNode takeNodes(std::vector< std::pair< SHAMapNodeID, SHAMapTreeNodePtr > > data, std::shared_ptr< Peer > const &peer)
std::shared_ptr< SHAMap > map_
void addPeers(std::size_t limit)
void onTimer(bool progress, ScopedLockType &sl) override
Hook called from invokeOnTimer().
void trigger(std::shared_ptr< Peer > const &)
TransactionAcquire(Application &app, UInt256 const &hash, std::unique_ptr< PeerSet > peerSet, std::chrono::milliseconds retryInterval=kRetryInterval)
std::weak_ptr< TimeoutCounter > pmDowncast() override
Return a weak pointer to this.
std::unique_ptr< PeerSet > peerSet_
T make_shared(T... args)
T min(T... args)
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
@ JtTxnData
Definition Job.h:55
static constexpr auto kNormTimeouts
static constexpr auto kMaxTimeouts
T what(T... args)