1#include <test/jtx/Env.h>
2#include <test/jtx/noop.h>
4#include <xrpld/app/main/Application.h>
5#include <xrpld/core/Config.h>
6#include <xrpld/overlay/Message.h>
7#include <xrpld/overlay/Peer.h>
8#include <xrpld/overlay/detail/Handshake.h>
9#include <xrpld/overlay/detail/OverlayImpl.h>
10#include <xrpld/overlay/detail/PeerImp.h>
11#include <xrpld/overlay/detail/ProtocolVersion.h>
12#include <xrpld/peerfinder/Slot.h>
14#include <xrpl/basics/base_uint.h>
15#include <xrpl/basics/make_SSLContext.h>
16#include <xrpl/beast/net/IPEndpoint.h>
17#include <xrpl/beast/unit_test/suite.h>
18#include <xrpl/protocol/KeyType.h>
19#include <xrpl/protocol/PublicKey.h>
20#include <xrpl/protocol/SecretKey.h>
21#include <xrpl/protocol/Serializer.h>
22#include <xrpl/resource/Consumer.h>
23#include <xrpl/server/Handoff.h>
25#include <boost/asio/io_context.hpp>
26#include <boost/asio/ip/address.hpp>
27#include <boost/asio/ip/tcp.hpp>
28#include <boost/asio/ssl/context.hpp>
29#include <boost/beast/core/multi_buffer.hpp>
30#include <boost/beast/core/tcp_stream.hpp>
31#include <boost/beast/ssl/ssl_stream.hpp>
68 auto test = [&](
bool enable,
72 bool success =
true) {
74 str <<
"[reduce_relay]\n"
75 <<
"tx_enable=" <<
static_cast<int>(enable) <<
"\n"
76 <<
"tx_metrics=" <<
static_cast<int>(
metrics) <<
"\n"
77 <<
"tx_min_peers=" << min <<
"\n"
78 <<
"tx_relay_percentage=" << pct <<
"\n";
110 test(
true,
true, 20, 25);
111 test(
false,
false, 20, 25);
112 test(
false,
false, 20, 0,
false);
113 test(
false,
false, 20, 101,
false);
114 test(
false,
false, 9, 10,
false);
115 test(
false,
false, 10, 9,
false);
139 std::move(streamPtr),
187 auto& overlay =
dynamic_cast<OverlayImpl&
>(env.
app().getOverlay());
188 boost::beast::http::request<boost::beast::http::dynamic_body> request;
200 auto consumer = overlay.resourceManager().newInboundEndpoint(remote);
201 auto [slot, _] = overlay.peerFinder().newInboundSlot(local, remote);
209 std::move(streamPtr),
212 overlay.addActive(peer);
213 BEAST_EXPECT(overlay.findPeerByPublicKey(key) == peer);
214 peers.emplace_back(peer);
235 env.app().config().txReduceRelayEnable = txRREnabled;
236 env.app().config().txReduceRelayMinPeers = minPeers;
237 env.app().config().txRelayPercentage = relayPercentage;
241 for (
int i = 0; i < nPeers; i++)
242 addPeer(env, peers, nDisabled);
244 auto const jtx = env.jt(
noop(env.master));
245 if (BEAST_EXPECT(
jtx.stx))
247 protocol::TMTransaction m;
250 m.set_rawtransaction(s.
data(), s.
size());
251 m.set_deferred(
false);
252 m.set_status(protocol::TransactionStatus::tsNEW);
253 env.app().getOverlay().relay(
uint256{0}, m, toSkip);
261 bool const log =
false;
265 testRelay(
"feature disabled",
false, 10, 0, 10, 25, 10, 0);
267 testRelay(
"feature disabled & skip",
false, 10, 0, 10, 25, 5, 0, skip);
269 testRelay(
"relay all 1",
true, 10, 0, 20, 25, 10, 0);
271 testRelay(
"relay all 2",
true, 20, 15, 10, 25, 20, 0);
274 testRelay(
"relay & queue",
true, 60, 0, 20, 25, 30, 30);
278 testRelay(
"skip",
true, 60, 0, 20, 25, 25, 30, skip);
281 testRelay(
"disabled",
true, 70, 10, 20, 25, 40, 30);
285 testRelay(
"disabled & skip",
true, 70, 10, 20, 25, 35, 30, skip);
289 skip = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
290 testRelay(
"disabled & skip, no queue",
true, 15, 5, 10, 25, 5, 0, skip);
294 skip = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
295 testRelay(
"disabled & skip, no relay",
true, 20, 2, 10, 25, 0, 6, skip);
A version-independent IP address and port combination.
void pass()
Record a successful test condition.
void fail(String const &reason, char const *file, int line)
Record a failure.
LogOs< char > log
Logging output stream.
TestcaseT testcase
Memberspace for declaring test cases.
std::size_t txReduceRelayMinPeers
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
std::size_t txRelayPercentage
bool txReduceRelayMetrics
std::shared_ptr< PeerFinder::Slot > const & slot()
PeerImp(PeerImp const &)=delete
An endpoint that consumes resources.
std::size_t size() const noexcept
void const * data() const noexcept
virtual boost::asio::io_context & getIOContext()=0
A transaction testing environment.
void addTxQueue(uint256 const &hash) override
Add transaction's hash to the transactions' hashes queue.
void send(std::shared_ptr< Message > const &) override
static std::uint16_t sendTx
~PeerTest() override=default
static std::uint16_t queueTx
PeerTest(Application &app, std::shared_ptr< PeerFinder::Slot > const &slot, http_request_type &&request, PublicKey const &publicKey, ProtocolVersion protocol, Resource::Consumer consumer, std::unique_ptr< tx_reduce_relay_test::stream_type > &&streamPtr, OverlayImpl &overlay)
void testRelay(std::string const &test, bool txRREnabled, std::uint16_t nPeers, std::uint16_t nDisabled, std::uint16_t minPeers, std::uint16_t relayPercentage, std::uint16_t expectRelay, std::uint16_t expectQueue, std::set< Peer::id_t > const &toSkip={})
ProtocolVersion protocolVersion_
void addPeer(jtx::Env &env, std::vector< std::shared_ptr< PeerTest > > &peers, std::uint16_t &nDisabled)
void testConfig(bool log)
boost::asio::ip::tcp::socket socket_type
boost::beast::multi_buffer readBuf_
boost::beast::tcp_stream middle_type
std::shared_ptr< boost::asio::ssl::context > shared_context
void run() override
Runs the suite.
boost::beast::ssl_stream< middle_type > stream_type
void doTest(std::string const &msg, bool log, std::function< void(bool)> f)
json::Value noop(Account const &account)
The null transaction.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
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::shared_ptr< boost::asio::ssl::context > makeSslContext(std::string const &cipherList)
Create a self-signed SSL context that allows anonymous Diffie Hellman.
std::string makeFeaturesRequestHeader(bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.
std::pair< std::uint16_t, std::uint16_t > ProtocolVersion
Represents a particular version of the peer-to-peer protocol.
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type