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
22namespace ripple {
23
24auto
26{
27 auto iter = suppressionMap_.find(key);
28
29 if (iter != suppressionMap_.end())
30 {
31 suppressionMap_.touch(iter);
32 return std::make_pair(std::ref(iter->second), false);
33 }
34
35 // See if any supressions need to be expired
36 expire(suppressionMap_, holdTime_);
37
38 return std::make_pair(
39 std::ref(suppressionMap_.emplace(key, Entry()).first->second), true);
40}
41
42void
44{
46
47 emplace(key);
48}
49
50bool
52{
53 return addSuppressionPeerWithStatus(key, peer).first;
54}
55
58{
60
61 auto result = emplace(key);
62 result.first.addPeer(peer);
63 return {result.second, result.first.relayed()};
64}
65
66bool
68{
70
71 auto [s, created] = emplace(key);
72 s.addPeer(peer);
73 flags = s.getFlags();
74 return created;
75}
76
77bool
79 uint256 const& key,
80 PeerShortID peer,
81 int& flags,
82 std::chrono::seconds tx_interval)
83{
85
86 auto result = emplace(key);
87 auto& s = result.first;
88 s.addPeer(peer);
89 flags = s.getFlags();
90 return s.shouldProcess(suppressionMap_.clock().now(), tx_interval);
91}
92
93bool
95 uint256 const& key,
96 PeerShortID peer,
97 std::chrono::seconds interval)
98{
100
101 auto& entry = emplace(key).first;
102
103 return entry.shouldProcessForPeer(
104 peer, suppressionMap_.clock().now(), interval);
105}
106
107int
109{
111
112 return emplace(key).first.getFlags();
113}
114
115bool
116HashRouter::setFlags(uint256 const& key, int flags)
117{
118 XRPL_ASSERT(flags, "ripple::HashRouter::setFlags : valid input");
119
121
122 auto& s = emplace(key).first;
123
124 if ((s.getFlags() & flags) == flags)
125 return false;
126
127 s.setFlags(flags);
128 return true;
129}
130
131auto
134{
135 std::lock_guard lock(mutex_);
136
137 auto& s = emplace(key).first;
138
139 if (!s.shouldRelay(suppressionMap_.clock().now(), holdTime_))
140 return {};
141
142 return s.releasePeerSet();
143}
144
145auto
147{
148 std::lock_guard lock(mutex_);
149
150 auto& s = emplace(key).first;
151 return s.peekPeerSet();
152}
153
154} // namespace ripple
An entry in the routing table.
Definition: HashRouter.h:63
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition: HashRouter.h:261
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Definition: HashRouter.cpp:132
bool shouldProcess(uint256 const &key, PeerShortID peer, int &flags, std::chrono::seconds tx_interval)
Definition: HashRouter.cpp:78
std::mutex mutex_
Definition: HashRouter.h:253
int getFlags(uint256 const &key)
Definition: HashRouter.cpp:108
bool shouldProcessForPeer(uint256 const &key, PeerShortID peer, std::chrono::seconds interval)
Determines whether the hashed item should be processed for the given peer.
Definition: HashRouter.cpp:94
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
Definition: HashRouter.cpp:51
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:57
bool setFlags(uint256 const &key, int flags)
Set the flags on a hash.
Definition: HashRouter.cpp:116
std::set< PeerShortID > getPeers(uint256 const &key)
Returns a copy of the set of peers in the Entry for the key.
Definition: HashRouter.cpp:146
std::pair< Entry &, bool > emplace(uint256 const &)
Definition: HashRouter.cpp:25
void addSuppression(uint256 const &key)
Definition: HashRouter.cpp:43
T make_pair(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
T ref(T... args)