rippled
tx_reduce_relay_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright 2020 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 #include <ripple/basics/make_SSLContext.h>
20 #include <ripple/beast/unit_test.h>
21 #include <ripple/overlay/impl/OverlayImpl.h>
22 #include <ripple/overlay/impl/PeerImp.h>
23 #include <ripple/peerfinder/impl/SlotImp.h>
24 #include <test/jtx/Env.h>
25 
26 namespace ripple {
27 
28 namespace test {
29 
30 class tx_reduce_relay_test : public beast::unit_test::suite
31 {
32 public:
33  using socket_type = boost::asio::ip::tcp::socket;
34  using middle_type = boost::beast::tcp_stream;
35  using stream_type = boost::beast::ssl_stream<middle_type>;
37 
38 private:
39  void
40  doTest(const std::string& msg, bool log, std::function<void(bool)> f)
41  {
42  testcase(msg);
43  f(log);
44  }
45 
46  void
47  testConfig(bool log)
48  {
49  doTest("Config Test", log, [&](bool log) {
50  auto test = [&](bool enable,
51  bool metrics,
52  std::uint16_t min,
53  std::uint16_t pct,
54  bool success = true) {
55  std::stringstream str("[reduce_relay]");
56  str << "[reduce_relay]\n"
57  << "tx_enable=" << static_cast<int>(enable) << "\n"
58  << "tx_metrics=" << static_cast<int>(metrics) << "\n"
59  << "tx_min_peers=" << min << "\n"
60  << "tx_relay_percentage=" << pct << "\n";
61  Config c;
62  try
63  {
64  c.loadFromString(str.str());
65 
66  BEAST_EXPECT(c.TX_REDUCE_RELAY_ENABLE == enable);
67  BEAST_EXPECT(c.TX_REDUCE_RELAY_METRICS == metrics);
68  BEAST_EXPECT(c.TX_REDUCE_RELAY_MIN_PEERS == min);
69  BEAST_EXPECT(c.TX_RELAY_PERCENTAGE == pct);
70  if (success)
71  pass();
72  else
73  fail();
74  }
75  catch (...)
76  {
77  if (success)
78  fail();
79  else
80  pass();
81  }
82  };
83 
84  test(true, true, 20, 25);
85  test(false, false, 20, 25);
86  test(false, false, 20, 0, false);
87  test(false, false, 20, 101, false);
88  test(false, false, 9, 10, false);
89  test(false, false, 10, 9, false);
90  });
91  }
92 
93  class PeerTest : public PeerImp
94  {
95  public:
97  Application& app,
99  http_request_type&& request,
100  PublicKey const& publicKey,
102  Resource::Consumer consumer,
104  OverlayImpl& overlay)
105  : PeerImp(
106  app,
107  sid_,
108  slot,
109  std::move(request),
110  publicKey,
111  protocol,
112  consumer,
113  std::move(stream_ptr),
114  overlay)
115  {
116  sid_++;
117  }
118  ~PeerTest() = default;
119 
120  void
121  run() override
122  {
123  }
124  void
126  {
127  sendTx_++;
128  }
129  void
130  addTxQueue(const uint256& hash) override
131  {
132  queueTx_++;
133  }
134  static void
136  {
137  queueTx_ = 0;
138  sendTx_ = 0;
139  sid_ = 0;
140  }
141  inline static std::size_t sid_ = 0;
142  inline static std::uint16_t queueTx_ = 0;
143  inline static std::uint16_t sendTx_ = 0;
144  };
145 
150  boost::beast::multi_buffer read_buf_;
151 
152 public:
155  {
156  }
157 
158 private:
159  void
161  jtx::Env& env,
163  std::uint16_t& nDisabled)
164  {
165  auto& overlay = dynamic_cast<OverlayImpl&>(env.app().overlay());
166  boost::beast::http::request<boost::beast::http::dynamic_body> request;
167  (nDisabled == 0)
168  ? (void)request.insert(
169  "X-Protocol-Ctl",
170  makeFeaturesRequestHeader(false, false, true, false))
171  : (void)nDisabled--;
172  auto stream_ptr = std::make_unique<stream_type>(
173  socket_type(std::forward<boost::asio::io_service&>(
174  env.app().getIOService())),
175  *context_);
176  beast::IP::Endpoint local(
177  beast::IP::Address::from_string("172.1.1." + std::to_string(lid_)));
178  beast::IP::Endpoint remote(
179  beast::IP::Address::from_string("172.1.1." + std::to_string(rid_)));
180  PublicKey key(std::get<0>(randomKeyPair(KeyType::ed25519)));
181  auto consumer = overlay.resourceManager().newInboundEndpoint(remote);
182  auto slot = overlay.peerFinder().new_inbound_slot(local, remote);
183  auto const peer = std::make_shared<PeerTest>(
184  env.app(),
185  slot,
186  std::move(request),
187  key,
189  consumer,
190  std::move(stream_ptr),
191  overlay);
192  overlay.add_active(peer);
193  peers.emplace_back(peer); // overlay stores week ptr to PeerImp
194  lid_ += 2;
195  rid_ += 2;
196  assert(lid_ <= 254);
197  }
198 
199  void
201  std::string const& test,
202  bool txRREnabled,
203  std::uint16_t nPeers,
204  std::uint16_t nDisabled,
205  std::uint16_t minPeers,
206  std::uint16_t relayPercentage,
207  std::uint16_t expectRelay,
208  std::uint16_t expectQueue,
209  std::set<Peer::id_t> const& toSkip = {})
210  {
211  testcase(test);
212  jtx::Env env(*this);
214  env.app().config().TX_REDUCE_RELAY_ENABLE = txRREnabled;
215  env.app().config().TX_REDUCE_RELAY_MIN_PEERS = minPeers;
216  env.app().config().TX_RELAY_PERCENTAGE = relayPercentage;
217  PeerTest::init();
218  lid_ = 0;
219  rid_ = 0;
220  for (int i = 0; i < nPeers; i++)
221  addPeer(env, peers, nDisabled);
222  protocol::TMTransaction m;
223  m.set_rawtransaction("transaction");
224  m.set_deferred(false);
225  m.set_status(protocol::TransactionStatus::tsNEW);
226  env.app().overlay().relay(uint256{0}, m, toSkip);
227  BEAST_EXPECT(
228  PeerTest::sendTx_ == expectRelay &&
229  PeerTest::queueTx_ == expectQueue);
230  }
231 
232  void
233  run() override
234  {
235  bool log = false;
236  std::set<Peer::id_t> skip = {0, 1, 2, 3, 4};
237  testConfig(log);
238  // relay to all peers, no hash queue
239  testRelay("feature disabled", false, 10, 0, 10, 25, 10, 0);
240  // relay to nPeers - skip (10-5=5)
241  testRelay("feature disabled & skip", false, 10, 0, 10, 25, 5, 0, skip);
242  // relay to all peers because min is greater than nPeers
243  testRelay("relay all 1", true, 10, 0, 20, 25, 10, 0);
244  // relay to all peers because min + disabled is greater thant nPeers
245  testRelay("relay all 2", true, 20, 15, 10, 25, 20, 0);
246  // relay to minPeers + 25% of nPeers-minPeers (20+0.25*(60-20)=30),
247  // queue the rest (30)
248  testRelay("relay & queue", true, 60, 0, 20, 25, 30, 30);
249  // relay to minPeers + 25% of (nPeers - nPeers) - skip
250  // (20+0.25*(60-20)-5=25), queue the rest, skip counts towards relayed
251  // (60-25-5=30)
252  testRelay("skip", true, 60, 0, 20, 25, 25, 30, skip);
253  // relay to minPeers + disabled + 25% of (nPeers - minPeers - disalbed)
254  // (20+10+0.25*(70-20-10)=40), queue the rest (30)
255  testRelay("disabled", true, 70, 10, 20, 25, 40, 30);
256  // relay to minPeers + disabled-not-in-skip + 25% of (nPeers - minPeers
257  // - disabled) (20+5+0.25*(70-20-10)=35), queue the rest, skip counts
258  // towards relayed (70-35-5=30))
259  testRelay("disabled & skip", true, 70, 10, 20, 25, 35, 30, skip);
260  // relay to minPeers + disabled + 25% of (nPeers - minPeers - disabled)
261  // - skip (10+5+0.25*(15-10-5)-10=5), queue the rest, skip counts
262  // towards relayed (15-5-10=0)
263  skip = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
264  testRelay("disabled & skip, no queue", true, 15, 5, 10, 25, 5, 0, skip);
265  // relay to minPeers + disabled + 25% of (nPeers - minPeers - disabled)
266  // - skip (10+2+0.25*(20-10-2)-14=0), queue the rest, skip counts
267  // towards relayed (20-14=6)
268  skip = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
269  testRelay("disabled & skip, no relay", true, 20, 2, 10, 25, 0, 6, skip);
270  }
271 };
272 
273 BEAST_DEFINE_TESTSUITE(tx_reduce_relay, ripple_data, ripple);
274 } // namespace test
275 } // namespace ripple
ripple::test::tx_reduce_relay_test::PeerTest::queueTx_
static std::uint16_t queueTx_
Definition: tx_reduce_relay_test.cpp:142
ripple::Application
Definition: Application.h:115
ripple::test::tx_reduce_relay_test::addPeer
void addPeer(jtx::Env &env, std::vector< std::shared_ptr< PeerTest >> &peers, std::uint16_t &nDisabled)
Definition: tx_reduce_relay_test.cpp:160
ripple::test::tx_reduce_relay_test::run
void run() override
Definition: tx_reduce_relay_test.cpp:233
std::string
STL class.
std::shared_ptr< boost::asio::ssl::context >
ripple::test::tx_reduce_relay_test::PeerTest::run
void run() override
Definition: tx_reduce_relay_test.cpp:121
ripple::test::tx_reduce_relay_test::socket_type
boost::asio::ip::tcp::socket socket_type
Definition: tx_reduce_relay_test.cpp:33
ripple::test::tx_reduce_relay_test::protocolVersion_
ProtocolVersion protocolVersion_
Definition: tx_reduce_relay_test.cpp:149
std::pair
std::vector
STL class.
ripple::makeFeaturesRequestHeader
std::string makeFeaturesRequestHeader(bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.
Definition: Handshake.cpp:74
ripple::make_SSLContext
std::shared_ptr< boost::asio::ssl::context > make_SSLContext(std::string const &cipherList)
Create a self-signed SSL context that allows anonymous Diffie Hellman.
Definition: make_SSLContext.cpp:364
ripple::test::tx_reduce_relay_test::PeerTest::addTxQueue
void addTxQueue(const uint256 &hash) override
Add transaction's hash to the transactions' hashes queue.
Definition: tx_reduce_relay_test.cpp:130
std::stringstream
STL class.
ripple::PeerImp::slot
std::shared_ptr< PeerFinder::Slot > const & slot()
Definition: PeerImp.h:260
ripple::test::tx_reduce_relay_test::testRelay
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={})
Definition: tx_reduce_relay_test.cpp:200
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:241
std::function
ripple::test::tx_reduce_relay_test::testConfig
void testConfig(bool log)
Definition: tx_reduce_relay_test.cpp:47
ripple::PeerImp
Definition: PeerImp.h:52
ripple::test::tx_reduce_relay_test::stream_type
boost::beast::ssl_stream< middle_type > stream_type
Definition: tx_reduce_relay_test.cpp:35
ripple::KeyType::ed25519
@ ed25519
ripple::base_uint< 256 >
ripple::test::tx_reduce_relay_test::tx_reduce_relay_test
tx_reduce_relay_test()
Definition: tx_reduce_relay_test.cpp:153
ripple::test::tx_reduce_relay_test::PeerTest::sid_
static std::size_t sid_
Definition: tx_reduce_relay_test.cpp:141
ripple::test::tx_reduce_relay_test::middle_type
boost::beast::tcp_stream middle_type
Definition: tx_reduce_relay_test.cpp:34
ripple::Config::loadFromString
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
Definition: Config.cpp:457
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::Config
Definition: Config.h:89
ripple::test::tx_reduce_relay_test::read_buf_
boost::beast::multi_buffer read_buf_
Definition: tx_reduce_relay_test.cpp:150
ripple::test::tx_reduce_relay_test::rid_
std::uint16_t rid_
Definition: tx_reduce_relay_test.cpp:147
ripple::test::tx_reduce_relay_test::lid_
std::uint16_t lid_
Definition: tx_reduce_relay_test.cpp:146
std::to_string
T to_string(T... args)
ripple::test::tx_reduce_relay_test::PeerTest::~PeerTest
~PeerTest()=default
std::uint16_t
ripple::test::tx_reduce_relay_test::context_
shared_context context_
Definition: tx_reduce_relay_test.cpp:148
ripple::test::tx_reduce_relay_test::PeerTest::init
static void init()
Definition: tx_reduce_relay_test.cpp:135
ripple::Application::getIOService
virtual boost::asio::io_service & getIOService()=0
ripple::test::tx_reduce_relay_test::PeerTest
Definition: tx_reduce_relay_test.cpp:93
ripple::randomKeyPair
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Definition: SecretKey.cpp:368
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::tx_reduce_relay_test::PeerTest::PeerTest
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 > &&stream_ptr, OverlayImpl &overlay)
Definition: tx_reduce_relay_test.cpp:96
protocol
Definition: ValidatorList.h:38
ripple::test::tx_reduce_relay_test
Definition: tx_reduce_relay_test.cpp:30
ripple::Config::TX_REDUCE_RELAY_MIN_PEERS
std::size_t TX_REDUCE_RELAY_MIN_PEERS
Definition: Config.h:274
ripple::Config::TX_REDUCE_RELAY_METRICS
bool TX_REDUCE_RELAY_METRICS
Definition: Config.h:271
std
STL namespace.
ripple::Resource::Consumer
An endpoint that consumes resources.
Definition: Consumer.h:34
ripple::Application::overlay
virtual Overlay & overlay()=0
std::stringstream::str
T str(T... args)
std::size_t
ripple::test::tx_reduce_relay_test::PeerTest::send
void send(std::shared_ptr< Message > const &) override
Definition: tx_reduce_relay_test.cpp:125
ripple::Config::TX_RELAY_PERCENTAGE
std::size_t TX_RELAY_PERCENTAGE
Definition: Config.h:277
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
ripple::Config::TX_REDUCE_RELAY_ENABLE
bool TX_REDUCE_RELAY_ENABLE
Definition: Config.h:264
ripple::OverlayImpl
Definition: OverlayImpl.h:58
ripple::test::tx_reduce_relay_test::PeerTest::sendTx_
static std::uint16_t sendTx_
Definition: tx_reduce_relay_test.cpp:143
ripple::test::tx_reduce_relay_test::doTest
void doTest(const std::string &msg, bool log, std::function< void(bool)> f)
Definition: tx_reduce_relay_test.cpp:40
ripple::http_request_type
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition: Handshake.h:47
std::unique_ptr
STL class.
std::set
STL class.
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)