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
31namespace ripple {
32
33// TODO convert these macros to int constants or an enum
34#define SF_BAD 0x02 // Temporarily bad
35#define SF_SAVED 0x04
36#define SF_TRUSTED 0x10 // comes from trusted source
37
38// Private flags, used internally in apply.cpp.
39// Do not attempt to read, set, or reuse.
40#define SF_PRIVATE1 0x0100
41#define SF_PRIVATE2 0x0200
42#define SF_PRIVATE3 0x0400
43#define SF_PRIVATE4 0x0800
44#define SF_PRIVATE5 0x1000
45#define SF_PRIVATE6 0x2000
46
54{
55public:
56 // The type here *MUST* match the type of Peer::id_t
58
59private:
62 class Entry : public CountedObject<Entry>
63 {
64 public:
66 {
67 }
68
69 void
71 {
72 if (peer != 0)
73 peers_.insert(peer);
74 }
75
76 int
77 getFlags(void) const
78 {
79 return flags_;
80 }
81
82 void
83 setFlags(int flagsToSet)
84 {
85 flags_ |= flagsToSet;
86 }
87
91 {
92 return std::move(peers_);
93 }
94
98 {
99 return peers_;
100 }
101
104 relayed() const
105 {
106 return relayed_;
107 }
108
115 bool
117 Stopwatch::time_point const& now,
118 std::chrono::seconds holdTime)
119 {
120 if (relayed_ && *relayed_ + holdTime > now)
121 return false;
122 relayed_.emplace(now);
123 return true;
124 }
125
126 bool
128 {
129 if (processed_ && ((*processed_ + interval) > now))
130 return false;
131 processed_.emplace(now);
132 return true;
133 }
134
135 bool
137 PeerShortID peer,
139 std::chrono::seconds interval)
140 {
141 if (peerProcessed_.contains(peer) &&
142 ((peerProcessed_[peer] + interval) > now))
143 return false;
144 // Peer may already be in the list, but adding it again doesn't hurt
145 addPeer(peer);
146 peerProcessed_[peer] = now;
147 return true;
148 }
149
150 private:
151 int flags_ = 0;
153 // This could be generalized to a map, if more
154 // than one flag needs to expire independently.
158 };
159
160public:
161 static inline std::chrono::seconds
163 {
164 using namespace std::chrono;
165
166 return 300s;
167 }
168
169 HashRouter(Stopwatch& clock, std::chrono::seconds entryHoldTimeInSeconds)
170 : suppressionMap_(clock), holdTime_(entryHoldTimeInSeconds)
171 {
172 }
173
175 operator=(HashRouter const&) = delete;
176
177 virtual ~HashRouter() = default;
178
179 // VFALCO TODO Replace "Supression" terminology with something more
180 // semantically meaningful.
181 void
182 addSuppression(uint256 const& key);
183
184 bool
185 addSuppressionPeer(uint256 const& key, PeerShortID peer);
186
194
195 bool
196 addSuppressionPeer(uint256 const& key, PeerShortID peer, int& flags);
197
198 // Add a peer suppression and return whether the entry should be processed
199 bool
201 uint256 const& key,
202 PeerShortID peer,
203 int& flags,
204 std::chrono::seconds tx_interval);
205
212 bool
214 uint256 const& key,
215 PeerShortID peer,
216 std::chrono::seconds interval);
217
222 bool
223 setFlags(uint256 const& key, int flags);
224
225 int
226 getFlags(uint256 const& key);
227
241 shouldRelay(uint256 const& key);
242
246 getPeers(uint256 const& key);
247
248private:
249 // pair.second indicates whether the entry was created
251 emplace(uint256 const&);
252
254
255 // Stores all suppressed hashes and their expiration time
257 uint256,
258 Entry,
262
264};
265
266} // namespace ripple
267
268#endif
typename Clock::time_point time_point
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:63
std::map< PeerShortID, Stopwatch::time_point > peerProcessed_
Definition: HashRouter.h:157
void setFlags(int flagsToSet)
Definition: HashRouter.h:83
std::set< PeerShortID > const & peekPeerSet()
Return set of peers waiting for reply.
Definition: HashRouter.h:97
std::optional< Stopwatch::time_point > processed_
Definition: HashRouter.h:156
int getFlags(void) const
Definition: HashRouter.h:77
std::set< PeerShortID > peers_
Definition: HashRouter.h:152
std::optional< Stopwatch::time_point > relayed() const
Return seated relay time point if the message has been relayed.
Definition: HashRouter.h:104
bool shouldProcessForPeer(PeerShortID peer, Stopwatch::time_point now, std::chrono::seconds interval)
Definition: HashRouter.h:136
std::optional< Stopwatch::time_point > relayed_
Definition: HashRouter.h:155
void addPeer(PeerShortID peer)
Definition: HashRouter.h:70
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition: HashRouter.h:90
bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval)
Definition: HashRouter.h:127
bool shouldRelay(Stopwatch::time_point const &now, std::chrono::seconds holdTime)
Determines if this item should be relayed.
Definition: HashRouter.h:116
Routing table for objects identified by hash.
Definition: HashRouter.h:54
std::chrono::seconds const holdTime_
Definition: HashRouter.h:263
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition: HashRouter.h:261
HashRouter(Stopwatch &clock, std::chrono::seconds entryHoldTimeInSeconds)
Definition: HashRouter.h:169
static std::chrono::seconds getDefaultHoldTime()
Definition: HashRouter.h:162
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Definition: HashRouter.cpp:132
bool shouldProcess(uint256 const &key, PeerShortID peer, int &flags, std::chrono::seconds tx_interval)
Definition: HashRouter.cpp:78
std::mutex mutex_
Definition: HashRouter.h:253
int getFlags(uint256 const &key)
Definition: HashRouter.cpp:108
virtual ~HashRouter()=default
bool shouldProcessForPeer(uint256 const &key, PeerShortID peer, std::chrono::seconds interval)
Determines whether the hashed item should be processed for the given peer.
Definition: HashRouter.cpp:94
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
Definition: HashRouter.cpp:51
std::pair< bool, std::optional< Stopwatch::time_point > > addSuppressionPeerWithStatus(uint256 const &key, PeerShortID peer)
Add a suppression peer and get message's relay status.
Definition: HashRouter.cpp:57
HashRouter & operator=(HashRouter const &)=delete
bool setFlags(uint256 const &key, int flags)
Set the flags on a hash.
Definition: HashRouter.cpp:116
std::set< PeerShortID > getPeers(uint256 const &key)
Returns a copy of the set of peers in the Entry for the key.
Definition: HashRouter.cpp:146
std::pair< Entry &, bool > emplace(uint256 const &)
Definition: HashRouter.cpp:25
void addSuppression(uint256 const &key)
Definition: HashRouter.cpp:43
Seed functor once per construction.
Definition: hardened_hash.h:97
T emplace(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
base_uint< 256 > uint256
Definition: base_uint.h:557