rippled
Loading...
Searching...
No Matches
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 <xrpld/app/misc/HashRouter.h>
21#include <xrpld/core/Config.h>
22
23namespace ripple {
24
25auto
27{
28 auto iter = suppressionMap_.find(key);
29
30 if (iter != suppressionMap_.end())
31 {
32 suppressionMap_.touch(iter);
33 return std::make_pair(std::ref(iter->second), false);
34 }
35
36 // See if any supressions need to be expired
37 expire(suppressionMap_, setup_.holdTime);
38
39 return std::make_pair(
40 std::ref(suppressionMap_.emplace(key, Entry()).first->second), true);
41}
42
43void
45{
47
48 emplace(key);
49}
50
51bool
53{
54 return addSuppressionPeerWithStatus(key, peer).first;
55}
56
59{
61
62 auto result = emplace(key);
63 result.first.addPeer(peer);
64 return {result.second, result.first.relayed()};
65}
66
67bool
69 uint256 const& key,
70 PeerShortID peer,
71 HashRouterFlags& flags)
72{
74
75 auto [s, created] = emplace(key);
76 s.addPeer(peer);
77 flags = s.getFlags();
78 return created;
79}
80
81bool
83 uint256 const& key,
84 PeerShortID peer,
85 HashRouterFlags& flags,
86 std::chrono::seconds tx_interval)
87{
89
90 auto result = emplace(key);
91 auto& s = result.first;
92 s.addPeer(peer);
93 flags = s.getFlags();
94 return s.shouldProcess(suppressionMap_.clock().now(), tx_interval);
95}
96
99{
101
102 return emplace(key).first.getFlags();
103}
104
105bool
107{
108 XRPL_ASSERT(
109 static_cast<bool>(flags), "ripple::HashRouter::setFlags : valid input");
110
112
113 auto& s = emplace(key).first;
114
115 if ((s.getFlags() & flags) == flags)
116 return false;
117
118 s.setFlags(flags);
119 return true;
120}
121
122auto
125{
126 std::lock_guard lock(mutex_);
127
128 auto& s = emplace(key).first;
129
130 if (!s.shouldRelay(suppressionMap_.clock().now(), setup_.relayTime))
131 return {};
132
133 return s.releasePeerSet();
134}
135
138{
139 using namespace std::chrono;
140
141 HashRouter::Setup setup;
142 auto const& section = config.section("hashrouter");
143
144 std::int32_t tmp;
145
146 if (set(tmp, "hold_time", section))
147 {
148 if (tmp < 12)
149 Throw<std::runtime_error>(
150 "HashRouter hold time must be at least 12 seconds (the "
151 "approximate validation time for three ledgers).");
152 setup.holdTime = seconds(tmp);
153 }
154 if (set(tmp, "relay_time", section))
155 {
156 if (tmp < 8)
157 Throw<std::runtime_error>(
158 "HashRouter relay time must be at least 8 seconds (the "
159 "approximate validation time for two ledgers).");
160 setup.relayTime = seconds(tmp);
161 }
162 if (setup.relayTime > setup.holdTime)
163 {
164 Throw<std::runtime_error>(
165 "HashRouter relay time must be less than or equal to hold time");
166 }
167
168 return setup;
169}
170
171} // namespace ripple
Section & section(std::string const &name)
Returns the section with the given name.
An entry in the routing table.
Definition HashRouter.h:130
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition HashRouter.h:286
HashRouterFlags getFlags(uint256 const &key)
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
std::mutex mutex_
Definition HashRouter.h:275
bool shouldProcess(uint256 const &key, PeerShortID peer, HashRouterFlags &flags, std::chrono::seconds tx_interval)
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
bool setFlags(uint256 const &key, HashRouterFlags flags)
Set the flags on a hash.
std::pair< bool, std::optional< Stopwatch::time_point > > addSuppressionPeerWithStatus(uint256 const &key, PeerShortID peer)
Add a suppression peer and get message's relay status.
std::pair< Entry &, bool > emplace(uint256 const &)
void addSuppression(uint256 const &key)
T make_pair(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
HashRouterFlags
Definition HashRouter.h:34
HashRouter::Setup setup_HashRouter(Config const &config)
T ref(T... args)
Structure used to customize HashRouter behavior.
Definition HashRouter.h:111
seconds holdTime
Expiration time for a hash entry.
Definition HashRouter.h:119
seconds relayTime
Amount of time required before a relayed item will be relayed again.
Definition HashRouter.h:123