20 #include <ripple/basics/Log.h>
21 #include <ripple/basics/StringUtilities.h>
22 #include <ripple/basics/chrono.h>
23 #include <ripple/basics/contract.h>
24 #include <ripple/ledger/BookDirs.h>
25 #include <ripple/ledger/ReadView.h>
26 #include <ripple/ledger/View.h>
27 #include <ripple/protocol/Feature.h>
28 #include <ripple/protocol/Protocol.h>
29 #include <ripple/protocol/Quality.h>
30 #include <ripple/protocol/st.h>
31 #include <boost/algorithm/string.hpp>
84 if (issuer != account)
114 amount.
clear({currency, issuer});
118 isFrozen(view, account, currency, issuer))
125 if (account > issuer)
132 JLOG(j.
trace()) <<
"accountHolds:"
152 JLOG(j.
trace()) <<
"accountFunds:"
166 JLOG(j.
trace()) <<
"accountFunds:"
191 if (adjusted < current)
196 <<
"Account " << *
id <<
" owner count exceeds max!";
204 if (adjusted > current)
209 <<
"Account " << *
id <<
" owner count set below 0!";
235 auto const fullBalance = sle->getFieldAmount(
sfBalance);
239 STAmount amount = balance - reserve;
240 if (balance < reserve)
243 JLOG(j.
trace()) <<
"accountHolds:"
246 <<
" fullBalance=" << fullBalance.getFullText()
247 <<
" balance=" << balance.getFullText()
248 <<
" reserve=" << reserve <<
" ownerCount=" << ownerCount
249 <<
" ownerCountAdj=" << ownerCountAdj;
264 auto sle = view.
read(pos);
268 for (
auto const& key : sle->getFieldV256(
sfIndexes))
287 auto currentIndex = rootIndex;
290 if (
after.isNonZero())
293 auto hintDir = view.
read(hintIndex);
296 for (
auto const& key : hintDir->getFieldV256(
sfIndexes))
301 currentIndex = hintIndex;
310 auto const ownerDir = view.
read(currentIndex);
313 for (
auto const& key : ownerDir->getFieldV256(
sfIndexes))
326 auto const uNodeNext = ownerDir->getFieldU64(
sfIndexNext);
336 auto const ownerDir = view.
read(currentIndex);
339 for (
auto const& key : ownerDir->getFieldV256(
sfIndexes))
342 auto const uNodeNext = ownerDir->getFieldU64(
sfIndexNext);
377 if (hash && (*hash != validLedger.
info().
hash))
379 JLOG(s) << reason <<
" incompatible with valid ledger";
381 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
393 if (hash && (*hash != testLedger.
info().
hash))
395 JLOG(s) << reason <<
" incompatible preceding ledger";
397 JLOG(s) <<
"Hash(NSeq): " <<
to_string(*hash);
407 JLOG(s) << reason <<
" incompatible ledger";
414 JLOG(s) <<
"Val: " << validLedger.
info().
seq <<
" "
417 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
434 if (testLedger.
info().
seq > validIndex)
441 if (hash && (*hash != validHash))
443 JLOG(s) << reason <<
" incompatible following ledger";
444 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
450 (validIndex == testLedger.
info().
seq) &&
451 (testLedger.
info().
hash != validHash))
453 JLOG(s) << reason <<
" incompatible ledger";
460 JLOG(s) <<
"Val: " << validIndex <<
" " <<
to_string(validHash);
462 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
472 auto const sleNode = view.
read(k);
475 if (!sleNode->getFieldV256(
sfIndexes).empty())
488 unsigned int& uDirEntry,
495 return cdirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
503 unsigned int& uDirEntry,
508 assert(uDirEntry <= svIndexes.size());
509 if (uDirEntry >= svIndexes.size())
522 JLOG(j.
fatal()) <<
"Corrupt directory: index:" << uRootIndex
523 <<
" next:" << uNodeNext;
527 return cdirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
529 uEntryIndex = svIndexes[uDirEntry++];
530 JLOG(j.
trace()) <<
"dirNext:"
531 <<
" uDirEntry=" << uDirEntry
532 <<
" uEntryIndex=" << uEntryIndex;
546 amendments.insert(v.begin(), v.end());
563 using d = tp::duration;
565 auto const majorities = sle->getFieldArray(
sfMajorities);
567 for (
auto const& m : majorities)
580 if (seq > ledger.
seq())
583 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" future";
586 if (seq == ledger.
seq())
588 if (seq == (ledger.
seq() - 1))
604 <<
"Ledger " << ledger.
seq() <<
" missing hash for " << seq
605 <<
" (" << vec.
size() <<
"," <<
diff <<
")";
610 <<
"Ledger " << ledger.
seq() <<
":" << ledger.
info().
hash
611 <<
" missing normal list";
615 if ((seq & 0xff) != 0)
617 JLOG(journal.
debug())
618 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" past";
627 assert(lastSeq >= seq);
628 assert((lastSeq & 0xff) == 0);
629 auto const diff = (lastSeq - seq) >> 8;
634 JLOG(journal.
warn()) <<
"Can't get seq " << seq <<
" from " << ledger.
seq()
668 unsigned int& uDirEntry,
675 return dirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
683 unsigned int& uDirEntry,
688 assert(uDirEntry <= svIndexes.size());
689 if (uDirEntry >= svIndexes.size())
702 JLOG(j.
fatal()) <<
"Corrupt directory: index:" << uRootIndex
703 <<
" next:" << uNodeNext;
707 return dirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
709 uEntryIndex = svIndexes[uDirEntry++];
710 JLOG(j.
trace()) <<
"dirNext:"
711 <<
" uDirEntry=" << uDirEntry
712 <<
" uEntryIndex=" << uEntryIndex;
733 const bool bNoRipple,
743 JLOG(j.
trace()) <<
"trustCreate: " <<
to_string(uSrcAccountID) <<
", "
747 auto const& uLowAccountID = !bSrcHigh ? uSrcAccountID : uDstAccountID;
748 auto const& uHighAccountID = bSrcHigh ? uSrcAccountID : uDstAccountID;
750 auto const sleRippleState = std::make_shared<SLE>(
ltRIPPLE_STATE, uIndex);
751 view.
insert(sleRippleState);
755 sleRippleState->
key(),
763 sleRippleState->
key(),
769 const bool bSetDst = saLimit.
getIssuer() == uDstAccountID;
770 const bool bSetHigh = bSrcHigh ^ bSetDst;
778 (bSetHigh ? uHighAccountID : uLowAccountID));
785 sleRippleState->setFieldU64(
sfLowNode, *lowNode);
786 sleRippleState->setFieldU64(
sfHighNode, *highNode);
788 sleRippleState->setFieldAmount(
790 sleRippleState->setFieldAmount(
794 bSetDst ? uSrcAccountID : uDstAccountID}));
797 sleRippleState->setFieldU32(
801 sleRippleState->setFieldU32(
825 sleRippleState->setFieldU32(
sfFlags, uFlags);
829 sleRippleState->setFieldAmount(
830 sfBalance, bSetHigh ? -saBalance : saBalance);
833 uSrcAccountID, uDstAccountID, saBalance, saBalance.zeroed());
850 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: low";
855 sleRippleState->
key(),
861 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: high";
866 sleRippleState->
key(),
872 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: state";
873 view.
erase(sleRippleState);
883 auto offerIndex = sle->
key();
931 assert(!bCheckIssuer || uSenderID == issuer || uReceiverID == issuer);
935 assert(uSenderID != uReceiverID);
937 bool const bSenderHigh = uSenderID > uReceiverID;
938 auto const index =
keylet::line(uSenderID, uReceiverID, currency);
944 if (
auto const sleRippleState = view.
peek(index))
951 view.
creditHook(uSenderID, uReceiverID, saAmount, saBalance);
953 STAmount const saBefore = saBalance;
955 saBalance -= saAmount;
957 JLOG(j.
trace()) <<
"rippleCredit: " <<
to_string(uSenderID) <<
" -> "
964 bool bDelete =
false;
968 if (saBefore > beast::zero
970 && saBalance <= beast::zero
981 !sleRippleState->getFieldAmount(
984 && !sleRippleState->getFieldU32(
987 && !sleRippleState->getFieldU32(
996 sleRippleState->setFieldU32(
1001 bDelete = !saBalance
1010 sleRippleState->setFieldAmount(
sfBalance, saBalance);
1018 bSenderHigh ? uReceiverID : uSenderID,
1019 !bSenderHigh ? uReceiverID : uSenderID,
1023 view.
update(sleRippleState);
1027 STAmount const saReceiverLimit({currency, uReceiverID});
1032 JLOG(j.
debug()) <<
"rippleCredit: "
1072 auto const issuer = saAmount.
getIssuer();
1074 assert(!
isXRP(uSenderID) && !
isXRP(uReceiverID));
1075 assert(uSenderID != uReceiverID);
1077 if (uSenderID == issuer || uReceiverID == issuer || issuer ==
noAccount())
1081 rippleCredit(view, uSenderID, uReceiverID, saAmount,
false, j);
1084 saActual = saAmount;
1094 JLOG(j.
debug()) <<
"rippleSend> " <<
to_string(uSenderID) <<
" - > "
1099 TER terResult =
rippleCredit(view, issuer, uReceiverID, saAmount,
true, j);
1102 terResult =
rippleCredit(view, uSenderID, issuer, saActual,
true, j);
1115 assert(saAmount >= beast::zero);
1120 if (!saAmount || (uSenderID == uReceiverID))
1127 JLOG(j.
trace()) <<
"accountSend: " <<
to_string(uSenderID) <<
" -> "
1131 return rippleSend(view, uSenderID, uReceiverID, saAmount, saActual, j);
1148 if (
auto stream = j.
trace())
1159 stream <<
"accountSend> " <<
to_string(uSenderID) <<
" (" << sender_bal
1160 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1194 if (
auto stream = j.
trace())
1205 stream <<
"accountSend< " <<
to_string(uSenderID) <<
" (" << sender_bal
1206 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1232 if (before > beast::zero
1234 &&
after <= beast::zero
1238 &&
static_cast<bool>(
1277 assert(issue == amount.
issue());
1280 assert(issue.
account != account);
1285 bool bSenderHigh = issue.
account > account;
1289 if (
auto state = view.
peek(index))
1296 STAmount const start_balance = final_balance;
1298 final_balance -= amount;
1317 state->setFieldAmount(
sfBalance, final_balance);
1322 bSenderHigh ? account : issue.
account,
1323 bSenderHigh ? issue.
account : account,
1340 if (!receiverAccount)
1373 assert(issue == amount.
issue());
1376 assert(issue.
account != account);
1381 bool bSenderHigh = account > issue.
account;
1391 STAmount const start_balance = final_balance;
1393 final_balance -= amount;
1396 view, state, bSenderHigh, account, start_balance, final_balance, j);
1406 state->setFieldAmount(
sfBalance, final_balance);
1413 bSenderHigh ? issue.
account : account,
1414 bSenderHigh ? account : issue.
account,
1427 <<
" but no trust line exists!";
1440 assert(from != beast::zero);
1441 assert(to != beast::zero);
1447 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)
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.
Keylet const & amendments() noexcept
The index of the amendment table.
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.
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
std::uint64_t getFieldU64(SField const &field) const
const STVector256 & getFieldV256(SField const &field) const
const SF_HASH256 sfBookDirectory
constexpr value_type drops() const
Returns the number of drops.
void addRaw(LedgerInfo const &info, Serializer &s, bool includeHash)
bool cdirNext(ReadView const &view, uint256 const &uRootIndex, std::shared_ptr< SLE const > &sleNode, unsigned int &uDirEntry, uint256 &uEntryIndex, beast::Journal j)
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
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.
virtual STAmount balanceHook(AccountID const &account, AccountID const &issuer, STAmount const &amount) const
const SF_UINT32 sfCloseTime
const SF_HASH256 sfAmendment
void setIssuer(AccountID const &uIssuer)
bool dirNext(ApplyView &view, uint256 const &uRootIndex, std::shared_ptr< SLE > &sleNode, unsigned int &uDirEntry, uint256 &uEntryIndex, beast::Journal j)
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.
const uint256 featureDeletableAccounts
const SF_VECTOR256 sfIndexes
void forEachItem(ReadView const &view, AccountID const &id, std::function< void(std::shared_ptr< SLE const > const &)> f)
Iterate all items in an account's owner directory.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
bool dirFirst(ApplyView &view, uint256 const &uRootIndex, std::shared_ptr< SLE > &sleNode, unsigned int &uDirEntry, uint256 &uEntryIndex, beast::Journal j)
NetClock::time_point closeTime
uint256 const & key() const
Returns the 'key' (or 'index') of this item.
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
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
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)
Currency const & getCurrency() const
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
AccountID const & getIssuer() const
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)
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.
Issue const & issue() const
NetClock::duration closeTimeResolution
virtual std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const
const SF_AMOUNT sfBalance
bool forEachItemAfter(ReadView const &view, AccountID const &id, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(std::shared_ptr< SLE const > const &)> f)
Iterate all items after an item in an owner directory.
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
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
int add32(std::uint32_t i)
Information about the notional ledger backing the view.
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 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)
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
bool cdirFirst(ReadView const &view, uint256 const &uRootIndex, std::shared_ptr< SLE const > &sleNode, unsigned int &uDirEntry, uint256 &uEntryIndex, beast::Journal j)