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
34// TODO convert these macros to int constants or an enum
35#define SF_BAD 0x02 // Temporarily bad
36#define SF_SAVED 0x04
37#define SF_HELD 0x08 // Held by LedgerMaster after potential processing failure
38#define SF_TRUSTED 0x10 // comes from trusted source
39
40// Private flags, used internally in apply.cpp.
41// Do not attempt to read, set, or reuse.
42#define SF_PRIVATE1 0x0100
43#define SF_PRIVATE2 0x0200
44#define SF_PRIVATE3 0x0400
45#define SF_PRIVATE4 0x0800
46#define SF_PRIVATE5 0x1000
47#define SF_PRIVATE6 0x2000
48
49class Config;
50
58{
59public:
60 // The type here *MUST* match the type of Peer::id_t
62
71 struct Setup
72 {
74 explicit Setup() = default;
75
77
81
85 };
86
87private:
90 class Entry : public CountedObject<Entry>
91 {
92 public:
94 {
95 }
96
97 void
99 {
100 if (peer != 0)
101 peers_.insert(peer);
102 }
103
104 int
105 getFlags(void) const
106 {
107 return flags_;
108 }
109
110 void
111 setFlags(int flagsToSet)
112 {
113 flags_ |= flagsToSet;
114 }
115
119 {
120 return std::move(peers_);
121 }
122
125 relayed() const
126 {
127 return relayed_;
128 }
129
136 bool
138 Stopwatch::time_point const& now,
139 std::chrono::seconds relayTime)
140 {
141 if (relayed_ && *relayed_ + relayTime > now)
142 return false;
143 relayed_.emplace(now);
144 return true;
145 }
146
147 bool
149 {
150 if (processed_ && ((*processed_ + interval) > now))
151 return false;
152 processed_.emplace(now);
153 return true;
154 }
155
156 private:
157 int flags_ = 0;
159 // This could be generalized to a map, if more
160 // than one flag needs to expire independently.
163 };
164
165public:
166 HashRouter(Setup const& setup, Stopwatch& clock)
167 : setup_(setup), suppressionMap_(clock)
168 {
169 }
170
172 operator=(HashRouter const&) = delete;
173
174 virtual ~HashRouter() = default;
175
176 // VFALCO TODO Replace "Supression" terminology with something more
177 // semantically meaningful.
178 void
179 addSuppression(uint256 const& key);
180
181 bool
182 addSuppressionPeer(uint256 const& key, PeerShortID peer);
183
191
192 bool
193 addSuppressionPeer(uint256 const& key, PeerShortID peer, int& flags);
194
195 // Add a peer suppression and return whether the entry should be processed
196 bool
198 uint256 const& key,
199 PeerShortID peer,
200 int& flags,
201 std::chrono::seconds tx_interval);
202
207 bool
208 setFlags(uint256 const& key, int flags);
209
210 int
211 getFlags(uint256 const& key);
212
226 shouldRelay(uint256 const& key);
227
228private:
229 // pair.second indicates whether the entry was created
231 emplace(uint256 const&);
232
234
235 // Configurable parameters
237
238 // Stores all suppressed hashes and their expiration time
240 uint256,
241 Entry,
245};
246
249
250} // namespace ripple
251
252#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:91
void setFlags(int flagsToSet)
Definition: HashRouter.h:111
std::optional< Stopwatch::time_point > processed_
Definition: HashRouter.h:162
int getFlags(void) const
Definition: HashRouter.h:105
std::set< PeerShortID > peers_
Definition: HashRouter.h:158
std::optional< Stopwatch::time_point > relayed() const
Return seated relay time point if the message has been relayed.
Definition: HashRouter.h:125
std::optional< Stopwatch::time_point > relayed_
Definition: HashRouter.h:161
void addPeer(PeerShortID peer)
Definition: HashRouter.h:98
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition: HashRouter.h:118
bool shouldRelay(Stopwatch::time_point const &now, std::chrono::seconds relayTime)
Determines if this item should be relayed.
Definition: HashRouter.h:137
bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval)
Definition: HashRouter.h:148
Routing table for objects identified by hash.
Definition: HashRouter.h:58
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition: HashRouter.h:244
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Definition: HashRouter.cpp:119
bool shouldProcess(uint256 const &key, PeerShortID peer, int &flags, std::chrono::seconds tx_interval)
Definition: HashRouter.cpp:79
std::mutex mutex_
Definition: HashRouter.h:233
int getFlags(uint256 const &key)
Definition: HashRouter.cpp:95
virtual ~HashRouter()=default
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
Definition: HashRouter.cpp:52
Setup const setup_
Definition: HashRouter.h:236
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:58
HashRouter & operator=(HashRouter const &)=delete
HashRouter(Setup const &setup, Stopwatch &clock)
Definition: HashRouter.h:166
bool setFlags(uint256 const &key, int flags)
Set the flags on a hash.
Definition: HashRouter.cpp:103
std::pair< Entry &, bool > emplace(uint256 const &)
Definition: HashRouter.cpp:26
void addSuppression(uint256 const &key)
Definition: HashRouter.cpp:44
Seed functor once per construction.
Definition: hardened_hash.h:93
T emplace(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
base_uint< 256 > uint256
Definition: base_uint.h:558
HashRouter::Setup setup_HashRouter(Config const &config)
Definition: HashRouter.cpp:133
Structure used to customize HashRouter behavior.
Definition: HashRouter.h:72
Setup()=default
Default constructor.
seconds holdTime
Expiration time for a hash entry.
Definition: HashRouter.h:80
seconds relayTime
Amount of time required before a relayed item will be relayed again.
Definition: HashRouter.h:84