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 bool
75{
76 return static_cast<std::underlying_type_t<HashRouterFlags>>(flags) != 0;
77}
78
79class Config;
80
89{
90public:
91 // The type here *MUST* match the type of Peer::ID
93
103 struct Setup
104 {
108 explicit Setup() = default;
109
111
116
121 };
122
123private:
127 class Entry : public CountedObject<Entry>
128 {
129 public:
130 Entry() = default;
131
132 void
134 {
135 if (peer != 0)
136 peers_.insert(peer);
137 }
138
139 [[nodiscard]] HashRouterFlags
140 getFlags() const
141 {
142 return flags_;
143 }
144
145 void
147 {
148 flags_ |= flagsToSet;
149 }
150
156 {
157 return std::move(peers_);
158 }
159
164 relayed() const
165 {
166 return relayed_;
167 }
168
176 bool
178 {
179 if (relayed_ && *relayed_ + relayTime > now)
180 return false;
181 relayed_.emplace(now);
182 return true;
183 }
184
185 bool
187 {
188 if (processed_ && ((*processed_ + interval) > now))
189 return false;
190 processed_.emplace(now);
191 return true;
192 }
193
194 private:
197 // This could be generalized to a map, if more
198 // than one flag needs to expire independently.
201 };
202
203public:
204 HashRouter(Setup const& setup, Stopwatch& clock) : setup_(setup), suppressionMap_(clock)
205 {
206 }
207
209 operator=(HashRouter const&) = delete;
210
211 virtual ~HashRouter() = default;
212
213 // VFALCO TODO Replace "Suppression" terminology with something more
214 // semantically meaningful.
215 void
216 addSuppression(UInt256 const& key);
217
218 bool
219 addSuppressionPeer(UInt256 const& key, PeerShortID peer);
220
230
231 bool
232 addSuppressionPeer(UInt256 const& key, PeerShortID peer, HashRouterFlags& flags);
233
234 // Add a peer suppression and return whether the entry should be processed
235 bool
237 UInt256 const& key,
238 PeerShortID peer,
239 HashRouterFlags& flags,
240 std::chrono::seconds txInterval);
241
247 bool
248 setFlags(UInt256 const& key, HashRouterFlags flags);
249
251 getFlags(UInt256 const& key);
252
267 shouldRelay(UInt256 const& key);
268
269private:
270 // pair.second indicates whether the entry was created
272 emplace(UInt256 const&);
273
275
276 // Configurable parameters
278
279 // Stores all suppressed hashes and their expiration time
282};
283
284} // namespace xrpl
std::chrono::steady_clock::time_point time_point
bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval)
Definition HashRouter.h:186
bool shouldRelay(Stopwatch::time_point const &now, std::chrono::seconds relayTime)
Determines if this item should be relayed.
Definition HashRouter.h:177
std::optional< Stopwatch::time_point > relayed_
Definition HashRouter.h:199
void addPeer(PeerShortID peer)
Definition HashRouter.h:133
std::optional< Stopwatch::time_point > relayed() const
Return seated relay time point if the message has been relayed.
Definition HashRouter.h:164
void setFlags(HashRouterFlags flagsToSet)
Definition HashRouter.h:146
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition HashRouter.h:155
HashRouterFlags flags_
Definition HashRouter.h:195
HashRouterFlags getFlags() const
Definition HashRouter.h:140
std::optional< Stopwatch::time_point > processed_
Definition HashRouter.h:200
std::set< PeerShortID > peers_
Definition HashRouter.h:196
Routing table for objects identified by hash.
Definition HashRouter.h:89
Setup const setup_
Definition HashRouter.h:277
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:204
void addSuppression(UInt256 const &key)
std::mutex mutex_
Definition HashRouter.h:274
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
std::uint32_t PeerShortID
Definition HashRouter.h:92
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:281
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:635
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:642
constexpr HashRouterFlags & operator&=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:67
BaseUInt< 256 > UInt256
Definition base_uint.h:580
HashRouterFlags
Definition HashRouter.h:20
constexpr bool any(HashRouterFlags flags)
Definition HashRouter.h:74
Structure used to customize HashRouter behavior.
Definition HashRouter.h:104
std::chrono::seconds Seconds
Definition HashRouter.h:110
Seconds relayTime
Amount of time required before a relayed item will be relayed again.
Definition HashRouter.h:120
Seconds holdTime
Expiration time for a hash entry.
Definition HashRouter.h:115
Setup()=default
Default constructor.