xrpld
Loading...
Searching...
No Matches
Squelch.h
1#pragma once
2
3#include <xrpld/overlay/ReduceRelayCommon.h>
4
5#include <xrpl/basics/Log.h>
6#include <xrpl/basics/UnorderedContainers.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/protocol/PublicKey.h>
9
10#include <chrono>
11
12namespace xrpl::reduce_relay {
13
17template <typename ClockType>
19{
20 using time_point = ClockType::time_point;
21
22public:
23 explicit Squelch(beast::Journal journal) : journal_(journal)
24 {
25 }
26 virtual ~Squelch() = default;
27
34 bool
35 addSquelch(PublicKey const& validator, std::chrono::seconds const& squelchDuration);
36
41 void
42 removeSquelch(PublicKey const& validator);
43
49 bool
50 expireSquelch(PublicKey const& validator);
51
52private:
59};
60
61template <typename ClockType>
62bool
64 PublicKey const& validator,
65 std::chrono::seconds const& squelchDuration)
66{
67 if (squelchDuration >= kMinUnsquelchExpire && squelchDuration <= kMaxUnsquelchExpirePeers)
68 {
69 squelched_[validator] = ClockType::now() + squelchDuration;
70 return true;
71 }
72
73 JLOG(journal_.error()) << "squelch: invalid squelch duration " << squelchDuration.count();
74
75 // unsquelch if invalid duration
76 removeSquelch(validator);
77
78 return false;
79}
80
81template <typename ClockType>
82void
84{
85 squelched_.erase(validator);
86}
87
88template <typename ClockType>
89bool
91{
92 auto now = ClockType::now();
93
94 auto const& it = squelched_.find(validator);
95 if (it == squelched_.end())
96 return true;
97 if (it->second > now)
98 return false;
99
100 // squelch expired
101 squelched_.erase(it);
102
103 return true;
104}
105
106} // namespace xrpl::reduce_relay
A generic endpoint for log messages.
Definition Journal.h:44
A public key.
Definition PublicKey.h:53
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:57
ClockType::time_point time_point
Definition Squelch.h:20
void removeSquelch(PublicKey const &validator)
Remove the squelch.
Definition Squelch.h:83
bool expireSquelch(PublicKey const &validator)
Remove expired squelch.
Definition Squelch.h:90
beast::Journal const journal_
Definition Squelch.h:58
bool addSquelch(PublicKey const &validator, std::chrono::seconds const &squelchDuration)
Squelch validation/proposal relaying for the validator.
Definition Squelch.h:63
virtual ~Squelch()=default
T count(T... args)
static constexpr auto kMinUnsquelchExpire
static constexpr auto kMaxUnsquelchExpirePeers
std::unordered_map< Key, Value, Hash, Pred, Allocator > hash_map