rippled
Loading...
Searching...
No Matches
TMGetObjectByHash_test.cpp
1#include <test/jtx.h>
2#include <test/jtx/Env.h>
3
4#include <xrpld/overlay/Message.h>
5#include <xrpld/overlay/detail/OverlayImpl.h>
6#include <xrpld/overlay/detail/PeerImp.h>
7#include <xrpld/overlay/detail/Tuning.h>
8#include <xrpld/peerfinder/detail/SlotImp.h>
9
10#include <xrpl/basics/make_SSLContext.h>
11#include <xrpl/beast/unit_test.h>
12#include <xrpl/nodestore/NodeObject.h>
13#include <xrpl/protocol/digest.h>
14#include <xrpl/protocol/messages.h>
15
16namespace xrpl {
17namespace test {
18
19using namespace jtx;
20
29{
30 using middle_type = boost::beast::tcp_stream;
31 using stream_type = boost::beast::ssl_stream<middle_type>;
32 using socket_type = boost::asio::ip::tcp::socket;
37 class PeerTest : public PeerImp
38 {
39 public:
41 Application& app,
43 http_request_type&& request,
44 PublicKey const& publicKey,
46 Resource::Consumer consumer,
48 OverlayImpl& overlay)
49 : PeerImp(
50 app,
51 id_++,
52 slot,
53 std::move(request),
54 publicKey,
56 consumer,
57 std::move(stream_ptr),
58 overlay)
59 {
60 }
61
62 ~PeerTest() = default;
63
64 void
65 run() override
66 {
67 }
68
69 void
70 send(std::shared_ptr<Message> const& m) override
71 {
73 }
74
77 {
78 return lastSentMessage_;
79 }
80
81 static void
83 {
84 id_ = 0;
85 }
86
87 private:
88 inline static Peer::id_t id_ = 0;
90 };
91
94
97 {
98 auto& overlay = dynamic_cast<OverlayImpl&>(env.app().overlay());
99 boost::beast::http::request<boost::beast::http::dynamic_body> request;
101
102 beast::IP::Endpoint local(boost::asio::ip::make_address("172.1.1.1"), 51235);
103 beast::IP::Endpoint remote(boost::asio::ip::make_address("172.1.1.2"), 51235);
104
106 auto consumer = overlay.resourceManager().newInboundEndpoint(remote);
107 auto [slot, _] = overlay.peerFinder().new_inbound_slot(local, remote);
108
109 auto peer = std::make_shared<PeerTest>(
110 env.app(), slot, std::move(request), key, protocolVersion_, consumer, std::move(stream_ptr), overlay);
111
112 overlay.add_active(peer);
113 return peer;
114 }
115
117 createRequest(size_t const numObjects, Env& env)
118 {
119 // Store objects in the NodeStore that will be found during the query
120 auto& nodeStore = env.app().getNodeStore();
121
122 // Create and store objects
124 hashes.reserve(numObjects);
125 for (int i = 0; i < numObjects; ++i)
126 {
127 uint256 hash(xrpl::sha512Half(i));
128 hashes.push_back(hash);
129
130 Blob data(100, static_cast<unsigned char>(i % 256));
131 nodeStore.store(hotLEDGER, std::move(data), hash, nodeStore.earliestLedgerSeq());
132 }
133
134 // Create a request with more objects than hardMaxReplyNodes
136 request->set_type(protocol::TMGetObjectByHash_ObjectType_otLEDGER);
137 request->set_query(true);
138
139 for (int i = 0; i < numObjects; ++i)
140 {
141 auto object = request->add_objects();
142 object->set_hash(hashes[i].data(), hashes[i].size());
143 object->set_ledgerseq(i);
144 }
145 return request;
146 }
147
152 void
153 testReplyLimit(size_t const numObjects, int const expectedReplySize)
154 {
155 testcase("Reply Limit");
156
157 Env env(*this);
159
160 auto peer = createPeer(env);
161
162 auto request = createRequest(numObjects, env);
163 // Call the onMessage handler
164 peer->onMessage(request);
165
166 // Verify that a reply was sent
167 auto sentMessage = peer->getLastSentMessage();
168 BEAST_EXPECT(sentMessage != nullptr);
169
170 // Parse the reply message
171 auto const& buffer = sentMessage->getBuffer(compression::Compressed::Off);
172
173 BEAST_EXPECT(buffer.size() > 6);
174 // Skip the message header (6 bytes: 4 for size, 2 for type)
175 protocol::TMGetObjectByHash reply;
176 BEAST_EXPECT(reply.ParseFromArray(buffer.data() + 6, buffer.size() - 6) == true);
177
178 // Verify the reply is limited to expectedReplySize
179 BEAST_EXPECT(reply.objects_size() == expectedReplySize);
180 }
181
182 void
183 run() override
184 {
185 int const limit = static_cast<int>(Tuning::hardMaxReplyNodes);
186 testReplyLimit(limit + 1, limit);
187 testReplyLimit(limit, limit);
188 testReplyLimit(limit - 1, limit - 1);
189 }
190};
191
192BEAST_DEFINE_TESTSUITE(TMGetObjectByHash, overlay, xrpl);
193
194} // namespace test
195} // namespace xrpl
A version-independent IP address and port combination.
Definition IPEndpoint.h:18
A testsuite class.
Definition suite.h:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:147
virtual boost::asio::io_context & getIOContext()=0
This class manages established peer-to-peer connections, handles message exchange,...
Definition PeerImp.h:96
std::shared_ptr< PeerFinder::Slot > const & slot()
Definition PeerImp.h:326
A public key.
Definition PublicKey.h:42
An endpoint that consumes resources.
Definition Consumer.h:16
virtual Overlay & overlay()=0
virtual NodeStore::Database & getNodeStore()=0
Test peer that captures sent messages for verification.
std::shared_ptr< Message > getLastSentMessage() const
void send(std::shared_ptr< Message > const &m) override
PeerTest(Application &app, std::shared_ptr< PeerFinder::Slot > const &slot, http_request_type &&request, PublicKey const &publicKey, ProtocolVersion protocol, Resource::Consumer consumer, std::unique_ptr< TMGetObjectByHash_test::stream_type > &&stream_ptr, OverlayImpl &overlay)
Test for TMGetObjectByHash reply size limiting.
std::shared_ptr< protocol::TMGetObjectByHash > createRequest(size_t const numObjects, Env &env)
boost::beast::ssl_stream< middle_type > stream_type
void testReplyLimit(size_t const numObjects, int const expectedReplySize)
Test that reply is limited to hardMaxReplyNodes when more objects are requested than the limit allows...
std::shared_ptr< PeerTest > createPeer(jtx::Env &env)
boost::asio::ip::tcp::socket socket_type
A transaction testing environment.
Definition Env.h:97
Application & app()
Definition Env.h:229
T is_same_v
STL namespace.
@ hardMaxReplyNodes
The hard cap on the number of ledger entries in a single reply.
auto const data
General field definitions, or fields used in multiple transaction namespaces.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:204
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition Handoff.h:12
std::shared_ptr< boost::asio::ssl::context > make_SSLContext(std::string const &cipherList)
Create a self-signed SSL context that allows anonymous Diffie Hellman.
@ hotLEDGER
Definition NodeObject.h:14
T push_back(T... args)
T reserve(T... args)