1#include <test/app/AcquireTestHelpers.h>
2#include <test/jtx/Env.h>
4#include <xrpld/app/ledger/InboundTransactions.h>
5#include <xrpld/app/ledger/detail/TransactionAcquire.h>
6#include <xrpld/overlay/Peer.h>
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>
42 return map_->isValid();
93 [[nodiscard]]
unsigned int
134 [&] {
return (delivered = inbound.getSet(setHash,
false)) !=
nullptr; },
150 testcase(
"A chain ending in a leaf completes the acquire");
155 auto const*
const peerSetPtr = peerSet.get();
157 UInt256 const setHash = chain.rootHash.asUInt256();
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);
169 auto const result = acquire->takeNodes(chain.nodesBelowRoot(), peer);
170 BEAST_EXPECT(result.isUseful());
171 BEAST_EXPECT(!result.isInvalid());
174 BEAST_EXPECT(peerSetPtr->requests() == requestsWhileIncomplete);
177 BEAST_EXPECT(delivered !=
nullptr);
180 BEAST_EXPECT(delivered->getHash() == chain.rootHash);
181 BEAST_EXPECT(delivered->isValid());
186 BEAST_EXPECT(
wasIgnored(acquire->takeNodes({{SHAMapNodeID{}, chain.nodeAt(0)}}, peer)));
187 BEAST_EXPECT(peerSetPtr->requests() == requestsWhileIncomplete);
202 testcase(
"Two peers each supplying part of a set are both accepted without penalty");
208 UInt256 const setHash = chain.rootHash.asUInt256();
209 BEAST_EXPECT(inbound.getSet(setHash,
true) ==
nullptr);
214 BEAST_EXPECT(peerA->charges().empty());
218 inbound.gotData(setHash, peerB,
packetFor(chain, chain.nodesBelowRoot()));
219 BEAST_EXPECT(peerB->charges().empty());
222 BEAST_EXPECT(delivered !=
nullptr);
224 BEAST_EXPECT(delivered->getHash() == chain.rootHash);
236 testcase(
"A mismatched root leaves the acquire recoverable");
246 BEAST_EXPECTS(
tallyIs(result, 0, 1, 0), result.get());
250 BEAST_EXPECT(acquire->isMapValid());
253 BEAST_EXPECT(!
wasIgnored(acquire->takeNodes({{SHAMapNodeID{}, chain.nodeAt(0)}}, peer)));
274 testcase(
"A reply of a root we already have is free");
281 BEAST_EXPECT(inbound.getSet(setHash,
true) ==
nullptr);
287 inbound.gotData(setHash, firstPeer, rootPacket);
288 BEAST_EXPECT(firstPeer->charges().empty());
292 inbound.gotData(setHash, secondPeer, rootPacket);
293 BEAST_EXPECT(secondPeer->charges().empty());
308 testcase(
"A repeated non-root node is free");
313 UInt256 const setHash = chain.rootHash.asUInt256();
314 BEAST_EXPECT(inbound.getSet(setHash,
true) ==
nullptr);
318 BEAST_EXPECT(rootPeer->charges().empty());
322 auto const level1 =
packetFor(chain, {{chain.idAt(1), chain.nodeAt(1)}});
325 inbound.gotData(setHash, firstPeer, level1);
326 BEAST_EXPECT(firstPeer->charges().empty());
329 inbound.gotData(setHash, secondPeer, level1);
330 BEAST_EXPECT(secondPeer->charges().empty());
347 testcase(
"A reply with undeserializable node data is charged");
353 BEAST_EXPECT(inbound.getSet(setHash,
true) ==
nullptr);
358 packet->set_ledgerseq(0);
359 packet->set_type(protocol::liTS_CANDIDATE);
361 auto*
const node = packet->add_nodes();
362 node->set_nodedata(
"\xff", 1);
366 inbound.gotData(setHash, peer, packet);
368 BEAST_EXPECT(peer->charges() ==
std::vector{resource::kFeeInvalidData});
385 testcase(
"A well-formed reply with a mismatched root is charged");
394 BEAST_EXPECT(inbound.getSet(setHash,
true) ==
nullptr);
401 inbound.gotData(setHash, peer, packet);
403 BEAST_EXPECT(peer->charges() ==
std::vector{resource::kFeeInvalidData});
419 testcase(
"init() passes hasTxSet as its candidate filter");
430 auto const*
const peerSetPtr = peerSet.get();
441 BEAST_EXPECT(peerSetPtr->firstLimit() ==
kStartPeers);
445 BEAST_EXPECT(peerSetPtr->requests() >= 1);
465 testcase(
"The retry timer broadcasts, then gives up");
470 auto const*
const peerSetPtr = peerSet.get();
478 BEAST_EXPECT(peerSetPtr->requests() == 0);
483 auto const probe = [&] {
496 BEAST_EXPECT(peerSetPtr->broadcasts() > 0);
TestcaseT testcase
Memberspace for declaring test cases.
static constexpr std::size_t size()
UInt256 const & asUInt256() const
Identifies a node inside a SHAMap.
std::string getRawString() const
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.
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.
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.
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 run() override
Runs the suite.
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...
SHAMapTreeNodePtr nodeAt(unsigned int depth) const
The node the chain holds at the given depth, root first.