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>)
206 if (issuer != account)
231 if (issuer != account)
261 amount.
clear({currency, issuer});
265 isFrozen(view, account, currency, issuer))
272 if (account > issuer)
279 JLOG(j.
trace()) <<
"accountHolds:"
335 if (adjusted < current)
340 <<
"Account " << *
id <<
" owner count exceeds max!";
348 if (adjusted > current)
353 <<
"Account " << *
id <<
" owner count set below 0!";
378 auto const reserve = (sle->getFlags() &
lsfAMM)
382 auto const fullBalance = sle->getFieldAmount(
sfBalance);
387 (balance < reserve) ?
STAmount{0} : balance - reserve;
389 JLOG(j.
trace()) <<
"accountHolds:"
392 <<
" fullBalance=" << fullBalance.getFullText()
393 <<
" balance=" << balance.getFullText()
394 <<
" reserve=" << reserve <<
" ownerCount=" << ownerCount
395 <<
" ownerCountAdj=" << ownerCountAdj;
415 auto sle = view.
read(pos);
418 for (
auto const& key : sle->getFieldV256(
sfIndexes))
441 auto currentIndex =
root;
444 if (
after.isNonZero())
448 if (
auto hintDir = view.
read(hintIndex))
450 for (
auto const& key : hintDir->getFieldV256(
sfIndexes))
455 currentIndex = hintIndex;
464 auto const ownerDir = view.
read(currentIndex);
467 for (
auto const& key : ownerDir->getFieldV256(
sfIndexes))
480 auto const uNodeNext = ownerDir->getFieldU64(
sfIndexNext);
490 auto const ownerDir = view.
read(currentIndex);
493 for (
auto const& key : ownerDir->getFieldV256(
sfIndexes))
496 auto const uNodeNext = ownerDir->getFieldU64(
sfIndexNext);
531 if (hash && (*hash != validLedger.
info().
hash))
533 JLOG(s) << reason <<
" incompatible with valid ledger";
535 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
547 if (hash && (*hash != testLedger.
info().
hash))
549 JLOG(s) << reason <<
" incompatible preceding ledger";
551 JLOG(s) <<
"Hash(NSeq): " <<
to_string(*hash);
561 JLOG(s) << reason <<
" incompatible ledger";
568 JLOG(s) <<
"Val: " << validLedger.
info().
seq <<
" "
571 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
588 if (testLedger.
info().
seq > validIndex)
595 if (hash && (*hash != validHash))
597 JLOG(s) << reason <<
" incompatible following ledger";
598 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
604 (validIndex == testLedger.
info().
seq) &&
605 (testLedger.
info().
hash != validHash))
607 JLOG(s) << reason <<
" incompatible ledger";
614 JLOG(s) <<
"Val: " << validIndex <<
" " <<
to_string(validHash);
616 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
626 auto const sleNode = view.
read(k);
629 if (!sleNode->getFieldV256(
sfIndexes).empty())
647 amendments.insert(v.begin(), v.end());
664 using d = tp::duration;
666 auto const majorities = sle->getFieldArray(
sfMajorities);
668 for (
auto const& m : majorities)
681 if (seq > ledger.
seq())
684 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" future";
687 if (seq == ledger.
seq())
689 if (seq == (ledger.
seq() - 1))
692 if (
int diff = ledger.
seq() - seq; diff <= 256)
702 if (vec.
size() >= diff)
703 return vec[vec.
size() - diff];
705 <<
"Ledger " << ledger.
seq() <<
" missing hash for " << seq
706 <<
" (" << vec.
size() <<
"," << diff <<
")";
711 <<
"Ledger " << ledger.
seq() <<
":" << ledger.
info().
hash
712 <<
" missing normal list";
716 if ((seq & 0xff) != 0)
718 JLOG(journal.
debug())
719 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" past";
728 assert(lastSeq >= seq);
729 assert((lastSeq & 0xff) == 0);
730 auto const diff = (lastSeq - seq) >> 8;
732 if (vec.
size() > diff)
733 return vec[vec.
size() - diff - 1];
735 JLOG(journal.
warn()) <<
"Can't get seq " << seq <<
" from " << ledger.
seq()
781 const bool bNoRipple,
791 JLOG(j.
trace()) <<
"trustCreate: " <<
to_string(uSrcAccountID) <<
", "
795 auto const& uLowAccountID = !bSrcHigh ? uSrcAccountID : uDstAccountID;
796 auto const& uHighAccountID = bSrcHigh ? uSrcAccountID : uDstAccountID;
798 auto const sleRippleState = std::make_shared<SLE>(
ltRIPPLE_STATE, uIndex);
799 view.
insert(sleRippleState);
803 sleRippleState->
key(),
811 sleRippleState->
key(),
817 const bool bSetDst = saLimit.
getIssuer() == uDstAccountID;
818 const bool bSetHigh = bSrcHigh ^ bSetDst;
826 (bSetHigh ? uHighAccountID : uLowAccountID));
833 sleRippleState->setFieldU64(
sfLowNode, *lowNode);
834 sleRippleState->setFieldU64(
sfHighNode, *highNode);
836 sleRippleState->setFieldAmount(
838 sleRippleState->setFieldAmount(
842 bSetDst ? uSrcAccountID : uDstAccountID}));
845 sleRippleState->setFieldU32(
849 sleRippleState->setFieldU32(
873 sleRippleState->setFieldU32(
sfFlags, uFlags);
877 sleRippleState->setFieldAmount(
878 sfBalance, bSetHigh ? -saBalance : saBalance);
881 uSrcAccountID, uDstAccountID, saBalance, saBalance.zeroed());
898 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: low";
903 sleRippleState->
key(),
909 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: high";
914 sleRippleState->
key(),
920 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: state";
921 view.
erase(sleRippleState);
931 auto offerIndex = sle->
key();
979 assert(!bCheckIssuer || uSenderID == issuer || uReceiverID == issuer);
983 assert(uSenderID != uReceiverID);
985 bool const bSenderHigh = uSenderID > uReceiverID;
986 auto const index =
keylet::line(uSenderID, uReceiverID, currency);
992 if (
auto const sleRippleState = view.
peek(index))
999 view.
creditHook(uSenderID, uReceiverID, saAmount, saBalance);
1001 STAmount const saBefore = saBalance;
1003 saBalance -= saAmount;
1005 JLOG(j.
trace()) <<
"rippleCredit: " <<
to_string(uSenderID) <<
" -> "
1012 bool bDelete =
false;
1016 if (saBefore > beast::zero
1018 && saBalance <= beast::zero
1029 !sleRippleState->getFieldAmount(
1032 && !sleRippleState->getFieldU32(
1035 && !sleRippleState->getFieldU32(
1044 sleRippleState->setFieldU32(
1049 bDelete = !saBalance
1058 sleRippleState->setFieldAmount(
sfBalance, saBalance);
1066 bSenderHigh ? uReceiverID : uSenderID,
1067 !bSenderHigh ? uReceiverID : uSenderID,
1071 view.
update(sleRippleState);
1075 STAmount const saReceiverLimit({currency, uReceiverID});
1080 JLOG(j.
debug()) <<
"rippleCredit: "
1121 auto const issuer = saAmount.
getIssuer();
1123 assert(!
isXRP(uSenderID) && !
isXRP(uReceiverID));
1124 assert(uSenderID != uReceiverID);
1126 if (uSenderID == issuer || uReceiverID == issuer || issuer ==
noAccount())
1130 rippleCredit(view, uSenderID, uReceiverID, saAmount,
false, j);
1133 saActual = saAmount;
1145 JLOG(j.
debug()) <<
"rippleSend> " <<
to_string(uSenderID) <<
" - > "
1150 TER terResult =
rippleCredit(view, issuer, uReceiverID, saAmount,
true, j);
1153 terResult =
rippleCredit(view, uSenderID, issuer, saActual,
true, j);
1167 assert(saAmount >= beast::zero);
1172 if (!saAmount || (uSenderID == uReceiverID))
1179 JLOG(j.
trace()) <<
"accountSend: " <<
to_string(uSenderID) <<
" -> "
1184 view, uSenderID, uReceiverID, saAmount, saActual, j, waiveFee);
1201 if (
auto stream = j.
trace())
1212 stream <<
"accountSend> " <<
to_string(uSenderID) <<
" (" << sender_bal
1213 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1247 if (
auto stream = j.
trace())
1258 stream <<
"accountSend< " <<
to_string(uSenderID) <<
" (" << sender_bal
1259 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1285 if (before > beast::zero
1287 &&
after <= beast::zero
1291 &&
static_cast<bool>(
1330 assert(issue == amount.
issue());
1333 assert(issue.
account != account);
1338 bool bSenderHigh = issue.
account > account;
1342 if (
auto state = view.
peek(index))
1349 STAmount const start_balance = final_balance;
1351 final_balance -= amount;
1370 state->setFieldAmount(
sfBalance, final_balance);
1375 bSenderHigh ? account : issue.
account,
1376 bSenderHigh ? issue.
account : account,
1393 if (!receiverAccount)
1426 assert(issue == amount.
issue());
1429 assert(issue.
account != account);
1434 bool bSenderHigh = account > issue.
account;
1444 STAmount const start_balance = final_balance;
1446 final_balance -= amount;
1449 view, state, bSenderHigh, account, start_balance, final_balance, j);
1459 state->setFieldAmount(
sfBalance, final_balance);
1466 bSenderHigh ? issue.
account : account,
1467 bSenderHigh ? account : issue.
account,
1480 <<
" but no trust line exists!";
1493 assert(from != beast::zero);
1494 assert(to != beast::zero);
1500 if (!sender || !receiver)
1535 if (
auto const trustLine =
1537 return ((*trustLine)[
sfFlags] &
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.
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
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.
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account)
Check if the account requires authorization.
@ 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
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.
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)
bool isIndividualFrozen(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer)
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)
static TER rippleSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, STAmount &saActual, beast::Journal j, WaiveTransferFee waiveFee)
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)