xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
csf
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
18
namespace
xrpl::test::csf
{
19
23
class
Tx
24
{
25
public
:
26
using
ID
=
std::uint32_t
;
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
57
private
:
58
ID
id_
;
59
};
60
65
using
TxSetType
= boost::container::flat_set<Tx>;
66
70
class
TxSet
71
{
72
public
:
73
using
ID
=
beast::Uhash<>::result_type
;
74
using
Tx
=
csf::Tx
;
75
76
static
ID
77
calcID
(
TxSetType
const
&
txs
)
78
{
79
return
beast::Uhash<>
{}(
txs
);
80
}
81
82
class
MutableTxSet
83
{
84
friend
class
TxSet
;
85
86
TxSetType
txs_
;
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
;
107
TxSet
(
TxSetType
s) :
txs_
{
std
::move(s)},
id_
{
calcID
(
txs_
)}
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
{
152
std::map<Tx::ID, bool>
res;
153
154
auto
populateDiffs = [&res](
auto
const
& a,
auto
const
& b,
bool
s) {
155
auto
populator = [&](
auto
const
& tx) { res[tx.id()] = s; };
156
std::set_difference
(
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
169
private
:
173
TxSetType
txs_
;
174
178
ID
id_
{};
179
};
180
181
//------------------------------------------------------------------------------
182
// Helper functions for debug printing
183
184
inline
std::ostream
&
185
operator<<
(
std::ostream
& o,
Tx
const
& t)
186
{
187
return
o << t.
id
();
188
}
189
190
template
<
class
T>
191
inline
std::ostream
&
192
operator<<
(
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
212
inline
std::string
213
to_string
(
TxSetType
const
& txs)
214
{
215
std::stringstream
ss;
216
ss << txs;
217
return
ss.
str
();
218
}
219
220
template
<
class
Hasher>
221
inline
void
222
hash_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
algorithm
std::ostream
std::string
std::stringstream
xrpl::test::csf::TxSet::MutableTxSet
Definition
Tx.h:83
xrpl::test::csf::TxSet::MutableTxSet::txs_
TxSetType txs_
Definition
Tx.h:86
xrpl::test::csf::TxSet::MutableTxSet::MutableTxSet
MutableTxSet(TxSet const &s)
Definition
Tx.h:89
xrpl::test::csf::TxSet::MutableTxSet::erase
bool erase(Tx::ID const &txId)
Definition
Tx.h:100
xrpl::test::csf::TxSet::MutableTxSet::TxSet
friend class TxSet
Definition
Tx.h:84
xrpl::test::csf::TxSet::MutableTxSet::insert
bool insert(Tx const &t)
Definition
Tx.h:94
xrpl::test::csf::TxSet::TxSet
TxSet(MutableTxSet &&m)
Definition
Tx.h:111
xrpl::test::csf::TxSet::TxSet
TxSet()=default
xrpl::test::csf::TxSet::txs_
TxSetType txs_
The set contains the actual transactions.
Definition
Tx.h:173
xrpl::test::csf::TxSet::ID
beast::Uhash<>::result_type ID
Definition
Tx.h:73
xrpl::test::csf::TxSet::calcID
static ID calcID(TxSetType const &txs)
Definition
Tx.h:77
xrpl::test::csf::TxSet::id_
ID id_
The unique ID of this tx set.
Definition
Tx.h:178
xrpl::test::csf::TxSet::compare
std::map< Tx::ID, bool > compare(TxSet const &other) const
Definition
Tx.h:150
xrpl::test::csf::TxSet::find
Tx const * find(Tx::ID const &txId) const
Definition
Tx.h:124
xrpl::test::csf::TxSet::TxSet
TxSet(TxSetType s)
Definition
Tx.h:107
xrpl::test::csf::TxSet::id
ID id() const
Definition
Tx.h:139
xrpl::test::csf::TxSet::Tx
csf::Tx Tx
Definition
Tx.h:74
xrpl::test::csf::TxSet::txs
TxSetType const & txs() const
Definition
Tx.h:133
xrpl::test::csf::TxSet::exists
bool exists(Tx::ID const txId) const
Definition
Tx.h:117
xrpl::test::csf::Tx
A single transaction.
Definition
Tx.h:24
xrpl::test::csf::Tx::Tx
Tx(ID i)
Definition
Tx.h:28
xrpl::test::csf::Tx::operator<
bool operator<(Tx const &o) const
Definition
Tx.h:46
xrpl::test::csf::Tx::id
ID const & id() const
Definition
Tx.h:40
xrpl::test::csf::Tx::operator==
bool operator==(Tx const &o) const
Definition
Tx.h:52
xrpl::test::csf::Tx::ID
std::uint32_t ID
Definition
Tx.h:26
xrpl::test::csf::Tx::id_
ID id_
Definition
Tx.h:58
xrpl::test::csf::Tx::Tx
Tx(T const *t)
Definition
Tx.h:33
cstdint
std::uint32_t
std::is_same_v
T is_same_v
map
beast::hash_append
void hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
Definition
hash_append.h:206
std
STL namespace.
xrpl::test::csf
Definition
Validations.cpp:23
xrpl::test::csf::to_string
std::string to_string(TxSetType const &txs)
Definition
Tx.h:213
xrpl::test::csf::TxSetType
boost::container::flat_set< Tx > TxSetType
Definition
Tx.h:65
xrpl::test::csf::operator<<
std::ostream & operator<<(std::ostream &o, Tx const &t)
Definition
Tx.h:185
xrpl::test::csf::hash_append
void hash_append(Hasher &h, Tx const &tx)
Definition
Tx.h:222
ostream
std::ref
T ref(T... args)
std::set_difference
T set_difference(T... args)
sstream
std::stringstream::str
T str(T... args)
string
beast::Uhash
Definition
uhash.h:11
beast::Uhash::result_type
Hasher::result_type result_type
Definition
uhash.h:14
type_traits
utility
Generated by
1.17.0