rippled
Tx.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2017 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 #ifndef RIPPLE_TEST_CSF_TX_H_INCLUDED
20 #define RIPPLE_TEST_CSF_TX_H_INCLUDED
21 #include <ripple/beast/hash/hash_append.h>
22 #include <ripple/beast/hash/uhash.h>
23 #include <boost/container/flat_set.hpp>
24 #include <boost/iterator/function_output_iterator.hpp>
25 #include <map>
26 #include <ostream>
27 #include <string>
28 #include <type_traits>
29 
30 namespace ripple {
31 namespace test {
32 namespace csf {
33 
35 class Tx
36 {
37 public:
38  using ID = std::uint32_t;
39 
40  Tx(ID i) : id_{i}
41  {
42  }
43 
44  template <typename T, typename = std::enable_if_t<std::is_same_v<T, Tx>>>
45  Tx(T const* t) : id_{t->id_}
46  {
47  }
48 
49  ID
50  id() const
51  {
52  return id_;
53  }
54 
55  bool
56  operator<(Tx const& o) const
57  {
58  return id_ < o.id_;
59  }
60 
61  bool
62  operator==(Tx const& o) const
63  {
64  return id_ == o.id_;
65  }
66 
67 private:
69 };
70 
73 using TxSetType = boost::container::flat_set<Tx>;
74 
76 class TxSet
77 {
78 public:
80  using Tx = csf::Tx;
81 
82  static ID
84  {
85  return beast::uhash<>{}(txs);
86  }
87 
89  {
90  friend class TxSet;
91 
93 
94  public:
95  MutableTxSet(TxSet const& s) : txs_{s.txs_}
96  {
97  }
98 
99  bool
100  insert(Tx const& t)
101  {
102  return txs_.insert(t).second;
103  }
104 
105  bool
106  erase(Tx::ID const& txId)
107  {
108  return txs_.erase(Tx{txId}) > 0;
109  }
110  };
111 
112  TxSet() = default;
113  TxSet(TxSetType const& s) : txs_{s}, id_{calcID(txs_)}
114  {
115  }
116 
117  TxSet(MutableTxSet&& m) : txs_{std::move(m.txs_)}, id_{calcID(txs_)}
118  {
119  }
120 
121  bool
122  exists(Tx::ID const txId) const
123  {
124  auto it = txs_.find(Tx{txId});
125  return it != txs_.end();
126  }
127 
128  Tx const*
129  find(Tx::ID const& txId) const
130  {
131  auto it = txs_.find(Tx{txId});
132  if (it != txs_.end())
133  return &(*it);
134  return nullptr;
135  }
136 
137  TxSetType const&
138  txs() const
139  {
140  return txs_;
141  }
142 
143  ID
144  id() const
145  {
146  return id_;
147  }
148 
154  compare(TxSet const& other) const
155  {
157 
158  auto populate_diffs = [&res](auto const& a, auto const& b, bool s) {
159  auto populator = [&](auto const& tx) { res[tx.id()] = s; };
161  a.begin(),
162  a.end(),
163  b.begin(),
164  b.end(),
165  boost::make_function_output_iterator(std::ref(populator)));
166  };
167 
168  populate_diffs(txs_, other.txs_, true);
169  populate_diffs(other.txs_, txs_, false);
170  return res;
171  }
172 
173 private:
176 
179 };
180 
181 //------------------------------------------------------------------------------
182 // Helper functions for debug printing
183 
184 inline std::ostream&
186 {
187  return o << t.id();
188 }
189 
190 template <class T>
191 inline std::ostream&
192 operator<<(std::ostream& o, boost::container::flat_set<T> const& ts)
193 {
194  o << "{ ";
195  bool do_comma = false;
196  for (auto const& t : ts)
197  {
198  if (do_comma)
199  o << ", ";
200  else
201  do_comma = true;
202  o << t;
203  }
204  o << " }";
205  return o;
206 }
207 
208 inline std::string
209 to_string(TxSetType const& txs)
210 {
212  ss << txs;
213  return ss.str();
214 }
215 
216 template <class Hasher>
217 inline void
218 hash_append(Hasher& h, Tx const& tx)
219 {
220  using beast::hash_append;
221  hash_append(h, tx.id());
222 }
223 
224 } // namespace csf
225 } // namespace test
226 } // namespace ripple
227 
228 #endif
ripple::test::csf::TxSet::TxSet
TxSet(MutableTxSet &&m)
Definition: Tx.h:117
ripple::test::csf::Tx::operator==
bool operator==(Tx const &o) const
Definition: Tx.h:62
ripple::test::csf::TxSet::MutableTxSet::erase
bool erase(Tx::ID const &txId)
Definition: Tx.h:106
std::string
STL class.
ripple::test::csf::TxSet::calcID
static ID calcID(TxSetType const &txs)
Definition: Tx.h:83
ripple::test::csf::TxSetType
boost::container::flat_set< Tx > TxSetType
Definition: Tx.h:73
ripple::test::csf::Tx::id_
ID id_
Definition: Tx.h:68
ripple::test::csf::TxSet
TxSet is a set of transactions to consider including in the ledger.
Definition: Tx.h:76
ripple::test::csf::Tx::operator<
bool operator<(Tx const &o) const
Definition: Tx.h:56
std::stringstream
STL class.
ripple::test::csf::TxSet::id
ID id() const
Definition: Tx.h:144
ripple::test::csf::operator<<
std::ostream & operator<<(std::ostream &o, const Tx &t)
Definition: Tx.h:185
ripple::test::csf::TxSet::compare
std::map< Tx::ID, bool > compare(TxSet const &other) const
Definition: Tx.h:154
ripple::test::csf::Tx::id
ID id() const
Definition: Tx.h:50
ripple::test::csf::TxSet::txs
TxSetType const & txs() const
Definition: Tx.h:138
beast::uhash::result_type
typename Hasher::result_type result_type
Definition: uhash.h:35
ripple::test::csf::TxSet::MutableTxSet::txs_
TxSetType txs_
Definition: Tx.h:92
ripple::test::csf::TxSet::ID
beast::uhash<>::result_type ID
Definition: Tx.h:79
ripple::test::csf::TxSet::exists
bool exists(Tx::ID const txId) const
Definition: Tx.h:122
ripple::test::csf::TxSet::MutableTxSet::insert
bool insert(Tx const &t)
Definition: Tx.h:100
ripple::test::csf::Tx::Tx
Tx(ID i)
Definition: Tx.h:40
ripple::test::csf::TxSet::MutableTxSet::MutableTxSet
MutableTxSet(TxSet const &s)
Definition: Tx.h:95
std::ostream
STL class.
ripple::test::csf::Tx
A single transaction.
Definition: Tx.h:35
ripple::test::csf::TxSet::txs_
TxSetType txs_
The set contains the actual transactions.
Definition: Tx.h:175
std::uint32_t
map
ripple::test::csf::TxSet::TxSet
TxSet()=default
ripple::test::csf::TxSet::id_
ID id_
The unique ID of this tx set.
Definition: Tx.h:178
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::csf::Tx::ID
std::uint32_t ID
Definition: Tx.h:38
std::set_difference
T set_difference(T... args)
beast::hash_append
std::enable_if_t< is_contiguously_hashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
Definition: hash_append.h:236
ripple::test::csf::hash_append
void hash_append(Hasher &h, Tx const &tx)
Definition: Tx.h:218
ripple::test::csf::TxSet::find
Tx const * find(Tx::ID const &txId) const
Definition: Tx.h:129
std::stringstream::str
T str(T... args)
ripple::test::csf::TxSet::TxSet
TxSet(TxSetType const &s)
Definition: Tx.h:113
beast::uhash<>
ostream
ripple::test::csf::to_string
std::string to_string(TxSetType const &txs)
Definition: Tx.h:209
type_traits
std::ref
T ref(T... args)
ripple::test::csf::TxSet::MutableTxSet
Definition: Tx.h:88
ripple::test::csf::Tx::Tx
Tx(T const *t)
Definition: Tx.h:45
string