xrpld
Loading...
Searching...
No Matches
ledgers.h
1#pragma once
2
3#include <test/csf/Tx.h>
4
5#include <xrpl/basics/UnorderedContainers.h>
6#include <xrpl/basics/chrono.h>
7#include <xrpl/basics/comparators.h>
8#include <xrpl/basics/tagged_integer.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/ledger/LedgerTiming.h>
11
12#include <boost/bimap/bimap.hpp>
13
14#include <optional>
15#include <set>
16
17namespace xrpl::test::csf {
18
40class Ledger
41{
42 friend class LedgerOracle;
43
44public:
45 struct SeqTag;
47
48 struct IdTag;
50
52 {
53 };
54
55private:
56 // The instance is the common immutable data that will be assigned a unique
57 // ID by the oracle
58 struct Instance
59 {
60 Instance() = default;
61
62 // Sequence number
63 Seq seq{0};
64
65 // Transactions added to generate this ledger
67
68 // Resolution used to determine close time
70
73
75 bool closeTimeAgree = true;
76
79
82
87
88 [[nodiscard]] auto
89 asTie() const
90 {
91 return std::tie(
92 seq,
93 txs,
99 }
100
101 friend bool
102 operator==(Instance const& a, Instance const& b)
103 {
104 return a.asTie() == b.asTie();
105 }
106
107 friend bool
108 operator!=(Instance const& a, Instance const& b)
109 {
110 return a.asTie() != b.asTie();
111 }
112
113 friend bool
114 operator<(Instance const& a, Instance const& b)
115 {
116 return a.asTie() < b.asTie();
117 }
118
119 template <class Hasher>
120 friend void
122 Hasher& h,
123 Ledger::Instance const& instance) // NOLINT(readability-identifier-naming)
124 {
125 using beast::hash_append;
126 hash_append(h, instance.asTie());
127 }
128 };
129
130 // Single common genesis instance
131 static Instance const kGenesis;
132
133 Ledger(ID id, Instance const* i) : id_{id}, instance_{i}
134 {
135 }
136
137public:
141
142 // This is required by the generic Consensus for now and should be
143 // migrated to the MakeGenesis approach above.
145 {
146 }
147
148 [[nodiscard]] ID
149 id() const
150 {
151 return id_;
152 }
153
154 [[nodiscard]] Seq
155 seq() const
156 {
157 return instance_->seq;
158 }
159
160 [[nodiscard]] NetClock::duration
162 {
163 return instance_->closeTimeResolution;
164 }
165
166 [[nodiscard]] bool
168 {
169 return instance_->closeTimeAgree;
170 }
171
172 [[nodiscard]] NetClock::time_point
173 closeTime() const
174 {
175 return instance_->closeTime;
176 }
177
178 [[nodiscard]] NetClock::time_point
180 {
181 return instance_->parentCloseTime;
182 }
183
184 [[nodiscard]] ID
185 parentID() const
186 {
187 return instance_->parentID;
188 }
189
190 [[nodiscard]] TxSetType const&
191 txs() const
192 {
193 return instance_->txs;
194 }
195
197 [[nodiscard]] bool
198 isAncestor(Ledger const& ancestor) const;
199
202 ID
203 operator[](Seq seq) const;
204
207 friend Ledger::Seq
208 mismatch(Ledger const& a, Ledger const& o);
209
210 [[nodiscard]] json::Value
211 getJson() const;
212
213 friend bool
214 operator<(Ledger const& a, Ledger const& b)
215 {
216 return a.id() < b.id();
217 }
218
219private:
220 ID id_{0};
222};
223
227{
228 using InstanceMap = boost::bimaps::bimap<
229 boost::bimaps::set_of<Ledger::Instance, xrpl::less<Ledger::Instance>>,
230 boost::bimaps::set_of<Ledger::ID, xrpl::less<Ledger::ID>>>;
231 using InstanceEntry = InstanceMap::value_type;
232
233 // Set of all known ledgers; note this is never pruned
235
236 // ID for the next unique ledger
237 [[nodiscard]] Ledger::ID
238 nextID() const;
239
240public:
241 LedgerOracle();
242
244 [[nodiscard]] std::optional<Ledger>
245 lookup(Ledger::ID const& id) const;
246
255 Ledger
256 accept(
257 Ledger const& curr,
258 TxSetType const& txs,
259 NetClock::duration closeTimeResolution,
260 NetClock::time_point const& consensusCloseTime);
261
262 Ledger
263 accept(Ledger const& curr, Tx tx)
264 {
265 using namespace std::chrono_literals;
266 return accept(curr, TxSetType{tx}, curr.closeTimeResolution(), curr.closeTime() + 1s);
267 }
268
278 static std::size_t
279 branches(std::set<Ledger> const& ledgers);
280};
281
302{
307
312
318 Ledger const&
320 {
321 auto it = ledgers.find(s);
322 if (it != ledgers.end())
323 return it->second;
324
325 // enforce that the new suffix has never been seen
326 assert(seen.emplace(s.back()).second);
327
328 Ledger const& parent = (*this)[s.substr(0, s.size() - 1)];
329 return ledgers.emplace(s, oracle.accept(parent, Tx{++nextTx})).first->second;
330 }
331};
332
333} // namespace xrpl::test::csf
T back(T... args)
Represents a JSON value.
Definition json_value.h:130
std::chrono::time_point< NetClock > time_point
Definition chrono.h:46
std::chrono::duration< rep, period > duration
Definition chrono.h:45
A type-safe wrap around standard integral types.
Oracle maintaining unique ledgers for a simulation.
Definition ledgers.h:227
Ledger::ID nextID() const
Definition ledgers.cpp:83
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:228
InstanceMap::value_type InstanceEntry
Definition ledgers.h:231
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
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:263
A ledger is a set of observed transactions and a sequence number identifying the ledger.
Definition ledgers.h:41
Instance const * instance_
Definition ledgers.h:221
friend bool operator<(Ledger const &a, Ledger const &b)
Definition ledgers.h:214
Ledger(MakeGenesis)
Definition ledgers.h:138
TaggedInteger< std::uint32_t, IdTag > ID
Definition ledgers.h:49
TaggedInteger< std::uint32_t, SeqTag > Seq
Definition ledgers.h:46
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:131
friend class LedgerOracle
Definition ledgers.h:42
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:161
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:133
bool closeAgree() const
Definition ledgers.h:167
NetClock::time_point parentCloseTime() const
Definition ledgers.h:179
NetClock::time_point closeTime() const
Definition ledgers.h:173
TxSetType const & txs() const
Definition ledgers.h:191
json::Value getJson() const
Definition ledgers.cpp:21
A single transaction.
Definition Tx.h:21
std::uint32_t ID
Definition Tx.h:23
std::enable_if_t< IsContiguouslyHashable< 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:58
constexpr auto kLedgerDefaultTimeResolution
Initial resolution of ledger close time.
T size(T... args)
std::unordered_map< std::string, Ledger > ledgers
Definition ledgers.h:305
Ledger const & operator[](std::string const &s)
Get or create the ledger with the given string history.
Definition ledgers.h:319
friend bool operator==(Instance const &a, Instance const &b)
Definition ledgers.h:102
bool closeTimeAgree
Whether consensus agreed on the close time.
Definition ledgers.h:75
ID parentID
Parent ledger id.
Definition ledgers.h:78
std::vector< Ledger::ID > ancestors
Definition ledgers.h:86
friend bool operator<(Instance const &a, Instance const &b)
Definition ledgers.h:114
friend bool operator!=(Instance const &a, Instance const &b)
Definition ledgers.h:108
friend void hash_append(Hasher &h, Ledger::Instance const &instance)
Definition ledgers.h:121
NetClock::time_point closeTime
When the ledger closed (up to closeTimeResolution).
Definition ledgers.h:72
NetClock::duration closeTimeResolution
Definition ledgers.h:69
NetClock::time_point parentCloseTime
Parent ledger close time.
Definition ledgers.h:81
Set the sequence number on a JTx.
Definition seq.h:12
T substr(T... args)
T tie(T... args)