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,
91 printf(
"=== compress/decompress %s ===\n", msg);
94 auto& buffer = m.
getBuffer(Compressed::On);
98 "==> compressed, original %d bytes, compressed %d bytes\n",
102 boost::beast::multi_buffer buffers;
105 auto sz = buffer.size() / nbuffers;
106 for (
int i = 0; i < nbuffers; i++)
108 auto start = buffer.begin() + sz * i;
109 auto end = i < nbuffers - 1 ? (buffer.begin() + sz * (i + 1))
112 buffers.commit(boost::asio::buffer_copy(
113 buffers.prepare(slice.
size()), boost::asio::buffer(slice)));
120 "==> parsed header: buffers size %d, compressed %d, algorithm "
121 "%d, header size %d, payload size %d, buffer size %d\n",
123 header->algorithm != Algorithm::None,
124 (
int)header->algorithm,
125 (
int)header->header_size,
126 (
int)header->payload_wire_size,
129 if (header->algorithm == Algorithm::None)
132 printf(
"==> NOT COMPRESSED\n");
137 decompressed.
resize(header->uncompressed_size);
140 header->payload_wire_size == buffer.size() - header->header_size);
143 stream.Skip(header->header_size);
147 header->payload_wire_size,
149 header->uncompressed_size);
150 BEAST_EXPECT(decompressedSize == header->uncompressed_size);
151 auto const proto1 = std::make_shared<T>();
154 proto1->ParseFromArray(decompressed.
data(), decompressedSize));
155 auto uncompressed = m.
getBuffer(Compressed::Off);
159 decompressed.
begin()));
167 auto manifests = std::make_shared<protocol::TMManifests>();
168 manifests->mutable_list()->Reserve(n);
169 for (
int i = 0; i < n; i++)
190 std::get<1>(signing));
193 auto*
manifest = manifests->add_list();
202 auto endpoints = std::make_shared<protocol::TMEndpoints>();
203 endpoints->mutable_endpoints()->Reserve(n);
204 for (
int i = 0; i < n; i++)
206 auto* endpoint = endpoints->add_endpoints();
207 endpoint->set_hops(i);
209 endpoint->mutable_ipv4()->set_ipv4(boost::endian::native_to_big(
210 boost::asio::ip::address_v4::from_string(addr).to_uint()));
211 endpoint->mutable_ipv4()->set_ipv4port(i);
213 endpoints->set_version(2);
223 auto const alice =
Account(
"alice");
224 auto const bob =
Account(
"bob");
225 env.
fund(
XRP(fund),
"alice",
"bob");
226 env.
trust(bob[
"USD"](fund), alice);
231 for (
size_t i = 0; i < text.size(); ++i)
248 jrequestUsd[jss::tx_json] =
249 pay(
"bob",
"alice", bob[
"USD"](fund / 2));
250 Json::Value jreply_usd = wsc->invoke(
"sign", jrequestUsd);
253 toBinary(jreply_usd[jss::result][jss::tx_blob].asString());
256 auto transaction = std::make_shared<protocol::TMTransaction>();
257 transaction->set_rawtransaction(usdTxBlob);
258 transaction->set_status(protocol::tsNEW);
260 transaction->set_receivetimestamp(tk->now().time_since_epoch().count());
261 transaction->set_deferred(
true);
269 auto getLedger = std::make_shared<protocol::TMGetLedger>();
270 getLedger->set_itype(protocol::liTS_CANDIDATE);
271 getLedger->set_ltype(protocol::TMLedgerType::ltACCEPTED);
273 getLedger->set_ledgerhash(hash.
begin(), hash.
size());
274 getLedger->set_ledgerseq(123456789);
277 getLedger->set_requestcookie(123456789);
278 getLedger->set_querytype(protocol::qtINDIRECT);
279 getLedger->set_querydepth(3);
286 auto ledgerData = std::make_shared<protocol::TMLedgerData>();
288 ledgerData->set_ledgerhash(hash.
data(), hash.
size());
289 ledgerData->set_ledgerseq(123456789);
290 ledgerData->set_type(protocol::TMLedgerInfoType::liAS_NODE);
291 ledgerData->set_requestcookie(123456789);
292 ledgerData->set_error(protocol::TMReplyError::reNO_LEDGER);
293 ledgerData->mutable_nodes()->Reserve(n);
295 for (
int i = 0; i < n; i++)
311 ledgerData->add_nodes()->set_nodedata(
321 auto getObject = std::make_shared<protocol::TMGetObjectByHash>();
323 getObject->set_type(protocol::TMGetObjectByHash_ObjectType::
324 TMGetObjectByHash_ObjectType_otTRANSACTION);
325 getObject->set_query(
true);
326 getObject->set_seq(123456789);
328 getObject->set_ledgerhash(hash.
data(), hash.
size());
329 getObject->set_fat(
true);
330 for (
int i = 0; i < 100; i++)
333 auto object = getObject->add_objects();
334 object->set_hash(hash.
data(), hash.
size());
337 object->set_index(
"");
338 object->set_data(
"");
339 object->set_ledgerseq(i);
347 auto list = std::make_shared<protocol::TMValidatorList>();
365 list->set_manifest(s.
data(), s.
size());
366 list->set_version(3);
372 list->set_signature(s1.
data(), s1.
size());
380 testcase(
"Message Compression");
382 auto thresh = beast::severities::Severity::kInfo;
383 auto logs = std::make_unique<Logs>(thresh);
385 protocol::TMManifests manifests;
386 protocol::TMEndpoints endpoints;
387 protocol::TMTransaction transaction;
388 protocol::TMGetLedger get_ledger;
389 protocol::TMLedgerData ledger_data;
390 protocol::TMGetObjectByHash get_object;
391 protocol::TMValidatorList validator_list;
394 doTest(buildManifests(20), protocol::mtMANIFESTS, 4,
"TMManifests20");
396 doTest(buildManifests(100), protocol::mtMANIFESTS, 4,
"TMManifests100");
398 doTest(buildEndpoints(10), protocol::mtENDPOINTS, 4,
"TMEndpoints10");
400 doTest(buildEndpoints(100), protocol::mtENDPOINTS, 4,
"TMEndpoints100");
403 buildTransaction(*logs),
404 protocol::mtTRANSACTION,
408 doTest(buildGetLedger(), protocol::mtGET_LEDGER, 1,
"TMGetLedger");
411 buildLedgerData(500, *logs),
412 protocol::mtLEDGER_DATA,
417 buildLedgerData(1000, *logs),
418 protocol::mtLEDGER_DATA,
423 buildLedgerData(10000, *logs),
424 protocol::mtLEDGER_DATA,
426 "TMLedgerData10000");
429 buildLedgerData(100000, *logs),
430 protocol::mtLEDGER_DATA,
432 "TMLedgerData100000");
435 buildLedgerData(500000, *logs),
436 protocol::mtLEDGER_DATA,
438 "TMLedgerData500000");
441 buildGetObjectByHash(),
442 protocol::mtGET_OBJECTS,
444 "TMGetObjectByHash");
447 buildValidatorList(),
448 protocol::mtVALIDATORLIST,