rippled
RCLCensorshipDetector_test.cpp
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 #include <ripple/beast/unit_test.h>
21 #include <ripple/app/consensus/RCLCensorshipDetector.h>
22 #include <algorithm>
23 #include <vector>
24 
25 namespace ripple {
26 namespace test {
27 
28 class RCLCensorshipDetector_test : public beast::unit_test::suite
29 {
30  void test(
31  RCLCensorshipDetector<int, int>& cdet, int round,
32  std::vector<int> proposed, std::vector<int> accepted,
33  std::vector<int> remain, std::vector<int> remove)
34  {
35  // Begin tracking what we're proposing this round
37  for (auto const& i : proposed)
38  proposal.emplace_back(i, round);
39  cdet.propose(std::move(proposal));
40 
41  // Finalize the round, by processing what we accepted; then
42  // remove anything that needs to be removed and ensure that
43  // what remains is correct.
44  cdet.check(std::move(accepted),
45  [&remove, &remain](auto id, auto seq)
46  {
47  // If the item is supposed to be removed from the censorship
48  // detector internal tracker manually, do it now:
49  if (std::find(remove.begin(), remove.end(), id) != remove.end())
50  return true;
51 
52  // If the item is supposed to still remain in the censorship
53  // detector internal tracker; remove it from the vector.
54  auto it = std::find(remain.begin(), remain.end(), id);
55  if (it != remain.end())
56  remain.erase(it);
57  return false;
58  });
59 
60  // On entry, this set contained all the elements that should be tracked
61  // by the detector after we process this round. We removed all the items
62  // that actually were in the tracker, so this should now be empty:
63  BEAST_EXPECT(remain.empty());
64  }
65 
66 public:
67  void
68  run() override
69  {
70  testcase ("Censorship Detector");
71 
73  int round = 0;
74  // proposed accepted remain remove
75  test(cdet, ++round, { }, { }, { }, { });
76  test(cdet, ++round, { 10, 11, 12, 13 }, { 11, 2 }, { 10, 13 }, { });
77  test(cdet, ++round, { 10, 13, 14, 15 }, { 14 }, { 10, 13, 15 }, { });
78  test(cdet, ++round, { 10, 13, 15, 16 }, { 15, 16 }, { 10, 13 }, { });
79  test(cdet, ++round, { 10, 13 }, { 17, 18 }, { 10, 13 }, { });
80  test(cdet, ++round, { 10, 19 }, { }, { 10, 19 }, { });
81  test(cdet, ++round, { 10, 19, 20 }, { 20 }, { 10 }, { 19 });
82  test(cdet, ++round, { 21 }, { 21 }, { }, { });
83  test(cdet, ++round, { }, { 22 }, { }, { });
84  test(cdet, ++round, { 23, 24, 25, 26 }, { 25, 27 }, { 23, 26 }, { 24 });
85  test(cdet, ++round, { 23, 26, 28 }, { 26, 28 }, { 23 }, { });
86 
87  for (int i = 0; i != 10; ++i)
88  test(cdet, ++round, { 23 }, { }, { 23 }, { });
89 
90  test(cdet, ++round, { 23, 29 }, { 29 }, { 23 }, { });
91  test(cdet, ++round, { 30, 31 }, { 31 }, { 30 }, { });
92  test(cdet, ++round, { 30 }, { 30 }, { }, { });
93  test(cdet, ++round, { }, { }, { }, { });
94  }
95 };
96 
98 } // namespace test
99 } // namespace ripple
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountDelete, app, ripple)
vector
std::find
T find(T... args)
ripple::test::RCLCensorshipDetector_test::run
void run() override
Definition: RCLCensorshipDetector_test.cpp:68
algorithm
ripple::test::RCLCensorshipDetector_test
Definition: RCLCensorshipDetector_test.cpp:28
std::vector::erase
T erase(T... args)
ripple::HashPrefix::proposal
@ proposal
proposal for signing
ripple::ManifestDisposition::accepted
@ accepted
Manifest is valid.
ripple::test::jtx::seq
Set the sequence number on a JTx.
Definition: seq.h:32
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
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:76
std::vector::begin
T begin(T... args)
ripple::test::RCLCensorshipDetector_test::test
void test(RCLCensorshipDetector< int, int > &cdet, int round, std::vector< int > proposed, std::vector< int > accepted, std::vector< int > remain, std::vector< int > remove)
Definition: RCLCensorshipDetector_test.cpp:30
std::vector::empty
T empty(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:104
std::vector::end
T end(T... args)