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>
25#include <xrpld/app/ledger/Ledger.h>
26#include <xrpld/app/ledger/LedgerMaster.h>
27#include <xrpld/overlay/Compression.h>
28#include <xrpld/overlay/Message.h>
29#include <xrpld/overlay/detail/Handshake.h>
30#include <xrpld/overlay/detail/ProtocolMessage.h>
31#include <xrpld/overlay/detail/ZeroCopyStream.h>
32#include <xrpld/shamap/SHAMapNodeID.h>
33#include <xrpl/basics/random.h>
34#include <xrpl/beast/unit_test.h>
35#include <xrpl/beast/utility/Journal.h>
36#include <xrpl/protocol/HashPrefix.h>
37#include <xrpl/protocol/PublicKey.h>
38#include <xrpl/protocol/SecretKey.h>
39#include <xrpl/protocol/Sign.h>
40#include <xrpl/protocol/digest.h>
41#include <xrpl/protocol/jss.h>
42#include <xrpl/protocol/messages.h>
44#include <boost/asio/ip/address_v4.hpp>
45#include <boost/beast/core/multi_buffer.hpp>
46#include <boost/endian/conversion.hpp>
87 protocol::MessageType mt,
91 testcase(
"Compress/Decompress: " + msg);
95 auto& buffer = m.
getBuffer(Compressed::On);
97 boost::beast::multi_buffer buffers;
100 auto sz = buffer.
size() / nbuffers;
101 for (
int i = 0; i < nbuffers; i++)
103 auto start = buffer.begin() + sz * i;
104 auto end = i < nbuffers - 1 ? (buffer.begin() + sz * (i + 1))
107 buffers.commit(boost::asio::buffer_copy(
108 buffers.prepare(slice.
size()), boost::asio::buffer(slice)));
111 boost::system::error_code ec;
113 ec, buffers.data(), buffer.size());
115 BEAST_EXPECT(header);
117 if (!header || header->algorithm == Algorithm::None)
121 decompressed.
resize(header->uncompressed_size);
124 header->payload_wire_size == buffer.size() - header->header_size);
127 stream.Skip(header->header_size);
131 header->payload_wire_size,
133 header->uncompressed_size);
134 BEAST_EXPECT(decompressedSize == header->uncompressed_size);
135 auto const proto1 = std::make_shared<T>();
138 proto1->ParseFromArray(decompressed.
data(), decompressedSize));
139 auto uncompressed = m.
getBuffer(Compressed::Off);
143 decompressed.
begin()));
149 auto manifests = std::make_shared<protocol::TMManifests>();
150 manifests->mutable_list()->Reserve(n);
151 for (
int i = 0; i < n; i++)
157 st[sfPublicKey] = std::get<0>(master);
158 st[sfSigningPubKey] = std::get<0>(signing);
172 std::get<1>(signing));
175 auto*
manifest = manifests->add_list();
184 auto endpoints = std::make_shared<protocol::TMEndpoints>();
185 endpoints->mutable_endpoints_v2()->Reserve(n);
186 for (
int i = 0; i < n; i++)
188 auto ep = endpoints->add_endpoints_v2();
192 endpoints->set_version(2);
202 auto const alice =
Account(
"alice");
203 auto const bob =
Account(
"bob");
212 reinterpret_cast<char const*
>(blob->data()), blob->
size()};
220 jrequestUsd[jss::tx_json] =
221 pay(
"bob",
"alice", bob[
"USD"](
fund / 2));
222 Json::Value jreply_usd = wsc->invoke(
"sign", jrequestUsd);
225 toBinary(jreply_usd[jss::result][jss::tx_blob].asString());
228 auto transaction = std::make_shared<protocol::TMTransaction>();
229 transaction->set_rawtransaction(usdTxBlob);
230 transaction->set_status(protocol::tsNEW);
231 transaction->set_receivetimestamp(rand_int<std::uint64_t>());
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);
270 for (
int i = 0; i < n; i++)
286 ledgerData->add_nodes()->set_nodedata(
296 auto getObject = std::make_shared<protocol::TMGetObjectByHash>();
298 getObject->set_type(protocol::TMGetObjectByHash_ObjectType::
299 TMGetObjectByHash_ObjectType_otTRANSACTION);
300 getObject->set_query(
true);
301 getObject->set_seq(123456789);
303 getObject->set_ledgerhash(hash.
data(), hash.
size());
304 getObject->set_fat(
true);
305 for (
int i = 0; i < 100; i++)
308 auto object = getObject->add_objects();
309 object->set_hash(hash.
data(), hash.
size());
312 object->set_index(
"");
313 object->set_data(
"");
314 object->set_ledgerseq(i);
322 auto list = std::make_shared<protocol::TMValidatorList>();
328 st[sfPublicKey] = std::get<0>(master);
329 st[sfSigningPubKey] = std::get<0>(signing);
340 list->set_manifest(s.
data(), s.
size());
341 list->set_version(3);
347 list->set_signature(s1.
data(), s1.
size());
355 auto list = std::make_shared<protocol::TMValidatorListCollection>();
361 st[sfPublicKey] = std::get<0>(master);
362 st[sfSigningPubKey] = std::get<0>(signing);
373 list->set_manifest(s.
data(), s.
size());
374 list->set_version(4);
380 auto& blob = *list->add_blobs();
381 blob.set_signature(s1.
data(), s1.
size());
390 auto logs = std::make_unique<Logs>(thresh);
392 protocol::TMManifests manifests;
393 protocol::TMEndpoints endpoints;
394 protocol::TMTransaction transaction;
395 protocol::TMGetLedger get_ledger;
396 protocol::TMLedgerData ledger_data;
397 protocol::TMGetObjectByHash get_object;
398 protocol::TMValidatorList validator_list;
399 protocol::TMValidatorListCollection validator_list_collection;
412 protocol::mtTRANSACTION,
420 protocol::mtLEDGER_DATA,
426 protocol::mtLEDGER_DATA,
432 protocol::mtLEDGER_DATA,
434 "TMLedgerData10000");
438 protocol::mtLEDGER_DATA,
440 "TMLedgerData100000");
444 protocol::mtLEDGER_DATA,
446 "TMLedgerData500000");
450 protocol::mtGET_OBJECTS,
452 "TMGetObjectByHash");
456 protocol::mtVALIDATORLIST,
461 protocol::mtVALIDATORLISTCOLLECTION,
463 "TMValidatorListCollection");
470 auto getEnv = [&](
bool enable) {
473 str <<
"[reduce_relay]\n"
479 auto env = std::make_shared<jtx::Env>(*
this);
481 env->app().config().VP_REDUCE_RELAY_ENABLE =
483 env->app().config().VP_REDUCE_RELAY_SQUELCH =
487 auto handshake = [&](
int outboundEnable,
int inboundEnable) {
489 boost::asio::ip::address::from_string(
"172.1.1.100");
491 auto env = getEnv(outboundEnable);
494 env->app().config().COMPRESSION,
496 env->app().config().TX_REDUCE_RELAY_ENABLE,
497 env->app().config().VP_REDUCE_RELAY_ENABLE);
499 http_request.version(request.version());
500 http_request.base() = request.base();
503 auto const peerEnabled = inboundEnable && outboundEnable;
508 BEAST_EXPECT(!(peerEnabled ^ inboundEnabled));
511 env = getEnv(inboundEnable);
525 BEAST_EXPECT(!(peerEnabled ^ outboundEnabled));
541BEAST_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)