rippled
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 <ripple/basics/CountedObject.h>
24 #include <ripple/basics/UnorderedContainers.h>
25 #include <ripple/basics/base_uint.h>
26 #include <ripple/basics/chrono.h>
27 #include <ripple/beast/container/aged_unordered_map.h>
28 
29 #include <optional>
30 
31 namespace 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 {
55 public:
56  // The type here *MUST* match the type of Peer::id_t
58 
59 private:
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 
97  relayed() const
98  {
99  return relayed_;
100  }
101 
108  bool
110  Stopwatch::time_point const& now,
111  std::chrono::seconds holdTime)
112  {
113  if (relayed_ && *relayed_ + holdTime > now)
114  return false;
115  relayed_.emplace(now);
116  return true;
117  }
118 
127  bool
129  {
130  return ++recoveries_ % limit != 0;
131  }
132 
133  bool
135  {
136  if (processed_ && ((*processed_ + interval) > now))
137  return false;
138  processed_.emplace(now);
139  return true;
140  }
141 
142  private:
143  int flags_ = 0;
145  // This could be generalized to a map, if more
146  // than one flag needs to expire independently.
150  };
151 
152 public:
153  static inline std::chrono::seconds
155  {
156  using namespace std::chrono;
157 
158  return 300s;
159  }
160 
161  static inline std::uint32_t
163  {
164  return 1;
165  }
166 
168  Stopwatch& clock,
169  std::chrono::seconds entryHoldTimeInSeconds,
170  std::uint32_t recoverLimit)
171  : suppressionMap_(clock)
172  , holdTime_(entryHoldTimeInSeconds)
173  , recoverLimit_(recoverLimit + 1u)
174  {
175  }
176 
177  HashRouter&
178  operator=(HashRouter const&) = delete;
179 
180  virtual ~HashRouter() = default;
181 
182  // VFALCO TODO Replace "Supression" terminology with something more
183  // semantically meaningful.
184  void
185  addSuppression(uint256 const& key);
186 
187  bool
188  addSuppressionPeer(uint256 const& key, PeerShortID peer);
189 
197 
198  bool
199  addSuppressionPeer(uint256 const& key, PeerShortID peer, int& flags);
200 
201  // Add a peer suppression and return whether the entry should be processed
202  bool
204  uint256 const& key,
205  PeerShortID peer,
206  int& flags,
207  std::chrono::seconds tx_interval);
208 
213  bool
214  setFlags(uint256 const& key, int flags);
215 
216  int
217  getFlags(uint256 const& key);
218 
232  shouldRelay(uint256 const& key);
233 
240  bool
241  shouldRecover(uint256 const& key);
242 
243 private:
244  // pair.second indicates whether the entry was created
246  emplace(uint256 const&);
247 
249 
250  // Stores all suppressed hashes and their expiration time
252  uint256,
253  Entry,
257 
259 
261 };
262 
263 } // namespace ripple
264 
265 #endif
ripple::HashRouter::Entry::shouldRelay
bool shouldRelay(Stopwatch::time_point const &now, std::chrono::seconds holdTime)
Determines if this item should be relayed.
Definition: HashRouter.h:109
ripple::HashRouter::addSuppressionPeerWithStatus
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
ripple::HashRouter::Entry::setFlags
void setFlags(int flagsToSet)
Definition: HashRouter.h:83
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::HashRouter::addSuppressionPeer
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
Definition: HashRouter.cpp:51
ripple::HashRouter::emplace
std::pair< Entry &, bool > emplace(uint256 const &)
Definition: HashRouter.cpp:25
ripple::HashRouter::getFlags
int getFlags(uint256 const &key)
Definition: HashRouter.cpp:94
ripple::HashRouter::mutex_
std::mutex mutex_
Definition: HashRouter.h:248
ripple::HashRouter::Entry::releasePeerSet
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition: HashRouter.h:90
std::pair
ripple::HashRouter::Entry
An entry in the routing table.
Definition: HashRouter.h:62
ripple::HashRouter::shouldProcess
bool shouldProcess(uint256 const &key, PeerShortID peer, int &flags, std::chrono::seconds tx_interval)
Definition: HashRouter.cpp:78
std::chrono::seconds
std::optional::emplace
T emplace(T... args)
ripple::HashRouter::Entry::getFlags
int getFlags(void) const
Definition: HashRouter.h:77
ripple::HashRouter::Entry::relayed_
std::optional< Stopwatch::time_point > relayed_
Definition: HashRouter.h:147
beast::abstract_clock< std::chrono::steady_clock >::clock_type
std::chrono::steady_clock clock_type
Definition: abstract_clock.h:64
ripple::HashRouter::Entry::processed_
std::optional< Stopwatch::time_point > processed_
Definition: HashRouter.h:148
ripple::HashRouter
Routing table for objects identified by hash.
Definition: HashRouter.h:53
ripple::HashRouter::~HashRouter
virtual ~HashRouter()=default
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:529
ripple::HashRouter::Entry::shouldRecover
bool shouldRecover(std::uint32_t limit)
Determines if this item should be recovered from the open ledger.
Definition: HashRouter.h:128
ripple::HashRouter::HashRouter
HashRouter(Stopwatch &clock, std::chrono::seconds entryHoldTimeInSeconds, std::uint32_t recoverLimit)
Definition: HashRouter.h:167
ripple::base_uint< 256 >
ripple::HashRouter::getDefaultRecoverLimit
static std::uint32_t getDefaultRecoverLimit()
Definition: HashRouter.h:162
ripple::HashRouter::Entry::relayed
std::optional< Stopwatch::time_point > relayed() const
Return seated relay time point if the message has been relayed.
Definition: HashRouter.h:97
ripple::HashRouter::suppressionMap_
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition: HashRouter.h:256
ripple::HashRouter::holdTime_
const std::chrono::seconds holdTime_
Definition: HashRouter.h:258
ripple::HashRouter::getDefaultHoldTime
static std::chrono::seconds getDefaultHoldTime()
Definition: HashRouter.h:154
ripple::HashRouter::addSuppression
void addSuppression(uint256 const &key)
Definition: HashRouter.cpp:43
ripple::hardened_hash
Seed functor once per construction.
Definition: hardened_hash.h:96
ripple::HashRouter::shouldRecover
bool shouldRecover(uint256 const &key)
Determines whether the hashed item should be recovered from the open ledger into the next open ledger...
Definition: HashRouter.cpp:132
std::uint32_t
ripple::HashRouter::shouldRelay
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Definition: HashRouter.cpp:118
ripple::HashRouter::operator=
HashRouter & operator=(HashRouter const &)=delete
ripple::HashRouter::Entry::recoveries_
std::uint32_t recoveries_
Definition: HashRouter.h:149
beast::abstract_clock< std::chrono::steady_clock >
ripple::HashRouter::Entry::addPeer
void addPeer(PeerShortID peer)
Definition: HashRouter.h:70
beast::detail::aged_unordered_container
Associative container where each element is also indexed by time.
Definition: aged_unordered_container.h:85
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::HashRouter::Entry::shouldProcess
bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval)
Definition: HashRouter.h:134
ripple::HashRouter::PeerShortID
std::uint32_t PeerShortID
Definition: HashRouter.h:57
ripple::HashRouter::Entry::flags_
int flags_
Definition: HashRouter.h:143
optional
std::mutex
STL class.
ripple::HashRouter::recoverLimit_
const std::uint32_t recoverLimit_
Definition: HashRouter.h:260
beast::abstract_clock< std::chrono::steady_clock >::time_point
typename std::chrono::steady_clock ::time_point time_point
Definition: abstract_clock.h:63
std::set
STL class.
ripple::HashRouter::setFlags
bool setFlags(uint256 const &key, int flags)
Set the flags on a hash.
Definition: HashRouter.cpp:102
ripple::HashRouter::Entry::Entry
Entry()
Definition: HashRouter.h:65
ripple::HashRouter::Entry::peers_
std::set< PeerShortID > peers_
Definition: HashRouter.h:144
std::chrono