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>
83 if (issuer != account)
113 amount.
clear({currency, issuer});
117 isFrozen(view, account, currency, issuer))
124 if (account > issuer)
131 JLOG(j.
trace()) <<
"accountHolds:"
151 JLOG(j.
trace()) <<
"accountFunds:"
165 JLOG(j.
trace()) <<
"accountFunds:"
183 boost::optional<AccountID>
const&
id = boost::none,
190 if (adjusted < current)
195 <<
"Account " << *
id <<
" owner count exceeds max!";
203 if (adjusted > current)
208 <<
"Account " << *
id <<
" owner count set below 0!";
234 auto const fullBalance = sle->getFieldAmount(
sfBalance);
238 STAmount amount = balance - reserve;
239 if (balance < reserve)
242 JLOG(j.
trace()) <<
"accountHolds:"
245 <<
" fullBalance=" << fullBalance.getFullText()
246 <<
" balance=" << balance.getFullText()
247 <<
" reserve=" << reserve <<
" ownerCount=" << ownerCount
248 <<
" ownerCountAdj=" << ownerCountAdj;
263 auto sle = view.
read(pos);
267 for (
auto const& key : sle->getFieldV256(
sfIndexes))
286 auto currentIndex = rootIndex;
289 if (
after.isNonZero())
292 auto hintDir = view.
read(hintIndex);
295 for (
auto const& key : hintDir->getFieldV256(
sfIndexes))
300 currentIndex = hintIndex;
309 auto const ownerDir = view.
read(currentIndex);
312 for (
auto const& key : ownerDir->getFieldV256(
sfIndexes))
325 auto const uNodeNext = ownerDir->getFieldU64(
sfIndexNext);
335 auto const ownerDir = view.
read(currentIndex);
338 for (
auto const& key : ownerDir->getFieldV256(
sfIndexes))
341 auto const uNodeNext = ownerDir->getFieldU64(
sfIndexNext);
376 if (hash && (*hash != validLedger.
info().
hash))
378 JLOG(s) << reason <<
" incompatible with valid ledger";
380 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
392 if (hash && (*hash != testLedger.
info().
hash))
394 JLOG(s) << reason <<
" incompatible preceding ledger";
396 JLOG(s) <<
"Hash(NSeq): " <<
to_string(*hash);
406 JLOG(s) << reason <<
" incompatible ledger";
413 JLOG(s) <<
"Val: " << validLedger.
info().
seq <<
" "
416 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
433 if (testLedger.
info().
seq > validIndex)
440 if (hash && (*hash != validHash))
442 JLOG(s) << reason <<
" incompatible following ledger";
443 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
449 (validIndex == testLedger.
info().
seq) &&
450 (testLedger.
info().
hash != validHash))
452 JLOG(s) << reason <<
" incompatible ledger";
459 JLOG(s) <<
"Val: " << validIndex <<
" " <<
to_string(validHash);
461 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
471 auto const sleNode = view.
read(k);
474 if (!sleNode->getFieldV256(
sfIndexes).empty())
487 unsigned int& uDirEntry,
494 return cdirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
502 unsigned int& uDirEntry,
507 assert(uDirEntry <= svIndexes.size());
508 if (uDirEntry >= svIndexes.size())
521 JLOG(j.
fatal()) <<
"Corrupt directory: index:" << uRootIndex
522 <<
" next:" << uNodeNext;
526 return cdirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
528 uEntryIndex = svIndexes[uDirEntry++];
529 JLOG(j.
trace()) <<
"dirNext:"
530 <<
" uDirEntry=" << uDirEntry
531 <<
" uEntryIndex=" << uEntryIndex;
545 amendments.insert(v.begin(), v.end());
562 using d = tp::duration;
564 auto const majorities = sle->getFieldArray(
sfMajorities);
566 for (
auto const& m : majorities)
575 boost::optional<uint256>
579 if (seq > ledger.
seq())
582 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" future";
585 if (seq == ledger.
seq())
587 if (seq == (ledger.
seq() - 1))
603 <<
"Ledger " << ledger.
seq() <<
" missing hash for " << seq
604 <<
" (" << vec.
size() <<
"," <<
diff <<
")";
609 <<
"Ledger " << ledger.
seq() <<
":" << ledger.
info().
hash
610 <<
" missing normal list";
614 if ((seq & 0xff) != 0)
616 JLOG(journal.
debug())
617 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" past";
626 assert(lastSeq >= seq);
627 assert((lastSeq & 0xff) == 0);
628 auto const diff = (lastSeq - seq) >> 8;
633 JLOG(journal.
warn()) <<
"Can't get seq " << seq <<
" from " << ledger.
seq()
667 unsigned int& uDirEntry,
674 return dirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
682 unsigned int& uDirEntry,
687 assert(uDirEntry <= svIndexes.size());
688 if (uDirEntry >= svIndexes.size())
701 JLOG(j.
fatal()) <<
"Corrupt directory: index:" << uRootIndex
702 <<
" next:" << uNodeNext;
706 return dirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
708 uEntryIndex = svIndexes[uDirEntry++];
709 JLOG(j.
trace()) <<
"dirNext:"
710 <<
" uDirEntry=" << uDirEntry
711 <<
" uEntryIndex=" << uEntryIndex;
723 boost::optional<std::uint64_t>
733 return view.
dirAppend(dir, uLedgerIndex, fDescriber);
735 return view.
dirInsert(dir, uLedgerIndex, fDescriber);
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);
770 sleRippleState->
key(),
781 sleRippleState->
key(),
789 const bool bSetDst = saLimit.
getIssuer() == uDstAccountID;
790 const bool bSetHigh = bSrcHigh ^ bSetDst;
798 (bSetHigh ? uHighAccountID : uLowAccountID));
805 sleRippleState->setFieldU64(
sfLowNode, *lowNode);
806 sleRippleState->setFieldU64(
sfHighNode, *highNode);
808 sleRippleState->setFieldAmount(
810 sleRippleState->setFieldAmount(
814 bSetDst ? uSrcAccountID : uDstAccountID}));
817 sleRippleState->setFieldU32(
821 sleRippleState->setFieldU32(
845 sleRippleState->setFieldU32(
sfFlags, uFlags);
849 sleRippleState->setFieldAmount(
850 sfBalance, bSetHigh ? -saBalance : saBalance);
853 uSrcAccountID, uDstAccountID, saBalance, saBalance.zeroed());
870 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: low";
875 sleRippleState->
key(),
881 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: high";
886 sleRippleState->
key(),
892 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: state";
893 view.
erase(sleRippleState);
903 auto offerIndex = sle->
key();
951 assert(!bCheckIssuer || uSenderID == issuer || uReceiverID == issuer);
955 assert(uSenderID != uReceiverID);
957 bool const bSenderHigh = uSenderID > uReceiverID;
958 auto const index =
keylet::line(uSenderID, uReceiverID, currency);
964 if (
auto const sleRippleState = view.
peek(index))
971 view.
creditHook(uSenderID, uReceiverID, saAmount, saBalance);
973 STAmount const saBefore = saBalance;
975 saBalance -= saAmount;
977 JLOG(j.
trace()) <<
"rippleCredit: " <<
to_string(uSenderID) <<
" -> "
984 bool bDelete =
false;
988 if (saBefore > beast::zero
990 && saBalance <= beast::zero
1001 !sleRippleState->getFieldAmount(
1004 && !sleRippleState->getFieldU32(
1007 && !sleRippleState->getFieldU32(
1016 sleRippleState->setFieldU32(
1021 bDelete = !saBalance
1030 sleRippleState->setFieldAmount(
sfBalance, saBalance);
1038 bSenderHigh ? uReceiverID : uSenderID,
1039 !bSenderHigh ? uReceiverID : uSenderID,
1043 view.
update(sleRippleState);
1047 STAmount const saReceiverLimit({currency, uReceiverID});
1052 JLOG(j.
debug()) <<
"rippleCredit: "
1092 auto const issuer = saAmount.
getIssuer();
1094 assert(!
isXRP(uSenderID) && !
isXRP(uReceiverID));
1095 assert(uSenderID != uReceiverID);
1097 if (uSenderID == issuer || uReceiverID == issuer || issuer ==
noAccount())
1101 rippleCredit(view, uSenderID, uReceiverID, saAmount,
false, j);
1104 saActual = saAmount;
1114 JLOG(j.
debug()) <<
"rippleSend> " <<
to_string(uSenderID) <<
" - > "
1119 TER terResult =
rippleCredit(view, issuer, uReceiverID, saAmount,
true, j);
1122 terResult =
rippleCredit(view, uSenderID, issuer, saActual,
true, j);
1135 assert(saAmount >= beast::zero);
1140 if (!saAmount || (uSenderID == uReceiverID))
1147 JLOG(j.
trace()) <<
"accountSend: " <<
to_string(uSenderID) <<
" -> "
1151 return rippleSend(view, uSenderID, uReceiverID, saAmount, saActual, j);
1168 if (
auto stream = j.
trace())
1179 stream <<
"accountSend> " <<
to_string(uSenderID) <<
" (" << sender_bal
1180 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1214 if (
auto stream = j.
trace())
1225 stream <<
"accountSend< " <<
to_string(uSenderID) <<
" (" << sender_bal
1226 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1252 if (before > beast::zero
1254 &&
after <= beast::zero
1258 &&
static_cast<bool>(
1297 assert(issue == amount.
issue());
1300 assert(issue.
account != account);
1305 bool bSenderHigh = issue.
account > account;
1309 if (
auto state = view.
peek(index))
1316 STAmount const start_balance = final_balance;
1318 final_balance -= amount;
1337 state->setFieldAmount(
sfBalance, final_balance);
1342 bSenderHigh ? account : issue.
account,
1343 bSenderHigh ? issue.
account : account,
1360 if (!receiverAccount)
1393 assert(issue == amount.
issue());
1396 assert(issue.
account != account);
1401 bool bSenderHigh = account > issue.
account;
1411 STAmount const start_balance = final_balance;
1413 final_balance -= amount;
1416 view, state, bSenderHigh, account, start_balance, final_balance, j);
1426 state->setFieldAmount(
sfBalance, final_balance);
1433 bSenderHigh ? issue.
account : account,
1434 bSenderHigh ? account : issue.
account,
1447 <<
" but no trust line exists!";
1460 assert(from != beast::zero);
1461 assert(to != beast::zero);
1467 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.
boost::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.
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.
boost::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.
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
std::string to_string(ListDisposition disposition)
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)
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
boost::optional< std::uint64_t > dirAdd(ApplyView &view, Keylet const &dir, uint256 const &uLedgerIndex, bool strictOrder, std::function< void(SLE::ref)> fDescriber, beast::Journal j)
STAmount multiply(STAmount const &amount, Rate const &rate)
boost::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
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.
const SF_UINT32 sfHighQualityOut
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
const SF_ACCOUNT sfAccount
int add32(std::uint32_t i)
Information about the notional ledger backing the view.
static std::uint32_t confineOwnerCount(std::uint32_t current, std::int32_t adjustment, boost::optional< AccountID > const &id=boost::none, beast::Journal j=beast::Journal{beast::Journal::getNullSink()})
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
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)