xrpld
Loading...
Searching...
No Matches
TransactionAcquire_test.cpp
1#include <test/app/AcquireTestHelpers.h>
2#include <test/jtx/Env.h>
3
4#include <xrpld/app/ledger/InboundTransactions.h>
5#include <xrpld/app/ledger/detail/TransactionAcquire.h>
6#include <xrpld/overlay/Peer.h>
7
8#include <xrpl/basics/base_uint.h>
9#include <xrpl/beast/unit_test/suite.h>
10#include <xrpl/resource/Fees.h>
11#include <xrpl/shamap/SHAMap.h>
12#include <xrpl/shamap/SHAMapAddNode.h>
13#include <xrpl/shamap/SHAMapNodeID.h>
14
15#include <xrpl.pb.h>
16
17#include <chrono>
18#include <memory>
19#include <set>
20#include <utility>
21#include <vector>
22
23namespace xrpl::test {
24
29{
31
37 [[nodiscard]] bool
38 isMapValid() const
39 {
40 // Under the lock: a batch on another thread can reach the verdict.
41 ScopedLockType const sl(mtx_);
42 return map_->isValid();
43 }
44
50 [[nodiscard]] bool
52 {
53 // Under the lock: a timer tick clears the flag on a job thread.
54 ScopedLockType const sl(mtx_);
55 return progress_;
56 }
57
61 void
63 {
64 ScopedLockType const sl(mtx_);
65 progress_ = false;
66 }
67};
68
70{
80 static constexpr auto kFastRetry = std::chrono::milliseconds{20};
81
93 [[nodiscard]] unsigned int
95 {
96 return ++seed_;
97 }
98
109 static bool
111 {
112 return tallyIs(san, 0, 0, 0);
113 }
114
125 [[nodiscard]] static std::shared_ptr<SHAMap>
127 {
128 auto& inbound = env.app().getInboundTransactions();
129
130 // acquire=false keeps this to a lookup rather than registering an acquisition. The job is
131 // queued before takeNodes() returns, so a few seconds is a generous deadline.
132 std::shared_ptr<SHAMap> delivered;
133 if (!waitFor(
134 [&] { return (delivered = inbound.getSet(setHash, false)) != nullptr; },
136 return nullptr;
137 return delivered;
138 }
139
147 void
149 {
150 testcase("A chain ending in a leaf completes the acquire");
151
152 auto const chain = DeepChain::toLeaf(3, nextSeed());
153
155 auto const* const peerSetPtr = peerSet.get();
156
157 UInt256 const setHash = chain.rootHash.asUInt256();
158 auto const acquire =
159 std::make_shared<TransactionAcquire>(env.app(), setHash, std::move(peerSet));
160 auto const peer = std::make_shared<ChargeRecordingPeer>();
161
162 // The root alone leaves the set incomplete, so accepting it asks for the level below.
163 auto const rootResult = acquire->takeNodes({{SHAMapNodeID{}, chain.nodeAt(0)}}, peer);
164 BEAST_EXPECT(rootResult.isUseful());
165 int const requestsWhileIncomplete = peerSetPtr->requests();
166 BEAST_EXPECT(requestsWhileIncomplete > 0);
167
168 // The rest of the chain, ending in the leaf.
169 auto const result = acquire->takeNodes(chain.nodesBelowRoot(), peer);
170 BEAST_EXPECT(result.isUseful());
171 BEAST_EXPECT(!result.isInvalid());
172
173 // The request count holds steady, so the asking has stopped.
174 BEAST_EXPECT(peerSetPtr->requests() == requestsWhileIncomplete);
175
176 auto const delivered = waitForDeliveredSet(env, setHash);
177 BEAST_EXPECT(delivered != nullptr);
178 if (delivered)
179 {
180 BEAST_EXPECT(delivered->getHash() == chain.rootHash);
181 BEAST_EXPECT(delivered->isValid());
182 }
183
184 // A reply arriving after the set is finished is left alone, and the asking stays stopped.
185 // init() stayed uncalled, so the set above is what settled the acquisition.
186 BEAST_EXPECT(wasIgnored(acquire->takeNodes({{SHAMapNodeID{}, chain.nodeAt(0)}}, peer)));
187 BEAST_EXPECT(peerSetPtr->requests() == requestsWhileIncomplete);
188 }
189
199 void
201 {
202 testcase("Two peers each supplying part of a set are both accepted without penalty");
203
204 auto const chain = DeepChain::toLeaf(3, nextSeed());
205 auto& inbound = env.app().getInboundTransactions();
206
207 // acquire=true registers the TransactionAcquire that gotData() looks up by hash.
208 UInt256 const setHash = chain.rootHash.asUInt256();
209 BEAST_EXPECT(inbound.getSet(setHash, true) == nullptr);
210
211 // The first peer answers with the root only.
212 auto const peerA = std::make_shared<ChargeRecordingPeer>();
213 inbound.gotData(setHash, peerA, packetFor(chain, {{SHAMapNodeID{}, chain.nodeAt(0)}}));
214 BEAST_EXPECT(peerA->charges().empty());
215
216 // The second answers with everything the first left out, and finishes the set.
217 auto const peerB = std::make_shared<ChargeRecordingPeer>();
218 inbound.gotData(setHash, peerB, packetFor(chain, chain.nodesBelowRoot()));
219 BEAST_EXPECT(peerB->charges().empty());
220
221 auto const delivered = waitForDeliveredSet(env, setHash);
222 BEAST_EXPECT(delivered != nullptr);
223 if (delivered)
224 BEAST_EXPECT(delivered->getHash() == chain.rootHash);
225 }
226
233 void
235 {
236 testcase("A mismatched root leaves the acquire recoverable");
237
238 DeepChain const chain{nextSeed()};
239
240 // Acquire an unrelated hash, so the chain's root cannot match it.
243 auto const peer = std::make_shared<ChargeRecordingPeer>();
244
245 auto const result = acquire->takeNodes({{SHAMapNodeID{}, chain.nodeAt(0)}}, peer);
246 BEAST_EXPECTS(tallyIs(result, 0, 1, 0), result.get());
247
248 // A mismatched root tells the acquisition only that this peer's answer is wrong, so the
249 // map keeps its state.
250 BEAST_EXPECT(acquire->isMapValid());
251
252 // Still alive: the next packet is examined rather than waved through.
253 BEAST_EXPECT(!wasIgnored(acquire->takeNodes({{SHAMapNodeID{}, chain.nodeAt(0)}}, peer)));
254 }
255
271 void
273 {
274 testcase("A reply of a root we already have is free");
275
276 DeepChain const chain{nextSeed()};
277 auto& inbound = env.app().getInboundTransactions();
278
279 // acquire=true registers the TransactionAcquire that gotData() looks up by hash.
280 UInt256 const setHash = chain.rootHash.asUInt256();
281 BEAST_EXPECT(inbound.getSet(setHash, true) == nullptr);
282
283 auto const rootPacket = packetFor(chain, {{SHAMapNodeID{}, chain.nodeAt(0)}});
284
285 // The first responder supplies the root, which is genuinely useful.
286 auto const firstPeer = std::make_shared<ChargeRecordingPeer>();
287 inbound.gotData(setHash, firstPeer, rootPacket);
288 BEAST_EXPECT(firstPeer->charges().empty());
289
290 // The second sends the same root. It answered what we asked, so it goes uncharged.
291 auto const secondPeer = std::make_shared<ChargeRecordingPeer>();
292 inbound.gotData(setHash, secondPeer, rootPacket);
293 BEAST_EXPECT(secondPeer->charges().empty());
294 }
295
305 void
307 {
308 testcase("A repeated non-root node is free");
309
310 auto const chain = DeepChain::toLeaf(3, nextSeed());
311 auto& inbound = env.app().getInboundTransactions();
312
313 UInt256 const setHash = chain.rootHash.asUInt256();
314 BEAST_EXPECT(inbound.getSet(setHash, true) == nullptr);
315
316 auto const rootPeer = std::make_shared<ChargeRecordingPeer>();
317 inbound.gotData(setHash, rootPeer, packetFor(chain, {{SHAMapNodeID{}, chain.nodeAt(0)}}));
318 BEAST_EXPECT(rootPeer->charges().empty());
319
320 // Depth 1 alone, so the set stays incomplete and the acquisition keeps examining data
321 // rather than waving the second copy through as a late reply.
322 auto const level1 = packetFor(chain, {{chain.idAt(1), chain.nodeAt(1)}});
323
324 auto const firstPeer = std::make_shared<ChargeRecordingPeer>();
325 inbound.gotData(setHash, firstPeer, level1);
326 BEAST_EXPECT(firstPeer->charges().empty());
327
328 auto const secondPeer = std::make_shared<ChargeRecordingPeer>();
329 inbound.gotData(setHash, secondPeer, level1);
330 BEAST_EXPECT(secondPeer->charges().empty());
331 }
332
344 void
346 {
347 testcase("A reply with undeserializable node data is charged");
348
349 DeepChain const chain{nextSeed()};
350 auto& inbound = env.app().getInboundTransactions();
351
352 UInt256 const setHash = chain.rootHash.asUInt256();
353 BEAST_EXPECT(inbound.getSet(setHash, true) == nullptr);
354
355 // getTreeNode() rejects this before the acquisition is handed anything.
356 auto const packet = std::make_shared<protocol::TMLedgerData>();
357 packet->set_ledgerhash(setHash.data(), UInt256::size());
358 packet->set_ledgerseq(0);
359 packet->set_type(protocol::liTS_CANDIDATE);
360
361 auto* const node = packet->add_nodes();
362 node->set_nodedata("\xff", 1);
363 node->set_id(SHAMapNodeID{}.getRawString());
364
365 auto const peer = std::make_shared<ChargeRecordingPeer>();
366 inbound.gotData(setHash, peer, packet);
367
368 BEAST_EXPECT(peer->charges() == std::vector{resource::kFeeInvalidData});
369 }
370
382 void
384 {
385 testcase("A well-formed reply with a mismatched root is charged");
386
387 // The set being acquired, and an unrelated chain whose root cannot hash to it.
388 DeepChain const chain{nextSeed()};
389 DeepChain const stranger{nextSeed()};
390 auto& inbound = env.app().getInboundTransactions();
391
392 // acquire=true registers the TransactionAcquire that gotData() looks up by hash.
393 UInt256 const setHash = chain.rootHash.asUInt256();
394 BEAST_EXPECT(inbound.getSet(setHash, true) == nullptr);
395
396 // The reply names the set we asked for, but carries the stranger's root.
397 auto const packet = packetFor(
398 stranger, {{SHAMapNodeID{}, stranger.nodeAt(0)}}, protocol::liTS_CANDIDATE, setHash);
399
400 auto const peer = std::make_shared<ChargeRecordingPeer>();
401 inbound.gotData(setHash, peer, packet);
402
403 BEAST_EXPECT(peer->charges() == std::vector{resource::kFeeInvalidData});
404 }
405
416 void
418 {
419 testcase("init() passes hasTxSet as its candidate filter");
420
421 DeepChain const chain{nextSeed()};
422
423 // Ordered with the useless peer first, so a filter that is ignored altogether shows up as
424 // the wrong peer being asked rather than as one extra request.
425 auto const withoutSet = std::make_shared<ChargeRecordingPeer>(false);
426 auto const withSet = std::make_shared<ChargeRecordingPeer>(true);
427
429 std::vector<std::shared_ptr<Peer>>{withoutSet, withSet});
430 auto const* const peerSetPtr = peerSet.get();
431
432 auto const acquire = std::make_shared<TransactionAcquire>(
433 env.app(), chain.rootHash.asUInt256(), std::move(peerSet));
434
435 static constexpr int kStartPeers = 2;
436 acquire->init(kStartPeers);
437
438 // Stop the retry loop, which keeps offering the same candidates while the acquisition runs.
439 acquire->cancel();
440
441 BEAST_EXPECT(peerSetPtr->firstLimit() == kStartPeers);
442 BEAST_EXPECT(peerSetPtr->addedPeers() == std::set<Peer::ID>{withSet->id()});
443
444 // The peer that was added is also asked, rather than merely tracked.
445 BEAST_EXPECT(peerSetPtr->requests() >= 1);
446 }
447
462 void
464 {
465 testcase("The retry timer broadcasts, then gives up");
466
467 DeepChain const chain{nextSeed()};
468
470 auto const* const peerSetPtr = peerSet.get();
471
472 // An unrelated hash, so the probe below is always rejected.
473 auto const acquire = std::make_shared<TransactionAcquire>(
474 env.app(), UInt256{42}, std::move(peerSet), kFastRetry);
475
476 // No candidates, so nothing goes out until onTimer() decides to broadcast.
477 acquire->init(1);
478 BEAST_EXPECT(peerSetPtr->requests() == 0);
479
480 // A root that cannot hash to this acquisition's set is rejected without recording progress,
481 // so polling with it does not postpone the timeout. A fresh peer each time keeps the
482 // rejections from piling up on one.
483 auto const probe = [&] {
484 return wasIgnored(acquire->takeNodes(
485 {{SHAMapNodeID{}, chain.nodeAt(0)}}, std::make_shared<ChargeRecordingPeer>()));
486 };
487
488 // kNormTimeouts (4) intervals in, onTimer() broadcasts with no peer of its own to ask. Past
489 // kMaxTimeouts (20) it fails itself, and stops examining data.
490 BEAST_EXPECT(waitFor(probe));
491
492 // Read as a broadcast rather than as a request, since a request here would mean a peer was
493 // named. Reading it after the failure is safe. trigger() returns before sending once
494 // failed_ is set, and both run under the acquisition's mutex, so any counted broadcast
495 // went out while the acquisition was still alive.
496 BEAST_EXPECT(peerSetPtr->broadcasts() > 0);
497 }
498
499 void
500 run() override
501 {
502 // One Env for the suite, which is safe only because every chain is seeded through
503 // nextSeed(): cases sharing an Env share a NodeCache, so they must not share a hash.
504 jtx::Env env{*this};
505
514
515 // Last: the only case that waits out a whole timeout chain.
517 }
518
519private:
520 unsigned int seed_{0};
521};
522
524
525} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
pointer data()
Definition base_uint.h:117
static constexpr std::size_t size()
Definition base_uint.h:549
UInt256 const & asUInt256() const
Definition SHAMapHash.h:26
Identifies a node inside a SHAMap.
virtual InboundTransactions & getInboundTransactions()=0
std::recursive_mutex mtx_
std::unique_lock< std::recursive_mutex > ScopedLockType
bool progress_
Whether forward progress has been made.
std::shared_ptr< SHAMap > map_
TransactionAcquire(Application &app, UInt256 const &hash, std::unique_ptr< PeerSet > peerSet, std::chrono::milliseconds retryInterval=kRetryInterval)
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
T make_shared(T... args)
T make_unique(T... args)
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
bool waitFor(std::function< bool()> const &condition, std::chrono::steady_clock::duration deadline=std::chrono::seconds{10})
Poll until the condition holds, or give up.
std::shared_ptr< protocol::TMLedgerData > packetFor(DeepChain const &chain, std::vector< std::pair< SHAMapNodeID, SHAMapTreeNodePtr > > const &data, protocol::TMLedgerInfoType type=protocol::liTS_CANDIDATE, std::optional< UInt256 > const &ledgerHash=std::nullopt, std::uint32_t ledgerSeq=0)
The given nodes of a chain as a TMLedgerData, so a test can go through the real dispatch.
bool tallyIs(SHAMapAddNode const &san, int good, int bad, int duplicate)
Whether a batch verdict carries exactly the given counts.
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
static constexpr auto kStartPeers
static DeepChain toLeaf(unsigned int depth, unsigned int seed=1)
A chain ending in a real transaction leaf, which completes an acquisition.
Definition DeepChain.h:95
An acquisition that exposes state its bases keep protected.
void clearProgress()
Forget any recorded progress.
bool madeProgress() const
Whether a batch has advanced the set since the flag was last cleared.
TransactionAcquire(Application &app, UInt256 const &hash, std::unique_ptr< PeerSet > peerSet, std::chrono::milliseconds retryInterval=kRetryInterval)
bool isMapValid() const
Whether the set being acquired is still structurally coherent.
void testMismatchedRootThroughGotDataIsCharged(jtx::Env &env)
A well-formed reply whose root hashes to a different set is charged for.
void testTwoPeersEachSupplyPartOfTheSet(jtx::Env &env)
Two peers each answering with a different missing piece are both accepted without penalty,...
static std::shared_ptr< SHAMap > waitForDeliveredSet(jtx::Env &env, UInt256 const &setHash)
Wait for a finished set to reach InboundTransactions.
void testTimerBroadcastsThenGivesUp(jtx::Env &env)
The retry timer broadcasts with no peer of its own, then gives up on its own.
void testDuplicateRootReplyIsFree(jtx::Env &env)
A second reply carrying a root we already have must stay free.
static constexpr auto kFastRetry
A retry interval short enough that a whole timeout chain costs a fraction of a second.
void testHappyPathCompletesAcquisition(jtx::Env &env)
A chain ending in a leaf completes the acquisition, which stops the asking and hands the map to Inbou...
void testInitFiltersCandidatesByHasTxSet(jtx::Env &env)
init() passes hasTxSet() to addPeers() as its candidate filter.
void testBadRootKeepsAcquireAlive(jtx::Env &env)
A root that does not hash to the set we asked for is a plain mismatch, and has to leave the acquisiti...
unsigned int nextSeed()
A seed no other chain in this suite has used.
void testDuplicateNonRootReplyIsFree(jtx::Env &env)
A repeated non-root node stays free too.
static bool wasIgnored(SHAMapAddNode const &san)
Whether takeNodes() declined to look at the data at all.
void testUndeserializableNodeIsCharged(jtx::Env &env)
A reply whose node data cannot be deserialized is charged for.
A chain of inner nodes in wire form, from the root down to one deepest node, each with one real child...
Definition DeepChain.h:38
SHAMapTreeNodePtr nodeAt(unsigned int depth) const
The node the chain holds at the given depth, root first.
Definition DeepChain.h:109