rippled
Loading...
Searching...
No Matches
Tx.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2017 Ripple Labs Inc
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19#ifndef RIPPLE_TEST_CSF_TX_H_INCLUDED
20#define RIPPLE_TEST_CSF_TX_H_INCLUDED
21#include <xrpl/beast/hash/hash_append.h>
22#include <xrpl/beast/hash/uhash.h>
23#include <boost/container/flat_set.hpp>
24#include <boost/iterator/function_output_iterator.hpp>
25#include <algorithm>
26#include <map>
27#include <ostream>
28#include <sstream>
29#include <string>
30#include <type_traits>
31
32namespace ripple {
33namespace test {
34namespace csf {
35
37class Tx
38{
39public:
41
42 Tx(ID i) : id_{i}
43 {
44 }
45
46 template <typename T, typename = std::enable_if_t<std::is_same_v<T, Tx>>>
47 Tx(T const* t) : id_{t->id_}
48 {
49 }
50
51 ID
52 id() const
53 {
54 return id_;
55 }
56
57 bool
58 operator<(Tx const& o) const
59 {
60 return id_ < o.id_;
61 }
62
63 bool
64 operator==(Tx const& o) const
65 {
66 return id_ == o.id_;
67 }
68
69private:
71};
72
75using TxSetType = boost::container::flat_set<Tx>;
76
78class TxSet
79{
80public:
82 using Tx = csf::Tx;
83
84 static ID
86 {
87 return beast::uhash<>{}(txs);
88 }
89
91 {
92 friend class TxSet;
93
95
96 public:
97 MutableTxSet(TxSet const& s) : txs_{s.txs_}
98 {
99 }
100
101 bool
102 insert(Tx const& t)
103 {
104 return txs_.insert(t).second;
105 }
106
107 bool
108 erase(Tx::ID const& txId)
109 {
110 return txs_.erase(Tx{txId}) > 0;
111 }
112 };
113
114 TxSet() = default;
115 TxSet(TxSetType const& s) : txs_{s}, id_{calcID(txs_)}
116 {
117 }
118
120 {
121 }
122
123 bool
124 exists(Tx::ID const txId) const
125 {
126 auto it = txs_.find(Tx{txId});
127 return it != txs_.end();
128 }
129
130 Tx const*
131 find(Tx::ID const& txId) const
132 {
133 auto it = txs_.find(Tx{txId});
134 if (it != txs_.end())
135 return &(*it);
136 return nullptr;
137 }
138
139 TxSetType const&
140 txs() const
141 {
142 return txs_;
143 }
144
145 ID
146 id() const
147 {
148 return id_;
149 }
150
156 compare(TxSet const& other) const
157 {
159
160 auto populate_diffs = [&res](auto const& a, auto const& b, bool s) {
161 auto populator = [&](auto const& tx) { res[tx.id()] = s; };
163 a.begin(),
164 a.end(),
165 b.begin(),
166 b.end(),
167 boost::make_function_output_iterator(std::ref(populator)));
168 };
169
170 populate_diffs(txs_, other.txs_, true);
171 populate_diffs(other.txs_, txs_, false);
172 return res;
173 }
174
175private:
178
181};
182
183//------------------------------------------------------------------------------
184// Helper functions for debug printing
185
187operator<<(std::ostream& o, const Tx& t)
188{
189 return o << t.id();
190}
191
192template <class T>
194operator<<(std::ostream& o, boost::container::flat_set<T> const& ts)
195{
196 o << "{ ";
197 bool do_comma = false;
198 for (auto const& t : ts)
199 {
200 if (do_comma)
201 o << ", ";
202 else
203 do_comma = true;
204 o << t;
205 }
206 o << " }";
207 return o;
208}
209
210inline std::string
212{
214 ss << txs;
215 return ss.str();
216}
217
218template <class Hasher>
219inline void
220hash_append(Hasher& h, Tx const& tx)
221{
222 using beast::hash_append;
223 hash_append(h, tx.id());
224}
225
226} // namespace csf
227} // namespace test
228} // namespace ripple
229
230#endif
bool erase(Tx::ID const &txId)
Definition: Tx.h:108
bool insert(Tx const &t)
Definition: Tx.h:102
MutableTxSet(TxSet const &s)
Definition: Tx.h:97
TxSet is a set of transactions to consider including in the ledger.
Definition: Tx.h:79
std::map< Tx::ID, bool > compare(TxSet const &other) const
Definition: Tx.h:156
TxSet(TxSetType const &s)
Definition: Tx.h:115
TxSetType const & txs() const
Definition: Tx.h:140
ID id() const
Definition: Tx.h:146
ID id_
The unique ID of this tx set.
Definition: Tx.h:180
beast::uhash<>::result_type ID
Definition: Tx.h:81
static ID calcID(TxSetType const &txs)
Definition: Tx.h:85
TxSet(MutableTxSet &&m)
Definition: Tx.h:119
Tx const * find(Tx::ID const &txId) const
Definition: Tx.h:131
bool exists(Tx::ID const txId) const
Definition: Tx.h:124
TxSetType txs_
The set contains the actual transactions.
Definition: Tx.h:177
A single transaction.
Definition: Tx.h:38
bool operator<(Tx const &o) const
Definition: Tx.h:58
std::uint32_t ID
Definition: Tx.h:40
bool operator==(Tx const &o) const
Definition: Tx.h:64
ID id() const
Definition: Tx.h:52
Tx(T const *t)
Definition: Tx.h:47
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.
Definition: hash_append.h:236
boost::container::flat_set< Tx > TxSetType
Definition: Tx.h:75
std::string to_string(TxSetType const &txs)
Definition: Tx.h:211
void hash_append(Hasher &h, Tx const &tx)
Definition: Tx.h:220
std::ostream & operator<<(std::ostream &o, const Tx &t)
Definition: Tx.h:187
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
STL namespace.
T ref(T... args)
T set_difference(T... args)
T str(T... args)
typename Hasher::result_type result_type
Definition: uhash.h:35