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/strHex.h>
21#include <xrpl/beast/net/IPAddress.h>
22#include <xrpl/beast/unit_test/suite.h>
23#include <xrpl/beast/utility/Journal.h>
24#include <xrpl/json/json_value.h>
25#include <xrpl/protocol/HashPrefix.h>
26#include <xrpl/protocol/KeyType.h>
27#include <xrpl/protocol/LedgerHeader.h>
28#include <xrpl/protocol/SField.h>
29#include <xrpl/protocol/STObject.h>
30#include <xrpl/protocol/SecretKey.h>
31#include <xrpl/protocol/Seed.h>
32#include <xrpl/protocol/Serializer.h>
33#include <xrpl/protocol/Sign.h>
34#include <xrpl/protocol/digest.h>
35#include <xrpl/protocol/jss.h>
36#include <xrpl/shamap/SHAMapNodeID.h>
38#include <boost/asio/buffer.hpp>
39#include <boost/asio/ip/address.hpp>
40#include <boost/beast/core/multi_buffer.hpp>
41#include <boost/system/detail/error_code.hpp>
54using namespace xrpl::test;
55using namespace xrpl::test::jtx;
85 testcase(
"Compress/Decompress: " + msg);
89 auto& buffer = m.
getBuffer(Compressed::On);
91 boost::beast::multi_buffer buffers;
94 auto const sz = buffer.
size() / nbuffers;
95 for (
int i = 0; i < nbuffers; i++)
97 auto const start = buffer.begin() + sz * i;
98 auto const end = i < nbuffers - 1 ? (buffer.begin() + sz * (i + 1)) : buffer.end();
101 boost::asio::buffer_copy(
102 buffers.prepare(slice.
size()), boost::asio::buffer(slice)));
105 boost::system::error_code ec;
108 BEAST_EXPECT(header);
110 if (!header || header->algorithm == Algorithm::None)
114 decompressed.
resize(header->uncompressedSize);
116 BEAST_EXPECT(header->payloadWireSize == buffer.size() - header->headerSize);
119 stream.Skip(header->headerSize);
122 stream, header->payloadWireSize, decompressed.
data(), header->uncompressedSize);
123 BEAST_EXPECT(decompressedSize == header->uncompressedSize);
126 BEAST_EXPECT(proto1->ParseFromArray(decompressed.
data(), decompressedSize));
127 auto uncompressed = m.
getBuffer(Compressed::Off);
132 decompressed.
begin()));
139 manifests->mutable_list()->Reserve(n);
140 for (
int i = 0; i < n; i++)
146 st[sfPublicKey] = std::get<0>(master);
147 st[sfSigningPubKey] = std::get<0>(signing);
155 auto* manifest = manifests->add_list();
156 manifest->set_stobject(s.
data(), s.
size());
165 endpoints->mutable_endpoints_v2()->Reserve(n);
166 for (
int i = 0; i < n; i++)
168 auto ep = endpoints->add_endpoints_v2();
172 endpoints->set_version(2);
181 int const fund = 10000;
182 auto const alice =
Account(
"alice");
183 auto const bob =
Account(
"bob");
188 auto const toBinary = [
this](
std::string const& text) {
191 return std::string{
reinterpret_cast<char const*
>(blob->data()), blob->size()};
199 requestUSD[jss::tx_json] =
pay(
"bob",
"alice", bob[
"USD"](
fund / 2));
200 json::Value replyUSD = wsc->invoke(
"sign", requestUSD);
202 usdTxBlob = toBinary(replyUSD[jss::result][jss::tx_blob].asString());
206 transaction->set_rawtransaction(usdTxBlob);
207 transaction->set_status(protocol::tsNEW);
208 transaction->set_deferred(
true);
217 getLedger->set_itype(protocol::liTS_CANDIDATE);
218 getLedger->set_ltype(protocol::TMLedgerType::ltACCEPTED);
220 getLedger->set_ledgerhash(hash.
begin(), hash.
size());
221 getLedger->set_ledgerseq(123456789);
224 getLedger->set_requestcookie(123456789);
225 getLedger->set_querytype(protocol::qtINDIRECT);
226 getLedger->set_querydepth(3);
235 ledgerData->set_ledgerhash(hash.
data(), hash.
size());
236 ledgerData->set_ledgerseq(123456789);
237 ledgerData->set_type(protocol::TMLedgerInfoType::liAS_NODE);
238 ledgerData->set_requestcookie(123456789);
239 ledgerData->set_error(protocol::TMReplyError::reNO_LEDGER);
240 ledgerData->mutable_nodes()->Reserve(n);
246 for (
int i = 0; i < n; i++)
274 protocol::TMGetObjectByHash_ObjectType::TMGetObjectByHash_ObjectType_otTRANSACTION);
275 getObject->set_query(
true);
277 getObject->set_ledgerhash(hash.
data(), hash.
size());
278 for (
int i = 0; i < 100; i++)
281 auto object = getObject->add_objects();
282 object->set_hash(hash.
data(), hash.
size());
283 object->set_data(
"");
284 object->set_ledgerseq(i);
298 st[sfPublicKey] = std::get<0>(master);
299 st[sfSigningPubKey] = std::get<0>(signing);
305 list->set_manifest(s.
data(), s.
size());
306 list->set_version(4);
307 STObject const signature(sfSignature);
311 auto& blob = *list->add_blobs();
312 blob.set_signature(s1.
data(), s1.
size());
323 protocol::TMManifests
const manifests;
324 protocol::TMEndpoints
const endpoints;
325 protocol::TMTransaction
const transaction;
326 protocol::TMGetLedger
const getLedger;
327 protocol::TMLedgerData
const ledgerData;
328 protocol::TMGetObjectByHash
const getObject;
329 protocol::TMValidatorListCollection
const validatorListCollection;
357 protocol::mtVALIDATOR_LIST_COLLECTION,
359 "TMValidatorListCollection");
366 auto getEnv = [&](
bool enable) {
369 str <<
"[reduce_relay]\n"
370 <<
"vp_base_squelch_enable=1\n"
379 auto const handshake = [&](
int outboundEnable,
int inboundEnable) {
382 auto env = getEnv(outboundEnable);
385 env->app().config().compression,
387 env->app().config().txReduceRelayEnable,
388 env->app().config().vpReduceRelayBaseSquelchEnable);
390 httpRequest.version(request.version());
391 httpRequest.base() = request.base();
394 auto const peerEnabled = inboundEnable && outboundEnable;
397 auto const inboundEnabled =
399 BEAST_EXPECT(!(peerEnabled ^ inboundEnabled));
402 env = getEnv(inboundEnable);
404 true, httpRequest, addr, addr,
UInt256{1}, 1, {1, 0}, env->app());
407 auto const outboundEnabled =
409 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)
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.
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
void sign(json::Value const &jv, Account const &account, json::Value &sigObject, HashPrefix prefix)
Sign automatically into a specific Json field of the jv object.
BEAST_DEFINE_TESTSUITE_MANUAL(AMMCalc, app, xrpl)
static UInt256 ledgerHash(LedgerHeader const &info)
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.
std::string strHex(FwdIt begin, FwdIt end)
HttpResponseType makeResponse(bool crawlPublic, HttpRequestType 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.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> RequestType
Make outbound http request.
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Slice makeSlice(std::array< T, N > const &a)
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
void addRaw(LedgerHeader const &, Serializer &, bool includeHash=false)
boost::beast::http::request< boost::beast::http::dynamic_body > HttpRequestType
@ 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.
static constexpr char kFeatureCompr[]
Sha512HalfHasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
T time_since_epoch(T... args)