rippled
Loading...
Searching...
No Matches
HashRouter.cpp
1#include <xrpld/app/misc/HashRouter.h>
2#include <xrpld/core/Config.h>
3
4namespace xrpl {
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 suppressions need to be expired
18 expire(suppressionMap_, setup_.holdTime);
19
20 return std::make_pair(std::ref(suppressionMap_.emplace(key, Entry()).first->second), true);
21}
22
23void
25{
27
28 emplace(key);
29}
30
31bool
33{
34 return addSuppressionPeerWithStatus(key, peer).first;
35}
36
39{
41
42 auto result = emplace(key);
43 result.first.addPeer(peer);
44 return {result.second, result.first.relayed()};
45}
46
47bool
49{
51
52 auto [s, created] = emplace(key);
53 s.addPeer(peer);
54 flags = s.getFlags();
55 return created;
56}
57
58bool
60 uint256 const& key,
61 PeerShortID peer,
62 HashRouterFlags& flags,
63 std::chrono::seconds tx_interval)
64{
66
67 auto result = emplace(key);
68 auto& s = result.first;
69 s.addPeer(peer);
70 flags = s.getFlags();
71 return s.shouldProcess(suppressionMap_.clock().now(), tx_interval);
72}
73
76{
78
79 return emplace(key).first.getFlags();
80}
81
82bool
84{
85 XRPL_ASSERT(static_cast<bool>(flags), "xrpl::HashRouter::setFlags : valid input");
86
88
89 auto& s = emplace(key).first;
90
91 if ((s.getFlags() & flags) == flags)
92 return false;
93
94 s.setFlags(flags);
95 return true;
96}
97
98auto
100{
101 std::lock_guard lock(mutex_);
102
103 auto& s = emplace(key).first;
104
105 if (!s.shouldRelay(suppressionMap_.clock().now(), setup_.relayTime))
106 return {};
107
108 return s.releasePeerSet();
109}
110
113{
114 using namespace std::chrono;
115
116 HashRouter::Setup setup;
117 auto const& section = config.section("hashrouter");
118
119 std::int32_t tmp;
120
121 if (set(tmp, "hold_time", section))
122 {
123 if (tmp < 12)
124 Throw<std::runtime_error>(
125 "HashRouter hold time must be at least 12 seconds (the "
126 "approximate validation time for three ledgers).");
127 setup.holdTime = seconds(tmp);
128 }
129 if (set(tmp, "relay_time", section))
130 {
131 if (tmp < 8)
132 Throw<std::runtime_error>(
133 "HashRouter relay time must be at least 8 seconds (the "
134 "approximate validation time for two ledgers).");
135 setup.relayTime = seconds(tmp);
136 }
137 if (setup.relayTime > setup.holdTime)
138 {
139 Throw<std::runtime_error>("HashRouter relay time must be less than or equal to hold time");
140 }
141
142 return setup;
143}
144
145} // namespace xrpl
Section & section(std::string const &name)
Returns the section with the given name.
An entry in the routing table.
Definition HashRouter.h:111
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
HashRouterFlags getFlags(uint256 const &key)
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
std::mutex mutex_
Definition HashRouter.h:246
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
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition HashRouter.h:252
void addSuppression(uint256 const &key)
bool shouldProcess(uint256 const &key, PeerShortID peer, HashRouterFlags &flags, std::chrono::seconds tx_interval)
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