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};
39
40constexpr HashRouterFlags
42{
43 return static_cast<HashRouterFlags>(
46}
47
48constexpr HashRouterFlags&
50{
51 lhs = lhs | rhs;
52 return lhs;
53}
54
55constexpr HashRouterFlags
57{
58 return static_cast<HashRouterFlags>(
61}
62
63constexpr HashRouterFlags&
65{
66 lhs = lhs & rhs;
67 return lhs;
68}
69
70constexpr bool
72{
73 return static_cast<std::underlying_type_t<HashRouterFlags>>(flags) != 0;
74}
75
76class Config;
77
86{
87public:
88 // The type here *MUST* match the type of Peer::id_t
90
100 struct Setup
101 {
105 explicit Setup() = default;
106
108
113
118 };
119
120private:
124 class Entry : public CountedObject<Entry>
125 {
126 public:
127 Entry() = default;
128
129 void
131 {
132 if (peer != 0)
133 peers_.insert(peer);
134 }
135
136 [[nodiscard]] HashRouterFlags
137 getFlags() const
138 {
139 return flags_;
140 }
141
142 void
144 {
145 flags_ |= flagsToSet;
146 }
147
153 {
154 return std::move(peers_);
155 }
156
161 relayed() const
162 {
163 return relayed_;
164 }
165
173 bool
175 {
176 if (relayed_ && *relayed_ + relayTime > now)
177 return false;
178 relayed_.emplace(now);
179 return true;
180 }
181
182 bool
184 {
185 if (processed_ && ((*processed_ + interval) > now))
186 return false;
187 processed_.emplace(now);
188 return true;
189 }
190
191 private:
194 // This could be generalized to a map, if more
195 // than one flag needs to expire independently.
198 };
199
200public:
201 HashRouter(Setup const& setup, Stopwatch& clock) : setup_(setup), suppressionMap_(clock)
202 {
203 }
204
206 operator=(HashRouter const&) = delete;
207
208 virtual ~HashRouter() = default;
209
210 // VFALCO TODO Replace "Suppression" terminology with something more
211 // semantically meaningful.
212 void
213 addSuppression(uint256 const& key);
214
215 bool
216 addSuppressionPeer(uint256 const& key, PeerShortID peer);
217
227
228 bool
229 addSuppressionPeer(uint256 const& key, PeerShortID peer, HashRouterFlags& flags);
230
231 // Add a peer suppression and return whether the entry should be processed
232 bool
234 uint256 const& key,
235 PeerShortID peer,
236 HashRouterFlags& flags,
237 std::chrono::seconds txInterval);
238
244 bool
245 setFlags(uint256 const& key, HashRouterFlags flags);
246
248 getFlags(uint256 const& key);
249
264 shouldRelay(uint256 const& key);
265
266private:
267 // pair.second indicates whether the entry was created
269 emplace(uint256 const&);
270
272
273 // Configurable parameters
275
276 // Stores all suppressed hashes and their expiration time
279};
280
281} // namespace xrpl
std::chrono::steady_clock::time_point time_point
bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval)
Definition HashRouter.h:183
bool shouldRelay(Stopwatch::time_point const &now, std::chrono::seconds relayTime)
Determines if this item should be relayed.
Definition HashRouter.h:174
std::optional< Stopwatch::time_point > relayed_
Definition HashRouter.h:196
void addPeer(PeerShortID peer)
Definition HashRouter.h:130
std::optional< Stopwatch::time_point > relayed() const
Return seated relay time point if the message has been relayed.
Definition HashRouter.h:161
void setFlags(HashRouterFlags flagsToSet)
Definition HashRouter.h:143
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition HashRouter.h:152
HashRouterFlags flags_
Definition HashRouter.h:192
HashRouterFlags getFlags() const
Definition HashRouter.h:137
std::optional< Stopwatch::time_point > processed_
Definition HashRouter.h:197
std::set< PeerShortID > peers_
Definition HashRouter.h:193
Routing table for objects identified by hash.
Definition HashRouter.h:86
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Setup const setup_
Definition HashRouter.h:274
virtual ~HashRouter()=default
HashRouterFlags getFlags(uint256 const &key)
HashRouter(Setup const &setup, Stopwatch &clock)
Definition HashRouter.h:201
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
std::mutex mutex_
Definition HashRouter.h:271
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:278
void addSuppression(uint256 const &key)
HashRouter & operator=(HashRouter const &)=delete
std::uint32_t PeerShortID
Definition HashRouter.h:89
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:90
constexpr BaseUInt< Bits, Tag > operator&(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Definition base_uint.h:629
constexpr HashRouterFlags & operator|=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:49
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:64
HashRouterFlags
Definition HashRouter.h:20
BaseUInt< 256 > uint256
Definition base_uint.h:580
constexpr bool any(HashRouterFlags flags)
Definition HashRouter.h:71
Structure used to customize HashRouter behavior.
Definition HashRouter.h:101
seconds holdTime
Expiration time for a hash entry.
Definition HashRouter.h:112
seconds relayTime
Amount of time required before a relayed item will be relayed again.
Definition HashRouter.h:117
std::chrono::seconds seconds
Definition HashRouter.h:107
Setup()=default
Default constructor.