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
104 {
105 }
106
107 bool
108 exists(Tx::ID const txId) const
109 {
110 auto it = txs_.find(Tx{txId});
111 return it != txs_.end();
112 }
113
114 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 TxSetType const&
124 txs() const
125 {
126 return txs_;
127 }
128
129 ID
130 id() const
131 {
132 return id_;
133 }
134
140 compare(TxSet const& other) const
141 {
143
144 auto populate_diffs = [&res](auto const& a, auto const& b, bool s) {
145 auto populator = [&](auto const& tx) { res[tx.id()] = s; };
147 a.begin(), a.end(), b.begin(), b.end(), boost::make_function_output_iterator(std::ref(populator)));
148 };
149
150 populate_diffs(txs_, other.txs_, true);
151 populate_diffs(other.txs_, txs_, false);
152 return res;
153 }
154
155private:
158
161};
162
163//------------------------------------------------------------------------------
164// Helper functions for debug printing
165
167operator<<(std::ostream& o, Tx const& t)
168{
169 return o << t.id();
170}
171
172template <class T>
174operator<<(std::ostream& o, boost::container::flat_set<T> const& ts)
175{
176 o << "{ ";
177 bool do_comma = false;
178 for (auto const& t : ts)
179 {
180 if (do_comma)
181 o << ", ";
182 else
183 do_comma = true;
184 o << t;
185 }
186 o << " }";
187 return o;
188}
189
190inline std::string
192{
194 ss << txs;
195 return ss.str();
196}
197
198template <class Hasher>
199inline void
200hash_append(Hasher& h, Tx const& tx)
201{
202 using beast::hash_append;
203 hash_append(h, tx.id());
204}
205
206} // namespace csf
207} // namespace test
208} // 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:157
static ID calcID(TxSetType const &txs)
Definition Tx.h:69
ID id_
The unique ID of this tx set.
Definition Tx.h:160
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
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
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.
STL namespace.
std::string to_string(TxSetType const &txs)
Definition Tx.h:191
std::ostream & operator<<(std::ostream &o, Tx const &t)
Definition Tx.h:167
boost::container::flat_set< Tx > TxSetType
Definition Tx.h:59
void hash_append(Hasher &h, Tx const &tx)
Definition Tx.h:200
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