1#include <test/jtx/Env.h>
2#include <test/jtx/noop.h>
3#include <test/overlay/CapturePeer.h>
5#include <xrpld/app/main/Application.h>
6#include <xrpld/core/Config.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>
12#include <xrpl/basics/base_uint.h>
13#include <xrpl/beast/unit_test/suite.h>
14#include <xrpl/protocol/KeyType.h>
15#include <xrpl/protocol/PublicKey.h>
16#include <xrpl/protocol/SecretKey.h>
17#include <xrpl/protocol/Serializer.h>
18#include <xrpl/server/Handoff.h>
48 auto test = [&](
bool enable,
52 bool success =
true) {
54 str <<
"[reduce_relay]\n"
55 <<
"tx_enable=" <<
static_cast<int>(enable) <<
"\n"
56 <<
"tx_metrics=" <<
static_cast<int>(
metrics) <<
"\n"
57 <<
"tx_min_peers=" << min <<
"\n"
58 <<
"tx_relay_percentage=" << pct <<
"\n";
90 test(
true,
true, 20, 25);
91 test(
false,
false, 20, 25);
92 test(
false,
false, 20, 0,
false);
93 test(
false,
false, 20, 101,
false);
94 test(
false,
false, 9, 10,
false);
95 test(
false,
false, 10, 9,
false);
146 auto& overlay =
dynamic_cast<OverlayImpl&
>(env.
app().getOverlay());
149 bool const disabled = nDisabled > 0;
159 BEAST_EXPECT(overlay.findPeerByPublicKey(key) == peer);
160 peers.emplace_back(peer);
196 for (
int i = 0; i < nPeers; i++)
197 addPeer(env, peers, nDisabled);
201 if (!BEAST_EXPECT(nSkip <= peers.
size()))
208 toSkip.
insert(peers[i]->id());
211 if (BEAST_EXPECT(
jtx.stx))
213 protocol::TMTransaction m;
216 m.set_rawtransaction(s.
data(), s.
size());
217 m.set_deferred(
false);
218 m.set_status(protocol::TransactionStatus::tsNEW);
223 for (
auto const& peer : peers)
225 sendTx += peer->sent().size();
226 queueTx += peer->queued();
228 BEAST_EXPECT(sendTx == expectRelay && queueTx == expectQueue);
261 for (
int i = 0; i < nPeers; i++)
262 addPeer(env, peers, nDisabled);
268 for (
auto const& peer : peers)
270 sendTx += peer->sent().size();
271 queueTx += peer->queued();
273 BEAST_EXPECT(sendTx == 0 && queueTx == expectQueue);
279 bool const log =
false;
282 testRelay(
"feature disabled",
false, 10, 0, 10, 25, 10, 0);
284 testRelay(
"feature disabled & skip",
false, 10, 0, 10, 25, 5, 0, 5);
286 testRelay(
"relay all 1",
true, 10, 0, 20, 25, 10, 0);
288 testRelay(
"relay all 2",
true, 20, 15, 10, 25, 20, 0);
291 testRelay(
"relay & queue",
true, 60, 0, 20, 25, 30, 30);
295 testRelay(
"skip",
true, 60, 0, 20, 25, 25, 30, 5);
298 testRelay(
"disabled",
true, 70, 10, 20, 25, 40, 30);
302 testRelay(
"disabled & skip",
true, 70, 10, 20, 25, 35, 30, 5);
306 testRelay(
"disabled & skip, no queue",
true, 15, 5, 10, 25, 5, 0, 10);
310 testRelay(
"disabled & skip, no relay",
true, 20, 2, 10, 25, 0, 6, 14);
312 testQueueOnly(
"queue only, feature disabled",
false, 10, 0, 0);
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.
virtual Config & config()=0
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
bool txReduceRelayEnable
////////////// END OF TEMPORARY CODE BLOCK /////////////////////
virtual std::set< Peer::ID > relay(protocol::TMProposeSet const &m, UInt256 const &uid, PublicKey const &validator)=0
Relay a proposal.
std::size_t size() const noexcept
void const * data() const noexcept
virtual Overlay & getOverlay()=0
CapturePeer(Application &app, Peer::ID id, std::shared_ptr< peer_finder::Slot > const &slot, HttpRequestType request, PublicKey const &publicKey, ProtocolVersion protocol, resource::Consumer consumer, std::unique_ptr< StreamType > streamPtr, OverlayImpl &overlay)
Takes PeerImp's two rvalue-reference parameters by value instead, so a derived double can inherit thi...
A transaction testing environment.
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Counts queued transaction hashes.
std::size_t queued() const
void addTxQueue(UInt256 const &) override
Add transaction's hash to the transactions' hashes queue.
CapturePeer(Application &app, Peer::ID id, std::shared_ptr< peer_finder::Slot > const &slot, HttpRequestType request, PublicKey const &publicKey, ProtocolVersion protocol, resource::Consumer consumer, std::unique_ptr< StreamType > streamPtr, OverlayImpl &overlay)
Takes PeerImp's two rvalue-reference parameters by value instead, so a derived double can inherit thi...
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::size_t nSkip=0)
Relay one transaction to nPeers peers and check the split.
void testConfig(bool log)
void run() override
Runs the suite.
void testQueueOnly(std::string const &test, bool txRREnabled, std::uint16_t nPeers, std::uint16_t nDisabled, std::uint16_t expectQueue)
Exercises the branch that only queues, reached when relay() is handed no transaction to send: a pseud...
void doTest(std::string const &msg, bool log, std::function< void(bool)> f)
void addPeer(jtx::Env &env, std::vector< std::shared_ptr< TxReducePeer > > &peers, std::uint16_t &nDisabled)
Build one peer and register it with the overlay.
json::Value noop(Account const &account)
The null transaction.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
std::shared_ptr< PeerType > makeCapturePeer(jtx::Env &env, std::optional< PublicKey > key=std::nullopt, HttpRequestType request={})
Build an active CapturePeer and register it with the overlay.
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.
boost::beast::http::request< boost::beast::http::dynamic_body > HttpRequestType
std::string makeFeaturesRequestHeader(bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.