xrpld
Loading...
Searching...
No Matches
libxrpl/ledger/helpers/ProposalHelpers.cpp
1#include <xrpl/ledger/helpers/ProposalHelpers.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/ledger/ApplyView.h>
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/ledger/View.h>
10#include <xrpl/ledger/helpers/AccountRootHelpers.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/Keylet.h>
14#include <xrpl/protocol/LedgerFormats.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STArray.h>
17#include <xrpl/protocol/STLedgerEntry.h>
18#include <xrpl/protocol/STObject.h>
19#include <xrpl/protocol/STTx.h>
20#include <xrpl/protocol/Serializer.h>
21#include <xrpl/protocol/TER.h>
22#include <xrpl/protocol/TxFlags.h>
23#include <xrpl/protocol/TxFormats.h>
24
25#include <algorithm>
26#include <array>
27#include <cstdint>
28#include <optional>
29
30namespace xrpl::proposal {
31
32namespace {
33
34// Proposals do not nest, so a proposed transaction may carry none of these
35// types, neither as its own payload nor as a Batch inner transaction.
36constexpr auto kProposalTxTypes = std::to_array<TxType>({
37 ttTRANSACTION_PROPOSAL_CREATE,
38 ttTRANSACTION_PROPOSAL_CANCEL,
39});
40
41} // namespace
42
43bool
45{
46 if (!proposedTx.isFieldPresent(sfTransactionType))
47 return false;
48
49 auto const type = proposedTx.getFieldU16(sfTransactionType);
50
51 if (std::ranges::find(kProposalTxTypes, type) != kProposalTxTypes.end())
52 return false;
53
54 if (isPseudoTx(proposedTx))
55 return false;
56
57 if (proposedTx.isFieldPresent(sfFlags) &&
58 (proposedTx.getFieldU32(sfFlags) & tfInnerBatchTxn) != 0u)
59 return false;
60
61 if (type == ttBATCH && proposedTx.isFieldPresent(sfRawTransactions))
62 {
63 STArray const& innerTxns = proposedTx.getFieldArray(sfRawTransactions);
64 for (STObject const& inner : innerTxns)
65 {
66 // The only production caller reaches this walk through STTx
67 // construction, which rejects a typeless inner first, so this is
68 // re-checked rather than relied upon.
69 if (!inner.isFieldPresent(sfTransactionType))
70 return false;
71
72 auto const innerType = inner.getFieldU16(sfTransactionType);
73
74 // A nested Batch is rejected alongside nested proposals and
75 // pseudo-transactions: STTx construction forbids inner Batches, so
76 // a proposal must not be able to store one either.
77 if (std::ranges::find(kProposalTxTypes, innerType) != kProposalTxTypes.end() ||
78 innerType == ttBATCH || isPseudoTx(inner))
79 return false;
80 }
81 }
82
83 return true;
84}
85
86bool
88 ReadView const& view,
90 STObject const& proposedTx)
91{
92 if (hasExpired(view, expiration))
93 return true;
94
95 // Mirror the ordinary transactor's tefMAX_LEDGER rule: view.seq() > bound.
96 // At view.seq() == bound the payload can still be submitted, so the
97 // proposal is still live (On-Chain Cosigner spec §4.5).
98 return proposedTx.isFieldPresent(sfLastLedgerSequence) &&
99 view.seq() > proposedTx.getFieldU32(sfLastLedgerSequence);
100}
101
102TER
104{
105 if (!sleProposal || sleProposal->getType() != ltTRANSACTION_PROPOSAL ||
106 !view.exists(Keylet{ltTRANSACTION_PROPOSAL, sleProposal->key()}))
107 {
108 // LCOV_EXCL_START
109 JLOG(j.fatal()) << "Invalid TransactionProposal deletion.";
110 return tecINTERNAL;
111 // LCOV_EXCL_STOP
112 }
113
114 AccountID const owner = sleProposal->getAccountID(sfOwner);
115
116 std::uint64_t const page{(*sleProposal)[sfOwnerNode]};
117 if (!view.dirRemove(keylet::ownerDir(owner), page, sleProposal->key(), true))
118 {
119 // LCOV_EXCL_START
120 JLOG(j.fatal()) << "Unable to delete TransactionProposal from owner.";
121 return tefBAD_LEDGER;
122 // LCOV_EXCL_STOP
123 }
124
125 auto const sleOwner = view.peek(keylet::account(owner));
126 if (!sleOwner)
127 {
128 // LCOV_EXCL_START
129 JLOG(j.fatal()) << "Could not find TransactionProposal owner account root.";
130 return tefBAD_LEDGER;
131 // LCOV_EXCL_STOP
132 }
133
134 // Release the reserve against the Owner or, if the entry carries a
135 // reserve sponsor, against that sponsor.
137 view,
138 sleOwner,
139 sleProposal,
140 proposalOwnerCount(sleProposal->getFieldObject(sfProposedTransaction)),
141 j);
142
143 view.erase(sleProposal);
144 return tesSUCCESS;
145}
146
147bool
148payloadMatches(STObject const& proposedTx, STObject const& tx)
149{
150 XRPL_ASSERT(
151 proposedTx.isFieldPresent(sfTransactionType) && tx.isFieldPresent(sfTransactionType),
152 "xrpl::proposal::payloadMatches : transaction-shaped inputs");
153
154 // The signing-payload serialization omits every kNotSigning field: the
155 // signature containers a proposal lets evolve (TxnSignature, Signers,
156 // BatchSigners, CounterpartySignature, SponsorSignature) plus Signature
157 // and MasterSignature, which the account's own signature does not cover
158 // either (see the header). What remains is the field set fixed at
159 // creation — except SigningPubKey, which signing payloads include but
160 // which is also mutable here (stored empty, filled if the target signs
161 // with its own key). Neutralize it on both sides before comparing.
162 auto const payloadBytes = [](STObject const& obj) {
163 STObject copy{obj};
164 copy.setFieldVL(sfSigningPubKey, Slice{});
165 Serializer s;
166 copy.addWithoutSigningFields(s);
167 return s.getData();
168 };
169
170 return payloadBytes(proposedTx) == payloadBytes(tx);
171}
172
173bool
174canConsumeTicket(ReadView const& view, STTx const& tx)
175{
176 auto const seqProx = tx.getSeqProxy();
177 if (!seqProx.isTicket())
178 {
179 // The caller only reaches here for a Ticket-based transaction.
180 // LCOV_EXCL_START
181 UNREACHABLE("xrpl::proposal::canConsumeTicket : tx spends a Sequence");
182 return false;
183 // LCOV_EXCL_STOP
184 }
185
186 auto const sleProposal =
187 view.read(keylet::txProposal(tx.getAccountID(sfAccount), seqProx.value()));
188 if (!sleProposal)
189 return true;
190
191 return payloadMatches(sleProposal->getFieldObject(sfProposedTransaction), tx);
192}
193
194} // namespace xrpl::proposal
A generic endpoint for log messages.
Definition Journal.h:44
Stream fatal() const
Definition Journal.h:368
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:141
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
bool dirRemove(Keylet const &directory, std::uint64_t page, UInt256 const &key, bool keepRoot)
Remove an entry from a directory.
virtual void erase(SLE::Ref sle)=0
Remove a peeked SLE.
A view into a ledger.
Definition ReadView.h:41
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition ReadView.h:115
std::shared_ptr< STLedgerEntry > pointer
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:601
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:688
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:643
std::uint16_t getFieldU16(SField const &field) const
Definition STObject.cpp:595
SeqProxy getSeqProxy() const
Definition STTx.cpp:198
Blob getData() const
Definition Serializer.h:278
An immutable linear range of bytes.
Definition Slice.h:28
T find(T... args)
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:415
Keylet txProposal(AccountID const &target, std::uint32_t ticketSequence) noexcept
A TransactionProposal.
Definition Indexes.cpp:378
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
bool isTerminal(ReadView const &view, std::optional< std::uint32_t > expiration, STObject const &proposedTx)
Whether the proposal is terminal (On-Chain Cosigner spec §4.5).
std::uint32_t proposalOwnerCount(STObject const &proposedTx)
Owner-reserve increments held by a proposal of the given transaction.
bool payloadMatches(STObject const &proposedTx, STObject const &tx)
Whether tx carries the same payload as a proposal's stored ProposedTransaction (On-Chain Cosigner spe...
bool canConsumeTicket(ReadView const &view, STTx const &tx)
Whether tx may consume its Ticket.
bool isValidProposalTxnType(STObject const &proposedTx)
Whether the proposed transaction is independently submittable through the ordinary multi-sign path: n...
TER deleteProposal(ApplyView &view, SLE::pointer const &sleProposal, beast::Journal j)
Delete a TransactionProposal ledger entry.
constexpr FlagValue tfInnerBatchTxn
Definition TxFlags.h:44
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp, ExpiryComparison comparison=ExpiryComparison::Inclusive)
Determines whether the given expiration time has passed.
Definition View.cpp:52
@ tefBAD_LEDGER
Definition TER.h:164
void decreaseOwnerCountForObject(ApplyView &view, SLE::Ref accountSle, SLE::Ref objectSle, std::uint32_t count, beast::Journal j)
Decrease owner-count fields for an existing ledger object.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecINTERNAL
Definition TER.h:321
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
Definition STTx.cpp:889
@ tesSUCCESS
Definition TER.h:253
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20