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/basics/hardened_hash.h>
8#include <xrpl/beast/container/aged_unordered_map.h>
9
10#include <chrono>
11#include <cstdint>
12#include <mutex>
13#include <optional>
14#include <set>
15#include <type_traits>
16#include <utility>
17
18namespace xrpl {
19
21 // Public flags
22 UNDEFINED = 0x00,
23 BAD = 0x02, // Temporarily bad
24 SAVED = 0x04,
25 HELD = 0x08, // Held by LedgerMaster after potential processing failure
26 TRUSTED = 0x10, // Comes from a trusted source
27
28 // Private flags. Each group is owned by one file; do not read, set, or
29 // reuse a flag outside the file noted.
30 // Used in apply.cpp
31 PRIVATE1 = 0x0100,
32 PRIVATE2 = 0x0200,
33 PRIVATE3 = 0x0400,
34 PRIVATE4 = 0x0800,
35 // Used in EscrowFinish.cpp
36 PRIVATE5 = 0x1000,
37 PRIVATE6 = 0x2000,
38 // Used in apply.cpp
39 PRIVATE7 = 0x4000,
40 PRIVATE8 = 0x8000,
41};
42
43constexpr HashRouterFlags
45{
46 return static_cast<HashRouterFlags>(
49}
50
51constexpr HashRouterFlags&
53{
54 lhs = lhs | rhs;
55 return lhs;
56}
57
58constexpr HashRouterFlags
60{
61 return static_cast<HashRouterFlags>(
64}
65
66constexpr HashRouterFlags&
68{
69 lhs = lhs & rhs;
70 return lhs;
71}
72
73constexpr HashRouterFlags
75{
76 return static_cast<HashRouterFlags>(
77 ~static_cast<std::underlying_type_t<HashRouterFlags>>(flags));
78}
79
80constexpr bool
82{
83 return static_cast<std::underlying_type_t<HashRouterFlags>>(flags) != 0;
84}
85
86class Config;
87
96{
97public:
98 // The type here *MUST* match the type of Peer::ID
100
110 struct Setup
111 {
115 explicit Setup() = default;
116
118
123
128 };
129
130private:
134 class Entry : public CountedObject<Entry>
135 {
136 public:
137 Entry() = default;
138
139 void
141 {
142 if (peer != 0)
143 peers_.insert(peer);
144 }
145
146 [[nodiscard]] HashRouterFlags
147 getFlags() const
148 {
149 return flags_;
150 }
151
152 void
154 {
155 flags_ |= flagsToSet;
156 }
157
163 void
165 {
166 flags_ &= ~flags;
167 }
168
174 {
175 return std::move(peers_);
176 }
177
182 relayed() const
183 {
184 return relayed_;
185 }
186
194 bool
196 {
197 if (relayed_ && *relayed_ + relayTime > now)
198 return false;
199 relayed_.emplace(now);
200 return true;
201 }
202
203 bool
205 {
206 if (processed_ && ((*processed_ + interval) > now))
207 return false;
208 processed_.emplace(now);
209 return true;
210 }
211
212 private:
215 // This could be generalized to a map, if more
216 // than one flag needs to expire independently.
219 };
220
221public:
222 HashRouter(Setup const& setup, Stopwatch& clock) : setup_(setup), suppressionMap_(clock)
223 {
224 }
225
227 operator=(HashRouter const&) = delete;
228
229 virtual ~HashRouter() = default;
230
231 // VFALCO TODO Replace "Suppression" terminology with something more
232 // semantically meaningful.
233 void
234 addSuppression(UInt256 const& key);
235
236 bool
237 addSuppressionPeer(UInt256 const& key, PeerShortID peer);
238
248
249 bool
250 addSuppressionPeer(UInt256 const& key, PeerShortID peer, HashRouterFlags& flags);
251
252 // Add a peer suppression and return whether the entry should be processed
253 bool
255 UInt256 const& key,
256 PeerShortID peer,
257 HashRouterFlags& flags,
258 std::chrono::seconds txInterval);
259
265 bool
266 setFlags(UInt256 const& key, HashRouterFlags flags);
267
279 bool
280 clearFlags(UInt256 const& key, HashRouterFlags flags);
281
283 getFlags(UInt256 const& key);
284
299 shouldRelay(UInt256 const& key);
300
301private:
302 // pair.second indicates whether the entry was created
304 emplace(UInt256 const&);
305
307
308 // Configurable parameters
310
311 // Stores all suppressed hashes and their expiration time
314};
315
316} // namespace xrpl
std::chrono::steady_clock::time_point time_point
bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval)
Definition HashRouter.h:204
void clearFlags(HashRouterFlags flags)
Clear the given flags, leaving every other flag as it is.
Definition HashRouter.h:164
bool shouldRelay(Stopwatch::time_point const &now, std::chrono::seconds relayTime)
Determines if this item should be relayed.
Definition HashRouter.h:195
std::optional< Stopwatch::time_point > relayed_
Definition HashRouter.h:217
void addPeer(PeerShortID peer)
Definition HashRouter.h:140
std::optional< Stopwatch::time_point > relayed() const
Return seated relay time point if the message has been relayed.
Definition HashRouter.h:182
void setFlags(HashRouterFlags flagsToSet)
Definition HashRouter.h:153
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition HashRouter.h:173
HashRouterFlags flags_
Definition HashRouter.h:213
HashRouterFlags getFlags() const
Definition HashRouter.h:147
std::optional< Stopwatch::time_point > processed_
Definition HashRouter.h:218
std::set< PeerShortID > peers_
Definition HashRouter.h:214
Routing table for objects identified by hash.
Definition HashRouter.h:96
Setup const setup_
Definition HashRouter.h:309
virtual ~HashRouter()=default
std::pair< bool, std::optional< Stopwatch::time_point > > addSuppressionPeerWithStatus(UInt256 const &key, PeerShortID peer)
Add a suppression peer and get message's relay status.
HashRouter(Setup const &setup, Stopwatch &clock)
Definition HashRouter.h:222
void addSuppression(UInt256 const &key)
std::mutex mutex_
Definition HashRouter.h:306
bool setFlags(UInt256 const &key, HashRouterFlags flags)
Set the flags on a hash.
bool addSuppressionPeer(UInt256 const &key, PeerShortID peer)
std::pair< Entry &, bool > emplace(UInt256 const &)
std::optional< std::set< PeerShortID > > shouldRelay(UInt256 const &key)
Determines whether the hashed item should be relayed.
HashRouter & operator=(HashRouter const &)=delete
bool clearFlags(UInt256 const &key, HashRouterFlags flags)
Clear flags on a hash.
std::uint32_t PeerShortID
Definition HashRouter.h:99
HashRouterFlags getFlags(UInt256 const &key)
bool shouldProcess(UInt256 const &key, PeerShortID peer, HashRouterFlags &flags, std::chrono::seconds txInterval)
beast::AgedUnorderedMap< UInt256, Entry, Stopwatch::ClockType, HardenedHash< StrongHash > > suppressionMap_
Definition HashRouter.h:313
detail::AgedUnorderedContainer< false, true, Key, T, Clock, Hash, KeyEqual, Allocator > AgedUnorderedMap
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:90
constexpr BaseUInt< Bits, Tag > operator&(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Definition base_uint.h:636
constexpr HashRouterFlags & operator|=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:52
constexpr BaseUInt< Bits, Tag > operator|(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Definition base_uint.h:643
constexpr HashRouterFlags & operator&=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:67
BaseUInt< 256 > UInt256
Definition base_uint.h:581
HashRouterFlags
Definition HashRouter.h:20
constexpr HashRouterFlags operator~(HashRouterFlags flags)
Definition HashRouter.h:74
constexpr bool any(HashRouterFlags flags)
Definition HashRouter.h:81
Structure used to customize HashRouter behavior.
Definition HashRouter.h:111
std::chrono::seconds Seconds
Definition HashRouter.h:117
Seconds relayTime
Amount of time required before a relayed item will be relayed again.
Definition HashRouter.h:127
Seconds holdTime
Expiration time for a hash entry.
Definition HashRouter.h:122
Setup()=default
Default constructor.