Skip to content

Prefer confirmed utxos during coin selection - #2095

Merged
nullPointerEnjoyer merged 6 commits into
mintlayer:masterfrom
oliv3rdrt:prefer_confirmed_utxos
Nov 19, 2026
Merged

nullPointerEnjoyer merged 6 commits into
mintlayer:masterfrom
oliv3rdrt:prefer_confirmed_utxos

Conversation

@oliv3rdrt

Copy link
Copy Markdown
Contributor

Problem

Since #2063 the mempool limits how many transactions a cluster may contain and how big it may be. A transaction that spends unconfirmed (in-mempool) utxos joins the cluster of the transactions that produced them, so a wallet that builds on top of unconfirmed utxos can create a transaction that the mempool then rejects because the cluster grew too large.

Change

This is the first step of #2066: prefer confirmed utxos during selection.

select_inputs_for_send_request now also gathers the confirmed-only utxos, and a new select_coins_preferring_confirmed helper tries to reach the target from the confirmed set first, falling back to the full set (which also contains the unconfirmed utxos) only when the confirmed ones are not enough. The fallback is exactly the call that was made before, so behaviour is unchanged whenever confirmed utxos can't cover the amount. The preference is only applied when the caller did not select the inputs explicitly.

Not included here

The rest of #2066 is left for follow-ups:

  • Querying the node/mempool for the resulting cluster's tx count and byte size and retrying with a different utxo combination (needs new node RPC methods).
  • Handling non-utxo cluster relations once the mempool tracks them.

Tests

Added unit tests for select_coins_preferring_confirmed: it uses the confirmed set when it is sufficient, falls back to the full set when it is not, and uses the full set directly when there is no preference. The existing selection tests still pass.

Addresses #2066

Since the mempool started limiting the size of transaction clusters, a wallet
that builds a transaction on top of unconfirmed (in-mempool) utxos can produce
a transaction that the mempool then rejects because the resulting cluster is
too big.

As a first step towards taking cluster limits into account, prefer confirmed
utxos during selection. select_inputs_for_send_request now also gathers the
confirmed-only utxos and select_coins_preferring_confirmed tries to satisfy the
target from those first, falling back to the full set (which also includes the
unconfirmed utxos) only when the confirmed ones are not enough. The fallback
path is identical to the previous behaviour, so nothing regresses when confirmed
utxos can't cover the amount.

This addresses the first item of mintlayer#2066; querying the mempool for the resulting
cluster size and retrying is left for a follow-up.
@oliv3rdrt
oliv3rdrt force-pushed the prefer_confirmed_utxos branch from b495cc0 to 61273bc Compare August 28, 2026 13:34
@nullPointerEnjoyer

Copy link
Copy Markdown
Collaborator

Automated review — OpenCodeReview model (glm-5.3-flash via z.ai), run locally against the PR diff because the CI review job skips fork PRs. Not a human review; findings here do not replace maintainer approval.

Overall: Sound change implementing the issue #2066 mitigation already sketched in the TODO at wallet/src/account/mod.rs:217-229. I verified the key correctness properties: the confirmed-only pool is a strict subset of the full pool (output_cache's is_in_state is a per-tx bitflag check over UtxoState::Confirmed | InMempool | Inactive), the two pools are partitioned by currency identically so no outpoint can be double-selected, the zero-target short-circuit in select_coins keeps Some(vec![])-pool behavior identical to before, the fallback surfaces the (more informative) full-pool error, and the new Copy derive on CurrentFeeRate is genuinely required because utxo_output_groups_by_currency takes it by value and it is now consumed twice. The three new unit tests are deterministic despite Randomize (single-coin pools, or an assertion that holds for any reaching subset). No correctness, security, or consensus issues found; findings below are minor.

