From 61273bcec4d128ec050ae0919901890d7eb13449 Mon Sep 17 00:00:00 2001 From: Oliver Anyanwu Date: Sat, 11 Jul 2026 16:23:42 +0200 Subject: [PATCH 1/3] Prefer confirmed utxos during coin selection 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 #2066; querying the mempool for the resulting cluster size and retrying is left for a follow-up. --- wallet/src/account/mod.rs | 69 ++++++++++++++++------- wallet/src/account/utxo_selector/mod.rs | 43 ++++++++++++++ wallet/src/account/utxo_selector/tests.rs | 67 ++++++++++++++++++++++ 3 files changed, 158 insertions(+), 21 deletions(-) diff --git a/wallet/src/account/mod.rs b/wallet/src/account/mod.rs index 59b3ca04d4..3765dc369a 100644 --- a/wallet/src/account/mod.rs +++ b/wallet/src/account/mod.rs @@ -84,7 +84,7 @@ use wallet_types::{ use crate::{ SendRequest, WalletError, WalletResult, - account::utxo_selector::{OutputGroup, select_coins}, + account::utxo_selector::{OutputGroup, select_coins_preferring_confirmed}, destination_getters::{HtlcSpendingCondition, get_tx_output_destination}, key_chain::{AccountKeyChains, KeyChainError, VRFAccountKeyChains}, send_request::{ @@ -112,7 +112,7 @@ pub use self::{ utxo_selector::{CoinSelectionAlgo, UtxoSelectorError}, }; -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub struct CurrentFeeRate { pub current_fee_rate: FeeRate, pub consolidate_fee_rate: FeeRate, @@ -277,39 +277,58 @@ impl Account { Ok(()) })?; - let (utxos, selection_algo) = if input_utxos.is_empty() { + let (utxos, confirmed_utxos, selection_algo) = if input_utxos.is_empty() { + let utxo_types = UtxoType::Transfer | UtxoType::LockThenTransfer | UtxoType::IssueNft; + let utxos = self.get_utxos( + utxo_types, + median_time, + UtxoState::Confirmed | UtxoState::InMempool | UtxoState::Inactive, + WithLocked::Unlocked, + ); + // Prefer confirmed utxos during selection: spending an unconfirmed utxo forms a + // mempool cluster, which is size limited, so building on top of confirmed utxos + // reduces the chance of producing a tx that the mempool would reject (see issue + // #2066). The confirmed-only set is tried first, with a fallback to the full set. + let confirmed_utxos = self.get_utxos( + utxo_types, + median_time, + UtxoState::Confirmed.into(), + WithLocked::Unlocked, + ); ( - self.get_utxos( - UtxoType::Transfer | UtxoType::LockThenTransfer | UtxoType::IssueNft, - median_time, - UtxoState::Confirmed | UtxoState::InMempool | UtxoState::Inactive, - WithLocked::Unlocked, - ), + utxos, + Some(confirmed_utxos), selection_algo.unwrap_or(CoinSelectionAlgo::Randomize), ) } else { let selection_algo = selection_algo.unwrap_or(CoinSelectionAlgo::UsePreselected); - match input_utxos { + let utxos = match input_utxos { SelectedInputs::Utxos(input_utxos) => { let current_block_info = BlockInfo { height: self.account_info.best_block_height(), timestamp: median_time, }; - ( - self.output_cache.find_utxos(current_block_info, input_utxos)?, - selection_algo, - ) + self.output_cache.find_utxos(current_block_info, input_utxos)? } - SelectedInputs::Inputs(ref inputs) => ( - inputs.iter().map(|(outpoint, utxo)| (outpoint.clone(), utxo)).collect(), - selection_algo, - ), - } + SelectedInputs::Inputs(ref inputs) => { + inputs.iter().map(|(outpoint, utxo)| (outpoint.clone(), utxo)).collect() + } + }; + (utxos, None, selection_algo) }; let current_fee_rate = fee_rates.current_fee_rate; let mut utxos_by_currency = self.utxo_output_groups_by_currency(fee_rates, &pay_fee_with_currency, utxos)?; + let mut confirmed_utxos_by_currency = confirmed_utxos + .map(|confirmed_utxos| { + self.utxo_output_groups_by_currency( + fee_rates, + &pay_fee_with_currency, + confirmed_utxos, + ) + }) + .transpose()?; let amount_to_be_paid_in_currency_with_fees = output_currency_amounts.remove(&pay_fee_with_currency).unwrap_or(Amount::ZERO); @@ -342,6 +361,9 @@ impl Account { .iter() .map(|(currency, output_amount)| -> WalletResult<_> { let utxos = utxos_by_currency.remove(currency).unwrap_or(vec![]); + let confirmed_utxos = confirmed_utxos_by_currency + .as_mut() + .map(|by_currency| by_currency.remove(currency).unwrap_or_default()); let preselected_amount = preselected_inputs .amounts .remove(currency) @@ -354,7 +376,8 @@ impl Account { change_address.clone(), )?; - let selection_result = select_coins( + let selection_result = select_coins_preferring_confirmed( + confirmed_utxos, utxos, output_amount.sub(preselected_amount).unwrap_or(Amount::ZERO), PayFee::DoNotPayFeeWithThisCurrency, @@ -393,6 +416,9 @@ impl Account { .try_collect()?; let utxos = utxos_by_currency.remove(&pay_fee_with_currency).unwrap_or(vec![]); + let confirmed_utxos = confirmed_utxos_by_currency + .as_mut() + .map(|by_currency| by_currency.remove(&pay_fee_with_currency).unwrap_or_default()); let num_inputs = selected_inputs .values() @@ -438,7 +464,8 @@ impl Account { change_address.clone(), )?; - let selection_result = select_coins( + let selection_result = select_coins_preferring_confirmed( + confirmed_utxos, utxos, (amount_to_be_paid_in_currency_with_fees - preselected_amount).unwrap_or(Amount::ZERO), PayFee::PayFeeWithThisCurrency, diff --git a/wallet/src/account/utxo_selector/mod.rs b/wallet/src/account/utxo_selector/mod.rs index dc03b8b84b..c38c8f142f 100644 --- a/wallet/src/account/utxo_selector/mod.rs +++ b/wallet/src/account/utxo_selector/mod.rs @@ -734,6 +734,49 @@ pub fn select_coins( } } +/// Select coins, preferring confirmed utxos. +/// +/// A transaction that spends an unconfirmed (in-mempool) utxo forms a cluster with the +/// transaction that produced it, and the mempool limits how big such a cluster may be. To +/// reduce the chance of building a transaction that the mempool would reject, this first tries +/// to reach the target using `confirmed_pool` alone. Only if that doesn't work (for example +/// because the confirmed utxos aren't enough) does it fall back to `full_pool`, which also +/// contains the unconfirmed utxos. +/// +/// `confirmed_pool` is `None` when there is no preference to apply, for example when the caller +/// selected the inputs explicitly; in that case `full_pool` is used directly. +pub fn select_coins_preferring_confirmed( + confirmed_pool: Option>, + full_pool: Vec, + selection_target: Amount, + pay_fees: PayFee, + cost_of_change: Amount, + coin_selection_algo: CoinSelectionAlgo, + max_tx_weight: usize, +) -> Result { + if let Some(confirmed_pool) = confirmed_pool + && let Ok(result) = select_coins( + confirmed_pool, + selection_target, + pay_fees, + cost_of_change, + coin_selection_algo, + max_tx_weight, + ) + { + return Ok(result); + } + + select_coins( + full_pool, + selection_target, + pay_fees, + cost_of_change, + coin_selection_algo, + max_tx_weight, + ) +} + fn select_random_coins( mut utxo_pool: Vec, selection_target: Amount, diff --git a/wallet/src/account/utxo_selector/tests.rs b/wallet/src/account/utxo_selector/tests.rs index 10b6f3ecd5..2784cd05e7 100644 --- a/wallet/src/account/utxo_selector/tests.rs +++ b/wallet/src/account/utxo_selector/tests.rs @@ -450,3 +450,70 @@ fn test_srd_solver_max_weight(#[case] seed: Seed) { assert_eq!(result.unwrap_err(), UtxoSelectorError::MaxWeightExceeded); } + +#[test] +fn test_prefer_confirmed_uses_confirmed_when_sufficient() { + // The confirmed pool alone can cover the target, so it must be used and the full pool + // (deliberately given a different, larger value) must be left untouched. + let mut confirmed = vec![]; + add_output(Amount::from_atoms(60), &mut confirmed); + let mut full = vec![]; + add_output(Amount::from_atoms(1000), &mut full); + + let result = select_coins_preferring_confirmed( + Some(confirmed), + full, + Amount::from_atoms(50), + PayFee::PayFeeWithThisCurrency, + Amount::ZERO, + CoinSelectionAlgo::Randomize, + 100, + ) + .unwrap(); + + assert_eq!(result.effective_value, Amount::from_atoms(60)); +} + +#[test] +fn test_prefer_confirmed_falls_back_when_confirmed_insufficient() { + // The confirmed pool can't reach the target on its own, so selection must fall back to the + // full pool, which additionally contains a larger (unconfirmed) utxo. + let mut confirmed = vec![]; + add_output(Amount::from_atoms(10), &mut confirmed); + let mut full = vec![]; + add_output(Amount::from_atoms(10), &mut full); + add_output(Amount::from_atoms(90), &mut full); + + let result = select_coins_preferring_confirmed( + Some(confirmed), + full, + Amount::from_atoms(50), + PayFee::PayFeeWithThisCurrency, + Amount::ZERO, + CoinSelectionAlgo::Randomize, + 100, + ) + .unwrap(); + + assert!(result.effective_value >= Amount::from_atoms(50)); +} + +#[test] +fn test_prefer_confirmed_without_preference_uses_full_pool() { + // With no confirmed pool the full pool is used directly. + let mut full = vec![]; + add_output(Amount::from_atoms(70), &mut full); + + let result = select_coins_preferring_confirmed( + None, + full, + Amount::from_atoms(50), + PayFee::PayFeeWithThisCurrency, + Amount::ZERO, + CoinSelectionAlgo::Randomize, + 100, + ) + .unwrap(); + + assert_eq!(result.effective_value, Amount::from_atoms(70)); +} From c1fb51274325f6814e98182c6d4c9ad50903a721 Mon Sep 17 00:00:00 2001 From: nullPointerEnjoyer Date: Tue, 17 Nov 2026 11:02:46 +0100 Subject: [PATCH 2/3] fix(wallet): keep the confirmed UTXO pool a subset of the full pool 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. --- CHANGELOG.md | 6 +++ wallet/src/account/mod.rs | 26 +++++++--- wallet/src/account/utxo_selector/mod.rs | 3 ++ wallet/src/account/utxo_selector/tests.rs | 63 +++++++++++++++++++++++ 4 files changed, 92 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d69fd82c03..d6d7659fe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/ ## [Unreleased] +### Changed + - Wallet: + - coin selection now prefers confirmed UTXOs, reducing the chance that a + send is rejected for forming an oversized mempool cluster; the full UTXO set + is still used when the confirmed UTXOs cannot cover the payment. + ## [1.4.0] - 2026-07-09 ### Added diff --git a/wallet/src/account/mod.rs b/wallet/src/account/mod.rs index 3765dc369a..3d45c2bb96 100644 --- a/wallet/src/account/mod.rs +++ b/wallet/src/account/mod.rs @@ -289,12 +289,26 @@ impl Account { // mempool cluster, which is size limited, so building on top of confirmed utxos // reduces the chance of producing a tx that the mempool would reject (see issue // #2066). The confirmed-only set is tried first, with a fallback to the full set. - let confirmed_utxos = self.get_utxos( - utxo_types, - median_time, - UtxoState::Confirmed.into(), - WithLocked::Unlocked, - ); + // + // The confirmed-only query filters by the state of the transaction that produced + // an utxo, and its state mask is also used to decide whether an utxo has already + // been consumed. That mask alone would therefore re-admit utxos of confirmed + // transactions that the wallet has already spent in its own pending (in-mempool) + // or evicted transactions. Intersecting with the full set keeps the confirmed + // pool a subset of it, so such utxos stay excluded from selection. + let confirmed_utxos = { + let full_outpoints: BTreeSet<_> = + utxos.iter().map(|(outpoint, _)| outpoint.clone()).collect(); + self.get_utxos( + utxo_types, + median_time, + UtxoState::Confirmed.into(), + WithLocked::Unlocked, + ) + .into_iter() + .filter(|(outpoint, _)| full_outpoints.contains(outpoint)) + .collect() + }; ( utxos, Some(confirmed_utxos), diff --git a/wallet/src/account/utxo_selector/mod.rs b/wallet/src/account/utxo_selector/mod.rs index c38c8f142f..84d7909d3d 100644 --- a/wallet/src/account/utxo_selector/mod.rs +++ b/wallet/src/account/utxo_selector/mod.rs @@ -745,6 +745,9 @@ pub fn select_coins( /// /// `confirmed_pool` is `None` when there is no preference to apply, for example when the caller /// selected the inputs explicitly; in that case `full_pool` is used directly. +/// +/// This deliberately ignores waste: a selection from the confirmed pool is used even when the +/// full pool admits a cheaper one. pub fn select_coins_preferring_confirmed( confirmed_pool: Option>, full_pool: Vec, diff --git a/wallet/src/account/utxo_selector/tests.rs b/wallet/src/account/utxo_selector/tests.rs index 2784cd05e7..5465642d54 100644 --- a/wallet/src/account/utxo_selector/tests.rs +++ b/wallet/src/account/utxo_selector/tests.rs @@ -23,6 +23,24 @@ use test_utils::random::{Seed, make_seedable_rng}; use super::*; +fn add_output_with_weight(value: Amount, weight: usize, groups: &mut Vec) { + let outpoint = UtxoOutPoint::new( + OutPointSourceId::BlockReward(Id::::new(Uint256::from_u64(1).into())), + 0, + ); + let tx_output = TxOutput::Transfer(OutputValue::Coin(value), Destination::AnyoneCanSpend); + + let outputs = vec![(outpoint.into(), tx_output)]; + + groups.push(OutputGroup { + outputs, + value, + fee: Amount::ZERO, + long_term_fee: Amount::ZERO, + weight, + }); +} + fn add_output(value: Amount, groups: &mut Vec) { let outpoint = UtxoOutPoint::new( OutPointSourceId::BlockReward(Id::::new(Uint256::from_u64(1).into())), @@ -517,3 +535,48 @@ fn test_prefer_confirmed_without_preference_uses_full_pool() { assert_eq!(result.effective_value, Amount::from_atoms(70)); } + +#[test] +fn test_prefer_confirmed_falls_back_when_confirmed_exceeds_weight_cap() { + // Every confirmed utxo is too heavy for the weight cap, so selection from the + // confirmed pool fails; the full pool's lighter utxos cover the target. + let mut confirmed = vec![]; + add_output_with_weight(Amount::from_atoms(100), 2, &mut confirmed); + let mut full = vec![]; + add_output_with_weight(Amount::from_atoms(60), 1, &mut full); + + let result = select_coins_preferring_confirmed( + Some(confirmed), + full, + Amount::from_atoms(50), + PayFee::PayFeeWithThisCurrency, + Amount::ZERO, + CoinSelectionAlgo::Randomize, + 1, + ) + .unwrap(); + + // The exact full-pool value proves the fallback produced this selection. + assert_eq!(result.effective_value, Amount::from_atoms(60)); +} + +#[test] +fn test_prefer_confirmed_falls_back_on_empty_confirmed_bucket() { + // An empty confirmed bucket (no confirmed utxos of this currency) is not a + // preference failure: selection falls back to the full pool. + let mut full = vec![]; + add_output(Amount::from_atoms(70), &mut full); + + let result = select_coins_preferring_confirmed( + Some(vec![]), + full, + Amount::from_atoms(50), + PayFee::PayFeeWithThisCurrency, + Amount::ZERO, + CoinSelectionAlgo::Randomize, + 100, + ) + .unwrap(); + + assert_eq!(result.effective_value, Amount::from_atoms(70)); +} From c761856eb02899c9c020b70bd1d67793cf286e5e Mon Sep 17 00:00:00 2001 From: Oliver Anyanwu <113263811+oliv3rdrt@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:17:28 +0100 Subject: [PATCH 3/3] Restore CHANGELOG entries dropped in the master merge 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. --- CHANGELOG.md | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37c42d6573..847cab4cfd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,11 +17,109 @@ The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/ as the target is met, and among equally economical solutions the one spending fewer inputs is preferred, so default sends spend fewer UTXOs and produce smaller change. - - coin selection now prefers confirmed UTXOs, reducing the chance that a send is rejected for forming an oversized mempool cluster; the full UTXO set is still used when the confirmed UTXOs cannot cover the payment. + - Node: + - `node-daemon` and `node-gui`: the "reckless" mode of the chainstate database is now + enabled by default during the initial block download or bootstrapping, which + significantly increases its speed at the cost of a potential db corruption if the + system crashes in the meantime; the node automatically switches back to the normal + mode of operation when the download/bootstrapping finishes. + + - Node CLI: + - boolean node options now require an explicit `true`/`false` value (e.g. + `--enable-db-reckless-mode-in-ibd true`) instead of working as value-less flags; the + corresponding environment variables (`ML_NODE_*` for top-level options and + `ML_MAINNET_NODE_*` / `ML_TESTNET_NODE_*` / `ML_REGTEST_NODE_*` for per-network ones) + likewise now require an explicit `true`/`false` value. This makes the absence of an + option distinguishable from an explicit opt-out, so a value from the configuration + file is no longer silently overridden by the command line or the environment. + corresponding `ML_NODE_*` environment variables likewise now require an explicit + `true`/`false` value. This makes the absence of an option distinguishable from an + explicit opt-out, so a value from the configuration file is no longer silently + overridden by the command line or the environment. + `--enable-db-reckless-mode-in-ibd true`) instead of working as value-less flags; this + makes the absence of an option distinguishable from an explicit opt-out, so a value + from the configuration file is no longer silently overridden by the command line. +## [1.4.5] - 2026-11-10 + +### Added + - Node: + - Per-peer fork download budget in the block sync manager: after initial + sync, each peer may download a bounded number of header lists per refill + interval (defaults: 2000 downloads, 600 seconds), tip-anchored lists + included and initial sync exempt. Requests beyond the budget are deferred + without punishment and retried at the next refill, which bounds the cost + of adversarial fork announcements. + + - Wallet: + - A read-only MCP server for the wallet RPC daemon (new `mcp-server` crate, + stdio transport), allowing MCP clients to inspect wallets. The server + validates the MCP protocol envelope and tool arguments, bounds requests + with a timeout, and resynchronizes the stream after an oversized message. + + - Wallet CLI: + - Commands are reorganized into a nested grammar (for example + `wallet create`, `staking pool create`). The previous flat command names + keep working, but are hidden from help and tab completion. + - Command results carry a machine-readable payload alongside the + human-readable text; `wallet-cli exec` JSON output reflects the command + payload. + - Interactive wizards for complex commands, gated on an interactive terminal. + - Destructive commands go through a confirmation gate. + - The choice to store or only display the seed phrase at wallet creation is + now an optional flag. + - Block generation asks for confirmation first. + +### Security + - Wallet: + - Password file handling hardened: the file size cap is enforced before + reads, the size race between stat and read is closed, and unverifiable + password sources fail closed instead of falling back silently. + +## [1.4.4] - 2026-10-31 + +### Added + - Node: + - The orphan pool now enforces a per-peer capacity limit for remote + orphans: peers reached through outbound connections get a larger orphan + quota, eviction honors per-peer reservations, occupancy is tracked per + origin, and the new quota fields are exposed through the mempool + config RPC. + +### Fixed + - Node: + - Orphan pool hardening: stale orphans are rejected before insertion and + expiry takes priority over capacity during admission, the + first-recorded origin class stays authoritative, the outbound tier is + honored by eviction reservations, quota configuration is validated, + and zero-count origin statistics are purged on teardown. + - Wallet: + - Remote orphan quota rejections are classified as transient errors + instead of permanent failures. + +### Security + - Node: + - Advisory-exposed dependencies were patched (rustls-webpki, + webpki-root-certs, tokio) and the cargo-vet imports refreshed. + +## [1.4.1] - 2026-09-25 + +### Added + - Node: + - Added PoS seal indexing infrastructure: duplicate-proof evidence storage for + block seals, active by default, with a chainstate configuration option to disable it. + +### Fixed + - Wallet: + - Fixed an issue where `standalone-add-watch-only-address`, `standalone-add-private-key-from-hex` and + `standalone-add-multisig` (and the corresponding RPC methods) would reset the wallet to genesis but never + perform the announced rescan; the rescan is now done before the command returns. Previously, in `wallet-cli` + non-interactive mode the process would exit before the background rescan could run, leaving the wallet + stuck at genesis so that every subsequent wallet open had to rescan the whole blockchain. + ## [1.4.0] - 2026-07-09 ### Added