rippled
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/random.h>
20 #include <ripple/beast/unit_test.h>
21 #include <ripple/overlay/Message.h>
22 #include <ripple/overlay/Peer.h>
23 #include <ripple/overlay/Slot.h>
24 #include <ripple/protocol/SecretKey.h>
25 #include <ripple.pb.h>
26 #include <test/jtx/Env.h>
27 
28 #include <boost/optional.hpp>
29 #include <boost/thread.hpp>
30 
31 #include <numeric>
32 #include <optional>
33 
34 namespace ripple {
35 
36 namespace test {
37 
38 using namespace std::chrono;
39 
40 class Link;
41 
46 using SquelchCB =
48 using UnsquelchCB = std::function<void(PublicKey const&, PeerWPtr const&)>;
50 
51 static constexpr std::uint32_t MAX_PEERS = 10;
52 static constexpr std::uint32_t MAX_VALIDATORS = 10;
53 static constexpr std::uint32_t MAX_MESSAGES = 200000;
54 
58 class PeerPartial : public Peer
59 {
60 public:
61  virtual ~PeerPartial()
62  {
63  }
64  virtual void
65  onMessage(MessageSPtr const& m, SquelchCB f) = 0;
66  virtual void
67  onMessage(protocol::TMSquelch const& squelch) = 0;
68  void
69  send(protocol::TMSquelch const& squelch)
70  {
71  onMessage(squelch);
72  }
73 
74  // dummy implementation
75  void
76  send(std::shared_ptr<Message> const& m) override
77  {
78  }
80  getRemoteAddress() const override
81  {
82  return {};
83  }
84  void
85  charge(Resource::Charge const& fee) override
86  {
87  }
88  bool
89  cluster() const override
90  {
91  return false;
92  }
93  bool
94  isHighLatency() const override
95  {
96  return false;
97  }
98  int
99  getScore(bool) const override
100  {
101  return 0;
102  }
103  PublicKey const&
104  getNodePublic() const override
105  {
106  static PublicKey key{};
107  return key;
108  }
110  json() override
111  {
112  return {};
113  }
114  bool
116  {
117  return false;
118  }
119  boost::optional<std::size_t>
120  publisherListSequence(PublicKey const&) const override
121  {
122  return {};
123  }
124  void
126  {
127  }
128  uint256 const&
129  getClosedLedgerHash() const override
130  {
131  static uint256 hash{};
132  return hash;
133  }
134  bool
135  hasLedger(uint256 const& hash, std::uint32_t seq) const override
136  {
137  return false;
138  }
139  void
140  ledgerRange(std::uint32_t& minSeq, std::uint32_t& maxSeq) const override
141  {
142  }
143  bool
144  hasShard(std::uint32_t shardIndex) const override
145  {
146  return false;
147  }
148  bool
149  hasTxSet(uint256 const& hash) const override
150  {
151  return false;
152  }
153  void
154  cycleStatus() override
155  {
156  }
157  bool
158  hasRange(std::uint32_t uMin, std::uint32_t uMax) override
159  {
160  return false;
161  }
162  bool
163  compressionEnabled() const override
164  {
165  return false;
166  }
167 };
168 
171 {
172 public:
173  typedef uint64_t rep;
177  inline static const bool is_steady = false;
178 
179  static void
180  advance(duration d) noexcept
181  {
182  now_ += d;
183  }
184 
185  static void
187  {
188  now_ += randDuration(min, max);
189  }
190 
191  static void
192  reset() noexcept
193  {
194  now_ = time_point(seconds(0));
195  }
196 
197  static time_point
198  now() noexcept
199  {
200  return now_;
201  }
202 
203  static duration
205  {
206  return duration(milliseconds(rand_int(min.count(), max.count())));
207  }
208 
209  explicit ManualClock() = default;
210 
211 private:
212  inline static time_point now_ = time_point(seconds(0));
213 };
214 
216 class Overlay
217 {
218 public:
219  Overlay() = default;
220  virtual ~Overlay() = default;
221 
222  virtual void
223  updateSlotAndSquelch(
224  uint256 const& key,
225  PublicKey const& validator,
226  Peer::id_t id,
227  SquelchCB f,
228  protocol::MessageType type = protocol::mtVALIDATION) = 0;
229 
230  virtual void deleteIdlePeers(UnsquelchCB) = 0;
231 
232  virtual void deletePeer(Peer::id_t, UnsquelchCB) = 0;
233 };
234 
235 class Validator;
236 
240 class Link
241 {
243 
244 public:
246  Validator& validator,
247  PeerSPtr peer,
248  Latency const& latency = {milliseconds(5), milliseconds(15)})
249  : validator_(validator), peer_(peer), latency_(latency), up_(true)
250  {
251  auto sp = peer_.lock();
252  assert(sp);
253  }
254  ~Link() = default;
255  void
257  {
258  if (!up_)
259  return;
260  auto sp = peer_.lock();
261  assert(sp);
262  auto peer = std::dynamic_pointer_cast<PeerPartial>(sp);
263  peer->onMessage(m, f);
264  }
265  Validator&
267  {
268  return validator_;
269  }
270  void
271  up(bool linkUp)
272  {
273  up_ = linkUp;
274  }
275  Peer::id_t
277  {
278  auto p = peer_.lock();
279  assert(p);
280  return p->id();
281  }
282  PeerSPtr
284  {
285  auto p = peer_.lock();
286  assert(p);
287  return p;
288  }
289 
290 private:
294  bool up_;
295 };
296 
299 {
301 
302 public:
304  {
305  pkey_ = std::get<0>(randomKeyPair(KeyType::ed25519));
306  protocol::TMValidation v;
307  v.set_validation("validation");
308  message_ = std::make_shared<Message>(v, protocol::mtVALIDATION, pkey_);
309  id_ = sid_++;
310  }
311  Validator(Validator const&) = default;
312  Validator(Validator&&) = default;
313  Validator&
314  operator=(Validator const&) = default;
315  Validator&
316  operator=(Validator&&) = default;
318  {
319  clear();
320  }
321 
322  void
324  {
325  links_.clear();
326  }
327 
328  static void
330  {
331  sid_ = 0;
332  }
333 
334  PublicKey const&
335  key()
336  {
337  return pkey_;
338  }
339 
340  operator PublicKey() const
341  {
342  return pkey_;
343  }
344 
345  void
347  {
348  links_.emplace(
349  std::make_pair(peer->id(), std::make_shared<Link>(*this, peer)));
350  }
351 
352  void
354  {
355  links_.erase(id);
356  }
357 
358  void
360  {
361  for (auto id : peers)
362  {
363  assert(links_.find(id) != links_.end());
364  f(*links_[id], message_);
365  }
366  }
367 
368  void
369  for_links(LinkIterCB f, bool simulateSlow = false)
370  {
373  links_.begin(), links_.end(), std::back_inserter(v), [](auto& kv) {
374  return kv.second;
375  });
377  std::mt19937 g(d());
378  std::shuffle(v.begin(), v.end(), g);
379 
380  for (auto& link : v)
381  {
382  f(*link, message_);
383  }
384  }
385 
387  void
389  {
390  for_links(peers, [&](Link& link, MessageSPtr m) { link.send(m, f); });
391  }
392 
394  void
396  {
397  for_links([&](Link& link, MessageSPtr m) { link.send(m, f); });
398  }
399 
402  {
403  return message_;
404  }
405 
407  id()
408  {
409  return id_;
410  }
411 
412  void
414  {
415  auto it = links_.find(id);
416  assert(it != links_.end());
417  it->second->up(true);
418  }
419 
420  void
422  {
423  auto it = links_.find(id);
424  assert(it != links_.end());
425  it->second->up(false);
426  }
427 
428 private:
430  PublicKey pkey_{};
431  MessageSPtr message_ = nullptr;
432  inline static std::uint16_t sid_ = 0;
433  std::uint16_t id_ = 0;
434 };
435 
436 class PeerSim : public PeerPartial, public std::enable_shared_from_this<PeerSim>
437 {
438 public:
439  using id_t = Peer::id_t;
440  PeerSim(Overlay& overlay) : overlay_(overlay)
441  {
442  id_ = sid_++;
443  }
444 
445  ~PeerSim() = default;
446 
447  id_t
448  id() const override
449  {
450  return id_;
451  }
452 
453  static void
455  {
456  sid_ = 0;
457  }
458 
460  void
461  onMessage(MessageSPtr const& m, SquelchCB f) override
462  {
463  auto validator = m->getValidatorKey();
464  assert(validator);
465  if (squelch_.isSquelched(*validator))
466  return;
467 
468  overlay_.updateSlotAndSquelch({}, *validator, id(), f);
469  }
470 
472  virtual void
473  onMessage(protocol::TMSquelch const& squelch) override
474  {
475  auto validator = squelch.validatorpubkey();
476  PublicKey key(Slice(validator.data(), validator.size()));
477  squelch_.squelch(key, squelch.squelch(), squelch.squelchduration());
478  }
479 
480 private:
481  inline static id_t sid_ = 0;
485 };
486 
488 {
490 
491 public:
492  using id_t = Peer::id_t;
494  OverlaySim(Application& app) : slots_(app, *this)
495  {
496  }
497 
498  ~OverlaySim() = default;
499 
500  void
502  {
503  peers_.clear();
505  slots_.deleteIdlePeers();
506  }
507 
509  inState(PublicKey const& validator, squelch::PeerState state)
510  {
511  auto res = slots_.inState(validator, state);
512  return res ? *res : 0;
513  }
514 
515  void
517  uint256 const& key,
518  PublicKey const& validator,
519  Peer::id_t id,
520  SquelchCB f,
521  protocol::MessageType type = protocol::mtVALIDATION) override
522  {
523  squelch_ = f;
524  slots_.updateSlotAndSquelch(key, validator, id, type);
525  }
526 
527  void
528  deletePeer(id_t id, UnsquelchCB f) override
529  {
530  unsquelch_ = f;
531  slots_.deletePeer(id, true);
532  }
533 
534  void
536  {
537  unsquelch_ = f;
538  slots_.deleteIdlePeers();
539  }
540 
541  PeerSPtr
542  addPeer(bool useCache = true)
543  {
544  PeerSPtr peer{};
545  Peer::id_t id;
546  if (peersCache_.empty() || !useCache)
547  {
548  peer = std::make_shared<PeerSim>(*this);
549  id = peer->id();
550  }
551  else
552  {
553  auto it = peersCache_.begin();
554  peer = it->second;
555  id = it->first;
556  peersCache_.erase(it);
557  }
558  peers_.emplace(std::make_pair(id, peer));
559  return peer;
560  }
561 
562  void
563  deletePeer(Peer::id_t id, bool useCache = true)
564  {
565  auto it = peers_.find(id);
566  assert(it != peers_.end());
567  deletePeer(id, [&](PublicKey const&, PeerWPtr) {});
568  if (useCache)
569  peersCache_.emplace(std::make_pair(id, it->second));
570  peers_.erase(it);
571  }
572 
573  void
575  {
576  while (!peers_.empty())
577  deletePeer(peers_.begin()->first);
578  while (!peersCache_.empty())
579  addPeer();
580  }
581 
584  {
585  if (peers_.empty())
586  return {};
587 
588  std::uint8_t maxId = 0;
589 
590  for (auto& [id, _] : peers_)
591  {
592  (void)_;
593  if (id > maxId)
594  maxId = id;
595  }
596 
597  deletePeer(maxId, false);
598 
599  return maxId;
600  }
601 
602  bool
603  isCountingState(PublicKey const& validator)
604  {
605  return slots_.inState(validator, squelch::SlotState::Counting);
606  }
607 
609  getSelected(PublicKey const& validator)
610  {
611  return slots_.getSelected(validator);
612  }
613 
614  bool
615  isSelected(PublicKey const& validator, Peer::id_t peer)
616  {
617  auto selected = slots_.getSelected(validator);
618  return selected.find(peer) != selected.end();
619  }
620 
621  id_t
622  getSelectedPeer(PublicKey const& validator)
623  {
624  auto selected = slots_.getSelected(validator);
625  assert(selected.size());
626  return *selected.begin();
627  }
628 
630  id_t,
631  std::tuple<
635  std::uint32_t>>
636  getPeers(PublicKey const& validator)
637  {
638  return slots_.getPeers(validator);
639  }
640 
642  getNumPeers() const
643  {
644  return peers_.size();
645  }
646 
647 private:
648  void
650  PublicKey const& validator,
651  Peer::id_t id,
652  std::uint32_t squelchDuration) const override
653  {
654  if (auto it = peers_.find(id); it != peers_.end())
655  squelch_(validator, it->second, squelchDuration);
656  }
657  void
658  unsquelch(PublicKey const& validator, Peer::id_t id) const override
659  {
660  if (auto it = peers_.find(id); it != peers_.end())
661  unsquelch_(validator, it->second);
662  }
668 };
669 
670 class Network
671 {
672 public:
673  Network(Application& app) : overlay_(app)
674  {
675  init();
676  }
677 
678  void
680  {
681  validators_.resize(MAX_VALIDATORS);
682  for (int p = 0; p < MAX_PEERS; p++)
683  {
684  auto peer = overlay_.addPeer();
685  for (auto& v : validators_)
686  v.addPeer(peer);
687  }
688  }
689 
690  ~Network() = default;
691 
692  void
694  {
695  validators_.clear();
696  overlay_.clear();
699  init();
700  }
701 
702  Peer::id_t
704  {
705  auto peer = overlay_.addPeer();
706  for (auto& v : validators_)
707  v.addPeer(peer);
708  return peer->id();
709  }
710 
711  void
713  {
714  auto id = overlay_.deleteLastPeer();
715 
716  if (!id)
717  return;
718 
719  for (auto& validator : validators_)
720  validator.deletePeer(*id);
721  }
722 
723  void
725  {
726  while (overlay_.getNumPeers() > MAX_PEERS)
727  deleteLastPeer();
728  }
729 
730  Validator&
732  {
733  assert(v < validators_.size());
734  return validators_[v];
735  }
736 
737  OverlaySim&
739  {
740  return overlay_;
741  }
742 
743  void
744  enableLink(std::uint16_t validatorId, Peer::id_t peer, bool enable)
745  {
746  auto it =
747  std::find_if(validators_.begin(), validators_.end(), [&](auto& v) {
748  return v.id() == validatorId;
749  });
750  assert(it != validators_.end());
751  if (enable)
752  it->linkUp(peer);
753  else
754  it->linkDown(peer);
755  }
756 
757  void
759  {
760  // Send unsquelch to the Peer on all links. This way when
761  // the Peer "reconnects" it starts sending messages on the link.
762  // We expect that if a Peer disconnects and then reconnects, it's
763  // unsquelched.
764  protocol::TMSquelch squelch;
765  squelch.set_squelch(false);
766  for (auto& v : validators_)
767  {
768  PublicKey key = v;
769  squelch.clear_validatorpubkey();
770  squelch.set_validatorpubkey(key.data(), key.size());
771  v.for_links({peer}, [&](Link& l, MessageSPtr) {
772  std::dynamic_pointer_cast<PeerSim>(l.getPeer())->send(squelch);
773  });
774  }
775  }
776 
777  void
779  std::uint32_t min,
780  std::uint32_t max,
781  std::function<void(std::uint32_t)> f)
782  {
783  auto size = max - min;
785  std::iota(s.begin(), s.end(), min);
787  std::mt19937 g(d());
788  std::shuffle(s.begin(), s.end(), g);
789  for (auto v : s)
790  f(v);
791  }
792 
793  void
795  LinkIterCB link,
796  std::uint16_t nValidators = MAX_VALIDATORS,
797  std::uint32_t nMessages = MAX_MESSAGES,
798  bool purge = true,
799  bool resetClock = true)
800  {
801  if (resetClock)
803 
804  if (purge)
805  {
806  purgePeers();
807  overlay_.resetPeers();
808  }
809 
810  for (int m = 0; m < nMessages; ++m)
811  {
813  for_rand(0, nValidators, [&](std::uint32_t v) {
814  validators_[v].for_links(link);
815  });
816  }
817  }
818 
820  bool
822  {
823  for (auto& v : validators_)
824  {
825  if (overlay_.isSelected(v, id))
826  return true;
827  }
828  return false;
829  }
830 
835  bool
837  {
838  for (auto& v : validators_)
839  {
840  if (!overlay_.isSelected(v, peer))
841  continue;
842  auto peers = overlay_.getPeers(v);
843  for (auto& [_, v] : peers)
844  {
845  (void)_;
846  if (std::get<squelch::PeerState>(v) ==
848  return false;
849  }
850  }
851  return true;
852  }
853 
854 private:
857 };
858 
859 class reduce_relay_test : public beast::unit_test::suite
860 {
862  using id_t = Peer::id_t;
863 
864 protected:
865  void
866  printPeers(const std::string& msg, std::uint16_t validator = 0)
867  {
868  auto peers = network_.overlay().getPeers(network_.validator(validator));
869  std::cout << msg << " "
870  << "num peers " << (int)network_.overlay().getNumPeers()
871  << std::endl;
872  for (auto& [k, v] : peers)
873  std::cout << k << ":" << (int)std::get<squelch::PeerState>(v)
874  << " ";
875  std::cout << std::endl;
876  }
877 
879  Peer::id_t
881  PublicKey const& validator,
882  PeerWPtr const& peerPtr,
884  {
885  protocol::TMSquelch squelch;
886  bool res = duration ? true : false;
887  squelch.set_squelch(res);
888  squelch.set_validatorpubkey(validator.data(), validator.size());
889  if (res)
890  squelch.set_squelchduration(*duration);
891  auto sp = peerPtr.lock();
892  assert(sp);
893  std::dynamic_pointer_cast<PeerSim>(sp)->send(squelch);
894  return sp->id();
895  }
896 
897  enum State { On, Off, WaitReset };
898  enum EventType { LinkDown = 0, PeerDisconnected = 1 };
899  // Link down or Peer disconnect event
900  // TBD - add new peer event
901  // TBD - add overlapping type of events at any
902  // time in any quantity
903  struct Event
904  {
905  State state_ = State::Off;
906  std::uint32_t cnt_ = 0;
907  std::uint32_t handledCnt_ = 0;
908  bool isSelected_ = false;
913  bool handled_ = false;
914  };
915 
919  void
920  random(bool log)
921  {
923  {LinkDown, {}}, {PeerDisconnected, {}}};
925 
926  network_.reset();
927  network_.propagate([&](Link& link, MessageSPtr m) {
928  auto& validator = link.validator();
929  auto now = ManualClock::now();
930 
931  bool squelched = false;
932  std::stringstream str;
933 
934  link.send(
935  m,
936  [&](PublicKey const& key,
937  PeerWPtr const& peerPtr,
939  assert(key == validator);
940  auto p = sendSquelch(key, peerPtr, duration);
941  squelched = true;
942  str << p << " ";
943  });
944 
945  if (squelched)
946  {
947  auto selected = network_.overlay().getSelected(validator);
948  str << " selected: ";
949  for (auto s : selected)
950  str << s << " ";
951  if (log)
952  std::cout
953  << (double)squelch::epoch<milliseconds>(now).count() /
954  1000.
955  << " random, squelched, validator: " << validator.id()
956  << " peers: " << str.str() << std::endl;
957  auto countingState =
958  network_.overlay().isCountingState(validator);
959  BEAST_EXPECT(
960  countingState == false &&
961  selected.size() == squelch::MAX_SELECTED_PEERS);
962  }
963 
964  // Trigger Link Down or Peer Disconnect event
965  // Only one Link Down at a time
966  if (events[EventType::LinkDown].state_ == State::Off)
967  {
968  auto update = [&](EventType event) {
969  events[event].cnt_++;
970  events[event].validator_ = validator.id();
971  events[event].key_ = validator;
972  events[event].peer_ = link.peerId();
973  events[event].state_ = State::On;
974  events[event].time_ = now;
975  if (event == EventType::LinkDown)
976  {
977  network_.enableLink(
978  validator.id(), link.peerId(), false);
979  events[event].isSelected_ =
980  network_.overlay().isSelected(
981  validator, link.peerId());
982  }
983  else
984  events[event].isSelected_ =
985  network_.isSelected(link.peerId());
986  };
987  auto r = rand_int(0, 1000);
988  if (r == (int)EventType::LinkDown ||
989  r == (int)EventType::PeerDisconnected)
990  {
991  update(static_cast<EventType>(r));
992  }
993  }
994 
995  if (events[EventType::PeerDisconnected].state_ == State::On)
996  {
997  auto& event = events[EventType::PeerDisconnected];
998  bool allCounting = network_.allCounting(event.peer_);
999  network_.overlay().deletePeer(
1000  event.peer_,
1001  [&](PublicKey const& v, PeerWPtr const& peerPtr) {
1002  if (event.isSelected_)
1003  sendSquelch(v, peerPtr, {});
1004  event.handled_ = true;
1005  });
1006  // Should only be unsquelched if the peer is in Selected state
1007  // If in Selected state it's possible unsquelching didn't
1008  // take place because there is no peers in Squelched state in
1009  // any of the slots where the peer is in Selected state
1010  // (allCounting is true)
1011  bool handled =
1012  (event.isSelected_ == false && !event.handled_) ||
1013  (event.isSelected_ == true &&
1014  (event.handled_ || allCounting));
1015  BEAST_EXPECT(handled);
1016  event.state_ = State::Off;
1017  event.isSelected_ = false;
1018  event.handledCnt_ += handled;
1019  event.handled_ = false;
1020  network_.onDisconnectPeer(event.peer_);
1021  }
1022 
1023  auto& event = events[EventType::LinkDown];
1024  // Check every sec for idled peers. Idled peers are
1025  // created by Link Down event.
1026  if (now - lastCheck > milliseconds(1000))
1027  {
1028  lastCheck = now;
1029  // Check if Link Down event must be handled by
1030  // deleteIdlePeer(): 1) the peer is in Selected state;
1031  // 2) the peer has not received any messages for IDLED time;
1032  // 3) there are peers in Squelched state in the slot.
1033  // 4) peer is in Slot's peers_ (if not then it is deleted
1034  // by Slots::deleteIdlePeers())
1035  bool mustHandle = false;
1036  if (event.state_ == State::On)
1037  {
1038  event.isSelected_ =
1039  network_.overlay().isSelected(event.key_, event.peer_);
1040  auto peers = network_.overlay().getPeers(event.key_);
1041  auto d = squelch::epoch<milliseconds>(now).count() -
1042  std::get<3>(peers[event.peer_]);
1043  mustHandle = event.isSelected_ &&
1045  network_.overlay().inState(
1046  event.key_, squelch::PeerState::Squelched) > 0 &&
1047  peers.find(event.peer_) != peers.end();
1048  }
1049  network_.overlay().deleteIdlePeers(
1050  [&](PublicKey const& v, PeerWPtr const& ptr) {
1051  event.handled_ = true;
1052  if (mustHandle && v == event.key_)
1053  {
1054  event.state_ = State::WaitReset;
1055  sendSquelch(validator, ptr, {});
1056  }
1057  });
1058  bool handled =
1059  (event.handled_ && event.state_ == State::WaitReset) ||
1060  (!event.handled_ && !mustHandle);
1061  BEAST_EXPECT(handled);
1062  }
1063  if (event.state_ == State::WaitReset ||
1064  (event.state_ == State::On &&
1065  (now - event.time_ > (squelch::IDLED + seconds(2)))))
1066  {
1067  bool handled =
1068  event.state_ == State::WaitReset || !event.handled_;
1069  BEAST_EXPECT(handled);
1070  event.state_ = State::Off;
1071  event.isSelected_ = false;
1072  event.handledCnt_ += handled;
1073  event.handled_ = false;
1074  network_.enableLink(event.validator_, event.peer_, true);
1075  }
1076  });
1077 
1078  auto& down = events[EventType::LinkDown];
1079  auto& disconnected = events[EventType::PeerDisconnected];
1080  // It's possible the last Down Link event is not handled
1081  BEAST_EXPECT(down.handledCnt_ >= down.cnt_ - 1);
1082  // All Peer Disconnect events must be handled
1083  BEAST_EXPECT(disconnected.cnt_ == disconnected.handledCnt_);
1084  if (log)
1085  std::cout << "link down count: " << down.cnt_ << "/"
1086  << down.handledCnt_
1087  << " peer disconnect count: " << disconnected.cnt_ << "/"
1088  << disconnected.handledCnt_;
1089  }
1090 
1091  bool
1092  checkCounting(PublicKey const& validator, bool isCountingState)
1093  {
1094  auto countingState = network_.overlay().isCountingState(validator);
1095  BEAST_EXPECT(countingState == isCountingState);
1096  return countingState == isCountingState;
1097  }
1098 
1099  void
1100  doTest(const std::string& msg, bool log, std::function<void(bool)> f)
1101  {
1102  testcase(msg);
1103  f(log);
1104  }
1105 
1111  void
1113  {
1114  doTest("Initial Round", log, [this](bool log) {
1115  BEAST_EXPECT(propagateAndSquelch(log));
1116  });
1117  }
1118 
1122  void
1124  {
1125  doTest("Peer Unsquelched Too Soon", log, [this](bool log) {
1126  BEAST_EXPECT(propagateNoSquelch(log, 1, false, false, false));
1127  });
1128  }
1129 
1133  void
1135  {
1137  doTest("Peer Unsquelched", log, [this](bool log) {
1138  BEAST_EXPECT(propagateNoSquelch(log, 2, true, true, false));
1139  });
1140  }
1141 
1143  bool
1144  propagateAndSquelch(bool log, bool purge = true, bool resetClock = true)
1145  {
1146  int n = 0;
1147  network_.propagate(
1148  [&](Link& link, MessageSPtr message) {
1149  std::uint16_t squelched = 0;
1150  link.send(
1151  message,
1152  [&](PublicKey const& key,
1153  PeerWPtr const& peerPtr,
1155  squelched++;
1156  sendSquelch(key, peerPtr, duration);
1157  });
1158  if (squelched)
1159  {
1160  BEAST_EXPECT(
1161  squelched == MAX_PEERS - squelch::MAX_SELECTED_PEERS);
1162  n++;
1163  }
1164  },
1165  1,
1167  purge,
1168  resetClock);
1169  auto selected = network_.overlay().getSelected(network_.validator(0));
1170  BEAST_EXPECT(selected.size() == squelch::MAX_SELECTED_PEERS);
1171  BEAST_EXPECT(n == 1); // only one selection round
1172  auto res = checkCounting(network_.validator(0), false);
1173  BEAST_EXPECT(res);
1174  return n == 1 && res;
1175  }
1176 
1178  bool
1180  bool log,
1181  std::uint16_t nMessages,
1182  bool countingState,
1183  bool purge = true,
1184  bool resetClock = true)
1185  {
1186  bool squelched = false;
1187  network_.propagate(
1188  [&](Link& link, MessageSPtr message) {
1189  link.send(
1190  message,
1191  [&](PublicKey const& key,
1192  PeerWPtr const& peerPtr,
1194  squelched = true;
1195  BEAST_EXPECT(false);
1196  });
1197  },
1198  1,
1199  nMessages,
1200  purge,
1201  resetClock);
1202  auto res = checkCounting(network_.validator(0), countingState);
1203  return !squelched && res;
1204  }
1205 
1209  void
1210  testNewPeer(bool log)
1211  {
1212  doTest("New Peer", log, [this](bool log) {
1213  BEAST_EXPECT(propagateAndSquelch(log, true, false));
1214  network_.addPeer();
1215  BEAST_EXPECT(propagateNoSquelch(log, 1, true, false, false));
1216  });
1217  }
1218 
1221  void
1223  {
1224  doTest("Selected Peer Disconnects", log, [this](bool log) {
1226  BEAST_EXPECT(propagateAndSquelch(log, true, false));
1227  auto id = network_.overlay().getSelectedPeer(network_.validator(0));
1228  std::uint16_t unsquelched = 0;
1229  network_.overlay().deletePeer(
1230  id, [&](PublicKey const& key, PeerWPtr const& peer) {
1231  unsquelched++;
1232  });
1233  BEAST_EXPECT(
1234  unsquelched == MAX_PEERS - squelch::MAX_SELECTED_PEERS);
1235  BEAST_EXPECT(checkCounting(network_.validator(0), true));
1236  });
1237  }
1238 
1241  void
1243  {
1244  doTest("Selected Peer Stops Relaying", log, [this](bool log) {
1246  BEAST_EXPECT(propagateAndSquelch(log, true, false));
1248  std::uint16_t unsquelched = 0;
1249  network_.overlay().deleteIdlePeers(
1250  [&](PublicKey const& key, PeerWPtr const& peer) {
1251  unsquelched++;
1252  });
1253  auto peers = network_.overlay().getPeers(network_.validator(0));
1254  BEAST_EXPECT(
1255  unsquelched == MAX_PEERS - squelch::MAX_SELECTED_PEERS);
1256  BEAST_EXPECT(checkCounting(network_.validator(0), true));
1257  });
1258  }
1259 
1262  void
1264  {
1265  doTest("Squelched Peer Disconnects", log, [this](bool log) {
1267  BEAST_EXPECT(propagateAndSquelch(log, true, false));
1268  auto peers = network_.overlay().getPeers(network_.validator(0));
1269  auto it = std::find_if(peers.begin(), peers.end(), [&](auto it) {
1270  return std::get<squelch::PeerState>(it.second) ==
1271  squelch::PeerState::Squelched;
1272  });
1273  assert(it != peers.end());
1274  std::uint16_t unsquelched = 0;
1275  network_.overlay().deletePeer(
1276  it->first, [&](PublicKey const& key, PeerWPtr const& peer) {
1277  unsquelched++;
1278  });
1279  BEAST_EXPECT(unsquelched == 0);
1280  BEAST_EXPECT(checkCounting(network_.validator(0), false));
1281  });
1282  }
1283 
1284  void
1285  testConfig(bool log)
1286  {
1287  doTest("Config Test", log, [&](bool log) {
1288  Config c;
1289 
1290  std::string toLoad(R"rippleConfig(
1291 [reduce_relay]
1292 enable=1
1293 squelch=1
1294 )rippleConfig");
1295 
1296  c.loadFromString(toLoad);
1297  BEAST_EXPECT(c.REDUCE_RELAY_ENABLE == true);
1298  BEAST_EXPECT(c.REDUCE_RELAY_SQUELCH == true);
1299 
1300  Config c1;
1301 
1302  toLoad = (R"rippleConfig(
1303 [reduce_relay]
1304 enable=0
1305 squelch=0
1306 )rippleConfig");
1307 
1308  c1.loadFromString(toLoad);
1309  BEAST_EXPECT(c1.REDUCE_RELAY_ENABLE == false);
1310  BEAST_EXPECT(c1.REDUCE_RELAY_SQUELCH == false);
1311 
1312  Config c2;
1313 
1314  toLoad = R"rippleConfig(
1315 [reduce_relay]
1316 enabled=1
1317 squelched=1
1318 )rippleConfig";
1319 
1320  c2.loadFromString(toLoad);
1321  BEAST_EXPECT(c2.REDUCE_RELAY_ENABLE == false);
1322  BEAST_EXPECT(c2.REDUCE_RELAY_SQUELCH == false);
1323  });
1324  }
1325 
1326  void
1328  {
1329  doTest("Duplicate Message", log, [&](bool log) {
1330  network_.reset();
1331  // update message count for the same peer/validator
1332  std::int16_t nMessages = 5;
1333  for (int i = 0; i < nMessages; i++)
1334  {
1335  uint256 key(i);
1336  network_.overlay().updateSlotAndSquelch(
1337  key,
1338  network_.validator(0),
1339  0,
1340  [&](PublicKey const&, PeerWPtr, std::uint32_t) {});
1341  }
1342  auto peers = network_.overlay().getPeers(network_.validator(0));
1343  // first message changes Slot state to Counting and is not counted,
1344  // hence '-1'.
1345  BEAST_EXPECT(std::get<1>(peers[0]) == (nMessages - 1));
1346  // add duplicate
1347  uint256 key(nMessages - 1);
1348  network_.overlay().updateSlotAndSquelch(
1349  key,
1350  network_.validator(0),
1351  0,
1352  [&](PublicKey const&, PeerWPtr, std::uint32_t) {});
1353  // confirm the same number of messages
1354  peers = network_.overlay().getPeers(network_.validator(0));
1355  BEAST_EXPECT(std::get<1>(peers[0]) == (nMessages - 1));
1356  // advance the clock
1358  network_.overlay().updateSlotAndSquelch(
1359  key,
1360  network_.validator(0),
1361  0,
1362  [&](PublicKey const&, PeerWPtr, std::uint32_t) {});
1363  peers = network_.overlay().getPeers(network_.validator(0));
1364  // confirm message number increased
1365  BEAST_EXPECT(std::get<1>(peers[0]) == nMessages);
1366  });
1367  }
1368 
1371 
1372 public:
1373  reduce_relay_test() : env_(*this), network_(env_.app())
1374  {
1375  }
1376 
1377  void
1378  run() override
1379  {
1380  bool log = false;
1381  testConfig(log);
1382  testInitialRound(log);
1383  testPeerUnsquelchedTooSoon(log);
1384  testPeerUnsquelched(log);
1385  testNewPeer(log);
1386  testSquelchedPeerDisconnects(log);
1387  testSelectedPeerDisconnects(log);
1388  testSelectedPeerStopsRelaying(log);
1389  testInternalHashRouter(log);
1390  }
1391 };
1392 
1394 {
1395  void
1396  testRandom(bool log)
1397  {
1398  doTest("Random Test", log, [&](bool log) { random(log); });
1399  }
1400 
1401  void
1402  run() override
1403  {
1404  bool log = false;
1405  testRandom(log);
1406  }
1407 };
1408 
1409 BEAST_DEFINE_TESTSUITE(reduce_relay, ripple_data, ripple);
1410 BEAST_DEFINE_TESTSUITE_MANUAL(reduce_relay_simulate, ripple_data, ripple);
1411 
1412 } // namespace test
1413 
1414 } // namespace ripple
ripple::PublicKey::data
std::uint8_t const * data() const noexcept
Definition: PublicKey.h:81
ripple::test::Validator::clear
void clear()
Definition: reduce_relay_test.cpp:323
ripple::test::Validator
Simulate Validator.
Definition: reduce_relay_test.cpp:298
ripple::Application
Definition: Application.h:97
ripple::test::OverlaySim::deleteIdlePeers
void deleteIdlePeers(UnsquelchCB f) override
Definition: reduce_relay_test.cpp:535
ripple::test::OverlaySim::getNumPeers
std::uint16_t getNumPeers() const
Definition: reduce_relay_test.cpp:642
ripple::test::reduce_relay_test::reduce_relay_test
reduce_relay_test()
Definition: reduce_relay_test.cpp:1373
ripple::test::reduce_relay_test::run
void run() override
Definition: reduce_relay_test.cpp:1378
ripple::test::OverlaySim::isSelected
bool isSelected(PublicKey const &validator, Peer::id_t peer)
Definition: reduce_relay_test.cpp:615
std::weak_ptr::lock
T lock(T... args)
ripple::test::reduce_relay_test::testConfig
void testConfig(bool log)
Definition: reduce_relay_test.cpp:1285
ripple::test::ManualClock::randDuration
static duration randDuration(milliseconds min, milliseconds max)
Definition: reduce_relay_test.cpp:204
ripple::test::PeerSim::overlay_
Overlay & overlay_
Definition: reduce_relay_test.cpp:483
ripple::test::reduce_relay_test::testInternalHashRouter
void testInternalHashRouter(bool log)
Definition: reduce_relay_test.cpp:1327
ripple::test::PeerPartial::cycleStatus
void cycleStatus() override
Definition: reduce_relay_test.cpp:154
ripple::test::Network::isSelected
bool isSelected(Peer::id_t id)
Is peer in Selected state in any of the slots.
Definition: reduce_relay_test.cpp:821
ripple::test::OverlaySim::peers_
Peers peers_
Definition: reduce_relay_test.cpp:665
std::string
STL class.
std::shared_ptr< Message >
ripple::test::PeerPartial::getClosedLedgerHash
uint256 const & getClosedLedgerHash() const override
Definition: reduce_relay_test.cpp:129
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::test::OverlaySim::unsquelch
void unsquelch(PublicKey const &validator, Peer::id_t id) const override
Unsquelch handler.
Definition: reduce_relay_test.cpp:658
ripple::test::ManualClock::randAdvance
static void randAdvance(milliseconds min, milliseconds max)
Definition: reduce_relay_test.cpp:186
ripple::test::reduce_relay_test
Definition: reduce_relay_test.cpp:859
ripple::test::PeerSim::resetId
static void resetId()
Definition: reduce_relay_test.cpp:454
ripple::test::Network
Definition: reduce_relay_test.cpp:670
ripple::test::reduce_relay_test::network_
Network network_
Definition: reduce_relay_test.cpp:1370
ripple::test::Validator::message
MessageSPtr message()
Definition: reduce_relay_test.cpp:401
ripple::squelch::MAX_SELECTED_PEERS
static constexpr uint16_t MAX_SELECTED_PEERS
Definition: SquelchCommon.h:43
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::test::reduce_relay_test::propagateNoSquelch
bool propagateNoSquelch(bool log, std::uint16_t nMessages, bool countingState, bool purge=true, bool resetClock=true)
Send fewer message so that squelch event is not generated.
Definition: reduce_relay_test.cpp:1179
std::pair< milliseconds, milliseconds >
ripple::test::OverlaySim::peersCache_
Peers peersCache_
Definition: reduce_relay_test.cpp:666
ripple::test::Network::validator
Validator & validator(std::uint16_t v)
Definition: reduce_relay_test.cpp:731
ripple::test::Network::overlay_
OverlaySim overlay_
Definition: reduce_relay_test.cpp:855
ripple::test::reduce_relay_test::Event::validator_
std::uint16_t validator_
Definition: reduce_relay_test.cpp:910
ripple::Config::REDUCE_RELAY_ENABLE
bool REDUCE_RELAY_ENABLE
Definition: Config.h:197
ripple::Peer::id_t
std::uint32_t id_t
Uniquely identifies a peer.
Definition: ripple/overlay/Peer.h:53
ripple::test::reduce_relay_test::printPeers
void printPeers(const std::string &msg, std::uint16_t validator=0)
Definition: reduce_relay_test.cpp:866
ripple::test::jtx::validator
std::unique_ptr< Config > validator(std::unique_ptr< Config >, std::string const &)
adjust configuration with params needed to be a validator
Definition: envconfig.cpp:89
std::vector
STL class.
std::find_if
T find_if(T... args)
ripple::squelch::SlotState::Counting
@ Counting
std::back_inserter
T back_inserter(T... args)
ripple::test::PeerPartial::supportsFeature
bool supportsFeature(ProtocolFeature f) const override
Definition: reduce_relay_test.cpp:115
ripple::test::PeerPartial::getScore
int getScore(bool) const override
Definition: reduce_relay_test.cpp:99
std::chrono::duration
ripple::test::PeerPartial::hasShard
bool hasShard(std::uint32_t shardIndex) const override
Definition: reduce_relay_test.cpp:144
ripple::test::PeerPartial::setPublisherListSequence
void setPublisherListSequence(PublicKey const &, std::size_t const) override
Definition: reduce_relay_test.cpp:125
ripple::test::Validator::id
std::uint16_t id()
Definition: reduce_relay_test.cpp:407
ripple::test::OverlaySim::addPeer
PeerSPtr addPeer(bool useCache=true)
Definition: reduce_relay_test.cpp:542
std::stringstream
STL class.
ripple::test::PeerPartial::cluster
bool cluster() const override
Returns true if this connection is a member of the cluster.
Definition: reduce_relay_test.cpp:89
ripple::test::MAX_VALIDATORS
static constexpr std::uint32_t MAX_VALIDATORS
Definition: reduce_relay_test.cpp:52
std::tuple
ripple::test::reduce_relay_test::testSelectedPeerDisconnects
void testSelectedPeerDisconnects(bool log)
Selected peer disconnects.
Definition: reduce_relay_test.cpp:1222
std::function< void(PublicKey const &, PeerWPtr const &, std::uint32_t)>
ripple::test::OverlaySim::getPeers
std::unordered_map< id_t, std::tuple< squelch::PeerState, std::uint16_t, std::uint32_t, std::uint32_t > > getPeers(PublicKey const &validator)
Definition: reduce_relay_test.cpp:636
ripple::test::ManualClock::time_point
std::chrono::time_point< ManualClock > time_point
Definition: reduce_relay_test.cpp:176
ripple::squelch::SquelchHandler
Abstract class.
Definition: overlay/Slot.h:71
ripple::ProtocolFeature
ProtocolFeature
Definition: ripple/overlay/Peer.h:38
ripple::test::OverlaySim::slots_
squelch::Slots< ManualClock > slots_
Definition: reduce_relay_test.cpp:667
ripple::test::Validator::linkUp
void linkUp(Peer::id_t id)
Definition: reduce_relay_test.cpp:413
ripple::test::OverlaySim::resetPeers
void resetPeers()
Definition: reduce_relay_test.cpp:574
ripple::test::Validator::for_links
void for_links(LinkIterCB f, bool simulateSlow=false)
Definition: reduce_relay_test.cpp:369
ripple::test::ManualClock::duration
std::chrono::duration< std::uint32_t, period > duration
Definition: reduce_relay_test.cpp:175
ripple::squelch::PeerState
PeerState
Peer's State.
Definition: overlay/Slot.h:49
ripple::Config::REDUCE_RELAY_SQUELCH
bool REDUCE_RELAY_SQUELCH
Definition: Config.h:199
ripple::test::reduce_relay_test::checkCounting
bool checkCounting(PublicKey const &validator, bool isCountingState)
Definition: reduce_relay_test.cpp:1092
ripple::test::PeerPartial::getNodePublic
PublicKey const & getNodePublic() const override
Definition: reduce_relay_test.cpp:104
ripple::test::PeerSim::PeerSim
PeerSim(Overlay &overlay)
Definition: reduce_relay_test.cpp:440
ripple::test::Overlay
Simulate server's OverlayImpl.
Definition: reduce_relay_test.cpp:216
ripple::test::PeerSim::onMessage
virtual void onMessage(protocol::TMSquelch const &squelch) override
Remote Peer (Directly connected Peer)
Definition: reduce_relay_test.cpp:473
ripple::test::PeerPartial::json
Json::Value json() override
Definition: reduce_relay_test.cpp:110
std::mt19937
std::cout
ripple::KeyType::ed25519
@ ed25519
ripple::base_uint< 256 >
ripple::test::PeerSim::onMessage
void onMessage(MessageSPtr const &m, SquelchCB f) override
Local Peer (PeerImp)
Definition: reduce_relay_test.cpp:461
ripple::test::reduce_relay_test::random
void random(bool log)
Randomly brings the link between a validator and a peer down.
Definition: reduce_relay_test.cpp:920
ripple::test::reduce_relay_test::Event::peer_
Peer::id_t peer_
Definition: reduce_relay_test.cpp:909
ripple::test::PeerPartial::publisherListSequence
boost::optional< std::size_t > publisherListSequence(PublicKey const &) const override
Definition: reduce_relay_test.cpp:120
ripple::test::reduce_relay_test::Event::key_
PublicKey key_
Definition: reduce_relay_test.cpp:911
ripple::test::PeerSim::id_
id_t id_
Definition: reduce_relay_test.cpp:482
ripple::test::Network::propagate
void propagate(LinkIterCB link, std::uint16_t nValidators=MAX_VALIDATORS, std::uint32_t nMessages=MAX_MESSAGES, bool purge=true, bool resetClock=true)
Definition: reduce_relay_test.cpp:794
ripple::test::Network::reset
void reset()
Definition: reduce_relay_test.cpp:693
ripple::test::PeerPartial::compressionEnabled
bool compressionEnabled() const override
Definition: reduce_relay_test.cpp:163
ripple::rand_int
std::enable_if_t< std::is_integral< Integral >::value &&detail::is_engine< Engine >::value, Integral > rand_int(Engine &engine, Integral min, Integral max)
Return a uniformly distributed random integer.
Definition: ripple/basics/random.h:115
ripple::Config::loadFromString
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
Definition: Config.cpp:339
ripple::test::Network::allCounting
bool allCounting(Peer::id_t peer)
Check if there are peers to unsquelch - peer is in Selected state in any of the slots and there are p...
Definition: reduce_relay_test.cpp:836
std::milli
std::random_device
ripple::test::reduce_relay_test::EventType
EventType
Definition: reduce_relay_test.cpp:898
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::Config
Definition: Config.h:69
ripple::test::reduce_relay_test::propagateAndSquelch
bool propagateAndSquelch(bool log, bool purge=true, bool resetClock=true)
Propagate enough messages to generate one squelch event.
Definition: reduce_relay_test.cpp:1144
ripple::PublicKey::size
std::size_t size() const noexcept
Definition: PublicKey.h:87
ripple::test::PeerPartial::send
void send(std::shared_ptr< Message > const &m) override
Definition: reduce_relay_test.cpp:76
ripple::test::OverlaySim::inState
std::uint16_t inState(PublicKey const &validator, squelch::PeerState state)
Definition: reduce_relay_test.cpp:509
ripple::test::Network::overlay
OverlaySim & overlay()
Definition: reduce_relay_test.cpp:738
ripple::test::Network::addPeer
Peer::id_t addPeer()
Definition: reduce_relay_test.cpp:703
ripple::test::OverlaySim::clear
void clear()
Definition: reduce_relay_test.cpp:501
ripple::test::OverlaySim::deletePeer
void deletePeer(Peer::id_t id, bool useCache=true)
Definition: reduce_relay_test.cpp:563
ripple::squelch::MAX_MESSAGE_THRESHOLD
static constexpr uint16_t MAX_MESSAGE_THRESHOLD
Definition: SquelchCommon.h:41
ripple::test::Validator::linkDown
void linkDown(Peer::id_t id)
Definition: reduce_relay_test.cpp:421
ripple::test::Validator::send
void send(std::vector< Peer::id_t > peers, SquelchCB f)
Send to specific peers.
Definition: reduce_relay_test.cpp:388
ripple::test::OverlaySim::isCountingState
bool isCountingState(PublicKey const &validator)
Definition: reduce_relay_test.cpp:603
std::enable_shared_from_this
ripple::test::PeerPartial::ledgerRange
void ledgerRange(std::uint32_t &minSeq, std::uint32_t &maxSeq) const override
Definition: reduce_relay_test.cpp:140
std::chrono::time_point
ripple::test::Validator::deletePeer
void deletePeer(Peer::id_t id)
Definition: reduce_relay_test.cpp:353
ripple::test::OverlaySim::deletePeer
void deletePeer(id_t id, UnsquelchCB f) override
Definition: reduce_relay_test.cpp:528
ripple::test::Network::onDisconnectPeer
void onDisconnectPeer(Peer::id_t peer)
Definition: reduce_relay_test.cpp:758
std::uint32_t
ripple::test::OverlaySim::OverlaySim
OverlaySim(Application &app)
Definition: reduce_relay_test.cpp:494
ripple::test::PeerPartial::hasRange
bool hasRange(std::uint32_t uMin, std::uint32_t uMax) override
Definition: reduce_relay_test.cpp:158
ripple::test::ManualClock
Manually advanced clock.
Definition: reduce_relay_test.cpp:170
std::transform
T transform(T... args)
ripple::test::Network::enableLink
void enableLink(std::uint16_t validatorId, Peer::id_t peer, bool enable)
Definition: reduce_relay_test.cpp:744
ripple::test::reduce_relay_test::testSelectedPeerStopsRelaying
void testSelectedPeerStopsRelaying(bool log)
Selected peer stops relaying.
Definition: reduce_relay_test.cpp:1242
ripple::test::ManualClock::period
std::milli period
Definition: reduce_relay_test.cpp:174
ripple::test::MAX_MESSAGES
static constexpr std::uint32_t MAX_MESSAGES
Definition: reduce_relay_test.cpp:53
ripple::test::reduce_relay_test::Event
Definition: reduce_relay_test.cpp:903
ripple::test::jtx::fee
Set the fee on a JTx.
Definition: fee.h:34
ripple::test::PeerPartial::hasTxSet
bool hasTxSet(uint256 const &hash) const override
Definition: reduce_relay_test.cpp:149
ripple::test::Network::purgePeers
void purgePeers()
Definition: reduce_relay_test.cpp:724
ripple::test::reduce_relay_test::sendSquelch
Peer::id_t sendSquelch(PublicKey const &validator, PeerWPtr const &peerPtr, std::optional< std::uint32_t > duration)
Send squelch (if duration is set) or unsquelch (if duration not set)
Definition: reduce_relay_test.cpp:880
ripple::test::OverlaySim::updateSlotAndSquelch
void updateSlotAndSquelch(uint256 const &key, PublicKey const &validator, Peer::id_t id, SquelchCB f, protocol::MessageType type=protocol::mtVALIDATION) override
Definition: reduce_relay_test.cpp:516
std::weak_ptr< Peer >
ripple::test::Network::for_rand
void for_rand(std::uint32_t min, std::uint32_t max, std::function< void(std::uint32_t)> f)
Definition: reduce_relay_test.cpp:778
ripple::test::OverlaySim::deleteLastPeer
std::optional< Peer::id_t > deleteLastPeer()
Definition: reduce_relay_test.cpp:583
ripple::test::Network::Network
Network(Application &app)
Definition: reduce_relay_test.cpp:673
ripple::test::Validator::Validator
Validator()
Definition: reduce_relay_test.cpp:303
ripple::test::PeerPartial::hasLedger
bool hasLedger(uint256 const &hash, std::uint32_t seq) const override
Definition: reduce_relay_test.cpp:135
ripple::randomKeyPair
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Definition: SecretKey.cpp:260
ripple::test::reduce_relay_simulate_test
Definition: reduce_relay_test.cpp:1393
ripple::test::Validator::send
void send(SquelchCB f)
Send to all peers.
Definition: reduce_relay_test.cpp:395
ripple::test::PeerPartial::isHighLatency
bool isHighLatency() const override
Definition: reduce_relay_test.cpp:94
ripple::test::OverlaySim::squelch
void squelch(PublicKey const &validator, Peer::id_t id, std::uint32_t squelchDuration) const override
Squelch handler.
Definition: reduce_relay_test.cpp:649
ripple::test::jtx::seq
Set the sequence number on a JTx.
Definition: seq.h:32
ripple::test::reduce_relay_test::testNewPeer
void testNewPeer(bool log)
Receiving a message from new peer should change the slot's state to Counting.
Definition: reduce_relay_test.cpp:1210
ripple::test::PeerSim::id
id_t id() const override
Definition: reduce_relay_test.cpp:448
ripple::test::reduce_relay_test::testPeerUnsquelchedTooSoon
void testPeerUnsquelchedTooSoon(bool log)
Receiving message from squelched peer too soon should not change the slot's state to Counting.
Definition: reduce_relay_test.cpp:1123
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::reduce_relay_simulate_test::run
void run() override
Definition: reduce_relay_test.cpp:1402
ripple::test::reduce_relay_test::env_
jtx::Env env_
Definition: reduce_relay_test.cpp:1369
ripple::test::Validator::addPeer
void addPeer(PeerSPtr peer)
Definition: reduce_relay_test.cpp:346
ripple::test::Network::deleteLastPeer
void deleteLastPeer()
Definition: reduce_relay_test.cpp:712
std::endl
T endl(T... args)
ripple::test::PeerPartial::send
void send(protocol::TMSquelch const &squelch)
Definition: reduce_relay_test.cpp:69
ripple::test::reduce_relay_test::doTest
void doTest(const std::string &msg, bool log, std::function< void(bool)> f)
Definition: reduce_relay_test.cpp:1100
ripple::test::ManualClock::reset
static void reset() noexcept
Definition: reduce_relay_test.cpp:192
ripple::test::PeerSim::squelch_
squelch::Squelch< ManualClock > squelch_
Definition: reduce_relay_test.cpp:484
ripple::squelch::Squelch
Maintains squelching of relaying messages from validators.
Definition: Squelch.h:36
ripple::squelch::IDLED
static constexpr seconds IDLED
Definition: SquelchCommon.h:35
std::vector::begin
T begin(T... args)
ripple::test::Validator::~Validator
~Validator()
Definition: reduce_relay_test.cpp:317
ripple::test::reduce_relay_test::testInitialRound
void testInitialRound(bool log)
Initial counting round: three peers receive message "faster" then others.
Definition: reduce_relay_test.cpp:1112
std::iota
T iota(T... args)
ripple::Resource::Charge
A consumption charge.
Definition: Charge.h:30
ripple::test::Validator::links_
Links links_
Definition: reduce_relay_test.cpp:429
ripple::test::PeerPartial::charge
void charge(Resource::Charge const &fee) override
Adjust this peer's load balance based on the type of load imposed.
Definition: reduce_relay_test.cpp:85
ripple::test::PeerSim
Definition: reduce_relay_test.cpp:436
ripple::test::PeerPartial::getRemoteAddress
beast::IP::Endpoint getRemoteAddress() const override
Definition: reduce_relay_test.cpp:80
std::chrono::milliseconds::count
T count(T... args)
ripple::test::PeerPartial::~PeerPartial
virtual ~PeerPartial()
Definition: reduce_relay_test.cpp:61
ripple::test::OverlaySim::unsquelch_
UnsquelchCB unsquelch_
Definition: reduce_relay_test.cpp:664
ripple::test::MessageSPtr
std::shared_ptr< Message > MessageSPtr
Definition: reduce_relay_test.cpp:42
optional
ripple::test::PeerPartial
Simulate two entities - peer directly connected to the server (via squelch in PeerSim) and PeerImp (v...
Definition: reduce_relay_test.cpp:58
ripple::test::Network::validators_
std::vector< Validator > validators_
Definition: reduce_relay_test.cpp:856
std::stringstream::str
T str(T... args)
ripple::test::MAX_PEERS
static constexpr std::uint32_t MAX_PEERS
Definition: reduce_relay_test.cpp:51
std::size_t
std::make_pair
T make_pair(T... args)
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
ripple::squelch::Slots
Slots is a container for validator's Slot and handles Slot update when a message is received from a v...
Definition: overlay/Slot.h:46
std::vector::end
T end(T... args)
ripple::test::OverlaySim::getSelectedPeer
id_t getSelectedPeer(PublicKey const &validator)
Definition: reduce_relay_test.cpp:622
ripple::squelch::Slot
Slot is associated with a specific validator via validator's public key.
Definition: overlay/Slot.h:104
ripple::test::OverlaySim
Definition: reduce_relay_test.cpp:487
ripple::test::ManualClock::rep
uint64_t rep
Definition: reduce_relay_test.cpp:173
ripple::test::ManualClock::now
static time_point now() noexcept
Definition: reduce_relay_test.cpp:198
ripple::test::reduce_relay_test::Event::time_
time_point< ManualClock > time_
Definition: reduce_relay_test.cpp:912
numeric
ripple::test::Validator::key
PublicKey const & key()
Definition: reduce_relay_test.cpp:335
ripple::test::reduce_relay_test::State
State
Definition: reduce_relay_test.cpp:897
ripple::test::Validator::resetId
static void resetId()
Definition: reduce_relay_test.cpp:329
ripple::test::Validator::for_links
void for_links(std::vector< Peer::id_t > peers, LinkIterCB f)
Definition: reduce_relay_test.cpp:359
std::shuffle
T shuffle(T... args)
ripple::test::ManualClock::advance
static void advance(duration d) noexcept
Definition: reduce_relay_test.cpp:180
std::unordered_map< Peer::id_t, LinkSPtr >
ripple::test::reduce_relay_test::testSquelchedPeerDisconnects
void testSquelchedPeerDisconnects(bool log)
Squelched peer disconnects.
Definition: reduce_relay_test.cpp:1263
ripple::squelch::PeerState::Squelched
@ Squelched
std::set
STL class.
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:115
ripple::test::reduce_relay_test::testPeerUnsquelched
void testPeerUnsquelched(bool log)
Receiving message from squelched peer should change the slot's state to Counting.
Definition: reduce_relay_test.cpp:1134
ripple::test::OverlaySim::getSelected
std::set< id_t > getSelected(PublicKey const &validator)
Definition: reduce_relay_test.cpp:609
ripple::test::Network::init
void init()
Definition: reduce_relay_test.cpp:679
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::test::OverlaySim::squelch_
SquelchCB squelch_
Definition: reduce_relay_test.cpp:663
ripple::test::reduce_relay_simulate_test::testRandom
void testRandom(bool log)
Definition: reduce_relay_test.cpp:1396
ripple::Peer
Represents a peer connection in the overlay.
Definition: ripple/overlay/Peer.h:43
std::chrono