rippled
Loading...
Searching...
No Matches
HashRouter.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_APP_MISC_HASHROUTER_H_INCLUDED
21#define RIPPLE_APP_MISC_HASHROUTER_H_INCLUDED
22
23#include <xrpl/basics/CountedObject.h>
24#include <xrpl/basics/UnorderedContainers.h>
25#include <xrpl/basics/base_uint.h>
26#include <xrpl/basics/chrono.h>
27#include <xrpl/beast/container/aged_unordered_map.h>
28
29#include <optional>
30#include <set>
31
32namespace ripple {
33
35 // Public flags
36 UNDEFINED = 0x00,
37 BAD = 0x02, // Temporarily bad
38 SAVED = 0x04,
39 HELD = 0x08, // Held by LedgerMaster after potential processing failure
40 TRUSTED = 0x10, // Comes from a trusted source
41
42 // Private flags (used internally in apply.cpp)
43 // Do not attempt to read, set, or reuse.
44 PRIVATE1 = 0x0100,
45 PRIVATE2 = 0x0200,
46 PRIVATE3 = 0x0400,
47 PRIVATE4 = 0x0800,
48 PRIVATE5 = 0x1000,
49 PRIVATE6 = 0x2000
50};
51
52constexpr HashRouterFlags
54{
55 return static_cast<HashRouterFlags>(
58}
59
60constexpr HashRouterFlags&
62{
63 lhs = lhs | rhs;
64 return lhs;
65}
66
67constexpr HashRouterFlags
69{
70 return static_cast<HashRouterFlags>(
73}
74
75constexpr HashRouterFlags&
77{
78 lhs = lhs & rhs;
79 return lhs;
80}
81
82constexpr bool
84{
85 return static_cast<std::underlying_type_t<HashRouterFlags>>(flags) != 0;
86}
87
88class Config;
89
97{
98public:
99 // The type here *MUST* match the type of Peer::id_t
101
110 struct Setup
111 {
113 explicit Setup() = default;
114
116
120
124 };
125
126private:
129 class Entry : public CountedObject<Entry>
130 {
131 public:
133 {
134 }
135
136 void
138 {
139 if (peer != 0)
140 peers_.insert(peer);
141 }
142
144 getFlags(void) const
145 {
146 return flags_;
147 }
148
149 void
151 {
152 flags_ |= flagsToSet;
153 }
154
158 {
159 return std::move(peers_);
160 }
161
164 relayed() const
165 {
166 return relayed_;
167 }
168
175 bool
177 Stopwatch::time_point const& now,
178 std::chrono::seconds relayTime)
179 {
180 if (relayed_ && *relayed_ + relayTime > now)
181 return false;
182 relayed_.emplace(now);
183 return true;
184 }
185
186 bool
188 {
189 if (processed_ && ((*processed_ + interval) > now))
190 return false;
191 processed_.emplace(now);
192 return true;
193 }
194
195 private:
198 // This could be generalized to a map, if more
199 // than one flag needs to expire independently.
202 };
203
204public:
205 HashRouter(Setup const& setup, Stopwatch& clock)
206 : setup_(setup), suppressionMap_(clock)
207 {
208 }
209
211 operator=(HashRouter const&) = delete;
212
213 virtual ~HashRouter() = default;
214
215 // VFALCO TODO Replace "Supression" terminology with something more
216 // semantically meaningful.
217 void
218 addSuppression(uint256 const& key);
219
220 bool
221 addSuppressionPeer(uint256 const& key, PeerShortID peer);
222
230
231 bool
233 uint256 const& key,
234 PeerShortID peer,
235 HashRouterFlags& flags);
236
237 // Add a peer suppression and return whether the entry should be processed
238 bool
240 uint256 const& key,
241 PeerShortID peer,
242 HashRouterFlags& flags,
243 std::chrono::seconds tx_interval);
244
249 bool
250 setFlags(uint256 const& key, HashRouterFlags flags);
251
253 getFlags(uint256 const& key);
254
268 shouldRelay(uint256 const& key);
269
270private:
271 // pair.second indicates whether the entry was created
273 emplace(uint256 const&);
274
276
277 // Configurable parameters
279
280 // Stores all suppressed hashes and their expiration time
282 uint256,
283 Entry,
287};
288
291
292} // namespace ripple
293
294#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:130
std::optional< Stopwatch::time_point > processed_
Definition HashRouter.h:201
std::set< PeerShortID > peers_
Definition HashRouter.h:197
std::optional< Stopwatch::time_point > relayed() const
Return seated relay time point if the message has been relayed.
Definition HashRouter.h:164
std::optional< Stopwatch::time_point > relayed_
Definition HashRouter.h:200
void setFlags(HashRouterFlags flagsToSet)
Definition HashRouter.h:150
HashRouterFlags getFlags(void) const
Definition HashRouter.h:144
HashRouterFlags flags_
Definition HashRouter.h:196
void addPeer(PeerShortID peer)
Definition HashRouter.h:137
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition HashRouter.h:157
bool shouldRelay(Stopwatch::time_point const &now, std::chrono::seconds relayTime)
Determines if this item should be relayed.
Definition HashRouter.h:176
bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval)
Definition HashRouter.h:187
Routing table for objects identified by hash.
Definition HashRouter.h:97
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition HashRouter.h:286
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:275
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:278
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:205
std::pair< Entry &, bool > emplace(uint256 const &)
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:25
ApplyFlags operator|=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition ApplyView.h:90
constexpr base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:615
base_uint< 256 > uint256
Definition base_uint.h:558
ApplyFlags operator&=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition ApplyView.h:97
constexpr base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:608
HashRouterFlags
Definition HashRouter.h:34
HashRouter::Setup setup_HashRouter(Config const &config)
Structure used to customize HashRouter behavior.
Definition HashRouter.h:111
Setup()=default
Default constructor.
seconds holdTime
Expiration time for a hash entry.
Definition HashRouter.h:119
seconds relayTime
Amount of time required before a relayed item will be relayed again.
Definition HashRouter.h:123