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
20#ifndef RIPPLE_TEST_CSF_LEDGERS_H_INCLUDED
21#define RIPPLE_TEST_CSF_LEDGERS_H_INCLUDED
22
23#include <test/csf/Tx.h>
24
25#include <xrpld/consensus/LedgerTiming.h>
26
27#include <xrpl/basics/UnorderedContainers.h>
28#include <xrpl/basics/chrono.h>
29#include <xrpl/basics/comparators.h>
30#include <xrpl/basics/tagged_integer.h>
31#include <xrpl/json/json_value.h>
32
33#include <boost/bimap/bimap.hpp>
34
35#include <optional>
36#include <set>
37
38namespace ripple {
39namespace test {
40namespace csf {
41
63class Ledger
64{
65 friend class LedgerOracle;
66
67public:
68 struct SeqTag;
70
71 struct IdTag;
73
75 {
76 };
77
78private:
79 // The instance is the common immutable data that will be assigned a unique
80 // ID by the oracle
81 struct Instance
82 {
84 {
85 }
86
87 // Sequence number
88 Seq seq{0};
89
90 // Transactions added to generate this ledger
92
93 // Resolution used to determine close time
95
98
100 bool closeTimeAgree = true;
101
104
107
112
113 auto
114 asTie() const
115 {
116 return std::tie(
117 seq,
118 txs,
120 closeTime,
122 parentID,
124 }
125
126 friend bool
127 operator==(Instance const& a, Instance const& b)
128 {
129 return a.asTie() == b.asTie();
130 }
131
132 friend bool
133 operator!=(Instance const& a, Instance const& b)
134 {
135 return a.asTie() != b.asTie();
136 }
137
138 friend bool
139 operator<(Instance const& a, Instance const& b)
140 {
141 return a.asTie() < b.asTie();
142 }
143
144 template <class Hasher>
145 friend void
146 hash_append(Hasher& h, Ledger::Instance const& instance)
147 {
148 using beast::hash_append;
149 hash_append(h, instance.asTie());
150 }
151 };
152
153 // Single common genesis instance
154 static Instance const genesis;
155
156 Ledger(ID id, Instance const* i) : id_{id}, instance_{i}
157 {
158 }
159
160public:
164
165 // This is required by the generic Consensus for now and should be
166 // migrated to the MakeGenesis approach above.
168 {
169 }
170
171 ID
172 id() const
173 {
174 return id_;
175 }
176
177 Seq
178 seq() const
179 {
180 return instance_->seq;
181 }
182
185 {
187 }
188
189 bool
191 {
193 }
194
196 closeTime() const
197 {
198 return instance_->closeTime;
199 }
200
203 {
205 }
206
207 ID
208 parentID() const
209 {
210 return instance_->parentID;
211 }
212
213 TxSetType const&
214 txs() const
215 {
216 return instance_->txs;
217 }
218
220 bool
221 isAncestor(Ledger const& ancestor) const;
222
225 ID
226 operator[](Seq seq) const;
227
230 friend Ledger::Seq
231 mismatch(Ledger const& a, Ledger const& o);
232
234 getJson() const;
235
236 friend bool
237 operator<(Ledger const& a, Ledger const& b)
238 {
239 return a.id() < b.id();
240 }
241
242private:
243 ID id_{0};
245};
246
250{
251 using InstanceMap = boost::bimaps::bimap<
252 boost::bimaps::set_of<Ledger::Instance, ripple::less<Ledger::Instance>>,
253 boost::bimaps::set_of<Ledger::ID, ripple::less<Ledger::ID>>>;
254 using InstanceEntry = InstanceMap::value_type;
255
256 // Set of all known ledgers; note this is never pruned
258
259 // ID for the next unique ledger
261 nextID() const;
262
263public:
264 LedgerOracle();
265
268 lookup(Ledger::ID const& id) const;
269
278 Ledger
279 accept(
280 Ledger const& curr,
281 TxSetType const& txs,
282 NetClock::duration closeTimeResolution,
283 NetClock::time_point const& consensusCloseTime);
284
285 Ledger
286 accept(Ledger const& curr, Tx tx)
287 {
288 using namespace std::chrono_literals;
289 return accept(
290 curr,
291 TxSetType{tx},
292 curr.closeTimeResolution(),
293 curr.closeTime() + 1s);
294 }
295
306 branches(std::set<Ledger> const& ledgers) const;
307};
308
329{
334
339
345 Ledger const&
347 {
348 auto it = ledgers.find(s);
349 if (it != ledgers.end())
350 return it->second;
351
352 // enforce that the new suffix has never been seen
353 assert(seen.emplace(s.back()).second);
354
355 Ledger const& parent = (*this)[s.substr(0, s.size() - 1)];
356 return ledgers.emplace(s, oracle.accept(parent, Tx{++nextTx}))
357 .first->second;
358 }
359};
360
361} // namespace csf
362} // namespace test
363} // namespace ripple
364
365#endif
T back(T... args)
Represents a JSON value.
Definition json_value.h:149
Oracle maintaining unique ledgers for a simulation.
Definition ledgers.h:250
Ledger accept(Ledger const &curr, Tx tx)
Definition ledgers.h:286
InstanceMap::value_type InstanceEntry
Definition ledgers.h:254
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:253
A ledger is a set of observed transactions and a sequence number identifying the ledger.
Definition ledgers.h:64
static Instance const genesis
Definition ledgers.h:154
friend bool operator<(Ledger const &a, Ledger const &b)
Definition ledgers.h:237
NetClock::duration closeTimeResolution() const
Definition ledgers.h:184
NetClock::time_point parentCloseTime() const
Definition ledgers.h:202
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:196
Instance const * instance_
Definition ledgers.h:244
Json::Value getJson() const
Definition ledgers.cpp:31
bool closeAgree() const
Definition ledgers.h:190
TxSetType const & txs() const
Definition ledgers.h:214
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:156
tagged_integer< std::uint32_t, SeqTag > Seq
Definition ledgers.h:69
tagged_integer< std::uint32_t, IdTag > ID
Definition ledgers.h:72
A single transaction.
Definition Tx.h:42
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.
boost::container::flat_set< Tx > TxSetType
Definition Tx.h:79
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
auto constexpr ledgerDefaultTimeResolution
Initial resolution of ledger close time.
T size(T... args)
Helper for writing unit tests with controlled ledger histories.
Definition ledgers.h:329
Ledger const & operator[](std::string const &s)
Get or create the ledger with the given string history.
Definition ledgers.h:346
std::unordered_map< std::string, Ledger > ledgers
Definition ledgers.h:332
NetClock::time_point parentCloseTime
Parent ledger close time.
Definition ledgers.h:106
friend bool operator==(Instance const &a, Instance const &b)
Definition ledgers.h:127
NetClock::duration closeTimeResolution
Definition ledgers.h:94
ID parentID
Parent ledger id.
Definition ledgers.h:103
bool closeTimeAgree
Whether consensus agreed on the close time.
Definition ledgers.h:100
friend bool operator<(Instance const &a, Instance const &b)
Definition ledgers.h:139
friend bool operator!=(Instance const &a, Instance const &b)
Definition ledgers.h:133
friend void hash_append(Hasher &h, Ledger::Instance const &instance)
Definition ledgers.h:146
std::vector< Ledger::ID > ancestors
IDs of this ledgers ancestors.
Definition ledgers.h:111
NetClock::time_point closeTime
When the ledger closed (up to closeTimeResolution)
Definition ledgers.h:97
Set the sequence number on a JTx.
Definition seq.h:34
T substr(T... args)
T tie(T... args)