rippled
Loading...
Searching...
No Matches
ledgers.h
1#pragma once
2
3#include <test/csf/Tx.h>
4
5#include <xrpld/consensus/LedgerTiming.h>
6
7#include <xrpl/basics/UnorderedContainers.h>
8#include <xrpl/basics/chrono.h>
9#include <xrpl/basics/comparators.h>
10#include <xrpl/basics/tagged_integer.h>
11#include <xrpl/json/json_value.h>
12
13#include <boost/bimap/bimap.hpp>
14
15#include <optional>
16#include <set>
17
18namespace xrpl {
19namespace test {
20namespace csf {
21
43class Ledger
44{
45 friend class LedgerOracle;
46
47public:
48 struct SeqTag;
50
51 struct IdTag;
53
55 {
56 };
57
58private:
59 // The instance is the common immutable data that will be assigned a unique
60 // ID by the oracle
61 struct Instance
62 {
64 {
65 }
66
67 // Sequence number
68 Seq seq{0};
69
70 // Transactions added to generate this ledger
72
73 // Resolution used to determine close time
75
78
80 bool closeTimeAgree = true;
81
84
87
92
93 auto
98
99 friend bool
100 operator==(Instance const& a, Instance const& b)
101 {
102 return a.asTie() == b.asTie();
103 }
104
105 friend bool
106 operator!=(Instance const& a, Instance const& b)
107 {
108 return a.asTie() != b.asTie();
109 }
110
111 friend bool
112 operator<(Instance const& a, Instance const& b)
113 {
114 return a.asTie() < b.asTie();
115 }
116
117 template <class Hasher>
118 friend void
119 hash_append(Hasher& h, Ledger::Instance const& instance)
120 {
121 using beast::hash_append;
122 hash_append(h, instance.asTie());
123 }
124 };
125
126 // Single common genesis instance
127 static Instance const genesis;
128
129 Ledger(ID id, Instance const* i) : id_{id}, instance_{i}
130 {
131 }
132
133public:
137
138 // This is required by the generic Consensus for now and should be
139 // migrated to the MakeGenesis approach above.
141 {
142 }
143
144 ID
145 id() const
146 {
147 return id_;
148 }
149
150 Seq
151 seq() const
152 {
153 return instance_->seq;
154 }
155
158 {
160 }
161
162 bool
164 {
166 }
167
169 closeTime() const
170 {
171 return instance_->closeTime;
172 }
173
176 {
178 }
179
180 ID
181 parentID() const
182 {
183 return instance_->parentID;
184 }
185
186 TxSetType const&
187 txs() const
188 {
189 return instance_->txs;
190 }
191
193 bool
194 isAncestor(Ledger const& ancestor) const;
195
198 ID
199 operator[](Seq seq) const;
200
203 friend Ledger::Seq
204 mismatch(Ledger const& a, Ledger const& o);
205
207 getJson() const;
208
209 friend bool
210 operator<(Ledger const& a, Ledger const& b)
211 {
212 return a.id() < b.id();
213 }
214
215private:
216 ID id_{0};
218};
219
223{
224 using InstanceMap = boost::bimaps::bimap<
225 boost::bimaps::set_of<Ledger::Instance, xrpl::less<Ledger::Instance>>,
226 boost::bimaps::set_of<Ledger::ID, xrpl::less<Ledger::ID>>>;
227 using InstanceEntry = InstanceMap::value_type;
228
229 // Set of all known ledgers; note this is never pruned
231
232 // ID for the next unique ledger
234 nextID() const;
235
236public:
237 LedgerOracle();
238
241 lookup(Ledger::ID const& id) const;
242
251 Ledger
252 accept(
253 Ledger const& curr,
254 TxSetType const& txs,
255 NetClock::duration closeTimeResolution,
256 NetClock::time_point const& consensusCloseTime);
257
258 Ledger
259 accept(Ledger const& curr, Tx tx)
260 {
261 using namespace std::chrono_literals;
262 return accept(curr, TxSetType{tx}, curr.closeTimeResolution(), curr.closeTime() + 1s);
263 }
264
275 branches(std::set<Ledger> const& ledgers) const;
276};
277
298{
303
308
314 Ledger const&
316 {
317 auto it = ledgers.find(s);
318 if (it != ledgers.end())
319 return it->second;
320
321 // enforce that the new suffix has never been seen
322 assert(seen.emplace(s.back()).second);
323
324 Ledger const& parent = (*this)[s.substr(0, s.size() - 1)];
325 return ledgers.emplace(s, oracle.accept(parent, Tx{++nextTx})).first->second;
326 }
327};
328
329} // namespace csf
330} // namespace test
331} // namespace xrpl
T back(T... args)
Represents a JSON value.
Definition json_value.h:130
Oracle maintaining unique ledgers for a simulation.
Definition ledgers.h:223
Ledger::ID nextID() const
Definition ledgers.cpp:72
InstanceMap::value_type InstanceEntry
Definition ledgers.h:227
std::size_t branches(std::set< Ledger > const &ledgers) const
Determine the number of distinct branches for the set of ledgers.
Definition ledgers.cpp:120
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:78
boost::bimaps::bimap< boost::bimaps::set_of< Ledger::Instance, xrpl::less< Ledger::Instance > >, boost::bimaps::set_of< Ledger::ID, xrpl::less< Ledger::ID > > > InstanceMap
Definition ledgers.h:226
std::optional< Ledger > lookup(Ledger::ID const &id) const
Find the ledger with the given ID.
Definition ledgers.cpp:109
Ledger accept(Ledger const &curr, Tx tx)
Definition ledgers.h:259
A ledger is a set of observed transactions and a sequence number identifying the ledger.
Definition ledgers.h:44
Instance const * instance_
Definition ledgers.h:217
friend bool operator<(Ledger const &a, Ledger const &b)
Definition ledgers.h:210
Ledger(MakeGenesis)
Definition ledgers.h:134
bool isAncestor(Ledger const &ancestor) const
Determine whether ancestor is really an ancestor of this ledger.
Definition ledgers.cpp:21
Json::Value getJson() const
Definition ledgers.cpp:12
static Instance const genesis
Definition ledgers.h:127
friend Ledger::Seq mismatch(Ledger const &a, Ledger const &o)
Return the sequence number of the first mismatching ancestor.
Definition ledgers.cpp:39
NetClock::duration closeTimeResolution() const
Definition ledgers.h:157
ID operator[](Seq seq) const
Return the id of the ancestor with the given seq (if exists/known)
Definition ledgers.cpp:29
Ledger(ID id, Instance const *i)
Definition ledgers.h:129
bool closeAgree() const
Definition ledgers.h:163
NetClock::time_point parentCloseTime() const
Definition ledgers.h:175
NetClock::time_point closeTime() const
Definition ledgers.h:169
TxSetType const & txs() const
Definition ledgers.h:187
tagged_integer< std::uint32_t, IdTag > ID
Definition ledgers.h:52
tagged_integer< std::uint32_t, SeqTag > Seq
Definition ledgers.h:49
A single transaction.
Definition Tx.h:22
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:59
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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:298
std::unordered_map< std::string, Ledger > ledgers
Definition ledgers.h:301
Ledger const & operator[](std::string const &s)
Get or create the ledger with the given string history.
Definition ledgers.h:315
friend bool operator==(Instance const &a, Instance const &b)
Definition ledgers.h:100
bool closeTimeAgree
Whether consensus agreed on the close time.
Definition ledgers.h:80
ID parentID
Parent ledger id.
Definition ledgers.h:83
std::vector< Ledger::ID > ancestors
IDs of this ledgers ancestors.
Definition ledgers.h:91
friend bool operator<(Instance const &a, Instance const &b)
Definition ledgers.h:112
friend bool operator!=(Instance const &a, Instance const &b)
Definition ledgers.h:106
friend void hash_append(Hasher &h, Ledger::Instance const &instance)
Definition ledgers.h:119
NetClock::time_point closeTime
When the ledger closed (up to closeTimeResolution)
Definition ledgers.h:77
NetClock::duration closeTimeResolution
Definition ledgers.h:74
NetClock::time_point parentCloseTime
Parent ledger close time.
Definition ledgers.h:86
Set the sequence number on a JTx.
Definition seq.h:14
T substr(T... args)
T tie(T... args)