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;
734 return view.
dirAppend(dir, uLedgerIndex, fDescriber);
736 return view.
dirInsert(dir, uLedgerIndex, fDescriber);
748 const bool bNoRipple,
758 JLOG(j.
trace()) <<
"trustCreate: " <<
to_string(uSrcAccountID) <<
", "
762 auto const& uLowAccountID = !bSrcHigh ? uSrcAccountID : uDstAccountID;
763 auto const& uHighAccountID = bSrcHigh ? uSrcAccountID : uDstAccountID;
765 auto const sleRippleState = std::make_shared<SLE>(
ltRIPPLE_STATE, uIndex);
766 view.
insert(sleRippleState);
771 sleRippleState->
key(),
782 sleRippleState->
key(),
790 const bool bSetDst = saLimit.
getIssuer() == uDstAccountID;
791 const bool bSetHigh = bSrcHigh ^ bSetDst;
799 (bSetHigh ? uHighAccountID : uLowAccountID));
806 sleRippleState->setFieldU64(
sfLowNode, *lowNode);
807 sleRippleState->setFieldU64(
sfHighNode, *highNode);
809 sleRippleState->setFieldAmount(
811 sleRippleState->setFieldAmount(
815 bSetDst ? uSrcAccountID : uDstAccountID}));
818 sleRippleState->setFieldU32(
822 sleRippleState->setFieldU32(
846 sleRippleState->setFieldU32(
sfFlags, uFlags);
850 sleRippleState->setFieldAmount(
851 sfBalance, bSetHigh ? -saBalance : saBalance);
854 uSrcAccountID, uDstAccountID, saBalance, saBalance.zeroed());
871 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: low";
876 sleRippleState->
key(),
882 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: high";
887 sleRippleState->
key(),
893 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: state";
894 view.
erase(sleRippleState);
904 auto offerIndex = sle->
key();
952 assert(!bCheckIssuer || uSenderID == issuer || uReceiverID == issuer);
956 assert(uSenderID != uReceiverID);
958 bool const bSenderHigh = uSenderID > uReceiverID;
959 auto const index =
keylet::line(uSenderID, uReceiverID, currency);
965 if (
auto const sleRippleState = view.
peek(index))
972 view.
creditHook(uSenderID, uReceiverID, saAmount, saBalance);
974 STAmount const saBefore = saBalance;
976 saBalance -= saAmount;
978 JLOG(j.
trace()) <<
"rippleCredit: " <<
to_string(uSenderID) <<
" -> "
985 bool bDelete =
false;
989 if (saBefore > beast::zero
991 && saBalance <= beast::zero
1002 !sleRippleState->getFieldAmount(
1005 && !sleRippleState->getFieldU32(
1008 && !sleRippleState->getFieldU32(
1017 sleRippleState->setFieldU32(
1022 bDelete = !saBalance
1031 sleRippleState->setFieldAmount(
sfBalance, saBalance);
1039 bSenderHigh ? uReceiverID : uSenderID,
1040 !bSenderHigh ? uReceiverID : uSenderID,
1044 view.
update(sleRippleState);
1048 STAmount const saReceiverLimit({currency, uReceiverID});
1053 JLOG(j.
debug()) <<
"rippleCredit: "
1093 auto const issuer = saAmount.
getIssuer();
1095 assert(!
isXRP(uSenderID) && !
isXRP(uReceiverID));
1096 assert(uSenderID != uReceiverID);
1098 if (uSenderID == issuer || uReceiverID == issuer || issuer ==
noAccount())
1102 rippleCredit(view, uSenderID, uReceiverID, saAmount,
false, j);
1105 saActual = saAmount;
1115 JLOG(j.
debug()) <<
"rippleSend> " <<
to_string(uSenderID) <<
" - > "
1120 TER terResult =
rippleCredit(view, issuer, uReceiverID, saAmount,
true, j);
1123 terResult =
rippleCredit(view, uSenderID, issuer, saActual,
true, j);
1136 assert(saAmount >= beast::zero);
1141 if (!saAmount || (uSenderID == uReceiverID))
1148 JLOG(j.
trace()) <<
"accountSend: " <<
to_string(uSenderID) <<
" -> "
1152 return rippleSend(view, uSenderID, uReceiverID, saAmount, saActual, j);
1169 if (
auto stream = j.
trace())
1180 stream <<
"accountSend> " <<
to_string(uSenderID) <<
" (" << sender_bal
1181 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1215 if (
auto stream = j.
trace())
1226 stream <<
"accountSend< " <<
to_string(uSenderID) <<
" (" << sender_bal
1227 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1253 if (before > beast::zero
1255 &&
after <= beast::zero
1259 &&
static_cast<bool>(
1298 assert(issue == amount.
issue());
1301 assert(issue.
account != account);
1306 bool bSenderHigh = issue.
account > account;
1310 if (
auto state = view.
peek(index))
1317 STAmount const start_balance = final_balance;
1319 final_balance -= amount;
1338 state->setFieldAmount(
sfBalance, final_balance);
1343 bSenderHigh ? account : issue.
account,
1344 bSenderHigh ? issue.
account : account,
1361 if (!receiverAccount)
1394 assert(issue == amount.
issue());
1397 assert(issue.
account != account);
1402 bool bSenderHigh = account > issue.
account;
1412 STAmount const start_balance = final_balance;
1414 final_balance -= amount;
1417 view, state, bSenderHigh, account, start_balance, final_balance, j);
1427 state->setFieldAmount(
sfBalance, final_balance);
1434 bSenderHigh ? issue.
account : account,
1435 bSenderHigh ? account : issue.
account,
1448 <<
" but no trust line exists!";
1461 assert(from != beast::zero);
1462 assert(to != beast::zero);
1468 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)
std::optional< std::uint64_t > dirAppend(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Append an entry to a directory.
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)
std::optional< std::uint64_t > dirAdd(ApplyView &view, Keylet const &dir, uint256 const &uLedgerIndex, bool strictOrder, std::function< void(SLE::ref)> fDescriber, beast::Journal j)
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)