rippled
Squelch.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 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 
20 #ifndef RIPPLE_OVERLAY_SQUELCH_H_INCLUDED
21 #define RIPPLE_OVERLAY_SQUELCH_H_INCLUDED
22 
23 #include <ripple/basics/random.h>
24 #include <ripple/overlay/SquelchCommon.h>
25 #include <ripple/protocol/PublicKey.h>
26 
27 #include <chrono>
28 #include <functional>
29 
30 namespace ripple {
31 
32 namespace squelch {
33 
35 template <typename clock_type>
36 class Squelch
37 {
38  using time_point = typename clock_type::time_point;
39 
40 public:
41  Squelch() = default;
42  virtual ~Squelch() = default;
43 
49  void
50  squelch(PublicKey const& validator, bool squelch, uint64_t squelchDuration);
51 
56  bool
57  isSquelched(PublicKey const& validator);
58 
61  static seconds
63 
64 private:
68 };
69 
70 template <typename clock_type>
71 void
73  PublicKey const& validator,
74  bool squelch,
75  uint64_t squelchDuration)
76 {
77  if (squelch)
78  {
79  squelched_[validator] = [squelchDuration]() {
80  seconds duration = seconds(squelchDuration);
81  return clock_type::now() +
84  ? duration
85  : getSquelchDuration());
86  }();
87  }
88  else
89  squelched_.erase(validator);
90 }
91 
92 template <typename clock_type>
93 bool
95 {
96  auto now = clock_type::now();
97 
98  auto const& it = squelched_.find(validator);
99  if (it == squelched_.end())
100  return false;
101  else if (it->second > now)
102  return true;
103 
104  squelched_.erase(it);
105 
106  return false;
107 }
108 
109 template <typename clock_type>
110 seconds
112 {
113  auto d = seconds(ripple::rand_int(
115  return d;
116 }
117 
118 } // namespace squelch
119 
120 } // namespace ripple
121 
122 #endif // RIPPLED_SQUELCH_H
ripple::squelch::Squelch::squelched_
hash_map< PublicKey, time_point > squelched_
Maintains the list of squelched relaying to downstream peers.
Definition: Squelch.h:67
ripple::squelch::MAX_UNSQUELCH_EXPIRE
static constexpr seconds MAX_UNSQUELCH_EXPIRE
Definition: SquelchCommon.h:33
functional
std::chrono::seconds
ripple::squelch::Squelch::Squelch
Squelch()=default
ripple::squelch::Squelch::getSquelchDuration
static seconds getSquelchDuration()
Get random squelch duration between MIN_UNSQUELCH_EXPIRE and MAX_UNSQUELCH_EXPIRE.
Definition: Squelch.h:111
ripple::squelch::MIN_UNSQUELCH_EXPIRE
static constexpr seconds MIN_UNSQUELCH_EXPIRE
Definition: SquelchCommon.h:32
ripple::squelch::Squelch< ripple::UptimeClock >::time_point
typename ripple::UptimeClock ::time_point time_point
Definition: Squelch.h:38
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::PublicKey
A public key.
Definition: PublicKey.h:59
chrono
ripple::squelch::Squelch::isSquelched
bool isSquelched(PublicKey const &validator)
Are the messages to this validator squelched.
Definition: Squelch.h:94
ripple::squelch::Squelch::~Squelch
virtual ~Squelch()=default
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::squelch::Squelch
Maintains squelching of relaying messages from validators.
Definition: Squelch.h:36
std::chrono::seconds::count
T count(T... args)
std::unordered_map
STL class.
ripple::squelch::Squelch::squelch
void squelch(PublicKey const &validator, bool squelch, uint64_t squelchDuration)
Squelch/Unsquelch relaying for the validator.
Definition: Squelch.h:72