1#include <xrpl/basics/Log.h>
2#include <xrpl/basics/contract.h>
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/json/to_string.h>
5#include <xrpl/ledger/Ledger.h>
6#include <xrpl/ledger/LedgerTiming.h>
7#include <xrpl/protocol/Feature.h>
8#include <xrpl/protocol/HashPrefix.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/PublicKey.h>
11#include <xrpl/protocol/SecretKey.h>
12#include <xrpl/protocol/digest.h>
13#include <xrpl/protocol/jss.h>
47 equal(base_type
const& impl)
const override
50 return iter_ == p->iter_;
94 equal(base_type
const& impl)
const override
97 return iter_ == p->iter_;
110 auto const& item = *
iter_;
130 , j_(
beast::Journal(
beast::Journal::getNullSink()))
136 static auto const id =
140 sle->setFieldU32(sfSequence, 1);
141 sle->setAccountID(sfAccount,
id);
146 if (!amendments.empty())
149 sle->setFieldV256(sfAmendments,
STVector256{amendments});
156 if (
std::find(amendments.begin(), amendments.end(), featureXRPFees) != amendments.end())
158 sle->at(sfBaseFeeDrops) =
fees.
base;
165 sle->at(sfBaseFee) = *f;
167 sle->at(sfReserveBase) = *f;
169 sle->at(sfReserveIncrement) = *f;
200 JLOG(j.
warn()) <<
"Don't have transaction root for ledger" <<
header_.
seq;
207 JLOG(j.
warn()) <<
"Don't have state data root for ledger" <<
header_.
seq;
228 , stateMap_(prevLedger.stateMap_, true)
229 , fees_(prevLedger.fees_)
230 , rules_(prevLedger.rules_)
231 , j_(
beast::Journal(
beast::Journal::getNullSink()))
258 , j_(
beast::Journal(
beast::Journal::getNullSink()))
274 , j_(
beast::Journal(
beast::Journal::getNullSink()))
306 bool correctCloseTime)
309 XRPL_ASSERT(!
open(),
"xrpl::Ledger::setAccepted : valid ledger state");
371 if (last && item->key() >= last)
379 if (k.
key == beast::zero)
382 UNREACHABLE(
"xrpl::Ledger::read : zero key");
436 auto const& item = txMap_.peekItem(key);
441 auto result = deserializeTxPlusMeta(*item);
442 return {std::move(result.first), std::move(result.second)};
444 return {deserializeTx(*item),
nullptr};
453 if (!stateMap_.peekItem(key,
digest))
464 LogicError(
"Ledger::rawErase: key not found");
471 LogicError(
"Ledger::rawErase: key not found");
481 LogicError(
"Ledger::rawInsert: key already exists");
491 LogicError(
"Ledger::rawReplace: key not found");
500 XRPL_ASSERT(metaData,
"xrpl::Ledger::rawTxInsert : non-null metadata input");
503 Serializer s(txn->getDataLength() + metaData->getDataLength() + 16);
504 s.
addVL(txn->peekData());
505 s.
addVL(metaData->peekData());
516 XRPL_ASSERT(metaData,
"xrpl::Ledger::rawTxInsertWithHash : non-null metadata input");
519 Serializer s(txn->getDataLength() + metaData->getDataLength() + 16);
520 s.
addVL(txn->peekData());
521 s.
addVL(metaData->peekData());
545 JLOG(
j_.
error()) <<
"Exception in " << __func__ <<
": " << ex.
what();
553 bool oldFees =
false;
554 bool newFees =
false;
556 auto const baseFee = sle->at(~sfBaseFee);
557 auto const reserveBase = sle->at(~sfReserveBase);
558 auto const reserveIncrement = sle->at(~sfReserveIncrement);
563 if (reserveIncrement)
565 oldFees = baseFee || reserveBase || reserveIncrement;
568 auto const baseFeeXRP = sle->at(~sfBaseFeeDrops);
569 auto const reserveBaseXRP = sle->at(~sfReserveBaseDrops);
570 auto const reserveIncrementXRP = sle->at(~sfReserveIncrementDrops);
587 newFees = baseFeeXRP || reserveBaseXRP || reserveIncrementXRP;
589 if (oldFees && newFees)
607 JLOG(
j_.
error()) <<
"Exception in " << __func__ <<
": " << ex.
what();
632 auto const& nUnlData = sle->getFieldArray(sfDisabledValidators);
633 for (
auto const& n : nUnlData)
635 if (n.isFieldPresent(sfPublicKey))
637 auto d = n.getFieldVL(sfPublicKey);
656 auto d = sle->getFieldVL(sfValidatorToDisable);
670 auto d = sle->getFieldVL(sfValidatorToReEnable);
686 bool const hasToDisable = sle->isFieldPresent(sfValidatorToDisable);
687 bool const hasToReEnable = sle->isFieldPresent(sfValidatorToReEnable);
689 if (!hasToDisable && !hasToReEnable)
693 if (sle->isFieldPresent(sfDisabledValidators))
695 auto const& oldNUnl = sle->getFieldArray(sfDisabledValidators);
696 for (
auto const& v : oldNUnl)
698 if (hasToReEnable && v.isFieldPresent(sfPublicKey) &&
699 v.getFieldVL(sfPublicKey) == sle->getFieldVL(sfValidatorToReEnable))
708 newNUnl.
back().
setFieldVL(sfPublicKey, sle->getFieldVL(sfValidatorToDisable));
712 if (!newNUnl.
empty())
714 sle->setFieldArray(sfDisabledValidators, newNUnl);
716 sle->makeFieldAbsent(sfValidatorToReEnable);
718 sle->makeFieldAbsent(sfValidatorToDisable);
749 if (!missingNodes1.
empty())
751 if (
auto stream = j.
info())
753 stream << missingNodes1.
size() <<
" missing account node(s)";
754 stream <<
"First: " << missingNodes1[0].what();
768 if (!missingNodes2.
empty())
770 if (
auto stream = j.
info())
772 stream << missingNodes2.
size() <<
" missing transaction node(s)";
773 stream <<
"First: " << missingNodes2[0].what();
776 return missingNodes1.
empty() && missingNodes2.
empty();
804 if ((prevIndex & 0xff) == 0)
810 bool created =
false;
818 hashes =
static_cast<decltype(hashes)
>(sle->getFieldV256(sfHashes));
823 hashes.
size() <= 256,
"xrpl::Ledger::updateSkipList : first maximum hashes size");
826 sle->setFieldU32(sfLastLedgerSequence, prevIndex);
841 bool created =
false;
849 hashes =
static_cast<decltype(hashes)
>(sle->getFieldV256(sfHashes));
852 XRPL_ASSERT(hashes.
size() <= 256,
"xrpl::Ledger::updateSkipList : second maximum hashes size");
853 if (hashes.
size() == 256)
857 sle->setFieldU32(sfLastLedgerSequence, prevIndex);
876 return ::xrpl::isVotingLedger(
header_.
seq + 1);
A generic endpoint for log messages.
virtual void missingNodeAcquireByHash(uint256 const &refHash, std::uint32_t refNum)=0
Acquire ledger that has a missing node by ledger hash.
sles_iter_impl(sles_iter_impl const &)=default
void increment() override
SHAMap::const_iterator iter_
bool equal(base_type const &impl) const override
std::unique_ptr< base_type > copy() const override
sles_iter_impl & operator=(sles_iter_impl const &)=delete
sles_type::value_type dereference() const override
sles_iter_impl(SHAMap::const_iterator iter)
bool equal(base_type const &impl) const override
SHAMap::const_iterator iter_
txs_iter_impl(txs_iter_impl const &)=default
std::unique_ptr< base_type > copy() const override
txs_iter_impl(bool metadata, SHAMap::const_iterator iter)
void increment() override
txs_type::value_type dereference() const override
txs_iter_impl & operator=(txs_iter_impl const &)=delete
bool txExists(uint256 const &key) const override
std::unique_ptr< txs_type::iter_base > txsEnd() const override
bool isFlagLedger() const
Returns true if the ledger is a flag ledger.
std::optional< digest_type > digest(key_type const &key) const override
Return the digest associated with the key.
void rawTxInsert(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData) override
std::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
uint256 rawTxInsertWithHash(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData)
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
void updateNegativeUNL()
update the Negative UNL ledger component.
bool isVotingLedger() const
Returns true if the ledger directly precedes a flag ledger.
std::unique_ptr< sles_type::iter_base > slesBegin() const override
hash_set< PublicKey > negativeUNL() const
get Negative UNL validators' master public keys
void rawInsert(std::shared_ptr< SLE > const &sle) override
Unconditionally insert a state item.
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
void setImmutable(bool rehash=true)
bool open() const override
Returns true if this reflects an open ledger.
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
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.
void setAccepted(NetClock::time_point closeTime, NetClock::duration closeResolution, bool correctCloseTime)
Ledger(Ledger const &)=delete
static std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
std::shared_ptr< SLE > peek(Keylet const &k) const
bool addSLE(SLE const &sle)
std::unique_ptr< txs_type::iter_base > txsBegin() const override
std::unique_ptr< sles_type::iter_base > slesEnd() const override
std::optional< uint256 > succ(uint256 const &key, std::optional< uint256 > const &last=std::nullopt) const override
Fees const & fees() const override
Returns the fees for the base ledger.
bool walkLedger(beast::Journal j, bool parallel=false) const
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
std::optional< PublicKey > validatorToReEnable() const
get the to be re-enabled validator's master public key if any
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
bool exists(Keylet const &k) const override
Determine if a state item exists.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Rules controlling protocol behavior.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
uint256 const & as_uint256() const
bool addItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
const_iterator upper_bound(uint256 const &id) const
Find the first item after the given item.
const_iterator end() const
const_iterator begin() const
boost::intrusive_ptr< SHAMapItem const > const & peekItem(uint256 const &id) const
int flushDirty(NodeObjectType t)
Flush modified nodes to the nodestore and convert them to shared.
bool walkMapParallel(std::vector< SHAMapMissingNode > &missingNodes, int maxMissing) const
void walkMap(std::vector< SHAMapMissingNode > &missingNodes, int maxMissing) const
bool hasItem(uint256 const &id) const
Does the tree have an item with the given ID?
int unshare()
Convert any modified nodes to shared.
bool updateGiveItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
bool addGiveItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
bool fetchRoot(SHAMapHash const &hash, SHAMapSyncFilter *filter)
SHAMapHash getHash() const
bool delItem(uint256 const &id)
void push_back(STObject const &object)
uint256 const & key() const
Returns the 'key' (or 'index') of this item.
void setFieldVL(SField const &field, Blob const &)
void setFieldU32(SField const &field, std::uint32_t)
Serializer getSerializer() const
static STObject makeInnerObject(SField const &name)
Slice getSlice(std::size_t bytes)
int addVL(Blob const &vector)
Slice slice() const noexcept
std::optional< Dest > dropsAs() const
T emplace_back(T... args)
Keylet const & skip() noexcept
The index of the "short" skip list.
Keylet const & negativeUNL() noexcept
The (fixed) index of the object containing the ledger negativeUNL.
Keylet const & amendments() noexcept
The index of the amendment table.
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet const & fees() noexcept
The (fixed) index of the object containing the ledger fees.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
boost::intrusive_ptr< SHAMapItem > make_shamapitem(uint256 const &tag, Slice data)
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
std::string to_string(base_uint< Bits, Tag > const &a)
XRPL_NO_SANITIZE_ADDRESS void Rethrow()
Rethrow the exception currently being handled.
Rules makeRulesGivenLedger(DigestAwareReadView const &ledger, Rules const ¤t)
create_genesis_t const create_genesis
auto constexpr ledgerDefaultTimeResolution
Initial resolution of ledger close time.
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
std::chrono::duration< Rep, Period > getNextLedgerTimeResolution(std::chrono::duration< Rep, Period > previousResolution, bool previousAgree, Seq ledgerSeq)
Calculates the close time resolution for the specified ledger.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
bool getCloseAgree(LedgerHeader const &info)
uint256 calculateLedgerHash(LedgerHeader const &info)
Calculate the hash of a ledger header.
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
std::chrono::time_point< Clock, Duration > roundCloseTime(std::chrono::time_point< Clock, Duration > closeTime, std::chrono::duration< Rep, Period > closeResolution)
Calculates the close time for a ledger, given a close time resolution.
@ open
We haven't closed our ledger yet, but others might have.
constexpr std::uint32_t FEE_UNITS_DEPRECATED
static std::uint32_t const sLCF_NoConsensusTime
AccountID calcAccountID(PublicKey const &pk)
auto constexpr ledgerGenesisTimeResolution
Close time resolution in genesis ledger.
@ txNode
transaction plus metadata
constexpr XRPAmount INITIAL_XRP
Configure the native currency.
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Reflects the fee settings for a particular ledger.
XRPAmount reserve
Minimum XRP an account must hold to exist on the ledger.
XRPAmount increment
Additional XRP reserve required per owned ledger object.
XRPAmount base
Cost of a reference transaction in drops.
A pair of SHAMap key and LedgerEntryType.
bool check(STLedgerEntry const &) const
Returns true if the SLE matches the type.