rippled
HashRouter.cpp
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 #include <ripple/app/misc/HashRouter.h>
21 
22 namespace ripple {
23 
24 auto
27 {
28  auto iter = suppressionMap_.find (key);
29 
30  if (iter != suppressionMap_.end ())
31  {
32  suppressionMap_.touch(iter);
33  return std::make_pair(
34  std::ref(iter->second), false);
35  }
36 
37  // See if any supressions need to be expired
38  expire(suppressionMap_, holdTime_);
39 
40  return std::make_pair(std::ref(
41  suppressionMap_.emplace (
42  key, Entry ()).first->second),
43  true);
44 }
45 
47 {
48  std::lock_guard lock (mutex_);
49 
50  emplace (key);
51 }
52 
54 {
55  std::lock_guard lock (mutex_);
56 
57  auto result = emplace(key);
58  result.first.addPeer(peer);
59  return result.second;
60 }
61 
62 bool HashRouter::addSuppressionPeer (uint256 const& key, PeerShortID peer, int& flags)
63 {
64  std::lock_guard lock (mutex_);
65 
66  auto [s, created] = emplace(key);
67  s.addPeer (peer);
68  flags = s.getFlags ();
69  return created;
70 }
71 
73  int& flags, std::chrono::seconds tx_interval)
74 {
75  std::lock_guard lock (mutex_);
76 
77  auto result = emplace(key);
78  auto& s = result.first;
79  s.addPeer (peer);
80  flags = s.getFlags ();
81  return s.shouldProcess (suppressionMap_.clock().now(), tx_interval);
82 }
83 
84 int HashRouter::getFlags (uint256 const& key)
85 {
86  std::lock_guard lock (mutex_);
87 
88  return emplace(key).first.getFlags ();
89 }
90 
91 bool HashRouter::setFlags (uint256 const& key, int flags)
92 {
93  assert (flags != 0);
94 
95  std::lock_guard lock (mutex_);
96 
97  auto& s = emplace(key).first;
98 
99  if ((s.getFlags () & flags) == flags)
100  return false;
101 
102  s.setFlags (flags);
103  return true;
104 }
105 
106 auto
108  -> boost::optional<std::set<PeerShortID>>
109 {
110  std::lock_guard lock (mutex_);
111 
112  auto& s = emplace(key).first;
113 
114  if (!s.shouldRelay(suppressionMap_.clock().now(), holdTime_))
115  return boost::none;
116 
117  return s.releasePeerSet();
118 }
119 
120 bool
122 {
123  std::lock_guard lock(mutex_);
124 
125  auto& s = emplace(key).first;
126 
127  return s.shouldRecover(recoverLimit_);
128 }
129 
130 } // ripple
ripple::HashRouter::addSuppressionPeer
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
Definition: HashRouter.cpp:53
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:84
ripple::HashRouter::mutex_
std::mutex mutex_
Definition: HashRouter.h:211
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:72
std::chrono::seconds
std::lock_guard
STL class.
ripple::base_uint
Definition: base_uint.h:65
ripple::HashRouter::addSuppression
void addSuppression(uint256 const &key)
Definition: HashRouter.cpp:46
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:121
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:107
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::make_pair
T make_pair(T... args)
ripple::HashRouter::recoverLimit_
const std::uint32_t recoverLimit_
Definition: HashRouter.h:219
ripple::HashRouter::suppressionMap_
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition: HashRouter.h:215
ripple::HashRouter::setFlags
bool setFlags(uint256 const &key, int flags)
Set the flags on a hash.
Definition: HashRouter.cpp:91
std::ref
T ref(T... args)