rippled
RCLCensorshipDetector.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2018 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 #ifndef RIPPLE_APP_CONSENSUS_RCLCENSORSHIPDETECTOR_H_INCLUDED
21 #define RIPPLE_APP_CONSENSUS_RCLCENSORSHIPDETECTOR_H_INCLUDED
22 
23 #include <ripple/basics/algorithm.h>
24 #include <ripple/shamap/SHAMap.h>
25 #include <algorithm>
26 #include <utility>
27 #include <vector>
28 
29 namespace ripple {
30 
31 template <class TxID, class Sequence>
33 {
34 public:
35  struct TxIDSeq
36  {
38  Sequence seq;
39 
40  TxIDSeq(TxID const& txid_, Sequence const& seq_)
41  : txid(txid_), seq(seq_)
42  {
43  }
44  };
45 
46  friend bool
47  operator<(TxIDSeq const& lhs, TxIDSeq const& rhs)
48  {
49  if (lhs.txid != rhs.txid)
50  return lhs.txid < rhs.txid;
51  return lhs.seq < rhs.seq;
52  }
53 
54  friend bool
55  operator<(TxIDSeq const& lhs, TxID const& rhs)
56  {
57  return lhs.txid < rhs;
58  }
59 
60  friend bool
61  operator<(TxID const& lhs, TxIDSeq const& rhs)
62  {
63  return lhs < rhs.txid;
64  }
65 
67 
68 private:
70 
71 public:
72  RCLCensorshipDetector() = default;
73 
79  void
80  propose(TxIDSeqVec proposed)
81  {
82  // We want to remove any entries that we proposed in a previous round
83  // that did not make it in yet if we are no longer proposing them.
84  // And we also want to preserve the Sequence of entries that we proposed
85  // in the last round and want to propose again.
86  std::sort(proposed.begin(), proposed.end());
88  proposed.begin(),
89  proposed.end(),
90  tracker_.cbegin(),
91  tracker_.cend(),
92  [](auto& x, auto const& y) { x.seq = y.seq; },
93  [](auto const& x, auto const& y) { return x.txid < y.txid; });
94  tracker_ = std::move(proposed);
95  }
96 
110  template <class Predicate>
111  void
112  check(std::vector<TxID> accepted, Predicate&& pred)
113  {
114  auto acceptTxid = accepted.begin();
115  auto const ae = accepted.end();
116  std::sort(acceptTxid, ae);
117 
118  // We want to remove all tracking entries for transactions that were
119  // accepted as well as those which match the predicate.
120 
122  tracker_.begin(),
123  tracker_.end(),
124  accepted.begin(),
125  accepted.end(),
126  [&pred](auto const& x) { return pred(x.txid, x.seq); },
127  std::less<void>{});
128  tracker_.erase(i, tracker_.end());
129  }
130 
136  void
138  {
139  tracker_.clear();
140  }
141 };
142 
143 } // namespace ripple
144 
145 #endif
utility
vector
ripple::RCLCensorshipDetector::TxIDSeq::txid
TxID txid
Definition: RCLCensorshipDetector.h:37
std::less
std::sort
T sort(T... args)
algorithm
std::vector::clear
T clear(T... args)
ripple::base_uint< 256 >
ripple::RCLCensorshipDetector::RCLCensorshipDetector
RCLCensorshipDetector()=default
ripple::RCLCensorshipDetector::reset
void reset()
Removes all elements from the tracker.
Definition: RCLCensorshipDetector.h:137
std::vector::erase
T erase(T... args)
ripple::generalized_set_intersection
void generalized_set_intersection(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Action action, Comp comp)
Definition: algorithm.h:37
ripple::remove_if_intersect_or_match
FwdIter1 remove_if_intersect_or_match(FwdIter1 first1, FwdIter1 last1, InputIter2 first2, InputIter2 last2, Pred pred, Comp comp)
Definition: algorithm.h:75
ripple::ManifestDisposition::accepted
@ accepted
Manifest is valid.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RCLCensorshipDetector::TxIDSeq
Definition: RCLCensorshipDetector.h:35
ripple::RCLCensorshipDetector
Definition: RCLCensorshipDetector.h:32
ripple::RCLCensorshipDetector::propose
void propose(TxIDSeqVec proposed)
Add transactions being proposed for the current consensus round.
Definition: RCLCensorshipDetector.h:80
ripple::RCLCensorshipDetector::TxIDSeq::TxIDSeq
TxIDSeq(TxID const &txid_, Sequence const &seq_)
Definition: RCLCensorshipDetector.h:40
std::vector::begin
T begin(T... args)
ripple::RCLCensorshipDetector::check
void check(std::vector< TxID > accepted, Predicate &&pred)
Determine which transactions made it and perform censorship detection.
Definition: RCLCensorshipDetector.h:112
ripple::RCLCensorshipDetector::operator<
friend bool operator<(TxIDSeq const &lhs, TxIDSeq const &rhs)
Definition: RCLCensorshipDetector.h:47
std::vector::end
T end(T... args)
ripple::RCLCensorshipDetector::tracker_
TxIDSeqVec tracker_
Definition: RCLCensorshipDetector.h:69
ripple::RCLCensorshipDetector::TxIDSeq::seq
Sequence seq
Definition: RCLCensorshipDetector.h:38