xrpld
Loading...
Searching...
No Matches
Tx.h
1#pragma once
2
3#include <xrpl/beast/hash/hash_append.h>
4#include <xrpl/beast/hash/uhash.h>
5
6#include <boost/container/flat_set.hpp>
7#include <boost/iterator/function_output_iterator.hpp>
8
9#include <algorithm>
10#include <cstdint>
11#include <map>
12#include <ostream>
13#include <sstream>
14#include <string>
15#include <type_traits>
16#include <utility>
17
18namespace xrpl::test::csf {
19
23class Tx
24{
25public:
27
28 Tx(ID i) : id_{i}
29 {
30 }
31
32 template <typename T>
33 Tx(T const* t)
34 requires(std::is_same_v<T, Tx>)
35 : id_{t->id_}
36 {
37 }
38
39 [[nodiscard]] ID const&
40 id() const
41 {
42 return id_;
43 }
44
45 bool
46 operator<(Tx const& o) const
47 {
48 return id_ < o.id_;
49 }
50
51 bool
52 operator==(Tx const& o) const
53 {
54 return id_ == o.id_;
55 }
56
57private:
59};
60
65using TxSetType = boost::container::flat_set<Tx>;
66
70class TxSet
71{
72public:
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;
108 {
109 }
110
111 TxSet(MutableTxSet&& m) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
112 : txs_{m.txs_}, id_{calcID(txs_)}
113 {
114 }
115
116 [[nodiscard]] bool
117 exists(Tx::ID const txId) const
118 {
119 auto it = txs_.find(Tx{txId});
120 return it != txs_.end();
121 }
122
123 [[nodiscard]] Tx const*
124 find(Tx::ID const& txId) const
125 {
126 auto it = txs_.find(Tx{txId});
127 if (it != txs_.end())
128 return &(*it);
129 return nullptr;
130 }
131
132 [[nodiscard]] TxSetType const&
133 txs() const
134 {
135 return txs_;
136 }
137
138 [[nodiscard]] ID
139 id() const
140 {
141 return id_;
142 }
143
149 [[nodiscard]] std::map<Tx::ID, bool>
150 compare(TxSet const& other) const
151 {
153
154 auto populateDiffs = [&res](auto const& a, auto const& b, bool s) {
155 auto populator = [&](auto const& tx) { res[tx.id()] = s; };
157 a.begin(),
158 a.end(),
159 b.begin(),
160 b.end(),
161 boost::make_function_output_iterator(std::ref(populator)));
162 };
163
164 populateDiffs(txs_, other.txs_, true);
165 populateDiffs(other.txs_, txs_, false);
166 return res;
167 }
168
169private:
174
179};
180
181//------------------------------------------------------------------------------
182// Helper functions for debug printing
183
185operator<<(std::ostream& o, Tx const& t)
186{
187 return o << t.id();
188}
189
190template <class T>
192operator<<(std::ostream& o, boost::container::flat_set<T> const& ts)
193{
194 o << "{ ";
195 bool doComma = false;
196 for (auto const& t : ts)
197 {
198 if (doComma)
199 {
200 o << ", ";
201 }
202 else
203 {
204 doComma = true;
205 }
206 o << t;
207 }
208 o << " }";
209 return o;
210}
211
212inline std::string
214{
216 ss << txs;
217 return ss.str();
218}
219
220template <class Hasher>
221inline void
222hash_append(Hasher& h, Tx const& tx)
223{
224 using beast::hash_append;
225 hash_append(h, tx.id());
226}
227
228} // namespace xrpl::test::csf
MutableTxSet(TxSet const &s)
Definition Tx.h:89
bool erase(Tx::ID const &txId)
Definition Tx.h:100
bool insert(Tx const &t)
Definition Tx.h:94
TxSet(MutableTxSet &&m)
Definition Tx.h:111
TxSetType txs_
The set contains the actual transactions.
Definition Tx.h:173
beast::Uhash<>::result_type ID
Definition Tx.h:73
static ID calcID(TxSetType const &txs)
Definition Tx.h:77
ID id_
The unique ID of this tx set.
Definition Tx.h:178
std::map< Tx::ID, bool > compare(TxSet const &other) const
Definition Tx.h:150
Tx const * find(Tx::ID const &txId) const
Definition Tx.h:124
TxSet(TxSetType s)
Definition Tx.h:107
ID id() const
Definition Tx.h:139
TxSetType const & txs() const
Definition Tx.h:133
bool exists(Tx::ID const txId) const
Definition Tx.h:117
A single transaction.
Definition Tx.h:24
bool operator<(Tx const &o) const
Definition Tx.h:46
ID const & id() const
Definition Tx.h:40
bool operator==(Tx const &o) const
Definition Tx.h:52
std::uint32_t ID
Definition Tx.h:26
Tx(T const *t)
Definition Tx.h:33
T is_same_v
void hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
STL namespace.
std::string to_string(TxSetType const &txs)
Definition Tx.h:213
boost::container::flat_set< Tx > TxSetType
Definition Tx.h:65
std::ostream & operator<<(std::ostream &o, Tx const &t)
Definition Tx.h:185
void hash_append(Hasher &h, Tx const &tx)
Definition Tx.h:222
T ref(T... args)
T set_difference(T... args)
T str(T... args)
Hasher::result_type result_type
Definition uhash.h:14