xrpld
Loading...
Searching...
No Matches
HashRouter.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/UnorderedContainers.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/chrono.h>
7#include <xrpl/beast/container/aged_unordered_map.h>
8
9#include <optional>
10#include <set>
11
12namespace xrpl {
13
15 // Public flags
16 UNDEFINED = 0x00,
17 BAD = 0x02, // Temporarily bad
18 SAVED = 0x04,
19 HELD = 0x08, // Held by LedgerMaster after potential processing failure
20 TRUSTED = 0x10, // Comes from a trusted source
21
22 // Private flags (used internally in apply.cpp)
23 // Do not attempt to read, set, or reuse.
24 PRIVATE1 = 0x0100,
25 PRIVATE2 = 0x0200,
26 PRIVATE3 = 0x0400,
27 PRIVATE4 = 0x0800,
28 PRIVATE5 = 0x1000,
29 PRIVATE6 = 0x2000
30};
31
32constexpr HashRouterFlags
34{
35 return static_cast<HashRouterFlags>(
38}
39
40constexpr HashRouterFlags&
42{
43 lhs = lhs | rhs;
44 return lhs;
45}
46
47constexpr HashRouterFlags
49{
50 return static_cast<HashRouterFlags>(
53}
54
55constexpr HashRouterFlags&
57{
58 lhs = lhs & rhs;
59 return lhs;
60}
61
62constexpr bool
64{
65 return static_cast<std::underlying_type_t<HashRouterFlags>>(flags) != 0;
66}
67
68class Config;
69
77{
78public:
79 // The type here *MUST* match the type of Peer::id_t
81
90 struct Setup
91 {
93 explicit Setup() = default;
94
96
100
104 };
105
106private:
109 class Entry : public CountedObject<Entry>
110 {
111 public:
112 Entry() = default;
113
114 void
116 {
117 if (peer != 0)
118 peers_.insert(peer);
119 }
120
121 [[nodiscard]] HashRouterFlags
122 getFlags(void) const
123 {
124 return flags_;
125 }
126
127 void
129 {
130 flags_ |= flagsToSet;
131 }
132
136 {
137 return std::move(peers_);
138 }
139
142 relayed() const
143 {
144 return relayed_;
145 }
146
153 bool
155 {
156 if (relayed_ && *relayed_ + relayTime > now)
157 return false;
158 relayed_.emplace(now);
159 return true;
160 }
161
162 bool
164 {
165 if (processed_ && ((*processed_ + interval) > now))
166 return false;
167 processed_.emplace(now);
168 return true;
169 }
170
171 private:
174 // This could be generalized to a map, if more
175 // than one flag needs to expire independently.
178 };
179
180public:
181 HashRouter(Setup const& setup, Stopwatch& clock) : setup_(setup), suppressionMap_(clock)
182 {
183 }
184
186 operator=(HashRouter const&) = delete;
187
188 virtual ~HashRouter() = default;
189
190 // VFALCO TODO Replace "Suppression" terminology with something more
191 // semantically meaningful.
192 void
193 addSuppression(uint256 const& key);
194
195 bool
196 addSuppressionPeer(uint256 const& key, PeerShortID peer);
197
205
206 bool
207 addSuppressionPeer(uint256 const& key, PeerShortID peer, HashRouterFlags& flags);
208
209 // Add a peer suppression and return whether the entry should be processed
210 bool
212 uint256 const& key,
213 PeerShortID peer,
214 HashRouterFlags& flags,
215 std::chrono::seconds txInterval);
216
221 bool
222 setFlags(uint256 const& key, HashRouterFlags flags);
223
225 getFlags(uint256 const& key);
226
240 shouldRelay(uint256 const& key);
241
242private:
243 // pair.second indicates whether the entry was created
245 emplace(uint256 const&);
246
248
249 // Configurable parameters
251
252 // Stores all suppressed hashes and their expiration time
255};
256
257} // namespace xrpl
std::chrono::steady_clock::time_point time_point
bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval)
Definition HashRouter.h:163
bool shouldRelay(Stopwatch::time_point const &now, std::chrono::seconds relayTime)
Determines if this item should be relayed.
Definition HashRouter.h:154
std::optional< Stopwatch::time_point > relayed_
Definition HashRouter.h:176
void addPeer(PeerShortID peer)
Definition HashRouter.h:115
std::optional< Stopwatch::time_point > relayed() const
Return seated relay time point if the message has been relayed.
Definition HashRouter.h:142
void setFlags(HashRouterFlags flagsToSet)
Definition HashRouter.h:128
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition HashRouter.h:135
HashRouterFlags getFlags(void) const
Definition HashRouter.h:122
HashRouterFlags flags_
Definition HashRouter.h:172
std::optional< Stopwatch::time_point > processed_
Definition HashRouter.h:177
std::set< PeerShortID > peers_
Definition HashRouter.h:173
Routing table for objects identified by hash.
Definition HashRouter.h:77
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Setup const setup_
Definition HashRouter.h:250
virtual ~HashRouter()=default
HashRouterFlags getFlags(uint256 const &key)
HashRouter(Setup const &setup, Stopwatch &clock)
Definition HashRouter.h:181
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
std::mutex mutex_
Definition HashRouter.h:247
bool setFlags(uint256 const &key, HashRouterFlags flags)
Set the flags on a hash.
std::pair< bool, std::optional< Stopwatch::time_point > > addSuppressionPeerWithStatus(uint256 const &key, PeerShortID peer)
Add a suppression peer and get message's relay status.
std::pair< Entry &, bool > emplace(uint256 const &)
bool shouldProcess(uint256 const &key, PeerShortID peer, HashRouterFlags &flags, std::chrono::seconds txInterval)
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, HardenedHash< strong_hash > > suppressionMap_
Definition HashRouter.h:254
void addSuppression(uint256 const &key)
HashRouter & operator=(HashRouter const &)=delete
std::uint32_t PeerShortID
Definition HashRouter.h:80
detail::AgedUnorderedContainer< false, true, Key, T, Clock, Hash, KeyEqual, Allocator > aged_unordered_map
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
beast::AbstractClock< std::chrono::steady_clock > Stopwatch
A clock for measuring elapsed time.
Definition chrono.h:87
constexpr BaseUInt< Bits, Tag > operator&(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Definition base_uint.h:611
constexpr HashRouterFlags & operator|=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:41
constexpr BaseUInt< Bits, Tag > operator|(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Definition base_uint.h:618
constexpr HashRouterFlags & operator&=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:56
HashRouterFlags
Definition HashRouter.h:14
BaseUInt< 256 > uint256
Definition base_uint.h:562
constexpr bool any(HashRouterFlags flags)
Definition HashRouter.h:63
Structure used to customize HashRouter behavior.
Definition HashRouter.h:91
seconds holdTime
Expiration time for a hash entry.
Definition HashRouter.h:99
seconds relayTime
Amount of time required before a relayed item will be relayed again.
Definition HashRouter.h:103
std::chrono::seconds seconds
Definition HashRouter.h:95
Setup()=default
Default constructor.