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 <map>
11#include <ostream>
12#include <sstream>
13#include <string>
14#include <type_traits>
15#include <utility>
16
17namespace xrpl::test::csf {
18
20class Tx
21{
22public:
24
25 Tx(ID i) : id_{i}
26 {
27 }
28
29 template <typename T, typename = std::enable_if_t<std::is_same_v<T, Tx>>>
30 Tx(T const* t) : id_{t->id_}
31 {
32 }
33
34 [[nodiscard]] ID const&
35 id() const
36 {
37 return id_;
38 }
39
40 bool
41 operator<(Tx const& o) const
42 {
43 return id_ < o.id_;
44 }
45
46 bool
47 operator==(Tx const& o) const
48 {
49 return id_ == o.id_;
50 }
51
52private:
54};
55
58using TxSetType = boost::container::flat_set<Tx>;
59
61class TxSet
62{
63public:
65 using Tx = csf::Tx;
66
67 static ID
69 {
70 return beast::Uhash<>{}(txs);
71 }
72
74 {
75 friend class TxSet;
76
78
79 public:
80 MutableTxSet(TxSet const& s) : txs_{s.txs_}
81 {
82 }
83
84 bool
85 insert(Tx const& t)
86 {
87 return txs_.insert(t).second;
88 }
89
90 bool
91 erase(Tx::ID const& txId)
92 {
93 return txs_.erase(Tx{txId}) > 0;
94 }
95 };
96
97 TxSet() = default;
98 TxSet(TxSetType s) : txs_{std::move(s)}, id_{calcID(txs_)}
99 {
100 }
101
102 TxSet(MutableTxSet&& m) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
103 : txs_{m.txs_}, id_{calcID(txs_)}
104 {
105 }
106
107 [[nodiscard]] bool
108 exists(Tx::ID const txId) const
109 {
110 auto it = txs_.find(Tx{txId});
111 return it != txs_.end();
112 }
113
114 [[nodiscard]] Tx const*
115 find(Tx::ID const& txId) const
116 {
117 auto it = txs_.find(Tx{txId});
118 if (it != txs_.end())
119 return &(*it);
120 return nullptr;
121 }
122
123 [[nodiscard]] TxSetType const&
124 txs() const
125 {
126 return txs_;
127 }
128
129 [[nodiscard]] ID
130 id() const
131 {
132 return id_;
133 }
134
139 [[nodiscard]] std::map<Tx::ID, bool>
140 compare(TxSet const& other) const
141 {
143
144 auto populateDiffs = [&res](auto const& a, auto const& b, bool s) {
145 auto populator = [&](auto const& tx) { res[tx.id()] = s; };
147 a.begin(),
148 a.end(),
149 b.begin(),
150 b.end(),
151 boost::make_function_output_iterator(std::ref(populator)));
152 };
153
154 populateDiffs(txs_, other.txs_, true);
155 populateDiffs(other.txs_, txs_, false);
156 return res;
157 }
158
159private:
162
165};
166
167//------------------------------------------------------------------------------
168// Helper functions for debug printing
169
171operator<<(std::ostream& o, Tx const& t)
172{
173 return o << t.id();
174}
175
176template <class T>
178operator<<(std::ostream& o, boost::container::flat_set<T> const& ts)
179{
180 o << "{ ";
181 bool doComma = false;
182 for (auto const& t : ts)
183 {
184 if (doComma)
185 {
186 o << ", ";
187 }
188 else
189 {
190 doComma = true;
191 }
192 o << t;
193 }
194 o << " }";
195 return o;
196}
197
198inline std::string
200{
202 ss << txs;
203 return ss.str();
204}
205
206template <class Hasher>
207inline void
208hash_append(Hasher& h, Tx const& tx)
209{
210 using beast::hash_append;
211 hash_append(h, tx.id());
212}
213
214} // namespace xrpl::test::csf
MutableTxSet(TxSet const &s)
Definition Tx.h:80
bool erase(Tx::ID const &txId)
Definition Tx.h:91
bool insert(Tx const &t)
Definition Tx.h:85
TxSet(MutableTxSet &&m)
Definition Tx.h:102
TxSetType txs_
The set contains the actual transactions.
Definition Tx.h:161
beast::Uhash<>::result_type ID
Definition Tx.h:64
static ID calcID(TxSetType const &txs)
Definition Tx.h:68
ID id_
The unique ID of this tx set.
Definition Tx.h:164
std::map< Tx::ID, bool > compare(TxSet const &other) const
Definition Tx.h:140
Tx const * find(Tx::ID const &txId) const
Definition Tx.h:115
TxSet(TxSetType s)
Definition Tx.h:98
ID id() const
Definition Tx.h:130
TxSetType const & txs() const
Definition Tx.h:124
bool exists(Tx::ID const txId) const
Definition Tx.h:108
A single transaction.
Definition Tx.h:21
bool operator<(Tx const &o) const
Definition Tx.h:41
ID const & id() const
Definition Tx.h:35
bool operator==(Tx const &o) const
Definition Tx.h:47
Tx(T const *t)
Definition Tx.h:30
std::uint32_t ID
Definition Tx.h:23
std::enable_if_t< IsContiguouslyHashable< T, Hasher >::value > 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:199
boost::container::flat_set< Tx > TxSetType
Definition Tx.h:58
std::ostream & operator<<(std::ostream &o, Tx const &t)
Definition Tx.h:171
void hash_append(Hasher &h, Tx const &tx)
Definition Tx.h:208
T ref(T... args)
T set_difference(T... args)
T str(T... args)
Hasher::result_type result_type
Definition uhash.h:14