rippled
Loading...
Searching...
No Matches
ledgers.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_LEDGERS_H_INCLUDED
20#define RIPPLE_TEST_CSF_LEDGERS_H_INCLUDED
21
22#include <test/csf/Tx.h>
23#include <xrpld/consensus/LedgerTiming.h>
24#include <xrpl/basics/UnorderedContainers.h>
25#include <xrpl/basics/chrono.h>
26#include <xrpl/basics/comparators.h>
27#include <xrpl/basics/tagged_integer.h>
28#include <xrpl/json/json_value.h>
29#include <boost/bimap/bimap.hpp>
30#include <optional>
31#include <set>
32
33namespace ripple {
34namespace test {
35namespace csf {
36
58class Ledger
59{
60 friend class LedgerOracle;
61
62public:
63 struct SeqTag;
65
66 struct IdTag;
68
70 {
71 };
72
73private:
74 // The instance is the common immutable data that will be assigned a unique
75 // ID by the oracle
76 struct Instance
77 {
79 {
80 }
81
82 // Sequence number
83 Seq seq{0};
84
85 // Transactions added to generate this ledger
87
88 // Resolution used to determine close time
90
93
95 bool closeTimeAgree = true;
96
99
102
107
108 auto
109 asTie() const
110 {
111 return std::tie(
112 seq,
113 txs,
115 closeTime,
117 parentID,
119 }
120
121 friend bool
122 operator==(Instance const& a, Instance const& b)
123 {
124 return a.asTie() == b.asTie();
125 }
126
127 friend bool
128 operator!=(Instance const& a, Instance const& b)
129 {
130 return a.asTie() != b.asTie();
131 }
132
133 friend bool
134 operator<(Instance const& a, Instance const& b)
135 {
136 return a.asTie() < b.asTie();
137 }
138
139 template <class Hasher>
140 friend void
141 hash_append(Hasher& h, Ledger::Instance const& instance)
142 {
143 using beast::hash_append;
144 hash_append(h, instance.asTie());
145 }
146 };
147
148 // Single common genesis instance
149 static const Instance genesis;
150
151 Ledger(ID id, Instance const* i) : id_{id}, instance_{i}
152 {
153 }
154
155public:
157 {
158 }
159
160 // This is required by the generic Consensus for now and should be
161 // migrated to the MakeGenesis approach above.
163 {
164 }
165
166 ID
167 id() const
168 {
169 return id_;
170 }
171
172 Seq
173 seq() const
174 {
175 return instance_->seq;
176 }
177
180 {
182 }
183
184 bool
186 {
188 }
189
191 closeTime() const
192 {
193 return instance_->closeTime;
194 }
195
198 {
200 }
201
202 ID
203 parentID() const
204 {
205 return instance_->parentID;
206 }
207
208 TxSetType const&
209 txs() const
210 {
211 return instance_->txs;
212 }
213
215 bool
216 isAncestor(Ledger const& ancestor) const;
217
220 ID
221 operator[](Seq seq) const;
222
225 friend Ledger::Seq
226 mismatch(Ledger const& a, Ledger const& o);
227
229 getJson() const;
230
231 friend bool
232 operator<(Ledger const& a, Ledger const& b)
233 {
234 return a.id() < b.id();
235 }
236
237private:
238 ID id_{0};
240};
241
245{
246 using InstanceMap = boost::bimaps::bimap<
247 boost::bimaps::set_of<Ledger::Instance, ripple::less<Ledger::Instance>>,
248 boost::bimaps::set_of<Ledger::ID, ripple::less<Ledger::ID>>>;
249 using InstanceEntry = InstanceMap::value_type;
250
251 // Set of all known ledgers; note this is never pruned
253
254 // ID for the next unique ledger
256 nextID() const;
257
258public:
259 LedgerOracle();
260
263 lookup(Ledger::ID const& id) const;
264
273 Ledger
274 accept(
275 Ledger const& curr,
276 TxSetType const& txs,
277 NetClock::duration closeTimeResolution,
278 NetClock::time_point const& consensusCloseTime);
279
280 Ledger
281 accept(Ledger const& curr, Tx tx)
282 {
283 using namespace std::chrono_literals;
284 return accept(
285 curr,
286 TxSetType{tx},
287 curr.closeTimeResolution(),
288 curr.closeTime() + 1s);
289 }
290
301 branches(std::set<Ledger> const& ledgers) const;
302};
303
324{
329
331 {
333 }
334
340 Ledger const&
342 {
343 auto it = ledgers.find(s);
344 if (it != ledgers.end())
345 return it->second;
346
347 // enforce that the new suffix has never been seen
348 assert(seen.emplace(s.back()).second);
349
350 Ledger const& parent = (*this)[s.substr(0, s.size() - 1)];
351 return ledgers.emplace(s, oracle.accept(parent, Tx{++nextTx}))
352 .first->second;
353 }
354};
355
356} // namespace csf
357} // namespace test
358} // namespace ripple
359
360#endif
T back(T... args)
Represents a JSON value.
Definition: json_value.h:147
Oracle maintaining unique ledgers for a simulation.
Definition: ledgers.h:245
Ledger accept(Ledger const &curr, Tx tx)
Definition: ledgers.h:281
InstanceMap::value_type InstanceEntry
Definition: ledgers.h:249
Ledger::ID nextID() const
Definition: ledgers.cpp:91
Ledger accept(Ledger const &curr, TxSetType const &txs, NetClock::duration closeTimeResolution, NetClock::time_point const &consensusCloseTime)
Accept the given txs and generate a new ledger.
Definition: ledgers.cpp:97
std::optional< Ledger > lookup(Ledger::ID const &id) const
Find the ledger with the given ID.
Definition: ledgers.cpp:129
std::size_t branches(std::set< Ledger > const &ledgers) const
Determine the number of distinct branches for the set of ledgers.
Definition: ledgers.cpp:140
boost::bimaps::bimap< boost::bimaps::set_of< Ledger::Instance, ripple::less< Ledger::Instance > >, boost::bimaps::set_of< Ledger::ID, ripple::less< Ledger::ID > > > InstanceMap
Definition: ledgers.h:248
A ledger is a set of observed transactions and a sequence number identifying the ledger.
Definition: ledgers.h:59
static const Instance genesis
Definition: ledgers.h:149
friend bool operator<(Ledger const &a, Ledger const &b)
Definition: ledgers.h:232
NetClock::duration closeTimeResolution() const
Definition: ledgers.h:179
NetClock::time_point parentCloseTime() const
Definition: ledgers.h:197
friend Ledger::Seq mismatch(Ledger const &a, Ledger const &o)
Return the sequence number of the first mismatching ancestor.
Definition: ledgers.cpp:58
NetClock::time_point closeTime() const
Definition: ledgers.h:191
Instance const * instance_
Definition: ledgers.h:239
Json::Value getJson() const
Definition: ledgers.cpp:31
bool closeAgree() const
Definition: ledgers.h:185
Ledger(MakeGenesis)
Definition: ledgers.h:156
TxSetType const & txs() const
Definition: ledgers.h:209
bool isAncestor(Ledger const &ancestor) const
Determine whether ancestor is really an ancestor of this ledger.
Definition: ledgers.cpp:40
ID operator[](Seq seq) const
Return the id of the ancestor with the given seq (if exists/known)
Definition: ledgers.cpp:48
Ledger(ID id, Instance const *i)
Definition: ledgers.h:151
tagged_integer< std::uint32_t, SeqTag > Seq
Definition: ledgers.h:64
tagged_integer< std::uint32_t, IdTag > ID
Definition: ledgers.h:67
A single transaction.
Definition: Tx.h:38
T emplace(T... args)
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
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
auto constexpr ledgerDefaultTimeResolution
Initial resolution of ledger close time.
Definition: LedgerTiming.h:44
T size(T... args)
Helper for writing unit tests with controlled ledger histories.
Definition: ledgers.h:324
Ledger const & operator[](std::string const &s)
Get or create the ledger with the given string history.
Definition: ledgers.h:341
std::unordered_map< std::string, Ledger > ledgers
Definition: ledgers.h:327
NetClock::time_point parentCloseTime
Parent ledger close time.
Definition: ledgers.h:101
friend bool operator==(Instance const &a, Instance const &b)
Definition: ledgers.h:122
NetClock::duration closeTimeResolution
Definition: ledgers.h:89
ID parentID
Parent ledger id.
Definition: ledgers.h:98
bool closeTimeAgree
Whether consensus agreed on the close time.
Definition: ledgers.h:95
friend bool operator<(Instance const &a, Instance const &b)
Definition: ledgers.h:134
friend bool operator!=(Instance const &a, Instance const &b)
Definition: ledgers.h:128
friend void hash_append(Hasher &h, Ledger::Instance const &instance)
Definition: ledgers.h:141
std::vector< Ledger::ID > ancestors
IDs of this ledgers ancestors.
Definition: ledgers.h:106
NetClock::time_point closeTime
When the ledger closed (up to closeTimeResolution)
Definition: ledgers.h:92
Set the sequence number on a JTx.
Definition: seq.h:34
T substr(T... args)
T tie(T... args)