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/Handshake.h>
29 #include <ripple/overlay/impl/ProtocolMessage.h>
30 #include <ripple/overlay/impl/ZeroCopyStream.h>
31 #include <ripple/protocol/HashPrefix.h>
32 #include <ripple/protocol/PublicKey.h>
33 #include <ripple/protocol/SecretKey.h>
34 #include <ripple/protocol/Sign.h>
35 #include <ripple/protocol/digest.h>
36 #include <ripple/protocol/jss.h>
37 #include <ripple/shamap/SHAMapNodeID.h>
38 #include <boost/asio/ip/address_v4.hpp>
39 #include <boost/beast/core/multi_buffer.hpp>
40 #include <boost/endian/conversion.hpp>
42 #include <ripple.pb.h>
43 #include <test/jtx/Account.h>
44 #include <test/jtx/Env.h>
45 #include <test/jtx/WSClient.h>
46 #include <test/jtx/amount.h>
47 #include <test/jtx/pay.h>
86 protocol::MessageType mt,
90 testcase(
"Compress/Decompress: " + msg);
94 auto& buffer = m.
getBuffer(Compressed::On);
96 boost::beast::multi_buffer buffers;
99 auto sz = buffer.
size() / nbuffers;
100 for (
int i = 0; i < nbuffers; i++)
102 auto start = buffer.begin() + sz * i;
103 auto end = i < nbuffers - 1 ? (buffer.begin() + sz * (i + 1))
106 buffers.commit(boost::asio::buffer_copy(
107 buffers.prepare(slice.
size()), boost::asio::buffer(slice)));
110 boost::system::error_code ec;
112 ec, buffers.data(), buffer.size());
114 BEAST_EXPECT(header);
116 if (!header || header->algorithm == Algorithm::None)
120 decompressed.
resize(header->uncompressed_size);
123 header->payload_wire_size == buffer.size() - header->header_size);
126 stream.Skip(header->header_size);
130 header->payload_wire_size,
132 header->uncompressed_size);
133 BEAST_EXPECT(decompressedSize == header->uncompressed_size);
134 auto const proto1 = std::make_shared<T>();
137 proto1->ParseFromArray(decompressed.
data(), decompressedSize));
138 auto uncompressed = m.
getBuffer(Compressed::Off);
142 decompressed.
begin()));
148 auto manifests = std::make_shared<protocol::TMManifests>();
149 manifests->mutable_list()->Reserve(n);
150 for (
int i = 0; i < n; i++)
171 std::get<1>(signing));
174 auto*
manifest = manifests->add_list();
183 auto endpoints = std::make_shared<protocol::TMEndpoints>();
184 endpoints->mutable_endpoints_v2()->Reserve(n);
185 for (
int i = 0; i < n; i++)
187 auto ep = endpoints->add_endpoints_v2();
191 endpoints->set_version(2);
201 auto const alice =
Account(
"alice");
202 auto const bob =
Account(
"bob");
203 env.
fund(
XRP(fund),
"alice",
"bob");
204 env.
trust(bob[
"USD"](fund), alice);
211 reinterpret_cast<char const*
>(blob->data()), blob->
size()};
219 jrequestUsd[jss::tx_json] =
220 pay(
"bob",
"alice", bob[
"USD"](fund / 2));
221 Json::Value jreply_usd = wsc->invoke(
"sign", jrequestUsd);
224 toBinary(jreply_usd[jss::result][jss::tx_blob].asString());
227 auto transaction = std::make_shared<protocol::TMTransaction>();
228 transaction->set_rawtransaction(usdTxBlob);
229 transaction->set_status(protocol::tsNEW);
231 transaction->set_receivetimestamp(tk->now().time_since_epoch().count());
232 transaction->set_deferred(
true);
240 auto getLedger = std::make_shared<protocol::TMGetLedger>();
241 getLedger->set_itype(protocol::liTS_CANDIDATE);
242 getLedger->set_ltype(protocol::TMLedgerType::ltACCEPTED);
244 getLedger->set_ledgerhash(hash.
begin(), hash.
size());
245 getLedger->set_ledgerseq(123456789);
248 getLedger->set_requestcookie(123456789);
249 getLedger->set_querytype(protocol::qtINDIRECT);
250 getLedger->set_querydepth(3);
257 auto ledgerData = std::make_shared<protocol::TMLedgerData>();
259 ledgerData->set_ledgerhash(hash.
data(), hash.
size());
260 ledgerData->set_ledgerseq(123456789);
261 ledgerData->set_type(protocol::TMLedgerInfoType::liAS_NODE);
262 ledgerData->set_requestcookie(123456789);
263 ledgerData->set_error(protocol::TMReplyError::reNO_LEDGER);
264 ledgerData->mutable_nodes()->Reserve(n);
266 for (
int i = 0; i < n; i++)
282 ledgerData->add_nodes()->set_nodedata(
292 auto getObject = std::make_shared<protocol::TMGetObjectByHash>();
294 getObject->set_type(protocol::TMGetObjectByHash_ObjectType::
295 TMGetObjectByHash_ObjectType_otTRANSACTION);
296 getObject->set_query(
true);
297 getObject->set_seq(123456789);
299 getObject->set_ledgerhash(hash.
data(), hash.
size());
300 getObject->set_fat(
true);
301 for (
int i = 0; i < 100; i++)
304 auto object = getObject->add_objects();
305 object->set_hash(hash.
data(), hash.
size());
308 object->set_index(
"");
309 object->set_data(
"");
310 object->set_ledgerseq(i);
318 auto list = std::make_shared<protocol::TMValidatorList>();
336 list->set_manifest(s.
data(), s.
size());
337 list->set_version(3);
343 list->set_signature(s1.
data(), s1.
size());
351 auto list = std::make_shared<protocol::TMValidatorListCollection>();
369 list->set_manifest(s.
data(), s.
size());
370 list->set_version(4);
376 auto& blob = *list->add_blobs();
377 blob.set_signature(s1.
data(), s1.
size());
385 auto thresh = beast::severities::Severity::kInfo;
386 auto logs = std::make_unique<Logs>(thresh);
388 protocol::TMManifests manifests;
389 protocol::TMEndpoints endpoints;
390 protocol::TMTransaction transaction;
391 protocol::TMGetLedger get_ledger;
392 protocol::TMLedgerData ledger_data;
393 protocol::TMGetObjectByHash get_object;
394 protocol::TMValidatorList validator_list;
395 protocol::TMValidatorListCollection validator_list_collection;
398 doTest(buildManifests(20), protocol::mtMANIFESTS, 4,
"TMManifests20");
400 doTest(buildManifests(100), protocol::mtMANIFESTS, 4,
"TMManifests100");
402 doTest(buildEndpoints(10), protocol::mtENDPOINTS, 4,
"TMEndpoints10");
404 doTest(buildEndpoints(100), protocol::mtENDPOINTS, 4,
"TMEndpoints100");
407 buildTransaction(*logs),
408 protocol::mtTRANSACTION,
412 doTest(buildGetLedger(), protocol::mtGET_LEDGER, 1,
"TMGetLedger");
415 buildLedgerData(500, *logs),
416 protocol::mtLEDGER_DATA,
421 buildLedgerData(1000, *logs),
422 protocol::mtLEDGER_DATA,
427 buildLedgerData(10000, *logs),
428 protocol::mtLEDGER_DATA,
430 "TMLedgerData10000");
433 buildLedgerData(100000, *logs),
434 protocol::mtLEDGER_DATA,
436 "TMLedgerData100000");
439 buildLedgerData(500000, *logs),
440 protocol::mtLEDGER_DATA,
442 "TMLedgerData500000");
445 buildGetObjectByHash(),
446 protocol::mtGET_OBJECTS,
448 "TMGetObjectByHash");
451 buildValidatorList(),
452 protocol::mtVALIDATORLIST,
456 buildValidatorListCollection(),
457 protocol::mtVALIDATORLISTCOLLECTION,
459 "TMValidatorListCollection");
465 testcase(
"Handshake");
466 auto getEnv = [&](
bool enable) {
469 str <<
"[reduce_relay]\n"
475 auto env = std::make_shared<jtx::Env>(*
this);
477 env->app().config().VP_REDUCE_RELAY_ENABLE =
479 env->app().config().VP_REDUCE_RELAY_SQUELCH =
483 auto handshake = [&](
int outboundEnable,
int inboundEnable) {
485 boost::asio::ip::address::from_string(
"172.1.1.100");
487 auto env = getEnv(outboundEnable);
490 env->app().config().COMPRESSION,
492 env->app().config().TX_REDUCE_RELAY_ENABLE,
493 env->app().config().VP_REDUCE_RELAY_ENABLE);
495 http_request.version(request.version());
496 http_request.base() = request.base();
499 auto const peerEnabled = inboundEnable && outboundEnable;
504 BEAST_EXPECT(!(peerEnabled ^ inboundEnabled));
507 env = getEnv(inboundEnable);
521 BEAST_EXPECT(!(peerEnabled ^ outboundEnabled));