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/envconfig.h>
6#include <test/jtx/pay.h>
8#include <xrpld/core/Config.h>
9#include <xrpld/overlay/Compression.h>
10#include <xrpld/overlay/Message.h>
11#include <xrpld/overlay/detail/Handshake.h>
12#include <xrpld/overlay/detail/ProtocolMessage.h>
13#include <xrpld/overlay/detail/ZeroCopyStream.h>
15#include <xrpl/basics/Log.h>
16#include <xrpl/basics/Slice.h>
17#include <xrpl/basics/StringUtilities.h>
18#include <xrpl/basics/base_uint.h>
19#include <xrpl/basics/chrono.h>
20#include <xrpl/basics/random.h>
21#include <xrpl/basics/strHex.h>
22#include <xrpl/beast/net/IPAddress.h>
23#include <xrpl/beast/unit_test/suite.h>
24#include <xrpl/beast/utility/Journal.h>
25#include <xrpl/json/json_value.h>
26#include <xrpl/protocol/HashPrefix.h>
27#include <xrpl/protocol/KeyType.h>
28#include <xrpl/protocol/LedgerHeader.h>
29#include <xrpl/protocol/SField.h>
30#include <xrpl/protocol/STObject.h>
31#include <xrpl/protocol/SecretKey.h>
32#include <xrpl/protocol/Seed.h>
33#include <xrpl/protocol/Serializer.h>
34#include <xrpl/protocol/Sign.h>
35#include <xrpl/protocol/digest.h>
36#include <xrpl/protocol/jss.h>
37#include <xrpl/shamap/SHAMapNodeID.h>
39#include <boost/asio/buffer.hpp>
40#include <boost/asio/ip/address.hpp>
41#include <boost/beast/core/multi_buffer.hpp>
42#include <boost/system/detail/error_code.hpp>
55using namespace xrpl::test;
56using namespace xrpl::test::jtx;
86 testcase(
"Compress/Decompress: " + msg);
90 auto& buffer = m.
getBuffer(Compressed::On);
92 boost::beast::multi_buffer buffers;
95 auto sz = buffer.
size() / nbuffers;
96 for (
int i = 0; i < nbuffers; i++)
98 auto start = buffer.begin() + sz * i;
99 auto end = i < nbuffers - 1 ? (buffer.begin() + sz * (i + 1)) : buffer.end();
102 boost::asio::buffer_copy(
103 buffers.prepare(slice.
size()), boost::asio::buffer(slice)));
106 boost::system::error_code ec;
109 BEAST_EXPECT(header);
111 if (!header || header->algorithm == Algorithm::None)
115 decompressed.
resize(header->uncompressedSize);
117 BEAST_EXPECT(header->payloadWireSize == buffer.size() - header->headerSize);
120 stream.Skip(header->headerSize);
123 stream, header->payloadWireSize, decompressed.
data(), header->uncompressedSize);
124 BEAST_EXPECT(decompressedSize == header->uncompressedSize);
127 BEAST_EXPECT(proto1->ParseFromArray(decompressed.
data(), decompressedSize));
128 auto uncompressed = m.
getBuffer(Compressed::Off);
133 decompressed.
begin()));
140 manifests->mutable_list()->Reserve(n);
141 for (
int i = 0; i < n; i++)
147 st[sfPublicKey] = std::get<0>(master);
148 st[sfSigningPubKey] = std::get<0>(signing);
156 auto* manifest = manifests->add_list();
157 manifest->set_stobject(s.
data(), s.
size());
166 endpoints->mutable_endpoints_v2()->Reserve(n);
167 for (
int i = 0; i < n; i++)
169 auto ep = endpoints->add_endpoints_v2();
173 endpoints->set_version(2);
182 int const fund = 10000;
183 auto const alice =
Account(
"alice");
184 auto const bob =
Account(
"bob");
192 return std::string{
reinterpret_cast<char const*
>(blob->data()), blob->size()};
200 requestUSD[jss::tx_json] =
pay(
"bob",
"alice", bob[
"USD"](
fund / 2));
201 json::Value replyUSD = wsc->invoke(
"sign", requestUSD);
203 usdTxBlob = toBinary(replyUSD[jss::result][jss::tx_blob].asString());
207 transaction->set_rawtransaction(usdTxBlob);
208 transaction->set_status(protocol::tsNEW);
210 transaction->set_deferred(
true);
219 getLedger->set_itype(protocol::liTS_CANDIDATE);
220 getLedger->set_ltype(protocol::TMLedgerType::ltACCEPTED);
222 getLedger->set_ledgerhash(hash.
begin(), hash.
size());
223 getLedger->set_ledgerseq(123456789);
226 getLedger->set_requestcookie(123456789);
227 getLedger->set_querytype(protocol::qtINDIRECT);
228 getLedger->set_querydepth(3);
237 ledgerData->set_ledgerhash(hash.
data(), hash.
size());
238 ledgerData->set_ledgerseq(123456789);
239 ledgerData->set_type(protocol::TMLedgerInfoType::liAS_NODE);
240 ledgerData->set_requestcookie(123456789);
241 ledgerData->set_error(protocol::TMReplyError::reNO_LEDGER);
242 ledgerData->mutable_nodes()->Reserve(n);
248 for (
int i = 0; i < n; i++)
276 protocol::TMGetObjectByHash_ObjectType::TMGetObjectByHash_ObjectType_otTRANSACTION);
277 getObject->set_query(
true);
279 getObject->set_ledgerhash(hash.
data(), hash.
size());
280 getObject->set_fat(
true);
281 for (
int i = 0; i < 100; i++)
284 auto object = getObject->add_objects();
285 object->set_hash(hash.
data(), hash.
size());
288 object->set_index(
"");
289 object->set_data(
"");
290 object->set_ledgerseq(i);
304 st[sfPublicKey] = std::get<0>(master);
305 st[sfSigningPubKey] = std::get<0>(signing);
311 list->set_manifest(s.
data(), s.
size());
312 list->set_version(3);
313 STObject const signature(sfSignature);
317 list->set_signature(s1.
data(), s1.
size());
331 st[sfPublicKey] = std::get<0>(master);
332 st[sfSigningPubKey] = std::get<0>(signing);
338 list->set_manifest(s.
data(), s.
size());
339 list->set_version(4);
340 STObject const signature(sfSignature);
344 auto& blob = *list->add_blobs();
345 blob.set_signature(s1.
data(), s1.
size());
356 protocol::TMManifests
const manifests;
357 protocol::TMEndpoints
const endpoints;
358 protocol::TMTransaction
const transaction;
359 protocol::TMGetLedger
const getLedger;
360 protocol::TMLedgerData
const ledgerData;
361 protocol::TMGetObjectByHash
const getObject;
362 protocol::TMValidatorList
const validatorList;
363 protocol::TMValidatorListCollection
const validatorListCollection;
393 protocol::mtVALIDATOR_LIST_COLLECTION,
395 "TMValidatorListCollection");
402 auto getEnv = [&](
bool enable) {
405 str <<
"[reduce_relay]\n"
406 <<
"vp_base_squelch_enable=1\n"
415 auto handshake = [&](
int outboundEnable,
int inboundEnable) {
418 auto env = getEnv(outboundEnable);
421 env->app().config().compression,
423 env->app().config().txReduceRelayEnable,
424 env->app().config().vpReduceRelayBaseSquelchEnable);
426 httpRequest.version(request.version());
427 httpRequest.base() = request.base();
430 auto const peerEnabled = inboundEnable && outboundEnable;
433 auto const inboundEnabled =
435 BEAST_EXPECT(!(peerEnabled ^ inboundEnabled));
438 env = getEnv(inboundEnable);
440 true, httpRequest, addr, addr,
uint256{1}, 1, {1, 0}, env->app());
443 auto const outboundEnabled =
445 BEAST_EXPECT(!(peerEnabled ^ outboundEnabled));
TestcaseT testcase
Memberspace for declaring test cases.
virtual Config & config()=0
static constexpr std::size_t size()
bool vpReduceRelayBaseSquelchEnable
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
Manages partitions for logging.
std::vector< uint8_t > const & getBuffer(Compressed tryCompressed)
Retrieve the packed message data.
std::chrono::time_point< NetClock > time_point
std::chrono::duration< rep, period > duration
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 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()
compression::Compressed Compressed
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()
compression::Algorithm Algorithm
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)
compression_test()=default
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
constexpr std::size_t kHeaderBytes
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.
std::vector< STAmount > fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
XrpT const XRP
Converts to XRP Issue or STAmount.
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)
BEAST_DEFINE_TESTSUITE_MANUAL(AMMCalc, app, xrpl)
constexpr XRPAmount
Convert XRP to drops (integral types).
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpcVersion, 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.
std::string strHex(FwdIt begin, FwdIt end)
std::enable_if_t< std::is_integral_v< Integral > &&detail::is_engine< Engine >::value, Integral > randInt(Engine &engine, Integral min, Integral max)
Return a uniformly distributed random integer.
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)
http_response_type makeResponse(bool crawlPublic, http_request_type const &req, beast::IP::Address publicIp, beast::IP::Address remoteIp, 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.
bool peerFeatureEnabled(Headers const &request, std::string const &feature, std::string value, bool config)
Check if a feature should be enabled for a peer.
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
static constexpr char kFeatureCompr[]
std::enable_if_t< std::is_same_v< T, char >||std::is_same_v< T, unsigned char >, Slice > makeSlice(std::array< T, N > const &a)
T time_since_epoch(T... args)