xrpld
Loading...
Searching...
No Matches
TMGetObjectByHash_test.cpp
1#include <test/jtx/Env.h>
2#include <test/overlay/CapturePeer.h>
3
4#include <xrpld/app/main/Application.h>
5#include <xrpld/overlay/Compression.h>
6#include <xrpld/overlay/detail/OverlayImpl.h>
7#include <xrpld/overlay/detail/Tuning.h>
8
9#include <xrpl/basics/Blob.h>
10#include <xrpl/basics/base_uint.h>
11#include <xrpl/beast/unit_test/suite.h>
12#include <xrpl/nodestore/NodeObject.h>
13#include <xrpl/protocol/digest.h>
14
15#include <xrpl.pb.h>
16
17#include <cstddef>
18#include <memory>
19#include <utility>
20#include <vector>
21
22namespace xrpl::test {
23
24using namespace jtx;
25
34{
50
52 createRequest(size_t const numObjects, Env& env)
53 {
54 // Store objects in the NodeStore that will be found during the query
55 auto& nodeStore = env.app().getNodeStore();
56
57 // Create and store objects
59 hashes.reserve(numObjects);
60 for (int i = 0; i < numObjects; ++i)
61 {
62 UInt256 const hash(xrpl::sha512Half(i));
63 hashes.push_back(hash);
64
65 Blob data(100, static_cast<unsigned char>(i % 256));
66 nodeStore.store(
67 NodeObjectType::Ledger, std::move(data), hash, nodeStore.earliestLedgerSeq());
68 }
69
70 // Name every stored object in a single generic query.
72 request->set_type(protocol::TMGetObjectByHash_ObjectType_otLEDGER);
73 request->set_query(true);
74
75 for (int i = 0; i < numObjects; ++i)
76 {
77 auto object = request->add_objects();
78 object->set_hash(hashes[i].data(), hashes[i].size());
79 object->set_ledgerseq(i);
80 }
81 return request;
82 }
83
91 void
92 testReplyObjectCount(size_t const numObjects, int const expectedReplySize)
93 {
94 testcase("Reply Object Count");
95
96 Env env(*this);
97 auto peer = makeCapturePeer<GetObjectPeer>(env);
98
99 auto request = createRequest(numObjects, env);
100 peer->runProcessGetObjectByHash(request);
101
102 // Verify that a reply was sent
103 auto sentMessage = peer->lastSent();
104 BEAST_EXPECT(sentMessage != nullptr);
105
106 // Parse the reply message
107 auto const& buffer = sentMessage->getBuffer(compression::Compressed::Off);
108
109 BEAST_EXPECT(buffer.size() > 6);
110 // Skip the message header (6 bytes: 4 for size, 2 for type)
111 protocol::TMGetObjectByHash reply;
112 BEAST_EXPECT(reply.ParseFromArray(buffer.data() + 6, buffer.size() - 6) == true);
113
114 // The reply carries the expected number of objects.
115 BEAST_EXPECT(reply.objects_size() == expectedReplySize);
116 }
117
118 void
119 run() override
120 {
121 int const limit = static_cast<int>(tuning::kHardMaxReplyNodes);
122 testReplyObjectCount(limit + 1, limit);
123 testReplyObjectCount(limit, limit);
124 testReplyObjectCount(limit - 1, limit - 1);
125 }
126};
127
128BEAST_DEFINE_TESTSUITE(TMGetObjectByHash, overlay, xrpl);
129
130} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
void processGetObjectByHash(std::shared_ptr< protocol::TMGetObjectByHash > const &m)
Process a generic-query TMGetObjectByHash message.
Definition PeerImp.cpp:2731
virtual node_store::Database & getNodeStore()=0
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 sent().
void runProcessGetObjectByHash(std::shared_ptr< protocol::TMGetObjectByHash > const &m)
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
Coverage for the TMGetObjectByHash object-count bound.
static std::shared_ptr< protocol::TMGetObjectByHash > createRequest(size_t const numObjects, Env &env)
void testReplyObjectCount(size_t const numObjects, int const expectedReplySize)
Check the object count a generic-query reply carries.
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
T make_shared(T... args)
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 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
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
Sha512HalfHasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:215
T push_back(T... args)
T reserve(T... args)