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