Findings:

  • medium · test-coverage · wallet/src/account/mod.rs:280-306 — All new tests cover only the select_coins_preferring_confirmed unit; the actual wiring in select_inputs_for_send_request (building confirmed_utxos_by_currency, and the two per-currency remove sites feeding it) is untested. A regression such as passing the full pool as the "confirmed" pool, or dropping the fee-currency bucket, would pass CI. Suggest an account/wallet-level test asserting that a send with both a sufficient confirmed utxo and an in-mempool utxo selects the confirmed one (and still succeeds when only in-mempool funds exist).
  • low · design · wallet/src/account/mod.rs:288 — Subtle behavior change in an edge case: with empty input_utxos and explicit Some(CoinSelectionAlgo::UsePreselected), select_all_coins previously spent the entire (confirmed + in-mempool + inactive) pool, but now spends only confirmed utxos whenever they suffice. That combo ("use all specified inputs" with nothing specified) is already ill-defined, and the new behavior arguably matches the PR's goal, but it's undocumented; worth a note in the function's comment or a debug assertion rejecting that combination.
  • low · performance · wallet/src/account/mod.rs:281-306 — The auto-selection branch now scans the whole output cache twice (get_utxos ×2) and builds output groups twice (utxo_output_groups_by_currency ×2, including per-output fee computation). Acceptable as the simple correct approach (state info is lost after grouping, so filtering one grouped set isn't trivial), but consider profiling for wallets with large UTXO sets.

@nullPointerEnjoyer
nullPointerEnjoyer force-pushed the master branch 2 times, most recently from 70bd008 to 3b3867e Compare October 6, 2026 19:00
The confirmed-only UTXO query filters by the state of the producing
transaction, and the same state mask decides whether a UTXO has already
been consumed. On its own, the confirmed-only mask therefore re-admits
UTXOs of confirmed transactions that the wallet has already spent in its
own pending or evicted transactions, and selecting one would build a
conflicting transaction. Intersect the confirmed-only set with the full
set so such UTXOs stay excluded.

Also document that the confirmed preference deliberately ignores waste,
add CHANGELOG entries for the preference and the fix, and cover the
weight-cap and empty-bucket fallback paths with tests.
@nullPointerEnjoyer

Copy link
Copy Markdown
Collaborator

One correctness issue found while reviewing the confirmed-preference wiring — the confirmed-only UTXO query filters by the state of the producing transaction, but its state mask is also what decides whether a UTXO has already been consumed. With the confirmed-only mask, UTXOs of confirmed transactions that the wallet has already spent in its own pending or evicted transactions slip back into the pool — an automatic send could then pick the same outpoint its own unconfirmed tx spends, producing a conflicting transaction the mempool rejects (or, under RBF, replacing the user's earlier payment).

The pushed commit intersects the confirmed-only set with the full pool's outpoints, making the confirmed pool a subset of the full pool by construction, so anything the full pool excludes stays excluded. It also documents the deliberate waste trade-off, adds CHANGELOG entries, and covers the weight-cap and empty-bucket fallback paths with tests (the weight-cap one verified to fail if the fallback is removed).

Suggested follow-ups (not addressed here): an account-level regression test with a pending send (needs output-cache seeding); the add_output test helper mints identical outpoints per call, which is slightly misleading in fallback fixtures.

…d-pool-subset

# Conflicts:
#	CHANGELOG.md
#	wallet/src/account/mod.rs
#	wallet/src/account/utxo_selector/tests.rs
@oliv3rdrt

Copy link
Copy Markdown
Contributor Author

@nullPointerEnjoyer Good catch on the UTXOs already spent by pending txs, thanks for the fix. Merged master in and resolved the conflicts. Happy to do the account-level pending send test as a follow-up.

The previous merge resolution removed the unreleased Node and Node CLI entries and the 1.4.5, 1.4.4 and 1.4.1 sections. Bring them back and keep the confirmed UTXOs entry under Wallet.
@oliv3rdrt

Copy link
Copy Markdown
Contributor Author

@erubboli The master merge here dropped ~98 lines of CHANGELOG (Node/Node CLI unreleased entries and the 1.4.5, 1.4.4, 1.4.1 sections), so I've pushed a commit putting them back. Code is unchanged.

@oliv3rdrt

Copy link
Copy Markdown
Contributor Author

@erubboli CI failures here look unrelated: macOS build was cancelled, clang crashed building secp256k1-sys in the wasm job, and static checks hit the chrono advisory that's failing on master too. Could you re-run the failed jobs?

@nullPointerEnjoyer
nullPointerEnjoyer merged commit 06ba17d into mintlayer:master Nov 19, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants