rippled
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:
113 {
114 }
115
116 void
118 {
119 if (peer != 0)
120 peers_.insert(peer);
121 }
122
124 getFlags(void) const
125 {
126 return flags_;
127 }
128
129 void
131 {
132 flags_ |= flagsToSet;
133 }
134
138 {
139 return std::move(peers_);
140 }
141
144 relayed() const
145 {
146 return relayed_;
147 }
148
155 bool
157 {
158 if (relayed_ && *relayed_ + relayTime > now)
159 return false;
160 relayed_.emplace(now);
161 return true;
162 }
163
164 bool
166 {
167 if (processed_ && ((*processed_ + interval) > now))
168 return false;
169 processed_.emplace(now);
170 return true;
171 }
172
173 private:
176 // This could be generalized to a map, if more
177 // than one flag needs to expire independently.
180 };
181
182public:
183 HashRouter(Setup const& setup, Stopwatch& clock) : setup_(setup), suppressionMap_(clock)
184 {
185 }
186
188 operator=(HashRouter const&) = delete;
189
190 virtual ~HashRouter() = default;
191
192 // VFALCO TODO Replace "Suppression" terminology with something more
193 // semantically meaningful.
194 void
195 addSuppression(uint256 const& key);
196
197 bool
198 addSuppressionPeer(uint256 const& key, PeerShortID peer);
199
207
208 bool
209 addSuppressionPeer(uint256 const& key, PeerShortID peer, HashRouterFlags& flags);
210
211 // Add a peer suppression and return whether the entry should be processed
212 bool
213 shouldProcess(uint256 const& key, PeerShortID peer, HashRouterFlags& flags, std::chrono::seconds tx_interval);
214
219 bool
220 setFlags(uint256 const& key, HashRouterFlags flags);
221
223 getFlags(uint256 const& key);
224
238 shouldRelay(uint256 const& key);
239
240private:
241 // pair.second indicates whether the entry was created
243 emplace(uint256 const&);
244
246
247 // Configurable parameters
249
250 // Stores all suppressed hashes and their expiration time
252};
253
256
257} // namespace xrpl
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:110
bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval)
Definition HashRouter.h:165
bool shouldRelay(Stopwatch::time_point const &now, std::chrono::seconds relayTime)
Determines if this item should be relayed.
Definition HashRouter.h:156
std::optional< Stopwatch::time_point > relayed_
Definition HashRouter.h:178
void addPeer(PeerShortID peer)
Definition HashRouter.h:117
std::optional< Stopwatch::time_point > relayed() const
Return seated relay time point if the message has been relayed.
Definition HashRouter.h:144
void setFlags(HashRouterFlags flagsToSet)
Definition HashRouter.h:130
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition HashRouter.h:137
HashRouterFlags getFlags(void) const
Definition HashRouter.h:124
HashRouterFlags flags_
Definition HashRouter.h:174
std::optional< Stopwatch::time_point > processed_
Definition HashRouter.h:179
std::set< PeerShortID > peers_
Definition HashRouter.h:175
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:248
virtual ~HashRouter()=default
HashRouterFlags getFlags(uint256 const &key)
HashRouter(Setup const &setup, Stopwatch &clock)
Definition HashRouter.h:183
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
std::mutex mutex_
Definition HashRouter.h:245
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 &)
Definition HashRouter.cpp:7
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition HashRouter.h:251
void addSuppression(uint256 const &key)
HashRouter & operator=(HashRouter const &)=delete
bool shouldProcess(uint256 const &key, PeerShortID peer, HashRouterFlags &flags, std::chrono::seconds tx_interval)
T emplace(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:582
constexpr base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:575
ApplyFlags operator&=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition ApplyView.h:68
HashRouterFlags
Definition HashRouter.h:14
ApplyFlags operator|=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition ApplyView.h:61
HashRouter::Setup setup_HashRouter(Config const &config)
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
Setup()=default
Default constructor.