rippled
Loading...
Searching...
No Matches
HashRouter.cpp
1#include <xrpld/app/misc/HashRouter.h>
2#include <xrpld/core/Config.h>
3
4namespace ripple {
5
6auto
8{
9 auto iter = suppressionMap_.find(key);
10
11 if (iter != suppressionMap_.end())
12 {
13 suppressionMap_.touch(iter);
14 return std::make_pair(std::ref(iter->second), false);
15 }
16
17 // See if any supressions need to be expired
18 expire(suppressionMap_, setup_.holdTime);
19
20 return std::make_pair(
21 std::ref(suppressionMap_.emplace(key, Entry()).first->second), true);
22}
23
24void
26{
28
29 emplace(key);
30}
31
32bool
34{
35 return addSuppressionPeerWithStatus(key, peer).first;
36}
37
40{
42
43 auto result = emplace(key);
44 result.first.addPeer(peer);
45 return {result.second, result.first.relayed()};
46}
47
48bool
50 uint256 const& key,
51 PeerShortID peer,
52 HashRouterFlags& flags)
53{
55
56 auto [s, created] = emplace(key);
57 s.addPeer(peer);
58 flags = s.getFlags();
59 return created;
60}
61
62bool
64 uint256 const& key,
65 PeerShortID peer,
66 HashRouterFlags& flags,
67 std::chrono::seconds tx_interval)
68{
70
71 auto result = emplace(key);
72 auto& s = result.first;
73 s.addPeer(peer);
74 flags = s.getFlags();
75 return s.shouldProcess(suppressionMap_.clock().now(), tx_interval);
76}
77
80{
82
83 return emplace(key).first.getFlags();
84}
85
86bool
88{
89 XRPL_ASSERT(
90 static_cast<bool>(flags), "ripple::HashRouter::setFlags : valid input");
91
93
94 auto& s = emplace(key).first;
95
96 if ((s.getFlags() & flags) == flags)
97 return false;
98
99 s.setFlags(flags);
100 return true;
101}
102
103auto
106{
107 std::lock_guard lock(mutex_);
108
109 auto& s = emplace(key).first;
110
111 if (!s.shouldRelay(suppressionMap_.clock().now(), setup_.relayTime))
112 return {};
113
114 return s.releasePeerSet();
115}
116
119{
120 using namespace std::chrono;
121
122 HashRouter::Setup setup;
123 auto const& section = config.section("hashrouter");
124
125 std::int32_t tmp;
126
127 if (set(tmp, "hold_time", section))
128 {
129 if (tmp < 12)
130 Throw<std::runtime_error>(
131 "HashRouter hold time must be at least 12 seconds (the "
132 "approximate validation time for three ledgers).");
133 setup.holdTime = seconds(tmp);
134 }
135 if (set(tmp, "relay_time", section))
136 {
137 if (tmp < 8)
138 Throw<std::runtime_error>(
139 "HashRouter relay time must be at least 8 seconds (the "
140 "approximate validation time for two ledgers).");
141 setup.relayTime = seconds(tmp);
142 }
143 if (setup.relayTime > setup.holdTime)
144 {
145 Throw<std::runtime_error>(
146 "HashRouter relay time must be less than or equal to hold time");
147 }
148
149 return setup;
150}
151
152} // namespace ripple
Section & section(std::string const &name)
Returns the section with the given name.
An entry in the routing table.
Definition HashRouter.h:111
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition HashRouter.h:267
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:256
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 &)
Definition HashRouter.cpp:7
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:6
HashRouterFlags
Definition HashRouter.h:15
HashRouter::Setup setup_HashRouter(Config const &config)
T ref(T... args)
Structure used to customize HashRouter behavior.
Definition HashRouter.h:92
seconds holdTime
Expiration time for a hash entry.
Definition HashRouter.h:100
seconds relayTime
Amount of time required before a relayed item will be relayed again.
Definition HashRouter.h:104