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>
80 if (issuer != account)
110 amount.
clear({currency, issuer});
114 isFrozen(view, account, currency, issuer))
121 if (account > issuer)
128 JLOG(j.
trace()) <<
"accountHolds:"
148 JLOG(j.
trace()) <<
"accountFunds:"
162 JLOG(j.
trace()) <<
"accountFunds:"
180 boost::optional<AccountID>
const&
id = boost::none,
187 if (adjusted < current)
192 <<
"Account " << *
id <<
" owner count exceeds max!";
200 if (adjusted > current)
205 <<
"Account " << *
id <<
" owner count set below 0!";
231 auto const fullBalance = sle->getFieldAmount(
sfBalance);
235 STAmount amount = balance - reserve;
236 if (balance < reserve)
239 JLOG(j.
trace()) <<
"accountHolds:"
242 <<
" fullBalance=" << fullBalance.getFullText()
243 <<
" balance=" << balance.getFullText()
244 <<
" reserve=" << reserve <<
" ownerCount=" << ownerCount
245 <<
" ownerCountAdj=" << ownerCountAdj;
260 auto sle = view.
read(pos);
264 for (
auto const& key : sle->getFieldV256(
sfIndexes))
283 auto currentIndex = rootIndex;
286 if (
after.isNonZero())
289 auto hintDir = view.
read(hintIndex);
292 for (
auto const& key : hintDir->getFieldV256(
sfIndexes))
297 currentIndex = hintIndex;
306 auto const ownerDir = view.
read(currentIndex);
309 for (
auto const& key : ownerDir->getFieldV256(
sfIndexes))
322 auto const uNodeNext = ownerDir->getFieldU64(
sfIndexNext);
332 auto const ownerDir = view.
read(currentIndex);
335 for (
auto const& key : ownerDir->getFieldV256(
sfIndexes))
338 auto const uNodeNext = ownerDir->getFieldU64(
sfIndexNext);
373 if (hash && (*hash != validLedger.
info().
hash))
375 JLOG(s) << reason <<
" incompatible with valid ledger";
377 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
389 if (hash && (*hash != testLedger.
info().
hash))
391 JLOG(s) << reason <<
" incompatible preceding ledger";
393 JLOG(s) <<
"Hash(NSeq): " <<
to_string(*hash);
403 JLOG(s) << reason <<
" incompatible ledger";
410 JLOG(s) <<
"Val: " << validLedger.
info().
seq <<
" "
413 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
430 if (testLedger.
info().
seq > validIndex)
437 if (hash && (*hash != validHash))
439 JLOG(s) << reason <<
" incompatible following ledger";
440 JLOG(s) <<
"Hash(VSeq): " <<
to_string(*hash);
446 (validIndex == testLedger.
info().
seq) &&
447 (testLedger.
info().
hash != validHash))
449 JLOG(s) << reason <<
" incompatible ledger";
456 JLOG(s) <<
"Val: " << validIndex <<
" " <<
to_string(validHash);
458 JLOG(s) <<
"New: " << testLedger.
info().
seq <<
" "
468 auto const sleNode = view.
read(k);
471 if (!sleNode->getFieldV256(
sfIndexes).empty())
484 unsigned int& uDirEntry,
491 return cdirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
499 unsigned int& uDirEntry,
504 assert(uDirEntry <= svIndexes.size());
505 if (uDirEntry >= svIndexes.size())
518 JLOG(j.
fatal()) <<
"Corrupt directory: index:" << uRootIndex
519 <<
" next:" << uNodeNext;
523 return cdirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
525 uEntryIndex = svIndexes[uDirEntry++];
526 JLOG(j.
trace()) <<
"dirNext:"
527 <<
" uDirEntry=" << uDirEntry
528 <<
" uEntryIndex=" << uEntryIndex;
542 amendments.insert(v.begin(), v.end());
559 using d = tp::duration;
561 auto const majorities = sle->getFieldArray(
sfMajorities);
563 for (
auto const& m : majorities)
572 boost::optional<uint256>
576 if (seq > ledger.
seq())
579 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" future";
582 if (seq == ledger.
seq())
584 if (seq == (ledger.
seq() - 1))
600 <<
"Ledger " << ledger.
seq() <<
" missing hash for " << seq
601 <<
" (" << vec.
size() <<
"," <<
diff <<
")";
606 <<
"Ledger " << ledger.
seq() <<
":" << ledger.
info().
hash
607 <<
" missing normal list";
611 if ((seq & 0xff) != 0)
613 JLOG(journal.
debug())
614 <<
"Can't get seq " << seq <<
" from " << ledger.
seq() <<
" past";
623 assert(lastSeq >= seq);
624 assert((lastSeq & 0xff) == 0);
625 auto const diff = (lastSeq - seq) >> 8;
630 JLOG(journal.
warn()) <<
"Can't get seq " << seq <<
" from " << ledger.
seq()
664 unsigned int& uDirEntry,
671 return dirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
679 unsigned int& uDirEntry,
684 assert(uDirEntry <= svIndexes.size());
685 if (uDirEntry >= svIndexes.size())
698 JLOG(j.
fatal()) <<
"Corrupt directory: index:" << uRootIndex
699 <<
" next:" << uNodeNext;
703 return dirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j);
705 uEntryIndex = svIndexes[uDirEntry++];
706 JLOG(j.
trace()) <<
"dirNext:"
707 <<
" uDirEntry=" << uDirEntry
708 <<
" uEntryIndex=" << uEntryIndex;
720 boost::optional<std::uint64_t>
730 return view.
dirAppend(dir, uLedgerIndex, fDescriber);
732 return view.
dirInsert(dir, uLedgerIndex, fDescriber);
744 const bool bNoRipple,
754 JLOG(j.
trace()) <<
"trustCreate: " <<
to_string(uSrcAccountID) <<
", "
758 auto const& uLowAccountID = !bSrcHigh ? uSrcAccountID : uDstAccountID;
759 auto const& uHighAccountID = bSrcHigh ? uSrcAccountID : uDstAccountID;
761 auto const sleRippleState = std::make_shared<SLE>(
ltRIPPLE_STATE, uIndex);
762 view.
insert(sleRippleState);
767 sleRippleState->
key(),
778 sleRippleState->
key(),
786 const bool bSetDst = saLimit.
getIssuer() == uDstAccountID;
787 const bool bSetHigh = bSrcHigh ^ bSetDst;
795 (bSetHigh ? uHighAccountID : uLowAccountID));
802 sleRippleState->setFieldU64(
sfLowNode, *lowNode);
803 sleRippleState->setFieldU64(
sfHighNode, *highNode);
805 sleRippleState->setFieldAmount(
807 sleRippleState->setFieldAmount(
811 bSetDst ? uSrcAccountID : uDstAccountID}));
814 sleRippleState->setFieldU32(
818 sleRippleState->setFieldU32(
842 sleRippleState->setFieldU32(
sfFlags, uFlags);
846 sleRippleState->setFieldAmount(
847 sfBalance, bSetHigh ? -saBalance : saBalance);
850 uSrcAccountID, uDstAccountID, saBalance, saBalance.zeroed());
867 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: low";
872 sleRippleState->
key(),
878 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: high";
883 sleRippleState->
key(),
889 JLOG(j.
trace()) <<
"trustDelete: Deleting ripple line: state";
890 view.
erase(sleRippleState);
900 auto offerIndex = sle->
key();
948 assert(!bCheckIssuer || uSenderID == issuer || uReceiverID == issuer);
952 assert(uSenderID != uReceiverID);
954 bool const bSenderHigh = uSenderID > uReceiverID;
955 auto const index =
keylet::line(uSenderID, uReceiverID, currency);
961 if (
auto const sleRippleState = view.
peek(index))
968 view.
creditHook(uSenderID, uReceiverID, saAmount, saBalance);
970 STAmount const saBefore = saBalance;
972 saBalance -= saAmount;
974 JLOG(j.
trace()) <<
"rippleCredit: " <<
to_string(uSenderID) <<
" -> "
981 bool bDelete =
false;
985 if (saBefore > beast::zero
987 && saBalance <= beast::zero
998 !sleRippleState->getFieldAmount(
1001 && !sleRippleState->getFieldU32(
1004 && !sleRippleState->getFieldU32(
1013 sleRippleState->setFieldU32(
1018 bDelete = !saBalance
1027 sleRippleState->setFieldAmount(
sfBalance, saBalance);
1035 bSenderHigh ? uReceiverID : uSenderID,
1036 !bSenderHigh ? uReceiverID : uSenderID,
1040 view.
update(sleRippleState);
1044 STAmount const saReceiverLimit({currency, uReceiverID});
1049 JLOG(j.
debug()) <<
"rippleCredit: "
1089 auto const issuer = saAmount.
getIssuer();
1091 assert(!
isXRP(uSenderID) && !
isXRP(uReceiverID));
1092 assert(uSenderID != uReceiverID);
1094 if (uSenderID == issuer || uReceiverID == issuer || issuer ==
noAccount())
1098 rippleCredit(view, uSenderID, uReceiverID, saAmount,
false, j);
1101 saActual = saAmount;
1111 JLOG(j.
debug()) <<
"rippleSend> " <<
to_string(uSenderID) <<
" - > "
1116 TER terResult =
rippleCredit(view, issuer, uReceiverID, saAmount,
true, j);
1119 terResult =
rippleCredit(view, uSenderID, issuer, saActual,
true, j);
1132 assert(saAmount >= beast::zero);
1137 if (!saAmount || (uSenderID == uReceiverID))
1144 JLOG(j.
trace()) <<
"accountSend: " <<
to_string(uSenderID) <<
" -> "
1148 return rippleSend(view, uSenderID, uReceiverID, saAmount, saActual, j);
1165 if (
auto stream = j.
trace())
1176 stream <<
"accountSend> " <<
to_string(uSenderID) <<
" (" << sender_bal
1177 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1211 if (
auto stream = j.
trace())
1222 stream <<
"accountSend< " <<
to_string(uSenderID) <<
" (" << sender_bal
1223 <<
") -> " <<
to_string(uReceiverID) <<
" (" << receiver_bal
1249 if (before > beast::zero
1251 &&
after <= beast::zero
1255 &&
static_cast<bool>(
1294 assert(issue == amount.
issue());
1297 assert(issue.
account != account);
1302 bool bSenderHigh = issue.
account > account;
1306 if (
auto state = view.
peek(index))
1313 STAmount const start_balance = final_balance;
1315 final_balance -= amount;
1334 state->setFieldAmount(
sfBalance, final_balance);
1339 bSenderHigh ? account : issue.
account,
1340 bSenderHigh ? issue.
account : account,
1357 if (!receiverAccount)
1390 assert(issue == amount.
issue());
1393 assert(issue.
account != account);
1398 bool bSenderHigh = account > issue.
account;
1408 STAmount const start_balance = final_balance;
1410 final_balance -= amount;
1413 view, state, bSenderHigh, account, start_balance, final_balance, j);
1423 state->setFieldAmount(
sfBalance, final_balance);
1430 bSenderHigh ? issue.
account : account,
1431 bSenderHigh ? account : issue.
account,
1444 <<
" but no trust line exists!";
1457 assert(from != beast::zero);
1458 assert(to != beast::zero);
1464 if (!sender || !receiver)
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
const SF_U32 sfHighQualityIn(access, STI_UINT32, 16, "HighQualityIn")
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Rate transferRate(ReadView const &view, AccountID const &issuer)
majorityAmendments_t getMajorityAmendments(ReadView const &view)
A pair of SHAMap key and LedgerEntryType.
const SF_U64 sfIndexNext(access, STI_UINT64, 1, "IndexNext")
std::set< uint256 > getEnabledAmendments(ReadView const &view)
A currency issued by an account.
Keylet const & amendments() noexcept
The index of the amendment table.
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
constexpr value_type drops() const
Returns the number of drops.
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)
const SF_Account sfAccount(access, STI_ACCOUNT, 1, "Account")
const SF_U32 sfFlags(access, STI_UINT32, 2, "Flags")
virtual void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance)
const SField sfMajorities(access, STI_ARRAY, 16, "Majorities")
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
void addRaw(LedgerInfo const &info, Serializer &s)
std::string to_string(ListDisposition disposition)
const SF_U64 sfOwnerNode(access, STI_UINT64, 4, "OwnerNode")
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)
const SF_U32 sfOwnerCount(access, STI_UINT32, 13, "OwnerCount")
static Sink & getNullSink()
Returns a Sink which does nothing.
const SF_Amount sfHighLimit(access, STI_AMOUNT, 7, "HighLimit")
const Rate parityRate(QUALITY_ONE)
A transfer rate signifying a 1:1 exchange.
Writeable view to a ledger, for applying a transaction.
const SF_Amount sfLowLimit(access, STI_AMOUNT, 6, "LowLimit")
const uint256 featureDeletableAccounts
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_U32 sfLastLedgerSequence(access, STI_UINT32, 27, "LastLedgerSequence")
std::string getFullText() const override
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.
const SF_Account sfOwner(access, STI_ACCOUNT, 2, "Owner")
const SF_U64 sfHighNode(access, STI_UINT64, 8, "HighNode")
const SF_U256 sfBookDirectory(access, STI_HASH256, 16, "BookDirectory")
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
const SF_U256 sfAmendment(access, STI_HASH256, 19, "Amendment")
Keylet page(uint256 const &key, std::uint64_t index) noexcept
A page in a directory.
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)
bool isXRP(AccountID const &c)
@ current
This was a new validation and was added.
A generic endpoint for log messages.
const SF_U32 sfLowQualityOut(access, STI_UINT32, 19, "LowQualityOut")
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)
const SF_Vec256 sfIndexes(access, STI_VECTOR256, 1, "Indexes", SField::sMD_Never)
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_Vec256 sfHashes(access, STI_VECTOR256, 2, "Hashes")
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.
const SF_U32 sfLowQualityIn(access, STI_UINT32, 18, "LowQualityIn")
const SF_Amount sfBalance(access, STI_AMOUNT, 2, "Balance")
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_U32 sfCloseTime(access, STI_UINT32, 7, "CloseTime")
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 bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
const SF_U32 sfTransferRate(access, STI_UINT32, 11, "TransferRate")
const SF_U64 sfLowNode(access, STI_UINT64, 7, "LowNode")
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()})
const SF_Vec256 sfAmendments(access, STI_VECTOR256, 3, "Amendments")
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_U64 sfBookNode(access, STI_UINT64, 3, "BookNode")
const std::shared_ptr< STLedgerEntry > & ref
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_U32 sfHighQualityOut(access, STI_UINT32, 17, "HighQualityOut")
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
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)