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
58Squelch<clock_type>::addSquelch(PublicKey const& validator, std::chrono::seconds const& squelchDuration)
59{
60 if (squelchDuration >= MIN_UNSQUELCH_EXPIRE && squelchDuration <= MAX_UNSQUELCH_EXPIRE_PEERS)
61 {
62 squelched_[validator] = clock_type::now() + squelchDuration;
63 return true;
64 }
65
66 JLOG(journal_.error()) << "squelch: invalid squelch duration " << squelchDuration.count();
67
68 // unsquelch if invalid duration
69 removeSquelch(validator);
70
71 return false;
72}
73
74template <typename clock_type>
75void
77{
78 squelched_.erase(validator);
79}
80
81template <typename clock_type>
82bool
84{
85 auto now = clock_type::now();
86
87 auto const& it = squelched_.find(validator);
88 if (it == squelched_.end())
89 return true;
90 else if (it->second > now)
91 return false;
92
93 // squelch expired
94 squelched_.erase(it);
95
96 return true;
97}
98
99} // namespace reduce_relay
100
101} // 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:76
bool expireSquelch(PublicKey const &validator)
Remove expired squelch.
Definition Squelch.h:83
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