xrpld
Loading...
Searching...
No Matches
compression_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/WSClient.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/envconfig.h>
6#include <test/jtx/pay.h>
7
8#include <xrpld/core/Config.h>
9#include <xrpld/overlay/Compression.h>
10#include <xrpld/overlay/Message.h>
11#include <xrpld/overlay/detail/Handshake.h>
12#include <xrpld/overlay/detail/ProtocolMessage.h>
13#include <xrpld/overlay/detail/ZeroCopyStream.h>
14
15#include <xrpl/basics/Log.h>
16#include <xrpl/basics/Slice.h>
17#include <xrpl/basics/StringUtilities.h>
18#include <xrpl/basics/base_uint.h>
19#include <xrpl/basics/chrono.h>
20#include <xrpl/basics/random.h>
21#include <xrpl/basics/strHex.h>
22#include <xrpl/beast/net/IPAddress.h>
23#include <xrpl/beast/unit_test/suite.h>
24#include <xrpl/beast/utility/Journal.h>
25#include <xrpl/json/json_value.h>
26#include <xrpl/protocol/HashPrefix.h>
27#include <xrpl/protocol/KeyType.h>
28#include <xrpl/protocol/LedgerHeader.h>
29#include <xrpl/protocol/SField.h>
30#include <xrpl/protocol/STObject.h>
31#include <xrpl/protocol/SecretKey.h>
32#include <xrpl/protocol/Seed.h>
33#include <xrpl/protocol/Serializer.h>
34#include <xrpl/protocol/Sign.h>
35#include <xrpl/protocol/digest.h>
36#include <xrpl/protocol/jss.h>
37#include <xrpl/shamap/SHAMapNodeID.h>
38
39#include <boost/asio/buffer.hpp>
40#include <boost/asio/ip/address.hpp>
41#include <boost/beast/core/multi_buffer.hpp>
42#include <boost/system/detail/error_code.hpp>
43
44#include <xrpl.pb.h>
45
46#include <algorithm>
47#include <cstdint>
48#include <memory>
49#include <sstream>
50#include <string>
51#include <vector>
52
53namespace xrpl::test {
54
55using namespace xrpl::test;
56using namespace xrpl::test::jtx;
57
58static uint256
73
75{
78
79public:
80 compression_test() = default;
81
82 template <typename T>
83 void
84 doTest(std::shared_ptr<T> proto, protocol::MessageType mt, uint16_t nbuffers, std::string msg)
85 {
86 testcase("Compress/Decompress: " + msg);
87
88 Message m(*proto, mt);
89
90 auto& buffer = m.getBuffer(Compressed::On);
91
92 boost::beast::multi_buffer buffers;
93
94 // simulate multi-buffer
95 auto sz = buffer.size() / nbuffers;
96 for (int i = 0; i < nbuffers; i++)
97 {
98 auto start = buffer.begin() + sz * i;
99 auto end = i < nbuffers - 1 ? (buffer.begin() + sz * (i + 1)) : buffer.end();
100 std::vector<std::uint8_t> slice(start, end);
101 buffers.commit(
102 boost::asio::buffer_copy(
103 buffers.prepare(slice.size()), boost::asio::buffer(slice)));
104 }
105
106 boost::system::error_code ec;
107 auto header = xrpl::detail::parseMessageHeader(ec, buffers.data(), buffer.size());
108
109 BEAST_EXPECT(header);
110
111 if (!header || header->algorithm == Algorithm::None)
112 return;
113
114 std::vector<std::uint8_t> decompressed;
115 decompressed.resize(header->uncompressedSize);
116
117 BEAST_EXPECT(header->payloadWireSize == buffer.size() - header->headerSize);
118
119 ZeroCopyInputStream stream(buffers.data());
120 stream.Skip(header->headerSize);
121
122 auto decompressedSize = xrpl::compression::decompress(
123 stream, header->payloadWireSize, decompressed.data(), header->uncompressedSize);
124 BEAST_EXPECT(decompressedSize == header->uncompressedSize);
125 auto const proto1 = std::make_shared<T>();
126
127 BEAST_EXPECT(proto1->ParseFromArray(decompressed.data(), decompressedSize));
128 auto uncompressed = m.getBuffer(Compressed::Off);
129 BEAST_EXPECT(
131 uncompressed.begin() + xrpl::compression::kHeaderBytes,
132 uncompressed.end(),
133 decompressed.begin()));
134 }
135
138 {
140 manifests->mutable_list()->Reserve(n);
141 for (int i = 0; i < n; i++)
142 {
143 auto master = randomKeyPair(KeyType::Ed25519);
144 auto signing = randomKeyPair(KeyType::Ed25519);
146 st[sfSequence] = i;
147 st[sfPublicKey] = std::get<0>(master);
148 st[sfSigningPubKey] = std::get<0>(signing);
149 st[sfDomain] =
150 makeSlice(std::string("example") + std::to_string(i) + std::string(".com"));
151 sign(
152 st, HashPrefix::Manifest, KeyType::Ed25519, std::get<1>(master), sfMasterSignature);
153 sign(st, HashPrefix::Manifest, KeyType::Ed25519, std::get<1>(signing));
154 Serializer s;
155 st.add(s);
156 auto* manifest = manifests->add_list();
157 manifest->set_stobject(s.data(), s.size());
158 }
159 return manifests;
160 }
161
164 {
166 endpoints->mutable_endpoints_v2()->Reserve(n);
167 for (int i = 0; i < n; i++)
168 {
169 auto ep = endpoints->add_endpoints_v2();
170 ep->set_endpoint(std::string("10.0.1.") + std::to_string(i));
171 ep->set_hops(i);
172 }
173 endpoints->set_version(2);
174
175 return endpoints;
176 }
177
180 {
181 Env env(*this, envconfig());
182 int const fund = 10000;
183 auto const alice = Account("alice");
184 auto const bob = Account("bob");
185 env.fund(XRP(fund), "alice", "bob");
186 env.trust(bob["USD"](fund), alice);
187 env.close();
188
189 auto toBinary = [this](std::string const& text) {
190 auto blob = strUnHex(text);
191 BEAST_EXPECT(blob);
192 return std::string{reinterpret_cast<char const*>(blob->data()), blob->size()};
193 };
194
195 std::string usdTxBlob;
196 auto wsc = makeWSClient(env.app().config());
197 {
198 json::Value requestUSD;
199 requestUSD[jss::secret] = toBase58(generateSeed("bob"));
200 requestUSD[jss::tx_json] = pay("bob", "alice", bob["USD"](fund / 2));
201 json::Value replyUSD = wsc->invoke("sign", requestUSD);
202
203 usdTxBlob = toBinary(replyUSD[jss::result][jss::tx_blob].asString());
204 }
205
207 transaction->set_rawtransaction(usdTxBlob);
208 transaction->set_status(protocol::tsNEW);
209 transaction->set_receivetimestamp(randInt<std::uint64_t>());
210 transaction->set_deferred(true);
211
212 return transaction;
213 }
214
217 {
219 getLedger->set_itype(protocol::liTS_CANDIDATE);
220 getLedger->set_ltype(protocol::TMLedgerType::ltACCEPTED);
221 uint256 const hash(xrpl::sha512Half(123456789));
222 getLedger->set_ledgerhash(hash.begin(), hash.size());
223 getLedger->set_ledgerseq(123456789);
224 xrpl::SHAMapNodeID const sha(64, hash);
225 getLedger->add_nodeids(sha.getRawString());
226 getLedger->set_requestcookie(123456789);
227 getLedger->set_querytype(protocol::qtINDIRECT);
228 getLedger->set_querydepth(3);
229 return getLedger;
230 }
231
233 buildLedgerData(uint32_t n, Logs& logs)
234 {
236 uint256 const hash(xrpl::sha512Half(12356789));
237 ledgerData->set_ledgerhash(hash.data(), hash.size());
238 ledgerData->set_ledgerseq(123456789);
239 ledgerData->set_type(protocol::TMLedgerInfoType::liAS_NODE);
240 ledgerData->set_requestcookie(123456789);
241 ledgerData->set_error(protocol::TMReplyError::reNO_LEDGER);
242 ledgerData->mutable_nodes()->Reserve(n);
243 uint256 parentHash(0);
244
245 NetClock::duration const resolution{10};
246 NetClock::time_point ct{resolution};
247
248 for (int i = 0; i < n; i++)
249 {
250 LedgerHeader info;
251 info.seq = i;
252 info.parentCloseTime = ct;
253 info.hash = xrpl::sha512Half(i);
254 info.txHash = xrpl::sha512Half(i + 1);
255 info.accountHash = xrpl::sha512Half(i + 2);
256 info.parentHash = parentHash;
257 info.drops = XRPAmount(10);
258 info.closeTimeResolution = resolution;
259 info.closeTime = ct;
260 ct += resolution;
261 parentHash = ledgerHash(info);
262 Serializer nData;
263 xrpl::addRaw(info, nData);
264 ledgerData->add_nodes()->set_nodedata(nData.getDataPtr(), nData.getLength());
265 }
266
267 return ledgerData;
268 }
269
272 {
274
275 getObject->set_type(
276 protocol::TMGetObjectByHash_ObjectType::TMGetObjectByHash_ObjectType_otTRANSACTION);
277 getObject->set_query(true);
278 uint256 hash(xrpl::sha512Half(123456789));
279 getObject->set_ledgerhash(hash.data(), hash.size());
280 getObject->set_fat(true);
281 for (int i = 0; i < 100; i++)
282 {
283 uint256 hash(xrpl::sha512Half(i));
284 auto object = getObject->add_objects();
285 object->set_hash(hash.data(), hash.size());
286 xrpl::SHAMapNodeID const sha(64, hash);
287 object->set_nodeid(sha.getRawString());
288 object->set_index("");
289 object->set_data("");
290 object->set_ledgerseq(i);
291 }
292 return getObject;
293 }
294
297 {
299
300 auto master = randomKeyPair(KeyType::Ed25519);
301 auto signing = randomKeyPair(KeyType::Ed25519);
303 st[sfSequence] = 0;
304 st[sfPublicKey] = std::get<0>(master);
305 st[sfSigningPubKey] = std::get<0>(signing);
306 st[sfDomain] = makeSlice(std::string("example.com"));
307 sign(st, HashPrefix::Manifest, KeyType::Ed25519, std::get<1>(master), sfMasterSignature);
308 sign(st, HashPrefix::Manifest, KeyType::Ed25519, std::get<1>(signing));
309 Serializer s;
310 st.add(s);
311 list->set_manifest(s.data(), s.size());
312 list->set_version(4);
313 STObject const signature(sfSignature);
314 xrpl::sign(st, HashPrefix::Manifest, KeyType::Ed25519, std::get<1>(signing));
315 Serializer s1;
316 st.add(s1);
317 auto& blob = *list->add_blobs();
318 blob.set_signature(s1.data(), s1.size());
319 blob.set_blob(strHex(s.slice()));
320 return list;
321 }
322
323 void
325 {
326 auto thresh = beast::Severity::Info;
327 auto logs = std::make_unique<Logs>(thresh);
328
329 protocol::TMManifests const manifests;
330 protocol::TMEndpoints const endpoints;
331 protocol::TMTransaction const transaction;
332 protocol::TMGetLedger const getLedger;
333 protocol::TMLedgerData const ledgerData;
334 protocol::TMGetObjectByHash const getObject;
335 protocol::TMValidatorListCollection const validatorListCollection;
336
337 // 4.5KB
338 doTest(buildManifests(20), protocol::mtMANIFESTS, 4, "TMManifests20");
339 // 22KB
340 doTest(buildManifests(100), protocol::mtMANIFESTS, 4, "TMManifests100");
341 // 131B
342 doTest(buildEndpoints(10), protocol::mtENDPOINTS, 4, "TMEndpoints10");
343 // 1.3KB
344 doTest(buildEndpoints(100), protocol::mtENDPOINTS, 4, "TMEndpoints100");
345 // 242B
346 doTest(buildTransaction(*logs), protocol::mtTRANSACTION, 1, "TMTransaction");
347 // 87B
348 doTest(buildGetLedger(), protocol::mtGET_LEDGER, 1, "TMGetLedger");
349 // 61KB
350 doTest(buildLedgerData(500, *logs), protocol::mtLEDGER_DATA, 10, "TMLedgerData500");
351 // 122 KB
352 doTest(buildLedgerData(1000, *logs), protocol::mtLEDGER_DATA, 20, "TMLedgerData1000");
353 // 1.2MB
354 doTest(buildLedgerData(10000, *logs), protocol::mtLEDGER_DATA, 50, "TMLedgerData10000");
355 // 12MB
356 doTest(buildLedgerData(100000, *logs), protocol::mtLEDGER_DATA, 100, "TMLedgerData100000");
357 // 61MB
358 doTest(buildLedgerData(500000, *logs), protocol::mtLEDGER_DATA, 100, "TMLedgerData500000");
359 // 7.7KB
360 doTest(buildGetObjectByHash(), protocol::mtGET_OBJECTS, 4, "TMGetObjectByHash");
361 doTest(
363 protocol::mtVALIDATOR_LIST_COLLECTION,
364 4,
365 "TMValidatorListCollection");
366 }
367
368 void
370 {
371 testcase("Handshake");
372 auto getEnv = [&](bool enable) {
373 Config c;
375 str << "[reduce_relay]\n"
376 << "vp_base_squelch_enable=1\n"
377 << "[compression]\n"
378 << enable << "\n";
379 c.loadFromString(str.str());
380 auto env = std::make_shared<jtx::Env>(*this);
381 env->app().config().compression = c.compression;
382 env->app().config().vpReduceRelayBaseSquelchEnable = c.vpReduceRelayBaseSquelchEnable;
383 return env;
384 };
385 auto handshake = [&](int outboundEnable, int inboundEnable) {
386 beast::ip::Address const addr = boost::asio::ip::make_address("172.1.1.100");
387
388 auto env = getEnv(outboundEnable);
389 auto request = xrpl::makeRequest(
390 true,
391 env->app().config().compression,
392 false,
393 env->app().config().txReduceRelayEnable,
394 env->app().config().vpReduceRelayBaseSquelchEnable);
395 http_request_type httpRequest;
396 httpRequest.version(request.version());
397 httpRequest.base() = request.base();
398 // feature enabled on the peer's connection only if both sides are
399 // enabled
400 auto const peerEnabled = inboundEnable && outboundEnable;
401 // inbound is enabled if the request's header has the feature
402 // enabled and the peer's configuration is enabled
403 auto const inboundEnabled =
404 peerFeatureEnabled(httpRequest, kFeatureCompr, "lz4", inboundEnable);
405 BEAST_EXPECT(!(peerEnabled ^ inboundEnabled));
406
407 env.reset();
408 env = getEnv(inboundEnable);
409 auto httpResp = xrpl::makeResponse(
410 true, httpRequest, addr, addr, uint256{1}, 1, {1, 0}, env->app());
411 // outbound is enabled if the response's header has the feature
412 // enabled and the peer's configuration is enabled
413 auto const outboundEnabled =
414 peerFeatureEnabled(httpResp, kFeatureCompr, "lz4", outboundEnable);
415 BEAST_EXPECT(!(peerEnabled ^ outboundEnabled));
416 };
417 handshake(1, 1);
418 handshake(1, 0);
419 handshake(0, 1);
420 handshake(0, 0);
421 }
422
423 void
424 run() override
425 {
426 testProtocol();
428 }
429};
430
432
433} // namespace xrpl::test
T begin(T... args)
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:117
virtual Config & config()=0
pointer data()
Definition base_uint.h:117
iterator begin()
Definition base_uint.h:128
static constexpr std::size_t size()
Definition base_uint.h:548
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
Manages partitions for logging.
Definition Log.h:23
std::vector< uint8_t > const & getBuffer(Compressed tryCompressed)
Retrieve the packed message data.
Definition Message.cpp:201
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
Identifies a node inside a SHAMap.
std::string getRawString() const
void add(Serializer &s) const override
Definition STObject.cpp:123
void const * getDataPtr() const
Definition Serializer.h:198
int getLength() const
Definition Serializer.h:208
Slice slice() const noexcept
Definition Serializer.h:45
std::size_t size() const noexcept
Definition Serializer.h:51
void const * data() const noexcept
Definition Serializer.h:57
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:170
Implements ZeroCopyInputStream around a buffer sequence.
static std::shared_ptr< protocol::TMEndpoints > buildEndpoints(int n)
void doTest(std::shared_ptr< T > proto, protocol::MessageType mt, uint16_t nbuffers, std::string msg)
compression::Compressed Compressed
static std::shared_ptr< protocol::TMLedgerData > buildLedgerData(uint32_t n, Logs &logs)
std::shared_ptr< protocol::TMTransaction > buildTransaction(Logs &logs)
static std::shared_ptr< protocol::TMValidatorListCollection > buildValidatorListCollection()
compression::Algorithm Algorithm
static std::shared_ptr< protocol::TMGetLedger > buildGetLedger()
void run() override
Runs the suite.
static std::shared_ptr< protocol::TMGetObjectByHash > buildGetObjectByHash()
static std::shared_ptr< protocol::TMManifests > buildManifests(int n)
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
T data(T... args)
T equal(T... args)
T make_shared(T... args)
T make_unique(T... args)
boost::asio::ip::address Address
Definition IPAddress.h:20
constexpr std::size_t kHeaderBytes
Definition Compression.h:12
std::size_t decompress(InputStream &in, std::size_t inSize, std::uint8_t *decompressed, std::size_t decompressedSize, Algorithm algorithm=Algorithm::LZ4)
Decompress input stream.
Definition Compression.h:32
std::optional< MessageHeader > parseMessageHeader(boost::system::error_code &ec, BufferSequence const &bufs, std::size_t size)
Parse a message header.
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
std::vector< STAmount > fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
Definition AMMTest.cpp:34
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
void sign(json::Value &jv, Account const &account, json::Value &sigObject)
Sign automatically into a specific Json field of the jv object.
Definition utility.cpp:40
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:37
static uint256 ledgerHash(LedgerHeader const &info)
BEAST_DEFINE_TESTSUITE_MANUAL(AMMCalc, app, xrpl)
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:54
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpcVersion, std::unordered_map< std::string, std::string > const &headers)
Returns a client operating through WebSockets/S.
Definition WSClient.cpp:371
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:215
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
Integral randInt(Engine &engine, Integral min, Integral max)
Return a uniformly distributed random integer.
SField const sfGeneric
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:58
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
void addRaw(LedgerHeader const &, Serializer &, bool includeHash=false)
@ LedgerMaster
ledger master data for signing
Definition HashPrefix.h:59
@ Manifest
Manifest.
Definition HashPrefix.h:84
http_response_type makeResponse(bool crawlPublic, http_request_type const &req, beast::ip::Address publicIp, beast::ip::Address remoteIp, uint256 const &sharedValue, std::optional< std::uint32_t > networkID, ProtocolVersion protocol, Application &app)
Make http response.
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
bool peerFeatureEnabled(Headers const &request, std::string const &feature, std::string value, bool config)
Check if a feature should be enabled for a peer.
Definition Handshake.h:182
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition Handoff.h:12
static constexpr char kFeatureCompr[]
Definition Handshake.h:125
BaseUInt< 256 > uint256
Definition base_uint.h:580
T resize(T... args)
T size(T... args)
T str(T... args)
Information about the notional ledger backing the view.
NetClock::time_point parentCloseTime
NetClock::duration closeTimeResolution
NetClock::time_point closeTime
T time_since_epoch(T... args)
T to_string(T... args)