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
20#ifndef RIPPLE_TEST_CSF_TX_H_INCLUDED
21#define RIPPLE_TEST_CSF_TX_H_INCLUDED
22
23#include <xrpl/beast/hash/hash_append.h>
24#include <xrpl/beast/hash/uhash.h>
25
26#include <boost/container/flat_set.hpp>
27#include <boost/iterator/function_output_iterator.hpp>
28
29#include <algorithm>
30#include <map>
31#include <ostream>
32#include <sstream>
33#include <string>
34#include <type_traits>
35
36namespace ripple {
37namespace test {
38namespace csf {
39
41class Tx
42{
43public:
45
46 Tx(ID i) : id_{i}
47 {
48 }
49
50 template <typename T, typename = std::enable_if_t<std::is_same_v<T, Tx>>>
51 Tx(T const* t) : id_{t->id_}
52 {
53 }
54
55 ID const&
56 id() const
57 {
58 return id_;
59 }
60
61 bool
62 operator<(Tx const& o) const
63 {
64 return id_ < o.id_;
65 }
66
67 bool
68 operator==(Tx const& o) const
69 {
70 return id_ == o.id_;
71 }
72
73private:
75};
76
79using TxSetType = boost::container::flat_set<Tx>;
80
82class TxSet
83{
84public:
86 using Tx = csf::Tx;
87
88 static ID
90 {
91 return beast::uhash<>{}(txs);
92 }
93
95 {
96 friend class TxSet;
97
99
100 public:
102 {
103 }
104
105 bool
106 insert(Tx const& t)
107 {
108 return txs_.insert(t).second;
109 }
110
111 bool
112 erase(Tx::ID const& txId)
113 {
114 return txs_.erase(Tx{txId}) > 0;
115 }
116 };
117
118 TxSet() = default;
119 TxSet(TxSetType const& s) : txs_{s}, id_{calcID(txs_)}
120 {
121 }
122
124 {
125 }
126
127 bool
128 exists(Tx::ID const txId) const
129 {
130 auto it = txs_.find(Tx{txId});
131 return it != txs_.end();
132 }
133
134 Tx const*
135 find(Tx::ID const& txId) const
136 {
137 auto it = txs_.find(Tx{txId});
138 if (it != txs_.end())
139 return &(*it);
140 return nullptr;
141 }
142
143 TxSetType const&
144 txs() const
145 {
146 return txs_;
147 }
148
149 ID
150 id() const
151 {
152 return id_;
153 }
154
160 compare(TxSet const& other) const
161 {
163
164 auto populate_diffs = [&res](auto const& a, auto const& b, bool s) {
165 auto populator = [&](auto const& tx) { res[tx.id()] = s; };
167 a.begin(),
168 a.end(),
169 b.begin(),
170 b.end(),
171 boost::make_function_output_iterator(std::ref(populator)));
172 };
173
174 populate_diffs(txs_, other.txs_, true);
175 populate_diffs(other.txs_, txs_, false);
176 return res;
177 }
178
179private:
182
185};
186
187//------------------------------------------------------------------------------
188// Helper functions for debug printing
189
191operator<<(std::ostream& o, Tx const& t)
192{
193 return o << t.id();
194}
195
196template <class T>
198operator<<(std::ostream& o, boost::container::flat_set<T> const& ts)
199{
200 o << "{ ";
201 bool do_comma = false;
202 for (auto const& t : ts)
203 {
204 if (do_comma)
205 o << ", ";
206 else
207 do_comma = true;
208 o << t;
209 }
210 o << " }";
211 return o;
212}
213
214inline std::string
216{
218 ss << txs;
219 return ss.str();
220}
221
222template <class Hasher>
223inline void
224hash_append(Hasher& h, Tx const& tx)
225{
226 using beast::hash_append;
227 hash_append(h, tx.id());
228}
229
230} // namespace csf
231} // namespace test
232} // namespace ripple
233
234#endif
bool erase(Tx::ID const &txId)
Definition Tx.h:112
TxSet is a set of transactions to consider including in the ledger.
Definition Tx.h:83
std::map< Tx::ID, bool > compare(TxSet const &other) const
Definition Tx.h:160
TxSet(TxSetType const &s)
Definition Tx.h:119
TxSetType const & txs() const
Definition Tx.h:144
ID id() const
Definition Tx.h:150
ID id_
The unique ID of this tx set.
Definition Tx.h:184
beast::uhash<>::result_type ID
Definition Tx.h:85
static ID calcID(TxSetType const &txs)
Definition Tx.h:89
TxSet(MutableTxSet &&m)
Definition Tx.h:123
Tx const * find(Tx::ID const &txId) const
Definition Tx.h:135
bool exists(Tx::ID const txId) const
Definition Tx.h:128
TxSetType txs_
The set contains the actual transactions.
Definition Tx.h:181
A single transaction.
Definition Tx.h:42
bool operator<(Tx const &o) const
Definition Tx.h:62
std::uint32_t ID
Definition Tx.h:44
ID const & id() const
Definition Tx.h:56
bool operator==(Tx const &o) const
Definition Tx.h:68
Tx(T const *t)
Definition Tx.h:51
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::ostream & operator<<(std::ostream &o, Tx const &t)
Definition Tx.h:191
boost::container::flat_set< Tx > TxSetType
Definition Tx.h:79
std::string to_string(TxSetType const &txs)
Definition Tx.h:215
void hash_append(Hasher &h, Tx const &tx)
Definition Tx.h:224
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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