20 #include <ripple/app/misc/Manifest.h>
21 #include <ripple/app/ledger/Ledger.h>
22 #include <ripple/app/ledger/LedgerMaster.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/impl/ZeroCopyStream.h>
28 #include <ripple/overlay/impl/ProtocolMessage.h>
29 #include <ripple/overlay/Message.h>
30 #include <ripple/protocol/digest.h>
31 #include <ripple/protocol/HashPrefix.h>
32 #include <ripple/protocol/jss.h>
33 #include <ripple/protocol/PublicKey.h>
34 #include <ripple/protocol/SecretKey.h>
35 #include <ripple/protocol/Sign.h>
36 #include <ripple/shamap/SHAMapNodeID.h>
37 #include <ripple.pb.h>
38 #include <boost/asio/ip/address_v4.hpp>
39 #include <boost/beast/core/multi_buffer.hpp>
40 #include <boost/endian/conversion.hpp>
41 #include <test/jtx/Account.h>
42 #include <test/jtx/amount.h>
43 #include <test/jtx/Env.h>
44 #include <test/jtx/pay.h>
45 #include <test/jtx/WSClient.h>
84 printf(
"=== compress/decompress %s ===\n", msg);
87 auto &buffer = m.
getBuffer(Compressed::On);
90 printf(
"==> compressed, original %d bytes, compressed %d bytes\n",
94 boost::beast::multi_buffer buffers;
98 auto sz = buffer.size() / nbuffers;
99 for (
int i = 0; i < nbuffers; i++) {
100 auto start = buffer.begin() + sz * i;
101 auto end = i < nbuffers - 1 ? (buffer.begin() + sz * (i + 1)) : buffer.end();
104 boost::asio::buffer_copy(buffers.prepare(slice.
size()), boost::asio::buffer(slice)));
109 printf(
"==> parsed header: buffers size %d, compressed %d, algorithm %d, header size %d, payload size %d, buffer size %d\n",
110 (
int)buffers.size(), header->algorithm != Algorithm::None, (
int)header->algorithm,
111 (
int)header->header_size, (
int)header->payload_wire_size, (
int)buffer.size());
113 if (header->algorithm == Algorithm::None) {
115 printf(
"==> NOT COMPRESSED\n");
120 decompressed.
resize(header->uncompressed_size);
122 BEAST_EXPECT(header->payload_wire_size == buffer.size() - header->header_size);
125 stream.Skip(header->header_size);
128 decompressed.
data(), header->uncompressed_size);
129 BEAST_EXPECT(decompressedSize == header->uncompressed_size);
130 auto const proto1 = std::make_shared<T>();
132 BEAST_EXPECT(proto1->ParseFromArray(decompressed.
data(), decompressedSize));
133 auto uncompressed = m.
getBuffer(Compressed::Off);
136 decompressed.
begin()));
143 auto manifests = std::make_shared<protocol::TMManifests>();
144 manifests->mutable_list()->Reserve(n);
145 for (
int i = 0; i < n; i++) {
157 auto *
manifest = manifests->add_list();
165 auto endpoints = std::make_shared<protocol::TMEndpoints>();
166 endpoints->mutable_endpoints()->Reserve(n);
167 for (
int i = 0; i < n; i++) {
168 auto *endpoint = endpoints->add_endpoints();
169 endpoint->set_hops(i);
171 endpoint->mutable_ipv4()->set_ipv4(
172 boost::endian::native_to_big(boost::asio::ip::address_v4::from_string(addr).to_uint()));
173 endpoint->mutable_ipv4()->set_ipv4port(i);
175 endpoints->set_version(2);
184 auto const alice =
Account(
"alice");
185 auto const bob =
Account(
"bob");
186 env.
fund(
XRP(fund),
"alice",
"bob");
187 env.
trust(bob[
"USD"](fund), alice);
192 for (
size_t i = 0; i < text.size(); ++i) {
208 jrequestUsd[jss::tx_json] =
209 pay(
"bob",
"alice", bob[
"USD"](fund / 2));
210 Json::Value jreply_usd = wsc->invoke(
"sign", jrequestUsd);
213 toBinary(jreply_usd[jss::result][jss::tx_blob].asString());
216 auto transaction = std::make_shared<protocol::TMTransaction>();
217 transaction->set_rawtransaction(usdTxBlob);
218 transaction->set_status(protocol::tsNEW);
220 transaction->set_receivetimestamp(tk->now().time_since_epoch().count());
221 transaction->set_deferred(
true);
228 auto getLedger = std::make_shared<protocol::TMGetLedger>();
229 getLedger->set_itype(protocol::liTS_CANDIDATE);
230 getLedger->set_ltype(protocol::TMLedgerType::ltACCEPTED);
232 getLedger->set_ledgerhash(hash.
begin(), hash.
size());
233 getLedger->set_ledgerseq(123456789);
236 getLedger->set_requestcookie(123456789);
237 getLedger->set_querytype(protocol::qtINDIRECT);
238 getLedger->set_querydepth(3);
244 auto ledgerData = std::make_shared<protocol::TMLedgerData>();
246 ledgerData->set_ledgerhash(hash.
data(), hash.
size());
247 ledgerData->set_ledgerseq(123456789);
248 ledgerData->set_type(protocol::TMLedgerInfoType::liAS_NODE);
249 ledgerData->set_requestcookie(123456789);
250 ledgerData->set_error(protocol::TMReplyError::reNO_LEDGER);
251 ledgerData->mutable_nodes()->Reserve(n);
253 for (
int i = 0; i < n; i++) {
276 auto getObject = std::make_shared<protocol::TMGetObjectByHash>();
278 getObject->set_type(protocol::TMGetObjectByHash_ObjectType::TMGetObjectByHash_ObjectType_otTRANSACTION);
279 getObject->set_query(
true);
280 getObject->set_seq(123456789);
282 getObject->set_ledgerhash(hash.
data(), hash.
size());
283 getObject->set_fat(
true);
284 for (
int i = 0; i < 100; i++) {
286 auto object = getObject->add_objects();
287 object->set_hash(hash.
data(), hash.
size());
290 object->set_index(
"");
291 object->set_data(
"");
292 object->set_ledgerseq(i);
300 auto list = std::make_shared<protocol::TMValidatorList>();
313 list->set_manifest(s.
data(), s.
size());
314 list->set_version(3);
319 list->set_signature(s1.
data(), s1.
size());
326 testcase(
"Message Compression");
328 auto thresh = beast::severities::Severity::kInfo;
329 auto logs = std::make_unique<Logs>(thresh);
331 protocol::TMManifests manifests;
332 protocol::TMEndpoints endpoints;
333 protocol::TMTransaction transaction;
334 protocol::TMGetLedger get_ledger;
335 protocol::TMLedgerData ledger_data;
336 protocol::TMGetObjectByHash get_object;
337 protocol::TMValidatorList validator_list;
340 doTest(buildManifests(20), protocol::mtMANIFESTS, 4,
"TMManifests20");
342 doTest(buildManifests(100), protocol::mtMANIFESTS, 4,
"TMManifests100");
344 doTest(buildEndpoints(10), protocol::mtENDPOINTS, 4,
"TMEndpoints10");
346 doTest(buildEndpoints(100), protocol::mtENDPOINTS, 4,
"TMEndpoints100");
348 doTest(buildTransaction(*logs), protocol::mtTRANSACTION, 1,
"TMTransaction");
350 doTest(buildGetLedger(), protocol::mtGET_LEDGER, 1,
"TMGetLedger");
352 doTest(buildLedgerData(500, *logs), protocol::mtLEDGER_DATA, 10,
"TMLedgerData500");
354 doTest(buildLedgerData(1000, *logs), protocol::mtLEDGER_DATA, 20,
"TMLedgerData1000");
356 doTest(buildLedgerData(10000, *logs), protocol::mtLEDGER_DATA, 50,
"TMLedgerData10000");
358 doTest(buildLedgerData(100000, *logs), protocol::mtLEDGER_DATA, 100,
"TMLedgerData100000");
360 doTest(buildLedgerData(500000, *logs), protocol::mtLEDGER_DATA, 100,
"TMLedgerData500000");
362 doTest(buildGetObjectByHash(), protocol::mtGET_OBJECTS, 4,
"TMGetObjectByHash");
364 doTest(buildValidatorList(), protocol::mtVALIDATORLIST, 4,
"TMValidatorList");