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