xrpld
Loading...
Searching...
No Matches
TMGetLedger_test.cpp
1#include <test/jtx/Env.h>
2#include <test/overlay/CapturePeer.h>
3
4#include <xrpld/overlay/Compression.h>
5#include <xrpld/overlay/detail/Tuning.h>
6
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/beast/unit_test/suite.h>
9#include <xrpl/resource/Fees.h>
10#include <xrpl/shamap/SHAMapNodeID.h>
11
12#include <xrpl.pb.h>
13
14#include <cstddef>
15#include <memory>
16#include <utility>
17#include <vector>
18
19namespace xrpl::test {
20
21using namespace jtx;
22
24{
30 {
31 public:
33
34 void
41 };
42
43 // Build a well-formed TMGetLedger node request carrying `numNodeIds` node
44 // IDs.
46 createRequest(std::size_t const numNodeIds)
47 {
49 request->set_itype(protocol::liTX_NODE);
50
51 // A UInt256-sized ledger hash, as a well-formed request carries.
52 UInt256 const ledgerHash{1};
53 request->set_ledgerhash(ledgerHash.data(), ledgerHash.size());
54
55 // Valid, deserializable SHAMap node IDs.
56 auto const rootNodeId = SHAMapNodeID{}.getRawString();
57 for (std::size_t i = 0; i < numNodeIds; ++i)
58 {
59 request->add_nodeids(rootNodeId);
60 }
61
62 return request;
63 }
64
65 void
66 testNodeIdCountAccepted(std::size_t const numNodeIds, bool const expectRejected)
67 {
68 testcase("Node ID Count Accepted");
69
70 Env env{*this};
71
72 auto peer = makeCapturePeer(env);
73 peer->onMessage(createRequest(numNodeIds));
74
75 // A request outside the accepted node-ID count is charged kFeeInvalidData; one inside
76 // it is not. The JobQueue handler may run concurrently and update the fee in the
77 // accepted case.
78 BEAST_EXPECT(
79 expectRejected ? (peer->feeCharge() == resource::kFeeInvalidData)
80 : !(peer->feeCharge() == resource::kFeeInvalidData));
81 }
82
83 void
85 {
86 testcase("Process Ledger Request Node Count");
87
88 Env env{*this};
89 env.close();
90
91 auto peer = makeCapturePeer<GetLedgerPeer>(env);
92
93 // Ask for the account-state root node of the closed ledger.
94 auto request = createRequest(numNodeIds);
95 request->clear_ledgerhash();
96 request->set_itype(protocol::liAS_NODE);
97 request->set_ltype(protocol::ltCLOSED);
98
99 peer->runProcessLedgerRequest(request, std::vector<SHAMapNodeID>(numNodeIds));
100
101 auto sentMessage = peer->lastSent();
102 BEAST_EXPECT(sentMessage != nullptr);
103 if (!sentMessage)
104 {
105 return;
106 }
107
108 auto const& buffer = sentMessage->getBuffer(compression::Compressed::Off);
109 BEAST_EXPECT(buffer.size() > 6);
110
111 // Skip the message header (6 bytes: 4 for size, 2 for type).
112 protocol::TMLedgerData reply;
113 BEAST_EXPECT(reply.ParseFromArray(buffer.data() + 6, buffer.size() - 6) == true);
114
115 BEAST_EXPECT(reply.type() == protocol::liAS_NODE);
116 BEAST_EXPECT(reply.nodes_size() > 0);
117 BEAST_EXPECT(reply.nodes_size() <= static_cast<std::size_t>(tuning::kHardMaxReplyNodes));
118 }
119
120 void
121 run() override
122 {
123 auto const limit = static_cast<std::size_t>(tuning::kHardMaxReplyNodes);
124 testNodeIdCountAccepted(limit + 1, true);
125 testNodeIdCountAccepted(limit, false);
126 testNodeIdCountAccepted(limit - 1, false);
130 }
131};
132
133BEAST_DEFINE_TESTSUITE(TMGetLedger, overlay, xrpl);
134
135} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
void processLedgerRequest(std::shared_ptr< protocol::TMGetLedger > const &m, std::vector< SHAMapNodeID > nodeIDs)
Definition PeerImp.cpp:3466
Identifies a node inside a SHAMap.
CapturePeer(Application &app, Peer::ID id, std::shared_ptr< peer_finder::Slot > const &slot, HttpRequestType request, PublicKey const &publicKey, ProtocolVersion protocol, resource::Consumer consumer, std::unique_ptr< StreamType > streamPtr, OverlayImpl &overlay)
Takes PeerImp's two rvalue-reference parameters by value instead, so a derived double can inherit thi...
Definition CapturePeer.h:68
Calls the JtLedgerReq-dispatched processor synchronously, so the reply is visible through lastSent().
void runProcessLedgerRequest(std::shared_ptr< protocol::TMGetLedger > const &m, std::vector< SHAMapNodeID > nodeIDs)
CapturePeer(Application &app, Peer::ID id, std::shared_ptr< peer_finder::Slot > const &slot, HttpRequestType request, PublicKey const &publicKey, ProtocolVersion protocol, resource::Consumer consumer, std::unique_ptr< StreamType > streamPtr, OverlayImpl &overlay)
Takes PeerImp's two rvalue-reference parameters by value instead, so a derived double can inherit thi...
Definition CapturePeer.h:68
static std::shared_ptr< protocol::TMGetLedger > createRequest(std::size_t const numNodeIds)
void testNodeIdCountAccepted(std::size_t const numNodeIds, bool const expectRejected)
void testProcessLedgerRequestNodeCount(std::size_t const numNodeIds)
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:161
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
T make_shared(T... args)
Charge const kFeeInvalidData
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
std::shared_ptr< PeerType > makeCapturePeer(jtx::Env &env, std::optional< PublicKey > key=std::nullopt, HttpRequestType request={})
Build an active CapturePeer and register it with the overlay.
static UInt256 ledgerHash(LedgerHeader const &info)
static constexpr auto kHardMaxReplyNodes
The hard cap on the number of ledger entries in a single reply.
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:580