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().getOverlay());
99 boost::beast::http::request<boost::beast::http::dynamic_body> request;
100 auto stream_ptr =
102
103 beast::IP::Endpoint const local(boost::asio::ip::make_address("172.1.1.1"), 51235);
104 beast::IP::Endpoint const remote(boost::asio::ip::make_address("172.1.1.2"), 51235);
105
107 auto consumer = overlay.resourceManager().newInboundEndpoint(remote);
108 auto [slot, _] = overlay.peerFinder().new_inbound_slot(local, remote);
109
110 auto peer = std::make_shared<PeerTest>(
111 env.app(),
112 slot,
113 std::move(request),
114 key,
116 consumer,
117 std::move(stream_ptr),
118 overlay);
119
120 overlay.add_active(peer);
121 return peer;
122 }
123
125 createRequest(size_t const numObjects, Env& env)
126 {
127 // Store objects in the NodeStore that will be found during the query
128 auto& nodeStore = env.app().getNodeStore();
129
130 // Create and store objects
132 hashes.reserve(numObjects);
133 for (int i = 0; i < numObjects; ++i)
134 {
135 uint256 const hash(xrpl::sha512Half(i));
136 hashes.push_back(hash);
137
138 Blob data(100, static_cast<unsigned char>(i % 256));
139 nodeStore.store(hotLEDGER, std::move(data), hash, nodeStore.earliestLedgerSeq());
140 }
141
142 // Create a request with more objects than hardMaxReplyNodes
144 request->set_type(protocol::TMGetObjectByHash_ObjectType_otLEDGER);
145 request->set_query(true);
146
147 for (int i = 0; i < numObjects; ++i)
148 {
149 auto object = request->add_objects();
150 object->set_hash(hashes[i].data(), hashes[i].size());
151 object->set_ledgerseq(i);
152 }
153 return request;
154 }
155
160 void
161 testReplyLimit(size_t const numObjects, int const expectedReplySize)
162 {
163 testcase("Reply Limit");
164
165 Env env(*this);
167
168 auto peer = createPeer(env);
169
170 auto request = createRequest(numObjects, env);
171 // Call the onMessage handler
172 peer->onMessage(request);
173
174 // Verify that a reply was sent
175 auto sentMessage = peer->getLastSentMessage();
176 BEAST_EXPECT(sentMessage != nullptr);
177
178 // Parse the reply message
179 auto const& buffer = sentMessage->getBuffer(compression::Compressed::Off);
180
181 BEAST_EXPECT(buffer.size() > 6);
182 // Skip the message header (6 bytes: 4 for size, 2 for type)
183 protocol::TMGetObjectByHash reply;
184 BEAST_EXPECT(reply.ParseFromArray(buffer.data() + 6, buffer.size() - 6) == true);
185
186 // Verify the reply is limited to expectedReplySize
187 BEAST_EXPECT(reply.objects_size() == expectedReplySize);
188 }
189
190 void
191 run() override
192 {
193 int const limit = static_cast<int>(Tuning::hardMaxReplyNodes);
194 testReplyLimit(limit + 1, limit);
195 testReplyLimit(limit, limit);
196 testReplyLimit(limit - 1, limit - 1);
197 }
198};
199
200BEAST_DEFINE_TESTSUITE(TMGetObjectByHash, overlay, xrpl);
201
202} // namespace test
203} // 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:150
This class manages established peer-to-peer connections, handles message exchange,...
Definition PeerImp.h:95
std::shared_ptr< PeerFinder::Slot > const & slot()
Definition PeerImp.h:325
A public key.
Definition PublicKey.h:42
An endpoint that consumes resources.
Definition Consumer.h:16
virtual NodeStore::Database & getNodeStore()=0
virtual boost::asio::io_context & getIOContext()=0
virtual Overlay & getOverlay()=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.
static 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:122
Application & app()
Definition Env.h:259
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)