20#include <test/jtx/Account.h>
21#include <test/jtx/Env.h>
22#include <test/jtx/WSClient.h>
23#include <test/jtx/amount.h>
24#include <test/jtx/pay.h>
26#include <xrpld/app/ledger/Ledger.h>
27#include <xrpld/app/ledger/LedgerMaster.h>
28#include <xrpld/overlay/Compression.h>
29#include <xrpld/overlay/Message.h>
30#include <xrpld/overlay/detail/Handshake.h>
31#include <xrpld/overlay/detail/ProtocolMessage.h>
32#include <xrpld/overlay/detail/ZeroCopyStream.h>
33#include <xrpld/shamap/SHAMapNodeID.h>
35#include <xrpl/basics/random.h>
36#include <xrpl/beast/unit_test.h>
37#include <xrpl/beast/utility/Journal.h>
38#include <xrpl/protocol/HashPrefix.h>
39#include <xrpl/protocol/PublicKey.h>
40#include <xrpl/protocol/SecretKey.h>
41#include <xrpl/protocol/Sign.h>
42#include <xrpl/protocol/digest.h>
43#include <xrpl/protocol/jss.h>
44#include <xrpl/protocol/messages.h>
46#include <boost/asio/ip/address_v4.hpp>
47#include <boost/beast/core/multi_buffer.hpp>
48#include <boost/endian/conversion.hpp>
89 protocol::MessageType mt,
93 testcase(
"Compress/Decompress: " + msg);
97 auto& buffer = m.
getBuffer(Compressed::On);
99 boost::beast::multi_buffer buffers;
102 auto sz = buffer.
size() / nbuffers;
103 for (
int i = 0; i < nbuffers; i++)
105 auto start = buffer.begin() + sz * i;
106 auto end = i < nbuffers - 1 ? (buffer.begin() + sz * (i + 1))
109 buffers.commit(boost::asio::buffer_copy(
110 buffers.prepare(slice.
size()), boost::asio::buffer(slice)));
113 boost::system::error_code ec;
115 ec, buffers.data(), buffer.size());
117 BEAST_EXPECT(header);
119 if (!header || header->algorithm == Algorithm::None)
123 decompressed.
resize(header->uncompressed_size);
126 header->payload_wire_size == buffer.size() - header->header_size);
129 stream.Skip(header->header_size);
133 header->payload_wire_size,
135 header->uncompressed_size);
136 BEAST_EXPECT(decompressedSize == header->uncompressed_size);
137 auto const proto1 = std::make_shared<T>();
140 proto1->ParseFromArray(decompressed.
data(), decompressedSize));
141 auto uncompressed = m.
getBuffer(Compressed::Off);
145 decompressed.
begin()));
151 auto manifests = std::make_shared<protocol::TMManifests>();
152 manifests->mutable_list()->Reserve(n);
153 for (
int i = 0; i < n; i++)
159 st[sfPublicKey] = std::get<0>(master);
160 st[sfSigningPubKey] = std::get<0>(signing);
174 std::get<1>(signing));
177 auto*
manifest = manifests->add_list();
186 auto endpoints = std::make_shared<protocol::TMEndpoints>();
187 endpoints->mutable_endpoints_v2()->Reserve(n);
188 for (
int i = 0; i < n; i++)
190 auto ep = endpoints->add_endpoints_v2();
194 endpoints->set_version(2);
204 auto const alice =
Account(
"alice");
205 auto const bob =
Account(
"bob");
214 reinterpret_cast<char const*
>(blob->data()), blob->
size()};
222 jrequestUsd[jss::tx_json] =
223 pay(
"bob",
"alice", bob[
"USD"](
fund / 2));
224 Json::Value jreply_usd = wsc->invoke(
"sign", jrequestUsd);
227 toBinary(jreply_usd[jss::result][jss::tx_blob].asString());
230 auto transaction = std::make_shared<protocol::TMTransaction>();
231 transaction->set_rawtransaction(usdTxBlob);
232 transaction->set_status(protocol::tsNEW);
233 transaction->set_receivetimestamp(rand_int<std::uint64_t>());
234 transaction->set_deferred(
true);
242 auto getLedger = std::make_shared<protocol::TMGetLedger>();
243 getLedger->set_itype(protocol::liTS_CANDIDATE);
244 getLedger->set_ltype(protocol::TMLedgerType::ltACCEPTED);
246 getLedger->set_ledgerhash(hash.
begin(), hash.
size());
247 getLedger->set_ledgerseq(123456789);
250 getLedger->set_requestcookie(123456789);
251 getLedger->set_querytype(protocol::qtINDIRECT);
252 getLedger->set_querydepth(3);
259 auto ledgerData = std::make_shared<protocol::TMLedgerData>();
261 ledgerData->set_ledgerhash(hash.
data(), hash.
size());
262 ledgerData->set_ledgerseq(123456789);
263 ledgerData->set_type(protocol::TMLedgerInfoType::liAS_NODE);
264 ledgerData->set_requestcookie(123456789);
265 ledgerData->set_error(protocol::TMReplyError::reNO_LEDGER);
266 ledgerData->mutable_nodes()->Reserve(n);
272 for (
int i = 0; i < n; i++)
288 ledgerData->add_nodes()->set_nodedata(
298 auto getObject = std::make_shared<protocol::TMGetObjectByHash>();
300 getObject->set_type(protocol::TMGetObjectByHash_ObjectType::
301 TMGetObjectByHash_ObjectType_otTRANSACTION);
302 getObject->set_query(
true);
303 getObject->set_seq(123456789);
305 getObject->set_ledgerhash(hash.
data(), hash.
size());
306 getObject->set_fat(
true);
307 for (
int i = 0; i < 100; i++)
310 auto object = getObject->add_objects();
311 object->set_hash(hash.
data(), hash.
size());
314 object->set_index(
"");
315 object->set_data(
"");
316 object->set_ledgerseq(i);
324 auto list = std::make_shared<protocol::TMValidatorList>();
330 st[sfPublicKey] = std::get<0>(master);
331 st[sfSigningPubKey] = std::get<0>(signing);
342 list->set_manifest(s.
data(), s.
size());
343 list->set_version(3);
349 list->set_signature(s1.
data(), s1.
size());
357 auto list = std::make_shared<protocol::TMValidatorListCollection>();
363 st[sfPublicKey] = std::get<0>(master);
364 st[sfSigningPubKey] = std::get<0>(signing);
375 list->set_manifest(s.
data(), s.
size());
376 list->set_version(4);
382 auto& blob = *list->add_blobs();
383 blob.set_signature(s1.
data(), s1.
size());
392 auto logs = std::make_unique<Logs>(thresh);
394 protocol::TMManifests manifests;
395 protocol::TMEndpoints endpoints;
396 protocol::TMTransaction transaction;
397 protocol::TMGetLedger get_ledger;
398 protocol::TMLedgerData ledger_data;
399 protocol::TMGetObjectByHash get_object;
400 protocol::TMValidatorList validator_list;
401 protocol::TMValidatorListCollection validator_list_collection;
414 protocol::mtTRANSACTION,
422 protocol::mtLEDGER_DATA,
428 protocol::mtLEDGER_DATA,
434 protocol::mtLEDGER_DATA,
436 "TMLedgerData10000");
440 protocol::mtLEDGER_DATA,
442 "TMLedgerData100000");
446 protocol::mtLEDGER_DATA,
448 "TMLedgerData500000");
452 protocol::mtGET_OBJECTS,
454 "TMGetObjectByHash");
458 protocol::mtVALIDATORLIST,
463 protocol::mtVALIDATORLISTCOLLECTION,
465 "TMValidatorListCollection");
472 auto getEnv = [&](
bool enable) {
475 str <<
"[reduce_relay]\n"
481 auto env = std::make_shared<jtx::Env>(*
this);
483 env->app().config().VP_REDUCE_RELAY_ENABLE =
485 env->app().config().VP_REDUCE_RELAY_SQUELCH =
489 auto handshake = [&](
int outboundEnable,
int inboundEnable) {
491 boost::asio::ip::address::from_string(
"172.1.1.100");
493 auto env = getEnv(outboundEnable);
496 env->app().config().COMPRESSION,
498 env->app().config().TX_REDUCE_RELAY_ENABLE,
499 env->app().config().VP_REDUCE_RELAY_ENABLE);
501 http_request.version(request.version());
502 http_request.base() = request.base();
505 auto const peerEnabled = inboundEnable && outboundEnable;
510 BEAST_EXPECT(!(peerEnabled ^ inboundEnabled));
513 env = getEnv(inboundEnable);
527 BEAST_EXPECT(!(peerEnabled ^ outboundEnabled));
543BEAST_DEFINE_TESTSUITE_MANUAL(compression, ripple_data,
ripple);
testcase_t testcase
Memberspace for declaring test cases.
virtual Config & config()=0
bool VP_REDUCE_RELAY_ENABLE
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
bool VP_REDUCE_RELAY_SQUELCH
Manages partitions for logging.
std::vector< uint8_t > const & getBuffer(Compressed tryCompressed)
Retrieve the packed message data.
Identifies a node inside a SHAMap.
std::string getRawString() const
void add(Serializer &s) const override
std::size_t size() const noexcept
void const * data() const noexcept
Slice slice() const noexcept
const void * getDataPtr() const
constexpr value_type drops() const
Returns the number of drops.
static constexpr std::size_t size()
std::shared_ptr< protocol::TMGetObjectByHash > buildGetObjectByHash()
void run() override
Runs the suite.
std::shared_ptr< protocol::TMManifests > buildManifests(int n)
std::shared_ptr< protocol::TMLedgerData > buildLedgerData(uint32_t n, Logs &logs)
std::shared_ptr< protocol::TMValidatorListCollection > buildValidatorListCollection()
void doTest(std::shared_ptr< T > proto, protocol::MessageType mt, uint16_t nbuffers, std::string msg)
std::shared_ptr< protocol::TMValidatorList > buildValidatorList()
std::shared_ptr< protocol::TMGetLedger > buildGetLedger()
std::shared_ptr< protocol::TMTransaction > buildTransaction(Logs &logs)
std::shared_ptr< protocol::TMEndpoints > buildEndpoints(int n)
Immutable cryptographic account descriptor.
A transaction testing environment.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
boost::asio::ip::address Address
std::size_t constexpr headerBytes
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.
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.
void fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
void sign(Json::Value &jv, Account const &account)
Sign automatically.
XRP_t const XRP
Converts to XRP Issue or STAmount.
static uint256 ledgerHash(LedgerInfo const &info)
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpc_version, std::unordered_map< std::string, std::string > const &headers)
Returns a client operating through WebSockets/S.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
static constexpr char FEATURE_COMPR[]
http_response_type makeResponse(bool crawlPublic, http_request_type const &req, beast::IP::Address public_ip, beast::IP::Address remote_ip, 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.
std::string strHex(FwdIt begin, FwdIt end)
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
bool peerFeatureEnabled(headers const &request, std::string const &feature, std::string value, bool config)
Check if a feature should be enabled for a peer.
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
@ ledgerMaster
ledger master data for signing
void addRaw(LedgerHeader const &, Serializer &, bool includeHash=false)
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
T time_since_epoch(T... args)