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
std::lock_guard
lock(
mutex_
);
54
55
auto
result =
emplace
(key);
56
result.first.addPeer(peer);
57
return
result.second;
58
}
59
60
bool
61
HashRouter::addSuppressionPeer
(
uint256
const
& key,
PeerShortID
peer,
int
& flags)
62
{
63
std::lock_guard
lock(
mutex_
);
64
65
auto
[s, created] =
emplace
(key);
66
s.addPeer(peer);
67
flags = s.getFlags();
68
return
created;
69
}
70
71
bool
72
HashRouter::shouldProcess
(
73
uint256
const
& key,
74
PeerShortID
peer,
75
int
& flags,
76
std::chrono::seconds
tx_interval)
77
{
78
std::lock_guard
lock(
mutex_
);
79
80
auto
result =
emplace
(key);
81
auto
& s = result.first;
82
s.addPeer(peer);
83
flags = s.getFlags();
84
return
s.shouldProcess(
suppressionMap_
.clock().now(), tx_interval);
85
}
86
87
int
88
HashRouter::getFlags
(
uint256
const
& key)
89
{
90
std::lock_guard
lock(
mutex_
);
91
92
return
emplace
(key).first.getFlags();
93
}
94
95
bool
96
HashRouter::setFlags
(
uint256
const
& key,
int
flags)
97
{
98
assert(flags != 0);
99
100
std::lock_guard
lock(
mutex_
);
101
102
auto
& s =
emplace
(key).first;
103
104
if
((s.getFlags() & flags) == flags)
105
return
false
;
106
107
s.setFlags(flags);
108
return
true
;
109
}
110
111
auto
112
HashRouter::shouldRelay
(
uint256
const
& key)
113
-> boost::optional<std::set<PeerShortID>>
114
{
115
std::lock_guard
lock(mutex_);
116
117
auto
& s = emplace(key).first;
118
119
if
(!s.shouldRelay(suppressionMap_.clock().now(), holdTime_))
120
return
boost::none;
121
122
return
s.releasePeerSet();
123
}
124
125
bool
126
HashRouter::shouldRecover
(
uint256
const
& key)
127
{
128
std::lock_guard
lock(
mutex_
);
129
130
auto
& s =
emplace
(key).first;
131
132
return
s.shouldRecover(
recoverLimit_
);
133
}
134
135
}
// namespace ripple
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:88
ripple::HashRouter::mutex_
std::mutex mutex_
Definition:
HashRouter.h:238
std::pair
ripple::HashRouter::Entry
An entry in the routing table.
Definition:
HashRouter.h:61
ripple::HashRouter::shouldProcess
bool shouldProcess(uint256 const &key, PeerShortID peer, int &flags, std::chrono::seconds tx_interval)
Definition:
HashRouter.cpp:72
std::chrono::seconds
std::lock_guard
STL class.
ripple::base_uint
Definition:
base_uint.h:63
ripple::HashRouter::suppressionMap_
beast::aged_unordered_map< uint256, Entry, Stopwatch::clock_type, hardened_hash< strong_hash > > suppressionMap_
Definition:
HashRouter.h:246
ripple::HashRouter::addSuppression
void addSuppression(uint256 const &key)
Definition:
HashRouter.cpp:43
ripple::HashRouter::shouldRecover
bool shouldRecover(uint256 const &key)
Determines whether the hashed item should be recovered from the open ledger into the next open ledger...
Definition:
HashRouter.cpp:126
std::uint32_t
ripple::HashRouter::shouldRelay
boost::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Definition:
HashRouter.cpp:112
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
RCLCensorshipDetector.h:29
std::make_pair
T make_pair(T... args)
ripple::HashRouter::recoverLimit_
const std::uint32_t recoverLimit_
Definition:
HashRouter.h:250
ripple::HashRouter::setFlags
bool setFlags(uint256 const &key, int flags)
Set the flags on a hash.
Definition:
HashRouter.cpp:96
std::ref
T ref(T... args)
Generated by
1.8.17