xrpld
Loading...
Searching...
No Matches
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
10namespace xrpl {
11
12template <class TxID, class Sequence>
14{
15public:
16 struct TxIDSeq
17 {
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
47
48private:
50
51public:
52 CensorshipDetector() = default;
53
60 void
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());
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
104 tracker_.begin(),
105 tracker_.end(),
106 accepted.begin(),
107 accepted.end(),
108 [&pred](auto const& x) { return pred(x.txid, x.seq); },
110 tracker_.erase(i, tracker_.end());
111 }
112
119 void
121 {
122 tracker_.clear();
123 }
124};
125
126} // namespace xrpl
T begin(T... args)
void reset()
Removes all elements from the tracker.
void check(std::vector< TxID > accepted, Predicate &&pred)
Determine which transactions made it and perform censorship detection.
std::vector< TxIDSeq > TxIDSeqVec
void propose(TxIDSeqVec proposed)
Add transactions being proposed for the current consensus round.
friend bool operator<(TxIDSeq const &lhs, TxIDSeq const &rhs)
T end(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void generalizedSetIntersection(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Action action, Comp comp)
Definition algorithm.h:16
FwdIter1 removeIfIntersectOrMatch(FwdIter1 first1, FwdIter1 last1, InputIter2 first2, InputIter2 last2, Pred pred, Comp comp)
Definition algorithm.h:56
uint256 TxID
A transaction identifier.
Definition Protocol.h:391
T sort(T... args)
TxIDSeq(TxID const &txid, Sequence const &seq)