20#include <xrpld/app/main/Application.h>
21#include <xrpld/app/misc/CredentialHelpers.h>
22#include <xrpld/app/misc/LoadFeeTrack.h>
23#include <xrpld/app/tx/apply.h>
24#include <xrpld/app/tx/detail/NFTokenUtils.h>
25#include <xrpld/app/tx/detail/SignerEntries.h>
26#include <xrpld/app/tx/detail/Transactor.h>
27#include <xrpld/core/Config.h>
28#include <xrpld/ledger/View.h>
30#include <xrpl/basics/Log.h>
31#include <xrpl/basics/contract.h>
32#include <xrpl/json/to_string.h>
33#include <xrpl/protocol/Feature.h>
34#include <xrpl/protocol/Indexes.h>
35#include <xrpl/protocol/Protocol.h>
36#include <xrpl/protocol/UintTypes.h>
63 if (*txNID != nodeNID)
70 if (txID == beast::zero)
73 <<
"applyTransaction: transaction id may not be zero";
97 if (
id == beast::zero)
99 JLOG(ctx.
j.
warn()) <<
"preflight1: bad account id";
105 if (!fee.native() || fee.negative() || !
isLegalAmount(fee.xrp()))
107 JLOG(ctx.
j.
debug()) <<
"preflight1: invalid fee";
115 JLOG(ctx.
j.
debug()) <<
"preflight1: invalid signing key";
153 if (signer.isFieldPresent(sfTxnSignature) &&
154 !signer[sfTxnSignature].empty())
168 JLOG(ctx.
j.
debug()) <<
"preflight2: bad signature. " << sigValid.second;
182 : app(app_), tx(tx_), rules(rules_), flags(flags_), j(j_)
189 : ctx_(ctx), j_(ctx.journal), account_(ctx.tx.getAccountID(sfAccount))
208 return baseFee + (signerCount * baseFee);
224 if (!ctx.
tx[sfFee].native())
227 auto const feePaid = ctx.
tx[sfFee].xrp();
237 if (feePaid < feeDue)
240 <<
"Insufficient fee paid: " <<
to_string(feePaid) <<
"/"
246 if (feePaid == beast::zero)
254 auto const balance = (*sle)[sfBalance].xrp();
256 if (balance < feePaid)
259 <<
"Insufficient balance:" <<
" balance=" <<
to_string(balance)
262 if ((balance > beast::zero) && !ctx.
view.
open())
277 auto const feePaid =
ctx_.
tx[sfFee].xrp();
307 <<
"applyTransaction: delay: source account does not exist "
315 if (t_seqProx.
isSeq())
320 JLOG(j.
trace()) <<
"applyTransaction: has both a TicketSequence "
321 "and a non-zero Sequence number";
324 if (t_seqProx != a_seq)
326 if (a_seq < t_seqProx)
329 <<
"applyTransaction: has future sequence number "
330 <<
"a_seq=" << a_seq <<
" t_seq=" << t_seqProx;
334 JLOG(j.
trace()) <<
"applyTransaction: has past sequence number "
335 <<
"a_seq=" << a_seq <<
" t_seq=" << t_seqProx;
348 JLOG(j.
trace()) <<
"applyTransaction: has future ticket id "
349 <<
"a_seq=" << a_seq <<
" t_seq=" << t_seqProx;
357 <<
"applyTransaction: ticket already used or never created "
358 <<
"a_seq=" << a_seq <<
" t_seq=" << t_seqProx;
376 <<
"applyTransaction: delay: source account does not exist "
382 (sle->getFieldH256(sfAccountTxnID) !=
400 sleAccount,
"ripple::Transactor::consumeSeqProxy : non-null account");
407 sleAccount->setFieldU32(sfSequence, seqProx.
value() + 1);
427 JLOG(j.
fatal()) <<
"Ticket disappeared from ledger.";
434 JLOG(j.
fatal()) <<
"Unable to delete Ticket from owner.";
443 JLOG(j.
fatal()) <<
"Could not find Ticket owner account root.";
447 if (
auto ticketCount = (*sleAccount)[~sfTicketCount])
449 if (*ticketCount == 1)
450 sleAccount->makeFieldAbsent(sfTicketCount);
452 ticketCount = *ticketCount - 1;
456 JLOG(j.
fatal()) <<
"TicketCount field missing from account root.";
474 "ripple::Transactor::preCompute : nonzero account");
489 sle !=
nullptr ||
account_ == beast::zero,
490 "ripple::Transactor::apply : non-null SLE or zero account");
505 if (sle->isFieldPresent(sfAccountTxnID))
540 <<
"checkSingleSign: signing public key type is unknown";
553 "ripple::Transactor::checkSingleSign : non-empty signer or simulation");
554 auto const idSigner = pkSigner.empty()
562 if ((*sleAccount)[~sfRegularKey] == idSigner)
568 if (!isMasterDisabled && idAccount == idSigner)
574 if (isMasterDisabled && idAccount == idSigner)
583 if (idSigner == idAccount)
586 if (isMasterDisabled)
589 else if ((*sleAccount)[~sfRegularKey] == idSigner)
593 else if (sleAccount->isFieldPresent(sfRegularKey))
597 <<
"checkSingleSign: Not authorized to use account.";
605 <<
"checkSingleSign: Not authorized to use account.";
620 if (!sleAccountSigners)
623 <<
"applyTransaction: Invalid: Not a multi-signing account.";
630 sleAccountSigners->isFieldPresent(sfSignerListID),
631 "ripple::Transactor::checkMultiSign : has signer list ID");
633 sleAccountSigners->getFieldU32(sfSignerListID) == 0,
634 "ripple::Transactor::checkMultiSign : signer list ID is 0");
636 auto accountSigners =
639 return accountSigners.error();
651 auto iter = accountSigners->begin();
652 for (
auto const& txSigner : txSigners)
654 AccountID const txSignerAcctID = txSigner.getAccountID(sfAccount);
657 while (iter->account < txSignerAcctID)
659 if (++iter == accountSigners->end())
662 <<
"applyTransaction: Invalid SigningAccount.Account.";
666 if (iter->account != txSignerAcctID)
670 <<
"applyTransaction: Invalid SigningAccount.Account.";
677 auto const spk = txSigner.getFieldVL(sfSigningPubKey);
682 <<
"checkMultiSign: signing public key type is unknown";
689 "ripple::Transactor::checkMultiSign : non-empty signer or "
691 AccountID const signingAcctIDFromPubKey = spk.empty()
722 if (signingAcctIDFromPubKey == txSignerAcctID)
729 sleTxSignerRoot->getFieldU32(sfFlags);
734 <<
"applyTransaction: Signer:Account lsfDisableMaster.";
743 if (!sleTxSignerRoot)
745 JLOG(ctx.
j.
trace()) <<
"applyTransaction: Non-phantom signer "
746 "lacks account root.";
750 if (!sleTxSignerRoot->isFieldPresent(sfRegularKey))
753 <<
"applyTransaction: Account lacks RegularKey.";
756 if (signingAcctIDFromPubKey !=
757 sleTxSignerRoot->getAccountID(sfRegularKey))
760 <<
"applyTransaction: Account doesn't match RegularKey.";
765 weightSum += iter->weight;
769 if (weightSum < sleAccountSigners->getFieldU32(sfSignerQuorum))
772 <<
"applyTransaction: Signers failed to meet quorum.";
790 for (
auto const& index : offers)
810 for (
auto const& index : offers)
827 for (
auto const& index : creds)
843 <<
"removeDeletedTrustLines: deleted trustlines exceed max "
844 << trustLines.
size();
848 for (
auto const& index : trustLines)
850 if (
auto const sleState = view.
peek({ltRIPPLE_STATE, index});
855 <<
"removeDeletedTrustLines: failed to delete AMM trustline";
873 auto const balance = txnAcct->getFieldAmount(sfBalance).xrp();
877 balance != beast::zero && (!
view().
open() || balance >= fee),
878 "ripple::Transactor::reset : valid balance");
891 txnAcct->setFieldAmount(sfBalance, balance - fee);
894 isTesSuccess(ter),
"ripple::Transactor::reset : result is tesSUCCESS");
907 JLOG(
j_.
debug()) <<
"Transaction trapped: " << txHash;
931 JLOG(
j_.
fatal()) <<
"Transaction serdes mismatch";
935 "ripple::Transactor::operator() : transaction serdes mismatch");
954 "ripple::Transactor::operator() : result is not temUNKNOWN");
957 stream <<
"preclaim result: " <<
transToken(result);
989 bool const doOffers =
992 bool const doNFTokenOffers = (result ==
tecEXPIRED);
993 bool const doCredentials = (result ==
tecEXPIRED);
994 if (doOffers || doLines || doNFTokenOffers || doCredentials)
1001 &expiredNFTokenOffers,
1003 &expiredCredentials](
1012 "ripple::Transactor::operator()::visit : non-null SLE "
1014 if (doOffers && before &&
after &&
1015 (before->getType() == ltOFFER) &&
1016 (before->getFieldAmount(sfTakerPays) ==
1017 after->getFieldAmount(sfTakerPays)))
1023 if (doLines && before &&
after &&
1024 (before->getType() == ltRIPPLE_STATE))
1030 if (doNFTokenOffers && before &&
after &&
1031 (before->getType() == ltNFTOKEN_OFFER))
1034 if (doCredentials && before &&
after &&
1035 (before->getType() == ltCREDENTIAL))
1043 auto const resetResult =
reset(fee);
1045 result = resetResult.first;
1047 fee = resetResult.second;
1080 auto const resetResult =
reset(fee);
1082 result = resetResult.first;
1084 fee = resetResult.second;
1107 if (fee < beast::zero)
1108 Throw<std::logic_error>(
"fee charged is negative!");
1114 if (!
view().
open() && fee != beast::zero)
1126 JLOG(
j_.
trace()) << (applied ?
"applied " :
"not applied ")
1129 return {result, applied, metadata};
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
virtual Config & config()=0
virtual LoadFeeTrack & getFeeTrack()=0
virtual beast::Journal journal(std::string const &name)=0
virtual const std::optional< uint256 > & trapTxID() const =0
virtual HashRouter & getHashRouter()=0
State information when applying a tx.
void visit(std::function< void(uint256 const &key, bool isDelete, std::shared_ptr< SLE const > const &before, std::shared_ptr< SLE const > const &after)> const &func)
Visit unapplied changes.
std::optional< TxMeta > apply(TER)
Apply the transaction result to the base.
ApplyFlags const & flags() const
void discard()
Discard changes and start fresh.
void destroyXRP(XRPAmount const &fee)
std::size_t size()
Get the number of unapplied changes.
TER checkInvariants(TER const result, XRPAmount const fee)
Applies all invariant checkers one by one.
Writeable view to a ledger, for applying a transaction.
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
RAII class to set and restore the current transaction rules.
RAII class to set and restore the Number switchover.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual bool open() const =0
Returns true if this reflects an open ledger.
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual bool txExists(key_type const &key) const =0
Returns true if a tx exists in the tx map.
Rules controlling protocol behavior.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
RAII class to set and restore the STAmount canonicalize switchover.
AccountID getAccountID(SField const &field) const
const STArray & getFieldArray(SField const &field) const
bool isEquivalent(const STBase &t) const override
std::uint32_t getFieldU32(SField const &field) const
void add(Serializer &s) const override
STAmount const & getFieldAmount(SField const &field) const
bool isFieldPresent(SField const &field) const
uint256 getFieldH256(SField const &field) const
SeqProxy getSeqProxy() const
Json::Value getJson(JsonOptions options) const override
Blob getSigningPubKey() const
Blob getSignature() const
uint256 getTransactionID() const
A type that represents either a sequence value or a ticket value.
static constexpr SeqProxy sequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
constexpr bool isSeq() const
constexpr std::uint32_t value() const
constexpr bool isTicket() const
Slice slice() const noexcept
static Expected< std::vector< SignerEntry >, NotTEC > deserialize(STObject const &obj, beast::Journal journal, std::string_view annotation)
TER consumeSeqProxy(SLE::pointer const &sleAccount)
ApplyResult operator()()
Process the transaction.
static NotTEC checkPriorTxAndLastLedger(PreclaimContext const &ctx)
static TER checkFee(PreclaimContext const &ctx, XRPAmount baseFee)
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
static NotTEC checkSeqProxy(ReadView const &view, STTx const &tx, beast::Journal j)
static NotTEC checkSign(PreclaimContext const &ctx)
void trapTransaction(uint256) const
static XRPAmount minimumFee(Application &app, XRPAmount baseFee, Fees const &fees, ApplyFlags flags)
Compute the minimum fee required to process a transaction with a given baseFee based on the current s...
static NotTEC checkSingleSign(PreclaimContext const &ctx)
virtual void preCompute()
static TER ticketDelete(ApplyView &view, AccountID const &account, uint256 const &ticketIndex, beast::Journal j)
std::pair< TER, XRPAmount > reset(XRPAmount fee)
Reset the context, discarding any changes made and adjust the fee.
Transactor(Transactor const &)=delete
static NotTEC checkMultiSign(PreclaimContext const &ctx)
TER deleteSLE(ApplyView &view, std::shared_ptr< SLE > const &sleCredential, beast::Journal j)
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet signers(AccountID const &account) noexcept
A SignerList.
Keylet nftoffer(AccountID const &owner, std::uint32_t seq)
An offer from an account to buy or sell an NFT.
static ticket_t const ticket
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
bool deleteTokenOffer(ApplyView &view, std::shared_ptr< SLE > const &offer)
Deletes the given token offer.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
@ telNETWORK_ID_MAKES_TX_NON_CANONICAL
bool isLegalAmount(XRPAmount const &amount)
Returns true if the amount does not exceed the initial XRP in existence.
TER deleteAMMTrustLine(ApplyView &view, std::shared_ptr< SLE > sleState, std::optional< AccountID > const &ammAccountID, beast::Journal j)
Delete trustline to AMM.
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
std::size_t constexpr unfundedOfferRemoveLimit
The maximum number of unfunded offers to delete at once.
NotTEC preflight0(PreflightContext const &ctx)
Performs early sanity checks on the txid.
std::size_t constexpr expiredOfferRemoveLimit
The maximum number of expired offers to delete at once.
std::size_t constexpr oversizeMetaDataCap
The maximum number of metadata entries allowed in one transaction.
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
AccountID calcAccountID(PublicKey const &pk)
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
static void removeUnfundedOffers(ApplyView &view, std::vector< uint256 > const &offers, beast::Journal viewJ)
@ open
We haven't closed our ledger yet, but others might have.
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
static void removeExpiredCredentials(ApplyView &view, std::vector< uint256 > const &creds, beast::Journal viewJ)
static bool adjustOwnerCount(ApplyContext &ctx, int count)
std::string transToken(TER code)
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
static void removeExpiredNFTokenOffers(ApplyView &view, std::vector< uint256 > const &offers, beast::Journal viewJ)
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
bool isTecClaimHardFail(TER ter, ApplyFlags flags)
Return true if the transaction can claim a fee (tec), and the ApplyFlags do not allow soft failures.
uint256 getTicketIndex(AccountID const &account, std::uint32_t uSequence)
@ SigBad
Signature is bad. Didn't do local checks.
std::string to_string(base_uint< Bits, Tag > const &a)
static void removeDeletedTrustLines(ApplyView &view, std::vector< uint256 > const &trustLines, beast::Journal viewJ)
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
std::uint16_t constexpr maxDeletableAMMTrustLines
The maximum number of trustlines to delete as part of AMM account deletion cleanup.
std::pair< Validity, std::string > checkValidity(HashRouter &router, STTx const &tx, Rules const &rules, Config const &config)
Checks transaction signature and local checks.
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
TERSubset< CanCvtToNotTEC > NotTEC
Reflects the fee settings for a particular ledger.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.
PreflightContext(Application &app_, STTx const &tx_, Rules const &rules_, ApplyFlags flags_, beast::Journal j_)