rippled
Loading...
Searching...
No Matches
Squelch.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2020 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_OVERLAY_SQUELCH_H_INCLUDED
21#define RIPPLE_OVERLAY_SQUELCH_H_INCLUDED
22
23#include <xrpld/overlay/ReduceRelayCommon.h>
24
25#include <xrpl/beast/utility/Journal.h>
26#include <xrpl/protocol/PublicKey.h>
27
28#include <algorithm>
29#include <chrono>
30#include <functional>
31
32namespace ripple {
33
34namespace reduce_relay {
35
37template <typename clock_type>
39{
40 using time_point = typename clock_type::time_point;
41
42public:
43 explicit Squelch(beast::Journal journal) : journal_(journal)
44 {
45 }
46 virtual ~Squelch() = default;
47
53 bool
55 PublicKey const& validator,
56 std::chrono::seconds const& squelchDuration);
57
61 void
62 removeSquelch(PublicKey const& validator);
63
68 bool
69 expireSquelch(PublicKey const& validator);
70
71private:
76};
77
78template <typename clock_type>
79bool
81 PublicKey const& validator,
82 std::chrono::seconds const& squelchDuration)
83{
84 if (squelchDuration >= MIN_UNSQUELCH_EXPIRE &&
85 squelchDuration <= MAX_UNSQUELCH_EXPIRE_PEERS)
86 {
87 squelched_[validator] = clock_type::now() + squelchDuration;
88 return true;
89 }
90
91 JLOG(journal_.error()) << "squelch: invalid squelch duration "
92 << squelchDuration.count();
93
94 // unsquelch if invalid duration
95 removeSquelch(validator);
96
97 return false;
98}
99
100template <typename clock_type>
101void
103{
104 squelched_.erase(validator);
105}
106
107template <typename clock_type>
108bool
110{
111 auto now = clock_type::now();
112
113 auto const& it = squelched_.find(validator);
114 if (it == squelched_.end())
115 return true;
116 else if (it->second > now)
117 return false;
118
119 // squelch expired
120 squelched_.erase(it);
121
122 return true;
123}
124
125} // namespace reduce_relay
126
127} // namespace ripple
128
129#endif // RIPPLED_SQUELCH_H
A generic endpoint for log messages.
Definition Journal.h:60
A public key.
Definition PublicKey.h:61
Maintains squelching of relaying messages from validators.
Definition Squelch.h:39
typename clock_type::time_point time_point
Definition Squelch.h:40
beast::Journal const journal_
Definition Squelch.h:75
hash_map< PublicKey, time_point > squelched_
Maintains the list of squelched relaying to downstream peers.
Definition Squelch.h:74
virtual ~Squelch()=default
bool addSquelch(PublicKey const &validator, std::chrono::seconds const &squelchDuration)
Squelch validation/proposal relaying for the validator.
Definition Squelch.h:80
bool expireSquelch(PublicKey const &validator)
Remove expired squelch.
Definition Squelch.h:109
void removeSquelch(PublicKey const &validator)
Remove the squelch.
Definition Squelch.h:102
Squelch(beast::Journal journal)
Definition Squelch.h:43
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:25