xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
consensus
CensorshipDetector.h
1
#pragma once
2
3
#include <xrpl/basics/algorithm.h>
4
5
#include <
algorithm
>
6
#include <
functional
>
7
#include <
utility
>
8
#include <
vector
>
9
10
namespace
xrpl
{
11
12
template
<
class
TxID,
class
Sequence>
13
class
CensorshipDetector
14
{
15
public
:
16
struct
TxIDSeq
17
{
18
TxID
txid
;
19
Sequence
seq
;
20
21
TxIDSeq
(
TxID
const
&
txid
, Sequence
const
&
seq
) :
txid
(
txid
),
seq
(
seq
)
22
{
23
}
24
};
25
26
friend
bool
27
operator<
(
TxIDSeq
const
& lhs,
TxIDSeq
const
& rhs)
28
{
29
if
(lhs.
txid
!= rhs.
txid
)
30
return
lhs.
txid
< rhs.
txid
;
31
return
lhs.
seq
< rhs.
seq
;
32
}
33
34
friend
bool
35
operator<
(
TxIDSeq
const
& lhs,
TxID
const
& rhs)
36
{
37
return
lhs.
txid
< rhs;
38
}
39
40
friend
bool
41
operator<
(
TxID
const
& lhs,
TxIDSeq
const
& rhs)
42
{
43
return
lhs < rhs.
txid
;
44
}
45
46
using
TxIDSeqVec
=
std::vector<TxIDSeq>
;
47
48
private
:
49
TxIDSeqVec
tracker_
;
50
51
public
:
52
CensorshipDetector
() =
default
;
53
60
void
61
propose
(
TxIDSeqVec
proposed)
62
{
63
// We want to remove any entries that we proposed in a previous round
64
// that did not make it in yet if we are no longer proposing them.
65
// And we also want to preserve the Sequence of entries that we proposed
66
// in the last round and want to propose again.
67
std::sort
(proposed.
begin
(), proposed.
end
());
68
generalizedSetIntersection
(
69
proposed.
begin
(),
70
proposed.
end
(),
71
tracker_
.cbegin(),
72
tracker_
.cend(),
73
[](
auto
& x,
auto
const
& y) { x.seq = y.seq; },
74
[](
auto
const
& x,
auto
const
& y) { return x.txid < y.txid; });
75
tracker_
= std::move(proposed);
76
}
77
92
template
<
class
Predicate>
93
void
94
check
(
std::vector<TxID>
accepted, Predicate&& pred)
95
{
96
auto
acceptTxid = accepted.
begin
();
97
auto
const
ae = accepted.
end
();
98
std::sort
(acceptTxid, ae);
99
100
// We want to remove all tracking entries for transactions that were
101
// accepted as well as those which match the predicate.
102
103
auto
i =
removeIfIntersectOrMatch
(
104
tracker_
.begin(),
105
tracker_
.end(),
106
accepted.
begin
(),
107
accepted.
end
(),
108
[&pred](
auto
const
& x) { return pred(x.txid, x.seq); },
109
std::less<void>
{});
110
tracker_
.erase(i,
tracker_
.end());
111
}
112
119
void
120
reset
()
121
{
122
tracker_
.clear();
123
}
124
};
125
126
}
// namespace xrpl
algorithm
std::vector::begin
T begin(T... args)
xrpl::CensorshipDetector::reset
void reset()
Removes all elements from the tracker.
Definition
CensorshipDetector.h:120
xrpl::CensorshipDetector::check
void check(std::vector< TxID > accepted, Predicate &&pred)
Determine which transactions made it and perform censorship detection.
Definition
CensorshipDetector.h:94
xrpl::CensorshipDetector::TxIDSeqVec
std::vector< TxIDSeq > TxIDSeqVec
Definition
CensorshipDetector.h:46
xrpl::CensorshipDetector::propose
void propose(TxIDSeqVec proposed)
Add transactions being proposed for the current consensus round.
Definition
CensorshipDetector.h:61
xrpl::CensorshipDetector::CensorshipDetector
CensorshipDetector()=default
xrpl::CensorshipDetector::operator<
friend bool operator<(TxIDSeq const &lhs, TxIDSeq const &rhs)
Definition
CensorshipDetector.h:27
xrpl::CensorshipDetector::tracker_
TxIDSeqVec tracker_
Definition
CensorshipDetector.h:49
std::vector::end
T end(T... args)
functional
std::less
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::TxID
UInt256 TxID
A transaction identifier.
Definition
Protocol.h:403
xrpl::generalizedSetIntersection
void generalizedSetIntersection(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Action action, Comp comp)
Definition
algorithm.h:16
xrpl::removeIfIntersectOrMatch
FwdIter1 removeIfIntersectOrMatch(FwdIter1 first1, FwdIter1 last1, InputIter2 first2, InputIter2 last2, Pred pred, Comp comp)
Definition
algorithm.h:56
std::sort
T sort(T... args)
xrpl::CensorshipDetector::TxIDSeq
Definition
CensorshipDetector.h:17
xrpl::CensorshipDetector::TxIDSeq::txid
TxID txid
Definition
CensorshipDetector.h:18
xrpl::CensorshipDetector::TxIDSeq::TxIDSeq
TxIDSeq(TxID const &txid, Sequence const &seq)
Definition
CensorshipDetector.h:21
xrpl::CensorshipDetector::TxIDSeq::seq
Sequence seq
Definition
CensorshipDetector.h:19
utility
vector
Generated by
1.17.0