rippled
Loading...
Searching...
No Matches
Squelch.h
1#ifndef XRPL_OVERLAY_SQUELCH_H_INCLUDED
2#define XRPL_OVERLAY_SQUELCH_H_INCLUDED
3
4#include <xrpld/overlay/ReduceRelayCommon.h>
5
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/protocol/PublicKey.h>
8
9#include <algorithm>
10#include <chrono>
11#include <functional>
12
13namespace ripple {
14
15namespace reduce_relay {
16
18template <typename clock_type>
20{
21 using time_point = typename clock_type::time_point;
22
23public:
24 explicit Squelch(beast::Journal journal) : journal_(journal)
25 {
26 }
27 virtual ~Squelch() = default;
28
34 bool
36 PublicKey const& validator,
37 std::chrono::seconds const& squelchDuration);
38
42 void
43 removeSquelch(PublicKey const& validator);
44
49 bool
50 expireSquelch(PublicKey const& validator);
51
52private:
57};
58
59template <typename clock_type>
60bool
62 PublicKey const& validator,
63 std::chrono::seconds const& squelchDuration)
64{
65 if (squelchDuration >= MIN_UNSQUELCH_EXPIRE &&
66 squelchDuration <= MAX_UNSQUELCH_EXPIRE_PEERS)
67 {
68 squelched_[validator] = clock_type::now() + squelchDuration;
69 return true;
70 }
71
72 JLOG(journal_.error()) << "squelch: invalid squelch duration "
73 << squelchDuration.count();
74
75 // unsquelch if invalid duration
76 removeSquelch(validator);
77
78 return false;
79}
80
81template <typename clock_type>
82void
84{
85 squelched_.erase(validator);
86}
87
88template <typename clock_type>
89bool
91{
92 auto now = clock_type::now();
93
94 auto const& it = squelched_.find(validator);
95 if (it == squelched_.end())
96 return true;
97 else if (it->second > now)
98 return false;
99
100 // squelch expired
101 squelched_.erase(it);
102
103 return true;
104}
105
106} // namespace reduce_relay
107
108} // namespace ripple
109
110#endif // XRPL_SQUELCH_H
A generic endpoint for log messages.
Definition Journal.h:41
A public key.
Definition PublicKey.h:43
Maintains squelching of relaying messages from validators.
Definition Squelch.h:20
typename clock_type::time_point time_point
Definition Squelch.h:21
beast::Journal const journal_
Definition Squelch.h:56
hash_map< PublicKey, time_point > squelched_
Maintains the list of squelched relaying to downstream peers.
Definition Squelch.h:55
virtual ~Squelch()=default
bool addSquelch(PublicKey const &validator, std::chrono::seconds const &squelchDuration)
Squelch validation/proposal relaying for the validator.
Definition Squelch.h:61
bool expireSquelch(PublicKey const &validator)
Remove expired squelch.
Definition Squelch.h:90
void removeSquelch(PublicKey const &validator)
Remove the squelch.
Definition Squelch.h:83
Squelch(beast::Journal journal)
Definition Squelch.h:24
T count(T... args)
static constexpr auto MIN_UNSQUELCH_EXPIRE
static constexpr auto MAX_UNSQUELCH_EXPIRE_PEERS
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6