rippled
Loading...
Searching...
No Matches
Tx.h
1#ifndef XRPL_TEST_CSF_TX_H_INCLUDED
2#define XRPL_TEST_CSF_TX_H_INCLUDED
3
4#include <xrpl/beast/hash/hash_append.h>
5#include <xrpl/beast/hash/uhash.h>
6
7#include <boost/container/flat_set.hpp>
8#include <boost/iterator/function_output_iterator.hpp>
9
10#include <algorithm>
11#include <map>
12#include <ostream>
13#include <sstream>
14#include <string>
15#include <type_traits>
16
17namespace ripple {
18namespace test {
19namespace csf {
20
22class Tx
23{
24public:
26
27 Tx(ID i) : id_{i}
28 {
29 }
30
31 template <typename T, typename = std::enable_if_t<std::is_same_v<T, Tx>>>
32 Tx(T const* t) : id_{t->id_}
33 {
34 }
35
36 ID const&
37 id() const
38 {
39 return id_;
40 }
41
42 bool
43 operator<(Tx const& o) const
44 {
45 return id_ < o.id_;
46 }
47
48 bool
49 operator==(Tx const& o) const
50 {
51 return id_ == o.id_;
52 }
53
54private:
56};
57
60using TxSetType = boost::container::flat_set<Tx>;
61
63class TxSet
64{
65public:
67 using Tx = csf::Tx;
68
69 static ID
71 {
72 return beast::uhash<>{}(txs);
73 }
74
76 {
77 friend class TxSet;
78
80
81 public:
82 MutableTxSet(TxSet const& s) : txs_{s.txs_}
83 {
84 }
85
86 bool
87 insert(Tx const& t)
88 {
89 return txs_.insert(t).second;
90 }
91
92 bool
93 erase(Tx::ID const& txId)
94 {
95 return txs_.erase(Tx{txId}) > 0;
96 }
97 };
98
99 TxSet() = default;
100 TxSet(TxSetType const& s) : txs_{s}, id_{calcID(txs_)}
101 {
102 }
103
105 {
106 }
107
108 bool
109 exists(Tx::ID const txId) const
110 {
111 auto it = txs_.find(Tx{txId});
112 return it != txs_.end();
113 }
114
115 Tx const*
116 find(Tx::ID const& txId) const
117 {
118 auto it = txs_.find(Tx{txId});
119 if (it != txs_.end())
120 return &(*it);
121 return nullptr;
122 }
123
124 TxSetType const&
125 txs() const
126 {
127 return txs_;
128 }
129
130 ID
131 id() const
132 {
133 return id_;
134 }
135
141 compare(TxSet const& other) const
142 {
144
145 auto populate_diffs = [&res](auto const& a, auto const& b, bool s) {
146 auto populator = [&](auto const& tx) { res[tx.id()] = s; };
148 a.begin(),
149 a.end(),
150 b.begin(),
151 b.end(),
152 boost::make_function_output_iterator(std::ref(populator)));
153 };
154
155 populate_diffs(txs_, other.txs_, true);
156 populate_diffs(other.txs_, txs_, false);
157 return res;
158 }
159
160private:
163
166};
167
168//------------------------------------------------------------------------------
169// Helper functions for debug printing
170
172operator<<(std::ostream& o, Tx const& t)
173{
174 return o << t.id();
175}
176
177template <class T>
179operator<<(std::ostream& o, boost::container::flat_set<T> const& ts)
180{
181 o << "{ ";
182 bool do_comma = false;
183 for (auto const& t : ts)
184 {
185 if (do_comma)
186 o << ", ";
187 else
188 do_comma = true;
189 o << t;
190 }
191 o << " }";
192 return o;
193}
194
195inline std::string
197{
199 ss << txs;
200 return ss.str();
201}
202
203template <class Hasher>
204inline void
205hash_append(Hasher& h, Tx const& tx)
206{
207 using beast::hash_append;
208 hash_append(h, tx.id());
209}
210
211} // namespace csf
212} // namespace test
213} // namespace ripple
214
215#endif
bool erase(Tx::ID const &txId)
Definition Tx.h:93
TxSet is a set of transactions to consider including in the ledger.
Definition Tx.h:64
std::map< Tx::ID, bool > compare(TxSet const &other) const
Definition Tx.h:141
TxSet(TxSetType const &s)
Definition Tx.h:100
TxSetType const & txs() const
Definition Tx.h:125
ID id() const
Definition Tx.h:131
ID id_
The unique ID of this tx set.
Definition Tx.h:165
beast::uhash<>::result_type ID
Definition Tx.h:66
static ID calcID(TxSetType const &txs)
Definition Tx.h:70
TxSet(MutableTxSet &&m)
Definition Tx.h:104
Tx const * find(Tx::ID const &txId) const
Definition Tx.h:116
bool exists(Tx::ID const txId) const
Definition Tx.h:109
TxSetType txs_
The set contains the actual transactions.
Definition Tx.h:162
A single transaction.
Definition Tx.h:23
bool operator<(Tx const &o) const
Definition Tx.h:43
std::uint32_t ID
Definition Tx.h:25
ID const & id() const
Definition Tx.h:37
bool operator==(Tx const &o) const
Definition Tx.h:49
Tx(T const *t)
Definition Tx.h:32
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.
std::ostream & operator<<(std::ostream &o, Tx const &t)
Definition Tx.h:172
boost::container::flat_set< Tx > TxSetType
Definition Tx.h:60
std::string to_string(TxSetType const &txs)
Definition Tx.h:196
void hash_append(Hasher &h, Tx const &tx)
Definition Tx.h:205
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
STL namespace.
T ref(T... args)
T set_difference(T... args)
T str(T... args)
typename Hasher::result_type result_type
Definition uhash.h:15