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
93int
95{
97
98 return emplace(key).first.getFlags();
99}
100
101bool
102HashRouter::setFlags(uint256 const& key, int flags)
103{
104 XRPL_ASSERT(flags, "ripple::HashRouter::setFlags : valid input");
105
107
108 auto& s = emplace(key).first;
109
110 if ((s.getFlags() & flags) == flags)
111 return false;
112
113 s.setFlags(flags);
114 return true;
115}
116
117auto
120{
121 std::lock_guard lock(mutex_);
122
123 auto& s = emplace(key).first;
124
125 if (!s.shouldRelay(suppressionMap_.clock().now(), holdTime_))
126 return {};
127
128 return s.releasePeerSet();
129}
130
131} // 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:221
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Definition: HashRouter.cpp:118
bool shouldProcess(uint256 const &key, PeerShortID peer, int &flags, std::chrono::seconds tx_interval)
Definition: HashRouter.cpp:78
std::mutex mutex_
Definition: HashRouter.h:213
int getFlags(uint256 const &key)
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:102
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)