1#include <test/app/AcquireTestHelpers.h>
2#include <test/jtx/Env.h>
4#include <xrpld/app/ledger/InboundLedger.h>
5#include <xrpld/app/ledger/InboundLedgers.h>
6#include <xrpld/app/ledger/LedgerMaster.h>
8#include <xrpl/basics/base_uint.h>
9#include <xrpl/basics/chrono.h>
10#include <xrpl/beast/unit_test/suite.h>
11#include <xrpl/ledger/Ledger.h>
12#include <xrpl/nodestore/NodeObject.h>
13#include <xrpl/protocol/HashPrefix.h>
14#include <xrpl/protocol/LedgerHeader.h>
15#include <xrpl/protocol/Serializer.h>
16#include <xrpl/protocol/jss.h>
17#include <xrpl/shamap/SHAMapNodeID.h>
127 [[nodiscard]]
unsigned int
204 unsigned int maxDepth)
208 for (
auto depth = 0u; depth <= maxDepth; ++depth)
213 chain.
nodeAt(depth)->getHash().asUInt256(),
234 packet->set_ledgerseq(header.
seq);
235 packet->set_type(protocol::liBASE);
258 acquire->startAcquire();
274 testcase(
"A ledger found locally completes the acquire");
289 BEAST_EXPECT(acquired !=
nullptr);
292 BEAST_EXPECT(acquired->isImmutable());
293 BEAST_EXPECT(acquired->header().hash == header.hash);
308 auto const historyHeader =
makeHeader(historyChain);
311 storeStateNodes(env, historyHeader, historyChain, historyChain.deepestDepth);
325 BEAST_EXPECT(history->checkLocal());
326 BEAST_EXPECT(history->isComplete());
327 BEAST_EXPECT(!history->isFailed());
330 auto const historyLedger = history->getLedger();
331 BEAST_EXPECT(historyLedger !=
nullptr);
333 BEAST_EXPECT(historyLedger->isImmutable());
348 BEAST_EXPECT(again->checkLocal());
349 BEAST_EXPECT(again->isComplete());
350 BEAST_EXPECT(!again->isFailed());
352 auto const settled = again->getLedger();
353 BEAST_EXPECT(settled !=
nullptr);
355 BEAST_EXPECT(settled->isImmutable());
376 testcase(
"A ledger invalidated on its way to being settled fails");
393 BEAST_EXPECT(!acquire->checkLocal());
394 BEAST_EXPECT(!acquire->isFailed());
395 BEAST_EXPECT(!acquire->isComplete());
398 BEAST_EXPECT(ledger !=
nullptr);
403 acquire->markComplete();
406 auto& stateMap = ledger->stateMap();
407 BEAST_EXPECT(stateMap.addRootNode(chain.
rootHash, chain.
nodeAt(0),
nullptr).isGood());
409 stateMap.addKnownNode(nodeID, node,
nullptr);
410 BEAST_EXPECT(!stateMap.isValid());
412 acquire->signalDone();
416 BEAST_EXPECT(!acquire->isComplete());
417 BEAST_EXPECT(acquire->isFailed());
420 BEAST_EXPECT(acquire->getLedger() ==
nullptr);
442 testcase(
"An acquisition that fails locally still signals");
468 auto const wrongSeq = strayHeader.seq + 1;
494 viaTrigger->triggerAdded();
497 BEAST_EXPECT(viaTrigger->isFailed());
498 BEAST_EXPECT(!viaTrigger->isComplete());
499 BEAST_EXPECT(viaTrigger->getLedger() ==
nullptr);
500 BEAST_EXPECT(!viaTrigger->getJson(0)[jss::have_header].asBool());
515 testcase(
"A peer header with a zero account hash fails the acquire");
525 BEAST_EXPECT(!acquire->isFailed());
526 BEAST_EXPECT(!acquire->getJson(0)[jss::have_header].asBool());
529 BEAST_EXPECT(acquire->gotData(peer,
headerPacket(header)));
533 BEAST_EXPECT(acquire->isFailed());
534 BEAST_EXPECT(!acquire->isComplete());
535 BEAST_EXPECT(acquire->getLedger() ==
nullptr);
536 BEAST_EXPECT(!acquire->getJson(0)[jss::have_header].asBool());
539 BEAST_EXPECT(peer->charges().empty());
555 testcase(
"A peer header with no transactions completes the acquire");
564 BEAST_EXPECT(acquire->gotData(peer,
headerPacket(header)));
569 BEAST_EXPECT(!acquire->isFailed());
570 BEAST_EXPECT(!acquire->isComplete());
571 auto const json = acquire->getJson(0);
572 BEAST_EXPECT(
json[jss::have_header].asBool());
573 BEAST_EXPECT(
json[jss::have_transactions].asBool());
574 BEAST_EXPECT(!
json[jss::have_state].asBool());
578 BEAST_EXPECT(acquire->gotData(
586 BEAST_EXPECT(!acquire->gotData(
589 chain, chain.nodesBelowRoot(), protocol::liAS_NODE, header.hash, header.seq)));
592 BEAST_EXPECT(acquire->isComplete());
593 BEAST_EXPECT(!acquire->isFailed());
594 BEAST_EXPECT(peer->charges().empty());
596 auto const settled = acquire->getLedger();
597 BEAST_EXPECT(settled !=
nullptr);
599 BEAST_EXPECT(settled->isImmutable());
624 testcase(
"The retry timer re-asks, then gives up");
626 UInt256 const kUnknownLedger{8};
632 auto const*
const peerSetPtr = peerSet.get();
650 int requestsFromInit = 0;
652 auto const held = acquire->lock();
653 acquire->startAcquire();
654 BEAST_EXPECT(!acquire->isFailed());
655 requestsFromInit = peerSetPtr->requests();
657 BEAST_EXPECT(requestsFromInit > 0);
661 BEAST_EXPECT(
waitFor([&] {
return acquire->isFailed(); }));
662 BEAST_EXPECT(!acquire->isComplete());
663 BEAST_EXPECT(peerSetPtr->requests() > requestsFromInit);
TestcaseT testcase
Memberspace for declaring test cases.
static constexpr std::size_t size()
virtual node_store::Database & db()=0
InboundLedger(Application &app, UInt256 const &hash, std::uint32_t seq, Reason reason, ClockType &clock, std::unique_ptr< PeerSet > peerSet, std::chrono::milliseconds retryInterval=kRetryInterval)
void trigger(std::shared_ptr< Peer > const &peer, TriggerReason reason)
Ask for more nodes, or judge what has been collected.
void init(ScopedLockType &collectionLock)
void done()
Settle the acquisition and signal whatever is waiting on it.
std::shared_ptr< Ledger const > getLedger() const
The acquired ledger.
virtual std::size_t fetchRate()=0
Returns the rate of historical ledger fetches per minute.
virtual std::shared_ptr< Ledger const > acquire(UInt256 const &hash, std::uint32_t seq, InboundLedger::Reason)=0
virtual bool isFailure(UInt256 const &h)=0
void rendezvous()
Block until no jobs running.
std::shared_ptr< Ledger const > getLedgerByHash(UInt256 const &hash)
std::chrono::time_point< NetClock > time_point
std::chrono::duration< rep, period > duration
UInt256 const & asUInt256() const
Identifies a node inside a SHAMap.
Blob const & peekData() const
virtual JobQueue & getJobQueue()=0
virtual InboundLedgers & getInboundLedgers()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual Family & getNodeFamily()=0
std::recursive_mutex mtx_
std::unique_lock< std::recursive_mutex > ScopedLockType
virtual void store(NodeObjectType type, Blob &&data, UInt256 const &hash, std::uint32_t ledgerSeq)=0
Store the object.
A transaction testing environment.
JSON (JavaScript Object Notation).
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
static std::shared_ptr< Ledger > mutableLedger(InboundLedger const &acquire)
The ledger an acquisition is assembling, as a pointer that can modify it.
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Stopwatch & stopwatch()
Returns an instance of a wall clock.
void addRaw(LedgerHeader const &, Serializer &, bool includeHash=false)
@ LedgerMaster
ledger master data for signing
UInt256 calculateLedgerHash(LedgerHeader const &info)
Calculate the hash of a ledger header.
T const_pointer_cast(T... args)
static DeepChain toLeaf(unsigned int depth, unsigned int seed=1)
A chain ending in a real transaction leaf, which completes an acquisition.
void testInvalidatedLedgerFailsInDone(jtx::Env &env)
A ledger whose map goes invalid on the way to being settled must be discarded rather than delivered.
static void storeStateNodes(jtx::Env &env, LedgerHeader const &header, DeepChain const &chain, unsigned int maxDepth)
Put every node of a chain in the local store, so a state-map walk resolves the whole map from the sto...
void testLocalFailureSignalsDone(jtx::Env &env)
An acquisition that fails on local data must still signal.
void testPeerZeroAccountHashFails(jtx::Env &env)
A peer-supplied header whose account hash is zero fails the acquisition and signals,...
static std::shared_ptr< TestableInboundLedger > startPeerAcquire(jtx::Env &env, LedgerHeader const &header)
An acquisition started with nothing in the store for its hash, so it waits on its peers.
void testLocalLedgerCompletesAcquire(jtx::Env &env)
A ledger whose maps all resolve locally finishes on the spot, and the finished ledger is immutable an...
void testPeerHeaderWithoutTransactionsCompletes(jtx::Env &env)
A peer-supplied header with a zero transaction hash is taken, since that is an empty transaction set,...
static constexpr auto kFastRetry
A retry interval short enough that a whole timeout chain costs a fraction of a second.
void testTimerRetriesThenGivesUp(jtx::Env &env)
The retry timer re-asks, then gives up and signals.
unsigned int nextSeed()
A seed unique to this chain within the suite.
static std::shared_ptr< protocol::TMLedgerData > headerPacket(LedgerHeader const &header)
The header alone as a liBASE reply, so takeHeader() alone judges the packet.
static LedgerHeader makeHeader(DeepChain const &chain)
The common shape: no transactions, so only the state map is in play.
static LedgerHeader makeHeader(UInt256 const &txHash, UInt256 const &accountHash)
A ledger header naming the given map roots, with a hash derived from the fields, so an acquisition ac...
static void storeHeader(jtx::Env &env, LedgerHeader const &header)
Put the header in the local store, which is the first place tryDB() looks.
void run() override
Runs the suite.
An acquisition that exposes the entry points its bases keep protected, so a case can reach them throu...
InboundLedger(Application &app, UInt256 const &hash, std::uint32_t seq, Reason reason, ClockType &clock, std::unique_ptr< PeerSet > peerSet, std::chrono::milliseconds retryInterval=kRetryInterval)
void markComplete()
Record that every part has been fetched.
void triggerAdded()
Ask for more nodes, or judge what has been collected, as a fresh acquisition does.
void startAcquire()
Look for the ledger locally, ask the peers being tracked for the rest, and arm the timer,...
void signalDone()
Settle the acquisition and signal whatever is waiting on it.
ScopedLockType lock() const
Hold the acquisition's own lock.
A chain of inner nodes in wire form, from the root down to one deepest node, each with one real child...
Blob prefixedNodeAt(unsigned int depth) const
The same node in the prefixed form used for storage and fetch packs, which is what hashes to the node...
std::vector< std::pair< SHAMapNodeID, SHAMapTreeNodePtr > > nodesBelowRoot(unsigned int firstDepth=1) const
Every node below the root, down to and including the deepest one.
SHAMapTreeNodePtr nodeAt(unsigned int depth) const
The node the chain holds at the given depth, root first.