xrpld
Loading...
Searching...
No Matches
ledgers.h
1#pragma once
2
3#include <xrpl/basics/chrono.h>
4#include <xrpl/basics/tagged_integer.h>
5#include <xrpl/json/json_value.h>
6#include <xrpl/ledger/LedgerTiming.h>
7
8#include <boost/bimap/bimap.hpp>
9
10#include <csf/Tx.h>
11
12#include <cassert>
13#include <cstddef>
14#include <cstdint>
15#include <functional>
16#include <optional>
17#include <set>
18#include <string>
19#include <tuple>
20#include <unordered_map>
21#include <vector>
22
23namespace xrpl::test::csf {
24
47class Ledger
48{
49 friend class LedgerOracle;
50
51public:
52 struct SeqTag;
54
55 struct IdTag;
57
59 {
60 };
61
62private:
63 // The instance is the common immutable data that will be assigned a unique
64 // ID by the oracle
65 struct Instance
66 {
67 Instance() = default;
68
69 // Sequence number
70 Seq seq{0};
71
72 // Transactions added to generate this ledger
74
75 // Resolution used to determine close time
77
82
86 bool closeTimeAgree = true;
87
92
97
104
105 [[nodiscard]] auto
106 asTie() const
107 {
108 return std::tie(
109 seq,
110 txs,
112 closeTime,
114 parentID,
116 }
117
118 friend bool
119 operator==(Instance const& a, Instance const& b)
120 {
121 return a.asTie() == b.asTie();
122 }
123
124 friend bool
125 operator!=(Instance const& a, Instance const& b)
126 {
127 return a.asTie() != b.asTie();
128 }
129
130 friend bool
131 operator<(Instance const& a, Instance const& b)
132 {
133 return a.asTie() < b.asTie();
134 }
135
136 template <class Hasher>
137 friend void
139 Hasher& h,
140 Ledger::Instance const& instance) // NOLINT(readability-identifier-naming)
141 {
142 using beast::hash_append;
143 hash_append(h, instance.asTie());
144 }
145 };
146
147 // Single common genesis instance
148 static Instance const kGenesis;
149
150 Ledger(ID id, Instance const* i) : id_{id}, instance_{i}
151 {
152 }
153
154public:
158
159 // This is required by the generic Consensus for now and should be
160 // migrated to the MakeGenesis approach above.
162 {
163 }
164
165 [[nodiscard]] ID
166 id() const
167 {
168 return id_;
169 }
170
171 [[nodiscard]] Seq
172 seq() const
173 {
174 return instance_->seq;
175 }
176
177 [[nodiscard]] NetClock::duration
179 {
180 return instance_->closeTimeResolution;
181 }
182
183 [[nodiscard]] bool
185 {
186 return instance_->closeTimeAgree;
187 }
188
189 [[nodiscard]] NetClock::time_point
190 closeTime() const
191 {
192 return instance_->closeTime;
193 }
194
195 [[nodiscard]] NetClock::time_point
197 {
198 return instance_->parentCloseTime;
199 }
200
201 [[nodiscard]] ID
202 parentID() const
203 {
204 return instance_->parentID;
205 }
206
207 [[nodiscard]] TxSetType const&
208 txs() const
209 {
210 return instance_->txs;
211 }
212
216 [[nodiscard]] bool
217 isAncestor(Ledger const& ancestor) const;
218
222 ID
223 operator[](Seq seq) const;
224
228 friend Ledger::Seq
229 mismatch(Ledger const& a, Ledger const& o);
230
231 [[nodiscard]] json::Value
232 getJson() const;
233
234 friend bool
235 operator<(Ledger const& a, Ledger const& b)
236 {
237 return a.id() < b.id();
238 }
239
240private:
241 ID id_{0};
243};
244
249{
250 using InstanceMap = boost::bimaps::bimap<
251 boost::bimaps::set_of<Ledger::Instance, std::less<>>,
252 boost::bimaps::set_of<Ledger::ID, std::less<>>>;
253 using InstanceEntry = InstanceMap::value_type;
254
255 // Set of all known ledgers; note this is never pruned
257
258 // ID for the next unique ledger
259 [[nodiscard]] Ledger::ID
260 nextID() const;
261
262public:
263 LedgerOracle();
264
268 [[nodiscard]] std::optional<Ledger>
269 lookup(Ledger::ID const& id) const;
270
280 Ledger
281 accept(
282 Ledger const& curr,
283 TxSetType const& txs,
284 NetClock::duration closeTimeResolution,
285 NetClock::time_point const& consensusCloseTime);
286
287 Ledger
288 accept(Ledger const& curr, Tx tx)
289 {
290 using namespace std::chrono_literals;
291 return accept(curr, TxSetType{tx}, curr.closeTimeResolution(), curr.closeTime() + 1s);
292 }
293
304 static std::size_t
305 branches(std::set<Ledger> const& ledgers);
306};
307
328{
333
338
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})).first->second;
357 }
358};
359
360} // namespace xrpl::test::csf
T back(T... args)
Represents a JSON value.
Definition json_value.h:117
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
A type-safe wrap around standard integral types.
Oracle maintaining unique ledgers for a simulation.
Definition ledgers.h:249
Ledger::ID nextID() const
Definition ledgers.cpp:83
InstanceMap::value_type InstanceEntry
Definition ledgers.h:253
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:89
std::optional< Ledger > lookup(Ledger::ID const &id) const
Find the ledger with the given ID.
Definition ledgers.cpp:124
boost::bimaps::bimap< boost::bimaps::set_of< Ledger::Instance, std::less<> >, boost::bimaps::set_of< Ledger::ID, std::less<> > > InstanceMap
Definition ledgers.h:250
static std::size_t branches(std::set< Ledger > const &ledgers)
Determine the number of distinct branches for the set of ledgers.
Definition ledgers.cpp:135
Ledger accept(Ledger const &curr, Tx tx)
Definition ledgers.h:288
A ledger is a set of observed transactions and a sequence number identifying the ledger.
Definition ledgers.h:48
Instance const * instance_
Definition ledgers.h:242
friend bool operator<(Ledger const &a, Ledger const &b)
Definition ledgers.h:235
Ledger(MakeGenesis)
Definition ledgers.h:155
TaggedInteger< std::uint32_t, IdTag > ID
Definition ledgers.h:56
TaggedInteger< std::uint32_t, SeqTag > Seq
Definition ledgers.h:53
bool isAncestor(Ledger const &ancestor) const
Determine whether ancestor is really an ancestor of this ledger.
Definition ledgers.cpp:30
static Instance const kGenesis
Definition ledgers.h:148
friend class LedgerOracle
Definition ledgers.h:49
friend Ledger::Seq mismatch(Ledger const &a, Ledger const &o)
Return the sequence number of the first mismatching ancestor.
Definition ledgers.cpp:48
NetClock::duration closeTimeResolution() const
Definition ledgers.h:178
ID operator[](Seq seq) const
Return the id of the ancestor with the given seq (if exists/known).
Definition ledgers.cpp:38
Ledger(ID id, Instance const *i)
Definition ledgers.h:150
bool closeAgree() const
Definition ledgers.h:184
NetClock::time_point parentCloseTime() const
Definition ledgers.h:196
NetClock::time_point closeTime() const
Definition ledgers.h:190
TxSetType const & txs() const
Definition ledgers.h:208
json::Value getJson() const
Definition ledgers.cpp:21
A single transaction.
Definition Tx.h:24
std::uint32_t ID
Definition Tx.h:26
void hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
boost::container::flat_set< Tx > TxSetType
Definition Tx.h:65
constexpr auto kLedgerDefaultTimeResolution
Initial resolution of ledger close time.
T size(T... args)
std::unordered_map< std::string, Ledger > ledgers
Definition ledgers.h:331
Ledger const & operator[](std::string const &s)
Get or create the ledger with the given string history.
Definition ledgers.h:346
friend bool operator==(Instance const &a, Instance const &b)
Definition ledgers.h:119
bool closeTimeAgree
Whether consensus agreed on the close time.
Definition ledgers.h:86
ID parentID
Parent ledger id.
Definition ledgers.h:91
std::vector< Ledger::ID > ancestors
IDs of this ledgers ancestors.
Definition ledgers.h:103
friend bool operator<(Instance const &a, Instance const &b)
Definition ledgers.h:131
friend bool operator!=(Instance const &a, Instance const &b)
Definition ledgers.h:125
friend void hash_append(Hasher &h, Ledger::Instance const &instance)
Definition ledgers.h:138
NetClock::time_point closeTime
When the ledger closed (up to closeTimeResolution).
Definition ledgers.h:81
NetClock::duration closeTimeResolution
Definition ledgers.h:76
NetClock::time_point parentCloseTime
Parent ledger close time.
Definition ledgers.h:96
Set the sequence number on a JTx.
Definition seq.h:16
T substr(T... args)
T tie(T... args)