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{
71
72 auto [s, created] = emplace(key);
73 s.addPeer(peer);
74 flags = s.getFlags();
75 return created;
76}
77
78bool
80 uint256 const& key,
81 PeerShortID peer,
82 int& flags,
83 std::chrono::seconds tx_interval)
84{
86
87 auto result = emplace(key);
88 auto& s = result.first;
89 s.addPeer(peer);
90 flags = s.getFlags();
91 return s.shouldProcess(suppressionMap_.clock().now(), tx_interval);
92}
93
94int
96{
98
99 return emplace(key).first.getFlags();
100}
101
102bool
104{
105 XRPL_ASSERT(flags, "ripple::HashRouter::setFlags : valid input");
106
108
109 auto& s = emplace(key).first;
110
111 if ((s.getFlags() & flags) == flags)
112 return false;
113
114 s.setFlags(flags);
115 return true;
116}
117
118auto
121{
122 std::lock_guard lock(mutex_);
123
124 auto& s = emplace(key).first;
125
126 if (!s.shouldRelay(suppressionMap_.clock().now(), setup_.relayTime))
127 return {};
128
129 return s.releasePeerSet();
130}
131
134{
135 using namespace std::chrono;
136
137 HashRouter::Setup setup;
138 auto const& section = config.section("hashrouter");
139
140 std::int32_t tmp;
141
142 if (set(tmp, "hold_time", section))
143 {
144 if (tmp < 12)
145 Throw<std::runtime_error>(
146 "HashRouter hold time must be at least 12 seconds (the "
147 "approximate validation time for three ledgers).");
148 setup.holdTime = seconds(tmp);
149 }
150 if (set(tmp, "relay_time", section))
151 {
152 if (tmp < 8)
153 Throw<std::runtime_error>(
154 "HashRouter relay time must be at least 8 seconds (the "
155 "approximate validation time for two ledgers).");
156 setup.relayTime = seconds(tmp);
157 }
158 if (setup.relayTime > setup.holdTime)
159 {
160 Throw<std::runtime_error>(
161 "HashRouter relay time must be less than or equal to hold time");
162 }
163
164 return setup;
165}
166
167} // namespace ripple
Section & section(std::string const &name)
Returns the section with the given name.
An entry in the routing table.
Definition: HashRouter.h:90
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
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
Definition: HashRouter.cpp:52
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
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
Match set account flags.
Definition: flags.h:125
T make_pair(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
HashRouter::Setup setup_HashRouter(Config const &config)
Definition: HashRouter.cpp:133
T ref(T... args)
Structure used to customize HashRouter behavior.
Definition: HashRouter.h:71
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