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/beast/utility/Journal.h>
25 #include <ripple/overlay/ReduceRelayCommon.h>
26 #include <ripple/protocol/PublicKey.h>
27 
28 #include <algorithm>
29 #include <chrono>
30 #include <functional>
31 
32 namespace ripple {
33 
34 namespace reduce_relay {
35 
37 template <typename clock_type>
38 class Squelch
39 {
40  using time_point = typename clock_type::time_point;
41 
42 public:
43  explicit Squelch(beast::Journal journal) : journal_(journal)
44  {
45  }
46  virtual ~Squelch() = default;
47 
53  bool
54  addSquelch(
55  PublicKey const& validator,
56  std::chrono::seconds const& squelchDuration);
57 
61  void
62  removeSquelch(PublicKey const& validator);
63 
68  bool
69  expireSquelch(PublicKey const& validator);
70 
71 private:
76 };
77 
78 template <typename clock_type>
79 bool
81  PublicKey const& validator,
82  std::chrono::seconds const& squelchDuration)
83 {
84  if (squelchDuration >= MIN_UNSQUELCH_EXPIRE &&
85  squelchDuration <= MAX_UNSQUELCH_EXPIRE_PEERS)
86  {
87  squelched_[validator] = clock_type::now() + squelchDuration;
88  return true;
89  }
90 
91  JLOG(journal_.error()) << "squelch: invalid squelch duration "
92  << squelchDuration.count();
93 
94  // unsquelch if invalid duration
95  removeSquelch(validator);
96 
97  return false;
98 }
99 
100 template <typename clock_type>
101 void
103 {
104  squelched_.erase(validator);
105 }
106 
107 template <typename clock_type>
108 bool
110 {
111  auto now = clock_type::now();
112 
113  auto const& it = squelched_.find(validator);
114  if (it == squelched_.end())
115  return true;
116  else if (it->second > now)
117  return false;
118 
119  // squelch expired
120  squelched_.erase(it);
121 
122  return true;
123 }
124 
125 } // namespace reduce_relay
126 
127 } // namespace ripple
128 
129 #endif // RIPPLED_SQUELCH_H
ripple::reduce_relay::Squelch< ripple::UptimeClock >::time_point
typename ripple::UptimeClock ::time_point time_point
Definition: Squelch.h:40
ripple::reduce_relay::Squelch::~Squelch
virtual ~Squelch()=default
functional
ripple::reduce_relay::Squelch::removeSquelch
void removeSquelch(PublicKey const &validator)
Remove the squelch.
Definition: Squelch.h:102
ripple::reduce_relay::Squelch::addSquelch
bool addSquelch(PublicKey const &validator, std::chrono::seconds const &squelchDuration)
Squelch validation/proposal relaying for the validator.
Definition: Squelch.h:80
ripple::reduce_relay::Squelch::expireSquelch
bool expireSquelch(PublicKey const &validator)
Remove expired squelch.
Definition: Squelch.h:109
std::chrono::seconds
algorithm
ripple::reduce_relay::Squelch
Maintains squelching of relaying messages from validators.
Definition: Squelch.h:38
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
chrono
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::reduce_relay::MIN_UNSQUELCH_EXPIRE
static constexpr auto MIN_UNSQUELCH_EXPIRE
Definition: ReduceRelayCommon.h:34
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::chrono::seconds::count
T count(T... args)
ripple::reduce_relay::Squelch::journal_
const beast::Journal journal_
Definition: Squelch.h:75
ripple::reduce_relay::MAX_UNSQUELCH_EXPIRE_PEERS
static constexpr auto MAX_UNSQUELCH_EXPIRE_PEERS
Definition: ReduceRelayCommon.h:37
ripple::reduce_relay::Squelch::squelched_
hash_map< PublicKey, time_point > squelched_
Maintains the list of squelched relaying to downstream peers.
Definition: Squelch.h:74
std::unordered_map
STL class.
ripple::reduce_relay::Squelch::Squelch
Squelch(beast::Journal journal)
Definition: Squelch.h:43