1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/WSClient.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/pay.h>
7#include <xrpld/app/ledger/LedgerMaster.h>
8#include <xrpld/overlay/Compression.h>
9#include <xrpld/overlay/Message.h>
10#include <xrpld/overlay/detail/Handshake.h>
11#include <xrpld/overlay/detail/ProtocolMessage.h>
12#include <xrpld/overlay/detail/ZeroCopyStream.h>
14#include <xrpl/basics/random.h>
15#include <xrpl/beast/unit_test.h>
16#include <xrpl/beast/utility/Journal.h>
17#include <xrpl/ledger/Ledger.h>
18#include <xrpl/protocol/HashPrefix.h>
19#include <xrpl/protocol/PublicKey.h>
20#include <xrpl/protocol/SecretKey.h>
21#include <xrpl/protocol/Sign.h>
22#include <xrpl/protocol/digest.h>
23#include <xrpl/protocol/jss.h>
24#include <xrpl/protocol/messages.h>
25#include <xrpl/shamap/SHAMapNodeID.h>
27#include <boost/asio/ip/address_v4.hpp>
28#include <boost/beast/core/multi_buffer.hpp>
29#include <boost/endian/conversion.hpp>
70 testcase(
"Compress/Decompress: " + msg);
74 auto& buffer = m.
getBuffer(Compressed::On);
76 boost::beast::multi_buffer buffers;
79 auto sz = buffer.
size() / nbuffers;
80 for (
int i = 0; i < nbuffers; i++)
82 auto start = buffer.begin() + sz * i;
83 auto end = i < nbuffers - 1 ? (buffer.begin() + sz * (i + 1)) : buffer.end();
86 boost::asio::buffer_copy(
87 buffers.prepare(slice.
size()), boost::asio::buffer(slice)));
90 boost::system::error_code ec;
95 if (!header || header->algorithm == Algorithm::None)
99 decompressed.
resize(header->uncompressed_size);
101 BEAST_EXPECT(header->payload_wire_size == buffer.size() - header->header_size);
104 stream.Skip(header->header_size);
107 stream, header->payload_wire_size, decompressed.
data(), header->uncompressed_size);
108 BEAST_EXPECT(decompressedSize == header->uncompressed_size);
111 BEAST_EXPECT(proto1->ParseFromArray(decompressed.
data(), decompressedSize));
112 auto uncompressed = m.
getBuffer(Compressed::Off);
117 decompressed.
begin()));
124 manifests->mutable_list()->Reserve(n);
125 for (
int i = 0; i < n; i++)
140 auto*
manifest = manifests->add_list();
150 endpoints->mutable_endpoints_v2()->Reserve(n);
151 for (
int i = 0; i < n; i++)
153 auto ep = endpoints->add_endpoints_v2();
157 endpoints->set_version(2);
166 int const fund = 10000;
167 auto const alice =
Account(
"alice");
168 auto const bob =
Account(
"bob");
176 return std::string{
reinterpret_cast<char const*
>(blob->data()), blob->
size()};
184 requestUSD[jss::tx_json] =
pay(
"bob",
"alice", bob[
"USD"](
fund / 2));
185 Json::Value replyUSD = wsc->invoke(
"sign", requestUSD);
187 usdTxBlob = toBinary(replyUSD[jss::result][jss::tx_blob].asString());
191 transaction->set_rawtransaction(usdTxBlob);
192 transaction->set_status(protocol::tsNEW);
193 transaction->set_receivetimestamp(rand_int<std::uint64_t>());
194 transaction->set_deferred(
true);
203 getLedger->set_itype(protocol::liTS_CANDIDATE);
204 getLedger->set_ltype(protocol::TMLedgerType::ltACCEPTED);
206 getLedger->set_ledgerhash(hash.
begin(), hash.
size());
207 getLedger->set_ledgerseq(123456789);
210 getLedger->set_requestcookie(123456789);
211 getLedger->set_querytype(protocol::qtINDIRECT);
212 getLedger->set_querydepth(3);
221 ledgerData->set_ledgerhash(hash.
data(), hash.
size());
222 ledgerData->set_ledgerseq(123456789);
223 ledgerData->set_type(protocol::TMLedgerInfoType::liAS_NODE);
224 ledgerData->set_requestcookie(123456789);
225 ledgerData->set_error(protocol::TMReplyError::reNO_LEDGER);
226 ledgerData->mutable_nodes()->Reserve(n);
232 for (
int i = 0; i < n; i++)
260 protocol::TMGetObjectByHash_ObjectType::TMGetObjectByHash_ObjectType_otTRANSACTION);
261 getObject->set_query(
true);
262 getObject->set_seq(123456789);
264 getObject->set_ledgerhash(hash.
data(), hash.
size());
265 getObject->set_fat(
true);
266 for (
int i = 0; i < 100; i++)
269 auto object = getObject->add_objects();
270 object->set_hash(hash.
data(), hash.
size());
273 object->set_index(
"");
274 object->set_data(
"");
275 object->set_ledgerseq(i);
296 list->set_manifest(s.
data(), s.
size());
297 list->set_version(3);
298 STObject const signature(sfSignature);
302 list->set_signature(s1.
data(), s1.
size());
323 list->set_manifest(s.
data(), s.
size());
324 list->set_version(4);
325 STObject const signature(sfSignature);
329 auto& blob = *list->add_blobs();
330 blob.set_signature(s1.
data(), s1.
size());
341 protocol::TMManifests
const manifests;
342 protocol::TMEndpoints
const endpoints;
343 protocol::TMTransaction
const transaction;
344 protocol::TMGetLedger
const get_ledger;
345 protocol::TMLedgerData
const ledger_data;
346 protocol::TMGetObjectByHash
const get_object;
347 protocol::TMValidatorList
const validator_list;
348 protocol::TMValidatorListCollection
const validator_list_collection;
378 protocol::mtVALIDATOR_LIST_COLLECTION,
380 "TMValidatorListCollection");
387 auto getEnv = [&](
bool enable) {
390 str <<
"[reduce_relay]\n"
391 <<
"vp_base_squelch_enable=1\n"
397 env->app().config().VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE =
401 auto handshake = [&](
int outboundEnable,
int inboundEnable) {
404 auto env = getEnv(outboundEnable);
407 env->app().config().COMPRESSION,
409 env->app().config().TX_REDUCE_RELAY_ENABLE,
410 env->app().config().VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE);
412 http_request.version(request.version());
413 http_request.base() = request.base();
416 auto const peerEnabled = inboundEnable && outboundEnable;
419 auto const inboundEnabled =
421 BEAST_EXPECT(!(peerEnabled ^ inboundEnabled));
424 env = getEnv(inboundEnable);
426 true, http_request, addr, addr,
uint256{1}, 1, {1, 0}, env->app());
429 auto const outboundEnabled =
431 BEAST_EXPECT(!(peerEnabled ^ outboundEnabled));
447BEAST_DEFINE_TESTSUITE_MANUAL(compression, overlay,
xrpl);
testcase_t testcase
Memberspace for declaring test cases.
virtual Config & config()=0
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
bool VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE
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
void const * getDataPtr() const
Slice slice() const noexcept
std::size_t size() const noexcept
void const * data() const noexcept
constexpr value_type drops() const
Returns the number of drops.
static constexpr std::size_t size()
static std::shared_ptr< protocol::TMEndpoints > buildEndpoints(int n)
void doTest(std::shared_ptr< T > proto, protocol::MessageType mt, uint16_t nbuffers, std::string msg)
static std::shared_ptr< protocol::TMValidatorList > buildValidatorList()
static std::shared_ptr< protocol::TMLedgerData > buildLedgerData(uint32_t n, Logs &logs)
std::shared_ptr< protocol::TMTransaction > buildTransaction(Logs &logs)
static std::shared_ptr< protocol::TMValidatorListCollection > buildValidatorListCollection()
static std::shared_ptr< protocol::TMGetLedger > buildGetLedger()
void run() override
Runs the suite.
static std::shared_ptr< protocol::TMGetObjectByHash > buildGetObjectByHash()
static std::shared_ptr< protocol::TMManifests > buildManifests(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 fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
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.
void fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
XRP_t const XRP
Converts to XRP Issue or STAmount.
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
void sign(Json::Value &jv, Account const &account, Json::Value &sigObject)
Sign automatically into a specific Json field of the jv object.
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
static uint256 ledgerHash(LedgerHeader 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::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
static constexpr char FEATURE_COMPR[]
std::string strHex(FwdIt begin, FwdIt end)
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
void addRaw(LedgerHeader const &, Serializer &, bool includeHash=false)
bool peerFeatureEnabled(headers const &request, std::string const &feature, std::string value, bool config)
Check if a feature should be enabled for a peer.
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.
@ ledgerMaster
ledger master data for signing
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
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)
T time_since_epoch(T... args)