20 #include <ripple/basics/Log.h>
21 #include <ripple/basics/chrono.h>
22 #include <ripple/basics/contract.h>
23 #include <ripple/ledger/ReadView.h>
24 #include <ripple/ledger/View.h>
25 #include <ripple/protocol/Feature.h>
26 #include <ripple/protocol/Protocol.h>
27 #include <ripple/protocol/Quality.h>
28 #include <ripple/protocol/st.h>
40 std::is_same_v<std::remove_cv_t<N>,
SLE> &&
41 std::is_base_of_v<ReadView, V>>>
50 auto const& svIndexes = page->getFieldV256(
sfIndexes);
51 assert(index <= svIndexes.size());
53 if (index >= svIndexes.size())
63 if constexpr (std::is_const_v<N>)
78 entry = svIndexes[index++];
86 std::is_same_v<std::remove_cv_t<N>,
SLE> &&
87 std::is_base_of_v<ReadView, V>>>
96 if constexpr (std::is_const_v<N>)
211 if (issuer != account)
241 amount.
clear({currency, issuer});
245 isFrozen(view, account, currency, issuer))
252 if (account > issuer)
259 JLOG(j.
trace()) <<
"accountHolds:"
303 if (adjusted < current)
308 <<
"Account " << *
id <<
" owner count exceeds max!";
316 if (adjusted > current)
321 <<
"Account " << *
id <<
" owner count set below 0!";
347 auto const fullBalance = sle->getFieldAmount(
sfBalance);
351 STAmount amount = balance - reserve;
352 if (balance < reserve)
355 JLOG(j.
trace()) <<
"accountHolds:"
358 <<
" fullBalance=" << fullBalance.getFullText()
359 <<
" balance=" << balance.getFullText()
360 <<
" reserve=" << reserve <<
" ownerCount=" << ownerCount
361 <<
" ownerCountAdj=" << ownerCountAdj;
381 auto sle = view.
read(pos);
384 for (
auto const& key : sle->getFieldV256(
sfIndexes))
407 auto currentIndex =
root;
410 if (
after.isNonZero())
414 if (
auto hintDir = view.
read(hintIndex))
416 for (
auto const& key : hintDir->getFieldV256(
sfIndexes))
421 currentIndex = hintIndex;
430 auto const ownerDir = view.
read(currentIndex);
433 for (
auto const& key : ownerDir->getFieldV256(
sfIndexes))
446 auto const uNodeNext = ownerDir->getFieldU64(
sfIndexNext);
456 auto const ownerDir = view.
read(currentIndex);
459 for (
auto const& key : ownerDir->getFieldV256(
sfIndexes))
462 auto const uNodeNext = ownerDir->getFieldU64(
sfIndexNext);
497 if (hash && (*hash != validLedger.
info().
hash))
499 JLOG(s) << reason <<
" incompatible with valid ledger";
501 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
513 if (hash && (*hash != testLedger.
info().
hash))
515 JLOG(s) << reason <<
" incompatible preceding ledger";
517 JLOG(s) <<
"Hash(NSeq): " <<
to_string(*hash);
527 JLOG(s) << reason <<
" incompatible ledger";
534 JLOG(s) <<
"Val: " << validLedger.
info().
seq <<
" "
537 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
554 if (testLedger.
info().
seq > validIndex)
561 if (hash && (*hash != validHash))
563 JLOG(s) << reason <<
" incompatible following ledger";
564 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
570 (validIndex == testLedger.
info().
seq) &&
571 (testLedger.
info().
hash != validHash))
573 JLOG(s) << reason <<
" incompatible ledger";
580 JLOG(s) <<
"Val: " << validIndex <<
" " <<
to_string(validHash);
582 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
592 auto const sleNode = view.
read(k);
595 if (!sleNode->getFieldV256(
sfIndexes).empty())
613 amendments.insert(v.begin(), v.end());
630 using d = tp::duration;
632 auto const majorities = sle->getFieldArray(
sfMajorities);
634 for (
auto const& m : majorities)
647 if (seq > ledger.
seq())
650 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" future";
653 if (seq == ledger.
seq())
655 if (seq == (ledger.
seq() - 1))
658 if (
int diff = ledger.
seq() - seq; diff <= 256)
668 if (vec.
size() >= diff)
669 return vec[vec.
size() - diff];
671 <<
"Ledger " << ledger.
seq() <<
" missing hash for " << seq
672 <<
" (" << vec.
size() <<
"," << diff <<
")";
677 <<
"Ledger " << ledger.
seq() <<
":" << ledger.
info().
hash
678 <<
" missing normal list";
682 if ((seq & 0xff) != 0)
684 JLOG(journal.
debug())
685 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" past";
694 assert(lastSeq >= seq);
695 assert((lastSeq & 0xff) == 0);
696 auto const diff = (lastSeq - seq) >> 8;
698 if (vec.
size() > diff)
699 return vec[vec.
size() - diff - 1];
701 JLOG(journal.
warn()) <<
"Can't get seq " << seq <<
" from " << ledger.
seq()
747 const bool bNoRipple,
757 JLOG(j.
trace()) <<
"trustCreate: " <<
to_string(uSrcAccountID) <<
", "
761 auto const& uLowAccountID = !bSrcHigh ? uSrcAccountID : uDstAccountID;
762 auto const& uHighAccountID = bSrcHigh ? uSrcAccountID : uDstAccountID;
764 auto const sleRippleState = std::make_shared<SLE>(
ltRIPPLE_STATE, uIndex);
765 view.
insert(sleRippleState);
769 sleRippleState->
key(),
777 sleRippleState->
key(),
783 const bool bSetDst = saLimit.
getIssuer() == uDstAccountID;
784 const bool bSetHigh = bSrcHigh ^ bSetDst;
792 (bSetHigh ? uHighAccountID : uLowAccountID));
799 sleRippleState->setFieldU64(
sfLowNode, *lowNode);
800 sleRippleState->setFieldU64(
sfHighNode, *highNode);
802 sleRippleState->setFieldAmount(
804 sleRippleState->setFieldAmount(
808 bSetDst ? uSrcAccountID : uDstAccountID}));
811 sleRippleState->setFieldU32(
815 sleRippleState->setFieldU32(
839 sleRippleState->setFieldU32(
sfFlags, uFlags);
843 sleRippleState->setFieldAmount(
844 sfBalance, bSetHigh ? -saBalance : saBalance);
847 uSrcAccountID, uDstAccountID, saBalance, saBalance.zeroed());
864 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: low";
869 sleRippleState->
key(),
875 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: high";
880 sleRippleState->
key(),
886 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: state";
887 view.
erase(sleRippleState);
897 auto offerIndex = sle->
key();
945 assert(!bCheckIssuer || uSenderID == issuer || uReceiverID == issuer);
949 assert(uSenderID != uReceiverID);
951 bool const bSenderHigh = uSenderID > uReceiverID;
952 auto const index =
keylet::line(uSenderID, uReceiverID, currency);
958 if (
auto const sleRippleState = view.
peek(index))
965 view.
creditHook(uSenderID, uReceiverID, saAmount, saBalance);
967 STAmount const saBefore = saBalance;
969 saBalance -= saAmount;
971 JLOG(j.
trace()) <<
"rippleCredit: " <<
to_string(uSenderID) <<
" -> "
978 bool bDelete =
false;
982 if (saBefore > beast::zero
984 && saBalance <= beast::zero
995 !sleRippleState->getFieldAmount(
998 && !sleRippleState->getFieldU32(
1001 && !sleRippleState->getFieldU32(
1010 sleRippleState->setFieldU32(
1015 bDelete = !saBalance
1024 sleRippleState->setFieldAmount(
sfBalance, saBalance);
1032 bSenderHigh ? uReceiverID : uSenderID,
1033 !bSenderHigh ? uReceiverID : uSenderID,
1037 view.
update(sleRippleState);
1041 STAmount const saReceiverLimit({currency, uReceiverID});
1046 JLOG(j.
debug()) <<
"rippleCredit: "
1086 auto const issuer = saAmount.
getIssuer();
1088 assert(!
isXRP(uSenderID) && !
isXRP(uReceiverID));
1089 assert(uSenderID != uReceiverID);
1091 if (uSenderID == issuer || uReceiverID == issuer || issuer ==
noAccount())
1095 rippleCredit(view, uSenderID, uReceiverID, saAmount,
false, j);
1098 saActual = saAmount;
1108 JLOG(j.
debug()) <<
"rippleSend> " <<
to_string(uSenderID) <<
" - > "
1113 TER terResult =
rippleCredit(view, issuer, uReceiverID, saAmount,
true, j);
1116 terResult =
rippleCredit(view, uSenderID, issuer, saActual,
true, j);
1129 assert(saAmount >= beast::zero);
1134 if (!saAmount || (uSenderID == uReceiverID))
1141 JLOG(j.
trace()) <<
"accountSend: " <<
to_string(uSenderID) <<
" -> "
1145 return rippleSend(view, uSenderID, uReceiverID, saAmount, saActual, j);
1162 if (
auto stream = j.
trace())
1173 stream <<
"accountSend> " <<
to_string(uSenderID) <<
" (" << sender_bal
1174 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1208 if (
auto stream = j.
trace())
1219 stream <<
"accountSend< " <<
to_string(uSenderID) <<
" (" << sender_bal
1220 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1246 if (before > beast::zero
1248 &&
after <= beast::zero
1252 &&
static_cast<bool>(
1291 assert(issue == amount.
issue());
1294 assert(issue.
account != account);
1299 bool bSenderHigh = issue.
account > account;
1303 if (
auto state = view.
peek(index))
1310 STAmount const start_balance = final_balance;
1312 final_balance -= amount;
1331 state->setFieldAmount(
sfBalance, final_balance);
1336 bSenderHigh ? account : issue.
account,
1337 bSenderHigh ? issue.
account : account,
1354 if (!receiverAccount)
1387 assert(issue == amount.
issue());
1390 assert(issue.
account != account);
1395 bool bSenderHigh = account > issue.
account;
1405 STAmount const start_balance = final_balance;
1407 final_balance -= amount;
1410 view, state, bSenderHigh, account, start_balance, final_balance, j);
1420 state->setFieldAmount(
sfBalance, final_balance);
1427 bSenderHigh ? issue.
account : account,
1428 bSenderHigh ? account : issue.
account,
1441 <<
" but no trust line exists!";
1454 assert(from != beast::zero);
1455 assert(to != beast::zero);
1461 if (!sender || !receiver)
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
const SF_UINT64 sfIndexNext
const SF_UINT32 sfHighQualityIn
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
const SF_UINT32 sfOwnerCount
Rate transferRate(ReadView const &view, AccountID const &issuer)
bool cdirNext(ReadView const &view, uint256 const &root, std::shared_ptr< SLE const > &page, unsigned int &index, uint256 &entry)
Returns the next entry in the directory, advancing the index.
bool dirNext(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
uint256 const & key() const
Returns the 'key' (or 'index') of this item.
majorityAmendments_t getMajorityAmendments(ReadView const &view)
A pair of SHAMap key and LedgerEntryType.
std::set< uint256 > getEnabledAmendments(ReadView const &view)
A currency issued by an account.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Keylet const & amendments() noexcept
The index of the amendment table.
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items after an item in the given directory.
const SF_UINT64 sfOwnerNode
Represents a transfer rate.
Stream trace() const
Severity stream access functions.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
Issue const & issue() const
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
std::uint64_t getFieldU64(SField const &field) const
constexpr value_type drops() const
Returns the number of drops.
bool internalDirNext(V &view, uint256 const &root, std::shared_ptr< N > &page, unsigned int &index, uint256 &entry)
void addRaw(LedgerInfo const &info, Serializer &s, bool includeHash)
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
const SF_UINT256 sfBookDirectory
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
int add8(unsigned char i)
virtual void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance)
Keylet const & skip() noexcept
The index of the "short" skip list.
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
virtual STAmount balanceHook(AccountID const &account, AccountID const &issuer, STAmount const &amount) const
const SF_UINT32 sfCloseTime
void setIssuer(AccountID const &uIssuer)
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
FreezeHandling
Controls the treatment of frozen account balances.
int add64(std::uint64_t i)
static Sink & getNullSink()
Returns a Sink which does nothing.
const Rate parityRate(QUALITY_ONE)
A transfer rate signifying a 1:1 exchange.
Writeable view to a ledger, for applying a transaction.
@ ltDIR_NODE
A ledger object which contains a list of object identifiers.
AccountID const & getIssuer() const
const uint256 featureDeletableAccounts
const SF_VECTOR256 sfIndexes
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
NetClock::time_point closeTime
const SF_UINT64 sfLowNode
const SF_UINT32 sfLowQualityOut
std::string getFullText() const override
const SF_AMOUNT sfLowLimit
T time_since_epoch(T... args)
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
std::map< uint256, NetClock::time_point > majorityAmendments_t
Keylet account(AccountID const &id) noexcept
AccountID root.
void setFieldAmount(SField const &field, STAmount const &)
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
AccountID getAccountID(SField const &field) const
Keylet page(uint256 const &key, std::uint64_t index) noexcept
A page in a directory.
const SF_UINT64 sfBookNode
const SF_UINT32 sfLowQualityIn
TERSubset< CanCvtToTER > TER
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j)
AccountID const & xrpAccount()
Compute AccountID from public key.
Provide a light-weight way to check active() before string formatting.
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
const SF_UINT32 sfTransferRate
bool isXRP(AccountID const &c)
@ current
This was a new validation and was added.
A generic endpoint for log messages.
const SF_AMOUNT sfHighLimit
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
static TER rippleSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, STAmount &saActual, beast::Journal j)
virtual void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next)
TER trustDelete(ApplyView &view, std::shared_ptr< SLE > const &sleRippleState, AccountID const &uLowAccountID, AccountID const &uHighAccountID, beast::Journal j)
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
bool areCompatible(ReadView const &validLedger, ReadView const &testLedger, beast::Journal::Stream &s, const char *reason)
Return false if the test ledger is provably incompatible with the valid ledger, that is,...
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
STAmount multiply(STAmount const &amount, Rate const &rate)
const SF_VECTOR256 sfHashes
TER issueIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
bool native() const noexcept
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
int addBitString(base_uint< Bits, Tag > const &v)
bool dirFirst(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
TER trustCreate(ApplyView &view, const bool bSrcHigh, AccountID const &uSrcAccountID, AccountID const &uDstAccountID, uint256 const &uIndex, SLE::ref sleAccount, const bool bAuth, const bool bNoRipple, const bool bFreeze, STAmount const &saBalance, STAmount const &saLimit, std::uint32_t uQualityIn, std::uint32_t uQualityOut, beast::Journal j)
Create a trust line.
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
virtual Rules const & rules() const =0
Returns the tx processing rules.
NetClock::duration closeTimeResolution
virtual std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const
const SF_AMOUNT sfBalance
bool internalDirFirst(V &view, uint256 const &root, std::shared_ptr< N > &page, unsigned int &index, uint256 &entry)
static std::uint32_t confineOwnerCount(std::uint32_t current, std::int32_t adjustment, std::optional< AccountID > const &id=std::nullopt, beast::Journal j=beast::Journal{beast::Journal::getNullSink()})
const SF_UINT32 sfHighQualityOut
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items in the given directory.
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
const SF_ACCOUNT sfAccount
@ ltRIPPLE_STATE
A ledger object which describes a bidirectional trust line.
int add32(std::uint32_t i)
Information about the notional ledger backing the view.
std::chrono::duration< rep, period > duration
std::uint32_t getFieldU32(SField const &field) const
static bool updateTrustLine(ApplyView &view, SLE::pointer state, bool bSenderHigh, AccountID const &sender, STAmount const &before, STAmount const &after, beast::Journal j)
const SF_UINT32 sfLastLedgerSequence
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
const SF_UINT256 sfAmendment
bool cdirFirst(ReadView const &view, uint256 const &root, std::shared_ptr< SLE const > &page, unsigned int &index, uint256 &entry)
Returns the first entry in the directory, advancing the index.
const std::shared_ptr< STLedgerEntry > & ref
const SField sfMajorities
virtual bool open() const =0
Returns true if this reflects an open ledger.
std::chrono::time_point< NetClock > time_point
void setFieldU32(SField const &field, std::uint32_t)
Currency const & getCurrency() const
const SF_UINT64 sfHighNode
bool isFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
STAmount const & getFieldAmount(SField const &field) const
AccountID const & noAccount()
A placeholder for empty accounts.
std::shared_ptr< STLedgerEntry > pointer
const SF_VECTOR256 sfAmendments
NetClock::time_point parentCloseTime
uint256 getFieldH256(SField const &field) const
Number root(Number f, unsigned d)