rippled
ripple
app
misc
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 <ripple/app/misc/HashRouter.h>
21
22
namespace
ripple
{
23
24
auto
25
HashRouter::emplace
(
uint256
const
& key) ->
std::pair<Entry&, bool>
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
42
void
43
HashRouter::addSuppression
(
uint256
const
& key)
44
{
45
std::lock_guard
lock(
mutex_
);
46
47
emplace
(key);
48
}
49
50
bool
51
HashRouter::addSuppressionPeer
(
uint256
const
& key,
PeerShortID
peer)
52
{
53
return
addSuppressionPeerWithStatus
(key, peer).first;
54
}
55
56
std::pair<bool, std::optional<Stopwatch::time_point>
>
57
HashRouter::addSuppressionPeerWithStatus
(
const
uint256
& key,
PeerShortID
peer)
58
{
59
std::lock_guard
lock(
mutex_
);
60
61
auto
result =
emplace
(key);
62
result.first.addPeer(peer);
63
return
{result.second, result.first.relayed()};
64
}
65
66
bool
67
HashRouter::addSuppressionPeer
(
uint256
const
& key,
PeerShortID
peer,
int
& flags)
68
{
69
std::lock_guard
lock(
mutex_
);
70
71
auto
[s, created] =
emplace
(key);
72
s.addPeer(peer);
73
flags = s.getFlags();
74
return
created;
75
}
76
77
bool
78
HashRouter::shouldProcess
(
79
uint256
const
& key,
80
PeerShortID
peer,
81
int
& flags,
82
std::chrono::seconds
tx_interval)
83
{
84
std::lock_guard
lock(
mutex_
);
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
93
int
94
HashRouter::getFlags
(
uint256
const
& key)
95
{
96
std::lock_guard
lock(
mutex_
);
97
98
return
emplace
(key).first.getFlags();
99
}
100
101
bool
102
HashRouter::setFlags
(
uint256
const
& key,
int
flags)
103
{
104
assert(flags != 0);
105
106
std::lock_guard
lock(
mutex_
);
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
117
auto
118
HashRouter::shouldRelay
(
uint256
const
& key)
119
->
std::optional<std::set<PeerShortID>
>
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
ripple::HashRouter::addSuppressionPeerWithStatus
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
ripple::HashRouter::addSuppressionPeer
bool addSuppressionPeer(uint256 const &key, PeerShortID peer)
Definition:
HashRouter.cpp:51
ripple::HashRouter::emplace
std::pair< Entry &, bool > emplace(uint256 const &)
Definition:
HashRouter.cpp:25
ripple::HashRouter::getFlags
int getFlags(uint256 const &key)
Definition:
HashRouter.cpp:94
ripple::HashRouter::mutex_
std::mutex mutex_
Definition:
HashRouter.h:213
std::pair
ripple::HashRouter::Entry
An entry in the routing table.
Definition:
HashRouter.h:62
ripple::HashRouter::shouldProcess
bool shouldProcess(uint256 const &key, PeerShortID peer, int &flags, std::chrono::seconds tx_interval)
Definition:
HashRouter.cpp:78
std::chrono::seconds
std::lock_guard
STL class.
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition:
base_uint.h:82
ripple::HashRouter::suppressionMap_
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition:
HashRouter.h:221
ripple::HashRouter::addSuppression
void addSuppression(uint256 const &key)
Definition:
HashRouter.cpp:43
std::uint32_t
ripple::HashRouter::shouldRelay
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Definition:
HashRouter.cpp:118
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
RCLCensorshipDetector.h:29
std::optional
std::make_pair
T make_pair(T... args)
ripple::HashRouter::setFlags
bool setFlags(uint256 const &key, int flags)
Set the flags on a hash.
Definition:
HashRouter.cpp:102
std::ref
T ref(T... args)
Generated by
1.8.17