xrpld
Loading...
Searching...
No Matches
include/xrpl/ledger/Ledger.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/UnorderedContainers.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/chrono.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/ledger/CachedView.h>
9#include <xrpl/ledger/RawView.h>
10#include <xrpl/protocol/Fees.h>
11#include <xrpl/protocol/Keylet.h>
12#include <xrpl/protocol/LedgerHeader.h>
13#include <xrpl/protocol/PublicKey.h>
14#include <xrpl/protocol/Rules.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STObject.h>
17#include <xrpl/protocol/STTx.h>
18#include <xrpl/protocol/Serializer.h>
19#include <xrpl/protocol/XRPAmount.h>
20#include <xrpl/shamap/Family.h>
21#include <xrpl/shamap/SHAMap.h>
22#include <xrpl/shamap/SHAMapItem.h>
23
24#include <cstdint>
25#include <memory>
26#include <mutex>
27#include <optional>
28#include <utility>
29#include <vector>
30
31namespace xrpl {
32
33class ServiceRegistry;
34class Job;
36
37class SqliteStatement;
38
40{
41 explicit CreateGenesisT() = default;
42};
44
72class Ledger final : public std::enable_shared_from_this<Ledger>,
74 public TxsRawView,
75 public CountedObject<Ledger>
76{
77public:
78 Ledger(Ledger const&) = delete;
79 Ledger&
80 operator=(Ledger const&) = delete;
81
82 Ledger(Ledger&&) = delete;
83 Ledger&
84 operator=(Ledger&&) = delete;
85
101 Ledger(
103 Rules rules,
104 Fees const& fees,
105 std::vector<UInt256> const& amendments,
106 Family& family);
107
108 Ledger(LedgerHeader const& info, Rules rules, Family& family);
109
118 Ledger(
119 LedgerHeader const& info,
120 bool& loaded,
121 bool acquire,
122 Rules rules,
123 Fees const& fees,
124 Family& family,
126
134 Ledger(Ledger const& previous, NetClock::time_point closeTime);
135
136 // used for database ledgers
137 Ledger(
138 std::uint32_t ledgerSeq,
139 NetClock::time_point closeTime,
140 Rules rules,
141 Fees const& fees,
142 Family& family);
143
144 ~Ledger() override = default;
145
146 //
147 // ReadView
148 //
149
150 bool
151 open() const override
152 {
153 return false;
154 }
155
156 LedgerHeader const&
157 header() const override
158 {
159 return header_;
160 }
161
162 void
164 {
165 header_ = info;
166 }
167
168 Fees const&
169 fees() const override
170 {
171 return fees_;
172 }
173
174 Rules const&
175 rules() const override
176 {
177 return rules_;
178 }
179
180 bool
181 exists(Keylet const& k) const override;
182
183 bool
184 exists(UInt256 const& key) const;
185
187 succ(UInt256 const& key, std::optional<UInt256> const& last = std::nullopt) const override;
188
190 read(Keylet const& k) const override;
191
193 slesBegin() const override;
194
196 slesEnd() const override;
197
199 slesUpperBound(UInt256 const& key) const override;
200
202 txsBegin() const override;
203
205 txsEnd() const override;
206
207 bool
208 txExists(UInt256 const& key) const override;
209
210 TxType
211 txRead(key_type const& key) const override;
212
213 //
214 // DigestAwareReadView
215 //
216
218 digest(key_type const& key) const override;
219
220 //
221 // RawView
222 //
223
224 void
225 rawErase(SLE::Ref sle) override;
226
227 void
228 rawInsert(SLE::Ref sle) override;
229
230 void
231 rawErase(UInt256 const& key);
232
233 void
234 rawReplace(SLE::Ref sle) override;
235
236 void
237 rawDestroyXRP(XRPAmount const& fee) override
238 {
239 header_.drops -= fee;
240 }
241
242 //
243 // TxsRawView
244 //
245
246 void
248 UInt256 const& key,
250 std::shared_ptr<Serializer const> const& metaData) override;
251
252 //--------------------------------------------------------------------------
253
254 void
256 {
257 header_.validated = true;
258 }
259
272 [[nodiscard]] bool
274 NetClock::time_point closeTime,
275 NetClock::duration closeResolution,
276 bool correctCloseTime);
277
295 [[nodiscard]] bool
296 setImmutable(bool rehash = true);
297
298 bool
300 {
301 return immutable_;
302 }
303
308 [[nodiscard]] bool
309 mapsValid() const
310 {
311 return txMap_.isValid() && stateMap_.isValid();
312 }
313
321 void
322 setFull() const
323 {
324 txMap_.setFull(header_.seq);
325 stateMap_.setFull(header_.seq);
326 }
327
328 void
330 {
331 header_.drops = totDrops;
332 }
333
334 SHAMap const&
335 stateMap() const
336 {
337 return stateMap_;
338 }
339
340 SHAMap&
342 {
343 return stateMap_;
344 }
345
346 SHAMap const&
347 txMap() const
348 {
349 return txMap_;
350 }
351
352 SHAMap&
354 {
355 return txMap_;
356 }
357
358 // returns false on error
359 bool
360 addSLE(SLE const& sle);
361
362 //--------------------------------------------------------------------------
363
364 void
366
367 bool
368 walkLedger(beast::Journal j, bool parallel = false) const;
369
370 bool
371 isSensible() const;
372
373 void
374 invariants() const;
375 void
376 unshare() const;
377
384 negativeUNL() const;
385
392 validatorToDisable() const;
393
400 validatorToReEnable() const;
401
407 void
409
413 bool
414 isFlagLedger() const;
415
419 bool
420 isVotingLedger() const;
421
423 peek(Keylet const& k) const;
424
425private:
426 class SlesIterImpl;
427 class TxsIterImpl;
428
429 bool
430 setup();
431
440 deserializeTx(SHAMapItem const& item);
441
454
463 [[nodiscard]] bool
465 {
466 bool const txImmutable = txMap_.setImmutable();
467 bool const stateImmutable = stateMap_.setImmutable();
468 return txImmutable && stateImmutable;
469 }
470
472
473 // A SHAMap containing the transactions associated with this ledger.
474 SHAMap mutable txMap_;
475
476 // A SHAMap containing the state objects for this ledger.
478
479 // Protects fee variables
481
486};
487
492
493} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Wraps a DigestAwareReadView to provide caching.
Definition CachedView.h:143
std::unique_ptr< TxsType::IterBase > txsEnd() const override
HashSet< PublicKey > negativeUNL() const
get Negative UNL validators' master public keys
bool txExists(UInt256 const &key) const override
bool isFlagLedger() const
Returns true if the ledger is a flag ledger.
std::optional< DigestType > digest(key_type const &key) const override
Return the digest associated with the key.
std::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
void setLedgerInfo(LedgerHeader const &info)
void rawInsert(SLE::Ref sle) override
Unconditionally insert a state item.
void updateNegativeUNL()
update the Negative UNL ledger component.
bool isVotingLedger() const
Returns true if the ledger directly precedes a flag ledger.
std::unique_ptr< SlesType::IterBase > slesBegin() const override
void rawReplace(SLE::Ref sle) override
Unconditionally replace a state item.
Ledger(Ledger &&)=delete
bool setImmutable(bool rehash=true)
Mark this ledger as immutable, so it can no longer be modified.
SHAMap const & stateMap() const
SHAMap const & txMap() const
std::unique_ptr< SlesType::IterBase > slesUpperBound(UInt256 const &key) const override
bool open() const override
Returns true if this reflects an open ledger.
static std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > deserializeTxPlusMeta(SHAMapItem const &item)
Deserialize a SHAMapItem containing STTx + STObject metadata.
LedgerHeader const & header() const override
Returns information about the ledger.
Ledger(Ledger const &)=delete
static std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Ledger & operator=(Ledger const &)=delete
void setTotalDrops(std::uint64_t totDrops)
bool addSLE(SLE const &sle)
bool setAccepted(NetClock::time_point closeTime, NetClock::duration closeResolution, bool correctCloseTime)
Mark this ledger as accepted and attempt to make it immutable.
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
std::unique_ptr< TxsType::IterBase > txsBegin() const override
std::optional< UInt256 > succ(UInt256 const &key, std::optional< UInt256 > const &last=std::nullopt) const override
std::unique_ptr< SlesType::IterBase > slesEnd() const override
~Ledger() override=default
void rawTxInsert(UInt256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData) override
Ledger & operator=(Ledger &&)=delete
bool setMapsImmutable()
Make both maps immutable.
Rules const & rules() const override
Returns the tx processing rules.
Fees const & fees() const override
Returns the fees for the base ledger.
bool walkLedger(beast::Journal j, bool parallel=false) const
std::optional< PublicKey > validatorToReEnable() const
get the to be re-enabled validator's master public key if any
TxType txRead(key_type const &key) const override
Read a transaction from the tx map.
SLE::const_pointer read(Keylet const &k) const override
Return the state item associated with a key.
void setFull() const
Mark this ledger as "should be full", indicating that the local server wants all the corresponding no...
SLE::pointer peek(Keylet const &k) const
bool exists(Keylet const &k) const override
Determine if a state item exists.
void rawErase(SLE::Ref sle) override
Delete an existing state item.
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
UInt256 key_type
Definition ReadView.h:45
Rules controlling protocol behavior.
Definition Rules.h:40
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry > const & Ref
std::shared_ptr< STLedgerEntry const > const_pointer
Service registry for dependency injection.
Interface for changing ledger entries with transactions.
Definition RawView.h:84
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TxType
Transaction type identifiers.
Definition TxFormats.h:45
CreateGenesisT const kCreateGenesis
STLedgerEntry SLE
BaseUInt< 256 > UInt256
Definition base_uint.h:581
CachedView< Ledger > CachedLedger
A ledger wrapped in a CachedView.
std::unordered_set< Value, Hash, Pred, Allocator > HashSet
CreateGenesisT()=default
Reflects the fee settings for a particular ledger.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
Information about the notional ledger backing the view.