xrpld
Loading...
Searching...
No Matches
CosignerInvariant.cpp
1#include <xrpl/tx/invariants/CosignerInvariant.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/Feature.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/Keylet.h>
11#include <xrpl/protocol/LedgerFormats.h>
12#include <xrpl/protocol/Protocol.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STArray.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STObject.h>
17#include <xrpl/protocol/STTx.h>
18#include <xrpl/protocol/SeqProxy.h>
19#include <xrpl/protocol/TER.h>
20#include <xrpl/protocol/TxFormats.h>
21#include <xrpl/protocol/XRPAmount.h>
22
23#include <algorithm>
24#include <array>
25#include <cstddef>
26#include <cstdint>
27#include <optional>
28
29namespace xrpl {
30
31namespace {
32
33bool
34validSignerArray(STArray const& signers, std::size_t limit, SField const& elementName)
35{
36 if (signers.size() > limit)
37 return false;
38
39 std::optional<AccountID> previous;
40 for (auto const& signer : signers)
41 {
42 if (signer.getFName() != elementName || !signer.isFieldPresent(sfAccount))
43 return false;
44
45 auto const account = signer.getAccountID(sfAccount);
46 if (previous && account <= *previous)
47 return false;
48 previous = account;
49 }
50 return true;
51}
52
53bool
54validNestedSigners(STObject const& signature)
55{
56 return !signature.isFieldPresent(sfSigners) ||
57 validSignerArray(signature.getFieldArray(sfSigners), STTx::kMaxMultiSigners, sfSigner);
58}
59
60bool
61validSignerArrays(STObject const& proposedTx)
62{
63 if (!validNestedSigners(proposedTx))
64 return false;
65
66 for (auto const* field :
67 std::array<SField const*, 2>{&sfCounterpartySignature, &sfSponsorSignature})
68 {
69 if (proposedTx.isFieldPresent(*field) &&
70 !validNestedSigners(proposedTx.getFieldObject(*field)))
71 return false;
72 }
73
74 if (!proposedTx.isFieldPresent(sfBatchSigners))
75 return true;
76
77 auto const& batchSigners = proposedTx.getFieldArray(sfBatchSigners);
78 if (!validSignerArray(batchSigners, kMaxBatchSigners, sfBatchSigner))
79 return false;
80
82 batchSigners, [](auto const& batchSigner) { return validNestedSigners(batchSigner); });
83}
84
85// The Ticket a proposal is keyed to: the one its proposed transaction spends.
87proposalTicketKey(SLE const& proposal)
88{
89 auto const& proposedTx = proposal.getFieldObject(sfProposedTransaction);
90 return keylet::ticket(
91 proposedTx.getAccountID(sfAccount),
92 SeqProxy::rawTicket(proposedTx.getFieldU32(sfTicketSequence)))
93 .key;
94}
95
96} // namespace
97
98void
100{
101 // `after` is always present (on deletion it holds the final state), so it
102 // alone determines the entry type. LedgerEntryTypesMatch owns malformed
103 // type transitions; skip them rather than read one side with the other's
104 // schema.
105 if (!after || (before && before->getType() != after->getType()))
106 return;
107
108 auto const type = after->getType();
109
110 // Consuming a Ticket deletes the proposal keyed to it (XLS-0103 §4.5).
111 // Remember each Ticket that went, so finalize can tell a proposal deleted
112 // with its Ticket from one that simply vanished, and the proposal each
113 // Ticket was keyed to, so finalize can check that none stayed behind.
114 if (type == ltTICKET)
115 {
116 if (isDelete)
117 {
118 deletedTickets_.insert(after->key());
121 after->getAccountID(sfAccount), after->getFieldU32(sfTicketSequence)));
122 }
123 return;
124 }
125
126 if (type != ltTRANSACTION_PROPOSAL)
127 return;
128
129 if (!before)
130 {
131 ++created_;
132 }
133 else if (isDelete)
134 {
135 ++deleted_;
136 ticketsOfDeletedProposals_.push_back(proposalTicketKey(*before));
137 }
138 else
139 {
140 ++modified_;
141 }
142
143 // A live proposal's signer lists must stay submittable: within size
144 // limits, sorted by account, and free of duplicates.
145 if (!isDelete && !validSignerArrays(after->getFieldObject(sfProposedTransaction)))
147}
148
149bool
151 STTx const& tx,
152 TER result,
153 XRPAmount,
154 ReadView const& view,
155 beast::Journal const& j) const
156{
157 if (!view.rules().enabled(featureCosign))
158 return true;
159
161 {
162 JLOG(j.fatal()) << "Invariant failed: TransactionProposal signer arrays are not canonical.";
163 return false;
164 }
165
166 // A proposal leaves the ledger in two ways (XLS-0103 §4.5): a
167 // TransactionProposalCancel deletes the proposal it names, and consuming
168 // the Ticket a proposal is keyed to deletes the proposal with it, whether
169 // the proposed transaction executed (or failed with a claimed-fee tec) or
170 // AccountDelete swept the target's Tickets.
171 std::uint32_t deletedWithTicket = 0;
172 for (auto const& ticketKey : ticketsOfDeletedProposals_)
173 {
174 if (deletedTickets_.contains(ticketKey))
175 ++deletedWithTicket;
176 }
177
178 bool const succeeded = isTesSuccess(result);
179 bool effectsMatch = false;
180 if (tx.getTxnType() == ttTRANSACTION_PROPOSAL_CREATE)
181 {
182 effectsMatch = created_ == (succeeded ? 1u : 0u) && modified_ == 0 && deleted_ == 0;
183 }
184 else if (tx.getTxnType() == ttTRANSACTION_PROPOSAL_CANCEL)
185 {
186 // A Cancel deletes one proposal and nothing else. It cannot
187 // spend a reserved Ticket, so no proposal goes with its Ticket here.
188 effectsMatch = created_ == 0 && modified_ == 0 && deletedWithTicket == 0 &&
189 deleted_ == (succeeded ? 1u : 0u);
190 }
191 else
192 {
193 // Any other transaction deletes a proposal only together with its
194 // Ticket, and modifies at most one, only as a successful
195 // SponsorshipTransfer.
196 // TODO: extend this when TransactionProposalSign is implemented.
197 std::uint32_t const modifiable =
198 tx.getTxnType() == ttSPONSORSHIP_TRANSFER && succeeded ? 1 : 0;
199 effectsMatch = created_ == 0 && modified_ <= modifiable && deleted_ == deletedWithTicket;
200 }
201
202 if (!effectsMatch)
203 {
204 JLOG(j.fatal())
205 << "Invariant failed: TransactionProposal changes do not match transaction result.";
206 return false;
207 }
208
209 // The other direction of the deletion rule: ticketDelete deletes the
210 // proposal keyed to a Ticket along with it (XLS-0103 §4.5), so no
211 // proposal may outlive its Ticket, orphaned on a key that can no longer
212 // be spent.
213 bool const orphaned = std::ranges::any_of(
214 proposalsOfDeletedTickets_, [&view](Keylet const& keylet) { return view.exists(keylet); });
215 if (orphaned)
216 {
217 JLOG(j.fatal())
218 << "Invariant failed: TransactionProposal outlived the Ticket it is keyed to.";
219 return false;
220 }
221
222 return true;
223}
224
225} // namespace xrpl
T all_of(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
Stream fatal() const
Definition Journal.h:368
UInt size() const
Number of values in array or object.
A view into a ledger.
Definition ReadView.h:41
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
Identifies fields.
Definition SField.h:131
std::shared_ptr< STLedgerEntry const > const & ConstRef
static constexpr std::size_t kMaxMultiSigners
Definition STTx.h:49
TxType getTxnType() const
Definition STTx.h:250
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
Definition SeqProxy.h:74
std::set< uint256 > deletedTickets_
std::vector< uint256 > ticketsOfDeletedProposals_
bool finalize(STTx const &tx, TER result, XRPAmount, ReadView const &view, beast::Journal const &j) const
void visitEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after)
std::vector< Keylet > proposalsOfDeletedTickets_
Keylet computation functions.
Definition Indexes.h:39
Keylet txProposal(AccountID const &target, std::uint32_t ticketSequence) noexcept
A TransactionProposal.
Definition Indexes.cpp:378
Keylet ticket(AccountID const &id, SeqProxy const &ticketSeq)
A ticket belonging to an account.
Definition Indexes.cpp:344
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
json::Value signers(Account const &account, std::uint32_t quorum, std::vector< Signer > const &v)
Definition multisign.cpp:31
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr std::size_t kMaxBatchSigners
The maximum number of batch signers.
Definition Protocol.h:487
STLedgerEntry SLE
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:668
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
UInt256 uint256
Definition base_uint.h:587
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21