rippled
Loading...
Searching...
No Matches
HashRouter.h
1#ifndef XRPL_APP_MISC_HASHROUTER_H_INCLUDED
2#define XRPL_APP_MISC_HASHROUTER_H_INCLUDED
3
4#include <xrpl/basics/CountedObject.h>
5#include <xrpl/basics/UnorderedContainers.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/basics/chrono.h>
8#include <xrpl/beast/container/aged_unordered_map.h>
9
10#include <optional>
11#include <set>
12
13namespace ripple {
14
16 // Public flags
17 UNDEFINED = 0x00,
18 BAD = 0x02, // Temporarily bad
19 SAVED = 0x04,
20 HELD = 0x08, // Held by LedgerMaster after potential processing failure
21 TRUSTED = 0x10, // Comes from a trusted source
22
23 // Private flags (used internally in apply.cpp)
24 // Do not attempt to read, set, or reuse.
25 PRIVATE1 = 0x0100,
26 PRIVATE2 = 0x0200,
27 PRIVATE3 = 0x0400,
28 PRIVATE4 = 0x0800,
29 PRIVATE5 = 0x1000,
30 PRIVATE6 = 0x2000
31};
32
33constexpr HashRouterFlags
35{
36 return static_cast<HashRouterFlags>(
39}
40
41constexpr HashRouterFlags&
43{
44 lhs = lhs | rhs;
45 return lhs;
46}
47
48constexpr HashRouterFlags
50{
51 return static_cast<HashRouterFlags>(
54}
55
56constexpr HashRouterFlags&
58{
59 lhs = lhs & rhs;
60 return lhs;
61}
62
63constexpr bool
65{
66 return static_cast<std::underlying_type_t<HashRouterFlags>>(flags) != 0;
67}
68
69class Config;
70
78{
79public:
80 // The type here *MUST* match the type of Peer::id_t
82
91 struct Setup
92 {
94 explicit Setup() = default;
95
97
101
105 };
106
107private:
110 class Entry : public CountedObject<Entry>
111 {
112 public:
114 {
115 }
116
117 void
119 {
120 if (peer != 0)
121 peers_.insert(peer);
122 }
123
125 getFlags(void) const
126 {
127 return flags_;
128 }
129
130 void
132 {
133 flags_ |= flagsToSet;
134 }
135
139 {
140 return std::move(peers_);
141 }
142
145 relayed() const
146 {
147 return relayed_;
148 }
149
156 bool
158 Stopwatch::time_point const& now,
159 std::chrono::seconds relayTime)
160 {
161 if (relayed_ && *relayed_ + relayTime > now)
162 return false;
163 relayed_.emplace(now);
164 return true;
165 }
166
167 bool
169 {
170 if (processed_ && ((*processed_ + interval) > now))
171 return false;
172 processed_.emplace(now);
173 return true;
174 }
175
176 private:
179 // This could be generalized to a map, if more
180 // than one flag needs to expire independently.
183 };
184
185public:
186 HashRouter(Setup const& setup, Stopwatch& clock)
187 : setup_(setup), suppressionMap_(clock)
188 {
189 }
190
192 operator=(HashRouter const&) = delete;
193
194 virtual ~HashRouter() = default;
195
196 // VFALCO TODO Replace "Supression" terminology with something more
197 // semantically meaningful.
198 void
199 addSuppression(uint256 const& key);
200
201 bool
202 addSuppressionPeer(uint256 const& key, PeerShortID peer);
203
211
212 bool
214 uint256 const& key,
215 PeerShortID peer,
216 HashRouterFlags& flags);
217
218 // Add a peer suppression and return whether the entry should be processed
219 bool
221 uint256 const& key,
222 PeerShortID peer,
223 HashRouterFlags& flags,
224 std::chrono::seconds tx_interval);
225
230 bool
231 setFlags(uint256 const& key, HashRouterFlags flags);
232
234 getFlags(uint256 const& key);
235
249 shouldRelay(uint256 const& key);
250
251private:
252 // pair.second indicates whether the entry was created
254 emplace(uint256 const&);
255
257
258 // Configurable parameters
260
261 // Stores all suppressed hashes and their expiration time
263 uint256,
264 Entry,
268};
269
272
273} // namespace ripple
274
275#endif
Associative container where each element is also indexed by time.
Tracks the number of instances of an object.
An entry in the routing table.
Definition HashRouter.h:111
std::optional< Stopwatch::time_point > processed_
Definition HashRouter.h:182
std::set< PeerShortID > peers_
Definition HashRouter.h:178
std::optional< Stopwatch::time_point > relayed() const
Return seated relay time point if the message has been relayed.
Definition HashRouter.h:145
std::optional< Stopwatch::time_point > relayed_
Definition HashRouter.h:181
void setFlags(HashRouterFlags flagsToSet)
Definition HashRouter.h:131
HashRouterFlags getFlags(void) const
Definition HashRouter.h:125
HashRouterFlags flags_
Definition HashRouter.h:177
void addPeer(PeerShortID peer)
Definition HashRouter.h:118
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition HashRouter.h:138
bool shouldRelay(Stopwatch::time_point const &now, std::chrono::seconds relayTime)
Determines if this item should be relayed.
Definition HashRouter.h:157
bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval)
Definition HashRouter.h:168
Routing table for objects identified by hash.
Definition HashRouter.h:78
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition HashRouter.h:267
HashRouterFlags getFlags(uint256 const &key)
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
std::mutex mutex_
Definition HashRouter.h:256
bool shouldProcess(uint256 const &key, PeerShortID peer, HashRouterFlags &flags, std::chrono::seconds tx_interval)
virtual ~HashRouter()=default
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
Setup const setup_
Definition HashRouter.h:259
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.
HashRouter & operator=(HashRouter const &)=delete
HashRouter(Setup const &setup, Stopwatch &clock)
Definition HashRouter.h:186
std::pair< Entry &, bool > emplace(uint256 const &)
Definition HashRouter.cpp:7
void addSuppression(uint256 const &key)
Seed functor once per construction.
T emplace(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
ApplyFlags operator|=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition ApplyView.h:71
constexpr base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:596
base_uint< 256 > uint256
Definition base_uint.h:539
ApplyFlags operator&=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition ApplyView.h:78
constexpr base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:589
HashRouterFlags
Definition HashRouter.h:15
HashRouter::Setup setup_HashRouter(Config const &config)
Structure used to customize HashRouter behavior.
Definition HashRouter.h:92
Setup()=default
Default constructor.
seconds holdTime
Expiration time for a hash entry.
Definition HashRouter.h:100
seconds relayTime
Amount of time required before a relayed item will be relayed again.
Definition HashRouter.h:104