20 #include <ripple/app/ledger/Ledger.h>
21 #include <ripple/app/ledger/LedgerMaster.h>
22 #include <ripple/app/misc/Manifest.h>
23 #include <ripple/beast/unit_test.h>
24 #include <ripple/beast/utility/Journal.h>
25 #include <ripple/core/TimeKeeper.h>
26 #include <ripple/overlay/Compression.h>
27 #include <ripple/overlay/Message.h>
28 #include <ripple/overlay/impl/ProtocolMessage.h>
29 #include <ripple/overlay/impl/ZeroCopyStream.h>
30 #include <ripple/protocol/HashPrefix.h>
31 #include <ripple/protocol/PublicKey.h>
32 #include <ripple/protocol/SecretKey.h>
33 #include <ripple/protocol/Sign.h>
34 #include <ripple/protocol/digest.h>
35 #include <ripple/protocol/jss.h>
36 #include <ripple/shamap/SHAMapNodeID.h>
37 #include <boost/asio/ip/address_v4.hpp>
38 #include <boost/beast/core/multi_buffer.hpp>
39 #include <boost/endian/conversion.hpp>
41 #include <ripple.pb.h>
42 #include <test/jtx/Account.h>
43 #include <test/jtx/Env.h>
44 #include <test/jtx/WSClient.h>
45 #include <test/jtx/amount.h>
46 #include <test/jtx/pay.h>
85 protocol::MessageType mt,
89 testcase(
"Compress/Decompress: " + msg);
93 auto& buffer = m.
getBuffer(Compressed::On);
95 boost::beast::multi_buffer buffers;
98 auto sz = buffer.
size() / nbuffers;
99 for (
int i = 0; i < nbuffers; i++)
101 auto start = buffer.begin() + sz * i;
102 auto end = i < nbuffers - 1 ? (buffer.begin() + sz * (i + 1))
105 buffers.commit(boost::asio::buffer_copy(
106 buffers.prepare(slice.
size()), boost::asio::buffer(slice)));
109 boost::system::error_code ec;
111 ec, buffers.data(), buffer.size());
113 BEAST_EXPECT(header);
115 if (header->algorithm == Algorithm::None)
119 decompressed.
resize(header->uncompressed_size);
122 header->payload_wire_size == buffer.size() - header->header_size);
125 stream.Skip(header->header_size);
129 header->payload_wire_size,
131 header->uncompressed_size);
132 BEAST_EXPECT(decompressedSize == header->uncompressed_size);
133 auto const proto1 = std::make_shared<T>();
136 proto1->ParseFromArray(decompressed.
data(), decompressedSize));
137 auto uncompressed = m.
getBuffer(Compressed::Off);
141 decompressed.
begin()));
147 auto manifests = std::make_shared<protocol::TMManifests>();
148 manifests->mutable_list()->Reserve(n);
149 for (
int i = 0; i < n; i++)
170 std::get<1>(signing));
173 auto*
manifest = manifests->add_list();
182 auto endpoints = std::make_shared<protocol::TMEndpoints>();
183 endpoints->mutable_endpoints()->Reserve(n);
184 for (
int i = 0; i < n; i++)
186 auto* endpoint = endpoints->add_endpoints();
187 endpoint->set_hops(i);
189 endpoint->mutable_ipv4()->set_ipv4(boost::endian::native_to_big(
190 boost::asio::ip::address_v4::from_string(addr).to_uint()));
191 endpoint->mutable_ipv4()->set_ipv4port(i);
193 endpoints->set_version(2);
203 auto const alice =
Account(
"alice");
204 auto const bob =
Account(
"bob");
205 env.
fund(
XRP(fund),
"alice",
"bob");
206 env.
trust(bob[
"USD"](fund), alice);
211 for (
size_t i = 0; i < text.size(); ++i)
228 jrequestUsd[jss::tx_json] =
229 pay(
"bob",
"alice", bob[
"USD"](fund / 2));
230 Json::Value jreply_usd = wsc->invoke(
"sign", jrequestUsd);
233 toBinary(jreply_usd[jss::result][jss::tx_blob].asString());
236 auto transaction = std::make_shared<protocol::TMTransaction>();
237 transaction->set_rawtransaction(usdTxBlob);
238 transaction->set_status(protocol::tsNEW);
240 transaction->set_receivetimestamp(tk->now().time_since_epoch().count());
241 transaction->set_deferred(
true);
249 auto getLedger = std::make_shared<protocol::TMGetLedger>();
250 getLedger->set_itype(protocol::liTS_CANDIDATE);
251 getLedger->set_ltype(protocol::TMLedgerType::ltACCEPTED);
253 getLedger->set_ledgerhash(hash.
begin(), hash.
size());
254 getLedger->set_ledgerseq(123456789);
257 getLedger->set_requestcookie(123456789);
258 getLedger->set_querytype(protocol::qtINDIRECT);
259 getLedger->set_querydepth(3);
266 auto ledgerData = std::make_shared<protocol::TMLedgerData>();
268 ledgerData->set_ledgerhash(hash.
data(), hash.
size());
269 ledgerData->set_ledgerseq(123456789);
270 ledgerData->set_type(protocol::TMLedgerInfoType::liAS_NODE);
271 ledgerData->set_requestcookie(123456789);
272 ledgerData->set_error(protocol::TMReplyError::reNO_LEDGER);
273 ledgerData->mutable_nodes()->Reserve(n);
275 for (
int i = 0; i < n; i++)
291 ledgerData->add_nodes()->set_nodedata(
301 auto getObject = std::make_shared<protocol::TMGetObjectByHash>();
303 getObject->set_type(protocol::TMGetObjectByHash_ObjectType::
304 TMGetObjectByHash_ObjectType_otTRANSACTION);
305 getObject->set_query(
true);
306 getObject->set_seq(123456789);
308 getObject->set_ledgerhash(hash.
data(), hash.
size());
309 getObject->set_fat(
true);
310 for (
int i = 0; i < 100; i++)
313 auto object = getObject->add_objects();
314 object->set_hash(hash.
data(), hash.
size());
317 object->set_index(
"");
318 object->set_data(
"");
319 object->set_ledgerseq(i);
327 auto list = std::make_shared<protocol::TMValidatorList>();
345 list->set_manifest(s.
data(), s.
size());
346 list->set_version(3);
352 list->set_signature(s1.
data(), s1.
size());
360 testcase(
"Message Compression");
362 auto thresh = beast::severities::Severity::kInfo;
363 auto logs = std::make_unique<Logs>(thresh);
365 protocol::TMManifests manifests;
366 protocol::TMEndpoints endpoints;
367 protocol::TMTransaction transaction;
368 protocol::TMGetLedger get_ledger;
369 protocol::TMLedgerData ledger_data;
370 protocol::TMGetObjectByHash get_object;
371 protocol::TMValidatorList validator_list;
374 doTest(buildManifests(20), protocol::mtMANIFESTS, 4,
"TMManifests20");
376 doTest(buildManifests(100), protocol::mtMANIFESTS, 4,
"TMManifests100");
378 doTest(buildEndpoints(10), protocol::mtENDPOINTS, 4,
"TMEndpoints10");
380 doTest(buildEndpoints(100), protocol::mtENDPOINTS, 4,
"TMEndpoints100");
383 buildTransaction(*logs),
384 protocol::mtTRANSACTION,
388 doTest(buildGetLedger(), protocol::mtGET_LEDGER, 1,
"TMGetLedger");
391 buildLedgerData(500, *logs),
392 protocol::mtLEDGER_DATA,
397 buildLedgerData(1000, *logs),
398 protocol::mtLEDGER_DATA,
403 buildLedgerData(10000, *logs),
404 protocol::mtLEDGER_DATA,
406 "TMLedgerData10000");
409 buildLedgerData(100000, *logs),
410 protocol::mtLEDGER_DATA,
412 "TMLedgerData100000");
415 buildLedgerData(500000, *logs),
416 protocol::mtLEDGER_DATA,
418 "TMLedgerData500000");
421 buildGetObjectByHash(),
422 protocol::mtGET_OBJECTS,
424 "TMGetObjectByHash");
427 buildValidatorList(),
428 protocol::mtVALIDATORLIST,