xrpld
Loading...
Searching...
No Matches
tx_reduce_relay_test.cpp
1#include <test/jtx/Env.h>
2#include <test/jtx/noop.h>
3#include <test/overlay/CapturePeer.h>
4
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>
11
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/SField.h>
17#include <xrpl/protocol/STTx.h>
18#include <xrpl/protocol/SecretKey.h>
19#include <xrpl/protocol/Serializer.h>
20#include <xrpl/protocol/TxFormats.h>
21#include <xrpl/server/Handoff.h>
22
23#include <xrpl.pb.h>
24
25#include <cstddef>
26#include <cstdint>
27#include <functional>
28#include <memory>
29#include <optional>
30#include <set>
31#include <sstream>
32#include <string>
33#include <utility>
34#include <vector>
35
36namespace xrpl::test {
37
39{
40 void
41 doTest(std::string const& msg, bool log, std::function<void(bool)> f)
42 {
43 testcase(msg);
44 f(log);
45 }
46
47 void
49 {
50 doTest("Config Test", log, [&](bool log) {
51 auto const test = [&](bool enable,
52 bool metrics,
53 std::uint16_t min,
54 std::uint16_t pct,
55 bool success = true) {
56 std::stringstream str("[reduce_relay]");
57 str << "[reduce_relay]\n"
58 << "tx_enable=" << static_cast<int>(enable) << "\n"
59 << "tx_metrics=" << static_cast<int>(metrics) << "\n"
60 << "tx_min_peers=" << min << "\n"
61 << "tx_relay_percentage=" << pct << "\n";
62 Config c;
63 try
64 {
65 c.loadFromString(str.str());
66
67 BEAST_EXPECT(c.txReduceRelayEnable == enable);
68 BEAST_EXPECT(c.txReduceRelayMetrics == metrics);
69 BEAST_EXPECT(c.txReduceRelayMinPeers == min);
70 BEAST_EXPECT(c.txRelayPercentage == pct);
71 if (success)
72 {
73 pass();
74 }
75 else
76 {
77 fail();
78 }
79 }
80 catch (...)
81 {
82 if (success)
83 {
84 fail();
85 }
86 else
87 {
88 pass();
89 }
90 }
91 };
92
93 test(true, true, 20, 25);
94 test(false, false, 20, 25);
95 test(false, false, 20, 0, false);
96 test(false, false, 20, 101, false);
97 test(false, false, 9, 10, false);
98 test(false, false, 10, 9, false);
99 });
100 }
101
107 {
108 public:
110
111 void
112 addTxQueue(UInt256 const&) override
113 {
114 ++queued_;
115 }
116
121 queued() const
122 {
123 return queued_;
124 }
125
126 private:
128 };
129
143 void
145 jtx::Env& env,
147 std::uint16_t& nDisabled)
148 {
149 auto& overlay = dynamic_cast<OverlayImpl&>(env.app().getOverlay());
150 PublicKey const key(std::get<0>(randomKeyPair(KeyType::Ed25519)));
151
152 bool const disabled = nDisabled > 0;
153 if (disabled)
154 --nDisabled;
155
156 HttpRequestType request;
157 if (!disabled)
158 request.insert("X-Protocol-Ctl", makeFeaturesRequestHeader(false, false, true, false));
159
160 BEAST_EXPECT(overlay.findPeerByPublicKey(key) == std::shared_ptr<PeerImp>{});
161 auto const peer = makeCapturePeer<TxReducePeer>(env, key, std::move(request));
162 BEAST_EXPECT(overlay.findPeerByPublicKey(key) == peer);
163 peers.emplace_back(peer);
164 }
165
179 void
181 std::string const& test,
182 bool txRREnabled,
183 std::uint16_t nPeers,
184 std::uint16_t nDisabled,
185 std::uint16_t minPeers,
186 std::uint16_t relayPercentage,
187 std::uint16_t expectRelay,
188 std::uint16_t expectQueue,
189 std::size_t nSkip = 0)
190 {
191 testcase(test);
192 jtx::Env env(*this);
194 // `PeerImp` decides `txReduceRelayEnabled()` in its constructor, from
195 // the config and the handshake header, so set these first.
196 env.app().config().txReduceRelayEnable = txRREnabled;
197 env.app().config().txReduceRelayMinPeers = minPeers;
198 env.app().config().txRelayPercentage = relayPercentage;
199 for (int i = 0; i < nPeers; i++)
200 addPeer(env, peers, nDisabled);
201
202 // An under-filled skip set would also fail the relay counts below, for
203 // a reason that looks unrelated.
204 if (!BEAST_EXPECT(nSkip <= peers.size()))
205 return;
206
207 // Skip the peers built first, so the skip set overlaps the disabled
208 // peers as the expected counts assume.
209 std::set<Peer::ID> toSkip;
210 for (std::size_t i = 0; i < nSkip; ++i)
211 toSkip.insert(peers[i]->id());
212
213 auto const jtx = env.jt(noop(env.master));
214 if (BEAST_EXPECT(jtx.stx))
215 {
216 protocol::TMTransaction m;
217 Serializer s;
218 jtx.stx->add(s);
219 m.set_rawtransaction(s.data(), s.size());
220 m.set_deferred(false);
221 m.set_status(protocol::TransactionStatus::tsNEW);
222 env.app().getOverlay().relay(UInt256{0}, m, toSkip);
223
224 std::size_t sendTx = 0;
225 std::size_t queueTx = 0;
226 for (auto const& peer : peers)
227 {
228 sendTx += peer->sent().size();
229 queueTx += peer->queued();
230 }
231 BEAST_EXPECT(sendTx == expectRelay && queueTx == expectQueue);
232 }
233 }
234
250 void
252 std::string const& test,
253 bool txRREnabled,
254 std::uint16_t nPeers,
255 std::uint16_t nDisabled,
256 std::uint16_t expectQueue)
257 {
258 testcase(test);
259 jtx::Env env(*this);
261 // `PeerImp` decides `txReduceRelayEnabled()` in its constructor, from
262 // the config and the handshake header, so set this first.
263 env.app().config().txReduceRelayEnable = txRREnabled;
264 for (int i = 0; i < nPeers; i++)
265 addPeer(env, peers, nDisabled);
266
267 env.app().getOverlay().relay(uint256{0}, std::nullopt, {});
268
269 std::size_t sendTx = 0;
270 std::size_t queueTx = 0;
271 for (auto const& peer : peers)
272 {
273 sendTx += peer->sent().size();
274 queueTx += peer->queued();
275 }
276 BEAST_EXPECT(sendTx == 0 && queueTx == expectQueue);
277 }
278
290 void
291 testParsed(std::string const& test, bool pseudo, bool parsed)
292 {
293 testcase(test);
294 jtx::Env env(*this);
296 env.app().config().txReduceRelayEnable = true;
297 env.app().config().txReduceRelayMinPeers = 20;
298 env.app().config().txRelayPercentage = 25;
299 constexpr std::uint16_t kPeers = 10;
300 std::uint16_t nDisabled = 0;
301 for (int i = 0; i < kPeers; i++)
302 addPeer(env, peers, nDisabled);
303
304 auto const stx = pseudo ? std::make_shared<STTx const>(
305 ttAMENDMENT,
306 [](auto& obj) {
307 obj.setAccountID(sfAccount, AccountID());
308 obj.setFieldH256(sfAmendment, UInt256{1});
309 obj.setFieldU32(sfLedgerSequence, 1);
310 })
311 : env.jt(noop(env.master)).stx;
312 if (!BEAST_EXPECT(stx))
313 return;
314
315 protocol::TMTransaction m;
316 Serializer s;
317 stx->add(s);
318 m.set_rawtransaction(s.data(), s.size());
319 m.set_deferred(false);
320 m.set_status(protocol::TransactionStatus::tsNEW);
321 if (parsed)
322 {
323 env.app().getOverlay().relay(*stx, m, {});
324 }
325 else
326 {
327 env.app().getOverlay().relay(UInt256{0}, m, {});
328 }
329
330 std::size_t sendTx = 0;
331 std::size_t queueTx = 0;
332 for (auto const& peer : peers)
333 {
334 sendTx += peer->sent().size();
335 queueTx += peer->queued();
336 }
337 if (pseudo)
338 {
339 BEAST_EXPECT(sendTx == 0 && queueTx == kPeers);
340 }
341 else
342 {
343 BEAST_EXPECT(sendTx == kPeers && queueTx == 0);
344 }
345 }
346
347 void
348 run() override
349 {
350 bool const log = false;
352 // relay to all peers, no hash queue
353 testRelay("feature disabled", false, 10, 0, 10, 25, 10, 0);
354 // relay to nPeers - skip (10-5=5)
355 testRelay("feature disabled & skip", false, 10, 0, 10, 25, 5, 0, 5);
356 // relay to all peers because min is greater than nPeers
357 testRelay("relay all 1", true, 10, 0, 20, 25, 10, 0);
358 // relay to all peers because min + disabled is greater thant nPeers
359 testRelay("relay all 2", true, 20, 15, 10, 25, 20, 0);
360 // relay to minPeers + 25% of nPeers-minPeers (20+0.25*(60-20)=30),
361 // queue the rest (30)
362 testRelay("relay & queue", true, 60, 0, 20, 25, 30, 30);
363 // relay to minPeers + 25% of (nPeers - nPeers) - skip
364 // (20+0.25*(60-20)-5=25), queue the rest, skip counts towards relayed
365 // (60-25-5=30)
366 testRelay("skip", true, 60, 0, 20, 25, 25, 30, 5);
367 // relay to minPeers + disabled + 25% of (nPeers - minPeers - disabled)
368 // (20+10+0.25*(70-20-10)=40), queue the rest (30)
369 testRelay("disabled", true, 70, 10, 20, 25, 40, 30);
370 // relay to minPeers + disabled-not-in-skip + 25% of (nPeers - minPeers
371 // - disabled) (20+5+0.25*(70-20-10)=35), queue the rest, skip counts
372 // towards relayed (70-35-5=30))
373 testRelay("disabled & skip", true, 70, 10, 20, 25, 35, 30, 5);
374 // relay to minPeers + disabled + 25% of (nPeers - minPeers - disabled)
375 // - skip (10+5+0.25*(15-10-5)-10=5), queue the rest, skip counts
376 // towards relayed (15-5-10=0)
377 testRelay("disabled & skip, no queue", true, 15, 5, 10, 25, 5, 0, 10);
378 // relay to minPeers + disabled + 25% of (nPeers - minPeers - disabled)
379 // - skip (10+2+0.25*(20-10-2)-14=0), queue the rest, skip counts
380 // towards relayed (20-14=6)
381 testRelay("disabled & skip, no relay", true, 20, 2, 10, 25, 0, 6, 14);
382 // nothing to send, so nothing is queued either
383 testQueueOnly("queue only, feature disabled", false, 10, 0, 0);
384 // queue every peer, since all of them negotiated the feature
385 testQueueOnly("queue only", true, 10, 0, 10);
386 // queue the 6 that negotiated it and none of the 4 that did not
387 testQueueOnly("queue only, some disabled", true, 10, 4, 6);
388 // queue nobody, since no peer would understand the announcement
389 testQueueOnly("queue only, all disabled", true, 10, 10, 0);
390 testParsed("parsed tx", false, true);
391 testParsed("parsed pseudo-tx", true, true);
392 testParsed("unparsed pseudo-tx", true, false);
393 }
394};
395
396BEAST_DEFINE_TESTSUITE(tx_reduce_relay, overlay, xrpl);
397} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
void pass()
Record a successful test condition.
Definition suite.h:532
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
LogOs< char > log
Logging output stream.
Definition suite.h:150
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
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 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.
A public key.
Definition PublicKey.h:53
std::size_t size() const noexcept
Definition Serializer.h:147
void const * data() const noexcept
Definition Serializer.h:153
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...
Definition CapturePeer.h:68
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
Account const & master
Definition Env.h:165
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:723
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...
Definition CapturePeer.h:68
void testParsed(std::string const &test, bool pseudo, bool parsed)
Relay through the overload that takes the parsed STTx, and check that a pseudo-transaction is queued,...
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 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.
T insert(T... args)
T make_shared(T... args)
json::Value noop(Account const &account)
The null transaction.
Definition noop.h:14
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.
Definition algorithm.h:5
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
BaseUInt< 256 > UInt256
Definition base_uint.h:581
boost::beast::http::request< boost::beast::http::dynamic_body > HttpRequestType
Definition Handoff.h:12
std::string makeFeaturesRequestHeader(bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.
Definition Handshake.cpp:83
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
UInt256 uint256
Definition base_uint.h:587
T size(T... args)
T str(T... args)
std::shared_ptr< STTx const > stx
Definition JTx.h:37