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 #include <boost/optional.hpp>
29 
30 namespace ripple {
31 
32 // TODO convert these macros to int constants or an enum
33 #define SF_BAD 0x02 // Temporarily bad
34 #define SF_SAVED 0x04
35 #define SF_TRUSTED 0x10 // comes from trusted source
36 
37 // Private flags, used internally in apply.cpp.
38 // Do not attempt to read, set, or reuse.
39 #define SF_PRIVATE1 0x0100
40 #define SF_PRIVATE2 0x0200
41 #define SF_PRIVATE3 0x0400
42 #define SF_PRIVATE4 0x0800
43 #define SF_PRIVATE5 0x1000
44 #define SF_PRIVATE6 0x2000
45 
53 {
54 public:
55  // The type here *MUST* match the type of Peer::id_t
57 
58 private:
61  class Entry : public CountedObject<Entry>
62  {
63  public:
64  static char const*
66  {
67  return "HashRouterEntry";
68  }
69 
71  {
72  }
73 
74  void
76  {
77  if (peer != 0)
78  peers_.insert(peer);
79  }
80 
81  int
82  getFlags(void) const
83  {
84  return flags_;
85  }
86 
87  void
88  setFlags(int flagsToSet)
89  {
90  flags_ |= flagsToSet;
91  }
92 
96  {
97  return std::move(peers_);
98  }
99 
106  bool
108  Stopwatch::time_point const& now,
109  std::chrono::seconds holdTime)
110  {
111  if (relayed_ && *relayed_ + holdTime > now)
112  return false;
113  relayed_.emplace(now);
114  return true;
115  }
116 
125  bool
127  {
128  return ++recoveries_ % limit != 0;
129  }
130 
131  bool
133  {
134  if (processed_ && ((*processed_ + interval) > now))
135  return false;
136  processed_.emplace(now);
137  return true;
138  }
139 
140  private:
141  int flags_ = 0;
143  // This could be generalized to a map, if more
144  // than one flag needs to expire independently.
145  boost::optional<Stopwatch::time_point> relayed_;
146  boost::optional<Stopwatch::time_point> processed_;
148  };
149 
150 public:
151  static inline std::chrono::seconds
153  {
154  using namespace std::chrono;
155 
156  return 300s;
157  }
158 
159  static inline std::uint32_t
161  {
162  return 1;
163  }
164 
166  Stopwatch& clock,
167  std::chrono::seconds entryHoldTimeInSeconds,
168  std::uint32_t recoverLimit)
169  : suppressionMap_(clock)
170  , holdTime_(entryHoldTimeInSeconds)
171  , recoverLimit_(recoverLimit + 1u)
172  {
173  }
174 
175  HashRouter&
176  operator=(HashRouter const&) = delete;
177 
178  virtual ~HashRouter() = default;
179 
180  // VFALCO TODO Replace "Supression" terminology with something more
181  // semantically meaningful.
182  void
183  addSuppression(uint256 const& key);
184 
185  bool
186  addSuppressionPeer(uint256 const& key, PeerShortID peer);
187 
188  bool
189  addSuppressionPeer(uint256 const& key, PeerShortID peer, int& flags);
190 
191  // Add a peer suppression and return whether the entry should be processed
192  bool
194  uint256 const& key,
195  PeerShortID peer,
196  int& flags,
197  std::chrono::seconds tx_interval);
198 
203  bool
204  setFlags(uint256 const& key, int flags);
205 
206  int
207  getFlags(uint256 const& key);
208 
221  boost::optional<std::set<PeerShortID>>
222  shouldRelay(uint256 const& key);
223 
230  bool
231  shouldRecover(uint256 const& key);
232 
233 private:
234  // pair.second indicates whether the entry was created
236  emplace(uint256 const&);
237 
239 
240  // Stores all suppressed hashes and their expiration time
242  uint256,
243  Entry,
247 
249 
251 };
252 
253 } // namespace ripple
254 
255 #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:107
ripple::HashRouter::Entry::setFlags
void setFlags(int flagsToSet)
Definition: HashRouter.h:88
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:110
ripple::HashRouter::addSuppressionPeer
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
Definition: HashRouter.cpp:51
ripple::HashRouter::Entry::relayed_
boost::optional< Stopwatch::time_point > relayed_
Definition: HashRouter.h:145
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:88
ripple::HashRouter::mutex_
std::mutex mutex_
Definition: HashRouter.h:238
ripple::HashRouter::Entry::releasePeerSet
std::set< PeerShortID > releasePeerSet()
Return set of peers we've relayed to and reset tracking.
Definition: HashRouter.h:95
std::pair
ripple::HashRouter::Entry
An entry in the routing table.
Definition: HashRouter.h:61
ripple::HashRouter::shouldProcess
bool shouldProcess(uint256 const &key, PeerShortID peer, int &flags, std::chrono::seconds tx_interval)
Definition: HashRouter.cpp:72
std::chrono::seconds
ripple::HashRouter::Entry::getFlags
int getFlags(void) const
Definition: HashRouter.h:82
beast::abstract_clock< std::chrono::steady_clock >::clock_type
std::chrono::steady_clock clock_type
Definition: abstract_clock.h:64
ripple::HashRouter
Routing table for objects identified by hash.
Definition: HashRouter.h:52
ripple::HashRouter::~HashRouter
virtual ~HashRouter()=default
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:493
ripple::HashRouter::Entry::shouldRecover
bool shouldRecover(std::uint32_t limit)
Determines if this item should be recovered from the open ledger.
Definition: HashRouter.h:126
ripple::HashRouter::HashRouter
HashRouter(Stopwatch &clock, std::chrono::seconds entryHoldTimeInSeconds, std::uint32_t recoverLimit)
Definition: HashRouter.h:165
ripple::base_uint< 256 >
ripple::HashRouter::Entry::getCountedObjectName
static char const * getCountedObjectName()
Definition: HashRouter.h:65
ripple::HashRouter::getDefaultRecoverLimit
static std::uint32_t getDefaultRecoverLimit()
Definition: HashRouter.h:160
ripple::HashRouter::suppressionMap_
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition: HashRouter.h:246
ripple::HashRouter::holdTime_
const std::chrono::seconds holdTime_
Definition: HashRouter.h:248
ripple::HashRouter::getDefaultHoldTime
static std::chrono::seconds getDefaultHoldTime()
Definition: HashRouter.h:152
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:126
std::uint32_t
ripple::HashRouter::shouldRelay
boost::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Definition: HashRouter.cpp:112
ripple::HashRouter::operator=
HashRouter & operator=(HashRouter const &)=delete
ripple::HashRouter::Entry::processed_
boost::optional< Stopwatch::time_point > processed_
Definition: HashRouter.h:146
ripple::HashRouter::Entry::recoveries_
std::uint32_t recoveries_
Definition: HashRouter.h:147
beast::abstract_clock< std::chrono::steady_clock >
ripple::HashRouter::Entry::addPeer
void addPeer(PeerShortID peer)
Definition: HashRouter.h:75
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:132
ripple::HashRouter::PeerShortID
std::uint32_t PeerShortID
Definition: HashRouter.h:56
ripple::HashRouter::Entry::flags_
int flags_
Definition: HashRouter.h:141
std::mutex
STL class.
ripple::HashRouter::recoverLimit_
const std::uint32_t recoverLimit_
Definition: HashRouter.h:250
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:96
ripple::HashRouter::Entry::Entry
Entry()
Definition: HashRouter.h:70
ripple::HashRouter::Entry::peers_
std::set< PeerShortID > peers_
Definition: HashRouter.h:142
std::chrono