rippled
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
16namespace xrpl {
17namespace test {
18namespace csf {
19
21class Tx
22{
23public:
25
26 Tx(ID i) : id_{i}
27 {
28 }
29
30 template <typename T, typename = std::enable_if_t<std::is_same_v<T, Tx>>>
31 Tx(T const* t) : id_{t->id_}
32 {
33 }
34
35 ID const&
36 id() const
37 {
38 return id_;
39 }
40
41 bool
42 operator<(Tx const& o) const
43 {
44 return id_ < o.id_;
45 }
46
47 bool
48 operator==(Tx const& o) const
49 {
50 return id_ == o.id_;
51 }
52
53private:
55};
56
59using TxSetType = boost::container::flat_set<Tx>;
60
62class TxSet
63{
64public:
66 using Tx = csf::Tx;
67
68 static ID
70 {
71 return beast::uhash<>{}(txs);
72 }
73
75 {
76 friend class TxSet;
77
79
80 public:
81 MutableTxSet(TxSet const& s) : txs_{s.txs_}
82 {
83 }
84
85 bool
86 insert(Tx const& t)
87 {
88 return txs_.insert(t).second;
89 }
90
91 bool
92 erase(Tx::ID const& txId)
93 {
94 return txs_.erase(Tx{txId}) > 0;
95 }
96 };
97
98 TxSet() = default;
99 TxSet(TxSetType const& s) : txs_{s}, id_{calcID(txs_)}
100 {
101 }
102
103 TxSet(MutableTxSet&& m) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
104 : txs_{m.txs_}, id_{calcID(txs_)}
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 xrpl
MutableTxSet(TxSet const &s)
Definition Tx.h:81
bool erase(Tx::ID const &txId)
Definition Tx.h:92
bool insert(Tx const &t)
Definition Tx.h:86
TxSet is a set of transactions to consider including in the ledger.
Definition Tx.h:63
TxSet(TxSetType const &s)
Definition Tx.h:99
TxSet(MutableTxSet &&m)
Definition Tx.h:103
TxSetType txs_
The set contains the actual transactions.
Definition Tx.h:162
static ID calcID(TxSetType const &txs)
Definition Tx.h:69
ID id_
The unique ID of this tx set.
Definition Tx.h:165
std::map< Tx::ID, bool > compare(TxSet const &other) const
Definition Tx.h:141
Tx const * find(Tx::ID const &txId) const
Definition Tx.h:116
ID id() const
Definition Tx.h:131
TxSetType const & txs() const
Definition Tx.h:125
bool exists(Tx::ID const txId) const
Definition Tx.h:109
beast::uhash<>::result_type ID
Definition Tx.h:65
A single transaction.
Definition Tx.h:22
bool operator<(Tx const &o) const
Definition Tx.h:42
ID const & id() const
Definition Tx.h:36
bool operator==(Tx const &o) const
Definition Tx.h:48
Tx(T const *t)
Definition Tx.h:31
std::uint32_t ID
Definition Tx.h:24
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::string to_string(TxSetType const &txs)
Definition Tx.h:196
std::ostream & operator<<(std::ostream &o, Tx const &t)
Definition Tx.h:172
boost::container::flat_set< Tx > TxSetType
Definition Tx.h:59
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:5
T ref(T... args)
T set_difference(T... args)
T str(T... args)
typename Hasher::result_type result_type
Definition uhash.h:14