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/beast/utility/Journal.h>
7#include <xrpl/protocol/PublicKey.h>
8
9#include <chrono>
10
11namespace xrpl::reduce_relay {
12
14template <typename ClockType>
16{
17 using time_point = ClockType::time_point;
18
19public:
20 explicit Squelch(beast::Journal journal) : journal_(journal)
21 {
22 }
23 virtual ~Squelch() = default;
24
30 bool
31 addSquelch(PublicKey const& validator, std::chrono::seconds const& squelchDuration);
32
36 void
37 removeSquelch(PublicKey const& validator);
38
43 bool
44 expireSquelch(PublicKey const& validator);
45
46private:
51};
52
53template <typename ClockType>
54bool
56 PublicKey const& validator,
57 std::chrono::seconds const& squelchDuration)
58{
59 if (squelchDuration >= kMinUnsquelchExpire && squelchDuration <= kMaxUnsquelchExpirePeers)
60 {
61 squelched_[validator] = ClockType::now() + squelchDuration;
62 return true;
63 }
64
65 JLOG(journal_.error()) << "squelch: invalid squelch duration " << squelchDuration.count();
66
67 // unsquelch if invalid duration
68 removeSquelch(validator);
69
70 return false;
71}
72
73template <typename ClockType>
74void
76{
77 squelched_.erase(validator);
78}
79
80template <typename ClockType>
81bool
83{
84 auto now = ClockType::now();
85
86 auto const& it = squelched_.find(validator);
87 if (it == squelched_.end())
88 return true;
89 if (it->second > now)
90 return false;
91
92 // squelch expired
93 squelched_.erase(it);
94
95 return true;
96}
97
98} // namespace xrpl::reduce_relay
A generic endpoint for log messages.
Definition Journal.h:38
A public key.
Definition PublicKey.h:42
Squelch(beast::Journal journal)
Definition Squelch.h:20
hash_map< PublicKey, time_point > squelched_
Maintains the list of squelched relaying to downstream peers.
Definition Squelch.h:49
ClockType::time_point time_point
Definition Squelch.h:17
void removeSquelch(PublicKey const &validator)
Remove the squelch.
Definition Squelch.h:75
bool expireSquelch(PublicKey const &validator)
Remove expired squelch.
Definition Squelch.h:82
beast::Journal const journal_
Definition Squelch.h:50
bool addSquelch(PublicKey const &validator, std::chrono::seconds const &squelchDuration)
Squelch validation/proposal relaying for the validator.
Definition Squelch.h:55
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