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/function_output_iterator.hpp>
25 #include <map>
26 #include <ostream>
27 #include <string>
28 
29 namespace ripple {
30 namespace test {
31 namespace csf {
32 
34 class Tx
35 {
36 public:
37  using ID = std::uint32_t;
38 
39  Tx(ID i) : id_{i}
40  {
41  }
42 
43  ID
44  id() const
45  {
46  return id_;
47  }
48 
49  bool
50  operator<(Tx const& o) const
51  {
52  return id_ < o.id_;
53  }
54 
55  bool
56  operator==(Tx const& o) const
57  {
58  return id_ == o.id_;
59  }
60 
61 private:
63 };
64 
67 using TxSetType = boost::container::flat_set<Tx>;
68 
70 class TxSet
71 {
72 public:
74  using Tx = csf::Tx;
75 
76  static ID
78  {
79  return beast::uhash<>{}(txs);
80  }
81 
83  {
84  friend class TxSet;
85 
87 
88  public:
89  MutableTxSet(TxSet const& s) : txs_{s.txs_}
90  {
91  }
92 
93  bool
94  insert(Tx const& t)
95  {
96  return txs_.insert(t).second;
97  }
98 
99  bool
100  erase(Tx::ID const& txId)
101  {
102  return txs_.erase(Tx{txId}) > 0;
103  }
104  };
105 
106  TxSet() = default;
107  TxSet(TxSetType const& s) : txs_{s}, id_{calcID(txs_)}
108  {
109  }
110 
111  TxSet(MutableTxSet&& m) : txs_{std::move(m.txs_)}, id_{calcID(txs_)}
112  {
113  }
114 
115  bool
116  exists(Tx::ID const txId) const
117  {
118  auto it = txs_.find(Tx{txId});
119  return it != txs_.end();
120  }
121 
122  Tx const*
123  find(Tx::ID const& txId) const
124  {
125  auto it = txs_.find(Tx{txId});
126  if (it != txs_.end())
127  return &(*it);
128  return nullptr;
129  }
130 
131  TxSetType const&
132  txs() const
133  {
134  return txs_;
135  }
136 
137  ID
138  id() const
139  {
140  return id_;
141  }
142 
148  compare(TxSet const& other) const
149  {
151 
152  auto populate_diffs = [&res](auto const& a, auto const& b, bool s) {
153  auto populator = [&](auto const& tx) { res[tx.id()] = s; };
155  a.begin(),
156  a.end(),
157  b.begin(),
158  b.end(),
159  boost::make_function_output_iterator(std::ref(populator)));
160  };
161 
162  populate_diffs(txs_, other.txs_, true);
163  populate_diffs(other.txs_, txs_, false);
164  return res;
165  }
166 
167 private:
170 
173 };
174 
175 //------------------------------------------------------------------------------
176 // Helper functions for debug printing
177 
178 inline std::ostream&
180 {
181  return o << t.id();
182 }
183 
184 template <class T>
185 inline std::ostream&
186 operator<<(std::ostream& o, boost::container::flat_set<T> const& ts)
187 {
188  o << "{ ";
189  bool do_comma = false;
190  for (auto const& t : ts)
191  {
192  if (do_comma)
193  o << ", ";
194  else
195  do_comma = true;
196  o << t;
197  }
198  o << " }";
199  return o;
200 }
201 
202 inline std::string
203 to_string(TxSetType const& txs)
204 {
206  ss << txs;
207  return ss.str();
208 }
209 
210 template <class Hasher>
211 inline void
212 hash_append(Hasher& h, Tx const& tx)
213 {
214  using beast::hash_append;
215  hash_append(h, tx.id());
216 }
217 
218 } // namespace csf
219 } // namespace test
220 } // namespace ripple
221 
222 #endif
ripple::test::csf::TxSet::TxSet
TxSet(MutableTxSet &&m)
Definition: Tx.h:111
ripple::test::csf::Tx::operator==
bool operator==(Tx const &o) const
Definition: Tx.h:56
ripple::test::csf::TxSet::MutableTxSet::erase
bool erase(Tx::ID const &txId)
Definition: Tx.h:100
std::string
STL class.
ripple::test::csf::TxSet::calcID
static ID calcID(TxSetType const &txs)
Definition: Tx.h:77
ripple::test::csf::TxSetType
boost::container::flat_set< Tx > TxSetType
Definition: Tx.h:67
ripple::test::csf::Tx::id_
ID id_
Definition: Tx.h:62
ripple::test::csf::TxSet
TxSet is a set of transactions to consider including in the ledger.
Definition: Tx.h:70
ripple::test::csf::Tx::operator<
bool operator<(Tx const &o) const
Definition: Tx.h:50
std::stringstream
STL class.
ripple::test::csf::TxSet::id
ID id() const
Definition: Tx.h:138
ripple::test::csf::operator<<
std::ostream & operator<<(std::ostream &o, const Tx &t)
Definition: Tx.h:179
ripple::test::csf::TxSet::compare
std::map< Tx::ID, bool > compare(TxSet const &other) const
Definition: Tx.h:148
ripple::test::csf::Tx::id
ID id() const
Definition: Tx.h:44
ripple::test::csf::TxSet::txs
TxSetType const & txs() const
Definition: Tx.h:132
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:86
ripple::test::csf::TxSet::ID
beast::uhash<>::result_type ID
Definition: Tx.h:73
ripple::test::csf::TxSet::exists
bool exists(Tx::ID const txId) const
Definition: Tx.h:116
ripple::test::csf::TxSet::MutableTxSet::insert
bool insert(Tx const &t)
Definition: Tx.h:94
ripple::test::csf::Tx::Tx
Tx(ID i)
Definition: Tx.h:39
ripple::test::csf::TxSet::MutableTxSet::MutableTxSet
MutableTxSet(TxSet const &s)
Definition: Tx.h:89
std::ostream
STL class.
ripple::test::csf::Tx
A single transaction.
Definition: Tx.h:34
ripple::test::csf::TxSet::txs_
TxSetType txs_
The set contains the actual transactions.
Definition: Tx.h:169
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:172
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:37
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:212
ripple::test::csf::TxSet::find
Tx const * find(Tx::ID const &txId) const
Definition: Tx.h:123
std::stringstream::str
T str(T... args)
ripple::test::csf::TxSet::TxSet
TxSet(TxSetType const &s)
Definition: Tx.h:107
beast::uhash<>
ostream
ripple::test::csf::to_string
std::string to_string(TxSetType const &txs)
Definition: Tx.h:203
std::ref
T ref(T... args)
ripple::test::csf::TxSet::MutableTxSet
Definition: Tx.h:82
string