From f10723688fc9def5cda5879863f2b89b6aa37c77 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 25 Sep 2026 14:46:22 +0300 Subject: [PATCH 001/169] fix(platform-wallet): classify Core's broadcast verdicts and plan the abandon of a proven-dead chain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A send whose SPV broadcast sees no acceptance signal stays MaybeSent for good when the transaction can never land (its input does not exist or is already spent): P2P has no reject message, so nothing ever ends it. Core's sendrawtransaction, which DAPI's broadcastTransaction wraps, does answer. Measured on Dash Core 22.1.3: already in the mempool -> success; in a block -> -27; missing input -> -25 bad-txns-inputs-missingorspent; input spent by an IS-locked tx -> -26 tx-txlock-conflict. - broadcast_probe: classify a failed broadcastTransaction by Core's reason text (the JS and Rust DAPI servers surface -25 under different gRPC codes), and an AcceptanceProbe that resubmits the same signed bytes and calls a transaction dead only when two distinct nodes prove it. - DapiBroadcaster uses the same classifier: AlreadyExists is success, a proven-dead or refused transaction is Rejected, everything else stays MaybeSent. The error codes involved are non-retryable in dapi-client, so the verdict is always the answering node's own. - abandon_plan: the chain to remove (root plus unsettled descendants) and the SweepBatch that mirrors it to the persisters through the existing sweep channel. Every input the chain took from outside itself is released, so nothing is held under `superseded_by` (the root). The plan refuses when a transaction outside the chain still claims one of those inputs, or a settled transaction spends the chain's output — the only shapes that could leave a hold attributed to a transaction that no longer exists. - Persister regression tests (SQLite, SwiftData) for that batch: the real coins come back unspent, the chain's change is deleted, and no row or placeholder is held under the root. Both fail when an input is held. Ticket 32347. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../tests/sqlite_transaction_sweeps.rs | 143 ++++++ .../rs-platform-wallet/src/broadcast_probe.rs | 480 ++++++++++++++++++ .../rs-platform-wallet/src/broadcaster.rs | 64 ++- packages/rs-platform-wallet/src/lib.rs | 1 + .../src/wallet/core/abandon_plan.rs | 359 +++++++++++++ .../rs-platform-wallet/src/wallet/core/mod.rs | 1 + .../SweptTransactionPersistTests.swift | 83 +++ 7 files changed, 1112 insertions(+), 19 deletions(-) create mode 100644 packages/rs-platform-wallet/src/broadcast_probe.rs create mode 100644 packages/rs-platform-wallet/src/wallet/core/abandon_plan.rs diff --git a/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs b/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs index 6a7bbc1361d..e54ea424686 100644 --- a/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs +++ b/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs @@ -3823,3 +3823,146 @@ fn should_remove_a_swept_height_only_transaction_and_its_outputs() { .unwrap(); assert_eq!(count, 0); } + +/// The batch `abandon_plan` builds for a chain the network proved dead +/// (rust-dashcore#961's abandon, mirrored through this same channel): every +/// input the chain took from outside itself is released, `superseded_by` is +/// the chain's own root, and there is no winner height. Nothing may be held — +/// a hold would be attributed to the root, a transaction that no longer +/// exists, and nothing could ever resolve it. +/// +/// Shape of ticket 32347: root R spends a real coin F:0 and leaves change +/// R:1; child C spends that change plus another real coin F:1. +#[test] +fn an_abandoned_chain_frees_its_outside_inputs_and_holds_nothing() { + let (persister, _tmp, _path) = fresh_persister(); + let w: WalletId = wid(0xE9); + ensure_wallet_meta(&persister, &w); + + let addr = p2pkh(0x09); + let funding_txid = Txid::from_byte_array([0x90; 32]); + let real_a = OutPoint::new(funding_txid, 0); + let real_b = OutPoint::new(funding_txid, 1); + + let root_txid = Txid::from_byte_array([0x91; 32]); + let root_change = OutPoint::new(root_txid, 1); + let child_txid = Txid::from_byte_array([0x92; 32]); + let child_change = OutPoint::new(child_txid, 1); + + let pay_elsewhere = TxOut { + value: 100, + script_pubkey: p2pkh(0xEE).script_pubkey(), + }; + let root = tx_record( + root_txid, + vec![real_a], + vec![ + pay_elsewhere.clone(), + TxOut { + value: 300, + script_pubkey: addr.script_pubkey(), + }, + ], + ); + let child = tx_record( + child_txid, + vec![root_change, real_b], + vec![ + pay_elsewhere, + TxOut { + value: 600, + script_pubkey: addr.script_pubkey(), + }, + ], + ); + + let mut conn = persister.lock_conn_for_test(); + derive_address(&conn, &w, 0, &addr); + { + let tx = conn.transaction().unwrap(); + let cs = CoreChangeSet { + new_utxos: vec![ + make_utxo(&addr, funding_txid, 0, 500), + make_utxo(&addr, funding_txid, 1, 400), + ], + ..Default::default() + }; + core_state::apply(&tx, &w, &cs).unwrap(); + tx.commit().unwrap(); + } + { + let tx = conn.transaction().unwrap(); + let cs = CoreChangeSet { + records: vec![root, child], + new_utxos: vec![ + make_utxo(&addr, root_txid, 1, 300), + make_utxo(&addr, child_txid, 1, 600), + ], + spent_utxos: vec![ + make_utxo(&addr, funding_txid, 0, 500), + make_utxo(&addr, root_txid, 1, 300), + make_utxo(&addr, funding_txid, 1, 400), + ], + ..Default::default() + }; + core_state::apply(&tx, &w, &cs).unwrap(); + tx.commit().unwrap(); + } + // Sanity: the phantom state the customer's wallet was in — the real coins + // spent, the chain's final change counted as money. + assert_eq!(unspent(&conn, &w), std::collections::BTreeSet::from([child_change])); + + { + let tx = conn.transaction().unwrap(); + let cs = CoreChangeSet { + sweeps: vec![SweepBatch { + txids: vec![root_txid, child_txid], + superseded_by: root_txid, + winner_mined_height: None, + released_outpoints: vec![real_a, real_b], + }], + ..Default::default() + }; + core_state::apply(&tx, &w, &cs).unwrap(); + tx.commit().unwrap(); + } + + assert_eq!( + unspent(&conn, &w), + std::collections::BTreeSet::from([real_a, real_b]), + "exactly the real coins come back; the chain's change is gone" + ); + assert!(!row_exists(&conn, &w, &root_change), "root change must be deleted"); + assert!(!row_exists(&conn, &w, &child_change), "child change must be deleted"); + + let placeholders: i64 = conn + .query_row( + "SELECT COUNT(*) FROM core_utxos WHERE wallet_id = ?1 AND is_sweep_placeholder = 1", + params![w.as_slice()], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(placeholders, 0, "an abandon must never create a held placeholder"); + + let held_by_root: i64 = conn + .query_row( + "SELECT COUNT(*) FROM core_utxos WHERE wallet_id = ?1 AND spent_in_txid = ?2", + params![w.as_slice(), AsRef::<[u8]>::as_ref(&root_txid)], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(held_by_root, 0, "no row may be held under the abandoned root"); + + let chain_rows: i64 = conn + .query_row( + "SELECT COUNT(*) FROM core_transactions WHERE wallet_id = ?1 AND txid IN (?2, ?3)", + params![ + w.as_slice(), + AsRef::<[u8]>::as_ref(&root_txid), + AsRef::<[u8]>::as_ref(&child_txid) + ], + |row| row.get(0), + ) + .unwrap(); + assert_eq!(chain_rows, 0, "both transaction rows must be deleted"); +} diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs new file mode 100644 index 00000000000..aea98742a0c --- /dev/null +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -0,0 +1,480 @@ +//! Asking Core nodes directly whether a signed transaction can still land. +//! +//! The P2P network has no negative signal — modern Dash Core removed BIP61 +//! `reject` — so a transaction whose SPV broadcast saw no acceptance signal +//! (`BroadcastResult::Uncertain` → [`BroadcastError::MaybeSent`]) stays +//! ambiguous for as long as nobody asks. For a valid transaction that is the +//! right answer: it may still be relayed and mined. For one that can never +//! land — its input does not exist, or is already spent by a final +//! transaction — nothing ends the ambiguity: no block revisits it, dash-spv +//! keeps rebroadcasting it, and its inputs stay fenced indefinitely. +//! +//! Core's `sendrawtransaction`, which DAPI's `broadcastTransaction` wraps, is +//! not silent. Measured against Dash Core 22.1.3 (testnet and mainnet, +//! 2026-09-24): +//! +//! | transaction | Core answer | +//! |-----------------------------------------------|-------------------------------------------| +//! | already in the mempool | success, the txid | +//! | already in a block | `-27 Transaction already in block chain` | +//! | spends an input that does not exist | `-25 bad-txns-inputs-missingorspent` | +//! | spends an input an IS-locked tx already spent | `-26 tx-txlock-conflict` | +//! +//! The two DAPI implementations surface those codes differently — the JS +//! server maps `-25` to `InvalidArgument`, rs-dapi to `FailedPrecondition` — +//! so the classification keys on Core's reason text, which both pass through +//! in the status message, and on `AlreadyExists` for `-27`, which both agree +//! on. +//! +//! [`BroadcastError::MaybeSent`]: crate::broadcaster::BroadcastError::MaybeSent + +use std::collections::HashSet; + +use async_trait::async_trait; +use dashcore::Transaction; + +/// Core reject reasons that prove the transaction can never be mined as it +/// stands. +/// +/// - `bad-txns-inputs-missingorspent`: an input is neither in the UTXO set +/// nor created by anything in this node's mempool. `Missing inputs` is the +/// older spelling of the same check. +/// - `tx-txlock-conflict`: an input is already spent by an InstantSend-locked +/// transaction, which is final. +/// +/// `-25` has two false positives that the *caller* must rule out — a node +/// that has not yet seen one of our own unconfirmed parents, and a node +/// lagging behind the tip. See [`AcceptanceProbe`] for how the probe guards +/// against the second, and the wallet-side resolver for the first. +const DEAD_REASONS: &[&str] = &[ + "bad-txns-inputs-missingorspent", + "missing inputs", + "tx-txlock-conflict", +]; + +/// Core reject reasons that say this node refused the transaction without +/// proving it can never land: `txn-mempool-conflict` means another, +/// unconfirmed and not IS-locked, transaction spends the same input, and +/// either one may still be mined. +const REFUSED_REASONS: &[&str] = &["txn-mempool-conflict"]; + +/// What a single Core node said when handed a signed transaction. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum NodeVerdict { + /// The node holds the transaction — it accepted it into its mempool now, + /// already had it there, or has it in a block. + Accepted, + /// The node proved the transaction can never be mined as it stands. + Dead { reason: String }, + /// The node refused the transaction without proving it dead. + Refused { reason: String }, + /// No verdict: transport failure, timeout, or an unrecognised rejection. + Unknown { reason: String }, +} + +/// Classify a failed `broadcastTransaction` gRPC response. +pub(crate) fn classify_failed_submission( + code: dash_sdk::dapi_grpc::tonic::Code, + message: &str, +) -> NodeVerdict { + use dash_sdk::dapi_grpc::tonic::Code; + + if code == Code::AlreadyExists { + return NodeVerdict::Accepted; + } + let lowered = message.to_ascii_lowercase(); + if DEAD_REASONS.iter().any(|reason| lowered.contains(reason)) { + return NodeVerdict::Dead { + reason: message.to_string(), + }; + } + if REFUSED_REASONS + .iter() + .any(|reason| lowered.contains(reason)) + { + return NodeVerdict::Refused { + reason: message.to_string(), + }; + } + NodeVerdict::Unknown { + reason: format!("{code:?}: {message}"), + } +} + +/// The answer a probe gives about one transaction. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ProbeVerdict { + /// A node holds the transaction: it is in a mempool or a block. + Accepted, + /// Two different nodes proved the transaction can never be mined. + Dead { reason: String }, + /// Not enough evidence either way; the transaction stays ambiguous. + Unresolved { reason: String }, +} + +/// Submits a signed transaction to one Core node and reports which node +/// answered and what it said. A seam so the probe's evidence rules can be +/// tested without a network. +#[async_trait] +pub(crate) trait NodeSubmitter: Send + Sync { + async fn submit(&self, transaction: &Transaction) -> (Option, NodeVerdict); +} + +/// How many submissions one probe may spend looking for a verdict. +const MAX_SUBMISSIONS_PER_PROBE: usize = 4; + +/// How many *distinct* nodes must independently prove a transaction dead. +/// +/// One node is not enough: a node lagging behind the tip has not seen a +/// recently confirmed parent and answers `missingorspent` for a transaction +/// that is perfectly valid. Two unrelated evonodes lagging on the same parent +/// at the same time is far less likely, and the cost of waiting for a second +/// opinion is one more request. +const DEAD_QUORUM: usize = 2; + +/// Resubmits a transaction whose broadcast outcome is unknown and turns the +/// nodes' answers into a verdict. +/// +/// Resubmitting is safe: it is the same signed bytes, so a node either takes +/// a transaction it did not have — an extra delivery route — or tells us why +/// it will not. It can never create a second payment. +#[async_trait] +pub trait AcceptanceProbe: Send + Sync + 'static { + async fn probe(&self, transaction: &Transaction) -> ProbeVerdict; +} + +/// Evidence rules shared by every probe: stop at the first node that holds +/// the transaction, call it dead only when [`DEAD_QUORUM`] distinct nodes say +/// so, and give up after [`MAX_SUBMISSIONS_PER_PROBE`] submissions. +pub(crate) async fn probe_with( + submitter: &dyn NodeSubmitter, + transaction: &Transaction, +) -> ProbeVerdict { + let mut dead_nodes: HashSet = HashSet::new(); + let mut dead_reason: Option = None; + let mut last_other = String::from("no submission was answered"); + + for _ in 0..MAX_SUBMISSIONS_PER_PROBE { + let (node, verdict) = submitter.submit(transaction).await; + match verdict { + NodeVerdict::Accepted => return ProbeVerdict::Accepted, + NodeVerdict::Dead { reason } => { + // A dead verdict from an unidentified node cannot count + // towards the quorum — it might be the node we already heard. + if let Some(node) = node { + dead_nodes.insert(node); + } + dead_reason.get_or_insert(reason); + if dead_nodes.len() >= DEAD_QUORUM { + return ProbeVerdict::Dead { + reason: dead_reason.unwrap_or_default(), + }; + } + } + NodeVerdict::Refused { reason } | NodeVerdict::Unknown { reason } => { + last_other = reason; + } + } + } + + ProbeVerdict::Unresolved { + reason: match dead_reason { + Some(reason) => format!( + "{} of {DEAD_QUORUM} nodes needed proved it dead ({reason}); last other answer: {last_other}", + dead_nodes.len() + ), + None => last_other, + }, + } +} + +/// Submits through DAPI's `broadcastTransaction` — a Core `sendrawtransaction` +/// on a random evonode per request. +pub(crate) struct DapiNodeSubmitter { + sdk: std::sync::Arc, +} + +#[async_trait] +impl NodeSubmitter for DapiNodeSubmitter { + async fn submit(&self, transaction: &Transaction) -> (Option, NodeVerdict) { + use dash_sdk::dapi_client::transport::TransportError; + use dash_sdk::dapi_client::{DapiClientError, DapiRequestExecutor, RequestSettings}; + use dash_sdk::dapi_grpc::core::v0::BroadcastTransactionRequest; + + let request = BroadcastTransactionRequest { + transaction: dashcore::consensus::serialize(transaction), + allow_high_fees: false, + bypass_limits: false, + }; + // One node per submission: the probe decides whether to ask another, + // and it has to know which node each answer came from. + let settings = RequestSettings { + retries: Some(0), + ..RequestSettings::default() + }; + + match self.sdk.execute(request, settings).await { + Ok(response) => (Some(response.address.to_string()), NodeVerdict::Accepted), + Err(error) => { + let node = error.address.as_ref().map(ToString::to_string); + let verdict = match &error.inner { + DapiClientError::Transport(TransportError::Grpc(status)) => { + classify_failed_submission(status.code(), status.message()) + } + other => NodeVerdict::Unknown { + reason: other.to_string(), + }, + }; + (node, verdict) + } + } + } +} + +/// The production probe: resubmits through DAPI. +pub struct DapiAcceptanceProbe { + submitter: DapiNodeSubmitter, +} + +impl DapiAcceptanceProbe { + pub fn new(sdk: std::sync::Arc) -> Self { + Self { + submitter: DapiNodeSubmitter { sdk }, + } + } +} + +#[async_trait] +impl AcceptanceProbe for DapiAcceptanceProbe { + async fn probe(&self, transaction: &Transaction) -> ProbeVerdict { + probe_with(&self.submitter, transaction).await + } +} + +#[cfg(test)] +mod tests { + use std::collections::VecDeque; + use std::sync::Mutex; + + use dash_sdk::dapi_grpc::tonic::Code; + + use super::*; + + fn transaction() -> Transaction { + Transaction { + version: 1, + lock_time: 0, + input: Vec::new(), + output: Vec::new(), + special_transaction_payload: None, + } + } + + /// Replays scripted node answers in order and counts submissions. + struct ScriptedNodes { + answers: Mutex, NodeVerdict)>>, + submissions: Mutex, + } + + impl ScriptedNodes { + fn new(answers: Vec<(Option<&'static str>, NodeVerdict)>) -> Self { + Self { + answers: Mutex::new(answers.into()), + submissions: Mutex::new(0), + } + } + + fn submissions(&self) -> usize { + *self.submissions.lock().expect("submissions") + } + } + + #[async_trait] + impl NodeSubmitter for ScriptedNodes { + async fn submit(&self, _transaction: &Transaction) -> (Option, NodeVerdict) { + *self.submissions.lock().expect("submissions") += 1; + let (node, verdict) = self + .answers + .lock() + .expect("answers") + .pop_front() + .unwrap_or(( + None, + NodeVerdict::Unknown { + reason: "script exhausted".to_string(), + }, + )); + (node.map(str::to_string), verdict) + } + } + + fn dead() -> NodeVerdict { + NodeVerdict::Dead { + reason: "bad-txns-inputs-missingorspent".to_string(), + } + } + + fn unknown() -> NodeVerdict { + NodeVerdict::Unknown { + reason: "Unavailable: connection refused".to_string(), + } + } + + #[test] + fn should_classify_core_answers_the_way_both_dapi_servers_surface_them() { + let cases = [ + // -27, both servers. + ( + Code::AlreadyExists, + "Transaction already in chain: Transaction already in block chain", + NodeVerdict::Accepted, + ), + // -25, JS dapi. + ( + Code::InvalidArgument, + "invalid transaction: bad-txns-inputs-missingorspent", + dead(), + ), + // -25, rs-dapi. + ( + Code::FailedPrecondition, + "Transaction is rejected: bad-txns-inputs-missingorspent", + dead(), + ), + // -26 against an IS-locked spend: final. + ( + Code::FailedPrecondition, + "Transaction is rejected: tx-txlock-conflict", + NodeVerdict::Dead { + reason: "Transaction is rejected: tx-txlock-conflict".to_string(), + }, + ), + // -26 against an unconfirmed spend: not final. + ( + Code::FailedPrecondition, + "Transaction is rejected: txn-mempool-conflict", + NodeVerdict::Refused { + reason: "Transaction is rejected: txn-mempool-conflict".to_string(), + }, + ), + ]; + + for (code, message, expected) in cases { + let verdict = classify_failed_submission(code, message); + match (&verdict, &expected) { + (NodeVerdict::Dead { .. }, NodeVerdict::Dead { .. }) => {} + _ => assert_eq!(verdict, expected, "{code:?} {message}"), + } + } + } + + #[test] + fn should_leave_transport_failures_and_unknown_rejections_without_a_verdict() { + for (code, message) in [ + (Code::Unavailable, "connection refused"), + (Code::DeadlineExceeded, "timeout"), + ( + Code::InvalidArgument, + "invalid transaction: TX decode failed", + ), + (Code::Internal, "bad-txns-in-belowout"), + ] { + assert!( + matches!( + classify_failed_submission(code, message), + NodeVerdict::Unknown { .. } + ), + "{code:?} {message}" + ); + } + } + + #[tokio::test] + async fn should_accept_as_soon_as_any_node_holds_the_transaction() { + let nodes = ScriptedNodes::new(vec![ + (Some("a"), dead()), + (Some("b"), NodeVerdict::Accepted), + ]); + + assert_eq!( + probe_with(&nodes, &transaction()).await, + ProbeVerdict::Accepted + ); + assert_eq!(nodes.submissions(), 2); + } + + #[tokio::test] + async fn should_call_a_transaction_dead_only_when_two_distinct_nodes_prove_it() { + let nodes = ScriptedNodes::new(vec![(Some("a"), dead()), (Some("b"), dead())]); + + assert!(matches!( + probe_with(&nodes, &transaction()).await, + ProbeVerdict::Dead { .. } + )); + assert_eq!(nodes.submissions(), 2); + } + + /// A lagging node answers `missingorspent` for a valid transaction. Hearing + /// the same node twice must not look like two opinions. + #[tokio::test] + async fn should_not_count_the_same_node_twice_towards_the_dead_quorum() { + let nodes = ScriptedNodes::new(vec![ + (Some("a"), dead()), + (Some("a"), dead()), + (Some("a"), dead()), + (Some("a"), dead()), + ]); + + assert!(matches!( + probe_with(&nodes, &transaction()).await, + ProbeVerdict::Unresolved { .. } + )); + assert_eq!(nodes.submissions(), MAX_SUBMISSIONS_PER_PROBE); + } + + #[tokio::test] + async fn should_not_count_an_unidentified_node_towards_the_dead_quorum() { + let nodes = ScriptedNodes::new(vec![(Some("a"), dead()), (None, dead())]); + + assert!(matches!( + probe_with(&nodes, &transaction()).await, + ProbeVerdict::Unresolved { .. } + )); + } + + #[tokio::test] + async fn should_stay_unresolved_when_nodes_only_refuse_or_fail() { + let nodes = ScriptedNodes::new(vec![ + ( + Some("a"), + NodeVerdict::Refused { + reason: "txn-mempool-conflict".to_string(), + }, + ), + (Some("b"), unknown()), + (Some("c"), unknown()), + (Some("d"), unknown()), + ]); + + assert!(matches!( + probe_with(&nodes, &transaction()).await, + ProbeVerdict::Unresolved { .. } + )); + assert_eq!(nodes.submissions(), MAX_SUBMISSIONS_PER_PROBE); + } + + #[tokio::test] + async fn should_reach_the_quorum_across_intervening_failures() { + let nodes = ScriptedNodes::new(vec![ + (Some("a"), dead()), + (Some("b"), unknown()), + (Some("c"), dead()), + ]); + + assert!(matches!( + probe_with(&nodes, &transaction()).await, + ProbeVerdict::Dead { .. } + )); + assert_eq!(nodes.submissions(), 3); + } +} diff --git a/packages/rs-platform-wallet/src/broadcaster.rs b/packages/rs-platform-wallet/src/broadcaster.rs index dbbbf6ead04..18a942955e8 100644 --- a/packages/rs-platform-wallet/src/broadcaster.rs +++ b/packages/rs-platform-wallet/src/broadcaster.rs @@ -9,8 +9,9 @@ //! announcing the txid back, an InstantSend lock, or a confirmation proves //! the network accepted it. Trustless; no DAPI involvement. //! - [`DapiBroadcaster`] (fallback for wallets without an SPV runtime): -//! submission via DAPI's gRPC endpoint, with every failure conservatively -//! classified as [`BroadcastError::MaybeSent`]. +//! submission via DAPI's gRPC endpoint. Core's own verdict decides the +//! outcome where the node gave one (see [`crate::broadcast_probe`]); every +//! other failure is conservatively [`BroadcastError::MaybeSent`]. use std::sync::Arc; use std::time::Duration; @@ -117,10 +118,15 @@ impl DapiBroadcaster { #[async_trait] impl TransactionBroadcaster for DapiBroadcaster { async fn broadcast(&self, transaction: &Transaction) -> Result { - use dash_sdk::dapi_client::{DapiRequestExecutor, IntoInner, RequestSettings}; + use dash_sdk::dapi_client::transport::TransportError; + use dash_sdk::dapi_client::{ + DapiClientError, DapiRequestExecutor, IntoInner, RequestSettings, + }; use dash_sdk::dapi_grpc::core::v0::BroadcastTransactionRequest; use dashcore::consensus; + use crate::broadcast_probe::{classify_failed_submission, NodeVerdict}; + let tx_bytes = consensus::serialize(transaction); let request = BroadcastTransactionRequest { @@ -129,27 +135,47 @@ impl TransactionBroadcaster for DapiBroadcaster { bypass_limits: false, }; - // Every DAPI failure is classified `MaybeSent`: `sdk.execute` retries - // across nodes internally (RequestSettings::default()), so the error - // surfaced here is only the *last* attempt's — an earlier attempt may - // have delivered the transaction even though the response was lost - // (the classic shape being a node that accepts the tx while its gRPC - // response times out, followed by a retry that fails differently). - // Distinguishing a genuinely pre-send rejection would require - // disabling the internal retries and inspecting transport errors; - // until then the conservative classification keeps reserved inputs - // safe from double-spends at the cost of holding them for the - // reservation TTL. - let _response = self + // `sdk.execute` retries across nodes internally, but only on transport + // failures: `AlreadyExists`, `InvalidArgument` and `FailedPrecondition` + // are non-retryable, so when one of them surfaces it is the answer of + // the node that evaluated the transaction. That makes Core's own + // verdict usable (see `broadcast_probe`): `AlreadyExists` means the + // transaction is on chain, and a proven-dead or refused transaction + // did not enter this node's mempool — its reserved inputs are safe to + // release, since a rebuild re-selecting them conflicts with, rather + // than adds to, any copy an earlier attempt may have delivered. + // Everything else — transport errors, timeouts, unrecognised + // rejections — stays `MaybeSent`, because an earlier attempt may have + // delivered the transaction before the response was lost. + let error = match self .sdk .execute(request, RequestSettings::default()) .await .into_inner() - .map_err(|e| BroadcastError::MaybeSent { - reason: format!("DAPI broadcast failed: {}", e), - })?; + { + Ok(_response) => return Ok(transaction.txid()), + Err(error) => error, + }; - Ok(transaction.txid()) + let verdict = match &error { + DapiClientError::Transport(TransportError::Grpc(status)) => { + classify_failed_submission(status.code(), status.message()) + } + _ => NodeVerdict::Unknown { + reason: error.to_string(), + }, + }; + match verdict { + NodeVerdict::Accepted => Ok(transaction.txid()), + NodeVerdict::Dead { reason } | NodeVerdict::Refused { reason } => { + Err(BroadcastError::Rejected { + reason: format!("DAPI broadcast rejected: {reason}"), + }) + } + NodeVerdict::Unknown { .. } => Err(BroadcastError::MaybeSent { + reason: format!("DAPI broadcast failed: {error}"), + }), + } } } diff --git a/packages/rs-platform-wallet/src/lib.rs b/packages/rs-platform-wallet/src/lib.rs index 2fedc74a1a8..40da468297b 100644 --- a/packages/rs-platform-wallet/src/lib.rs +++ b/packages/rs-platform-wallet/src/lib.rs @@ -14,6 +14,7 @@ pub mod address_paths; pub(crate) mod broadcast_outcome; +pub mod broadcast_probe; pub mod broadcaster; pub mod changeset; pub mod error; diff --git a/packages/rs-platform-wallet/src/wallet/core/abandon_plan.rs b/packages/rs-platform-wallet/src/wallet/core/abandon_plan.rs new file mode 100644 index 00000000000..a49dfb79c28 --- /dev/null +++ b/packages/rs-platform-wallet/src/wallet/core/abandon_plan.rs @@ -0,0 +1,359 @@ +//! Planning the removal of an outgoing transaction the network proved dead. +//! +//! A transaction the network will never accept still mutated the wallet when +//! it was sent: its change was credited and its inputs marked spent. Once a +//! probe (see [`crate::broadcast_probe`]) proves it dead, the wallet drops it +//! and every transaction built on its change — key-wallet's +//! `abandon_transaction_with_spends` (rust-dashcore#961) — and the persisters +//! must drop the same rows, or the next load hands the phantom change back. +//! +//! The removal reaches the persisters as an ordinary [`SweepBatch`], the +//! channel they already apply for conflict sweeps (#4558/#4559/#4589): the +//! batch's `txids` rows and every UTXO they created are deleted, and its +//! `released_outpoints` are handed back as unspent. A sweep normally names the +//! transaction that beat the losers in `superseded_by`, and persisters hold +//! every input *not* released under that winner. An abandon has no winner, so +//! the plan releases **every** input the chain took from outside itself and +//! `superseded_by` names the root — a label no persister reads, because a +//! released input is never held. +//! +//! That is only true while no other transaction of this wallet still claims +//! one of those inputs. If one did, a persister would keep that input held and +//! attribute the hold to `superseded_by` — here the root, a transaction that no +//! longer exists — leaving a hold nothing could ever resolve. The plan +//! therefore refuses to abandon in that case. It is not a case the probe +//! should produce anyway: a claimant already in a block or IS-locked makes +//! key-wallet sweep the chain itself, with the real winner, before any probe +//! runs; a claimant still in the mempool makes Core answer +//! `txn-mempool-conflict`, which the probe reports as refused, not dead. + +use std::collections::{BTreeMap, BTreeSet}; + +use dashcore::{OutPoint, Txid}; + +use crate::changeset::changeset::SweepBatch; + +/// What the planner needs to know about one recorded transaction. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct RecordView { + pub txid: Txid, + pub inputs: Vec, + /// In a block or InstantSend-locked. A settled transaction spent real + /// coins; it is never abandoned and never followed. + pub settled: bool, +} + +/// Why a proven-dead root cannot be abandoned as planned. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum AbandonRefusal { + /// The wallet holds no record of the root — nothing to remove, or it was + /// already swept by a conflicting spend. + UnknownRoot, + /// The root is in a block or IS-locked: whatever the probe said, it is + /// not dead. + SettledRoot, + /// A transaction outside the abandoned chain also spends one of the + /// chain's inputs, so releasing that input would contradict a surviving + /// claim. + InputClaimedBySurvivor { outpoint: OutPoint, claimant: Txid }, + /// A settled transaction spends an output of the chain, so the chain is + /// on chain itself and not dead. + SettledDescendant { txid: Txid }, +} + +/// The rows an abandon removes and the coins it hands back. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct AbandonPlan { + pub root: Txid, + /// The root and every unsettled transaction built on it, transitively. + pub txids: BTreeSet, + /// Every input the chain took from outside itself. + pub released_outpoints: BTreeSet, +} + +impl AbandonPlan { + /// The persistence batch for this plan — see the module docs for why + /// `superseded_by` is the root and nothing is held. + pub(crate) fn sweep_batch(&self) -> SweepBatch { + SweepBatch { + txids: self.txids.iter().copied().collect(), + superseded_by: self.root, + winner_mined_height: None, + released_outpoints: self.released_outpoints.iter().copied().collect(), + } + } +} + +/// Plan the abandon of `root`. `records` may list one transaction more than +/// once (key-wallet records a transaction in every account it touches). +pub(crate) fn plan_abandon<'a>( + records: impl IntoIterator, + root: Txid, +) -> Result { + // One entry per transaction. A transaction recorded in several accounts + // is the same transaction; if any account holds it as settled, it is. + let mut by_txid: BTreeMap = BTreeMap::new(); + for record in records { + by_txid + .entry(record.txid) + .and_modify(|(_, settled)| *settled |= record.settled) + .or_insert((record.inputs.as_slice(), record.settled)); + } + + match by_txid.get(&root) { + None => return Err(AbandonRefusal::UnknownRoot), + Some((_, true)) => return Err(AbandonRefusal::SettledRoot), + Some((_, false)) => {} + } + + // The chain: the root plus every transaction spending an output of + // something already in it, transitively. A settled spender ends the plan + // instead of being skipped — see `SettledDescendant`. + let mut txids = BTreeSet::from([root]); + loop { + let mut grew = false; + for (txid, (inputs, settled)) in &by_txid { + if txids.contains(txid) || !inputs.iter().any(|input| txids.contains(&input.txid)) { + continue; + } + if *settled { + return Err(AbandonRefusal::SettledDescendant { txid: *txid }); + } + txids.insert(*txid); + grew = true; + } + if !grew { + break; + } + } + + let released_outpoints: BTreeSet = txids + .iter() + .flat_map(|txid| by_txid[txid].0.iter().copied()) + .filter(|input| !txids.contains(&input.txid)) + .collect(); + + // Nothing outside the chain may still claim a coin the chain releases: + // the persister would hold it under `superseded_by`, and that is the + // root, which is about to stop existing. + for (claimant, (inputs, _)) in &by_txid { + if txids.contains(claimant) { + continue; + } + if let Some(outpoint) = inputs + .iter() + .find(|input| released_outpoints.contains(input)) + { + return Err(AbandonRefusal::InputClaimedBySurvivor { + outpoint: *outpoint, + claimant: *claimant, + }); + } + } + + Ok(AbandonPlan { + root, + txids, + released_outpoints, + }) +} + +#[cfg(test)] +mod tests { + use dashcore::hashes::Hash; + + use super::*; + + fn txid(n: u8) -> Txid { + Txid::from_byte_array([n; 32]) + } + + fn outpoint(n: u8, vout: u32) -> OutPoint { + OutPoint { + txid: txid(n), + vout, + } + } + + fn record(n: u8, inputs: &[OutPoint]) -> RecordView { + RecordView { + txid: txid(n), + inputs: inputs.to_vec(), + settled: false, + } + } + + fn settled(n: u8, inputs: &[OutPoint]) -> RecordView { + RecordView { + settled: true, + ..record(n, inputs) + } + } + + #[test] + fn should_release_every_input_of_a_lone_dead_transaction() { + let records = [record(1, &[outpoint(90, 0), outpoint(91, 3)])]; + + let plan = plan_abandon(&records, txid(1)).expect("abandonable"); + + assert_eq!(plan.txids, BTreeSet::from([txid(1)])); + assert_eq!( + plan.released_outpoints, + BTreeSet::from([outpoint(90, 0), outpoint(91, 3)]) + ); + } + + /// The customer's shape: sends built on the change of a dead send. The + /// whole chain goes; the only coins handed back are the ones the chain + /// took from outside itself — the change it spent internally was never + /// real. + #[test] + fn should_take_the_whole_chain_and_release_only_its_outside_inputs() { + let records = [ + record(1, &[outpoint(90, 0)]), + record(2, &[outpoint(1, 1)]), + record(3, &[outpoint(2, 1), outpoint(91, 0)]), + ]; + + let plan = plan_abandon(&records, txid(1)).expect("abandonable"); + + assert_eq!(plan.txids, BTreeSet::from([txid(1), txid(2), txid(3)])); + assert_eq!( + plan.released_outpoints, + BTreeSet::from([outpoint(90, 0), outpoint(91, 0)]) + ); + } + + /// The case that would leave a hold attributed to a transaction that no + /// longer exists: another transaction of this wallet spends one of the + /// chain's inputs. The plan must refuse rather than release it. + #[test] + fn should_refuse_when_a_surviving_transaction_claims_one_of_the_inputs() { + let records = [ + record(1, &[outpoint(90, 0), outpoint(91, 0)]), + record(7, &[outpoint(91, 0)]), + ]; + + assert_eq!( + plan_abandon(&records, txid(1)), + Err(AbandonRefusal::InputClaimedBySurvivor { + outpoint: outpoint(91, 0), + claimant: txid(7), + }) + ); + } + + #[test] + fn should_refuse_when_a_settled_transaction_claims_one_of_the_inputs() { + let records = [ + record(1, &[outpoint(90, 0)]), + settled(7, &[outpoint(90, 0)]), + ]; + + assert!(matches!( + plan_abandon(&records, txid(1)), + Err(AbandonRefusal::InputClaimedBySurvivor { .. }) + )); + } + + /// A claimant deeper in the chain counts too: descendant 2 takes an + /// outside coin that transaction 7 also spends. + #[test] + fn should_refuse_when_a_survivor_claims_an_input_of_a_descendant() { + let records = [ + record(1, &[outpoint(90, 0)]), + record(2, &[outpoint(1, 1), outpoint(92, 0)]), + record(7, &[outpoint(92, 0)]), + ]; + + assert!(matches!( + plan_abandon(&records, txid(1)), + Err(AbandonRefusal::InputClaimedBySurvivor { claimant, .. }) if claimant == txid(7) + )); + } + + #[test] + fn should_refuse_a_settled_root() { + let records = [settled(1, &[outpoint(90, 0)])]; + + assert_eq!( + plan_abandon(&records, txid(1)), + Err(AbandonRefusal::SettledRoot) + ); + } + + #[test] + fn should_refuse_a_root_the_wallet_does_not_hold() { + let records = [record(2, &[outpoint(90, 0)])]; + + assert_eq!( + plan_abandon(&records, txid(1)), + Err(AbandonRefusal::UnknownRoot) + ); + } + + /// A settled transaction spending the chain's own output means the root + /// is in a block too — a child cannot be mined without its parent — so + /// whatever the probe said, the chain is not dead. key-wallet's walk + /// would simply stop at the settled child; the planner refuses instead, + /// because deleting the root's rows under a settled spender is exactly the + /// state a persister cannot reason about. + #[test] + fn should_refuse_when_a_settled_transaction_spends_the_chain() { + let records = [record(1, &[outpoint(90, 0)]), settled(2, &[outpoint(1, 1)])]; + + assert_eq!( + plan_abandon(&records, txid(1)), + Err(AbandonRefusal::SettledDescendant { txid: txid(2) }) + ); + } + + /// key-wallet records a transaction once per account it touches; the + /// duplicate must neither appear twice nor look like a rival claimant. + #[test] + fn should_treat_a_transaction_recorded_in_two_accounts_as_one() { + let records = [ + record(1, &[outpoint(90, 0)]), + record(1, &[outpoint(90, 0)]), + record(2, &[outpoint(1, 1)]), + record(2, &[outpoint(1, 1)]), + ]; + + let plan = plan_abandon(&records, txid(1)).expect("abandonable"); + + assert_eq!(plan.txids, BTreeSet::from([txid(1), txid(2)])); + assert_eq!(plan.released_outpoints, BTreeSet::from([outpoint(90, 0)])); + } + + /// The batch every persister applies: nothing held, so nothing is ever + /// attributed to `superseded_by`; no winner height, so no placeholder is + /// ever stamped. + #[test] + fn should_build_a_batch_that_holds_nothing() { + let records = [ + record(1, &[outpoint(90, 0)]), + record(2, &[outpoint(1, 1), outpoint(91, 0)]), + ]; + let plan = plan_abandon(&records, txid(1)).expect("abandonable"); + + let batch = plan.sweep_batch(); + + assert_eq!(batch.superseded_by, txid(1)); + assert_eq!(batch.winner_mined_height, None); + assert_eq!( + batch.txids.iter().copied().collect::>(), + BTreeSet::from([txid(1), txid(2)]) + ); + // Every input any removed transaction takes from outside the batch is + // released — the property the persisters' "held" path depends on. + let removed: BTreeSet = batch.txids.iter().copied().collect(); + let released: BTreeSet = batch.released_outpoints.iter().copied().collect(); + for record in &records { + for input in &record.inputs { + if !removed.contains(&input.txid) { + assert!(released.contains(input), "{input:?} would be held"); + } + } + } + } +} diff --git a/packages/rs-platform-wallet/src/wallet/core/mod.rs b/packages/rs-platform-wallet/src/wallet/core/mod.rs index e03363948f2..2d32e7e615e 100644 --- a/packages/rs-platform-wallet/src/wallet/core/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/core/mod.rs @@ -1,3 +1,4 @@ +pub(crate) mod abandon_plan; pub mod balance; pub mod balance_handler; mod broadcast; diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SweptTransactionPersistTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SweptTransactionPersistTests.swift index 0656e587879..f66a4af6a0d 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SweptTransactionPersistTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SweptTransactionPersistTests.swift @@ -3414,4 +3414,87 @@ final class SweptTransactionPersistTests: XCTestCase { "no claim on a dead parent's output survives the batch, released or not" ) } + + /// The batch `abandon_plan` builds for a chain the network proved dead + /// (rust-dashcore#961's abandon, mirrored through the sweep channel): + /// every input the chain took from outside itself is released, + /// `superseded_by` is the chain's own root and there is no winner height. + /// Nothing may be held — a hold would be attributed to the root, a + /// transaction that no longer exists, and nothing could ever resolve it. + /// + /// Shape of ticket 32347: root R spends a real coin A and leaves change + /// R:0; child C spends that change plus another real coin B and leaves + /// change C:0. + func testAnAbandonedChainFreesItsOutsideInputsAndHoldsNothing() throws { + let (handler, container) = try makeHandler() + let rootTxid = Data(repeating: 0xE1, count: 32) + let childTxid = Data(repeating: 0xE2, count: 32) + + let context = ModelContext(container) + context.insert(PersistentWallet(walletId: walletId, network: .testnet)) + let funding = PersistentTransaction( + txid: fundingTxid, + transactionData: Data(repeating: 0x04, count: 10), + context: 2, + blockHeight: 100, + netAmount: 140_000 + ) + let root = loserRow(txid: rootTxid, spending: [(txid: fundingTxid, vout: 0)], netAmount: -40_000) + let child = loserRow( + txid: childTxid, + spending: [(txid: rootTxid, vout: 0), (txid: fundingTxid, vout: 1)], + netAmount: -30_000 + ) + context.insert(funding) + context.insert(root) + context.insert(child) + + // The real coins, claimed by the chain but never flipped spent — the + // chain never reached a block. + let coinA = PersistentTxo(transaction: funding, vout: 0, amount: 100_000, address: "yFundAddr", height: 100) + coinA.walletId = walletId + coinA.spendingTransaction = root + context.insert(coinA) + let coinB = PersistentTxo(transaction: funding, vout: 1, amount: 40_000, address: "yFundAddr", height: 100) + coinB.walletId = walletId + coinB.spendingTransaction = child + context.insert(coinB) + + // The phantom change: the root's, spent by the child, and the child's. + let rootChange = PersistentTxo(transaction: root, vout: 0, amount: 60_000, address: "yChangeAddr", height: 0) + rootChange.walletId = walletId + rootChange.spendingTransaction = child + context.insert(rootChange) + let childChange = PersistentTxo(transaction: child, vout: 0, amount: 70_000, address: "yChangeAddr", height: 0) + childChange.walletId = walletId + context.insert(childChange) + try context.save() + + XCTAssertTrue(sweep(handler, [Batch( + losers: [rootTxid, childTxid], + winner: rootTxid, + winnerMinedHeight: nil, + released: [(txid: fundingTxid, vout: 0), (txid: fundingTxid, vout: 1)] + )])) + + XCTAssertNil(transaction(container, txid: rootTxid), "the root row is gone") + XCTAssertNil(transaction(container, txid: childTxid), "the child row is gone") + XCTAssertNil(txo(container, txid: rootTxid, vout: 0), "the root's change is gone") + XCTAssertNil(txo(container, txid: childTxid, vout: 0), "the child's change is gone") + + for vout: UInt32 in [0, 1] { + let coin = try XCTUnwrap(txo(container, txid: fundingTxid, vout: vout), "real coin \(vout) stays") + XCTAssertFalse(coin.isSpent, "real coin \(vout) comes back spendable") + XCTAssertNil(coin.spendingTransaction, "real coin \(vout) keeps no link to the dead chain") + XCTAssertNil(coin.supersededByTxid, "real coin \(vout) is not held under the root") + } + + let verify = ModelContext(container) + let tombstones = try verify.fetch(FetchDescriptor()) + .filter { $0.isSweptTombstone || $0.spendingTxid == rootTxid } + XCTAssertTrue(tombstones.isEmpty, "an abandon must never leave a hold attributed to the root") + let heldByRoot = try verify.fetch(FetchDescriptor()) + .filter { $0.supersededByTxid == rootTxid } + XCTAssertTrue(heldByRoot.isEmpty, "no coin may be held under the abandoned root") + } } From 8fc59afb157b058237c139b2d161fcbbe38c55b4 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 13:35:35 +0300 Subject: [PATCH 002/169] chore(platform-wallet): log each node's answer to a broadcast probe; add a testnet probe example A debug line per submission (txid, answering node, verdict) is the diagnostic a support export needs to tell a dead transaction from a slow one. The example resubmits raw transactions to testnet evonodes and prints the verdict; verified 2026-09-25: a mined tx -> Accepted, a missing input -> Dead from two distinct nodes (bad-txns-inputs-missingorspent), an input spent by an IS-locked tx -> Dead (tx-txlock-conflict). Ticket 32347. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../examples/broadcast_probe_testnet.rs | 52 +++++++++++++++++++ .../rs-platform-wallet/src/broadcast_probe.rs | 6 +++ 2 files changed, 58 insertions(+) create mode 100644 packages/rs-platform-wallet/examples/broadcast_probe_testnet.rs diff --git a/packages/rs-platform-wallet/examples/broadcast_probe_testnet.rs b/packages/rs-platform-wallet/examples/broadcast_probe_testnet.rs new file mode 100644 index 00000000000..0369e69b7e0 --- /dev/null +++ b/packages/rs-platform-wallet/examples/broadcast_probe_testnet.rs @@ -0,0 +1,52 @@ +//! Manual testnet check of the broadcast probe against real evonodes. +//! +//! Hands each transaction (raw hex, one per argument) to +//! [`DapiAcceptanceProbe`] and prints the verdict. Run with node-level detail: +//! +//! ```text +//! RUST_LOG=platform_wallet::broadcast_probe=debug \ +//! cargo run -p platform-wallet --example broadcast_probe_testnet -- [ ...] +//! ``` +//! +//! Resubmitting a transaction is harmless — a node either already has it, +//! takes it, or says why it will not — so any transaction may be probed. + +use std::sync::Arc; + +use dash_sdk::SdkBuilder; +use dashcore::{consensus, Network, Transaction}; +use platform_wallet::broadcast_probe::{AcceptanceProbe, DapiAcceptanceProbe}; +use rs_sdk_trusted_context_provider::TrustedHttpContextProvider; + +#[tokio::main(flavor = "multi_thread")] +async fn main() -> Result<(), Box> { + tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .init(); + + let transactions: Vec = std::env::args() + .skip(1) + .map(|raw| consensus::deserialize(&hex::decode(raw.trim()).expect("hex")).expect("tx")) + .collect(); + if transactions.is_empty() { + return Err("pass at least one raw transaction as hex".into()); + } + + let provider = TrustedHttpContextProvider::new( + Network::Testnet, + None, + std::num::NonZeroUsize::new(100).expect("non-zero"), + )?; + let sdk = Arc::new( + SdkBuilder::new_testnet() + .with_context_provider(provider) + .build()?, + ); + let probe = DapiAcceptanceProbe::new(sdk); + + for transaction in &transactions { + let verdict = probe.probe(transaction).await; + println!("{} {verdict:?}", transaction.txid()); + } + Ok(()) +} diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index aea98742a0c..ce64da6cf52 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -156,6 +156,12 @@ pub(crate) async fn probe_with( for _ in 0..MAX_SUBMISSIONS_PER_PROBE { let (node, verdict) = submitter.submit(transaction).await; + tracing::debug!( + txid = %transaction.txid(), + node = node.as_deref().unwrap_or("unidentified"), + ?verdict, + "broadcast probe: node answered" + ); match verdict { NodeVerdict::Accepted => return ProbeVerdict::Accepted, NodeVerdict::Dead { reason } => { From 64b00b80ad50470278f42b4eff821f4a4210d07c Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 13:44:50 +0300 Subject: [PATCH 003/169] feat(platform-wallet): probe unconfirmed sends whose broadcast outcome is unknown and publish the verdict A send the SPV broadcaster could not confirm stays an unconfirmed spend with nobody asking again, so the user never learns whether it landed. BroadcastResolver re-asks the network through the DAPI AcceptanceProbe and publishes the verdict; it changes nothing in the wallet (cancelling a proven-dead send is a separate, user-driven step). - Probes only chain roots: an unsettled transaction that spends this wallet's coins and no output of another such transaction. A node that has not seen the parent answers missingorspent for a valid child. - Once right after dash-spv reports Uncertain, then once per new block; after 24 blocks without a verdict, every 10 blocks. Stops at Accepted or Dead; forgets a root that settles or leaves the wallet. - Records are read under a short read lock; the probe runs without it. - New PlatformEventHandler::on_outgoing_transaction_probed plus an info log line per verdict. - Off by default: PlatformWalletManager::set_broadcast_probe_enabled. Probe tasks are aborted and joined on manager shutdown. - abandon_plan is kept for the user-driven cancel; its dead-code warnings are allowed until that lands. Tests: 14 new (written first, red against stubs), whole crate 1197/1197. Ticket 32347. Co-Authored-By: Claude Opus 5.5 (1M context) --- packages/rs-platform-wallet/src/events.rs | 30 + .../src/manager/accessors.rs | 16 + .../rs-platform-wallet/src/manager/mod.rs | 26 + .../src/wallet/core/broadcast_resolver.rs | 658 ++++++++++++++++++ .../rs-platform-wallet/src/wallet/core/mod.rs | 3 + 5 files changed, 733 insertions(+) create mode 100644 packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs diff --git a/packages/rs-platform-wallet/src/events.rs b/packages/rs-platform-wallet/src/events.rs index c329c5a32f5..8d02b691917 100644 --- a/packages/rs-platform-wallet/src/events.rs +++ b/packages/rs-platform-wallet/src/events.rs @@ -45,6 +45,22 @@ pub trait PlatformEventHandler: EventHandler { /// [`DpnsSyncManager`]: crate::manager::dpns_sync::DpnsSyncManager fn on_dpns_marketplace_sync_completed(&self, _summary: &DpnsSyncPassSummary) {} + /// Fired when the broadcast resolver has asked the network about an + /// unconfirmed send of this wallet whose broadcast outcome was unknown. + /// `Accepted` means a node holds the transaction; `Dead` means two + /// distinct nodes proved it can never be mined; `Unresolved` means no + /// verdict yet — the resolver asks again on a later block. Nothing in the + /// wallet changes either way. + /// + /// Default impl is a no-op so existing handlers don't have to care. + fn on_outgoing_transaction_probed( + &self, + _wallet_id: &key_wallet_manager::WalletId, + _txid: &dashcore::Txid, + _verdict: &crate::broadcast_probe::ProbeVerdict, + ) { + } + /// Fired after each [`ShieldedSyncManager`] pass completes, /// including passes that produced no updates or skipped every /// wallet because none had a bound shielded sub-wallet yet. @@ -153,6 +169,20 @@ impl PlatformEventManager { } } + /// Dispatch a broadcast-probe verdict to every handler. Rare: at most one + /// per unconfirmed send per block. + pub fn on_outgoing_transaction_probed( + &self, + wallet_id: &key_wallet_manager::WalletId, + txid: &dashcore::Txid, + verdict: &crate::broadcast_probe::ProbeVerdict, + ) { + let handlers = self.handlers.load(); + for h in handlers.iter() { + h.on_outgoing_transaction_probed(wallet_id, txid, verdict); + } + } + /// Dispatch a shielded sync completion to every handler. /// /// Not on the SPV hot path — called once per shielded sync pass diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index 1f9ed240c84..25deb35ec5c 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -523,6 +523,22 @@ impl PlatformWalletManager

{ } /// The SDK instance. + /// Turn automatic probing of unconfirmed sends on or off. + /// + /// When on, every unconfirmed send of a registered wallet whose broadcast + /// outcome went quiet is resubmitted to evonodes over DAPI — once right + /// after dash-spv reports it `Uncertain`, then once per block — and the + /// verdict is published as `on_outgoing_transaction_probed`. Nothing in + /// the wallet changes. Off by default. + pub fn set_broadcast_probe_enabled(&self, enabled: bool) { + self.broadcast_resolver.set_enabled(enabled); + } + + /// Whether automatic probing of unconfirmed sends is on. + pub fn is_broadcast_probe_enabled(&self) -> bool { + self.broadcast_resolver.is_enabled() + } + pub fn sdk(&self) -> &dash_sdk::Sdk { &self.sdk } diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 51a57c32451..c70914b366a 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -380,6 +380,10 @@ pub struct PlatformWalletManager { /// Tracks asynchronous payment hooks so manager shutdown can close /// admission and drain every task before host callback contexts are freed. pub(super) dashpay_payment_handler: Arc, + /// Asks the network about this manager's unconfirmed sends whose + /// broadcast outcome was unknown and publishes the verdicts. Off until + /// the host calls [`set_broadcast_probe_enabled`](Self::set_broadcast_probe_enabled). + pub(super) broadcast_resolver: Arc, /// Periodic shielded (Orchard) note sync coordinator (spends are /// detected during the note scan, no separate nullifier pass). /// Iterates every wallet that has been bound via @@ -539,13 +543,27 @@ impl PlatformWalletManager

{ Arc::clone(&wallet_manager), Arc::clone(&persister) as Arc, )); + // BroadcastResolver probes unconfirmed sends whose broadcast outcome + // was unknown (read-only; see its module docs). It publishes through + // the event manager it is registered with, so it gets a weak handle + // to it once that exists. + let broadcast_resolver = Arc::new( + crate::wallet::core::broadcast_resolver::BroadcastResolver::new( + Arc::new(crate::broadcast_probe::DapiAcceptanceProbe::new( + Arc::clone(&sdk), + )), + Arc::clone(&wallet_manager), + ), + ); let event_manager = Arc::new(PlatformEventManager::new(vec![ app_handler, lock_handler, balance_handler, spend_observation_handler, Arc::clone(&dashpay_payment_handler) as Arc, + Arc::clone(&broadcast_resolver) as Arc, ])); + broadcast_resolver.set_event_manager(Arc::downgrade(&event_manager)); let spv = Arc::new(SpvRuntime::new( Arc::clone(&wallet_manager), @@ -595,6 +613,7 @@ impl PlatformWalletManager

{ dashpay_sync_manager: dashpay_sync, dpns_sync_manager: dpns_sync, dashpay_payment_handler, + broadcast_resolver, #[cfg(feature = "shielded")] shielded_sync_manager: shielded_sync, #[cfg(feature = "shielded")] @@ -946,6 +965,13 @@ impl PlatformWalletManager

{ .dashpay_payment_handler .quiesce_within(PAYMENT_DRAIN_BUDGET) .await; + // Probe passes publish through the host's event callbacks, so they + // must be gone before destroy returns; they are safe to abort. + let probes_drained = self + .broadcast_resolver + .quiesce_within(PAYMENT_DRAIN_BUDGET) + .await; + let payments_drained = payments_drained && probes_drained; // Drain the coordinators concurrently against one shared budget so // the drain phase as a whole is bounded (a wedged pass surfaces as diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs new file mode 100644 index 00000000000..383b771a497 --- /dev/null +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -0,0 +1,658 @@ +//! Automatically asking the network about sends whose broadcast outcome is +//! unknown. +//! +//! A send the SPV broadcaster could not confirm ends as `MaybeSent` +//! (`BroadcastResult::Uncertain`): the wallet keeps its record as an +//! unconfirmed spend and nothing ever tells the user whether the payment +//! landed. This module re-asks the network through +//! [`AcceptanceProbe`](crate::broadcast_probe::AcceptanceProbe) and publishes +//! the verdict; it **changes nothing** in the wallet. Cancelling a dead send is +//! a separate, user-driven decision (see `abandon_plan`). +//! +//! What is probed: every *root* of an unsettled chain the wallet itself +//! signed — a transaction that spends this wallet's coins, is not in a block, +//! not InstantSend-locked, and does not spend the output of another such +//! transaction. Probing a child is pointless and misleading: a node that has +//! not seen the parent answers `missingorspent` for a perfectly valid child. +//! A healthy send is IS-locked within seconds, so in practice only sends that +//! went quiet are ever probed. +//! +//! When: once as soon as dash-spv reports an `Uncertain` broadcast, then once +//! per new block while unresolved; after [`EVERY_BLOCK_WINDOW`] blocks without +//! a verdict, every [`SLOW_INTERVAL`] blocks. A root stops being probed once a +//! probe says `Accepted` or `Dead`, or once it settles or leaves the wallet. +//! Probes run only while the SPV client delivers blocks, i.e. while the app is +//! in the foreground. + +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex, Weak}; + +use dash_spv::EventHandler; +use dashcore::{OutPoint, Transaction, Txid}; +use key_wallet::transaction_checking::TransactionContext; +use key_wallet_manager::{WalletEvent, WalletId, WalletManager}; +use tokio::sync::RwLock; + +use crate::broadcast_probe::{AcceptanceProbe, ProbeVerdict}; +use crate::events::{PlatformEventHandler, PlatformEventManager}; +use crate::wallet::platform_wallet::PlatformWalletInfo; + +/// Blocks after a root is first seen during which it is probed on every block. +pub(crate) const EVERY_BLOCK_WINDOW: u32 = 24; + +/// Blocks between probes once [`EVERY_BLOCK_WINDOW`] has passed. +pub(crate) const SLOW_INTERVAL: u32 = 10; + +/// What the resolver needs to know about one recorded transaction. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct OutgoingView { + pub txid: Txid, + pub inputs: Vec, + /// In a block, InstantSend-locked, or finalized. + pub settled: bool, + /// The transaction spends at least one of this wallet's coins, i.e. the + /// wallet signed it. + pub spends_own_coins: bool, + pub transaction: Transaction, +} + +/// The roots of this wallet's unsettled chains, one entry per txid. +pub(crate) fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { + // A transaction recorded in several accounts is one transaction; if any + // account holds it as settled, it is. + let mut by_txid: BTreeMap = BTreeMap::new(); + for view in views { + by_txid + .entry(view.txid) + .and_modify(|(_, settled)| *settled |= view.settled) + .or_insert((view, view.settled)); + } + let unsettled_own: BTreeSet = by_txid + .iter() + .filter(|(_, (view, settled))| !*settled && view.spends_own_coins) + .map(|(txid, _)| *txid) + .collect(); + unsettled_own + .iter() + .map(|txid| by_txid[txid].0) + .filter(|view| { + !view + .inputs + .iter() + .any(|input| unsettled_own.contains(&input.txid)) + }) + .collect() +} + +/// When a root is next due for a probe. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct ProbeSchedule { + first_seen: u32, + last_probe: Option, +} + +impl ProbeSchedule { + pub(crate) fn new(first_seen: u32) -> Self { + Self { + first_seen, + last_probe: None, + } + } + + pub(crate) fn is_due(&self, height: u32) -> bool { + match self.last_probe { + None => true, + Some(last) if height <= last => false, + Some(_) if height.saturating_sub(self.first_seen) < EVERY_BLOCK_WINDOW => true, + Some(last) => height - last >= SLOW_INTERVAL, + } + } + + fn probed_at(&mut self, height: u32) { + self.last_probe = Some(height); + } +} + +/// Per-root bookkeeping shared by every pass. +#[derive(Debug, Default)] +pub(crate) struct ResolverState { + schedules: HashMap<(WalletId, Txid), ProbeSchedule>, + resolved: HashSet<(WalletId, Txid)>, +} + +/// One pass over one wallet at `height`: probe every due root and return the +/// verdicts to publish. `force` makes one root due regardless of its schedule +/// (the SPV broadcaster has just reported it `Uncertain`). +pub(crate) async fn resolve_pass( + state: &Mutex, + probe: &dyn AcceptanceProbe, + wallet_id: WalletId, + height: u32, + views: &[OutgoingView], + force: Option, +) -> Vec<(Txid, ProbeVerdict)> { + let roots = ambiguous_roots(views); + let root_ids: HashSet = roots.iter().map(|view| view.txid).collect(); + + // Decide what is due under the lock; probe without it. + let due: Vec<(Txid, Transaction)> = { + let mut guard = state.lock().expect("resolver state poisoned"); + let ResolverState { + schedules, + resolved, + } = &mut *guard; + // Forget this wallet's roots that settled or left the wallet. + schedules.retain(|(wallet, txid), _| *wallet != wallet_id || root_ids.contains(txid)); + resolved.retain(|(wallet, txid)| *wallet != wallet_id || root_ids.contains(txid)); + + let mut due = Vec::new(); + for view in &roots { + let key = (wallet_id, view.txid); + if resolved.contains(&key) { + continue; + } + let schedule = schedules + .entry(key) + .or_insert_with(|| ProbeSchedule::new(height)); + if force == Some(view.txid) || schedule.is_due(height) { + schedule.probed_at(height); + due.push((view.txid, view.transaction.clone())); + } + } + due + }; + + let mut verdicts = Vec::with_capacity(due.len()); + for (txid, transaction) in due { + let verdict = probe.probe(&transaction).await; + if !matches!(verdict, ProbeVerdict::Unresolved { .. }) { + state + .lock() + .expect("resolver state poisoned") + .resolved + .insert((wallet_id, txid)); + } + verdicts.push((txid, verdict)); + } + verdicts +} + +/// This wallet's unsettled transactions that spend its own coins, one view per +/// account record. Only those can be ambiguity roots, so settled and incoming +/// records are skipped here rather than cloned. +pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { + let accounts = info.core_wallet.accounts.all_accounts(); + // A transaction settled in any account is settled. + let mut settled_anywhere: HashSet = HashSet::new(); + for account in accounts.iter() { + for record in account.transactions().values() { + if record.is_confirmed() + || matches!(record.context, TransactionContext::InstantSend(_)) + || account.transaction_is_finalized(&record.txid) + { + settled_anywhere.insert(record.txid); + } + } + } + let mut views = Vec::new(); + for account in accounts.iter() { + for record in account.transactions().values() { + if settled_anywhere.contains(&record.txid) || record.input_details.is_empty() { + continue; + } + views.push(OutgoingView { + txid: record.txid, + inputs: record + .transaction + .input + .iter() + .map(|input| input.previous_output) + .collect(), + settled: false, + spends_own_coins: true, + transaction: record.transaction.clone(), + }); + } + } + views +} + +/// Admission gate for probe tasks. A probe only reads the wallet and +/// resubmits bytes that were already sent, so a task may be aborted at any +/// point without losing anything. +struct ProbeTasks { + state: Mutex<(bool, Vec>)>, +} + +impl ProbeTasks { + fn new() -> Self { + Self { + state: Mutex::new((true, Vec::new())), + } + } + + fn spawn(&self, future: F) + where + F: std::future::Future + Send + 'static, + { + let mut state = self.state.lock().expect("probe task mutex poisoned"); + if !state.0 { + return; + } + state.1.retain(|handle| !handle.is_finished()); + state.1.push(tokio::spawn(future)); + } + + /// Close admission and stop every task, waiting at most `budget` for each + /// aborted task to unwind. Returns `false` if one outlived it. + async fn quiesce_within(&self, budget: std::time::Duration) -> bool { + let handles = { + let mut state = self.state.lock().expect("probe task mutex poisoned"); + state.0 = false; + std::mem::take(&mut state.1) + }; + let deadline = tokio::time::Instant::now() + budget; + let mut clean = true; + for handle in &handles { + handle.abort(); + } + for mut handle in handles { + if tokio::time::timeout_at(deadline, &mut handle) + .await + .is_err() + { + clean = false; + } + } + clean + } +} + +/// Runs probe passes off the wallet-event fan-out and publishes each verdict +/// as [`PlatformEventHandler::on_outgoing_transaction_probed`]. +/// +/// Off until the host turns it on with [`set_enabled`](Self::set_enabled): a +/// probe sends the signed transaction to evonodes over DAPI, which is a +/// product decision, not something a wallet should start doing on upgrade. +pub(crate) struct BroadcastResolver { + enabled: AtomicBool, + probe: Arc, + wallet_manager: Arc>>, + state: Arc>, + /// Last synced height per wallet, so an SPV-level `Uncertain` (which + /// carries no wallet id) can run a pass on every wallet at once. + heights: Mutex>, + events: Mutex>, + tasks: ProbeTasks, +} + +impl BroadcastResolver { + pub(crate) fn new( + probe: Arc, + wallet_manager: Arc>>, + ) -> Self { + Self { + enabled: AtomicBool::new(false), + probe, + wallet_manager, + state: Arc::new(Mutex::new(ResolverState::default())), + heights: Mutex::new(HashMap::new()), + events: Mutex::new(Weak::new()), + tasks: ProbeTasks::new(), + } + } + + /// Where verdicts are published. Weak: the event manager holds this + /// resolver as one of its handlers. + pub(crate) fn set_event_manager(&self, events: Weak) { + *self.events.lock().expect("events mutex poisoned") = events; + } + + pub(crate) fn set_enabled(&self, enabled: bool) { + self.enabled.store(enabled, Ordering::SeqCst); + } + + pub(crate) fn is_enabled(&self) -> bool { + self.enabled.load(Ordering::SeqCst) + } + + /// Stop admitting passes and stop the running ones. Idempotent. + pub(crate) async fn quiesce_within(&self, budget: std::time::Duration) -> bool { + self.tasks.quiesce_within(budget).await + } + + fn spawn_pass(&self, wallet_id: WalletId, height: u32, force: Option) { + let wallet_manager = Arc::clone(&self.wallet_manager); + let probe = Arc::clone(&self.probe); + let state = Arc::clone(&self.state); + let events = self.events.lock().expect("events mutex poisoned").clone(); + self.tasks.spawn(async move { + // Read under the lock, probe without it: a probe is network I/O. + let views = { + let manager = wallet_manager.read().await; + match manager.get_wallet_info(&wallet_id) { + Some(info) => collect_views(info), + None => return, + } + }; + let verdicts = + resolve_pass(&state, probe.as_ref(), wallet_id, height, &views, force).await; + if verdicts.is_empty() { + return; + } + let events = events.upgrade(); + for (txid, verdict) in &verdicts { + tracing::info!( + wallet_id = %hex::encode(wallet_id), + txid = %txid, + ?verdict, + "broadcast probe: verdict for an unconfirmed send" + ); + if let Some(events) = &events { + events.on_outgoing_transaction_probed(&wallet_id, txid, verdict); + } + } + }); + } +} + +impl EventHandler for BroadcastResolver { + fn on_wallet_event(&self, event: &WalletEvent) { + let WalletEvent::SyncHeightAdvanced { wallet_id, height } = event else { + return; + }; + self.heights + .lock() + .expect("heights mutex poisoned") + .insert(*wallet_id, *height); + if self.is_enabled() { + self.spawn_pass(*wallet_id, *height, None); + } + } + + fn on_sync_event(&self, event: &dash_spv::sync::SyncEvent) { + let dash_spv::sync::SyncEvent::TransactionBroadcastResult { + txid, + result: dash_spv::BroadcastResult::Uncertain, + } = event + else { + return; + }; + if !self.is_enabled() { + return; + } + let heights: Vec<(WalletId, u32)> = self + .heights + .lock() + .expect("heights mutex poisoned") + .iter() + .map(|(wallet, height)| (*wallet, *height)) + .collect(); + for (wallet_id, height) in heights { + self.spawn_pass(wallet_id, height, Some(*txid)); + } + } +} + +impl PlatformEventHandler for BroadcastResolver {} + +#[cfg(test)] +mod tests { + use std::sync::Mutex as StdMutex; + + use async_trait::async_trait; + use dashcore::hashes::Hash; + + use super::*; + + fn txid(n: u8) -> Txid { + Txid::from_byte_array([n; 32]) + } + + fn outpoint(n: u8, vout: u32) -> OutPoint { + OutPoint { + txid: txid(n), + vout, + } + } + + fn tx(lock_time: u32) -> Transaction { + Transaction { + version: 2, + lock_time, + input: Vec::new(), + output: Vec::new(), + special_transaction_payload: None, + } + } + + fn send(n: u8, inputs: &[OutPoint]) -> OutgoingView { + OutgoingView { + txid: txid(n), + inputs: inputs.to_vec(), + settled: false, + spends_own_coins: true, + transaction: tx(n as u32), + } + } + + fn roots(views: &[OutgoingView]) -> BTreeSet { + ambiguous_roots(views).into_iter().map(|v| v.txid).collect() + } + + // ---- ambiguous_roots ---------------------------------------------------- + + #[test] + fn should_pick_a_lone_unsettled_send() { + assert_eq!( + roots(&[send(1, &[outpoint(90, 0)])]), + BTreeSet::from([txid(1)]) + ); + } + + /// The customer's shape: sends built on the change of an unsettled send. + /// Only the first is probed — a node that has not seen it answers + /// `missingorspent` for the others. + #[test] + fn should_pick_only_the_root_of_a_chain() { + let views = [ + send(1, &[outpoint(90, 0)]), + send(2, &[outpoint(1, 1)]), + send(3, &[outpoint(2, 1)]), + ]; + assert_eq!(roots(&views), BTreeSet::from([txid(1)])); + } + + #[test] + fn should_skip_settled_and_incoming_transactions() { + let mut settled = send(1, &[outpoint(90, 0)]); + settled.settled = true; + let mut incoming = send(2, &[outpoint(91, 0)]); + incoming.spends_own_coins = false; + assert!(roots(&[settled, incoming]).is_empty()); + } + + /// Once the parent settles, the child has no unsettled parent and is a + /// root in its own right. + #[test] + fn should_treat_the_child_of_a_settled_parent_as_a_root() { + let mut parent = send(1, &[outpoint(90, 0)]); + parent.settled = true; + let child = send(2, &[outpoint(1, 1)]); + assert_eq!(roots(&[parent, child]), BTreeSet::from([txid(2)])); + } + + /// key-wallet records a transaction in every account it touches; one + /// root, and a settled copy anywhere settles it. + #[test] + fn should_merge_a_transaction_recorded_in_two_accounts() { + let a = send(1, &[outpoint(90, 0)]); + let mut b = send(1, &[outpoint(90, 0)]); + b.settled = true; + assert!(roots(&[a.clone(), b]).is_empty()); + assert_eq!(roots(&[a.clone(), a]), BTreeSet::from([txid(1)])); + } + + // ---- ProbeSchedule ------------------------------------------------------ + + #[test] + fn should_be_due_immediately_when_never_probed() { + assert!(ProbeSchedule::new(100).is_due(100)); + } + + #[test] + fn should_probe_at_most_once_per_block() { + let mut s = ProbeSchedule::new(100); + s.probed_at(100); + assert!(!s.is_due(100)); + assert!(s.is_due(101)); + } + + #[test] + fn should_slow_down_after_the_every_block_window() { + let first = 100; + let mut s = ProbeSchedule::new(first); + let late = first + EVERY_BLOCK_WINDOW; + s.probed_at(late); + for height in late + 1..late + SLOW_INTERVAL { + assert!(!s.is_due(height), "not due at {height}"); + } + assert!(s.is_due(late + SLOW_INTERVAL)); + } + + // ---- resolve_pass ------------------------------------------------------- + + /// Answers per txid and records every probe it received. + struct ScriptedProbe { + answers: BTreeMap, + probed: StdMutex>, + } + + impl ScriptedProbe { + fn new(answers: &[(Txid, ProbeVerdict)]) -> Self { + Self { + answers: answers.iter().cloned().collect(), + probed: StdMutex::new(Vec::new()), + } + } + + fn probed(&self) -> Vec { + self.probed.lock().expect("probed").clone() + } + } + + #[async_trait] + impl AcceptanceProbe for ScriptedProbe { + async fn probe(&self, transaction: &Transaction) -> ProbeVerdict { + // Fixtures key transactions by lock_time == txid byte. + let id = txid(transaction.lock_time as u8); + self.probed.lock().expect("probed").push(id); + self.answers + .get(&id) + .cloned() + .unwrap_or(ProbeVerdict::Unresolved { + reason: "scripted".to_string(), + }) + } + } + + fn wallet() -> WalletId { + [7u8; 32] + } + + fn dead() -> ProbeVerdict { + ProbeVerdict::Dead { + reason: "bad-txns-inputs-missingorspent".to_string(), + } + } + + fn unresolved() -> ProbeVerdict { + ProbeVerdict::Unresolved { + reason: "no quorum".to_string(), + } + } + + #[tokio::test] + async fn should_probe_only_roots_and_report_each_verdict() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), dead())]); + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; + + let verdicts = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + + assert_eq!(probe.probed(), vec![txid(1)]); + assert_eq!(verdicts, vec![(txid(1), dead())]); + } + + #[tokio::test] + async fn should_stop_probing_a_root_once_it_is_resolved() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); + let views = [send(1, &[outpoint(90, 0)])]; + + resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + let again = resolve_pass(&state, &probe, wallet(), 101, &views, None).await; + + assert!(again.is_empty(), "an accepted root is not probed again"); + assert_eq!(probe.probed(), vec![txid(1)]); + } + + #[tokio::test] + async fn should_keep_probing_an_unresolved_root_on_each_new_block() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); + let views = [send(1, &[outpoint(90, 0)])]; + + resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + let same_block = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + let next_block = resolve_pass(&state, &probe, wallet(), 101, &views, None).await; + + assert!(same_block.is_empty(), "one probe per block"); + assert_eq!(next_block, vec![(txid(1), unresolved())]); + assert_eq!(probe.probed(), vec![txid(1), txid(1)]); + } + + /// dash-spv's `Uncertain` result makes the root due at once, even inside + /// the same block as an earlier probe. + #[tokio::test] + async fn should_probe_a_forced_root_regardless_of_its_schedule() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); + let views = [send(1, &[outpoint(90, 0)])]; + + resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + let forced = resolve_pass(&state, &probe, wallet(), 100, &views, Some(txid(1))).await; + + assert_eq!(forced.len(), 1); + } + + /// A root that settled or left the wallet between passes is forgotten, + /// so a later reappearance starts a fresh schedule. + #[tokio::test] + async fn should_forget_a_root_that_is_no_longer_ambiguous() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); + let views = [send(1, &[outpoint(90, 0)])]; + + resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + resolve_pass(&state, &probe, wallet(), 101, &[], None).await; + + let guard = state.lock().expect("state"); + assert!(guard.schedules.is_empty()); + assert!(guard.resolved.is_empty()); + } + + /// Two wallets' roots are tracked independently. + #[tokio::test] + async fn should_not_let_one_wallets_pass_forget_another_wallets_roots() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); + let views = [send(1, &[outpoint(90, 0)])]; + + resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + resolve_pass(&state, &probe, [9u8; 32], 100, &[], None).await; + + assert_eq!(state.lock().expect("state").schedules.len(), 1); + } +} diff --git a/packages/rs-platform-wallet/src/wallet/core/mod.rs b/packages/rs-platform-wallet/src/wallet/core/mod.rs index 2d32e7e615e..da7e1d86492 100644 --- a/packages/rs-platform-wallet/src/wallet/core/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/core/mod.rs @@ -1,7 +1,10 @@ +// Used by the user-driven cancel of a proven-dead send, which lands separately. +#[allow(dead_code)] pub(crate) mod abandon_plan; pub mod balance; pub mod balance_handler; mod broadcast; +pub(crate) mod broadcast_resolver; pub mod generation; // Inherent `CoreWallet::sign_message` only — no types to re-export. mod sign_message; From 91455b7378013eee4bc039a965b668dd4b765487 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 14:15:51 +0300 Subject: [PATCH 004/169] feat(platform-wallet-ffi,swift-sdk): deliver broadcast-probe verdicts to the host and let it switch probing on MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - FFI: `on_outgoing_transaction_probed_fn(context, wallet_id, txid, verdict, reason)` appended to EventHandlerCallbacksExtension within version 1 — a host whose struct_size ends before it never receives it. txid is in wire byte order; verdict is OUTGOING_PROBE_VERDICT_{ACCEPTED,DEAD,UNRESOLVED}; reason is diagnostic only. `platform_wallet_manager_set_broadcast_probe_enabled` turns the resolver on (off by default). - Swift: OutgoingTransactionVerdict / OutgoingTransactionProbeEvent (with the display txid), published per txid in `outgoingTransactionVerdicts` and as `lastOutgoingTransactionProbe`; `setBroadcastProbeEnabled(_:)`. The trampoline copies every pointer before the main-actor hop and drops events after shutdown was requested. Tests: 4 new FFI tests (slot negotiation for old and current hosts, an unknown version, per-verdict delivery with code, wire txid and reason); FFI crate 388/388. Swift SDK 720/721 — the one failure, DashModelMigrationTests.testCaptureHistoricalV2Fixture, is flaky and fails on v4.3-dev without this change too. Ticket 32347. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/event_handler.rs | 164 ++++++++++++++++++ .../rs-platform-wallet-ffi/src/manager.rs | 117 ++++++++++++- .../PlatformWalletManager.swift | 9 + .../PlatformWalletManagerAddressSync.swift | 1 + .../PlatformWalletManagerBroadcastProbe.swift | 94 ++++++++++ 5 files changed, 384 insertions(+), 1 deletion(-) create mode 100644 packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift diff --git a/packages/rs-platform-wallet-ffi/src/event_handler.rs b/packages/rs-platform-wallet-ffi/src/event_handler.rs index 0db7649a7c6..e5f7b07b139 100644 --- a/packages/rs-platform-wallet-ffi/src/event_handler.rs +++ b/packages/rs-platform-wallet-ffi/src/event_handler.rs @@ -49,6 +49,25 @@ pub type DpnsMarketplaceSyncCompletedFn = unsafe extern "C" fn( sync_unix_seconds: u64, ); +/// Verdict codes for [`OutgoingTransactionProbedFn`]. +pub const OUTGOING_PROBE_VERDICT_ACCEPTED: u8 = 0; +pub const OUTGOING_PROBE_VERDICT_DEAD: u8 = 1; +pub const OUTGOING_PROBE_VERDICT_UNRESOLVED: u8 = 2; + +/// A verdict on an unconfirmed send whose broadcast outcome was unknown. +/// `wallet_id` and `txid` point to 32 bytes each; `txid` is in wire +/// (internal) byte order — reverse it for the display hex. `verdict` is one of +/// the `OUTGOING_PROBE_VERDICT_*` codes; `reason` is null for `ACCEPTED`, +/// otherwise a NUL-terminated diagnostic string (never user-facing copy). All +/// pointers are valid only for the duration of the call. +pub type OutgoingTransactionProbedFn = unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + txid: *const u8, + verdict: u8, + reason: *const c_char, +); + /// Size/version-tagged event extension. It shares the legacy event /// vtable's context and destructor; only callback pointers are copied. /// This avoids growing [`EventHandlerCallbacks`] and over-reading callers @@ -70,6 +89,17 @@ pub struct EventHandlerCallbacksExtension { sync_unix_seconds: u64, ), >, + /// Appended within version 1: a host whose `struct_size` stops before + /// this slot simply never receives it. See [`OutgoingTransactionProbedFn`]. + pub on_outgoing_transaction_probed_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + txid: *const u8, + verdict: u8, + reason: *const c_char, + ), + >, } impl Default for EventHandlerCallbacksExtension { @@ -79,6 +109,7 @@ impl Default for EventHandlerCallbacksExtension { version: PLATFORM_WALLET_EVENT_CALLBACKS_EXTENSION_VERSION, reserved: 0, on_dpns_marketplace_sync_completed_fn: None, + on_outgoing_transaction_probed_fn: None, } } } @@ -189,16 +220,19 @@ unsafe impl Sync for EventHandlerCallbacks {} pub(crate) struct FFIEventHandler { callbacks: EventHandlerCallbacks, dpns_sync_callback: Option, + outgoing_probe_callback: Option, } impl FFIEventHandler { pub fn new( callbacks: EventHandlerCallbacks, dpns_sync_callback: Option, + outgoing_probe_callback: Option, ) -> Self { Self { callbacks, dpns_sync_callback, + outgoing_probe_callback, } } } @@ -250,6 +284,36 @@ impl EventHandler for FFIEventHandler { } impl PlatformEventHandler for FFIEventHandler { + fn on_outgoing_transaction_probed( + &self, + wallet_id: &[u8; 32], + txid: &dashcore::Txid, + verdict: &platform_wallet::broadcast_probe::ProbeVerdict, + ) { + use platform_wallet::broadcast_probe::ProbeVerdict; + let Some(callback) = self.outgoing_probe_callback else { + return; + }; + let (code, reason) = match verdict { + ProbeVerdict::Accepted => (OUTGOING_PROBE_VERDICT_ACCEPTED, None), + ProbeVerdict::Dead { reason } => (OUTGOING_PROBE_VERDICT_DEAD, Some(reason)), + ProbeVerdict::Unresolved { reason } => { + (OUTGOING_PROBE_VERDICT_UNRESOLVED, Some(reason)) + } + }; + let reason = reason.and_then(|reason| std::ffi::CString::new(reason.as_str()).ok()); + let txid_bytes: &[u8] = txid.as_ref(); + unsafe { + callback( + self.callbacks.context, + wallet_id.as_ptr(), + txid_bytes.as_ptr(), + code, + reason.as_ref().map_or(std::ptr::null(), |r| r.as_ptr()), + ); + } + } + fn on_dpns_marketplace_sync_completed(&self, summary: &DpnsSyncPassSummary) { let Some(callback) = self.dpns_sync_callback else { return; @@ -496,6 +560,7 @@ mod release_tests { release_fn: None, }, None, + None, ); handler.on_shielded_sync_completed(&ShieldedSyncPassSummary { wallet_results: BTreeMap::from([ @@ -567,6 +632,7 @@ mod release_tests { release_fn: Some(count_release), }, None, + None, )); let straggler = Arc::clone(&handler); @@ -577,3 +643,101 @@ mod release_tests { assert_eq!(releases.load(Ordering::SeqCst), 1); } } + +#[cfg(test)] +mod outgoing_probe_callback_tests { + use std::ffi::CStr; + use std::sync::Mutex; + + use dashcore::hashes::Hash; + use platform_wallet::broadcast_probe::ProbeVerdict; + + use super::*; + + /// (wallet_id, txid, verdict, reason) per call. + static CALLS: Mutex)>> = Mutex::new(Vec::new()); + + unsafe extern "C" fn record( + _context: *mut c_void, + wallet_id: *const u8, + txid: *const u8, + verdict: u8, + reason: *const c_char, + ) { + let wallet_id = std::ptr::read(wallet_id as *const [u8; 32]); + let txid = std::ptr::read(txid as *const [u8; 32]); + let reason = + (!reason.is_null()).then(|| CStr::from_ptr(reason).to_string_lossy().into_owned()); + CALLS + .lock() + .expect("calls") + .push((wallet_id, txid, verdict, reason)); + } + + fn handler(probe: Option) -> FFIEventHandler { + FFIEventHandler::new( + EventHandlerCallbacks { + context: std::ptr::null_mut(), + on_wallet_event_fn: None, + on_error_fn: None, + on_platform_address_sync_completed_fn: None, + on_shielded_sync_completed_fn: None, + ..unsafe { std::mem::zeroed() } + }, + None, + probe, + ) + } + + /// One test drives every case so the shared capture is never raced by a + /// parallel test. + #[test] + fn should_deliver_each_verdict_with_its_code_wire_txid_and_reason() { + CALLS.lock().expect("calls").clear(); + let wallet = [7u8; 32]; + let txid = dashcore::Txid::from_byte_array([ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, + ]); + let with_callback = handler(Some(record)); + + with_callback.on_outgoing_transaction_probed(&wallet, &txid, &ProbeVerdict::Accepted); + with_callback.on_outgoing_transaction_probed( + &wallet, + &txid, + &ProbeVerdict::Dead { + reason: "bad-txns-inputs-missingorspent".to_string(), + }, + ); + with_callback.on_outgoing_transaction_probed( + &wallet, + &txid, + &ProbeVerdict::Unresolved { + reason: "no quorum".to_string(), + }, + ); + // A host without the slot receives nothing. + handler(None).on_outgoing_transaction_probed(&wallet, &txid, &ProbeVerdict::Accepted); + + let calls = CALLS.lock().expect("calls").clone(); + let wire: [u8; 32] = *txid.as_byte_array(); + assert_eq!( + calls, + vec![ + (wallet, wire, OUTGOING_PROBE_VERDICT_ACCEPTED, None), + ( + wallet, + wire, + OUTGOING_PROBE_VERDICT_DEAD, + Some("bad-txns-inputs-missingorspent".to_string()) + ), + ( + wallet, + wire, + OUTGOING_PROBE_VERDICT_UNRESOLVED, + Some("no quorum".to_string()) + ), + ] + ); + } +} diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 20f0c421b20..543de2fc8ba 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -4,7 +4,8 @@ use crate::check_ptr; use crate::error::*; use crate::event_handler::{ DpnsMarketplaceSyncCompletedFn, EventHandlerCallbacks, EventHandlerCallbacksExtension, - FFIEventHandler, PLATFORM_WALLET_EVENT_CALLBACKS_EXTENSION_VERSION, + FFIEventHandler, OutgoingTransactionProbedFn, + PLATFORM_WALLET_EVENT_CALLBACKS_EXTENSION_VERSION, }; use crate::handle::*; use crate::persistence::{ @@ -80,6 +81,7 @@ pub unsafe extern "C" fn platform_wallet_manager_create( PersistenceCapabilities::NONE, PersistenceExtensionCallbacks::default(), None, + None, out_handle, ) } @@ -106,6 +108,7 @@ pub unsafe extern "C" fn platform_wallet_manager_create_with_persistence_capabil declaration, PersistenceExtensionCallbacks::default(), None, + None, out_handle, ) } @@ -139,6 +142,7 @@ pub unsafe extern "C" fn platform_wallet_manager_create_with_persistence_extensi declaration, extensions, None, + None, out_handle, ) } @@ -163,6 +167,7 @@ pub unsafe extern "C" fn platform_wallet_manager_create_with_extensions( let declaration = persistence_capabilities_declaration(&*persistence_capabilities); let persistence_extensions = persistence_extension_callbacks(persistence_extension); let dpns_event_callback = event_extension_dpns_callback(event_extension); + let outgoing_probe_callback = event_extension_outgoing_probe_callback(event_extension); platform_wallet_manager_create_impl( sdk_ptr, persistence, @@ -170,6 +175,7 @@ pub unsafe extern "C" fn platform_wallet_manager_create_with_extensions( declaration, persistence_extensions, dpns_event_callback, + outgoing_probe_callback, out_handle, ) } @@ -299,6 +305,25 @@ unsafe fn event_extension_dpns_callback( ) } +unsafe fn event_extension_outgoing_probe_callback( + extension: *const EventHandlerCallbacksExtension, +) -> Option { + let supplied_size = std::ptr::addr_of!((*extension).struct_size).read(); + let version_end = + std::mem::offset_of!(EventHandlerCallbacksExtension, version) + std::mem::size_of::(); + let version_ok = supplied_size >= version_end + && std::ptr::addr_of!((*extension).version).read() + == PLATFORM_WALLET_EVENT_CALLBACKS_EXTENSION_VERSION; + negotiated_extension_slot!( + extension, + EventHandlerCallbacksExtension, + supplied_size, + version_ok, + on_outgoing_transaction_probed_fn, + OutgoingTransactionProbedFn + ) +} + // The C entry point's own shape: every callback table and out-param the // hosts pass, threaded straight through. Splitting it would only move the // same arguments behind a struct the FFI cannot express. @@ -310,6 +335,7 @@ unsafe fn platform_wallet_manager_create_impl( declared_capabilities: PersistenceCapabilities, persistence_extensions: PersistenceExtensionCallbacks, dpns_event_callback: Option, + outgoing_probe_callback: Option, out_handle: *mut Handle, ) -> PlatformWalletFFIResult { check_ptr!(sdk_ptr); @@ -355,6 +381,7 @@ unsafe fn platform_wallet_manager_create_impl( let handler: Arc = Arc::new(FFIEventHandler::new( std::ptr::read(event_handler), dpns_event_callback, + outgoing_probe_callback, )); // `PlatformWalletManager::new` spawns the wallet-event adapter @@ -2115,3 +2142,91 @@ mod remove_wallet_lifecycle_tests { }); } } + +/// Turn automatic probing of unconfirmed sends on or off (off by default). +/// +/// While on, every unconfirmed send whose broadcast outcome went quiet is +/// resubmitted to evonodes over DAPI — right after SPV reports it uncertain, +/// then once per block — and each verdict reaches the host through +/// `EventHandlerCallbacksExtension::on_outgoing_transaction_probed_fn`. +/// Nothing in the wallet changes. +/// +/// # Safety +/// `handle` must be a live manager handle. +#[no_mangle] +pub unsafe extern "C" fn platform_wallet_manager_set_broadcast_probe_enabled( + handle: Handle, + enabled: bool, +) -> PlatformWalletFFIResult { + let option = PLATFORM_WALLET_MANAGER_STORAGE.with_item(handle, |manager| { + manager.set_broadcast_probe_enabled(enabled); + }); + unwrap_option_or_return!(option); + PlatformWalletFFIResult::ok() +} + +#[cfg(test)] +mod outgoing_probe_extension_tests { + use std::os::raw::{c_char, c_void}; + + use super::*; + use crate::event_handler::DpnsSyncWalletResultFFI; + + unsafe extern "C" fn probed( + _context: *mut c_void, + _wallet_id: *const u8, + _txid: *const u8, + _verdict: u8, + _reason: *const c_char, + ) { + } + + unsafe extern "C" fn dpns( + _context: *mut c_void, + _results: *const DpnsSyncWalletResultFFI, + _count: usize, + _sync_unix_seconds: u64, + ) { + } + + fn both_slots() -> EventHandlerCallbacksExtension { + EventHandlerCallbacksExtension { + on_dpns_marketplace_sync_completed_fn: Some(dpns), + on_outgoing_transaction_probed_fn: Some(probed), + ..Default::default() + } + } + + /// A host built before the probe slot existed declares a `struct_size` + /// that ends at the DPNS slot. Its bytes past that point are not its own, + /// so the probe slot must be refused while DPNS keeps working. + #[test] + fn should_not_read_the_probe_slot_from_a_host_whose_extension_ends_before_it() { + let mut ext = both_slots(); + ext.struct_size = std::mem::offset_of!( + EventHandlerCallbacksExtension, + on_outgoing_transaction_probed_fn + ); + unsafe { + assert!(event_extension_dpns_callback(&ext).is_some()); + assert!(event_extension_outgoing_probe_callback(&ext).is_none()); + } + } + + #[test] + fn should_read_the_probe_slot_from_a_current_host() { + let ext = both_slots(); + unsafe { + assert!(event_extension_outgoing_probe_callback(&ext).is_some()); + } + } + + #[test] + fn should_refuse_the_probe_slot_under_an_unknown_version() { + let mut ext = both_slots(); + ext.version = PLATFORM_WALLET_EVENT_CALLBACKS_EXTENSION_VERSION + 1; + unsafe { + assert!(event_extension_outgoing_probe_callback(&ext).is_none()); + } + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift index 433250e87b8..5976d9bbeb9 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift @@ -455,6 +455,15 @@ public class PlatformWalletManager: ObservableObject { /// Rust. Every native pointer has been copied before publication. @Published public internal(set) var lastDpnsSyncEvent: DpnsSyncEvent? + /// The latest broadcast-probe verdict per unconfirmed send, keyed by the + /// txid in wire (internal) byte order. Filled only while + /// `setBroadcastProbeEnabled(true)` is on. See `OutgoingTransactionProbeEvent`. + @Published public internal(set) var outgoingTransactionVerdicts: [Data: OutgoingTransactionProbeEvent] = [:] + + /// The most recent broadcast-probe verdict, for observers that react to + /// each one as it arrives. + @Published public internal(set) var lastOutgoingTransactionProbe: OutgoingTransactionProbeEvent? + /// Cumulative number of encrypted notes scanned in the **current** /// in-flight shielded sync pass, published once per chunk (~every /// 2048 notes) via the Rust-side progress callback. Nil between diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerAddressSync.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerAddressSync.swift index 47b17f2bc94..1983f7c7484 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerAddressSync.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerAddressSync.swift @@ -86,6 +86,7 @@ final class PlatformWalletEventHandler: @unchecked Sendable { callbacks.version = UInt32(PLATFORM_WALLET_EVENT_CALLBACKS_EXTENSION_VERSION) callbacks.reserved = 0 callbacks.on_dpns_marketplace_sync_completed_fn = dpnsMarketplaceSyncCompletedCallback + callbacks.on_outgoing_transaction_probed_fn = outgoingTransactionProbedCallback return callbacks } } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift new file mode 100644 index 00000000000..1474aa6c85e --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -0,0 +1,94 @@ +import Foundation +import DashSDKFFI + +/// What the network said about an unconfirmed send whose broadcast outcome +/// was unknown ("Transaction status unknown"). +public enum OutgoingTransactionVerdict: Sendable, Equatable { + /// A node holds the transaction: it is in a mempool or a block. + case accepted + /// Two distinct nodes proved it can never be mined (for example its input + /// no longer exists). `reason` is diagnostic, never user-facing copy. + case dead(reason: String) + /// No verdict yet; the SDK asks again on a later block. + case unresolved(reason: String) +} + +/// One broadcast-probe verdict. Nothing in the wallet changed because of it. +public struct OutgoingTransactionProbeEvent: Sendable, Equatable { + public let walletId: Data + /// 32 bytes, wire (internal) byte order. + public let txidWire: Data + public let verdict: OutgoingTransactionVerdict + + /// The txid as explorers and the UI display it (byte-reversed hex). + public var txidDisplayHex: String { + txidWire.reversed().map { String(format: "%02x", $0) }.joined() + } + + public init(walletId: Data, txidWire: Data, verdict: OutgoingTransactionVerdict) { + self.walletId = walletId + self.txidWire = txidWire + self.verdict = verdict + } +} + +/// C trampoline for +/// `EventHandlerCallbacksExtension.on_outgoing_transaction_probed_fn`. Rust +/// owns every pointer only for this call, so everything is copied before the +/// main-actor hop. +func outgoingTransactionProbedCallback( + context: UnsafeMutableRawPointer?, + walletIdPtr: UnsafePointer?, + txidPtr: UnsafePointer?, + verdictCode: UInt8, + reasonPtr: UnsafePointer? +) { + guard let context, let walletIdPtr, let txidPtr else { return } + let handler = Unmanaged + .fromOpaque(context) + .takeUnretainedValue() + + let reason = reasonPtr.map { String(cString: $0) } ?? "" + let verdict: OutgoingTransactionVerdict + switch verdictCode { + case UInt8(OUTGOING_PROBE_VERDICT_ACCEPTED): + verdict = .accepted + case UInt8(OUTGOING_PROBE_VERDICT_DEAD): + verdict = .dead(reason: reason) + default: + verdict = .unresolved(reason: reason) + } + let event = OutgoingTransactionProbeEvent( + walletId: Data(bytes: walletIdPtr, count: 32), + txidWire: Data(bytes: txidPtr, count: 32), + verdict: verdict + ) + + Task { @MainActor [weak manager = handler.manager] in + manager?.handleOutgoingTransactionProbed(event) + } +} + +extension PlatformWalletManager { + /// Turn automatic probing of unconfirmed sends on or off (off by default). + /// + /// While on, the SDK resubmits every unconfirmed send whose broadcast + /// outcome went quiet to evonodes over DAPI — right after SPV reports it + /// uncertain, then once per block — and publishes each verdict in + /// `outgoingTransactionVerdicts` / `lastOutgoingTransactionProbe`. + /// Resubmitting sends the same signed bytes: it can deliver a payment, it + /// can never create a second one. Nothing in the wallet changes. + public func setBroadcastProbeEnabled(_ enabled: Bool) throws { + guard isConfigured, handle != NULL_HANDLE else { + throw PlatformWalletError.invalidHandle("PlatformWalletManager not configured") + } + try platform_wallet_manager_set_broadcast_probe_enabled(handle, enabled).check() + } + + @MainActor + func handleOutgoingTransactionProbed(_ event: OutgoingTransactionProbeEvent) { + guard !shutdownRequested, isConfigured else { return } + outgoingTransactionVerdicts[event.txidWire] = event + lastOutgoingTransactionProbe = event + } +} From 9d144766501abb55a1a493858f64f3675f26a8ee Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 15:41:45 +0300 Subject: [PATCH 005/169] chore(platform-wallet): log the height and trigger of each broadcast probe The verdict line now says whether dash-spv's Uncertain result or a new block started the pass, and at which height. In the 2026-09-29 simulator run the one verdict could have come from either, and the log could not tell them apart. Ticket 32347. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 383b771a497..80c16329086 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -342,10 +342,19 @@ impl BroadcastResolver { return; } let events = events.upgrade(); + // What started this pass: dash-spv's Uncertain result for one + // transaction, or a new block for every due root. + let trigger = if force.is_some() { + "uncertain" + } else { + "block" + }; for (txid, verdict) in &verdicts { tracing::info!( wallet_id = %hex::encode(wallet_id), txid = %txid, + height, + trigger, ?verdict, "broadcast probe: verdict for an unconfirmed send" ); From c5f3a172086e718d5042ecd31673663acd1c8c48 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 16:22:31 +0300 Subject: [PATCH 006/169] fix(platform-wallet): address review of the broadcast probe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - A mined transaction whose outputs are all spent is refused with -25 missingorspent too — Core recognises a mined tx only while one of its outputs is unspent (measured on testnet: e758f06a…, block 1560020). A dead quorum is now confirmed by DAPI getTransaction: a node that knows the txid makes it Accepted; only two distinct nodes not knowing it make it Dead. - DapiBroadcaster no longer turns one node's rejection into Rejected: only AlreadyExists is success, everything else stays MaybeSent, as the Rejected contract requires proof that the tx never entered the network. - A send that spends an unconfirmed incoming coin is not a root: nodes that have not seen that parent refuse it. - Only Dead ends probing; an Accepted root keeps being asked until it settles. Verdicts are published on change only, and a root that settles or leaves the wallet after a verdict is published as cleared (on_outgoing_transaction_cleared / OUTGOING_PROBE_VERDICT_CLEARED); Swift keys verdicts by wallet + txid and drops cleared ones. - At most one height-driven pass per wallet at a time. - FFI negotiates both extension slots from one header snapshot. - Shutdown reports probe drains as WalletWorker::BroadcastProbes and drains them concurrently with the payment hooks. - Docs back on sdk(), imports hoisted, test renamed to "should …". Tests: platform-wallet 1202/1202, FFI 388/388, SQLite sweeps 44/44; Swift 720/721 (the known-flaky testCaptureHistoricalV2Fixture, also on v4.3-dev). Live on testnet: e758f06a… Accepted, a phantom-input tx Dead. Ticket 32347. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/event_handler.rs | 41 ++- .../rs-platform-wallet-ffi/src/manager.rs | 67 ++-- .../tests/sqlite_transaction_sweeps.rs | 2 +- .../rs-platform-wallet/src/broadcast_probe.rs | 247 +++++++++++++-- .../rs-platform-wallet/src/broadcaster.rs | 71 ++--- packages/rs-platform-wallet/src/events.rs | 30 +- .../src/manager/accessors.rs | 2 +- .../rs-platform-wallet/src/manager/mod.rs | 52 ++-- .../src/wallet/core/broadcast_resolver.rs | 290 +++++++++++++----- .../PlatformWalletManager.swift | 9 +- .../PlatformWalletManagerBroadcastProbe.swift | 43 ++- 11 files changed, 624 insertions(+), 230 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/event_handler.rs b/packages/rs-platform-wallet-ffi/src/event_handler.rs index e5f7b07b139..bbd700f1450 100644 --- a/packages/rs-platform-wallet-ffi/src/event_handler.rs +++ b/packages/rs-platform-wallet-ffi/src/event_handler.rs @@ -4,6 +4,8 @@ use crate::platform_address_sync::{ PlatformAddressSyncMetricsFFI, PlatformAddressSyncWalletResultFFI, }; use crate::shielded_types::ShieldedSyncWalletResultFFI; +use dashcore::Txid; +use platform_wallet::broadcast_probe::ProbeVerdict; use platform_wallet::events::{EventHandler, PlatformEventHandler, WalletEvent}; use platform_wallet::manager::dpns_sync::{DpnsSyncPassSummary, WalletDpnsSyncOutcome}; #[cfg(feature = "shielded")] @@ -53,13 +55,16 @@ pub type DpnsMarketplaceSyncCompletedFn = unsafe extern "C" fn( pub const OUTGOING_PROBE_VERDICT_ACCEPTED: u8 = 0; pub const OUTGOING_PROBE_VERDICT_DEAD: u8 = 1; pub const OUTGOING_PROBE_VERDICT_UNRESOLVED: u8 = 2; +/// Not a verdict: the send is no longer unconfirmed (it settled or left the +/// wallet); drop whatever was kept for its earlier verdict. `reason` is null. +pub const OUTGOING_PROBE_VERDICT_CLEARED: u8 = 3; /// A verdict on an unconfirmed send whose broadcast outcome was unknown. /// `wallet_id` and `txid` point to 32 bytes each; `txid` is in wire /// (internal) byte order — reverse it for the display hex. `verdict` is one of -/// the `OUTGOING_PROBE_VERDICT_*` codes; `reason` is null for `ACCEPTED`, -/// otherwise a NUL-terminated diagnostic string (never user-facing copy). All -/// pointers are valid only for the duration of the call. +/// the `OUTGOING_PROBE_VERDICT_*` codes; `reason` is null for `ACCEPTED` and +/// `CLEARED`, otherwise a NUL-terminated diagnostic string (never user-facing +/// copy). All pointers are valid only for the duration of the call. pub type OutgoingTransactionProbedFn = unsafe extern "C" fn( context: *mut c_void, wallet_id: *const u8, @@ -284,13 +289,28 @@ impl EventHandler for FFIEventHandler { } impl PlatformEventHandler for FFIEventHandler { + fn on_outgoing_transaction_cleared(&self, wallet_id: &[u8; 32], txid: &Txid) { + let Some(callback) = self.outgoing_probe_callback else { + return; + }; + let txid_bytes: &[u8] = txid.as_ref(); + unsafe { + callback( + self.callbacks.context, + wallet_id.as_ptr(), + txid_bytes.as_ptr(), + OUTGOING_PROBE_VERDICT_CLEARED, + std::ptr::null(), + ); + } + } + fn on_outgoing_transaction_probed( &self, wallet_id: &[u8; 32], - txid: &dashcore::Txid, - verdict: &platform_wallet::broadcast_probe::ProbeVerdict, + txid: &Txid, + verdict: &ProbeVerdict, ) { - use platform_wallet::broadcast_probe::ProbeVerdict; let Some(callback) = self.outgoing_probe_callback else { return; }; @@ -654,8 +674,10 @@ mod outgoing_probe_callback_tests { use super::*; - /// (wallet_id, txid, verdict, reason) per call. - static CALLS: Mutex)>> = Mutex::new(Vec::new()); + /// (wallet_id, txid, verdict, reason) of one call. + type Call = ([u8; 32], [u8; 32], u8, Option); + + static CALLS: Mutex> = Mutex::new(Vec::new()); unsafe extern "C" fn record( _context: *mut c_void, @@ -716,8 +738,10 @@ mod outgoing_probe_callback_tests { reason: "no quorum".to_string(), }, ); + with_callback.on_outgoing_transaction_cleared(&wallet, &txid); // A host without the slot receives nothing. handler(None).on_outgoing_transaction_probed(&wallet, &txid, &ProbeVerdict::Accepted); + handler(None).on_outgoing_transaction_cleared(&wallet, &txid); let calls = CALLS.lock().expect("calls").clone(); let wire: [u8; 32] = *txid.as_byte_array(); @@ -737,6 +761,7 @@ mod outgoing_probe_callback_tests { OUTGOING_PROBE_VERDICT_UNRESOLVED, Some("no quorum".to_string()) ), + (wallet, wire, OUTGOING_PROBE_VERDICT_CLEARED, None), ] ); } diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 543de2fc8ba..5b0b8f04ba5 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -166,8 +166,9 @@ pub unsafe extern "C" fn platform_wallet_manager_create_with_extensions( check_ptr!(event_extension); let declaration = persistence_capabilities_declaration(&*persistence_capabilities); let persistence_extensions = persistence_extension_callbacks(persistence_extension); - let dpns_event_callback = event_extension_dpns_callback(event_extension); - let outgoing_probe_callback = event_extension_outgoing_probe_callback(event_extension); + let event_callbacks = event_extension_callbacks(event_extension); + let dpns_event_callback = event_callbacks.dpns; + let outgoing_probe_callback = event_callbacks.outgoing_probe; platform_wallet_manager_create_impl( sdk_ptr, persistence, @@ -286,42 +287,42 @@ unsafe fn persistence_extension_callbacks( } } -unsafe fn event_extension_dpns_callback( - extension: *const EventHandlerCallbacksExtension, -) -> Option { - let supplied_size = std::ptr::addr_of!((*extension).struct_size).read(); - let version_end = - std::mem::offset_of!(EventHandlerCallbacksExtension, version) + std::mem::size_of::(); - let version_ok = supplied_size >= version_end - && std::ptr::addr_of!((*extension).version).read() - == PLATFORM_WALLET_EVENT_CALLBACKS_EXTENSION_VERSION; - negotiated_extension_slot!( - extension, - EventHandlerCallbacksExtension, - supplied_size, - version_ok, - on_dpns_marketplace_sync_completed_fn, - DpnsMarketplaceSyncCompletedFn - ) +/// The additive event callbacks a host supplied, negotiated from ONE snapshot +/// of the extension header — `struct_size` and `version` are read once, so +/// every slot is judged against the same declared layout even if host memory +/// changes under us (see [`persistence_extension_callbacks`]). +struct EventExtensionCallbacks { + dpns: Option, + outgoing_probe: Option, } -unsafe fn event_extension_outgoing_probe_callback( +unsafe fn event_extension_callbacks( extension: *const EventHandlerCallbacksExtension, -) -> Option { +) -> EventExtensionCallbacks { let supplied_size = std::ptr::addr_of!((*extension).struct_size).read(); let version_end = std::mem::offset_of!(EventHandlerCallbacksExtension, version) + std::mem::size_of::(); let version_ok = supplied_size >= version_end && std::ptr::addr_of!((*extension).version).read() == PLATFORM_WALLET_EVENT_CALLBACKS_EXTENSION_VERSION; - negotiated_extension_slot!( - extension, - EventHandlerCallbacksExtension, - supplied_size, - version_ok, - on_outgoing_transaction_probed_fn, - OutgoingTransactionProbedFn - ) + EventExtensionCallbacks { + dpns: negotiated_extension_slot!( + extension, + EventHandlerCallbacksExtension, + supplied_size, + version_ok, + on_dpns_marketplace_sync_completed_fn, + DpnsMarketplaceSyncCompletedFn + ), + outgoing_probe: negotiated_extension_slot!( + extension, + EventHandlerCallbacksExtension, + supplied_size, + version_ok, + on_outgoing_transaction_probed_fn, + OutgoingTransactionProbedFn + ), + } } // The C entry point's own shape: every callback table and out-param the @@ -2208,8 +2209,8 @@ mod outgoing_probe_extension_tests { on_outgoing_transaction_probed_fn ); unsafe { - assert!(event_extension_dpns_callback(&ext).is_some()); - assert!(event_extension_outgoing_probe_callback(&ext).is_none()); + assert!(event_extension_callbacks(&ext).dpns.is_some()); + assert!(event_extension_callbacks(&ext).outgoing_probe.is_none()); } } @@ -2217,7 +2218,7 @@ mod outgoing_probe_extension_tests { fn should_read_the_probe_slot_from_a_current_host() { let ext = both_slots(); unsafe { - assert!(event_extension_outgoing_probe_callback(&ext).is_some()); + assert!(event_extension_callbacks(&ext).outgoing_probe.is_some()); } } @@ -2226,7 +2227,7 @@ mod outgoing_probe_extension_tests { let mut ext = both_slots(); ext.version = PLATFORM_WALLET_EVENT_CALLBACKS_EXTENSION_VERSION + 1; unsafe { - assert!(event_extension_outgoing_probe_callback(&ext).is_none()); + assert!(event_extension_callbacks(&ext).outgoing_probe.is_none()); } } } diff --git a/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs b/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs index e54ea424686..a4db6a4204c 100644 --- a/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs +++ b/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs @@ -3834,7 +3834,7 @@ fn should_remove_a_swept_height_only_transaction_and_its_outputs() { /// Shape of ticket 32347: root R spends a real coin F:0 and leaves change /// R:1; child C spends that change plus another real coin F:1. #[test] -fn an_abandoned_chain_frees_its_outside_inputs_and_holds_nothing() { +fn should_free_an_abandoned_chains_outside_inputs_and_hold_nothing() { let (persister, _tmp, _path) = fresh_persister(); let w: WalletId = wid(0xE9); ensure_wallet_meta(&persister, &w); diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index ce64da6cf52..e301f8020b3 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -16,10 +16,20 @@ //! | transaction | Core answer | //! |-----------------------------------------------|-------------------------------------------| //! | already in the mempool | success, the txid | -//! | already in a block | `-27 Transaction already in block chain` | +//! | already in a block, an output still unspent | `-27 Transaction already in block chain` | +//! | already in a block, **every output spent** | `-25 bad-txns-inputs-missingorspent` | //! | spends an input that does not exist | `-25 bad-txns-inputs-missingorspent` | //! | spends an input an IS-locked tx already spent | `-26 tx-txlock-conflict` | //! +//! The second row is why a rejection alone is never enough to call a +//! transaction dead: Core recognises a mined transaction only while one of its +//! outputs is still in the UTXO set, and otherwise evaluates it like a new one +//! whose inputs are gone (measured on testnet 2026-09-29: `e758f06a…`, block +//! 1560020, both outputs spent → `-25`). Before a dead verdict the probe +//! therefore asks nodes whether they know the txid at all (DAPI +//! `getTransaction`, served from the evonode's transaction index); a node that +//! knows it settles the verdict as accepted. +//! //! The two DAPI implementations surface those codes differently — the JS //! server maps `-25` to `InvalidArgument`, rs-dapi to `FailedPrecondition` — //! so the classification keys on Core's reason text, which both pass through @@ -29,9 +39,14 @@ //! [`BroadcastError::MaybeSent`]: crate::broadcaster::BroadcastError::MaybeSent use std::collections::HashSet; +use std::sync::Arc; use async_trait::async_trait; -use dashcore::Transaction; +use dash_sdk::dapi_client::transport::TransportError; +use dash_sdk::dapi_client::{DapiClientError, DapiRequestExecutor, RequestSettings}; +use dash_sdk::dapi_grpc::core::v0::{BroadcastTransactionRequest, GetTransactionRequest}; +use dash_sdk::dapi_grpc::tonic::Code; +use dashcore::{Transaction, Txid}; /// Core reject reasons that prove the transaction can never be mined as it /// stands. @@ -73,12 +88,7 @@ pub enum NodeVerdict { } /// Classify a failed `broadcastTransaction` gRPC response. -pub(crate) fn classify_failed_submission( - code: dash_sdk::dapi_grpc::tonic::Code, - message: &str, -) -> NodeVerdict { - use dash_sdk::dapi_grpc::tonic::Code; - +pub(crate) fn classify_failed_submission(code: Code, message: &str) -> NodeVerdict { if code == Code::AlreadyExists { return NodeVerdict::Accepted; } @@ -106,23 +116,40 @@ pub(crate) fn classify_failed_submission( pub enum ProbeVerdict { /// A node holds the transaction: it is in a mempool or a block. Accepted, - /// Two different nodes proved the transaction can never be mined. + /// Two different nodes refused it for missing or spent inputs, and two + /// different nodes do not know its txid: it is not in a block and can + /// never be mined. Dead { reason: String }, /// Not enough evidence either way; the transaction stays ambiguous. Unresolved { reason: String }, } -/// Submits a signed transaction to one Core node and reports which node -/// answered and what it said. A seam so the probe's evidence rules can be -/// tested without a network. +/// Whether one node knows a txid (DAPI `getTransaction`). +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum LookupAnswer { + /// The node has the transaction, in its mempool or in a block. + Known, + /// The node answered and does not have it. + NotFound, + /// No answer: transport failure, timeout, anything else. + Unknown { reason: String }, +} + +/// Talks to one Core node per call and reports which node answered and what +/// it said. A seam so the probe's evidence rules can be tested without a +/// network. #[async_trait] pub(crate) trait NodeSubmitter: Send + Sync { async fn submit(&self, transaction: &Transaction) -> (Option, NodeVerdict); + async fn lookup(&self, txid: &Txid) -> (Option, LookupAnswer); } /// How many submissions one probe may spend looking for a verdict. const MAX_SUBMISSIONS_PER_PROBE: usize = 4; +/// How many txid lookups one probe may spend confirming a dead verdict. +const MAX_LOOKUPS_PER_PROBE: usize = 4; + /// How many *distinct* nodes must independently prove a transaction dead. /// /// One node is not enough: a node lagging behind the tip has not seen a @@ -144,8 +171,10 @@ pub trait AcceptanceProbe: Send + Sync + 'static { } /// Evidence rules shared by every probe: stop at the first node that holds -/// the transaction, call it dead only when [`DEAD_QUORUM`] distinct nodes say -/// so, and give up after [`MAX_SUBMISSIONS_PER_PROBE`] submissions. +/// the transaction; call it dead only when [`DEAD_QUORUM`] distinct nodes +/// refuse it for its inputs **and** [`DEAD_QUORUM`] distinct nodes do not know +/// its txid; give up after [`MAX_SUBMISSIONS_PER_PROBE`] submissions and +/// [`MAX_LOOKUPS_PER_PROBE`] lookups. pub(crate) async fn probe_with( submitter: &dyn NodeSubmitter, transaction: &Transaction, @@ -172,9 +201,12 @@ pub(crate) async fn probe_with( } dead_reason.get_or_insert(reason); if dead_nodes.len() >= DEAD_QUORUM { - return ProbeVerdict::Dead { - reason: dead_reason.unwrap_or_default(), - }; + return confirm_dead( + submitter, + &transaction.txid(), + dead_reason.unwrap_or_default(), + ) + .await; } } NodeVerdict::Refused { reason } | NodeVerdict::Unknown { reason } => { @@ -194,32 +226,66 @@ pub(crate) async fn probe_with( } } -/// Submits through DAPI's `broadcastTransaction` — a Core `sendrawtransaction` -/// on a random evonode per request. +/// A rejection for missing or spent inputs is also what a *mined* transaction +/// whose outputs are all spent gets (see the module docs). Ask nodes whether +/// they know the txid: one that does settles it as accepted; only +/// [`DEAD_QUORUM`] distinct nodes not knowing it confirm the dead verdict. +async fn confirm_dead(submitter: &dyn NodeSubmitter, txid: &Txid, reason: String) -> ProbeVerdict { + let mut not_found: HashSet = HashSet::new(); + let mut last_unknown = String::from("no lookup was answered"); + for _ in 0..MAX_LOOKUPS_PER_PROBE { + let (node, answer) = submitter.lookup(txid).await; + tracing::debug!( + txid = %txid, + node = node.as_deref().unwrap_or("unidentified"), + ?answer, + "broadcast probe: txid lookup answered" + ); + match answer { + LookupAnswer::Known => return ProbeVerdict::Accepted, + LookupAnswer::NotFound => { + if let Some(node) = node { + not_found.insert(node); + } + if not_found.len() >= DEAD_QUORUM { + return ProbeVerdict::Dead { reason }; + } + } + LookupAnswer::Unknown { reason } => last_unknown = reason, + } + } + ProbeVerdict::Unresolved { + reason: format!( + "refused as {reason}, but only {} of {DEAD_QUORUM} nodes confirmed the txid is unknown; last lookup answer: {last_unknown}", + not_found.len() + ), + } +} + +/// One node per request: the probe decides whether to ask another, and it has +/// to know which node each answer came from. +fn single_node() -> RequestSettings { + RequestSettings { + retries: Some(0), + ..RequestSettings::default() + } +} + +/// Talks to evonodes through DAPI: `broadcastTransaction` (a Core +/// `sendrawtransaction`) and `getTransaction`, a random node per request. pub(crate) struct DapiNodeSubmitter { - sdk: std::sync::Arc, + sdk: Arc, } #[async_trait] impl NodeSubmitter for DapiNodeSubmitter { async fn submit(&self, transaction: &Transaction) -> (Option, NodeVerdict) { - use dash_sdk::dapi_client::transport::TransportError; - use dash_sdk::dapi_client::{DapiClientError, DapiRequestExecutor, RequestSettings}; - use dash_sdk::dapi_grpc::core::v0::BroadcastTransactionRequest; - let request = BroadcastTransactionRequest { transaction: dashcore::consensus::serialize(transaction), allow_high_fees: false, bypass_limits: false, }; - // One node per submission: the probe decides whether to ask another, - // and it has to know which node each answer came from. - let settings = RequestSettings { - retries: Some(0), - ..RequestSettings::default() - }; - - match self.sdk.execute(request, settings).await { + match self.sdk.execute(request, single_node()).await { Ok(response) => (Some(response.address.to_string()), NodeVerdict::Accepted), Err(error) => { let node = error.address.as_ref().map(ToString::to_string); @@ -235,6 +301,30 @@ impl NodeSubmitter for DapiNodeSubmitter { } } } + + async fn lookup(&self, txid: &Txid) -> (Option, LookupAnswer) { + // `id` is the display hex, as Core's getrawtransaction takes it. + let request = GetTransactionRequest { + id: txid.to_string(), + }; + match self.sdk.execute(request, single_node()).await { + Ok(response) => (Some(response.address.to_string()), LookupAnswer::Known), + Err(error) => { + let node = error.address.as_ref().map(ToString::to_string); + let answer = match &error.inner { + DapiClientError::Transport(TransportError::Grpc(status)) + if status.code() == Code::NotFound => + { + LookupAnswer::NotFound + } + other => LookupAnswer::Unknown { + reason: other.to_string(), + }, + }; + (node, answer) + } + } + } } /// The production probe: resubmits through DAPI. @@ -243,7 +333,7 @@ pub struct DapiAcceptanceProbe { } impl DapiAcceptanceProbe { - pub fn new(sdk: std::sync::Arc) -> Self { + pub fn new(sdk: Arc) -> Self { Self { submitter: DapiNodeSubmitter { sdk }, } @@ -276,23 +366,46 @@ mod tests { } } - /// Replays scripted node answers in order and counts submissions. + /// Replays scripted node answers in order and counts calls. Lookups + /// default to two distinct nodes not knowing the txid, so a dead quorum + /// is confirmed unless a test scripts otherwise. struct ScriptedNodes { answers: Mutex, NodeVerdict)>>, + lookups: Mutex, LookupAnswer)>>, submissions: Mutex, + lookups_made: Mutex, } impl ScriptedNodes { fn new(answers: Vec<(Option<&'static str>, NodeVerdict)>) -> Self { + Self::with_lookups( + answers, + vec![ + (Some("x"), LookupAnswer::NotFound), + (Some("y"), LookupAnswer::NotFound), + ], + ) + } + + fn with_lookups( + answers: Vec<(Option<&'static str>, NodeVerdict)>, + lookups: Vec<(Option<&'static str>, LookupAnswer)>, + ) -> Self { Self { answers: Mutex::new(answers.into()), + lookups: Mutex::new(lookups.into()), submissions: Mutex::new(0), + lookups_made: Mutex::new(0), } } fn submissions(&self) -> usize { *self.submissions.lock().expect("submissions") } + + fn lookups_made(&self) -> usize { + *self.lookups_made.lock().expect("lookups") + } } #[async_trait] @@ -312,6 +425,22 @@ mod tests { )); (node.map(str::to_string), verdict) } + + async fn lookup(&self, _txid: &Txid) -> (Option, LookupAnswer) { + *self.lookups_made.lock().expect("lookups") += 1; + let (node, answer) = self + .lookups + .lock() + .expect("lookups") + .pop_front() + .unwrap_or(( + None, + LookupAnswer::Unknown { + reason: "script exhausted".to_string(), + }, + )); + (node.map(str::to_string), answer) + } } fn dead() -> NodeVerdict { @@ -483,4 +612,54 @@ mod tests { )); assert_eq!(nodes.submissions(), 3); } + + /// Core answers `missingorspent` for a mined transaction whose outputs + /// are all spent (testnet `e758f06a…`, 2026-09-29). A node that still + /// knows the txid must turn the dead quorum into Accepted. + #[tokio::test] + async fn should_accept_a_refused_transaction_that_a_node_still_knows() { + let nodes = ScriptedNodes::with_lookups( + vec![(Some("a"), dead()), (Some("b"), dead())], + vec![(Some("c"), LookupAnswer::Known)], + ); + + assert_eq!( + probe_with(&nodes, &transaction()).await, + ProbeVerdict::Accepted + ); + assert_eq!(nodes.lookups_made(), 1); + } + + #[tokio::test] + async fn should_confirm_dead_only_when_two_distinct_nodes_do_not_know_the_txid() { + let nodes = ScriptedNodes::with_lookups( + vec![(Some("a"), dead()), (Some("b"), dead())], + vec![ + (Some("c"), LookupAnswer::NotFound), + (Some("c"), LookupAnswer::NotFound), + (None, LookupAnswer::NotFound), + ( + Some("d"), + LookupAnswer::Unknown { + reason: "Unavailable".to_string(), + }, + ), + ], + ); + + assert!(matches!( + probe_with(&nodes, &transaction()).await, + ProbeVerdict::Unresolved { .. } + )); + assert_eq!(nodes.lookups_made(), MAX_LOOKUPS_PER_PROBE); + } + + #[tokio::test] + async fn should_not_look_up_a_transaction_nobody_refused() { + let nodes = ScriptedNodes::new(vec![(Some("a"), NodeVerdict::Accepted)]); + + probe_with(&nodes, &transaction()).await; + + assert_eq!(nodes.lookups_made(), 0); + } } diff --git a/packages/rs-platform-wallet/src/broadcaster.rs b/packages/rs-platform-wallet/src/broadcaster.rs index 18a942955e8..3a5bfe16370 100644 --- a/packages/rs-platform-wallet/src/broadcaster.rs +++ b/packages/rs-platform-wallet/src/broadcaster.rs @@ -9,17 +9,22 @@ //! announcing the txid back, an InstantSend lock, or a confirmation proves //! the network accepted it. Trustless; no DAPI involvement. //! - [`DapiBroadcaster`] (fallback for wallets without an SPV runtime): -//! submission via DAPI's gRPC endpoint. Core's own verdict decides the -//! outcome where the node gave one (see [`crate::broadcast_probe`]); every -//! other failure is conservatively [`BroadcastError::MaybeSent`]. +//! submission via DAPI's gRPC endpoint. A node that already has the +//! transaction on chain counts as success; every other failure is +//! conservatively [`BroadcastError::MaybeSent`] (see [`crate::broadcast_probe`] +//! for why one node's rejection proves nothing). use std::sync::Arc; use std::time::Duration; use async_trait::async_trait; +use dash_sdk::dapi_client::transport::TransportError; +use dash_sdk::dapi_client::{DapiClientError, DapiRequestExecutor, IntoInner, RequestSettings}; +use dash_sdk::dapi_grpc::core::v0::BroadcastTransactionRequest; use dash_spv::BroadcastResult; -use dashcore::{Transaction, Txid}; +use dashcore::{consensus, Transaction, Txid}; +use crate::broadcast_probe::{classify_failed_submission, NodeVerdict}; use crate::error::PlatformWalletError; use crate::spv::SpvRuntime; @@ -118,15 +123,6 @@ impl DapiBroadcaster { #[async_trait] impl TransactionBroadcaster for DapiBroadcaster { async fn broadcast(&self, transaction: &Transaction) -> Result { - use dash_sdk::dapi_client::transport::TransportError; - use dash_sdk::dapi_client::{ - DapiClientError, DapiRequestExecutor, IntoInner, RequestSettings, - }; - use dash_sdk::dapi_grpc::core::v0::BroadcastTransactionRequest; - use dashcore::consensus; - - use crate::broadcast_probe::{classify_failed_submission, NodeVerdict}; - let tx_bytes = consensus::serialize(transaction); let request = BroadcastTransactionRequest { @@ -135,44 +131,29 @@ impl TransactionBroadcaster for DapiBroadcaster { bypass_limits: false, }; - // `sdk.execute` retries across nodes internally, but only on transport - // failures: `AlreadyExists`, `InvalidArgument` and `FailedPrecondition` - // are non-retryable, so when one of them surfaces it is the answer of - // the node that evaluated the transaction. That makes Core's own - // verdict usable (see `broadcast_probe`): `AlreadyExists` means the - // transaction is on chain, and a proven-dead or refused transaction - // did not enter this node's mempool — its reserved inputs are safe to - // release, since a rebuild re-selecting them conflicts with, rather - // than adds to, any copy an earlier attempt may have delivered. - // Everything else — transport errors, timeouts, unrecognised - // rejections — stays `MaybeSent`, because an earlier attempt may have - // delivered the transaction before the response was lost. - let error = match self + // A node that answers `AlreadyExists` has the transaction on chain, + // so that is success. Every other failure stays `MaybeSent`, including + // Core's own rejections: one node's `missingorspent` is also what a + // lagging node, a node that has not seen our unconfirmed parent, and + // a mined transaction whose outputs are all spent produce (see + // `broadcast_probe`), and the `Rejected` contract demands proof that + // the transaction never entered the network — which one answer cannot + // give. `sdk.execute` also retries transport failures across nodes, so + // an earlier attempt may have delivered it before this error. + match self .sdk .execute(request, RequestSettings::default()) .await .into_inner() { - Ok(_response) => return Ok(transaction.txid()), - Err(error) => error, - }; - - let verdict = match &error { - DapiClientError::Transport(TransportError::Grpc(status)) => { - classify_failed_submission(status.code(), status.message()) - } - _ => NodeVerdict::Unknown { - reason: error.to_string(), - }, - }; - match verdict { - NodeVerdict::Accepted => Ok(transaction.txid()), - NodeVerdict::Dead { reason } | NodeVerdict::Refused { reason } => { - Err(BroadcastError::Rejected { - reason: format!("DAPI broadcast rejected: {reason}"), - }) + Ok(_response) => Ok(transaction.txid()), + Err(DapiClientError::Transport(TransportError::Grpc(status))) + if classify_failed_submission(status.code(), status.message()) + == NodeVerdict::Accepted => + { + Ok(transaction.txid()) } - NodeVerdict::Unknown { .. } => Err(BroadcastError::MaybeSent { + Err(error) => Err(BroadcastError::MaybeSent { reason: format!("DAPI broadcast failed: {error}"), }), } diff --git a/packages/rs-platform-wallet/src/events.rs b/packages/rs-platform-wallet/src/events.rs index 8d02b691917..0ade95f93f6 100644 --- a/packages/rs-platform-wallet/src/events.rs +++ b/packages/rs-platform-wallet/src/events.rs @@ -12,10 +12,13 @@ use std::sync::Arc; use arc_swap::ArcSwap; +use dashcore::Txid; +use key_wallet_manager::WalletId; pub use dash_spv::EventHandler; pub use key_wallet_manager::WalletEvent; +use crate::broadcast_probe::ProbeVerdict; use crate::manager::dpns_sync::DpnsSyncPassSummary; use crate::manager::platform_address_sync::PlatformAddressSyncSummary; #[cfg(feature = "shielded")] @@ -55,12 +58,19 @@ pub trait PlatformEventHandler: EventHandler { /// Default impl is a no-op so existing handlers don't have to care. fn on_outgoing_transaction_probed( &self, - _wallet_id: &key_wallet_manager::WalletId, - _txid: &dashcore::Txid, - _verdict: &crate::broadcast_probe::ProbeVerdict, + _wallet_id: &WalletId, + _txid: &Txid, + _verdict: &ProbeVerdict, ) { } + /// Fired when a send that had a published verdict is no longer an + /// unconfirmed root — it settled (block or InstantSend lock) or left the + /// wallet. Hosts drop any state they kept for the verdict. + /// + /// Default impl is a no-op so existing handlers don't have to care. + fn on_outgoing_transaction_cleared(&self, _wallet_id: &WalletId, _txid: &Txid) {} + /// Fired after each [`ShieldedSyncManager`] pass completes, /// including passes that produced no updates or skipped every /// wallet because none had a bound shielded sub-wallet yet. @@ -173,9 +183,9 @@ impl PlatformEventManager { /// per unconfirmed send per block. pub fn on_outgoing_transaction_probed( &self, - wallet_id: &key_wallet_manager::WalletId, - txid: &dashcore::Txid, - verdict: &crate::broadcast_probe::ProbeVerdict, + wallet_id: &WalletId, + txid: &Txid, + verdict: &ProbeVerdict, ) { let handlers = self.handlers.load(); for h in handlers.iter() { @@ -183,6 +193,14 @@ impl PlatformEventManager { } } + /// Dispatch a cleared broadcast-probe verdict to every handler. + pub fn on_outgoing_transaction_cleared(&self, wallet_id: &WalletId, txid: &Txid) { + let handlers = self.handlers.load(); + for h in handlers.iter() { + h.on_outgoing_transaction_cleared(wallet_id, txid); + } + } + /// Dispatch a shielded sync completion to every handler. /// /// Not on the SPV hot path — called once per shielded sync pass diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index 25deb35ec5c..7879accdbff 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -522,7 +522,6 @@ impl PlatformWalletManager

{ self.persister.persistence_capabilities() } - /// The SDK instance. /// Turn automatic probing of unconfirmed sends on or off. /// /// When on, every unconfirmed send of a registered wallet whose broadcast @@ -539,6 +538,7 @@ impl PlatformWalletManager

{ self.broadcast_resolver.is_enabled() } + /// The SDK instance. pub fn sdk(&self) -> &dash_sdk::Sdk { &self.sdk } diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index c70914b366a..4f12f6d1941 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -24,6 +24,7 @@ use tokio_util::sync::CancellationToken; use key_wallet_manager::WalletManager; +use crate::broadcast_probe::DapiAcceptanceProbe; use crate::changeset::{spawn_wallet_event_adapter, PlatformWalletPersistence}; use crate::events::{PlatformEventHandler, PlatformEventManager}; use crate::manager::dashpay_sync::DashPaySyncManager; @@ -34,6 +35,7 @@ use crate::manager::platform_address_sync::PlatformAddressSyncManager; use crate::manager::shielded_sync::ShieldedSyncManager; use crate::spv::SpvRuntime; use crate::wallet::asset_lock::LockNotifyHandler; +use crate::wallet::core::broadcast_resolver::BroadcastResolver; use crate::wallet::core::{BalanceUpdateHandler, SpendObservationHandler}; use crate::wallet::identity::network::DashPayPaymentHandler; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; @@ -72,6 +74,11 @@ pub enum WalletWorker { /// tasks; its drain outcome is folded into the report because those /// tasks clone the FFI persister and can fire host callbacks. DashPayPayments, + /// Broadcast-probe passes spawned by `BroadcastResolver` in response to + /// SPV events. Not a registry worker: the resolver's own tracker closes + /// admission and aborts the passes; its drain outcome is folded into the + /// report because a pass publishes through the host's event callbacks. + BroadcastProbes, /// The wallet-event adapter task — the sink coordinator stores feed /// into. Not a registry worker: joined by /// [`shutdown`](PlatformWalletManager::shutdown) under a bounded @@ -383,7 +390,7 @@ pub struct PlatformWalletManager { /// Asks the network about this manager's unconfirmed sends whose /// broadcast outcome was unknown and publishes the verdicts. Off until /// the host calls [`set_broadcast_probe_enabled`](Self::set_broadcast_probe_enabled). - pub(super) broadcast_resolver: Arc, + pub(super) broadcast_resolver: Arc, /// Periodic shielded (Orchard) note sync coordinator (spends are /// detected during the note scan, no separate nullifier pass). /// Iterates every wallet that has been bound via @@ -547,14 +554,10 @@ impl PlatformWalletManager

{ // was unknown (read-only; see its module docs). It publishes through // the event manager it is registered with, so it gets a weak handle // to it once that exists. - let broadcast_resolver = Arc::new( - crate::wallet::core::broadcast_resolver::BroadcastResolver::new( - Arc::new(crate::broadcast_probe::DapiAcceptanceProbe::new( - Arc::clone(&sdk), - )), - Arc::clone(&wallet_manager), - ), - ); + let broadcast_resolver = Arc::new(BroadcastResolver::new( + Arc::new(DapiAcceptanceProbe::new(Arc::clone(&sdk))), + Arc::clone(&wallet_manager), + )); let event_manager = Arc::new(PlatformEventManager::new(vec![ app_handler, lock_handler, @@ -938,7 +941,7 @@ impl PlatformWalletManager

{ /// /// Returns a [`ShutdownReport`] keyed by [`WalletWorker`] — including /// the non-registry workers [`WalletWorker::Spv`], - /// [`WalletWorker::DashPayPayments`], and + /// [`WalletWorker::DashPayPayments`], [`WalletWorker::BroadcastProbes`], and /// [`WalletWorker::EventAdapter`], so no callback-capable background /// work is excluded from the verdict. Inspect /// [`ShutdownReport::all_clean`] before freeing the host callback @@ -961,17 +964,15 @@ impl PlatformWalletManager

{ // Close payment-hook admission and join the admitted tasks — // they clone the FFI persister, so a straggler is exactly the // callback-after-destroy hazard the report exists to catch. - let payments_drained = self - .dashpay_payment_handler - .quiesce_within(PAYMENT_DRAIN_BUDGET) - .await; - // Probe passes publish through the host's event callbacks, so they - // must be gone before destroy returns; they are safe to abort. - let probes_drained = self - .broadcast_resolver - .quiesce_within(PAYMENT_DRAIN_BUDGET) - .await; - let payments_drained = payments_drained && probes_drained; + // + // Broadcast-probe passes publish through the host's event callbacks + // too, so they must be gone before destroy returns; they are safe to + // abort. Both drains run concurrently under one shared budget. + let (payments_drained, probes_drained) = tokio::join!( + self.dashpay_payment_handler + .quiesce_within(PAYMENT_DRAIN_BUDGET), + self.broadcast_resolver.quiesce_within(PAYMENT_DRAIN_BUDGET), + ); // Drain the coordinators concurrently against one shared budget so // the drain phase as a whole is bounded (a wedged pass surfaces as @@ -1048,6 +1049,14 @@ impl PlatformWalletManager

{ WorkerStatus::Timeout }, ); + report.per_worker.insert( + WalletWorker::BroadcastProbes, + if probes_drained { + WorkerStatus::Ok + } else { + WorkerStatus::Timeout + }, + ); // The wallet-event adapter is the sink the coordinators' stores // feed into, so it drains AFTER them. It is a plain tokio task, @@ -1292,6 +1301,7 @@ mod tests { for worker in [ WalletWorker::Spv, WalletWorker::DashPayPayments, + WalletWorker::BroadcastProbes, WalletWorker::EventAdapter, ] { assert!( diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 80c16329086..2f716d5ca29 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -17,22 +17,35 @@ //! A healthy send is IS-locked within seconds, so in practice only sends that //! went quiet are ever probed. //! -//! When: once as soon as dash-spv reports an `Uncertain` broadcast, then once -//! per new block while unresolved; after [`EVERY_BLOCK_WINDOW`] blocks without -//! a verdict, every [`SLOW_INTERVAL`] blocks. A root stops being probed once a -//! probe says `Accepted` or `Dead`, or once it settles or leaves the wallet. -//! Probes run only while the SPV client delivers blocks, i.e. while the app is -//! in the foreground. +//! A send that spends an unconfirmed coin of *someone else's* — an incoming +//! payment not yet in a block or IS-locked — is not a root either, for the same +//! reason: nodes that have not seen that parent refuse the send. +//! +//! When: once as soon as dash-spv reports an `Uncertain` broadcast, then on +//! every advance of the wallet's synced height (a filter batch: once per block +//! at the tip, larger steps while catching up) while the root stays +//! unsettled; after [`EVERY_BLOCK_WINDOW`] blocks, every [`SLOW_INTERVAL`] +//! blocks. At most one height-driven pass per wallet runs at a time. Only +//! `Dead` ends the probing — acceptance by one node's mempool is not +//! settlement, so an `Accepted` root keeps being asked until it settles. A +//! verdict is published only when it differs from the last one published for +//! that root, and a root that settles or leaves the wallet after a verdict was +//! published is published as cleared. Probes run only while the SPV client +//! delivers blocks, i.e. while the app is in the foreground. use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::future::Future; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, Weak}; +use std::time::Duration; -use dash_spv::EventHandler; +use dash_spv::sync::SyncEvent; +use dash_spv::{BroadcastResult, EventHandler}; use dashcore::{OutPoint, Transaction, Txid}; use key_wallet::transaction_checking::TransactionContext; use key_wallet_manager::{WalletEvent, WalletId, WalletManager}; use tokio::sync::RwLock; +use tokio::task::JoinHandle; use crate::broadcast_probe::{AcceptanceProbe, ProbeVerdict}; use crate::events::{PlatformEventHandler, PlatformEventManager}; @@ -57,7 +70,9 @@ pub(crate) struct OutgoingView { pub transaction: Transaction, } -/// The roots of this wallet's unsettled chains, one entry per txid. +/// The roots of this wallet's unsettled chains, one entry per txid: unsettled +/// transactions it signed that spend no output of any unsettled transaction — +/// its own or an incoming one. pub(crate) fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { // A transaction recorded in several accounts is one transaction; if any // account holds it as settled, it is. @@ -68,19 +83,20 @@ pub(crate) fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { .and_modify(|(_, settled)| *settled |= view.settled) .or_insert((view, view.settled)); } - let unsettled_own: BTreeSet = by_txid + let unsettled: BTreeSet = by_txid .iter() - .filter(|(_, (view, settled))| !*settled && view.spends_own_coins) + .filter(|(_, (_, settled))| !*settled) .map(|(txid, _)| *txid) .collect(); - unsettled_own + unsettled .iter() .map(|txid| by_txid[txid].0) + .filter(|view| view.spends_own_coins) .filter(|view| { !view .inputs .iter() - .any(|input| unsettled_own.contains(&input.txid)) + .any(|input| unsettled.contains(&input.txid)) }) .collect() } @@ -118,12 +134,24 @@ impl ProbeSchedule { #[derive(Debug, Default)] pub(crate) struct ResolverState { schedules: HashMap<(WalletId, Txid), ProbeSchedule>, - resolved: HashSet<(WalletId, Txid)>, + /// Roots proven dead: never probed again while they stay roots. + dead: HashSet<(WalletId, Txid)>, + /// The last verdict published per root, so only changes are published. + published: HashMap<(WalletId, Txid), ProbeVerdict>, +} + +/// What a pass has to tell the host. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum ResolverEvent { + /// A root's verdict changed. + Verdict(Txid, ProbeVerdict), + /// A root that had a published verdict settled or left the wallet. + Cleared(Txid), } -/// One pass over one wallet at `height`: probe every due root and return the -/// verdicts to publish. `force` makes one root due regardless of its schedule -/// (the SPV broadcaster has just reported it `Uncertain`). +/// One pass over one wallet at `height`: probe every due root and return what +/// to publish. `force` makes one root due regardless of its schedule (the SPV +/// broadcaster has just reported it `Uncertain`). pub(crate) async fn resolve_pass( state: &Mutex, probe: &dyn AcceptanceProbe, @@ -131,25 +159,38 @@ pub(crate) async fn resolve_pass( height: u32, views: &[OutgoingView], force: Option, -) -> Vec<(Txid, ProbeVerdict)> { +) -> Vec { let roots = ambiguous_roots(views); let root_ids: HashSet = roots.iter().map(|view| view.txid).collect(); // Decide what is due under the lock; probe without it. + let mut events = Vec::new(); let due: Vec<(Txid, Transaction)> = { let mut guard = state.lock().expect("resolver state poisoned"); let ResolverState { schedules, - resolved, + dead, + published, } = &mut *guard; - // Forget this wallet's roots that settled or left the wallet. - schedules.retain(|(wallet, txid), _| *wallet != wallet_id || root_ids.contains(txid)); - resolved.retain(|(wallet, txid)| *wallet != wallet_id || root_ids.contains(txid)); + // Forget this wallet's roots that settled or left the wallet; tell the + // host about the ones it had a verdict for. + let is_gone = + |wallet: &WalletId, txid: &Txid| *wallet == wallet_id && !root_ids.contains(txid); + schedules.retain(|(wallet, txid), _| !is_gone(wallet, txid)); + dead.retain(|(wallet, txid)| !is_gone(wallet, txid)); + let mut cleared: Vec = published + .keys() + .filter(|(wallet, txid)| is_gone(wallet, txid)) + .map(|(_, txid)| *txid) + .collect(); + cleared.sort(); + published.retain(|(wallet, txid), _| !is_gone(wallet, txid)); + events.extend(cleared.into_iter().map(ResolverEvent::Cleared)); let mut due = Vec::new(); for view in &roots { let key = (wallet_id, view.txid); - if resolved.contains(&key) { + if dead.contains(&key) { continue; } let schedule = schedules @@ -163,24 +204,34 @@ pub(crate) async fn resolve_pass( due }; - let mut verdicts = Vec::with_capacity(due.len()); for (txid, transaction) in due { let verdict = probe.probe(&transaction).await; - if !matches!(verdict, ProbeVerdict::Unresolved { .. }) { - state - .lock() - .expect("resolver state poisoned") - .resolved - .insert((wallet_id, txid)); + let key = (wallet_id, txid); + let mut guard = state.lock().expect("resolver state poisoned"); + if matches!(verdict, ProbeVerdict::Dead { .. }) { + guard.dead.insert(key); + } + // Publish changes only. Unresolved reasons vary between probes + // (different nodes, different errors), so all Unresolved count as one. + let changed = match guard.published.get(&key) { + None => true, + Some(ProbeVerdict::Unresolved { .. }) => { + !matches!(verdict, ProbeVerdict::Unresolved { .. }) + } + Some(previous) => *previous != verdict, + }; + if changed { + guard.published.insert(key, verdict.clone()); + events.push(ResolverEvent::Verdict(txid, verdict)); } - verdicts.push((txid, verdict)); } - verdicts + events } -/// This wallet's unsettled transactions that spend its own coins, one view per -/// account record. Only those can be ambiguity roots, so settled and incoming -/// records are skipped here rather than cloned. +/// This wallet's unsettled transactions, one view per account record: the ones +/// it signed (the candidate roots) and the incoming ones (whose outputs a send +/// may spend, which disqualifies that send as a root). Settled records are +/// skipped rather than cloned. pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { let accounts = info.core_wallet.accounts.all_accounts(); // A transaction settled in any account is settled. @@ -198,7 +249,7 @@ pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { let mut views = Vec::new(); for account in accounts.iter() { for record in account.transactions().values() { - if settled_anywhere.contains(&record.txid) || record.input_details.is_empty() { + if settled_anywhere.contains(&record.txid) { continue; } views.push(OutgoingView { @@ -210,7 +261,7 @@ pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { .map(|input| input.previous_output) .collect(), settled: false, - spends_own_coins: true, + spends_own_coins: !record.input_details.is_empty(), transaction: record.transaction.clone(), }); } @@ -222,7 +273,7 @@ pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { /// resubmits bytes that were already sent, so a task may be aborted at any /// point without losing anything. struct ProbeTasks { - state: Mutex<(bool, Vec>)>, + state: Mutex<(bool, Vec>)>, } impl ProbeTasks { @@ -234,7 +285,7 @@ impl ProbeTasks { fn spawn(&self, future: F) where - F: std::future::Future + Send + 'static, + F: Future + Send + 'static, { let mut state = self.state.lock().expect("probe task mutex poisoned"); if !state.0 { @@ -246,7 +297,7 @@ impl ProbeTasks { /// Close admission and stop every task, waiting at most `budget` for each /// aborted task to unwind. Returns `false` if one outlived it. - async fn quiesce_within(&self, budget: std::time::Duration) -> bool { + async fn quiesce_within(&self, budget: Duration) -> bool { let handles = { let mut state = self.state.lock().expect("probe task mutex poisoned"); state.0 = false; @@ -283,6 +334,9 @@ pub(crate) struct BroadcastResolver { /// Last synced height per wallet, so an SPV-level `Uncertain` (which /// carries no wallet id) can run a pass on every wallet at once. heights: Mutex>, + /// Wallets with a height-driven pass running: a burst of height advances + /// (catch-up after time offline) must not stack passes for one wallet. + in_flight: Arc>>, events: Mutex>, tasks: ProbeTasks, } @@ -298,6 +352,7 @@ impl BroadcastResolver { wallet_manager, state: Arc::new(Mutex::new(ResolverState::default())), heights: Mutex::new(HashMap::new()), + in_flight: Arc::new(Mutex::new(HashSet::new())), events: Mutex::new(Weak::new()), tasks: ProbeTasks::new(), } @@ -318,16 +373,37 @@ impl BroadcastResolver { } /// Stop admitting passes and stop the running ones. Idempotent. - pub(crate) async fn quiesce_within(&self, budget: std::time::Duration) -> bool { + pub(crate) async fn quiesce_within(&self, budget: Duration) -> bool { self.tasks.quiesce_within(budget).await } fn spawn_pass(&self, wallet_id: WalletId, height: u32, force: Option) { + // A height-driven pass is skipped while one for this wallet runs; a + // forced pass (dash-spv just reported `Uncertain`) always runs — the + // schedule marks a root probed before probing, so the two cannot probe + // the same root twice unless forced. + let in_flight = if force.is_none() { + if !self + .in_flight + .lock() + .expect("in-flight mutex poisoned") + .insert(wallet_id) + { + return; + } + Some(InFlight { + set: Arc::clone(&self.in_flight), + wallet_id, + }) + } else { + None + }; let wallet_manager = Arc::clone(&self.wallet_manager); let probe = Arc::clone(&self.probe); let state = Arc::clone(&self.state); let events = self.events.lock().expect("events mutex poisoned").clone(); self.tasks.spawn(async move { + let _in_flight = in_flight; // Read under the lock, probe without it: a probe is network I/O. let views = { let manager = wallet_manager.read().await; @@ -336,36 +412,65 @@ impl BroadcastResolver { None => return, } }; - let verdicts = + let outcome = resolve_pass(&state, probe.as_ref(), wallet_id, height, &views, force).await; - if verdicts.is_empty() { + if outcome.is_empty() { return; } let events = events.upgrade(); // What started this pass: dash-spv's Uncertain result for one - // transaction, or a new block for every due root. + // transaction, or an advance of the wallet's synced height. let trigger = if force.is_some() { "uncertain" } else { - "block" + "height" }; - for (txid, verdict) in &verdicts { - tracing::info!( - wallet_id = %hex::encode(wallet_id), - txid = %txid, - height, - trigger, - ?verdict, - "broadcast probe: verdict for an unconfirmed send" - ); - if let Some(events) = &events { - events.on_outgoing_transaction_probed(&wallet_id, txid, verdict); + for event in &outcome { + match event { + ResolverEvent::Verdict(txid, verdict) => { + tracing::info!( + wallet_id = %hex::encode(wallet_id), + txid = %txid, + height, + trigger, + ?verdict, + "broadcast probe: verdict for an unconfirmed send" + ); + if let Some(events) = &events { + events.on_outgoing_transaction_probed(&wallet_id, txid, verdict); + } + } + ResolverEvent::Cleared(txid) => { + tracing::info!( + wallet_id = %hex::encode(wallet_id), + txid = %txid, + height, + "broadcast probe: send no longer unconfirmed, verdict cleared" + ); + if let Some(events) = &events { + events.on_outgoing_transaction_cleared(&wallet_id, txid); + } + } } } }); } } +/// Removes a wallet from the in-flight set when its pass ends, however it ends. +struct InFlight { + set: Arc>>, + wallet_id: WalletId, +} + +impl Drop for InFlight { + fn drop(&mut self) { + if let Ok(mut set) = self.set.lock() { + set.remove(&self.wallet_id); + } + } +} + impl EventHandler for BroadcastResolver { fn on_wallet_event(&self, event: &WalletEvent) { let WalletEvent::SyncHeightAdvanced { wallet_id, height } = event else { @@ -380,10 +485,10 @@ impl EventHandler for BroadcastResolver { } } - fn on_sync_event(&self, event: &dash_spv::sync::SyncEvent) { - let dash_spv::sync::SyncEvent::TransactionBroadcastResult { + fn on_sync_event(&self, event: &SyncEvent) { + let SyncEvent::TransactionBroadcastResult { txid, - result: dash_spv::BroadcastResult::Uncertain, + result: BroadcastResult::Uncertain, } = event else { return; @@ -588,37 +693,63 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), dead())]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; - let verdicts = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + let events = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; assert_eq!(probe.probed(), vec![txid(1)]); - assert_eq!(verdicts, vec![(txid(1), dead())]); + assert_eq!(events, vec![ResolverEvent::Verdict(txid(1), dead())]); } #[tokio::test] - async fn should_stop_probing_a_root_once_it_is_resolved() { + async fn should_stop_probing_a_root_proven_dead() { let state = Mutex::new(ResolverState::default()); - let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); + let probe = ScriptedProbe::new(&[(txid(1), dead())]); let views = [send(1, &[outpoint(90, 0)])]; resolve_pass(&state, &probe, wallet(), 100, &views, None).await; let again = resolve_pass(&state, &probe, wallet(), 101, &views, None).await; - assert!(again.is_empty(), "an accepted root is not probed again"); + assert!(again.is_empty()); assert_eq!(probe.probed(), vec![txid(1)]); } + /// Acceptance by one node's mempool is not settlement: the root keeps + /// being asked until it settles, but the host hears about it only once. + #[tokio::test] + async fn should_keep_probing_an_accepted_root_but_publish_it_once() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); + let views = [send(1, &[outpoint(90, 0)])]; + + let first = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + let second = resolve_pass(&state, &probe, wallet(), 101, &views, None).await; + + assert_eq!( + first, + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] + ); + assert!( + second.is_empty(), + "an unchanged verdict is not published again" + ); + assert_eq!(probe.probed(), vec![txid(1), txid(1)]); + } + #[tokio::test] async fn should_keep_probing_an_unresolved_root_on_each_new_block() { let state = Mutex::new(ResolverState::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)])]; - resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + let first = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; let same_block = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; let next_block = resolve_pass(&state, &probe, wallet(), 101, &views, None).await; + assert_eq!(first, vec![ResolverEvent::Verdict(txid(1), unresolved())]); assert!(same_block.is_empty(), "one probe per block"); - assert_eq!(next_block, vec![(txid(1), unresolved())]); + assert!( + next_block.is_empty(), + "still unresolved: nothing new to publish" + ); assert_eq!(probe.probed(), vec![txid(1), txid(1)]); } @@ -631,25 +762,40 @@ mod tests { let views = [send(1, &[outpoint(90, 0)])]; resolve_pass(&state, &probe, wallet(), 100, &views, None).await; - let forced = resolve_pass(&state, &probe, wallet(), 100, &views, Some(txid(1))).await; + resolve_pass(&state, &probe, wallet(), 100, &views, Some(txid(1))).await; - assert_eq!(forced.len(), 1); + assert_eq!(probe.probed(), vec![txid(1), txid(1)]); } - /// A root that settled or left the wallet between passes is forgotten, - /// so a later reappearance starts a fresh schedule. + /// A root that settled or left the wallet is forgotten, and the host is + /// told to drop the verdict it was shown. #[tokio::test] - async fn should_forget_a_root_that_is_no_longer_ambiguous() { + async fn should_clear_a_published_root_that_is_no_longer_ambiguous() { let state = Mutex::new(ResolverState::default()); let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); let views = [send(1, &[outpoint(90, 0)])]; resolve_pass(&state, &probe, wallet(), 100, &views, None).await; - resolve_pass(&state, &probe, wallet(), 101, &[], None).await; + let gone = resolve_pass(&state, &probe, wallet(), 101, &[], None).await; + assert_eq!(gone, vec![ResolverEvent::Cleared(txid(1))]); let guard = state.lock().expect("state"); assert!(guard.schedules.is_empty()); - assert!(guard.resolved.is_empty()); + assert!(guard.dead.is_empty()); + assert!(guard.published.is_empty()); + } + + /// Nodes that have not seen an unconfirmed incoming payment refuse a send + /// that spends it; such a send is not a root until its parent settles. + #[test] + fn should_not_pick_a_send_that_spends_an_unconfirmed_incoming_coin() { + let mut incoming = send(5, &[outpoint(80, 0)]); + incoming.spends_own_coins = false; + let send_on_it = send(6, &[outpoint(5, 0)]); + assert!(roots(&[incoming.clone(), send_on_it.clone()]).is_empty()); + + incoming.settled = true; + assert_eq!(roots(&[incoming, send_on_it]), BTreeSet::from([txid(6)])); } /// Two wallets' roots are tracked independently. diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift index 5976d9bbeb9..c84a22b30bb 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift @@ -455,10 +455,11 @@ public class PlatformWalletManager: ObservableObject { /// Rust. Every native pointer has been copied before publication. @Published public internal(set) var lastDpnsSyncEvent: DpnsSyncEvent? - /// The latest broadcast-probe verdict per unconfirmed send, keyed by the - /// txid in wire (internal) byte order. Filled only while - /// `setBroadcastProbeEnabled(true)` is on. See `OutgoingTransactionProbeEvent`. - @Published public internal(set) var outgoingTransactionVerdicts: [Data: OutgoingTransactionProbeEvent] = [:] + /// The latest broadcast-probe verdict per unconfirmed send (wallet + txid). + /// Filled only while `setBroadcastProbeEnabled(true)` is on; an entry is + /// removed once its send settles or leaves the wallet. See + /// `OutgoingTransactionProbeEvent`. + @Published public internal(set) var outgoingTransactionVerdicts: [OutgoingTransactionKey: OutgoingTransactionProbeEvent] = [:] /// The most recent broadcast-probe verdict, for observers that react to /// each one as it arrives. diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index 1474aa6c85e..7c77e71af55 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -13,6 +13,18 @@ public enum OutgoingTransactionVerdict: Sendable, Equatable { case unresolved(reason: String) } +/// Identifies one unconfirmed send across the wallets of a manager. +public struct OutgoingTransactionKey: Hashable, Sendable { + public let walletId: Data + /// 32 bytes, wire (internal) byte order. + public let txidWire: Data + + public init(walletId: Data, txidWire: Data) { + self.walletId = walletId + self.txidWire = txidWire + } +} + /// One broadcast-probe verdict. Nothing in the wallet changed because of it. public struct OutgoingTransactionProbeEvent: Sendable, Equatable { public let walletId: Data @@ -25,6 +37,10 @@ public struct OutgoingTransactionProbeEvent: Sendable, Equatable { txidWire.reversed().map { String(format: "%02x", $0) }.joined() } + public var key: OutgoingTransactionKey { + OutgoingTransactionKey(walletId: walletId, txidWire: txidWire) + } + public init(walletId: Data, txidWire: Data, verdict: OutgoingTransactionVerdict) { self.walletId = walletId self.txidWire = txidWire @@ -48,6 +64,15 @@ func outgoingTransactionProbedCallback( .fromOpaque(context) .takeUnretainedValue() + let walletId = Data(bytes: walletIdPtr, count: 32) + let txidWire = Data(bytes: txidPtr, count: 32) + if verdictCode == UInt8(OUTGOING_PROBE_VERDICT_CLEARED) { + let key = OutgoingTransactionKey(walletId: walletId, txidWire: txidWire) + Task { @MainActor [weak manager = handler.manager] in + manager?.handleOutgoingTransactionCleared(key) + } + return + } let reason = reasonPtr.map { String(cString: $0) } ?? "" let verdict: OutgoingTransactionVerdict switch verdictCode { @@ -59,8 +84,8 @@ func outgoingTransactionProbedCallback( verdict = .unresolved(reason: reason) } let event = OutgoingTransactionProbeEvent( - walletId: Data(bytes: walletIdPtr, count: 32), - txidWire: Data(bytes: txidPtr, count: 32), + walletId: walletId, + txidWire: txidWire, verdict: verdict ) @@ -74,8 +99,9 @@ extension PlatformWalletManager { /// /// While on, the SDK resubmits every unconfirmed send whose broadcast /// outcome went quiet to evonodes over DAPI — right after SPV reports it - /// uncertain, then once per block — and publishes each verdict in - /// `outgoingTransactionVerdicts` / `lastOutgoingTransactionProbe`. + /// uncertain, then as blocks arrive — and publishes each *change* of + /// verdict in `outgoingTransactionVerdicts` / `lastOutgoingTransactionProbe`. + /// A send that settles or leaves the wallet is removed from the map. /// Resubmitting sends the same signed bytes: it can deliver a payment, it /// can never create a second one. Nothing in the wallet changes. public func setBroadcastProbeEnabled(_ enabled: Bool) throws { @@ -88,7 +114,14 @@ extension PlatformWalletManager { @MainActor func handleOutgoingTransactionProbed(_ event: OutgoingTransactionProbeEvent) { guard !shutdownRequested, isConfigured else { return } - outgoingTransactionVerdicts[event.txidWire] = event + outgoingTransactionVerdicts[event.key] = event lastOutgoingTransactionProbe = event } + + /// The send settled or left the wallet: its verdict no longer applies. + @MainActor + func handleOutgoingTransactionCleared(_ key: OutgoingTransactionKey) { + guard !shutdownRequested, isConfigured else { return } + outgoingTransactionVerdicts.removeValue(forKey: key) + } } From 4506258a92c4d789ccc8bd9c8ef83570025c212c Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 16:36:19 +0300 Subject: [PATCH 007/169] fix(platform-wallet): second review pass on the broadcast resolver - Merge `spends_own_coins` across a transaction's account records (OR, like `settled`): a transfer between own accounts is "incoming" in the receiving account, which key-wallet lists first, and was never probed. - Probe the unconfirmed ancestors a send depends on, including someone else's incoming payment: the chain's root is probed instead of nothing. - A node that has the transaction in a block (-27, or getTransaction with a height) yields the new final verdict `Mined`; probing stops. Hosts see it as accepted. - A verdict is dropped if a concurrent pass cleared its root while the probe was out; Swift applies verdicts and clears on the FIFO main queue. - A removed wallet's roots, and all roots when probing is turned off, are forgotten and published as cleared. - One worker per wallet drains a queue that merges forced (Uncertain) and height requests, instead of a task per event; height steps larger than 3 blocks (catch-up after time offline) do not trigger a pass. - `collect_views` walks the records once and clones only unsettled ones. Tests: resolver 23 (+7), probe 13 (+2); platform-wallet 1210/1210, FFI 388/388, Swift 720/721 (the known-flaky testCaptureHistoricalV2Fixture). Ticket 32347. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/event_handler.rs | 4 +- .../rs-platform-wallet/src/broadcast_probe.rs | 55 +- .../rs-platform-wallet/src/broadcaster.rs | 6 +- .../src/wallet/core/broadcast_resolver.rs | 598 +++++++++++++----- .../PlatformWalletManagerBroadcastProbe.swift | 16 +- 5 files changed, 499 insertions(+), 180 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/event_handler.rs b/packages/rs-platform-wallet-ffi/src/event_handler.rs index bbd700f1450..c7d01f0972a 100644 --- a/packages/rs-platform-wallet-ffi/src/event_handler.rs +++ b/packages/rs-platform-wallet-ffi/src/event_handler.rs @@ -315,7 +315,9 @@ impl PlatformEventHandler for FFIEventHandler { return; }; let (code, reason) = match verdict { - ProbeVerdict::Accepted => (OUTGOING_PROBE_VERDICT_ACCEPTED, None), + // The host does not need to tell a mempool from a block: both mean + // the payment is going through. + ProbeVerdict::Accepted | ProbeVerdict::Mined => (OUTGOING_PROBE_VERDICT_ACCEPTED, None), ProbeVerdict::Dead { reason } => (OUTGOING_PROBE_VERDICT_DEAD, Some(reason)), ProbeVerdict::Unresolved { reason } => { (OUTGOING_PROBE_VERDICT_UNRESOLVED, Some(reason)) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index e301f8020b3..91f656b1b3e 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -76,9 +76,11 @@ const REFUSED_REASONS: &[&str] = &["txn-mempool-conflict"]; /// What a single Core node said when handed a signed transaction. #[derive(Debug, Clone, PartialEq, Eq)] pub enum NodeVerdict { - /// The node holds the transaction — it accepted it into its mempool now, - /// already had it there, or has it in a block. + /// The node holds the transaction in its mempool — it accepted it now or + /// already had it. Accepted, + /// The node has the transaction in a block (`-27`). + Mined, /// The node proved the transaction can never be mined as it stands. Dead { reason: String }, /// The node refused the transaction without proving it dead. @@ -90,7 +92,7 @@ pub enum NodeVerdict { /// Classify a failed `broadcastTransaction` gRPC response. pub(crate) fn classify_failed_submission(code: Code, message: &str) -> NodeVerdict { if code == Code::AlreadyExists { - return NodeVerdict::Accepted; + return NodeVerdict::Mined; } let lowered = message.to_ascii_lowercase(); if DEAD_REASONS.iter().any(|reason| lowered.contains(reason)) { @@ -114,8 +116,12 @@ pub(crate) fn classify_failed_submission(code: Code, message: &str) -> NodeVerdi /// The answer a probe gives about one transaction. #[derive(Debug, Clone, PartialEq, Eq)] pub enum ProbeVerdict { - /// A node holds the transaction: it is in a mempool or a block. + /// A node holds the transaction in its mempool. Not settlement: it can + /// still expire or lose to a conflict, so the resolver keeps asking. Accepted, + /// A node has the transaction in a block. Final — there is nothing left to + /// ask, even if this wallet has not seen that block yet. + Mined, /// Two different nodes refused it for missing or spent inputs, and two /// different nodes do not know its txid: it is not in a block and can /// never be mined. @@ -127,8 +133,8 @@ pub enum ProbeVerdict { /// Whether one node knows a txid (DAPI `getTransaction`). #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum LookupAnswer { - /// The node has the transaction, in its mempool or in a block. - Known, + /// The node has the transaction; `mined` when it is in a block. + Known { mined: bool }, /// The node answered and does not have it. NotFound, /// No answer: transport failure, timeout, anything else. @@ -193,6 +199,7 @@ pub(crate) async fn probe_with( ); match verdict { NodeVerdict::Accepted => return ProbeVerdict::Accepted, + NodeVerdict::Mined => return ProbeVerdict::Mined, NodeVerdict::Dead { reason } => { // A dead verdict from an unidentified node cannot count // towards the quorum — it might be the node we already heard. @@ -242,7 +249,8 @@ async fn confirm_dead(submitter: &dyn NodeSubmitter, txid: &Txid, reason: String "broadcast probe: txid lookup answered" ); match answer { - LookupAnswer::Known => return ProbeVerdict::Accepted, + LookupAnswer::Known { mined: true } => return ProbeVerdict::Mined, + LookupAnswer::Known { mined: false } => return ProbeVerdict::Accepted, LookupAnswer::NotFound => { if let Some(node) = node { not_found.insert(node); @@ -308,7 +316,13 @@ impl NodeSubmitter for DapiNodeSubmitter { id: txid.to_string(), }; match self.sdk.execute(request, single_node()).await { - Ok(response) => (Some(response.address.to_string()), LookupAnswer::Known), + Ok(response) => { + let mined = response.inner.height > 0 || response.inner.confirmations > 0; + ( + Some(response.address.to_string()), + LookupAnswer::Known { mined }, + ) + } Err(error) => { let node = error.address.as_ref().map(ToString::to_string); let answer = match &error.inner { @@ -462,7 +476,7 @@ mod tests { ( Code::AlreadyExists, "Transaction already in chain: Transaction already in block chain", - NodeVerdict::Accepted, + NodeVerdict::Mined, ), // -25, JS dapi. ( @@ -615,17 +629,17 @@ mod tests { /// Core answers `missingorspent` for a mined transaction whose outputs /// are all spent (testnet `e758f06a…`, 2026-09-29). A node that still - /// knows the txid must turn the dead quorum into Accepted. + /// knows the txid in a block must turn the dead quorum into Mined. #[tokio::test] - async fn should_accept_a_refused_transaction_that_a_node_still_knows() { + async fn should_report_mined_for_a_refused_transaction_a_node_has_in_a_block() { let nodes = ScriptedNodes::with_lookups( vec![(Some("a"), dead()), (Some("b"), dead())], - vec![(Some("c"), LookupAnswer::Known)], + vec![(Some("c"), LookupAnswer::Known { mined: true })], ); assert_eq!( probe_with(&nodes, &transaction()).await, - ProbeVerdict::Accepted + ProbeVerdict::Mined ); assert_eq!(nodes.lookups_made(), 1); } @@ -662,4 +676,19 @@ mod tests { assert_eq!(nodes.lookups_made(), 0); } + + /// Refused for its inputs yet still in a node's mempool (a double-spend + /// race the node lost locally): not dead, not mined — keep asking. + #[tokio::test] + async fn should_accept_a_refused_transaction_a_node_holds_in_its_mempool() { + let nodes = ScriptedNodes::with_lookups( + vec![(Some("a"), dead()), (Some("b"), dead())], + vec![(Some("c"), LookupAnswer::Known { mined: false })], + ); + + assert_eq!( + probe_with(&nodes, &transaction()).await, + ProbeVerdict::Accepted + ); + } } diff --git a/packages/rs-platform-wallet/src/broadcaster.rs b/packages/rs-platform-wallet/src/broadcaster.rs index 3a5bfe16370..87e2baff14f 100644 --- a/packages/rs-platform-wallet/src/broadcaster.rs +++ b/packages/rs-platform-wallet/src/broadcaster.rs @@ -148,8 +148,10 @@ impl TransactionBroadcaster for DapiBroadcaster { { Ok(_response) => Ok(transaction.txid()), Err(DapiClientError::Transport(TransportError::Grpc(status))) - if classify_failed_submission(status.code(), status.message()) - == NodeVerdict::Accepted => + if matches!( + classify_failed_submission(status.code(), status.message()), + NodeVerdict::Accepted | NodeVerdict::Mined + ) => { Ok(transaction.txid()) } diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 2f716d5ca29..28550bda981 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -26,7 +26,7 @@ //! at the tip, larger steps while catching up) while the root stays //! unsettled; after [`EVERY_BLOCK_WINDOW`] blocks, every [`SLOW_INTERVAL`] //! blocks. At most one height-driven pass per wallet runs at a time. Only -//! `Dead` ends the probing — acceptance by one node's mempool is not +//! `Dead` or `Mined` ends the probing — acceptance by one node's mempool is not //! settlement, so an `Accepted` root keeps being asked until it settles. A //! verdict is published only when it differs from the last one published for //! that root, and a root that settles or leaves the wallet after a verdict was @@ -70,28 +70,53 @@ pub(crate) struct OutgoingView { pub transaction: Transaction, } -/// The roots of this wallet's unsettled chains, one entry per txid: unsettled -/// transactions it signed that spend no output of any unsettled transaction — -/// its own or an incoming one. +/// The roots of this wallet's unsettled chains, one entry per txid. +/// +/// The chains that matter are the ones the wallet signed into: every unsettled +/// transaction that spends its own coins, plus every unsettled ancestor such a +/// transaction depends on — including someone else's incoming payment, whose +/// fate decides the send built on it. A root is a member of that set that +/// spends no output of any unsettled transaction, because a node that has not +/// seen a parent refuses a perfectly valid child. pub(crate) fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { - // A transaction recorded in several accounts is one transaction; if any - // account holds it as settled, it is. - let mut by_txid: BTreeMap = BTreeMap::new(); + // One entry per transaction. A transaction recorded in several accounts is + // one transaction: settled if any account holds it settled, the wallet's + // own send if any account records it spending the wallet's coins (a + // transfer between accounts is "incoming" in the receiving one). + let mut by_txid: BTreeMap = BTreeMap::new(); for view in views { by_txid .entry(view.txid) - .and_modify(|(_, settled)| *settled |= view.settled) - .or_insert((view, view.settled)); + .and_modify(|(_, settled, own)| { + *settled |= view.settled; + *own |= view.spends_own_coins; + }) + .or_insert((view, view.settled, view.spends_own_coins)); } let unsettled: BTreeSet = by_txid .iter() - .filter(|(_, (_, settled))| !*settled) + .filter(|(_, (_, settled, _))| !*settled) .map(|(txid, _)| *txid) .collect(); - unsettled + + // The wallet's unsettled sends and, transitively, their unsettled parents. + let mut relevant: BTreeSet = unsettled + .iter() + .filter(|txid| by_txid[*txid].2) + .copied() + .collect(); + let mut frontier: Vec = relevant.iter().copied().collect(); + while let Some(txid) = frontier.pop() { + for input in &by_txid[&txid].0.inputs { + if unsettled.contains(&input.txid) && relevant.insert(input.txid) { + frontier.push(input.txid); + } + } + } + + relevant .iter() .map(|txid| by_txid[txid].0) - .filter(|view| view.spends_own_coins) .filter(|view| { !view .inputs @@ -134,8 +159,9 @@ impl ProbeSchedule { #[derive(Debug, Default)] pub(crate) struct ResolverState { schedules: HashMap<(WalletId, Txid), ProbeSchedule>, - /// Roots proven dead: never probed again while they stay roots. - dead: HashSet<(WalletId, Txid)>, + /// Roots with a final verdict (dead or mined): never probed again while + /// they stay roots. + finished: HashSet<(WalletId, Txid)>, /// The last verdict published per root, so only changes are published. published: HashMap<(WalletId, Txid), ProbeVerdict>, } @@ -149,16 +175,53 @@ pub(crate) enum ResolverEvent { Cleared(Txid), } +impl ResolverState { + /// Drop everything kept for `wallet_id`; returns the roots the host had a + /// verdict for, which it must now be told are cleared. + fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { + self.schedules.retain(|(wallet, _), _| wallet != wallet_id); + self.finished.retain(|(wallet, _)| wallet != wallet_id); + let mut cleared: Vec = self + .published + .keys() + .filter(|(wallet, _)| wallet == wallet_id) + .map(|(_, txid)| *txid) + .collect(); + cleared.sort(); + self.published.retain(|(wallet, _), _| wallet != wallet_id); + cleared + } + + /// Drop everything; returns every root the host had a verdict for. + fn forget_all(&mut self) -> Vec<(WalletId, Txid)> { + self.schedules.clear(); + self.finished.clear(); + let mut cleared: Vec<(WalletId, Txid)> = + self.published.drain().map(|(key, _)| key).collect(); + cleared.sort(); + cleared + } +} + +/// How far a wallet's synced height may advance in one step and still count +/// as following the tip. Larger steps are catch-up after time offline: the +/// scan itself settles most roots, so a height-driven pass waits for it. +pub(crate) const CATCH_UP_STEP: u32 = 3; + +pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { + previous.is_some_and(|previous| height.saturating_sub(previous) > CATCH_UP_STEP) +} + /// One pass over one wallet at `height`: probe every due root and return what -/// to publish. `force` makes one root due regardless of its schedule (the SPV -/// broadcaster has just reported it `Uncertain`). +/// to publish. `forced` roots are due regardless of their schedule (the SPV +/// broadcaster has just reported them `Uncertain`). pub(crate) async fn resolve_pass( state: &Mutex, probe: &dyn AcceptanceProbe, wallet_id: WalletId, height: u32, views: &[OutgoingView], - force: Option, + forced: &HashSet, ) -> Vec { let roots = ambiguous_roots(views); let root_ids: HashSet = roots.iter().map(|view| view.txid).collect(); @@ -169,7 +232,7 @@ pub(crate) async fn resolve_pass( let mut guard = state.lock().expect("resolver state poisoned"); let ResolverState { schedules, - dead, + finished, published, } = &mut *guard; // Forget this wallet's roots that settled or left the wallet; tell the @@ -177,7 +240,7 @@ pub(crate) async fn resolve_pass( let is_gone = |wallet: &WalletId, txid: &Txid| *wallet == wallet_id && !root_ids.contains(txid); schedules.retain(|(wallet, txid), _| !is_gone(wallet, txid)); - dead.retain(|(wallet, txid)| !is_gone(wallet, txid)); + finished.retain(|(wallet, txid)| !is_gone(wallet, txid)); let mut cleared: Vec = published .keys() .filter(|(wallet, txid)| is_gone(wallet, txid)) @@ -190,13 +253,13 @@ pub(crate) async fn resolve_pass( let mut due = Vec::new(); for view in &roots { let key = (wallet_id, view.txid); - if dead.contains(&key) { + if finished.contains(&key) { continue; } let schedule = schedules .entry(key) .or_insert_with(|| ProbeSchedule::new(height)); - if force == Some(view.txid) || schedule.is_due(height) { + if forced.contains(&view.txid) || schedule.is_due(height) { schedule.probed_at(height); due.push((view.txid, view.transaction.clone())); } @@ -208,8 +271,13 @@ pub(crate) async fn resolve_pass( let verdict = probe.probe(&transaction).await; let key = (wallet_id, txid); let mut guard = state.lock().expect("resolver state poisoned"); - if matches!(verdict, ProbeVerdict::Dead { .. }) { - guard.dead.insert(key); + // A pass that ran meanwhile may have found this root settled or gone + // and cleared it; a verdict published now would resurrect it. + if !guard.schedules.contains_key(&key) { + continue; + } + if matches!(verdict, ProbeVerdict::Dead { .. } | ProbeVerdict::Mined) { + guard.finished.insert(key); } // Publish changes only. Unresolved reasons vary between probes // (different nodes, different errors), so all Unresolved count as one. @@ -228,45 +296,44 @@ pub(crate) async fn resolve_pass( events } -/// This wallet's unsettled transactions, one view per account record: the ones -/// it signed (the candidate roots) and the incoming ones (whose outputs a send -/// may spend, which disqualifies that send as a root). Settled records are -/// skipped rather than cloned. +/// This wallet's unsettled transactions, one view per transaction, merged +/// across the accounts that record it (settled if any account holds it +/// settled; the wallet's own if any account records it spending the wallet's +/// coins). One walk over the records; only unsettled transactions are cloned — +/// on a healthy wallet there are none or a handful. pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { let accounts = info.core_wallet.accounts.all_accounts(); - // A transaction settled in any account is settled. - let mut settled_anywhere: HashSet = HashSet::new(); + let mut merged: BTreeMap = BTreeMap::new(); for account in accounts.iter() { for record in account.transactions().values() { - if record.is_confirmed() + let settled = record.is_confirmed() || matches!(record.context, TransactionContext::InstantSend(_)) - || account.transaction_is_finalized(&record.txid) - { - settled_anywhere.insert(record.txid); - } - } - } - let mut views = Vec::new(); - for account in accounts.iter() { - for record in account.transactions().values() { - if settled_anywhere.contains(&record.txid) { - continue; - } - views.push(OutgoingView { - txid: record.txid, - inputs: record - .transaction - .input - .iter() - .map(|input| input.previous_output) - .collect(), - settled: false, - spends_own_coins: !record.input_details.is_empty(), - transaction: record.transaction.clone(), - }); + || account.transaction_is_finalized(&record.txid); + let own = !record.input_details.is_empty(); + merged + .entry(record.txid) + .and_modify(|(was_settled, was_own, _)| { + *was_settled |= settled; + *was_own |= own; + }) + .or_insert((settled, own, &record.transaction)); } } - views + merged + .into_iter() + .filter(|(_, (settled, _, _))| !*settled) + .map(|(txid, (_, own, transaction))| OutgoingView { + txid, + inputs: transaction + .input + .iter() + .map(|input| input.previous_output) + .collect(), + settled: false, + spends_own_coins: own, + transaction: transaction.clone(), + }) + .collect() } /// Admission gate for probe tasks. A probe only reads the wallet and @@ -320,8 +387,25 @@ impl ProbeTasks { } } +/// Which wallets have a pass running and what the next pass for each must do. +/// A pass for a wallet never runs twice at once; requests that arrive while +/// one runs are merged into the next. +#[derive(Default)] +struct PassQueue { + running: HashSet, + pending: HashMap, +} + +#[derive(Default)] +struct PendingPass { + height: u32, + forced: HashSet, +} + /// Runs probe passes off the wallet-event fan-out and publishes each verdict -/// as [`PlatformEventHandler::on_outgoing_transaction_probed`]. +/// change as [`PlatformEventHandler::on_outgoing_transaction_probed`], and each +/// root that stops being ambiguous as +/// [`PlatformEventHandler::on_outgoing_transaction_cleared`]. /// /// Off until the host turns it on with [`set_enabled`](Self::set_enabled): a /// probe sends the signed transaction to evonodes over DAPI, which is a @@ -331,12 +415,10 @@ pub(crate) struct BroadcastResolver { probe: Arc, wallet_manager: Arc>>, state: Arc>, - /// Last synced height per wallet, so an SPV-level `Uncertain` (which - /// carries no wallet id) can run a pass on every wallet at once. - heights: Mutex>, - /// Wallets with a height-driven pass running: a burst of height advances - /// (catch-up after time offline) must not stack passes for one wallet. - in_flight: Arc>>, + /// Last synced height per wallet: an SPV-level `Uncertain` carries no + /// wallet id, so it is queued for every wallet at its current height. + heights: Arc>>, + passes: Arc>, events: Mutex>, tasks: ProbeTasks, } @@ -351,8 +433,8 @@ impl BroadcastResolver { probe, wallet_manager, state: Arc::new(Mutex::new(ResolverState::default())), - heights: Mutex::new(HashMap::new()), - in_flight: Arc::new(Mutex::new(HashSet::new())), + heights: Arc::new(Mutex::new(HashMap::new())), + passes: Arc::new(Mutex::new(PassQueue::default())), events: Mutex::new(Weak::new()), tasks: ProbeTasks::new(), } @@ -364,8 +446,22 @@ impl BroadcastResolver { *self.events.lock().expect("events mutex poisoned") = events; } + /// Turning probing off forgets every root and tells the host to drop every + /// verdict it holds: with no passes running, nothing would ever clear them. pub(crate) fn set_enabled(&self, enabled: bool) { - self.enabled.store(enabled, Ordering::SeqCst); + let was = self.enabled.swap(enabled, Ordering::SeqCst); + if was && !enabled { + let cleared = self + .state + .lock() + .expect("resolver state poisoned") + .forget_all(); + if let Some(events) = self.events.lock().expect("events mutex poisoned").upgrade() { + for (wallet_id, txid) in &cleared { + events.on_outgoing_transaction_cleared(wallet_id, txid); + } + } + } } pub(crate) fn is_enabled(&self) -> bool { @@ -377,96 +473,125 @@ impl BroadcastResolver { self.tasks.quiesce_within(budget).await } - fn spawn_pass(&self, wallet_id: WalletId, height: u32, force: Option) { - // A height-driven pass is skipped while one for this wallet runs; a - // forced pass (dash-spv just reported `Uncertain`) always runs — the - // schedule marks a root probed before probing, so the two cannot probe - // the same root twice unless forced. - let in_flight = if force.is_none() { - if !self - .in_flight - .lock() - .expect("in-flight mutex poisoned") - .insert(wallet_id) - { - return; + /// Queue a pass for `wallet_id`; start a worker for it unless one runs. + fn request_pass(&self, wallet_id: WalletId, height: u32, force: Option) { + { + let mut passes = self.passes.lock().expect("pass queue poisoned"); + let pending = passes.pending.entry(wallet_id).or_default(); + pending.height = pending.height.max(height); + pending.forced.extend(force); + if !passes.running.insert(wallet_id) { + return; // the running worker picks it up } - Some(InFlight { - set: Arc::clone(&self.in_flight), - wallet_id, - }) - } else { - None - }; + } let wallet_manager = Arc::clone(&self.wallet_manager); let probe = Arc::clone(&self.probe); let state = Arc::clone(&self.state); + let heights = Arc::clone(&self.heights); + let passes = Arc::clone(&self.passes); let events = self.events.lock().expect("events mutex poisoned").clone(); self.tasks.spawn(async move { - let _in_flight = in_flight; - // Read under the lock, probe without it: a probe is network I/O. - let views = { - let manager = wallet_manager.read().await; - match manager.get_wallet_info(&wallet_id) { - Some(info) => collect_views(info), - None => return, - } - }; - let outcome = - resolve_pass(&state, probe.as_ref(), wallet_id, height, &views, force).await; - if outcome.is_empty() { - return; - } - let events = events.upgrade(); - // What started this pass: dash-spv's Uncertain result for one - // transaction, or an advance of the wallet's synced height. - let trigger = if force.is_some() { - "uncertain" - } else { - "height" + let _running = Running { + passes: Arc::clone(&passes), + wallet_id, }; - for event in &outcome { - match event { - ResolverEvent::Verdict(txid, verdict) => { - tracing::info!( - wallet_id = %hex::encode(wallet_id), - txid = %txid, - height, - trigger, - ?verdict, - "broadcast probe: verdict for an unconfirmed send" - ); - if let Some(events) = &events { - events.on_outgoing_transaction_probed(&wallet_id, txid, verdict); - } + loop { + let Some(PendingPass { height, forced }) = passes + .lock() + .expect("pass queue poisoned") + .pending + .remove(&wallet_id) + else { + break; + }; + // Read under the lock, probe without it: a probe is network I/O. + let views = { + let manager = wallet_manager.read().await; + manager.get_wallet_info(&wallet_id).map(collect_views) + }; + let outcome = match views { + Some(views) => { + resolve_pass(&state, probe.as_ref(), wallet_id, height, &views, &forced) + .await } - ResolverEvent::Cleared(txid) => { - tracing::info!( - wallet_id = %hex::encode(wallet_id), - txid = %txid, - height, - "broadcast probe: send no longer unconfirmed, verdict cleared" - ); - if let Some(events) = &events { - events.on_outgoing_transaction_cleared(&wallet_id, txid); - } + None => { + // The wallet was removed: forget it and clear whatever + // the host still shows for it. + heights + .lock() + .expect("heights mutex poisoned") + .remove(&wallet_id); + state + .lock() + .expect("resolver state poisoned") + .forget_wallet(&wallet_id) + .into_iter() + .map(ResolverEvent::Cleared) + .collect() } - } + }; + publish(&events, wallet_id, height, !forced.is_empty(), &outcome); } }); } } -/// Removes a wallet from the in-flight set when its pass ends, however it ends. -struct InFlight { - set: Arc>>, +/// Hand a pass's outcome to the host and the log. +fn publish( + events: &Weak, wallet_id: WalletId, + height: u32, + forced: bool, + outcome: &[ResolverEvent], +) { + if outcome.is_empty() { + return; + } + let events = events.upgrade(); + // What started this pass: dash-spv's Uncertain result, or an advance of + // the wallet's synced height. + let trigger = if forced { "uncertain" } else { "height" }; + for event in outcome { + match event { + ResolverEvent::Verdict(txid, verdict) => { + tracing::info!( + wallet_id = %hex::encode(wallet_id), + txid = %txid, + height, + trigger, + ?verdict, + "broadcast probe: verdict for an unconfirmed send" + ); + if let Some(events) = &events { + events.on_outgoing_transaction_probed(&wallet_id, txid, verdict); + } + } + ResolverEvent::Cleared(txid) => { + tracing::info!( + wallet_id = %hex::encode(wallet_id), + txid = %txid, + height, + "broadcast probe: send no longer unconfirmed, verdict cleared" + ); + if let Some(events) = &events { + events.on_outgoing_transaction_cleared(&wallet_id, txid); + } + } + } + } } -impl Drop for InFlight { +/// Marks a wallet's worker finished however it ends (including an abort), so +/// the next request starts a new one. +struct Running { + passes: Arc>, + wallet_id: WalletId, +} + +impl Drop for Running { fn drop(&mut self) { - if let Ok(mut set) = self.set.lock() { - set.remove(&self.wallet_id); + if let Ok(mut passes) = self.passes.lock() { + passes.running.remove(&self.wallet_id); } } } @@ -476,12 +601,13 @@ impl EventHandler for BroadcastResolver { let WalletEvent::SyncHeightAdvanced { wallet_id, height } = event else { return; }; - self.heights + let previous = self + .heights .lock() .expect("heights mutex poisoned") .insert(*wallet_id, *height); - if self.is_enabled() { - self.spawn_pass(*wallet_id, *height, None); + if self.is_enabled() && !is_catch_up_step(previous, *height) { + self.request_pass(*wallet_id, *height, None); } } @@ -504,7 +630,7 @@ impl EventHandler for BroadcastResolver { .map(|(wallet, height)| (*wallet, *height)) .collect(); for (wallet_id, height) in heights { - self.spawn_pass(wallet_id, height, Some(*txid)); + self.request_pass(wallet_id, height, Some(*txid)); } } } @@ -693,7 +819,7 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), dead())]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; - let events = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; + let events = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; assert_eq!(probe.probed(), vec![txid(1)]); assert_eq!(events, vec![ResolverEvent::Verdict(txid(1), dead())]); @@ -705,8 +831,8 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), dead())]); let views = [send(1, &[outpoint(90, 0)])]; - resolve_pass(&state, &probe, wallet(), 100, &views, None).await; - let again = resolve_pass(&state, &probe, wallet(), 101, &views, None).await; + resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + let again = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; assert!(again.is_empty()); assert_eq!(probe.probed(), vec![txid(1)]); @@ -720,8 +846,8 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); let views = [send(1, &[outpoint(90, 0)])]; - let first = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; - let second = resolve_pass(&state, &probe, wallet(), 101, &views, None).await; + let first = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + let second = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; assert_eq!( first, @@ -740,9 +866,9 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)])]; - let first = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; - let same_block = resolve_pass(&state, &probe, wallet(), 100, &views, None).await; - let next_block = resolve_pass(&state, &probe, wallet(), 101, &views, None).await; + let first = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + let same_block = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + let next_block = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; assert_eq!(first, vec![ResolverEvent::Verdict(txid(1), unresolved())]); assert!(same_block.is_empty(), "one probe per block"); @@ -761,8 +887,16 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)])]; - resolve_pass(&state, &probe, wallet(), 100, &views, None).await; - resolve_pass(&state, &probe, wallet(), 100, &views, Some(txid(1))).await; + resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + resolve_pass( + &state, + &probe, + wallet(), + 100, + &views, + &HashSet::from([txid(1)]), + ) + .await; assert_eq!(probe.probed(), vec![txid(1), txid(1)]); } @@ -775,29 +909,54 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); let views = [send(1, &[outpoint(90, 0)])]; - resolve_pass(&state, &probe, wallet(), 100, &views, None).await; - let gone = resolve_pass(&state, &probe, wallet(), 101, &[], None).await; + resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + let gone = resolve_pass(&state, &probe, wallet(), 101, &[], &HashSet::new()).await; assert_eq!(gone, vec![ResolverEvent::Cleared(txid(1))]); let guard = state.lock().expect("state"); assert!(guard.schedules.is_empty()); - assert!(guard.dead.is_empty()); + assert!(guard.finished.is_empty()); assert!(guard.published.is_empty()); } /// Nodes that have not seen an unconfirmed incoming payment refuse a send - /// that spends it; such a send is not a root until its parent settles. + /// that spends it, so the send is not probed itself — the incoming parent + /// is, since its fate decides the send's. Once the parent settles, the + /// send is the root. #[test] - fn should_not_pick_a_send_that_spends_an_unconfirmed_incoming_coin() { + fn should_probe_the_unconfirmed_incoming_parent_of_a_send_instead_of_the_send() { let mut incoming = send(5, &[outpoint(80, 0)]); incoming.spends_own_coins = false; let send_on_it = send(6, &[outpoint(5, 0)]); - assert!(roots(&[incoming.clone(), send_on_it.clone()]).is_empty()); + assert_eq!( + roots(&[incoming.clone(), send_on_it.clone()]), + BTreeSet::from([txid(5)]) + ); incoming.settled = true; assert_eq!(roots(&[incoming, send_on_it]), BTreeSet::from([txid(6)])); } + /// An unconfirmed incoming payment nobody built on is not this wallet's + /// business to probe. + #[test] + fn should_not_probe_an_incoming_payment_the_wallet_did_not_spend() { + let mut incoming = send(5, &[outpoint(80, 0)]); + incoming.spends_own_coins = false; + assert!(roots(&[incoming]).is_empty()); + } + + /// A transfer between the wallet's own accounts is recorded twice: as + /// incoming in the receiving account (listed first) and as spending in the + /// sending one. It is the wallet's own send either way. + #[test] + fn should_treat_a_transfer_between_own_accounts_as_the_wallets_send() { + let mut as_received = send(1, &[outpoint(90, 0)]); + as_received.spends_own_coins = false; + let as_sent = send(1, &[outpoint(90, 0)]); + assert_eq!(roots(&[as_received, as_sent]), BTreeSet::from([txid(1)])); + } + /// Two wallets' roots are tracked independently. #[tokio::test] async fn should_not_let_one_wallets_pass_forget_another_wallets_roots() { @@ -805,9 +964,130 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)])]; - resolve_pass(&state, &probe, wallet(), 100, &views, None).await; - resolve_pass(&state, &probe, [9u8; 32], 100, &[], None).await; + resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + resolve_pass(&state, &probe, [9u8; 32], 100, &[], &HashSet::new()).await; assert_eq!(state.lock().expect("state").schedules.len(), 1); } + + /// A mined transaction the wallet has not caught up with yet: nothing left + /// to ask, so it is not probed again. + #[tokio::test] + async fn should_stop_probing_a_root_found_in_a_block() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined)]); + let views = [send(1, &[outpoint(90, 0)])]; + + resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + let again = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; + + assert!(again.is_empty()); + assert_eq!(probe.probed(), vec![txid(1)]); + } + + #[test] + fn should_treat_only_large_height_steps_as_catch_up() { + assert!(!is_catch_up_step(None, 1_000)); + assert!(!is_catch_up_step(Some(999), 1_000)); + assert!(!is_catch_up_step(Some(1_000 - CATCH_UP_STEP), 1_000)); + assert!(is_catch_up_step(Some(1_000 - CATCH_UP_STEP - 1), 1_000)); + } + + #[tokio::test] + async fn should_forget_one_wallet_and_return_its_published_roots() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), unresolved()), (txid(2), unresolved())]); + resolve_pass( + &state, + &probe, + wallet(), + 100, + &[send(1, &[outpoint(90, 0)])], + &HashSet::new(), + ) + .await; + resolve_pass( + &state, + &probe, + [9u8; 32], + 100, + &[send(2, &[outpoint(91, 0)])], + &HashSet::new(), + ) + .await; + + let cleared = state.lock().expect("state").forget_wallet(&wallet()); + + assert_eq!(cleared, vec![txid(1)]); + let guard = state.lock().expect("state"); + assert_eq!(guard.published.len(), 1, "the other wallet is untouched"); + assert_eq!(guard.schedules.len(), 1); + } + + #[tokio::test] + async fn should_forget_everything_and_return_every_published_root() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), unresolved()), (txid(2), unresolved())]); + resolve_pass( + &state, + &probe, + wallet(), + 100, + &[send(1, &[outpoint(90, 0)])], + &HashSet::new(), + ) + .await; + resolve_pass( + &state, + &probe, + [9u8; 32], + 100, + &[send(2, &[outpoint(91, 0)])], + &HashSet::new(), + ) + .await; + + let cleared = state.lock().expect("state").forget_all(); + + assert_eq!(cleared.len(), 2); + let guard = state.lock().expect("state"); + assert!( + guard.published.is_empty() && guard.schedules.is_empty() && guard.finished.is_empty() + ); + } + + /// A probe that, while it is out on the network, lets a concurrent pass + /// clear the root (it settled): the late verdict must not be published. + struct ClearingProbe { + state: Arc>, + } + + #[async_trait] + impl AcceptanceProbe for ClearingProbe { + async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { + self.state.lock().expect("state").forget_wallet(&wallet()); + ProbeVerdict::Accepted + } + } + + #[tokio::test] + async fn should_not_publish_a_verdict_for_a_root_cleared_during_the_probe() { + let state = Arc::new(Mutex::new(ResolverState::default())); + let probe = ClearingProbe { + state: Arc::clone(&state), + }; + + let events = resolve_pass( + &state, + &probe, + wallet(), + 100, + &[send(1, &[outpoint(90, 0)])], + &HashSet::new(), + ) + .await; + + assert!(events.is_empty()); + assert!(state.lock().expect("state").published.is_empty()); + } } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index 7c77e71af55..f9296607032 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -51,7 +51,9 @@ public struct OutgoingTransactionProbeEvent: Sendable, Equatable { /// C trampoline for /// `EventHandlerCallbacksExtension.on_outgoing_transaction_probed_fn`. Rust /// owns every pointer only for this call, so everything is copied before the -/// main-actor hop. +/// hop to the main queue. `DispatchQueue.main` is FIFO, so a verdict and a +/// later clear for the same send are applied in the order Rust sent them — +/// separate `Task`s give no such guarantee. func outgoingTransactionProbedCallback( context: UnsafeMutableRawPointer?, walletIdPtr: UnsafePointer?, @@ -68,8 +70,10 @@ func outgoingTransactionProbedCallback( let txidWire = Data(bytes: txidPtr, count: 32) if verdictCode == UInt8(OUTGOING_PROBE_VERDICT_CLEARED) { let key = OutgoingTransactionKey(walletId: walletId, txidWire: txidWire) - Task { @MainActor [weak manager = handler.manager] in - manager?.handleOutgoingTransactionCleared(key) + DispatchQueue.main.async { [weak manager = handler.manager] in + MainActor.assumeIsolated { + manager?.handleOutgoingTransactionCleared(key) + } } return } @@ -89,8 +93,10 @@ func outgoingTransactionProbedCallback( verdict: verdict ) - Task { @MainActor [weak manager = handler.manager] in - manager?.handleOutgoingTransactionProbed(event) + DispatchQueue.main.async { [weak manager = handler.manager] in + MainActor.assumeIsolated { + manager?.handleOutgoingTransactionProbed(event) + } } } From 9f7ec324a46d4c4eaf6c3061c5a67cd87ac942da Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 17:11:37 +0300 Subject: [PATCH 008/169] refactor(platform-wallet): move the abandon planner to its own change abandon_plan has no caller in this change: it belongs with the user-driven cancel of a proven-dead send, which lands as a separate PR stacked on this one (branch feat/cancel-proven-dead-send). Removes the module and the SQLite / SwiftData tests that pin its sweep-batch contract; they move there unchanged. This change is now read-only end to end. Ticket 32347. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../tests/sqlite_transaction_sweeps.rs | 143 ------- .../src/wallet/core/abandon_plan.rs | 359 ------------------ .../src/wallet/core/broadcast_resolver.rs | 2 +- .../rs-platform-wallet/src/wallet/core/mod.rs | 3 - .../SweptTransactionPersistTests.swift | 83 ---- 5 files changed, 1 insertion(+), 589 deletions(-) delete mode 100644 packages/rs-platform-wallet/src/wallet/core/abandon_plan.rs diff --git a/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs b/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs index a4db6a4204c..6a7bbc1361d 100644 --- a/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs +++ b/packages/rs-platform-wallet-storage/tests/sqlite_transaction_sweeps.rs @@ -3823,146 +3823,3 @@ fn should_remove_a_swept_height_only_transaction_and_its_outputs() { .unwrap(); assert_eq!(count, 0); } - -/// The batch `abandon_plan` builds for a chain the network proved dead -/// (rust-dashcore#961's abandon, mirrored through this same channel): every -/// input the chain took from outside itself is released, `superseded_by` is -/// the chain's own root, and there is no winner height. Nothing may be held — -/// a hold would be attributed to the root, a transaction that no longer -/// exists, and nothing could ever resolve it. -/// -/// Shape of ticket 32347: root R spends a real coin F:0 and leaves change -/// R:1; child C spends that change plus another real coin F:1. -#[test] -fn should_free_an_abandoned_chains_outside_inputs_and_hold_nothing() { - let (persister, _tmp, _path) = fresh_persister(); - let w: WalletId = wid(0xE9); - ensure_wallet_meta(&persister, &w); - - let addr = p2pkh(0x09); - let funding_txid = Txid::from_byte_array([0x90; 32]); - let real_a = OutPoint::new(funding_txid, 0); - let real_b = OutPoint::new(funding_txid, 1); - - let root_txid = Txid::from_byte_array([0x91; 32]); - let root_change = OutPoint::new(root_txid, 1); - let child_txid = Txid::from_byte_array([0x92; 32]); - let child_change = OutPoint::new(child_txid, 1); - - let pay_elsewhere = TxOut { - value: 100, - script_pubkey: p2pkh(0xEE).script_pubkey(), - }; - let root = tx_record( - root_txid, - vec![real_a], - vec![ - pay_elsewhere.clone(), - TxOut { - value: 300, - script_pubkey: addr.script_pubkey(), - }, - ], - ); - let child = tx_record( - child_txid, - vec![root_change, real_b], - vec![ - pay_elsewhere, - TxOut { - value: 600, - script_pubkey: addr.script_pubkey(), - }, - ], - ); - - let mut conn = persister.lock_conn_for_test(); - derive_address(&conn, &w, 0, &addr); - { - let tx = conn.transaction().unwrap(); - let cs = CoreChangeSet { - new_utxos: vec![ - make_utxo(&addr, funding_txid, 0, 500), - make_utxo(&addr, funding_txid, 1, 400), - ], - ..Default::default() - }; - core_state::apply(&tx, &w, &cs).unwrap(); - tx.commit().unwrap(); - } - { - let tx = conn.transaction().unwrap(); - let cs = CoreChangeSet { - records: vec![root, child], - new_utxos: vec![ - make_utxo(&addr, root_txid, 1, 300), - make_utxo(&addr, child_txid, 1, 600), - ], - spent_utxos: vec![ - make_utxo(&addr, funding_txid, 0, 500), - make_utxo(&addr, root_txid, 1, 300), - make_utxo(&addr, funding_txid, 1, 400), - ], - ..Default::default() - }; - core_state::apply(&tx, &w, &cs).unwrap(); - tx.commit().unwrap(); - } - // Sanity: the phantom state the customer's wallet was in — the real coins - // spent, the chain's final change counted as money. - assert_eq!(unspent(&conn, &w), std::collections::BTreeSet::from([child_change])); - - { - let tx = conn.transaction().unwrap(); - let cs = CoreChangeSet { - sweeps: vec![SweepBatch { - txids: vec![root_txid, child_txid], - superseded_by: root_txid, - winner_mined_height: None, - released_outpoints: vec![real_a, real_b], - }], - ..Default::default() - }; - core_state::apply(&tx, &w, &cs).unwrap(); - tx.commit().unwrap(); - } - - assert_eq!( - unspent(&conn, &w), - std::collections::BTreeSet::from([real_a, real_b]), - "exactly the real coins come back; the chain's change is gone" - ); - assert!(!row_exists(&conn, &w, &root_change), "root change must be deleted"); - assert!(!row_exists(&conn, &w, &child_change), "child change must be deleted"); - - let placeholders: i64 = conn - .query_row( - "SELECT COUNT(*) FROM core_utxos WHERE wallet_id = ?1 AND is_sweep_placeholder = 1", - params![w.as_slice()], - |row| row.get(0), - ) - .unwrap(); - assert_eq!(placeholders, 0, "an abandon must never create a held placeholder"); - - let held_by_root: i64 = conn - .query_row( - "SELECT COUNT(*) FROM core_utxos WHERE wallet_id = ?1 AND spent_in_txid = ?2", - params![w.as_slice(), AsRef::<[u8]>::as_ref(&root_txid)], - |row| row.get(0), - ) - .unwrap(); - assert_eq!(held_by_root, 0, "no row may be held under the abandoned root"); - - let chain_rows: i64 = conn - .query_row( - "SELECT COUNT(*) FROM core_transactions WHERE wallet_id = ?1 AND txid IN (?2, ?3)", - params![ - w.as_slice(), - AsRef::<[u8]>::as_ref(&root_txid), - AsRef::<[u8]>::as_ref(&child_txid) - ], - |row| row.get(0), - ) - .unwrap(); - assert_eq!(chain_rows, 0, "both transaction rows must be deleted"); -} diff --git a/packages/rs-platform-wallet/src/wallet/core/abandon_plan.rs b/packages/rs-platform-wallet/src/wallet/core/abandon_plan.rs deleted file mode 100644 index a49dfb79c28..00000000000 --- a/packages/rs-platform-wallet/src/wallet/core/abandon_plan.rs +++ /dev/null @@ -1,359 +0,0 @@ -//! Planning the removal of an outgoing transaction the network proved dead. -//! -//! A transaction the network will never accept still mutated the wallet when -//! it was sent: its change was credited and its inputs marked spent. Once a -//! probe (see [`crate::broadcast_probe`]) proves it dead, the wallet drops it -//! and every transaction built on its change — key-wallet's -//! `abandon_transaction_with_spends` (rust-dashcore#961) — and the persisters -//! must drop the same rows, or the next load hands the phantom change back. -//! -//! The removal reaches the persisters as an ordinary [`SweepBatch`], the -//! channel they already apply for conflict sweeps (#4558/#4559/#4589): the -//! batch's `txids` rows and every UTXO they created are deleted, and its -//! `released_outpoints` are handed back as unspent. A sweep normally names the -//! transaction that beat the losers in `superseded_by`, and persisters hold -//! every input *not* released under that winner. An abandon has no winner, so -//! the plan releases **every** input the chain took from outside itself and -//! `superseded_by` names the root — a label no persister reads, because a -//! released input is never held. -//! -//! That is only true while no other transaction of this wallet still claims -//! one of those inputs. If one did, a persister would keep that input held and -//! attribute the hold to `superseded_by` — here the root, a transaction that no -//! longer exists — leaving a hold nothing could ever resolve. The plan -//! therefore refuses to abandon in that case. It is not a case the probe -//! should produce anyway: a claimant already in a block or IS-locked makes -//! key-wallet sweep the chain itself, with the real winner, before any probe -//! runs; a claimant still in the mempool makes Core answer -//! `txn-mempool-conflict`, which the probe reports as refused, not dead. - -use std::collections::{BTreeMap, BTreeSet}; - -use dashcore::{OutPoint, Txid}; - -use crate::changeset::changeset::SweepBatch; - -/// What the planner needs to know about one recorded transaction. -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct RecordView { - pub txid: Txid, - pub inputs: Vec, - /// In a block or InstantSend-locked. A settled transaction spent real - /// coins; it is never abandoned and never followed. - pub settled: bool, -} - -/// Why a proven-dead root cannot be abandoned as planned. -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) enum AbandonRefusal { - /// The wallet holds no record of the root — nothing to remove, or it was - /// already swept by a conflicting spend. - UnknownRoot, - /// The root is in a block or IS-locked: whatever the probe said, it is - /// not dead. - SettledRoot, - /// A transaction outside the abandoned chain also spends one of the - /// chain's inputs, so releasing that input would contradict a surviving - /// claim. - InputClaimedBySurvivor { outpoint: OutPoint, claimant: Txid }, - /// A settled transaction spends an output of the chain, so the chain is - /// on chain itself and not dead. - SettledDescendant { txid: Txid }, -} - -/// The rows an abandon removes and the coins it hands back. -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct AbandonPlan { - pub root: Txid, - /// The root and every unsettled transaction built on it, transitively. - pub txids: BTreeSet, - /// Every input the chain took from outside itself. - pub released_outpoints: BTreeSet, -} - -impl AbandonPlan { - /// The persistence batch for this plan — see the module docs for why - /// `superseded_by` is the root and nothing is held. - pub(crate) fn sweep_batch(&self) -> SweepBatch { - SweepBatch { - txids: self.txids.iter().copied().collect(), - superseded_by: self.root, - winner_mined_height: None, - released_outpoints: self.released_outpoints.iter().copied().collect(), - } - } -} - -/// Plan the abandon of `root`. `records` may list one transaction more than -/// once (key-wallet records a transaction in every account it touches). -pub(crate) fn plan_abandon<'a>( - records: impl IntoIterator, - root: Txid, -) -> Result { - // One entry per transaction. A transaction recorded in several accounts - // is the same transaction; if any account holds it as settled, it is. - let mut by_txid: BTreeMap = BTreeMap::new(); - for record in records { - by_txid - .entry(record.txid) - .and_modify(|(_, settled)| *settled |= record.settled) - .or_insert((record.inputs.as_slice(), record.settled)); - } - - match by_txid.get(&root) { - None => return Err(AbandonRefusal::UnknownRoot), - Some((_, true)) => return Err(AbandonRefusal::SettledRoot), - Some((_, false)) => {} - } - - // The chain: the root plus every transaction spending an output of - // something already in it, transitively. A settled spender ends the plan - // instead of being skipped — see `SettledDescendant`. - let mut txids = BTreeSet::from([root]); - loop { - let mut grew = false; - for (txid, (inputs, settled)) in &by_txid { - if txids.contains(txid) || !inputs.iter().any(|input| txids.contains(&input.txid)) { - continue; - } - if *settled { - return Err(AbandonRefusal::SettledDescendant { txid: *txid }); - } - txids.insert(*txid); - grew = true; - } - if !grew { - break; - } - } - - let released_outpoints: BTreeSet = txids - .iter() - .flat_map(|txid| by_txid[txid].0.iter().copied()) - .filter(|input| !txids.contains(&input.txid)) - .collect(); - - // Nothing outside the chain may still claim a coin the chain releases: - // the persister would hold it under `superseded_by`, and that is the - // root, which is about to stop existing. - for (claimant, (inputs, _)) in &by_txid { - if txids.contains(claimant) { - continue; - } - if let Some(outpoint) = inputs - .iter() - .find(|input| released_outpoints.contains(input)) - { - return Err(AbandonRefusal::InputClaimedBySurvivor { - outpoint: *outpoint, - claimant: *claimant, - }); - } - } - - Ok(AbandonPlan { - root, - txids, - released_outpoints, - }) -} - -#[cfg(test)] -mod tests { - use dashcore::hashes::Hash; - - use super::*; - - fn txid(n: u8) -> Txid { - Txid::from_byte_array([n; 32]) - } - - fn outpoint(n: u8, vout: u32) -> OutPoint { - OutPoint { - txid: txid(n), - vout, - } - } - - fn record(n: u8, inputs: &[OutPoint]) -> RecordView { - RecordView { - txid: txid(n), - inputs: inputs.to_vec(), - settled: false, - } - } - - fn settled(n: u8, inputs: &[OutPoint]) -> RecordView { - RecordView { - settled: true, - ..record(n, inputs) - } - } - - #[test] - fn should_release_every_input_of_a_lone_dead_transaction() { - let records = [record(1, &[outpoint(90, 0), outpoint(91, 3)])]; - - let plan = plan_abandon(&records, txid(1)).expect("abandonable"); - - assert_eq!(plan.txids, BTreeSet::from([txid(1)])); - assert_eq!( - plan.released_outpoints, - BTreeSet::from([outpoint(90, 0), outpoint(91, 3)]) - ); - } - - /// The customer's shape: sends built on the change of a dead send. The - /// whole chain goes; the only coins handed back are the ones the chain - /// took from outside itself — the change it spent internally was never - /// real. - #[test] - fn should_take_the_whole_chain_and_release_only_its_outside_inputs() { - let records = [ - record(1, &[outpoint(90, 0)]), - record(2, &[outpoint(1, 1)]), - record(3, &[outpoint(2, 1), outpoint(91, 0)]), - ]; - - let plan = plan_abandon(&records, txid(1)).expect("abandonable"); - - assert_eq!(plan.txids, BTreeSet::from([txid(1), txid(2), txid(3)])); - assert_eq!( - plan.released_outpoints, - BTreeSet::from([outpoint(90, 0), outpoint(91, 0)]) - ); - } - - /// The case that would leave a hold attributed to a transaction that no - /// longer exists: another transaction of this wallet spends one of the - /// chain's inputs. The plan must refuse rather than release it. - #[test] - fn should_refuse_when_a_surviving_transaction_claims_one_of_the_inputs() { - let records = [ - record(1, &[outpoint(90, 0), outpoint(91, 0)]), - record(7, &[outpoint(91, 0)]), - ]; - - assert_eq!( - plan_abandon(&records, txid(1)), - Err(AbandonRefusal::InputClaimedBySurvivor { - outpoint: outpoint(91, 0), - claimant: txid(7), - }) - ); - } - - #[test] - fn should_refuse_when_a_settled_transaction_claims_one_of_the_inputs() { - let records = [ - record(1, &[outpoint(90, 0)]), - settled(7, &[outpoint(90, 0)]), - ]; - - assert!(matches!( - plan_abandon(&records, txid(1)), - Err(AbandonRefusal::InputClaimedBySurvivor { .. }) - )); - } - - /// A claimant deeper in the chain counts too: descendant 2 takes an - /// outside coin that transaction 7 also spends. - #[test] - fn should_refuse_when_a_survivor_claims_an_input_of_a_descendant() { - let records = [ - record(1, &[outpoint(90, 0)]), - record(2, &[outpoint(1, 1), outpoint(92, 0)]), - record(7, &[outpoint(92, 0)]), - ]; - - assert!(matches!( - plan_abandon(&records, txid(1)), - Err(AbandonRefusal::InputClaimedBySurvivor { claimant, .. }) if claimant == txid(7) - )); - } - - #[test] - fn should_refuse_a_settled_root() { - let records = [settled(1, &[outpoint(90, 0)])]; - - assert_eq!( - plan_abandon(&records, txid(1)), - Err(AbandonRefusal::SettledRoot) - ); - } - - #[test] - fn should_refuse_a_root_the_wallet_does_not_hold() { - let records = [record(2, &[outpoint(90, 0)])]; - - assert_eq!( - plan_abandon(&records, txid(1)), - Err(AbandonRefusal::UnknownRoot) - ); - } - - /// A settled transaction spending the chain's own output means the root - /// is in a block too — a child cannot be mined without its parent — so - /// whatever the probe said, the chain is not dead. key-wallet's walk - /// would simply stop at the settled child; the planner refuses instead, - /// because deleting the root's rows under a settled spender is exactly the - /// state a persister cannot reason about. - #[test] - fn should_refuse_when_a_settled_transaction_spends_the_chain() { - let records = [record(1, &[outpoint(90, 0)]), settled(2, &[outpoint(1, 1)])]; - - assert_eq!( - plan_abandon(&records, txid(1)), - Err(AbandonRefusal::SettledDescendant { txid: txid(2) }) - ); - } - - /// key-wallet records a transaction once per account it touches; the - /// duplicate must neither appear twice nor look like a rival claimant. - #[test] - fn should_treat_a_transaction_recorded_in_two_accounts_as_one() { - let records = [ - record(1, &[outpoint(90, 0)]), - record(1, &[outpoint(90, 0)]), - record(2, &[outpoint(1, 1)]), - record(2, &[outpoint(1, 1)]), - ]; - - let plan = plan_abandon(&records, txid(1)).expect("abandonable"); - - assert_eq!(plan.txids, BTreeSet::from([txid(1), txid(2)])); - assert_eq!(plan.released_outpoints, BTreeSet::from([outpoint(90, 0)])); - } - - /// The batch every persister applies: nothing held, so nothing is ever - /// attributed to `superseded_by`; no winner height, so no placeholder is - /// ever stamped. - #[test] - fn should_build_a_batch_that_holds_nothing() { - let records = [ - record(1, &[outpoint(90, 0)]), - record(2, &[outpoint(1, 1), outpoint(91, 0)]), - ]; - let plan = plan_abandon(&records, txid(1)).expect("abandonable"); - - let batch = plan.sweep_batch(); - - assert_eq!(batch.superseded_by, txid(1)); - assert_eq!(batch.winner_mined_height, None); - assert_eq!( - batch.txids.iter().copied().collect::>(), - BTreeSet::from([txid(1), txid(2)]) - ); - // Every input any removed transaction takes from outside the batch is - // released — the property the persisters' "held" path depends on. - let removed: BTreeSet = batch.txids.iter().copied().collect(); - let released: BTreeSet = batch.released_outpoints.iter().copied().collect(); - for record in &records { - for input in &record.inputs { - if !removed.contains(&input.txid) { - assert!(released.contains(input), "{input:?} would be held"); - } - } - } - } -} diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 28550bda981..e9ead82a936 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -7,7 +7,7 @@ //! landed. This module re-asks the network through //! [`AcceptanceProbe`](crate::broadcast_probe::AcceptanceProbe) and publishes //! the verdict; it **changes nothing** in the wallet. Cancelling a dead send is -//! a separate, user-driven decision (see `abandon_plan`). +//! a separate, user-driven decision. //! //! What is probed: every *root* of an unsettled chain the wallet itself //! signed — a transaction that spends this wallet's coins, is not in a block, diff --git a/packages/rs-platform-wallet/src/wallet/core/mod.rs b/packages/rs-platform-wallet/src/wallet/core/mod.rs index da7e1d86492..2b05316555b 100644 --- a/packages/rs-platform-wallet/src/wallet/core/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/core/mod.rs @@ -1,6 +1,3 @@ -// Used by the user-driven cancel of a proven-dead send, which lands separately. -#[allow(dead_code)] -pub(crate) mod abandon_plan; pub mod balance; pub mod balance_handler; mod broadcast; diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SweptTransactionPersistTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SweptTransactionPersistTests.swift index f66a4af6a0d..0656e587879 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SweptTransactionPersistTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SweptTransactionPersistTests.swift @@ -3414,87 +3414,4 @@ final class SweptTransactionPersistTests: XCTestCase { "no claim on a dead parent's output survives the batch, released or not" ) } - - /// The batch `abandon_plan` builds for a chain the network proved dead - /// (rust-dashcore#961's abandon, mirrored through the sweep channel): - /// every input the chain took from outside itself is released, - /// `superseded_by` is the chain's own root and there is no winner height. - /// Nothing may be held — a hold would be attributed to the root, a - /// transaction that no longer exists, and nothing could ever resolve it. - /// - /// Shape of ticket 32347: root R spends a real coin A and leaves change - /// R:0; child C spends that change plus another real coin B and leaves - /// change C:0. - func testAnAbandonedChainFreesItsOutsideInputsAndHoldsNothing() throws { - let (handler, container) = try makeHandler() - let rootTxid = Data(repeating: 0xE1, count: 32) - let childTxid = Data(repeating: 0xE2, count: 32) - - let context = ModelContext(container) - context.insert(PersistentWallet(walletId: walletId, network: .testnet)) - let funding = PersistentTransaction( - txid: fundingTxid, - transactionData: Data(repeating: 0x04, count: 10), - context: 2, - blockHeight: 100, - netAmount: 140_000 - ) - let root = loserRow(txid: rootTxid, spending: [(txid: fundingTxid, vout: 0)], netAmount: -40_000) - let child = loserRow( - txid: childTxid, - spending: [(txid: rootTxid, vout: 0), (txid: fundingTxid, vout: 1)], - netAmount: -30_000 - ) - context.insert(funding) - context.insert(root) - context.insert(child) - - // The real coins, claimed by the chain but never flipped spent — the - // chain never reached a block. - let coinA = PersistentTxo(transaction: funding, vout: 0, amount: 100_000, address: "yFundAddr", height: 100) - coinA.walletId = walletId - coinA.spendingTransaction = root - context.insert(coinA) - let coinB = PersistentTxo(transaction: funding, vout: 1, amount: 40_000, address: "yFundAddr", height: 100) - coinB.walletId = walletId - coinB.spendingTransaction = child - context.insert(coinB) - - // The phantom change: the root's, spent by the child, and the child's. - let rootChange = PersistentTxo(transaction: root, vout: 0, amount: 60_000, address: "yChangeAddr", height: 0) - rootChange.walletId = walletId - rootChange.spendingTransaction = child - context.insert(rootChange) - let childChange = PersistentTxo(transaction: child, vout: 0, amount: 70_000, address: "yChangeAddr", height: 0) - childChange.walletId = walletId - context.insert(childChange) - try context.save() - - XCTAssertTrue(sweep(handler, [Batch( - losers: [rootTxid, childTxid], - winner: rootTxid, - winnerMinedHeight: nil, - released: [(txid: fundingTxid, vout: 0), (txid: fundingTxid, vout: 1)] - )])) - - XCTAssertNil(transaction(container, txid: rootTxid), "the root row is gone") - XCTAssertNil(transaction(container, txid: childTxid), "the child row is gone") - XCTAssertNil(txo(container, txid: rootTxid, vout: 0), "the root's change is gone") - XCTAssertNil(txo(container, txid: childTxid, vout: 0), "the child's change is gone") - - for vout: UInt32 in [0, 1] { - let coin = try XCTUnwrap(txo(container, txid: fundingTxid, vout: vout), "real coin \(vout) stays") - XCTAssertFalse(coin.isSpent, "real coin \(vout) comes back spendable") - XCTAssertNil(coin.spendingTransaction, "real coin \(vout) keeps no link to the dead chain") - XCTAssertNil(coin.supersededByTxid, "real coin \(vout) is not held under the root") - } - - let verify = ModelContext(container) - let tombstones = try verify.fetch(FetchDescriptor()) - .filter { $0.isSweptTombstone || $0.spendingTxid == rootTxid } - XCTAssertTrue(tombstones.isEmpty, "an abandon must never leave a hold attributed to the root") - let heldByRoot = try verify.fetch(FetchDescriptor()) - .filter { $0.supersededByTxid == rootTxid } - XCTAssertTrue(heldByRoot.isEmpty, "no coin may be held under the abandoned root") - } } From 3e0e112152e106225d478d5c65276a823dd5b508 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 17:31:09 +0300 Subject: [PATCH 009/169] fix(platform-wallet): third review pass on the broadcast resolver - Publish verdicts per own send, the row the host can show: a root's verdict goes to the root when it is the wallet's send, and a dead root takes every own send built on it with it. A dead incoming parent is now visible on the user's send instead of on a txid the host has no row for. State is kept while a transaction stays unsettled in the wallet, so a dead child is not cleared for not being a root. - A root found in a block counts as settled when picking roots, so the sends built on it are probed next instead of never. - Unresolved never replaces a published Accepted/Mined (no flicker back to "unknown" after one probe that met only transport errors). - The per-wallet worker stops and drops its queue once probing is off, and leaves the running set under the queue lock (no lost wakeup). - The first height advance after launch counts as catch-up. - Removing a wallet forgets its sends and publishes them cleared at once; Swift's deleteWallet drops their verdicts. - collect_views keeps only unsettled records in its map. - DapiBroadcaster checks AlreadyExists directly. Tests: resolver 28 (+5 new: dead-chain propagation, dead incoming parent on the own send, accepted incoming parent publishes nothing, children of a mined root, no unresolved-after-accepted); platform-wallet 1205/1205, FFI 388/388, Swift 720/720. Ticket 32347. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/broadcaster.rs | 9 +- .../src/manager/wallet_lifecycle.rs | 5 + .../src/wallet/core/broadcast_resolver.rs | 520 +++++++++++++----- .../PlatformWalletManager.swift | 3 + 4 files changed, 395 insertions(+), 142 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcaster.rs b/packages/rs-platform-wallet/src/broadcaster.rs index 87e2baff14f..bd55cb4808f 100644 --- a/packages/rs-platform-wallet/src/broadcaster.rs +++ b/packages/rs-platform-wallet/src/broadcaster.rs @@ -21,10 +21,10 @@ use async_trait::async_trait; use dash_sdk::dapi_client::transport::TransportError; use dash_sdk::dapi_client::{DapiClientError, DapiRequestExecutor, IntoInner, RequestSettings}; use dash_sdk::dapi_grpc::core::v0::BroadcastTransactionRequest; +use dash_sdk::dapi_grpc::tonic::Code; use dash_spv::BroadcastResult; use dashcore::{consensus, Transaction, Txid}; -use crate::broadcast_probe::{classify_failed_submission, NodeVerdict}; use crate::error::PlatformWalletError; use crate::spv::SpvRuntime; @@ -131,7 +131,7 @@ impl TransactionBroadcaster for DapiBroadcaster { bypass_limits: false, }; - // A node that answers `AlreadyExists` has the transaction on chain, + // A node that answers `AlreadyExists` (-27) has the transaction in a block, // so that is success. Every other failure stays `MaybeSent`, including // Core's own rejections: one node's `missingorspent` is also what a // lagging node, a node that has not seen our unconfirmed parent, and @@ -148,10 +148,7 @@ impl TransactionBroadcaster for DapiBroadcaster { { Ok(_response) => Ok(transaction.txid()), Err(DapiClientError::Transport(TransportError::Grpc(status))) - if matches!( - classify_failed_submission(status.code(), status.message()), - NodeVerdict::Accepted | NodeVerdict::Mined - ) => + if status.code() == Code::AlreadyExists => { Ok(transaction.txid()) } diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index bc211ce76cc..eeceb2a3775 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -855,6 +855,11 @@ impl PlatformWalletManager

{ .await; } + // Broadcast-probe state for the wallet's sends goes too, and the host + // is told to drop the verdicts it shows for them: a same-id recreation + // must not inherit them. + self.broadcast_resolver.wallet_removed(wallet_id); + // Only now free the id in the inner manager — the LAST id-keyed step. // An identity registered on this wallet after the snapshot above (a // host racing an identity add against its own removal) is left as a diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index e9ead82a936..78740a8c9c6 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -9,29 +9,33 @@ //! the verdict; it **changes nothing** in the wallet. Cancelling a dead send is //! a separate, user-driven decision. //! -//! What is probed: every *root* of an unsettled chain the wallet itself -//! signed — a transaction that spends this wallet's coins, is not in a block, -//! not InstantSend-locked, and does not spend the output of another such -//! transaction. Probing a child is pointless and misleading: a node that has -//! not seen the parent answers `missingorspent` for a perfectly valid child. -//! A healthy send is IS-locked within seconds, so in practice only sends that -//! went quiet are ever probed. +//! What is probed: the *roots* of the chains the wallet signed into — its +//! unsettled sends (not in a block, not InstantSend-locked) plus the unsettled +//! ancestors they depend on, including someone else's incoming payment — +//! taking only transactions that spend no output of an unsettled transaction. +//! Probing a child is pointless and misleading: a node that has not seen the +//! parent answers `missingorspent` for a perfectly valid child. A healthy send +//! is IS-locked within seconds, so in practice only sends that went quiet are +//! ever probed. //! -//! A send that spends an unconfirmed coin of *someone else's* — an incoming -//! payment not yet in a block or IS-locked — is not a root either, for the same -//! reason: nodes that have not seen that parent refuse the send. +//! What is published: verdicts per **own send**, which is what the host +//! shows. A root's verdict goes to the root when it is the wallet's send; a +//! dead root also takes every own send built on it (spending a dead +//! transaction is dead too). A root found in a block that the wallet has not +//! seen yet counts as settled, so the sends built on it are probed next. +//! Only changes are published — Unresolved never replaces Accepted or Mined — +//! and a send that settles or leaves the wallet after a verdict is published +//! as cleared, as are all of a removed wallet's and, when probing is turned +//! off, all of them. //! //! When: once as soon as dash-spv reports an `Uncertain` broadcast, then on -//! every advance of the wallet's synced height (a filter batch: once per block -//! at the tip, larger steps while catching up) while the root stays -//! unsettled; after [`EVERY_BLOCK_WINDOW`] blocks, every [`SLOW_INTERVAL`] -//! blocks. At most one height-driven pass per wallet runs at a time. Only -//! `Dead` or `Mined` ends the probing — acceptance by one node's mempool is not -//! settlement, so an `Accepted` root keeps being asked until it settles. A -//! verdict is published only when it differs from the last one published for -//! that root, and a root that settles or leaves the wallet after a verdict was -//! published is published as cleared. Probes run only while the SPV client -//! delivers blocks, i.e. while the app is in the foreground. +//! each advance of the wallet's synced height that follows the tip (steps of +//! more than [`CATCH_UP_STEP`] blocks, and the first step after launch, are +//! catch-up and skipped), every block for [`EVERY_BLOCK_WINDOW`] blocks and +//! every [`SLOW_INTERVAL`] blocks after. One worker per wallet drains a +//! queue of merged requests. `Dead` and `Mined` end the probing of a root; +//! `Accepted` does not — a mempool is not settlement. Probes run only while +//! the SPV client delivers blocks, i.e. while the app is in the foreground. use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::future::Future; @@ -70,60 +74,113 @@ pub(crate) struct OutgoingView { pub transaction: Transaction, } -/// The roots of this wallet's unsettled chains, one entry per txid. +/// This wallet's unsettled transactions as chains. /// -/// The chains that matter are the ones the wallet signed into: every unsettled -/// transaction that spends its own coins, plus every unsettled ancestor such a -/// transaction depends on — including someone else's incoming payment, whose -/// fate decides the send built on it. A root is a member of that set that -/// spends no output of any unsettled transaction, because a node that has not -/// seen a parent refuses a perfectly valid child. -pub(crate) fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { - // One entry per transaction. A transaction recorded in several accounts is - // one transaction: settled if any account holds it settled, the wallet's - // own send if any account records it spending the wallet's coins (a - // transfer between accounts is "incoming" in the receiving one). - let mut by_txid: BTreeMap = BTreeMap::new(); - for view in views { - by_txid - .entry(view.txid) - .and_modify(|(_, settled, own)| { - *settled |= view.settled; - *own |= view.spends_own_coins; +/// Built from the account views, merged per transaction: settled if any +/// account holds it settled (or the caller knows it is mined), the wallet's own +/// send if any account records it spending the wallet's coins — a transfer +/// between accounts is "incoming" in the receiving one. +pub(crate) struct ChainGraph<'a> { + by_txid: BTreeMap, + unsettled: BTreeSet, +} + +impl<'a> ChainGraph<'a> { + /// `mined` are transactions a probe found in a block that this wallet has + /// not seen yet: settled for the purpose of picking roots, so the sends + /// built on them become roots in turn. + pub(crate) fn new(views: &'a [OutgoingView], mined: &HashSet) -> Self { + let mut merged: BTreeMap = BTreeMap::new(); + for view in views { + merged + .entry(view.txid) + .and_modify(|(_, settled, own)| { + *settled |= view.settled; + *own |= view.spends_own_coins; + }) + .or_insert((view, view.settled, view.spends_own_coins)); + } + let unsettled = merged + .iter() + .filter(|(txid, (_, settled, _))| !*settled && !mined.contains(*txid)) + .map(|(txid, _)| *txid) + .collect(); + let by_txid = merged + .into_iter() + .map(|(txid, (view, _, own))| (txid, (view, own))) + .collect(); + Self { by_txid, unsettled } + } + + fn is_own(&self, txid: &Txid) -> bool { + self.by_txid.get(txid).is_some_and(|(_, own)| *own) + } + + /// The wallet's unsettled sends. + pub(crate) fn own_unsettled(&self) -> BTreeSet { + self.unsettled + .iter() + .filter(|txid| self.is_own(txid)) + .copied() + .collect() + } + + /// The roots to probe: members of the chains the wallet signed into — its + /// unsettled sends plus every unsettled ancestor they depend on, including + /// someone else's incoming payment — that spend no output of an unsettled + /// transaction. A node that has not seen a parent refuses a valid child. + pub(crate) fn roots(&self) -> Vec<&'a OutgoingView> { + let mut relevant = self.own_unsettled(); + let mut frontier: Vec = relevant.iter().copied().collect(); + while let Some(txid) = frontier.pop() { + for input in &self.by_txid[&txid].0.inputs { + if self.unsettled.contains(&input.txid) && relevant.insert(input.txid) { + frontier.push(input.txid); + } + } + } + relevant + .iter() + .map(|txid| self.by_txid[txid].0) + .filter(|view| { + !view + .inputs + .iter() + .any(|input| self.unsettled.contains(&input.txid)) }) - .or_insert((view, view.settled, view.spends_own_coins)); - } - let unsettled: BTreeSet = by_txid - .iter() - .filter(|(_, (_, settled, _))| !*settled) - .map(|(txid, _)| *txid) - .collect(); - - // The wallet's unsettled sends and, transitively, their unsettled parents. - let mut relevant: BTreeSet = unsettled - .iter() - .filter(|txid| by_txid[*txid].2) - .copied() - .collect(); - let mut frontier: Vec = relevant.iter().copied().collect(); - while let Some(txid) = frontier.pop() { - for input in &by_txid[&txid].0.inputs { - if unsettled.contains(&input.txid) && relevant.insert(input.txid) { - frontier.push(input.txid); + .collect() + } + + /// The wallet's own sends in `root`'s chain: `root` itself if it is one, + /// and every unsettled send built on it, transitively. A dead root takes + /// all of them with it. + pub(crate) fn own_in_chain(&self, root: &Txid) -> BTreeSet { + let mut chain = BTreeSet::from([*root]); + loop { + let before = chain.len(); + for txid in &self.unsettled { + if !chain.contains(txid) + && self.by_txid[txid] + .0 + .inputs + .iter() + .any(|input| chain.contains(&input.txid)) + { + chain.insert(*txid); + } + } + if chain.len() == before { + break; } } + chain.into_iter().filter(|txid| self.is_own(txid)).collect() } +} - relevant - .iter() - .map(|txid| by_txid[txid].0) - .filter(|view| { - !view - .inputs - .iter() - .any(|input| unsettled.contains(&input.txid)) - }) - .collect() +/// The roots of `views` with nothing known to be mined — see [`ChainGraph::roots`]. +#[cfg(test)] +fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { + ChainGraph::new(views, &HashSet::new()).roots() } /// When a root is next due for a probe. @@ -155,32 +212,39 @@ impl ProbeSchedule { } } -/// Per-root bookkeeping shared by every pass. +/// A root's final verdict: nothing is left to ask. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Final { + Dead, + Mined, +} + +/// Bookkeeping shared by every pass. Schedules and final verdicts are per +/// probed root; published verdicts are per own send, which is what the host +/// shows. Everything for a transaction is kept while it stays unsettled in the +/// wallet, and dropped once it settles or leaves. #[derive(Debug, Default)] pub(crate) struct ResolverState { schedules: HashMap<(WalletId, Txid), ProbeSchedule>, - /// Roots with a final verdict (dead or mined): never probed again while - /// they stay roots. - finished: HashSet<(WalletId, Txid)>, - /// The last verdict published per root, so only changes are published. + finished: HashMap<(WalletId, Txid), Final>, published: HashMap<(WalletId, Txid), ProbeVerdict>, } -/// What a pass has to tell the host. +/// What a pass has to tell the host, per own send. #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum ResolverEvent { - /// A root's verdict changed. + /// A send's verdict changed. Verdict(Txid, ProbeVerdict), - /// A root that had a published verdict settled or left the wallet. + /// A send that had a published verdict settled or left the wallet. Cleared(Txid), } impl ResolverState { - /// Drop everything kept for `wallet_id`; returns the roots the host had a + /// Drop everything kept for `wallet_id`; returns the sends the host had a /// verdict for, which it must now be told are cleared. fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { self.schedules.retain(|(wallet, _), _| wallet != wallet_id); - self.finished.retain(|(wallet, _)| wallet != wallet_id); + self.finished.retain(|(wallet, _), _| wallet != wallet_id); let mut cleared: Vec = self .published .keys() @@ -192,7 +256,7 @@ impl ResolverState { cleared } - /// Drop everything; returns every root the host had a verdict for. + /// Drop everything; returns every send the host had a verdict for. fn forget_all(&mut self) -> Vec<(WalletId, Txid)> { self.schedules.clear(); self.finished.clear(); @@ -201,6 +265,29 @@ impl ResolverState { cleared.sort(); cleared } + + /// Record `verdict` for `send`; returns whether the host must hear it. + /// Unresolved never replaces an Accepted or Mined verdict — one probe that + /// only met transport errors says nothing new — and Unresolved reasons + /// vary between probes, so they all count as one. + fn publish(&mut self, key: (WalletId, Txid), verdict: &ProbeVerdict) -> bool { + let changed = match self.published.get(&key) { + None => true, + Some(ProbeVerdict::Unresolved { .. }) => { + !matches!(verdict, ProbeVerdict::Unresolved { .. }) + } + Some(ProbeVerdict::Accepted | ProbeVerdict::Mined) + if matches!(verdict, ProbeVerdict::Unresolved { .. }) => + { + false + } + Some(previous) => previous != verdict, + }; + if changed { + self.published.insert(key, verdict.clone()); + } + changed + } } /// How far a wallet's synced height may advance in one step and still count @@ -208,13 +295,21 @@ impl ResolverState { /// scan itself settles most roots, so a height-driven pass waits for it. pub(crate) const CATCH_UP_STEP: u32 = 3; +/// The first advance after launch has no previous height to compare with, and +/// it is usually the stored tip from before the scan: treat it as catch-up +/// too. Probing starts from the next step. pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { - previous.is_some_and(|previous| height.saturating_sub(previous) > CATCH_UP_STEP) + previous.is_none_or(|previous| height.saturating_sub(previous) > CATCH_UP_STEP) } /// One pass over one wallet at `height`: probe every due root and return what /// to publish. `forced` roots are due regardless of their schedule (the SPV /// broadcaster has just reported them `Uncertain`). +/// +/// A root's verdict is published for the wallet's own sends: the root itself +/// when it is one, and — when the root is dead — every own send built on it, +/// since a transaction spending a dead one is dead too. A root found mined +/// counts as settled when picking roots, so its children are probed next. pub(crate) async fn resolve_pass( state: &Mutex, probe: &dyn AcceptanceProbe, @@ -223,37 +318,52 @@ pub(crate) async fn resolve_pass( views: &[OutgoingView], forced: &HashSet, ) -> Vec { - let roots = ambiguous_roots(views); - let root_ids: HashSet = roots.iter().map(|view| view.txid).collect(); + // What the wallet itself still holds unsettled — what state is kept for. + let wallet_view = ChainGraph::new(views, &HashSet::new()); + let present: HashSet = wallet_view.unsettled.iter().copied().collect(); + let own_present = wallet_view.own_unsettled(); - // Decide what is due under the lock; probe without it. let mut events = Vec::new(); - let due: Vec<(Txid, Transaction)> = { + let graph_mined: HashSet = { let mut guard = state.lock().expect("resolver state poisoned"); let ResolverState { schedules, finished, published, } = &mut *guard; - // Forget this wallet's roots that settled or left the wallet; tell the - // host about the ones it had a verdict for. let is_gone = - |wallet: &WalletId, txid: &Txid| *wallet == wallet_id && !root_ids.contains(txid); + |wallet: &WalletId, txid: &Txid| *wallet == wallet_id && !present.contains(txid); schedules.retain(|(wallet, txid), _| !is_gone(wallet, txid)); - finished.retain(|(wallet, txid)| !is_gone(wallet, txid)); + finished.retain(|(wallet, txid), _| !is_gone(wallet, txid)); let mut cleared: Vec = published .keys() - .filter(|(wallet, txid)| is_gone(wallet, txid)) + .filter(|(wallet, txid)| *wallet == wallet_id && !own_present.contains(txid)) .map(|(_, txid)| *txid) .collect(); cleared.sort(); - published.retain(|(wallet, txid), _| !is_gone(wallet, txid)); + published.retain(|(wallet, txid), _| *wallet != wallet_id || own_present.contains(txid)); events.extend(cleared.into_iter().map(ResolverEvent::Cleared)); - let mut due = Vec::new(); - for view in &roots { + let mined: HashSet = finished + .iter() + .filter(|((wallet, _), result)| *wallet == wallet_id && **result == Final::Mined) + .map(|((_, txid), _)| *txid) + .collect(); + mined + }; + + let graph = ChainGraph::new(views, &graph_mined); + let due: Vec<(Txid, Transaction)> = { + let mut guard = state.lock().expect("resolver state poisoned"); + let ResolverState { + schedules, + finished, + .. + } = &mut *guard; + let mut due_now = Vec::new(); + for view in graph.roots() { let key = (wallet_id, view.txid); - if finished.contains(&key) { + if finished.contains_key(&key) { continue; } let schedule = schedules @@ -261,36 +371,41 @@ pub(crate) async fn resolve_pass( .or_insert_with(|| ProbeSchedule::new(height)); if forced.contains(&view.txid) || schedule.is_due(height) { schedule.probed_at(height); - due.push((view.txid, view.transaction.clone())); + due_now.push((view.txid, view.transaction.clone())); } } - due + due_now }; - for (txid, transaction) in due { + for (root, transaction) in due { let verdict = probe.probe(&transaction).await; - let key = (wallet_id, txid); + let key = (wallet_id, root); let mut guard = state.lock().expect("resolver state poisoned"); // A pass that ran meanwhile may have found this root settled or gone - // and cleared it; a verdict published now would resurrect it. + // and dropped it; a verdict published now would resurrect it. if !guard.schedules.contains_key(&key) { continue; } - if matches!(verdict, ProbeVerdict::Dead { .. } | ProbeVerdict::Mined) { - guard.finished.insert(key); - } - // Publish changes only. Unresolved reasons vary between probes - // (different nodes, different errors), so all Unresolved count as one. - let changed = match guard.published.get(&key) { - None => true, - Some(ProbeVerdict::Unresolved { .. }) => { - !matches!(verdict, ProbeVerdict::Unresolved { .. }) + match verdict { + ProbeVerdict::Dead { .. } => { + guard.finished.insert(key, Final::Dead); + } + ProbeVerdict::Mined => { + guard.finished.insert(key, Final::Mined); } - Some(previous) => *previous != verdict, + _ => {} + } + let sends: BTreeSet = if matches!(verdict, ProbeVerdict::Dead { .. }) { + graph.own_in_chain(&root) + } else if graph.is_own(&root) { + BTreeSet::from([root]) + } else { + BTreeSet::new() }; - if changed { - guard.published.insert(key, verdict.clone()); - events.push(ResolverEvent::Verdict(txid, verdict)); + for send in sends { + if guard.publish((wallet_id, send), &verdict) { + events.push(ResolverEvent::Verdict(send, verdict.clone())); + } } } events @@ -303,26 +418,30 @@ pub(crate) async fn resolve_pass( /// on a healthy wallet there are none or a handful. pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { let accounts = info.core_wallet.accounts.all_accounts(); - let mut merged: BTreeMap = BTreeMap::new(); + // Settled txids in a set; only unsettled records go into the map, so a + // wallet with nothing unconfirmed costs one walk and no ordering work. + let mut settled: HashSet = HashSet::new(); + let mut unsettled: HashMap = HashMap::new(); for account in accounts.iter() { for record in account.transactions().values() { - let settled = record.is_confirmed() + if record.is_confirmed() || matches!(record.context, TransactionContext::InstantSend(_)) - || account.transaction_is_finalized(&record.txid); + || account.transaction_is_finalized(&record.txid) + { + settled.insert(record.txid); + continue; + } let own = !record.input_details.is_empty(); - merged + unsettled .entry(record.txid) - .and_modify(|(was_settled, was_own, _)| { - *was_settled |= settled; - *was_own |= own; - }) - .or_insert((settled, own, &record.transaction)); + .and_modify(|(was_own, _)| *was_own |= own) + .or_insert((own, &record.transaction)); } } - merged + unsettled .into_iter() - .filter(|(_, (settled, _, _))| !*settled) - .map(|(txid, (_, own, transaction))| OutgoingView { + .filter(|(txid, _)| !settled.contains(txid)) + .map(|(txid, (own, transaction))| OutgoingView { txid, inputs: transaction .input @@ -411,7 +530,7 @@ struct PendingPass { /// probe sends the signed transaction to evonodes over DAPI, which is a /// product decision, not something a wallet should start doing on upgrade. pub(crate) struct BroadcastResolver { - enabled: AtomicBool, + enabled: Arc, probe: Arc, wallet_manager: Arc>>, state: Arc>, @@ -429,7 +548,7 @@ impl BroadcastResolver { wallet_manager: Arc>>, ) -> Self { Self { - enabled: AtomicBool::new(false), + enabled: Arc::new(AtomicBool::new(false)), probe, wallet_manager, state: Arc::new(Mutex::new(ResolverState::default())), @@ -468,6 +587,30 @@ impl BroadcastResolver { self.enabled.load(Ordering::SeqCst) } + /// Forget a removed wallet: its queued pass, its last height and every + /// verdict, each of which the host is told is cleared. + pub(crate) fn wallet_removed(&self, wallet_id: &WalletId) { + self.passes + .lock() + .expect("pass queue poisoned") + .pending + .remove(wallet_id); + self.heights + .lock() + .expect("heights mutex poisoned") + .remove(wallet_id); + let cleared = self + .state + .lock() + .expect("resolver state poisoned") + .forget_wallet(wallet_id); + if let Some(events) = self.events.lock().expect("events mutex poisoned").upgrade() { + for txid in &cleared { + events.on_outgoing_transaction_cleared(wallet_id, txid); + } + } + } + /// Stop admitting passes and stop the running ones. Idempotent. pub(crate) async fn quiesce_within(&self, budget: Duration) -> bool { self.tasks.quiesce_within(budget).await @@ -489,6 +632,7 @@ impl BroadcastResolver { let state = Arc::clone(&self.state); let heights = Arc::clone(&self.heights); let passes = Arc::clone(&self.passes); + let enabled = Arc::clone(&self.enabled); let events = self.events.lock().expect("events mutex poisoned").clone(); self.tasks.spawn(async move { let _running = Running { @@ -496,12 +640,25 @@ impl BroadcastResolver { wallet_id, }; loop { - let Some(PendingPass { height, forced }) = passes - .lock() - .expect("pass queue poisoned") - .pending - .remove(&wallet_id) - else { + // Take the next request, or stop — under the same lock a + // request_pass would take, so a request can never land between + // "queue empty" and "no longer running" and be left unserved. + // Probing turned off meanwhile: drop what is queued, do not + // probe again (set_enabled already cleared the host's state). + let next = { + let mut queue = passes.lock().expect("pass queue poisoned"); + let next = if enabled.load(Ordering::SeqCst) { + queue.pending.remove(&wallet_id) + } else { + queue.pending.remove(&wallet_id); + None + }; + if next.is_none() { + queue.running.remove(&wallet_id); + } + next + }; + let Some(PendingPass { height, forced }) = next else { break; }; // Read under the lock, probe without it: a probe is network I/O. @@ -581,8 +738,8 @@ fn publish( } } -/// Marks a wallet's worker finished however it ends (including an abort), so -/// the next request starts a new one. +/// Marks a wallet's worker finished if it ends without reaching its own exit +/// (an abort at shutdown), so a later request is not refused forever. struct Running { passes: Arc>, wallet_id: WalletId, @@ -816,13 +973,13 @@ mod tests { #[tokio::test] async fn should_probe_only_roots_and_report_each_verdict() { let state = Mutex::new(ResolverState::default()); - let probe = ScriptedProbe::new(&[(txid(1), dead())]); + let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; let events = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; assert_eq!(probe.probed(), vec![txid(1)]); - assert_eq!(events, vec![ResolverEvent::Verdict(txid(1), dead())]); + assert_eq!(events, vec![ResolverEvent::Verdict(txid(1), unresolved())]); } #[tokio::test] @@ -987,7 +1144,10 @@ mod tests { #[test] fn should_treat_only_large_height_steps_as_catch_up() { - assert!(!is_catch_up_step(None, 1_000)); + assert!( + is_catch_up_step(None, 1_000), + "the first step after launch is catch-up" + ); assert!(!is_catch_up_step(Some(999), 1_000)); assert!(!is_catch_up_step(Some(1_000 - CATCH_UP_STEP), 1_000)); assert!(is_catch_up_step(Some(1_000 - CATCH_UP_STEP - 1), 1_000)); @@ -1090,4 +1250,92 @@ mod tests { assert!(events.is_empty()); assert!(state.lock().expect("state").published.is_empty()); } + + fn incoming(n: u8, inputs: &[OutPoint]) -> OutgoingView { + OutgoingView { + spends_own_coins: false, + ..send(n, inputs) + } + } + + /// Spending a dead transaction is dead too: the whole chain of own sends + /// built on a dead root is published dead, not just the root. + #[tokio::test] + async fn should_publish_a_dead_root_for_every_own_send_built_on_it() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), dead())]); + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; + + let events = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + + assert_eq!( + events, + vec![ + ResolverEvent::Verdict(txid(1), dead()), + ResolverEvent::Verdict(txid(2), dead()), + ] + ); + let again = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; + assert!( + again.is_empty(), + "a dead child is not cleared for not being a root" + ); + } + + /// Someone else's incoming payment is probed, but the host hears only + /// about the wallet's own send — the only row it can attach a verdict to. + #[tokio::test] + async fn should_publish_a_dead_incoming_parent_on_the_own_send_only() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(5), dead())]); + let views = [incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])]; + + let events = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + + assert_eq!(probe.probed(), vec![txid(5)]); + assert_eq!(events, vec![ResolverEvent::Verdict(txid(6), dead())]); + } + + #[tokio::test] + async fn should_publish_nothing_while_an_incoming_parent_is_only_accepted() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(5), ProbeVerdict::Accepted)]); + let views = [incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])]; + + let events = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + + assert!(events.is_empty()); + } + + /// A root found in a block that the wallet has not caught up with is + /// settled for picking roots, so the send built on it is probed next. + #[tokio::test] + async fn should_probe_the_children_of_a_root_found_in_a_block() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined), (txid(2), dead())]); + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; + + let first = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + let second = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; + + assert_eq!( + first, + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Mined)] + ); + assert_eq!(second, vec![ResolverEvent::Verdict(txid(2), dead())]); + assert_eq!(probe.probed(), vec![txid(1), txid(2)]); + } + + /// One probe that met only transport errors says nothing new about a send + /// already seen accepted; the UI must not flicker back to "unknown". + #[test] + fn should_not_let_unresolved_replace_an_accepted_verdict() { + let mut state = ResolverState::default(); + let key = (wallet(), txid(1)); + + assert!(state.publish(key, &ProbeVerdict::Accepted)); + assert!(!state.publish(key, &unresolved())); + assert!(!state.publish(key, &ProbeVerdict::Accepted)); + assert!(state.publish(key, &dead())); + } } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift index c84a22b30bb..e5611899773 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift @@ -2657,6 +2657,9 @@ public class PlatformWalletManager: ObservableObject { // with the same deterministic id doesn't inherit a stale banner (the // poller would also prune it, but not until the next tick). dashPayUnlockStatus.removeValue(forKey: walletId) + // Same for broadcast-probe verdicts: Rust forgets the wallet's sends + // on removal, and a restored wallet (same id) must start clean. + outgoingTransactionVerdicts = outgoingTransactionVerdicts.filter { $0.key.walletId != walletId } // A store reconcile in flight for any wallet stops between pages: // its next step would read rows `deleteWalletData` is about to // remove. Coarse on purpose — the cadence re-runs the others. From e0650a1b0f3e100aad3626b0be2027c0d58d0fc4 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 19:07:26 +0300 Subject: [PATCH 010/169] fix(platform-wallet): fourth review pass on the broadcast resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Verdicts reach the host under the state lock and only while still recorded, so a clear (probing off, wallet removed) can never be followed by a stale verdict; set_enabled(false) and wallet_removed emit their clears under the same lock. - wallet_removed runs after the wallet has left the wallet manager, so no pass can read it and rebuild its state. - The worker's Running guard is disarmed on the normal exit (it already left the running set under the queue lock), so it can no longer erase a newer worker's marker and let two passes run for one wallet. - resolve_pass checks the switch before every probe: nothing more is sent once probing is off. - A wallet whose last pass found nothing unconfirmed is idle: height advances skip it — no scan of every record per block — until a new transaction or an Uncertain result wakes it. - Uncertain probes the root of the reported transaction's chain (the customer's shape is a child) and reaches wallets seen in any event, not only those with a reported height. - The multi-account merge rule lives in one place (merge_records); OutgoingView derives inputs from its transaction and drops `settled`. - Accepted and Mined count as the same for publishing (one host code); Swift resets lastOutgoingTransactionProbe on clear and wallet removal. - NodeVerdict is crate-private and Refused is folded into Unknown. - Docs: schedule (24 blocks, then every 10), per-send clears, Mined, the DEAD_REASONS guards, and the Dead verdict's getTransaction confirmation. Tests: resolver 33, probe 12; platform-wallet 1208/1208, FFI 388/388, Swift 719/720 (the known-flaky testCaptureHistoricalV2Fixture). Ticket 32347. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet-ffi/src/manager.rs | 9 +- .../rs-platform-wallet/src/broadcast_probe.rs | 51 +- packages/rs-platform-wallet/src/events.rs | 13 +- .../src/manager/accessors.rs | 12 +- .../src/manager/wallet_lifecycle.rs | 11 +- .../src/wallet/core/broadcast_resolver.rs | 1046 ++++++++++------- .../PlatformWalletManager.swift | 3 + .../PlatformWalletManagerBroadcastProbe.swift | 6 +- 8 files changed, 673 insertions(+), 478 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 5b0b8f04ba5..1b9c2f2dfb0 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -2146,11 +2146,12 @@ mod remove_wallet_lifecycle_tests { /// Turn automatic probing of unconfirmed sends on or off (off by default). /// -/// While on, every unconfirmed send whose broadcast outcome went quiet is -/// resubmitted to evonodes over DAPI — right after SPV reports it uncertain, -/// then once per block — and each verdict reaches the host through +/// While on, the roots of every unconfirmed chain are resubmitted to evonodes +/// over DAPI — right after SPV reports a send uncertain, then every block for +/// 24 blocks and every 10 after — and each change of a send's verdict, and +/// each clear, reaches the host through /// `EventHandlerCallbacksExtension::on_outgoing_transaction_probed_fn`. -/// Nothing in the wallet changes. +/// Nothing in the wallet changes; turning it off clears every verdict. /// /// # Safety /// `handle` must be a live manager handle. diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index 91f656b1b3e..ac0b871fbb4 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -57,25 +57,25 @@ use dashcore::{Transaction, Txid}; /// - `tx-txlock-conflict`: an input is already spent by an InstantSend-locked /// transaction, which is final. /// -/// `-25` has two false positives that the *caller* must rule out — a node -/// that has not yet seen one of our own unconfirmed parents, and a node -/// lagging behind the tip. See [`AcceptanceProbe`] for how the probe guards -/// against the second, and the wallet-side resolver for the first. +/// A node's `-25` alone is never taken as proof. It is also what a node that +/// has not yet seen one of the transaction's unconfirmed parents answers +/// (the wallet-side resolver only probes chain roots for that reason), what a +/// node lagging behind the tip answers (hence [`DEAD_QUORUM`]: two distinct +/// nodes), and what a mined transaction whose outputs are all spent gets +/// (hence the txid lookup in [`confirm_dead`]). +/// +/// `txn-mempool-conflict` is deliberately absent: it means another, +/// unconfirmed and not IS-locked, transaction spends the same input, and +/// either one may still be mined — no verdict. const DEAD_REASONS: &[&str] = &[ "bad-txns-inputs-missingorspent", "missing inputs", "tx-txlock-conflict", ]; -/// Core reject reasons that say this node refused the transaction without -/// proving it can never land: `txn-mempool-conflict` means another, -/// unconfirmed and not IS-locked, transaction spends the same input, and -/// either one may still be mined. -const REFUSED_REASONS: &[&str] = &["txn-mempool-conflict"]; - /// What a single Core node said when handed a signed transaction. #[derive(Debug, Clone, PartialEq, Eq)] -pub enum NodeVerdict { +pub(crate) enum NodeVerdict { /// The node holds the transaction in its mempool — it accepted it now or /// already had it. Accepted, @@ -83,9 +83,8 @@ pub enum NodeVerdict { Mined, /// The node proved the transaction can never be mined as it stands. Dead { reason: String }, - /// The node refused the transaction without proving it dead. - Refused { reason: String }, - /// No verdict: transport failure, timeout, or an unrecognised rejection. + /// No verdict: transport failure, timeout, or a rejection that proves + /// nothing (such as `txn-mempool-conflict`). Unknown { reason: String }, } @@ -100,14 +99,6 @@ pub(crate) fn classify_failed_submission(code: Code, message: &str) -> NodeVerdi reason: message.to_string(), }; } - if REFUSED_REASONS - .iter() - .any(|reason| lowered.contains(reason)) - { - return NodeVerdict::Refused { - reason: message.to_string(), - }; - } NodeVerdict::Unknown { reason: format!("{code:?}: {message}"), } @@ -122,9 +113,10 @@ pub enum ProbeVerdict { /// A node has the transaction in a block. Final — there is nothing left to /// ask, even if this wallet has not seen that block yet. Mined, - /// Two different nodes refused it for missing or spent inputs, and two - /// different nodes do not know its txid: it is not in a block and can - /// never be mined. + /// Two different nodes refused it for a reason in [`DEAD_REASONS`] + /// (missing or spent inputs, or a conflict with an InstantSend-locked + /// spend), and two different nodes answered `getTransaction` with + /// NotFound: it is not in a block and can never be mined. Dead { reason: String }, /// Not enough evidence either way; the transaction stays ambiguous. Unresolved { reason: String }, @@ -216,7 +208,7 @@ pub(crate) async fn probe_with( .await; } } - NodeVerdict::Refused { reason } | NodeVerdict::Unknown { reason } => { + NodeVerdict::Unknown { reason } => { last_other = reason; } } @@ -502,8 +494,9 @@ mod tests { ( Code::FailedPrecondition, "Transaction is rejected: txn-mempool-conflict", - NodeVerdict::Refused { - reason: "Transaction is rejected: txn-mempool-conflict".to_string(), + NodeVerdict::Unknown { + reason: "FailedPrecondition: Transaction is rejected: txn-mempool-conflict" + .to_string(), }, ), ]; @@ -596,7 +589,7 @@ mod tests { let nodes = ScriptedNodes::new(vec![ ( Some("a"), - NodeVerdict::Refused { + NodeVerdict::Unknown { reason: "txn-mempool-conflict".to_string(), }, ), diff --git a/packages/rs-platform-wallet/src/events.rs b/packages/rs-platform-wallet/src/events.rs index 0ade95f93f6..c5d1708bd8a 100644 --- a/packages/rs-platform-wallet/src/events.rs +++ b/packages/rs-platform-wallet/src/events.rs @@ -48,12 +48,13 @@ pub trait PlatformEventHandler: EventHandler { /// [`DpnsSyncManager`]: crate::manager::dpns_sync::DpnsSyncManager fn on_dpns_marketplace_sync_completed(&self, _summary: &DpnsSyncPassSummary) {} - /// Fired when the broadcast resolver has asked the network about an - /// unconfirmed send of this wallet whose broadcast outcome was unknown. - /// `Accepted` means a node holds the transaction; `Dead` means two - /// distinct nodes proved it can never be mined; `Unresolved` means no - /// verdict yet — the resolver asks again on a later block. Nothing in the - /// wallet changes either way. + /// Fired when the verdict on an unconfirmed send of this wallet changes — + /// the network was asked about the root of its chain. `Accepted` means a + /// node holds the transaction in its mempool; `Mined` that a node has it + /// in a block; `Dead` that the transaction, or a parent it spends, can + /// never be mined (two distinct nodes refused it and two do not know it); + /// `Unresolved` that there is no verdict yet — the resolver asks again on + /// a later block. Nothing in the wallet changes either way. /// /// Default impl is a no-op so existing handlers don't have to care. fn on_outgoing_transaction_probed( diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index 7879accdbff..4655df5babf 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -524,11 +524,13 @@ impl PlatformWalletManager

{ /// Turn automatic probing of unconfirmed sends on or off. /// - /// When on, every unconfirmed send of a registered wallet whose broadcast - /// outcome went quiet is resubmitted to evonodes over DAPI — once right - /// after dash-spv reports it `Uncertain`, then once per block — and the - /// verdict is published as `on_outgoing_transaction_probed`. Nothing in - /// the wallet changes. Off by default. + /// When on, the roots of a registered wallet's unconfirmed chains are + /// resubmitted to evonodes over DAPI — right after dash-spv reports a send + /// `Uncertain`, then every block for 24 blocks and every 10 after — and + /// each change of a send's verdict is published as + /// `on_outgoing_transaction_probed`, each send that no longer needs one + /// as `on_outgoing_transaction_cleared`. Nothing in the wallet changes. + /// Off by default; turning it off clears every published verdict. pub fn set_broadcast_probe_enabled(&self, enabled: bool) { self.broadcast_resolver.set_enabled(enabled); } diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index eeceb2a3775..4bea185d2ed 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -855,11 +855,6 @@ impl PlatformWalletManager

{ .await; } - // Broadcast-probe state for the wallet's sends goes too, and the host - // is told to drop the verdicts it shows for them: a same-id recreation - // must not inherit them. - self.broadcast_resolver.wallet_removed(wallet_id); - // Only now free the id in the inner manager — the LAST id-keyed step. // An identity registered on this wallet after the snapshot above (a // host racing an identity add against its own removal) is left as a @@ -891,6 +886,12 @@ impl PlatformWalletManager

{ } } + // Broadcast-probe state for the wallet's sends goes too, and the host + // is told to drop the verdicts it shows for them. After the inner + // removal, so no probe pass can read the wallet and rebuild that state; + // before the midpoint, so a same-id recreation's state is never touched. + self.broadcast_resolver.wallet_removed(wallet_id); + // Test-only rendezvous: the window a concurrent same-id registration can // publish a new generation into. Sits AFTER every id-keyed unregister, // so state a midpoint recreation installs is never torn down by this diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 78740a8c9c6..3ff2be65326 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -23,21 +23,24 @@ //! dead root also takes every own send built on it (spending a dead //! transaction is dead too). A root found in a block that the wallet has not //! seen yet counts as settled, so the sends built on it are probed next. -//! Only changes are published — Unresolved never replaces Accepted or Mined — -//! and a send that settles or leaves the wallet after a verdict is published -//! as cleared, as are all of a removed wallet's and, when probing is turned -//! off, all of them. +//! Only changes are published — Unresolved never replaces Accepted or Mined, +//! and Accepted and Mined count as the same — and a send that settles or +//! leaves the wallet after a verdict is published as cleared, as are all of a +//! removed wallet's sends and, when probing is turned off, all of them. //! -//! When: once as soon as dash-spv reports an `Uncertain` broadcast, then on -//! each advance of the wallet's synced height that follows the tip (steps of -//! more than [`CATCH_UP_STEP`] blocks, and the first step after launch, are -//! catch-up and skipped), every block for [`EVERY_BLOCK_WINDOW`] blocks and -//! every [`SLOW_INTERVAL`] blocks after. One worker per wallet drains a -//! queue of merged requests. `Dead` and `Mined` end the probing of a root; -//! `Accepted` does not — a mempool is not settlement. Probes run only while -//! the SPV client delivers blocks, i.e. while the app is in the foreground. - -use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +//! When: once as soon as dash-spv reports an `Uncertain` broadcast (the root +//! of that transaction's chain), then on each advance of the wallet's synced +//! height that follows the tip — steps of more than [`CATCH_UP_STEP`] blocks, +//! and the first step after launch, are catch-up and skipped — every block +//! for [`EVERY_BLOCK_WINDOW`] blocks and every [`SLOW_INTERVAL`] blocks after. +//! A wallet whose last pass found nothing unconfirmed is idle and not scanned +//! again until a new transaction or an `Uncertain` result wakes it. One +//! worker per wallet drains a queue of merged requests. `Dead` and `Mined` end +//! the probing of a root; `Accepted` does not — a mempool is not settlement. +//! Probes run only while the SPV client delivers blocks, i.e. while the app is +//! in the foreground. + +use std::collections::{BTreeSet, HashMap, HashSet}; use std::future::Future; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, Weak}; @@ -45,7 +48,7 @@ use std::time::Duration; use dash_spv::sync::SyncEvent; use dash_spv::{BroadcastResult, EventHandler}; -use dashcore::{OutPoint, Transaction, Txid}; +use dashcore::{Transaction, Txid}; use key_wallet::transaction_checking::TransactionContext; use key_wallet_manager::{WalletEvent, WalletId, WalletManager}; use tokio::sync::RwLock; @@ -61,27 +64,98 @@ pub(crate) const EVERY_BLOCK_WINDOW: u32 = 24; /// Blocks between probes once [`EVERY_BLOCK_WINDOW`] has passed. pub(crate) const SLOW_INTERVAL: u32 = 10; -/// What the resolver needs to know about one recorded transaction. +/// How far a wallet's synced height may advance in one step and still count +/// as following the tip. Larger steps are catch-up after time offline: the +/// scan itself settles most roots, so a height-driven pass waits for it. +pub(crate) const CATCH_UP_STEP: u32 = 3; + +/// One unsettled transaction of the wallet, merged across the accounts that +/// record it. #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct OutgoingView { pub txid: Txid, - pub inputs: Vec, - /// In a block, InstantSend-locked, or finalized. - pub settled: bool, /// The transaction spends at least one of this wallet's coins, i.e. the /// wallet signed it. pub spends_own_coins: bool, pub transaction: Transaction, } -/// This wallet's unsettled transactions as chains. -/// -/// Built from the account views, merged per transaction: settled if any -/// account holds it settled (or the caller knows it is mined), the wallet's own -/// send if any account records it spending the wallet's coins — a transfer -/// between accounts is "incoming" in the receiving one. +impl OutgoingView { + fn spends_output_of(&self, txids: &impl Fn(&Txid) -> bool) -> bool { + self.transaction + .input + .iter() + .any(|input| txids(&input.previous_output.txid)) + } +} + +/// One account's record of a transaction, as far as the resolver cares. +pub(crate) struct RecordFacts<'a> { + pub txid: Txid, + /// In a block, InstantSend-locked, or finalized in this account. + pub settled: bool, + /// This account records the transaction spending the wallet's coins. + pub own: bool, + pub transaction: &'a Transaction, +} + +/// The one place the multi-account rule lives: a transaction is one +/// transaction however many accounts record it; it is settled if any account +/// holds it settled, and the wallet's own send if any account records it +/// spending the wallet's coins (a transfer between accounts is "incoming" in +/// the receiving one). Returns the unsettled ones. +pub(crate) fn merge_records<'a>( + records: impl IntoIterator>, +) -> Vec { + let mut settled: HashSet = HashSet::new(); + let mut unsettled: HashMap = HashMap::new(); + for record in records { + if record.settled { + settled.insert(record.txid); + continue; + } + unsettled + .entry(record.txid) + .and_modify(|(own, _)| *own |= record.own) + .or_insert((record.own, record.transaction)); + } + unsettled + .into_iter() + .filter(|(txid, _)| !settled.contains(txid)) + .map(|(txid, (own, transaction))| OutgoingView { + txid, + spends_own_coins: own, + transaction: transaction.clone(), + }) + .collect() +} + +/// This wallet's unsettled transactions. One walk over the records; only +/// unsettled ones are cloned — on a healthy wallet there are none or a +/// handful. +pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { + let accounts = info.core_wallet.accounts.all_accounts(); + merge_records(accounts.iter().flat_map(|account| { + account + .transactions() + .values() + .map(move |record| RecordFacts { + txid: record.txid, + settled: record.is_confirmed() + || matches!(record.context, TransactionContext::InstantSend(_)) + || account.transaction_is_finalized(&record.txid), + own: !record.input_details.is_empty(), + transaction: &record.transaction, + }) + })) +} + +/// This wallet's unsettled transactions as chains. `views` hold one entry per +/// transaction (see [`merge_records`]). pub(crate) struct ChainGraph<'a> { - by_txid: BTreeMap, + by_txid: HashMap, + /// Unsettled for the purpose of picking roots: every view except those a + /// probe found mined. unsettled: BTreeSet, } @@ -90,30 +164,24 @@ impl<'a> ChainGraph<'a> { /// not seen yet: settled for the purpose of picking roots, so the sends /// built on them become roots in turn. pub(crate) fn new(views: &'a [OutgoingView], mined: &HashSet) -> Self { - let mut merged: BTreeMap = BTreeMap::new(); - for view in views { - merged - .entry(view.txid) - .and_modify(|(_, settled, own)| { - *settled |= view.settled; - *own |= view.spends_own_coins; - }) - .or_insert((view, view.settled, view.spends_own_coins)); - } - let unsettled = merged - .iter() - .filter(|(txid, (_, settled, _))| !*settled && !mined.contains(*txid)) - .map(|(txid, _)| *txid) - .collect(); - let by_txid = merged - .into_iter() - .map(|(txid, (view, _, own))| (txid, (view, own))) + let by_txid: HashMap = + views.iter().map(|view| (view.txid, view)).collect(); + let unsettled = by_txid + .keys() + .filter(|txid| !mined.contains(*txid)) + .copied() .collect(); Self { by_txid, unsettled } } fn is_own(&self, txid: &Txid) -> bool { - self.by_txid.get(txid).is_some_and(|(_, own)| *own) + self.by_txid + .get(txid) + .is_some_and(|view| view.spends_own_coins) + } + + fn is_unsettled(&self, txid: &Txid) -> bool { + self.unsettled.contains(txid) } /// The wallet's unsettled sends. @@ -125,29 +193,50 @@ impl<'a> ChainGraph<'a> { .collect() } + /// Unsettled transactions `txid` depends on, transitively, including + /// itself. + fn with_ancestors(&self, txid: &Txid) -> BTreeSet { + let mut found = BTreeSet::new(); + let mut frontier = vec![*txid]; + while let Some(txid) = frontier.pop() { + if !self.is_unsettled(&txid) || !found.insert(txid) { + continue; + } + for input in &self.by_txid[&txid].transaction.input { + frontier.push(input.previous_output.txid); + } + } + found + } + + fn is_root(&self, txid: &Txid) -> bool { + self.is_unsettled(txid) + && !self.by_txid[txid].spends_output_of(&|parent| self.is_unsettled(parent)) + } + /// The roots to probe: members of the chains the wallet signed into — its /// unsettled sends plus every unsettled ancestor they depend on, including /// someone else's incoming payment — that spend no output of an unsettled /// transaction. A node that has not seen a parent refuses a valid child. pub(crate) fn roots(&self) -> Vec<&'a OutgoingView> { - let mut relevant = self.own_unsettled(); - let mut frontier: Vec = relevant.iter().copied().collect(); - while let Some(txid) = frontier.pop() { - for input in &self.by_txid[&txid].0.inputs { - if self.unsettled.contains(&input.txid) && relevant.insert(input.txid) { - frontier.push(input.txid); - } - } - } + let relevant: BTreeSet = self + .own_unsettled() + .iter() + .flat_map(|txid| self.with_ancestors(txid)) + .collect(); relevant .iter() - .map(|txid| self.by_txid[txid].0) - .filter(|view| { - !view - .inputs - .iter() - .any(|input| self.unsettled.contains(&input.txid)) - }) + .filter(|txid| self.is_root(txid)) + .map(|txid| self.by_txid[txid]) + .collect() + } + + /// The roots of `txid`'s chain — what to probe when dash-spv reports + /// `txid` uncertain but it spends an unsettled parent. + pub(crate) fn roots_of(&self, txid: &Txid) -> BTreeSet { + self.with_ancestors(txid) + .into_iter() + .filter(|txid| self.is_root(txid)) .collect() } @@ -160,11 +249,7 @@ impl<'a> ChainGraph<'a> { let before = chain.len(); for txid in &self.unsettled { if !chain.contains(txid) - && self.by_txid[txid] - .0 - .inputs - .iter() - .any(|input| chain.contains(&input.txid)) + && self.by_txid[txid].spends_output_of(&|parent| chain.contains(parent)) { chain.insert(*txid); } @@ -219,6 +304,22 @@ enum Final { Mined, } +/// Whether two verdicts say the same thing to the host. Accepted and Mined +/// both mean "going through" and reach the host as the same code; every +/// Unresolved counts as one, since its reason varies from probe to probe. +fn same_for_host(a: &ProbeVerdict, b: &ProbeVerdict) -> bool { + matches!( + (a, b), + ( + ProbeVerdict::Accepted | ProbeVerdict::Mined, + ProbeVerdict::Accepted | ProbeVerdict::Mined + ) | ( + ProbeVerdict::Unresolved { .. }, + ProbeVerdict::Unresolved { .. } + ) + ) || a == b +} + /// Bookkeeping shared by every pass. Schedules and final verdicts are per /// probed root; published verdicts are per own send, which is what the host /// shows. Everything for a transaction is kept while it stays unsettled in the @@ -266,34 +367,38 @@ impl ResolverState { cleared } + fn has_published(&self, wallet_id: &WalletId) -> bool { + self.published.keys().any(|(wallet, _)| wallet == wallet_id) + } + /// Record `verdict` for `send`; returns whether the host must hear it. /// Unresolved never replaces an Accepted or Mined verdict — one probe that - /// only met transport errors says nothing new — and Unresolved reasons - /// vary between probes, so they all count as one. + /// only met transport errors says nothing new. fn publish(&mut self, key: (WalletId, Txid), verdict: &ProbeVerdict) -> bool { let changed = match self.published.get(&key) { None => true, - Some(ProbeVerdict::Unresolved { .. }) => { - !matches!(verdict, ProbeVerdict::Unresolved { .. }) - } Some(ProbeVerdict::Accepted | ProbeVerdict::Mined) if matches!(verdict, ProbeVerdict::Unresolved { .. }) => { false } - Some(previous) => previous != verdict, + Some(previous) => !same_for_host(previous, verdict), }; if changed { self.published.insert(key, verdict.clone()); } changed } -} -/// How far a wallet's synced height may advance in one step and still count -/// as following the tip. Larger steps are catch-up after time offline: the -/// scan itself settles most roots, so a height-driven pass waits for it. -pub(crate) const CATCH_UP_STEP: u32 = 3; + /// Whether `verdict` is still what is recorded for `send` — false once a + /// clear (probing off, wallet removed, send settled) has run since the + /// pass recorded it. + fn still_published(&self, key: &(WalletId, Txid), verdict: &ProbeVerdict) -> bool { + self.published + .get(key) + .is_some_and(|recorded| same_for_host(recorded, verdict)) + } +} /// The first advance after launch has no previous height to compare with, and /// it is usually the stored tip from before the scan: treat it as catch-up @@ -303,8 +408,10 @@ pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { } /// One pass over one wallet at `height`: probe every due root and return what -/// to publish. `forced` roots are due regardless of their schedule (the SPV -/// broadcaster has just reported them `Uncertain`). +/// to publish. The roots of every `forced` transaction's chain are due +/// regardless of their schedule (dash-spv has just reported it `Uncertain`). +/// `keep_going` is asked before each probe; once it says no (probing turned +/// off), the pass sends nothing more to the network. /// /// A root's verdict is published for the wallet's own sends: the root itself /// when it is one, and — when the root is dead — every own send built on it, @@ -317,14 +424,18 @@ pub(crate) async fn resolve_pass( height: u32, views: &[OutgoingView], forced: &HashSet, + keep_going: &(dyn Fn() -> bool + Sync), ) -> Vec { // What the wallet itself still holds unsettled — what state is kept for. - let wallet_view = ChainGraph::new(views, &HashSet::new()); - let present: HashSet = wallet_view.unsettled.iter().copied().collect(); - let own_present = wallet_view.own_unsettled(); + let present: HashSet = views.iter().map(|view| view.txid).collect(); + let own_present: HashSet = views + .iter() + .filter(|view| view.spends_own_coins) + .map(|view| view.txid) + .collect(); let mut events = Vec::new(); - let graph_mined: HashSet = { + let mined: HashSet = { let mut guard = state.lock().expect("resolver state poisoned"); let ResolverState { schedules, @@ -344,15 +455,18 @@ pub(crate) async fn resolve_pass( published.retain(|(wallet, txid), _| *wallet != wallet_id || own_present.contains(txid)); events.extend(cleared.into_iter().map(ResolverEvent::Cleared)); - let mined: HashSet = finished + finished .iter() .filter(|((wallet, _), result)| *wallet == wallet_id && **result == Final::Mined) .map(|((_, txid), _)| *txid) - .collect(); - mined + .collect() }; - let graph = ChainGraph::new(views, &graph_mined); + let graph = ChainGraph::new(views, &mined); + let forced_roots: BTreeSet = forced + .iter() + .flat_map(|txid| graph.roots_of(txid)) + .collect(); let due: Vec<(Txid, Transaction)> = { let mut guard = state.lock().expect("resolver state poisoned"); let ResolverState { @@ -360,7 +474,7 @@ pub(crate) async fn resolve_pass( finished, .. } = &mut *guard; - let mut due_now = Vec::new(); + let mut due = Vec::new(); for view in graph.roots() { let key = (wallet_id, view.txid); if finished.contains_key(&key) { @@ -369,20 +483,23 @@ pub(crate) async fn resolve_pass( let schedule = schedules .entry(key) .or_insert_with(|| ProbeSchedule::new(height)); - if forced.contains(&view.txid) || schedule.is_due(height) { + if forced_roots.contains(&view.txid) || schedule.is_due(height) { schedule.probed_at(height); - due_now.push((view.txid, view.transaction.clone())); + due.push((view.txid, view.transaction.clone())); } } - due_now + due }; for (root, transaction) in due { + if !keep_going() { + break; + } let verdict = probe.probe(&transaction).await; let key = (wallet_id, root); let mut guard = state.lock().expect("resolver state poisoned"); // A pass that ran meanwhile may have found this root settled or gone - // and dropped it; a verdict published now would resurrect it. + // and dropped it; a verdict recorded now would resurrect it. if !guard.schedules.contains_key(&key) { continue; } @@ -411,50 +528,6 @@ pub(crate) async fn resolve_pass( events } -/// This wallet's unsettled transactions, one view per transaction, merged -/// across the accounts that record it (settled if any account holds it -/// settled; the wallet's own if any account records it spending the wallet's -/// coins). One walk over the records; only unsettled transactions are cloned — -/// on a healthy wallet there are none or a handful. -pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { - let accounts = info.core_wallet.accounts.all_accounts(); - // Settled txids in a set; only unsettled records go into the map, so a - // wallet with nothing unconfirmed costs one walk and no ordering work. - let mut settled: HashSet = HashSet::new(); - let mut unsettled: HashMap = HashMap::new(); - for account in accounts.iter() { - for record in account.transactions().values() { - if record.is_confirmed() - || matches!(record.context, TransactionContext::InstantSend(_)) - || account.transaction_is_finalized(&record.txid) - { - settled.insert(record.txid); - continue; - } - let own = !record.input_details.is_empty(); - unsettled - .entry(record.txid) - .and_modify(|(was_own, _)| *was_own |= own) - .or_insert((own, &record.transaction)); - } - } - unsettled - .into_iter() - .filter(|(txid, _)| !settled.contains(txid)) - .map(|(txid, (own, transaction))| OutgoingView { - txid, - inputs: transaction - .input - .iter() - .map(|input| input.previous_output) - .collect(), - settled: false, - spends_own_coins: own, - transaction: transaction.clone(), - }) - .collect() -} - /// Admission gate for probe tasks. A probe only reads the wallet and /// resubmits bytes that were already sent, so a task may be aborted at any /// point without losing anything. @@ -506,13 +579,17 @@ impl ProbeTasks { } } -/// Which wallets have a pass running and what the next pass for each must do. -/// A pass for a wallet never runs twice at once; requests that arrive while -/// one runs are merged into the next. +/// Which wallets have a pass running, what the next pass for each must do, +/// and which have nothing to probe. A pass for a wallet never runs twice at +/// once; requests that arrive while one runs are merged into the next. #[derive(Default)] struct PassQueue { running: HashSet, pending: HashMap, + /// Wallets whose last pass found nothing unconfirmed and published + /// nothing: height advances skip them (no scan of every record per block) + /// until a new transaction or an `Uncertain` result wakes them. + idle: HashSet, } #[derive(Default)] @@ -521,9 +598,9 @@ struct PendingPass { forced: HashSet, } -/// Runs probe passes off the wallet-event fan-out and publishes each verdict -/// change as [`PlatformEventHandler::on_outgoing_transaction_probed`], and each -/// root that stops being ambiguous as +/// Runs probe passes off the wallet-event fan-out and publishes each change of +/// a send's verdict as [`PlatformEventHandler::on_outgoing_transaction_probed`], +/// and each send that no longer needs one as /// [`PlatformEventHandler::on_outgoing_transaction_cleared`]. /// /// Off until the host turns it on with [`set_enabled`](Self::set_enabled): a @@ -534,9 +611,10 @@ pub(crate) struct BroadcastResolver { probe: Arc, wallet_manager: Arc>>, state: Arc>, - /// Last synced height per wallet: an SPV-level `Uncertain` carries no - /// wallet id, so it is queued for every wallet at its current height. - heights: Arc>>, + /// Every wallet seen in an event, with its last synced height if one has + /// been reported: an SPV-level `Uncertain` carries no wallet id, so it is + /// queued for every known wallet. + wallets: Mutex>>, passes: Arc>, events: Mutex>, tasks: ProbeTasks, @@ -552,7 +630,7 @@ impl BroadcastResolver { probe, wallet_manager, state: Arc::new(Mutex::new(ResolverState::default())), - heights: Arc::new(Mutex::new(HashMap::new())), + wallets: Mutex::new(HashMap::new()), passes: Arc::new(Mutex::new(PassQueue::default())), events: Mutex::new(Weak::new()), tasks: ProbeTasks::new(), @@ -565,19 +643,18 @@ impl BroadcastResolver { *self.events.lock().expect("events mutex poisoned") = events; } - /// Turning probing off forgets every root and tells the host to drop every + /// Turning probing off forgets every send and tells the host to drop every /// verdict it holds: with no passes running, nothing would ever clear them. + /// Done under the state lock, so no verdict a running pass recorded can + /// reach the host after its clear. pub(crate) fn set_enabled(&self, enabled: bool) { let was = self.enabled.swap(enabled, Ordering::SeqCst); if was && !enabled { - let cleared = self - .state - .lock() - .expect("resolver state poisoned") - .forget_all(); - if let Some(events) = self.events.lock().expect("events mutex poisoned").upgrade() { - for (wallet_id, txid) in &cleared { - events.on_outgoing_transaction_cleared(wallet_id, txid); + let events = self.events.lock().expect("events mutex poisoned").upgrade(); + let mut state = self.state.lock().expect("resolver state poisoned"); + for (wallet_id, txid) in state.forget_all() { + if let Some(events) = &events { + events.on_outgoing_transaction_cleared(&wallet_id, &txid); } } } @@ -587,26 +664,24 @@ impl BroadcastResolver { self.enabled.load(Ordering::SeqCst) } - /// Forget a removed wallet: its queued pass, its last height and every - /// verdict, each of which the host is told is cleared. + /// Forget a removed wallet — call after it has left the wallet manager, so + /// no pass can read it again: its queued pass, its entry and every verdict, + /// each of which the host is told is cleared. pub(crate) fn wallet_removed(&self, wallet_id: &WalletId) { - self.passes - .lock() - .expect("pass queue poisoned") - .pending - .remove(wallet_id); - self.heights + { + let mut passes = self.passes.lock().expect("pass queue poisoned"); + passes.pending.remove(wallet_id); + passes.idle.remove(wallet_id); + } + self.wallets .lock() - .expect("heights mutex poisoned") + .expect("wallets mutex poisoned") .remove(wallet_id); - let cleared = self - .state - .lock() - .expect("resolver state poisoned") - .forget_wallet(wallet_id); - if let Some(events) = self.events.lock().expect("events mutex poisoned").upgrade() { - for txid in &cleared { - events.on_outgoing_transaction_cleared(wallet_id, txid); + let events = self.events.lock().expect("events mutex poisoned").upgrade(); + let mut state = self.state.lock().expect("resolver state poisoned"); + for txid in state.forget_wallet(wallet_id) { + if let Some(events) = &events { + events.on_outgoing_transaction_cleared(wallet_id, &txid); } } } @@ -617,9 +692,15 @@ impl BroadcastResolver { } /// Queue a pass for `wallet_id`; start a worker for it unless one runs. + /// A height-driven request (no `force`) is dropped for an idle wallet. fn request_pass(&self, wallet_id: WalletId, height: u32, force: Option) { { let mut passes = self.passes.lock().expect("pass queue poisoned"); + if force.is_some() { + passes.idle.remove(&wallet_id); + } else if passes.idle.contains(&wallet_id) { + return; + } let pending = passes.pending.entry(wallet_id).or_default(); pending.height = pending.height.max(height); pending.forced.extend(force); @@ -630,12 +711,11 @@ impl BroadcastResolver { let wallet_manager = Arc::clone(&self.wallet_manager); let probe = Arc::clone(&self.probe); let state = Arc::clone(&self.state); - let heights = Arc::clone(&self.heights); let passes = Arc::clone(&self.passes); let enabled = Arc::clone(&self.enabled); let events = self.events.lock().expect("events mutex poisoned").clone(); self.tasks.spawn(async move { - let _running = Running { + let running = Running { passes: Arc::clone(&passes), wallet_id, }; @@ -643,22 +723,20 @@ impl BroadcastResolver { // Take the next request, or stop — under the same lock a // request_pass would take, so a request can never land between // "queue empty" and "no longer running" and be left unserved. - // Probing turned off meanwhile: drop what is queued, do not - // probe again (set_enabled already cleared the host's state). + // Probing turned off meanwhile: drop what is queued. let next = { let mut queue = passes.lock().expect("pass queue poisoned"); - let next = if enabled.load(Ordering::SeqCst) { - queue.pending.remove(&wallet_id) - } else { - queue.pending.remove(&wallet_id); - None - }; + let next = queue.pending.remove(&wallet_id); + let next = next.filter(|_| enabled.load(Ordering::SeqCst)); if next.is_none() { queue.running.remove(&wallet_id); } next }; let Some(PendingPass { height, forced }) = next else { + // Already left the running set under the lock; the guard + // must not remove a marker a new worker may have set since. + std::mem::forget(running); break; }; // Read under the lock, probe without it: a probe is network I/O. @@ -666,35 +744,52 @@ impl BroadcastResolver { let manager = wallet_manager.read().await; manager.get_wallet_info(&wallet_id).map(collect_views) }; - let outcome = match views { - Some(views) => { - resolve_pass(&state, probe.as_ref(), wallet_id, height, &views, &forced) - .await - } - None => { - // The wallet was removed: forget it and clear whatever - // the host still shows for it. - heights - .lock() - .expect("heights mutex poisoned") - .remove(&wallet_id); - state - .lock() - .expect("resolver state poisoned") - .forget_wallet(&wallet_id) - .into_iter() - .map(ResolverEvent::Cleared) - .collect() - } + let Some(views) = views else { + // Removed without wallet_removed having run yet: nothing + // to probe; wallet_removed does the clearing. + continue; }; - publish(&events, wallet_id, height, !forced.is_empty(), &outcome); + let keep_going = || enabled.load(Ordering::SeqCst); + let outcome = resolve_pass( + &state, + probe.as_ref(), + wallet_id, + height, + &views, + &forced, + &keep_going, + ) + .await; + let nothing_left = views.iter().all(|view| !view.spends_own_coins) + && !state + .lock() + .expect("resolver state poisoned") + .has_published(&wallet_id); + if nothing_left { + passes + .lock() + .expect("pass queue poisoned") + .idle + .insert(wallet_id); + } + publish( + &state, + &events, + wallet_id, + height, + !forced.is_empty(), + &outcome, + ); } }); } } -/// Hand a pass's outcome to the host and the log. +/// Hand a pass's outcome to the host and the log — under the state lock, and +/// only verdicts still recorded, so a clear that ran after the pass recorded a +/// verdict (probing off, wallet removed) always wins. fn publish( + state: &Mutex, events: &Weak, wallet_id: WalletId, height: u32, @@ -708,9 +803,13 @@ fn publish( // What started this pass: dash-spv's Uncertain result, or an advance of // the wallet's synced height. let trigger = if forced { "uncertain" } else { "height" }; + let guard = state.lock().expect("resolver state poisoned"); for event in outcome { match event { ResolverEvent::Verdict(txid, verdict) => { + if !guard.still_published(&(wallet_id, *txid), verdict) { + continue; + } tracing::info!( wallet_id = %hex::encode(wallet_id), txid = %txid, @@ -738,8 +837,10 @@ fn publish( } } -/// Marks a wallet's worker finished if it ends without reaching its own exit -/// (an abort at shutdown), so a later request is not refused forever. +/// Takes a wallet's worker out of the running set if it ends without reaching +/// its own exit — an abort at shutdown — so a later request is not refused +/// forever. Forgotten on the normal exit, which leaves the set under the queue +/// lock itself. struct Running { passes: Arc>, wallet_id: WalletId, @@ -755,16 +856,39 @@ impl Drop for Running { impl EventHandler for BroadcastResolver { fn on_wallet_event(&self, event: &WalletEvent) { - let WalletEvent::SyncHeightAdvanced { wallet_id, height } = event else { - return; - }; - let previous = self - .heights - .lock() - .expect("heights mutex poisoned") - .insert(*wallet_id, *height); - if self.is_enabled() && !is_catch_up_step(previous, *height) { - self.request_pass(*wallet_id, *height, None); + let wallet_id = event.wallet_id(); + match event { + WalletEvent::SyncHeightAdvanced { height, .. } => { + let previous = self + .wallets + .lock() + .expect("wallets mutex poisoned") + .insert(wallet_id, Some(*height)) + .flatten(); + if self.is_enabled() && !is_catch_up_step(previous, *height) { + self.request_pass(wallet_id, *height, None); + } + } + WalletEvent::TransactionDetected { .. } => { + // A new transaction may be something to probe: wake the wallet. + self.wallets + .lock() + .expect("wallets mutex poisoned") + .entry(wallet_id) + .or_insert(None); + self.passes + .lock() + .expect("pass queue poisoned") + .idle + .remove(&wallet_id); + } + _ => { + self.wallets + .lock() + .expect("wallets mutex poisoned") + .entry(wallet_id) + .or_insert(None); + } } } @@ -779,14 +903,14 @@ impl EventHandler for BroadcastResolver { if !self.is_enabled() { return; } - let heights: Vec<(WalletId, u32)> = self - .heights + let wallets: Vec<(WalletId, u32)> = self + .wallets .lock() - .expect("heights mutex poisoned") + .expect("wallets mutex poisoned") .iter() - .map(|(wallet, height)| (*wallet, *height)) + .map(|(wallet, height)| (*wallet, height.unwrap_or(0))) .collect(); - for (wallet_id, height) in heights { + for (wallet_id, height) in wallets { self.request_pass(wallet_id, height, Some(*txid)); } } @@ -796,10 +920,12 @@ impl PlatformEventHandler for BroadcastResolver {} #[cfg(test)] mod tests { + use std::collections::BTreeMap; use std::sync::Mutex as StdMutex; use async_trait::async_trait; use dashcore::hashes::Hash; + use dashcore::{OutPoint, TxIn}; use super::*; @@ -814,11 +940,19 @@ mod tests { } } - fn tx(lock_time: u32) -> Transaction { + /// Fixtures key transactions by `lock_time` == txid byte, so a probe can + /// tell which one it was handed. + fn tx(n: u8, inputs: &[OutPoint]) -> Transaction { Transaction { version: 2, - lock_time, - input: Vec::new(), + lock_time: n as u32, + input: inputs + .iter() + .map(|previous_output| TxIn { + previous_output: *previous_output, + ..Default::default() + }) + .collect(), output: Vec::new(), special_transaction_payload: None, } @@ -827,10 +961,15 @@ mod tests { fn send(n: u8, inputs: &[OutPoint]) -> OutgoingView { OutgoingView { txid: txid(n), - inputs: inputs.to_vec(), - settled: false, spends_own_coins: true, - transaction: tx(n as u32), + transaction: tx(n, inputs), + } + } + + fn incoming(n: u8, inputs: &[OutPoint]) -> OutgoingView { + OutgoingView { + spends_own_coins: false, + ..send(n, inputs) } } @@ -838,7 +977,45 @@ mod tests { ambiguous_roots(views).into_iter().map(|v| v.txid).collect() } - // ---- ambiguous_roots ---------------------------------------------------- + fn none() -> HashSet { + HashSet::new() + } + + fn always() -> impl Fn() -> bool + Sync { + || true + } + + // ---- merge_records ------------------------------------------------------ + + fn facts(n: u8, settled: bool, own: bool, transaction: &Transaction) -> RecordFacts<'_> { + RecordFacts { + txid: txid(n), + settled, + own, + transaction, + } + } + + /// A transfer between the wallet's own accounts is recorded twice: as + /// incoming in the receiving account (listed first) and as spending in the + /// sending one. It is the wallet's own send either way. + #[test] + fn should_treat_a_transfer_between_own_accounts_as_the_wallets_send() { + let t = tx(1, &[outpoint(90, 0)]); + let views = merge_records([facts(1, false, false, &t), facts(1, false, true, &t)]); + assert_eq!(views.len(), 1); + assert!(views[0].spends_own_coins); + } + + /// A transaction settled in any account is settled. + #[test] + fn should_drop_a_transaction_settled_in_any_account() { + let t = tx(1, &[outpoint(90, 0)]); + assert!(merge_records([facts(1, false, true, &t), facts(1, true, false, &t)]).is_empty()); + assert!(merge_records([facts(1, true, false, &t), facts(1, false, true, &t)]).is_empty()); + } + + // ---- ChainGraph --------------------------------------------------------- #[test] fn should_pick_a_lone_unsettled_send() { @@ -861,37 +1038,47 @@ mod tests { assert_eq!(roots(&views), BTreeSet::from([txid(1)])); } + /// Once the parent settles it is gone from the views, and the child has no + /// unsettled parent: it is a root in its own right. + #[test] + fn should_treat_the_child_of_a_settled_parent_as_a_root() { + assert_eq!( + roots(&[send(2, &[outpoint(1, 1)])]), + BTreeSet::from([txid(2)]) + ); + } + + /// Nodes that have not seen an unconfirmed incoming payment refuse a send + /// that spends it, so the send is not probed itself — the incoming parent + /// is, since its fate decides the send's. #[test] - fn should_skip_settled_and_incoming_transactions() { - let mut settled = send(1, &[outpoint(90, 0)]); - settled.settled = true; - let mut incoming = send(2, &[outpoint(91, 0)]); - incoming.spends_own_coins = false; - assert!(roots(&[settled, incoming]).is_empty()); + fn should_probe_the_unconfirmed_incoming_parent_of_a_send_instead_of_the_send() { + let views = [incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])]; + assert_eq!(roots(&views), BTreeSet::from([txid(5)])); } - /// Once the parent settles, the child has no unsettled parent and is a - /// root in its own right. + /// An unconfirmed incoming payment nobody built on is not this wallet's + /// business to probe. #[test] - fn should_treat_the_child_of_a_settled_parent_as_a_root() { - let mut parent = send(1, &[outpoint(90, 0)]); - parent.settled = true; - let child = send(2, &[outpoint(1, 1)]); - assert_eq!(roots(&[parent, child]), BTreeSet::from([txid(2)])); + fn should_not_probe_an_incoming_payment_the_wallet_did_not_spend() { + assert!(roots(&[incoming(5, &[outpoint(80, 0)])]).is_empty()); } - /// key-wallet records a transaction in every account it touches; one - /// root, and a settled copy anywhere settles it. + /// dash-spv reports the uncertain txid, which may be a child: the root of + /// its chain is what must be probed at once. #[test] - fn should_merge_a_transaction_recorded_in_two_accounts() { - let a = send(1, &[outpoint(90, 0)]); - let mut b = send(1, &[outpoint(90, 0)]); - b.settled = true; - assert!(roots(&[a.clone(), b]).is_empty()); - assert_eq!(roots(&[a.clone(), a]), BTreeSet::from([txid(1)])); + fn should_find_the_root_of_a_forced_child() { + let views = [ + send(1, &[outpoint(90, 0)]), + send(2, &[outpoint(1, 1)]), + send(3, &[outpoint(2, 1)]), + ]; + let graph = ChainGraph::new(&views, &none()); + assert_eq!(graph.roots_of(&txid(3)), BTreeSet::from([txid(1)])); + assert!(graph.roots_of(&txid(9)).is_empty(), "unknown txid"); } - // ---- ProbeSchedule ------------------------------------------------------ + // ---- ProbeSchedule / catch-up ------------------------------------------ #[test] fn should_be_due_immediately_when_never_probed() { @@ -918,6 +1105,17 @@ mod tests { assert!(s.is_due(late + SLOW_INTERVAL)); } + #[test] + fn should_treat_only_large_height_steps_as_catch_up() { + assert!( + is_catch_up_step(None, 1_000), + "the first step after launch is catch-up" + ); + assert!(!is_catch_up_step(Some(999), 1_000)); + assert!(!is_catch_up_step(Some(1_000 - CATCH_UP_STEP), 1_000)); + assert!(is_catch_up_step(Some(1_000 - CATCH_UP_STEP - 1), 1_000)); + } + // ---- resolve_pass ------------------------------------------------------- /// Answers per txid and records every probe it received. @@ -942,7 +1140,6 @@ mod tests { #[async_trait] impl AcceptanceProbe for ScriptedProbe { async fn probe(&self, transaction: &Transaction) -> ProbeVerdict { - // Fixtures key transactions by lock_time == txid byte. let id = txid(transaction.lock_time as u8); self.probed.lock().expect("probed").push(id); self.answers @@ -970,13 +1167,22 @@ mod tests { } } + async fn pass( + state: &Mutex, + probe: &dyn AcceptanceProbe, + height: u32, + views: &[OutgoingView], + ) -> Vec { + resolve_pass(state, probe, wallet(), height, views, &none(), &always()).await + } + #[tokio::test] async fn should_probe_only_roots_and_report_each_verdict() { let state = Mutex::new(ResolverState::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; - let events = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + let events = pass(&state, &probe, 100, &views).await; assert_eq!(probe.probed(), vec![txid(1)]); assert_eq!(events, vec![ResolverEvent::Verdict(txid(1), unresolved())]); @@ -988,8 +1194,8 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), dead())]); let views = [send(1, &[outpoint(90, 0)])]; - resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; - let again = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; + pass(&state, &probe, 100, &views).await; + let again = pass(&state, &probe, 101, &views).await; assert!(again.is_empty()); assert_eq!(probe.probed(), vec![txid(1)]); @@ -1003,8 +1209,8 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); let views = [send(1, &[outpoint(90, 0)])]; - let first = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; - let second = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; + let first = pass(&state, &probe, 100, &views).await; + let second = pass(&state, &probe, 101, &views).await; assert_eq!( first, @@ -1023,9 +1229,9 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)])]; - let first = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; - let same_block = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; - let next_block = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; + let first = pass(&state, &probe, 100, &views).await; + let same_block = pass(&state, &probe, 100, &views).await; + let next_block = pass(&state, &probe, 101, &views).await; assert_eq!(first, vec![ResolverEvent::Verdict(txid(1), unresolved())]); assert!(same_block.is_empty(), "one probe per block"); @@ -1044,7 +1250,7 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)])]; - resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + pass(&state, &probe, 100, &views).await; resolve_pass( &state, &probe, @@ -1052,22 +1258,59 @@ mod tests { 100, &views, &HashSet::from([txid(1)]), + &always(), + ) + .await; + + assert_eq!(probe.probed(), vec![txid(1), txid(1)]); + } + + /// The customer's shape: the uncertain send spends the change of an + /// earlier unsettled send. Forcing the child probes its root at once. + #[tokio::test] + async fn should_probe_the_root_when_a_child_is_forced() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; + + pass(&state, &probe, 100, &views).await; + resolve_pass( + &state, + &probe, + wallet(), + 100, + &views, + &HashSet::from([txid(2)]), + &always(), ) .await; assert_eq!(probe.probed(), vec![txid(1), txid(1)]); } - /// A root that settled or left the wallet is forgotten, and the host is + /// Probing turned off mid-pass: the pass sends nothing more to the network. + #[tokio::test] + async fn should_stop_probing_once_told_to() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[]); + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])]; + + let events = resolve_pass(&state, &probe, wallet(), 100, &views, &none(), &|| false).await; + + assert!(probe.probed().is_empty()); + assert!(events.is_empty()); + } + + /// A send that settled or left the wallet is forgotten, and the host is /// told to drop the verdict it was shown. #[tokio::test] - async fn should_clear_a_published_root_that_is_no_longer_ambiguous() { + async fn should_clear_a_published_send_that_is_no_longer_unconfirmed() { let state = Mutex::new(ResolverState::default()); let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); let views = [send(1, &[outpoint(90, 0)])]; - resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; - let gone = resolve_pass(&state, &probe, wallet(), 101, &[], &HashSet::new()).await; + pass(&state, &probe, 100, &views).await; + let gone = pass(&state, &probe, 101, &[]).await; assert_eq!(gone, vec![ResolverEvent::Cleared(txid(1))]); let guard = state.lock().expect("state"); @@ -1076,53 +1319,14 @@ mod tests { assert!(guard.published.is_empty()); } - /// Nodes that have not seen an unconfirmed incoming payment refuse a send - /// that spends it, so the send is not probed itself — the incoming parent - /// is, since its fate decides the send's. Once the parent settles, the - /// send is the root. - #[test] - fn should_probe_the_unconfirmed_incoming_parent_of_a_send_instead_of_the_send() { - let mut incoming = send(5, &[outpoint(80, 0)]); - incoming.spends_own_coins = false; - let send_on_it = send(6, &[outpoint(5, 0)]); - assert_eq!( - roots(&[incoming.clone(), send_on_it.clone()]), - BTreeSet::from([txid(5)]) - ); - - incoming.settled = true; - assert_eq!(roots(&[incoming, send_on_it]), BTreeSet::from([txid(6)])); - } - - /// An unconfirmed incoming payment nobody built on is not this wallet's - /// business to probe. - #[test] - fn should_not_probe_an_incoming_payment_the_wallet_did_not_spend() { - let mut incoming = send(5, &[outpoint(80, 0)]); - incoming.spends_own_coins = false; - assert!(roots(&[incoming]).is_empty()); - } - - /// A transfer between the wallet's own accounts is recorded twice: as - /// incoming in the receiving account (listed first) and as spending in the - /// sending one. It is the wallet's own send either way. - #[test] - fn should_treat_a_transfer_between_own_accounts_as_the_wallets_send() { - let mut as_received = send(1, &[outpoint(90, 0)]); - as_received.spends_own_coins = false; - let as_sent = send(1, &[outpoint(90, 0)]); - assert_eq!(roots(&[as_received, as_sent]), BTreeSet::from([txid(1)])); - } - - /// Two wallets' roots are tracked independently. #[tokio::test] async fn should_not_let_one_wallets_pass_forget_another_wallets_roots() { let state = Mutex::new(ResolverState::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)])]; - resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; - resolve_pass(&state, &probe, [9u8; 32], 100, &[], &HashSet::new()).await; + pass(&state, &probe, 100, &views).await; + resolve_pass(&state, &probe, [9u8; 32], 100, &[], &none(), &always()).await; assert_eq!(state.lock().expect("state").schedules.len(), 1); } @@ -1135,85 +1339,77 @@ mod tests { let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined)]); let views = [send(1, &[outpoint(90, 0)])]; - resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; - let again = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; + pass(&state, &probe, 100, &views).await; + let again = pass(&state, &probe, 101, &views).await; assert!(again.is_empty()); assert_eq!(probe.probed(), vec![txid(1)]); } - #[test] - fn should_treat_only_large_height_steps_as_catch_up() { + /// Spending a dead transaction is dead too: the whole chain of own sends + /// built on a dead root is published dead, not just the root. + #[tokio::test] + async fn should_publish_a_dead_root_for_every_own_send_built_on_it() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), dead())]); + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; + + let events = pass(&state, &probe, 100, &views).await; + + assert_eq!( + events, + vec![ + ResolverEvent::Verdict(txid(1), dead()), + ResolverEvent::Verdict(txid(2), dead()), + ] + ); + let again = pass(&state, &probe, 101, &views).await; assert!( - is_catch_up_step(None, 1_000), - "the first step after launch is catch-up" + again.is_empty(), + "a dead child is not cleared for not being a root" ); - assert!(!is_catch_up_step(Some(999), 1_000)); - assert!(!is_catch_up_step(Some(1_000 - CATCH_UP_STEP), 1_000)); - assert!(is_catch_up_step(Some(1_000 - CATCH_UP_STEP - 1), 1_000)); } + /// Someone else's incoming payment is probed, but the host hears only + /// about the wallet's own send — the only row it can attach a verdict to. #[tokio::test] - async fn should_forget_one_wallet_and_return_its_published_roots() { + async fn should_publish_a_dead_incoming_parent_on_the_own_send_only() { let state = Mutex::new(ResolverState::default()); - let probe = ScriptedProbe::new(&[(txid(1), unresolved()), (txid(2), unresolved())]); - resolve_pass( - &state, - &probe, - wallet(), - 100, - &[send(1, &[outpoint(90, 0)])], - &HashSet::new(), - ) - .await; - resolve_pass( - &state, - &probe, - [9u8; 32], - 100, - &[send(2, &[outpoint(91, 0)])], - &HashSet::new(), - ) - .await; + let probe = ScriptedProbe::new(&[(txid(5), dead())]); + let views = [incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])]; - let cleared = state.lock().expect("state").forget_wallet(&wallet()); + let events = pass(&state, &probe, 100, &views).await; - assert_eq!(cleared, vec![txid(1)]); - let guard = state.lock().expect("state"); - assert_eq!(guard.published.len(), 1, "the other wallet is untouched"); - assert_eq!(guard.schedules.len(), 1); + assert_eq!(probe.probed(), vec![txid(5)]); + assert_eq!(events, vec![ResolverEvent::Verdict(txid(6), dead())]); } #[tokio::test] - async fn should_forget_everything_and_return_every_published_root() { + async fn should_publish_nothing_while_an_incoming_parent_is_only_accepted() { let state = Mutex::new(ResolverState::default()); - let probe = ScriptedProbe::new(&[(txid(1), unresolved()), (txid(2), unresolved())]); - resolve_pass( - &state, - &probe, - wallet(), - 100, - &[send(1, &[outpoint(90, 0)])], - &HashSet::new(), - ) - .await; - resolve_pass( - &state, - &probe, - [9u8; 32], - 100, - &[send(2, &[outpoint(91, 0)])], - &HashSet::new(), - ) - .await; + let probe = ScriptedProbe::new(&[(txid(5), ProbeVerdict::Accepted)]); + let views = [incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])]; - let cleared = state.lock().expect("state").forget_all(); + assert!(pass(&state, &probe, 100, &views).await.is_empty()); + } - assert_eq!(cleared.len(), 2); - let guard = state.lock().expect("state"); - assert!( - guard.published.is_empty() && guard.schedules.is_empty() && guard.finished.is_empty() + /// A root found in a block that the wallet has not caught up with is + /// settled for picking roots, so the send built on it is probed next. + #[tokio::test] + async fn should_probe_the_children_of_a_root_found_in_a_block() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined), (txid(2), dead())]); + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; + + let first = pass(&state, &probe, 100, &views).await; + let second = pass(&state, &probe, 101, &views).await; + + assert_eq!( + first, + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Mined)] ); + assert_eq!(second, vec![ResolverEvent::Verdict(txid(2), dead())]); + assert_eq!(probe.probed(), vec![txid(1), txid(2)]); } /// A probe that, while it is out on the network, lets a concurrent pass @@ -1243,7 +1439,8 @@ mod tests { wallet(), 100, &[send(1, &[outpoint(90, 0)])], - &HashSet::new(), + &none(), + &always(), ) .await; @@ -1251,91 +1448,84 @@ mod tests { assert!(state.lock().expect("state").published.is_empty()); } - fn incoming(n: u8, inputs: &[OutPoint]) -> OutgoingView { - OutgoingView { - spends_own_coins: false, - ..send(n, inputs) - } - } - - /// Spending a dead transaction is dead too: the whole chain of own sends - /// built on a dead root is published dead, not just the root. - #[tokio::test] - async fn should_publish_a_dead_root_for_every_own_send_built_on_it() { - let state = Mutex::new(ResolverState::default()); - let probe = ScriptedProbe::new(&[(txid(1), dead())]); - let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; - - let events = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; - - assert_eq!( - events, - vec![ - ResolverEvent::Verdict(txid(1), dead()), - ResolverEvent::Verdict(txid(2), dead()), - ] - ); - let again = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; - assert!( - again.is_empty(), - "a dead child is not cleared for not being a root" - ); - } - - /// Someone else's incoming payment is probed, but the host hears only - /// about the wallet's own send — the only row it can attach a verdict to. - #[tokio::test] - async fn should_publish_a_dead_incoming_parent_on_the_own_send_only() { - let state = Mutex::new(ResolverState::default()); - let probe = ScriptedProbe::new(&[(txid(5), dead())]); - let views = [incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])]; - - let events = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; - - assert_eq!(probe.probed(), vec![txid(5)]); - assert_eq!(events, vec![ResolverEvent::Verdict(txid(6), dead())]); - } + // ---- ResolverState ------------------------------------------------------ #[tokio::test] - async fn should_publish_nothing_while_an_incoming_parent_is_only_accepted() { + async fn should_forget_one_wallet_and_return_its_published_sends() { let state = Mutex::new(ResolverState::default()); - let probe = ScriptedProbe::new(&[(txid(5), ProbeVerdict::Accepted)]); - let views = [incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])]; + let probe = ScriptedProbe::new(&[(txid(1), unresolved()), (txid(2), unresolved())]); + pass(&state, &probe, 100, &[send(1, &[outpoint(90, 0)])]).await; + resolve_pass( + &state, + &probe, + [9u8; 32], + 100, + &[send(2, &[outpoint(91, 0)])], + &none(), + &always(), + ) + .await; - let events = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; + let cleared = state.lock().expect("state").forget_wallet(&wallet()); - assert!(events.is_empty()); + assert_eq!(cleared, vec![txid(1)]); + let guard = state.lock().expect("state"); + assert_eq!(guard.published.len(), 1, "the other wallet is untouched"); + assert_eq!(guard.schedules.len(), 1); } - /// A root found in a block that the wallet has not caught up with is - /// settled for picking roots, so the send built on it is probed next. #[tokio::test] - async fn should_probe_the_children_of_a_root_found_in_a_block() { + async fn should_forget_everything_and_return_every_published_send() { let state = Mutex::new(ResolverState::default()); - let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined), (txid(2), dead())]); - let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; + let probe = ScriptedProbe::new(&[(txid(1), unresolved()), (txid(2), unresolved())]); + pass(&state, &probe, 100, &[send(1, &[outpoint(90, 0)])]).await; + resolve_pass( + &state, + &probe, + [9u8; 32], + 100, + &[send(2, &[outpoint(91, 0)])], + &none(), + &always(), + ) + .await; - let first = resolve_pass(&state, &probe, wallet(), 100, &views, &HashSet::new()).await; - let second = resolve_pass(&state, &probe, wallet(), 101, &views, &HashSet::new()).await; + let cleared = state.lock().expect("state").forget_all(); - assert_eq!( - first, - vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Mined)] + assert_eq!(cleared.len(), 2); + let guard = state.lock().expect("state"); + assert!( + guard.published.is_empty() && guard.schedules.is_empty() && guard.finished.is_empty() ); - assert_eq!(second, vec![ResolverEvent::Verdict(txid(2), dead())]); - assert_eq!(probe.probed(), vec![txid(1), txid(2)]); } /// One probe that met only transport errors says nothing new about a send - /// already seen accepted; the UI must not flicker back to "unknown". + /// already seen accepted; the UI must not flicker back to "unknown". And + /// Accepted → Mined is no news to the host either: both mean "going + /// through" and reach it as the same code. #[test] - fn should_not_let_unresolved_replace_an_accepted_verdict() { + fn should_publish_only_what_the_host_would_see_as_a_change() { let mut state = ResolverState::default(); let key = (wallet(), txid(1)); assert!(state.publish(key, &ProbeVerdict::Accepted)); assert!(!state.publish(key, &unresolved())); + assert!(!state.publish(key, &ProbeVerdict::Mined)); assert!(!state.publish(key, &ProbeVerdict::Accepted)); assert!(state.publish(key, &dead())); } + + /// A verdict recorded by a pass is published only while it is still what + /// the state holds — a clear that ran in between wins. + #[test] + fn should_hold_back_a_verdict_cleared_after_it_was_recorded() { + let mut state = ResolverState::default(); + let key = (wallet(), txid(1)); + state.publish(key, &ProbeVerdict::Accepted); + assert!(state.still_published(&key, &ProbeVerdict::Accepted)); + + state.forget_all(); + + assert!(!state.still_published(&key, &ProbeVerdict::Accepted)); + } } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift index e5611899773..e18f3d25390 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift @@ -2660,6 +2660,9 @@ public class PlatformWalletManager: ObservableObject { // Same for broadcast-probe verdicts: Rust forgets the wallet's sends // on removal, and a restored wallet (same id) must start clean. outgoingTransactionVerdicts = outgoingTransactionVerdicts.filter { $0.key.walletId != walletId } + if lastOutgoingTransactionProbe?.walletId == walletId { + lastOutgoingTransactionProbe = nil + } // A store reconcile in flight for any wallet stops between pages: // its next step would read rows `deleteWalletData` is about to // remove. Coarse on purpose — the cadence re-runs the others. diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index f9296607032..b5c58f0bad9 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -105,7 +105,8 @@ extension PlatformWalletManager { /// /// While on, the SDK resubmits every unconfirmed send whose broadcast /// outcome went quiet to evonodes over DAPI — right after SPV reports it - /// uncertain, then as blocks arrive — and publishes each *change* of + /// uncertain, then every block for 24 blocks and every 10 after — and + /// publishes each *change* of /// verdict in `outgoingTransactionVerdicts` / `lastOutgoingTransactionProbe`. /// A send that settles or leaves the wallet is removed from the map. /// Resubmitting sends the same signed bytes: it can deliver a payment, it @@ -129,5 +130,8 @@ extension PlatformWalletManager { func handleOutgoingTransactionCleared(_ key: OutgoingTransactionKey) { guard !shutdownRequested, isConfigured else { return } outgoingTransactionVerdicts.removeValue(forKey: key) + if lastOutgoingTransactionProbe?.key == key { + lastOutgoingTransactionProbe = nil + } } } From 3c5e1a47b7c44ad24e16e8f2b31d6ef6ccecda8a Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 19:44:58 +0300 Subject: [PATCH 011/169] fix(platform-wallet): fifth review pass on the broadcast resolver - A dead root's verdict is re-published on every pass to every own send in its chain, so a send built on its change later is marked dead too. - Host events are queued under the state lock and delivered with no lock held; a callback that turns probing off or removes a wallet no longer runs under the resolver's lock. - Passes carry the generation they started under; a wallet removed or probing turned off meanwhile stops the pass before it probes or records. - Unresolved never replaces a decided verdict, Dead included. - A wallet goes idle when every root has a final verdict, and only if no wallet event woke it since the pass read it; any non-height wallet event (reorg, new record) wakes it. - A pass uses the wallet's own synced height when no height was reported. - The running-set guard is disarmed instead of leaked with mem::forget. - FFI: a reason with an interior NUL is delivered without it, not dropped. - Docs: "cleared" means "drop the verdict", not "the send settled". Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/event_handler.rs | 23 +- packages/rs-platform-wallet/src/events.rs | 7 +- .../src/wallet/core/broadcast_resolver.rs | 770 ++++++++++++------ .../PlatformWalletManager.swift | 4 +- .../PlatformWalletManagerBroadcastProbe.swift | 19 +- 5 files changed, 544 insertions(+), 279 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/event_handler.rs b/packages/rs-platform-wallet-ffi/src/event_handler.rs index c7d01f0972a..c6850261304 100644 --- a/packages/rs-platform-wallet-ffi/src/event_handler.rs +++ b/packages/rs-platform-wallet-ffi/src/event_handler.rs @@ -55,8 +55,10 @@ pub type DpnsMarketplaceSyncCompletedFn = unsafe extern "C" fn( pub const OUTGOING_PROBE_VERDICT_ACCEPTED: u8 = 0; pub const OUTGOING_PROBE_VERDICT_DEAD: u8 = 1; pub const OUTGOING_PROBE_VERDICT_UNRESOLVED: u8 = 2; -/// Not a verdict: the send is no longer unconfirmed (it settled or left the -/// wallet); drop whatever was kept for its earlier verdict. `reason` is null. +/// Not a verdict: drop whatever was kept for the send's earlier verdict. Sent +/// when the send settled or left the wallet, when its wallet was removed, and +/// for every send when probing is turned off — so it does not mean the send +/// settled. `reason` is null. pub const OUTGOING_PROBE_VERDICT_CLEARED: u8 = 3; /// A verdict on an unconfirmed send whose broadcast outcome was unknown. @@ -323,7 +325,9 @@ impl PlatformEventHandler for FFIEventHandler { (OUTGOING_PROBE_VERDICT_UNRESOLVED, Some(reason)) } }; - let reason = reason.and_then(|reason| std::ffi::CString::new(reason.as_str()).ok()); + // A reason is a node's text: strip interior NULs rather than lose it. + let reason = + reason.and_then(|reason| std::ffi::CString::new(reason.replace('\0', "")).ok()); let txid_bytes: &[u8] = txid.as_ref(); unsafe { callback( @@ -740,6 +744,13 @@ mod outgoing_probe_callback_tests { reason: "no quorum".to_string(), }, ); + with_callback.on_outgoing_transaction_probed( + &wallet, + &txid, + &ProbeVerdict::Unresolved { + reason: "odd\0node".to_string(), + }, + ); with_callback.on_outgoing_transaction_cleared(&wallet, &txid); // A host without the slot receives nothing. handler(None).on_outgoing_transaction_probed(&wallet, &txid, &ProbeVerdict::Accepted); @@ -763,6 +774,12 @@ mod outgoing_probe_callback_tests { OUTGOING_PROBE_VERDICT_UNRESOLVED, Some("no quorum".to_string()) ), + ( + wallet, + wire, + OUTGOING_PROBE_VERDICT_UNRESOLVED, + Some("oddnode".to_string()) + ), (wallet, wire, OUTGOING_PROBE_VERDICT_CLEARED, None), ] ); diff --git a/packages/rs-platform-wallet/src/events.rs b/packages/rs-platform-wallet/src/events.rs index c5d1708bd8a..828f6f5a909 100644 --- a/packages/rs-platform-wallet/src/events.rs +++ b/packages/rs-platform-wallet/src/events.rs @@ -65,9 +65,10 @@ pub trait PlatformEventHandler: EventHandler { ) { } - /// Fired when a send that had a published verdict is no longer an - /// unconfirmed root — it settled (block or InstantSend lock) or left the - /// wallet. Hosts drop any state they kept for the verdict. + /// Fired when the host must drop a send's published verdict: the send + /// settled (block or InstantSend lock) or left the wallet, its wallet was + /// removed, or probing was turned off (then for every send). It does not + /// mean the send settled. Hosts drop any state they kept for the verdict. /// /// Default impl is a no-op so existing handlers don't have to care. fn on_outgoing_transaction_cleared(&self, _wallet_id: &WalletId, _txid: &Txid) {} diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 3ff2be65326..88313aaeb67 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -21,26 +21,30 @@ //! What is published: verdicts per **own send**, which is what the host //! shows. A root's verdict goes to the root when it is the wallet's send; a //! dead root also takes every own send built on it (spending a dead -//! transaction is dead too). A root found in a block that the wallet has not +//! transaction is dead too), including sends built on it after it was found +//! dead. A root found in a block that the wallet has not //! seen yet counts as settled, so the sends built on it are probed next. -//! Only changes are published — Unresolved never replaces Accepted or Mined, -//! and Accepted and Mined count as the same — and a send that settles or -//! leaves the wallet after a verdict is published as cleared, as are all of a -//! removed wallet's sends and, when probing is turned off, all of them. +//! Only changes are published — Unresolved never replaces a decided verdict, +//! and Accepted and Mined count as the same. The host is told to drop a +//! send's verdict (cleared) when the send settles or leaves the wallet, when +//! its wallet is removed and, for every send, when probing is turned off; +//! cleared says only "forget the verdict", not that the send settled. Events +//! are queued under the state lock and delivered with no lock held. //! //! When: once as soon as dash-spv reports an `Uncertain` broadcast (the root //! of that transaction's chain), then on each advance of the wallet's synced //! height that follows the tip — steps of more than [`CATCH_UP_STEP`] blocks, //! and the first step after launch, are catch-up and skipped — every block //! for [`EVERY_BLOCK_WINDOW`] blocks and every [`SLOW_INTERVAL`] blocks after. -//! A wallet whose last pass found nothing unconfirmed is idle and not scanned -//! again until a new transaction or an `Uncertain` result wakes it. One +//! A wallet whose last pass had nothing left to probe is idle and not scanned +//! again until another wallet event (a new transaction, a block that touched +//! its records, a reorg) or an `Uncertain` result wakes it. One //! worker per wallet drains a queue of merged requests. `Dead` and `Mined` end //! the probing of a root; `Accepted` does not — a mempool is not settlement. //! Probes run only while the SPV client delivers blocks, i.e. while the app is //! in the foreground. -use std::collections::{BTreeSet, HashMap, HashSet}; +use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; use std::future::Future; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, Weak}; @@ -50,6 +54,7 @@ use dash_spv::sync::SyncEvent; use dash_spv::{BroadcastResult, EventHandler}; use dashcore::{Transaction, Txid}; use key_wallet::transaction_checking::TransactionContext; +use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use key_wallet_manager::{WalletEvent, WalletId, WalletManager}; use tokio::sync::RwLock; use tokio::task::JoinHandle; @@ -298,9 +303,9 @@ impl ProbeSchedule { } /// A root's final verdict: nothing is left to ask. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] enum Final { - Dead, + Dead { reason: String }, Mined, } @@ -316,34 +321,85 @@ fn same_for_host(a: &ProbeVerdict, b: &ProbeVerdict) -> bool { ) | ( ProbeVerdict::Unresolved { .. }, ProbeVerdict::Unresolved { .. } - ) - ) || a == b + ) | (ProbeVerdict::Dead { .. }, ProbeVerdict::Dead { .. }) + ) +} + +/// What a pass has to tell the host, per own send. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum ResolverEvent { + /// A send's verdict changed. + Verdict(Txid, ProbeVerdict), + /// The host must drop the verdict it holds for a send: the send settled + /// or left the wallet, its wallet was removed, or probing was turned off. + /// Not a statement that the send settled. + Cleared(Txid), +} + +/// One event waiting to reach the host, with what the log line needs. +#[derive(Debug, Clone)] +struct Outgoing { + wallet_id: WalletId, + event: ResolverEvent, + height: Option, + trigger: &'static str, +} + +/// Which clears and removals a pass started under. A pass compares it with +/// the current one under the state lock before recording anything: a wallet +/// removed, or probing turned off, since the pass read the wallet makes it +/// stop instead of rebuilding state that was just dropped. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct Generation { + all: u64, + wallet: u64, } /// Bookkeeping shared by every pass. Schedules and final verdicts are per /// probed root; published verdicts are per own send, which is what the host /// shows. Everything for a transaction is kept while it stays unsettled in the /// wallet, and dropped once it settles or leaves. +/// +/// Events for the host are queued in `outbox` under the same lock that records +/// them, so they reach the host in the order the state changed; they are +/// delivered afterwards with no lock held (see [`deliver`]). #[derive(Debug, Default)] pub(crate) struct ResolverState { schedules: HashMap<(WalletId, Txid), ProbeSchedule>, finished: HashMap<(WalletId, Txid), Final>, published: HashMap<(WalletId, Txid), ProbeVerdict>, -} - -/// What a pass has to tell the host, per own send. -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) enum ResolverEvent { - /// A send's verdict changed. - Verdict(Txid, ProbeVerdict), - /// A send that had a published verdict settled or left the wallet. - Cleared(Txid), + generation_all: u64, + generations: HashMap, + outbox: VecDeque, } impl ResolverState { - /// Drop everything kept for `wallet_id`; returns the sends the host had a - /// verdict for, which it must now be told are cleared. - fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { + pub(crate) fn generation(&self, wallet_id: &WalletId) -> Generation { + Generation { + all: self.generation_all, + wallet: self.generations.get(wallet_id).copied().unwrap_or(0), + } + } + + fn enqueue( + &mut self, + wallet_id: WalletId, + event: ResolverEvent, + height: Option, + trigger: &'static str, + ) { + self.outbox.push_back(Outgoing { + wallet_id, + event, + height, + trigger, + }); + } + + /// Drop everything kept for `wallet_id` and queue a clear for every send + /// the host had a verdict for. Passes that started before are stopped. + fn forget_wallet(&mut self, wallet_id: &WalletId) { + *self.generations.entry(*wallet_id).or_insert(0) += 1; self.schedules.retain(|(wallet, _), _| wallet != wallet_id); self.finished.retain(|(wallet, _), _| wallet != wallet_id); let mut cleared: Vec = self @@ -354,31 +410,34 @@ impl ResolverState { .collect(); cleared.sort(); self.published.retain(|(wallet, _), _| wallet != wallet_id); - cleared + for txid in cleared { + self.enqueue(*wallet_id, ResolverEvent::Cleared(txid), None, "removed"); + } } - /// Drop everything; returns every send the host had a verdict for. - fn forget_all(&mut self) -> Vec<(WalletId, Txid)> { + /// Drop everything and queue a clear for every published send. Passes + /// that started before are stopped. + fn forget_all(&mut self) { + self.generation_all += 1; self.schedules.clear(); self.finished.clear(); let mut cleared: Vec<(WalletId, Txid)> = self.published.drain().map(|(key, _)| key).collect(); cleared.sort(); - cleared - } - - fn has_published(&self, wallet_id: &WalletId) -> bool { - self.published.keys().any(|(wallet, _)| wallet == wallet_id) + for (wallet_id, txid) in cleared { + self.enqueue(wallet_id, ResolverEvent::Cleared(txid), None, "disabled"); + } } /// Record `verdict` for `send`; returns whether the host must hear it. - /// Unresolved never replaces an Accepted or Mined verdict — one probe that - /// only met transport errors says nothing new. + /// Unresolved never replaces a decided verdict (Accepted, Mined, Dead) — + /// one probe that only met transport errors says nothing new. fn publish(&mut self, key: (WalletId, Txid), verdict: &ProbeVerdict) -> bool { let changed = match self.published.get(&key) { None => true, - Some(ProbeVerdict::Accepted | ProbeVerdict::Mined) - if matches!(verdict, ProbeVerdict::Unresolved { .. }) => + Some(previous) + if !matches!(previous, ProbeVerdict::Unresolved { .. }) + && matches!(verdict, ProbeVerdict::Unresolved { .. }) => { false } @@ -390,13 +449,8 @@ impl ResolverState { changed } - /// Whether `verdict` is still what is recorded for `send` — false once a - /// clear (probing off, wallet removed, send settled) has run since the - /// pass recorded it. - fn still_published(&self, key: &(WalletId, Txid), verdict: &ProbeVerdict) -> bool { - self.published - .get(key) - .is_some_and(|recorded| same_for_host(recorded, verdict)) + fn take_outbox(&mut self) -> Vec { + self.outbox.drain(..).collect() } } @@ -407,16 +461,28 @@ pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { previous.is_none_or(|previous| height.saturating_sub(previous) > CATCH_UP_STEP) } -/// One pass over one wallet at `height`: probe every due root and return what -/// to publish. The roots of every `forced` transaction's chain are due -/// regardless of their schedule (dash-spv has just reported it `Uncertain`). -/// `keep_going` is asked before each probe; once it says no (probing turned -/// off), the pass sends nothing more to the network. +/// What a pass leaves behind besides the queued events. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct PassOutcome { + /// The events this pass queued, in order. + pub events: Vec, + /// Nothing left to probe: no roots, or every root has a final verdict. + /// The wallet can go idle until something new happens. + pub nothing_to_probe: bool, +} + +/// One pass over one wallet at `height`: probe every due root, record and +/// queue what the host must hear. The roots of every `forced` transaction's +/// chain are due regardless of their schedule (dash-spv has just reported it +/// `Uncertain`). `started` is the [`Generation`] the caller read the wallet +/// under; once it is no longer current (wallet removed, probing turned off), +/// the pass records nothing more and sends nothing more to the network. /// /// A root's verdict is published for the wallet's own sends: the root itself /// when it is one, and — when the root is dead — every own send built on it, -/// since a transaction spending a dead one is dead too. A root found mined -/// counts as settled when picking roots, so its children are probed next. +/// on every pass, so a send built on a dead root's change later is covered +/// too. A root found mined counts as settled when picking roots, so its +/// children are probed next. pub(crate) async fn resolve_pass( state: &Mutex, probe: &dyn AcceptanceProbe, @@ -424,8 +490,13 @@ pub(crate) async fn resolve_pass( height: u32, views: &[OutgoingView], forced: &HashSet, - keep_going: &(dyn Fn() -> bool + Sync), -) -> Vec { + started: Generation, +) -> PassOutcome { + let trigger = if forced.is_empty() { + "height" + } else { + "uncertain" + }; // What the wallet itself still holds unsettled — what state is kept for. let present: HashSet = views.iter().map(|view| view.txid).collect(); let own_present: HashSet = views @@ -435,52 +506,91 @@ pub(crate) async fn resolve_pass( .collect(); let mut events = Vec::new(); + // Each locked stretch is its own block: a guard must not live across the + // probes' awaits. let mined: HashSet = { let mut guard = state.lock().expect("resolver state poisoned"); - let ResolverState { - schedules, - finished, - published, - } = &mut *guard; + if guard.generation(&wallet_id) != started { + return PassOutcome { + events, + nothing_to_probe: false, + }; + } + // Forget what settled or left the wallet; clear the sends the host had a + // verdict for. let is_gone = |wallet: &WalletId, txid: &Txid| *wallet == wallet_id && !present.contains(txid); - schedules.retain(|(wallet, txid), _| !is_gone(wallet, txid)); - finished.retain(|(wallet, txid), _| !is_gone(wallet, txid)); - let mut cleared: Vec = published + guard + .schedules + .retain(|(wallet, txid), _| !is_gone(wallet, txid)); + guard + .finished + .retain(|(wallet, txid), _| !is_gone(wallet, txid)); + let mut cleared: Vec = guard + .published .keys() .filter(|(wallet, txid)| *wallet == wallet_id && !own_present.contains(txid)) .map(|(_, txid)| *txid) .collect(); cleared.sort(); - published.retain(|(wallet, txid), _| *wallet != wallet_id || own_present.contains(txid)); - events.extend(cleared.into_iter().map(ResolverEvent::Cleared)); + guard + .published + .retain(|(wallet, txid), _| *wallet != wallet_id || own_present.contains(txid)); + for txid in cleared { + guard.enqueue( + wallet_id, + ResolverEvent::Cleared(txid), + Some(height), + trigger, + ); + events.push(ResolverEvent::Cleared(txid)); + } - finished + guard + .finished .iter() .filter(|((wallet, _), result)| *wallet == wallet_id && **result == Final::Mined) .map(|((_, txid), _)| *txid) .collect() }; - let graph = ChainGraph::new(views, &mined); + let roots = graph.roots(); let forced_roots: BTreeSet = forced .iter() .flat_map(|txid| graph.roots_of(txid)) .collect(); - let due: Vec<(Txid, Transaction)> = { + + let due = { let mut guard = state.lock().expect("resolver state poisoned"); - let ResolverState { - schedules, - finished, - .. - } = &mut *guard; + // A dead root takes every own send built on it — including ones built on + // its change after it was found dead. + for view in &roots { + if let Some(Final::Dead { reason }) = + guard.finished.get(&(wallet_id, view.txid)).cloned() + { + let verdict = ProbeVerdict::Dead { reason }; + for send in graph.own_in_chain(&view.txid) { + if guard.publish((wallet_id, send), &verdict) { + guard.enqueue( + wallet_id, + ResolverEvent::Verdict(send, verdict.clone()), + Some(height), + trigger, + ); + events.push(ResolverEvent::Verdict(send, verdict.clone())); + } + } + } + } + let mut due = Vec::new(); - for view in graph.roots() { + for view in &roots { let key = (wallet_id, view.txid); - if finished.contains_key(&key) { + if guard.finished.contains_key(&key) { continue; } - let schedule = schedules + let schedule = guard + .schedules .entry(key) .or_insert_with(|| ProbeSchedule::new(height)); if forced_roots.contains(&view.txid) || schedule.is_due(height) { @@ -492,20 +602,30 @@ pub(crate) async fn resolve_pass( }; for (root, transaction) in due { - if !keep_going() { + if state + .lock() + .expect("resolver state poisoned") + .generation(&wallet_id) + != started + { break; } let verdict = probe.probe(&transaction).await; let key = (wallet_id, root); let mut guard = state.lock().expect("resolver state poisoned"); - // A pass that ran meanwhile may have found this root settled or gone - // and dropped it; a verdict recorded now would resurrect it. - if !guard.schedules.contains_key(&key) { + // Stopped meanwhile, or the root dropped by a newer pass: recording + // now would resurrect state that was cleared. + if guard.generation(&wallet_id) != started || !guard.schedules.contains_key(&key) { continue; } - match verdict { - ProbeVerdict::Dead { .. } => { - guard.finished.insert(key, Final::Dead); + match &verdict { + ProbeVerdict::Dead { reason } => { + guard.finished.insert( + key, + Final::Dead { + reason: reason.clone(), + }, + ); } ProbeVerdict::Mined => { guard.finished.insert(key, Final::Mined); @@ -521,11 +641,92 @@ pub(crate) async fn resolve_pass( }; for send in sends { if guard.publish((wallet_id, send), &verdict) { + guard.enqueue( + wallet_id, + ResolverEvent::Verdict(send, verdict.clone()), + Some(height), + trigger, + ); events.push(ResolverEvent::Verdict(send, verdict.clone())); } } } - events + + let guard = state.lock().expect("resolver state poisoned"); + let nothing_to_probe = roots + .iter() + .all(|view| guard.finished.contains_key(&(wallet_id, view.txid))); + PassOutcome { + events, + nothing_to_probe, + } +} + +/// Deliver queued events to the host, in order, with no lock held while a +/// host callback runs — a callback may turn probing off or remove a wallet, +/// which queues more events and returns at once; the running delivery sends +/// those too. `delivering` makes sure only one thread delivers at a time. +fn deliver( + state: &Mutex, + delivering: &AtomicBool, + events: &Weak, +) { + loop { + if delivering.swap(true, Ordering::SeqCst) { + return; // the thread already delivering will send ours + } + let events = events.upgrade(); + loop { + let batch = state.lock().expect("resolver state poisoned").take_outbox(); + if batch.is_empty() { + break; + } + for item in batch { + log_outgoing(&item); + if let Some(events) = &events { + match &item.event { + ResolverEvent::Verdict(txid, verdict) => { + events.on_outgoing_transaction_probed(&item.wallet_id, txid, verdict) + } + ResolverEvent::Cleared(txid) => { + events.on_outgoing_transaction_cleared(&item.wallet_id, txid) + } + } + } + } + } + delivering.store(false, Ordering::SeqCst); + // Something queued between the last empty check and the flag reset + // would otherwise wait for the next delivery. + if state + .lock() + .expect("resolver state poisoned") + .outbox + .is_empty() + { + return; + } + } +} + +fn log_outgoing(item: &Outgoing) { + match &item.event { + ResolverEvent::Verdict(txid, verdict) => tracing::info!( + wallet_id = %hex::encode(item.wallet_id), + txid = %txid, + height = ?item.height, + trigger = item.trigger, + ?verdict, + "broadcast probe: verdict for an unconfirmed send" + ), + ResolverEvent::Cleared(txid) => tracing::info!( + wallet_id = %hex::encode(item.wallet_id), + txid = %txid, + height = ?item.height, + trigger = item.trigger, + "broadcast probe: verdict cleared" + ), + } } /// Admission gate for probe tasks. A probe only reads the wallet and @@ -586,10 +787,23 @@ impl ProbeTasks { struct PassQueue { running: HashSet, pending: HashMap, - /// Wallets whose last pass found nothing unconfirmed and published - /// nothing: height advances skip them (no scan of every record per block) - /// until a new transaction or an `Uncertain` result wakes them. + /// Wallets whose last pass had nothing to probe: height advances skip + /// them (no scan of every record per block) until something wakes them. idle: HashSet, + /// Bumped by every wake-up, so a pass that read the wallet before one + /// happened does not mark it idle after it. + wakes: HashMap, +} + +impl PassQueue { + fn wake(&mut self, wallet_id: &WalletId) { + self.idle.remove(wallet_id); + *self.wakes.entry(*wallet_id).or_insert(0) += 1; + } + + fn wakes(&self, wallet_id: &WalletId) -> u64 { + self.wakes.get(wallet_id).copied().unwrap_or(0) + } } #[derive(Default)] @@ -598,19 +812,20 @@ struct PendingPass { forced: HashSet, } -/// Runs probe passes off the wallet-event fan-out and publishes each change of +/// Runs probe passes off the wallet-event fan-out and delivers each change of /// a send's verdict as [`PlatformEventHandler::on_outgoing_transaction_probed`], -/// and each send that no longer needs one as +/// and each verdict the host must drop as /// [`PlatformEventHandler::on_outgoing_transaction_cleared`]. /// /// Off until the host turns it on with [`set_enabled`](Self::set_enabled): a /// probe sends the signed transaction to evonodes over DAPI, which is a /// product decision, not something a wallet should start doing on upgrade. pub(crate) struct BroadcastResolver { - enabled: Arc, + enabled: AtomicBool, probe: Arc, wallet_manager: Arc>>, state: Arc>, + delivering: Arc, /// Every wallet seen in an event, with its last synced height if one has /// been reported: an SPV-level `Uncertain` carries no wallet id, so it is /// queued for every known wallet. @@ -626,10 +841,11 @@ impl BroadcastResolver { wallet_manager: Arc>>, ) -> Self { Self { - enabled: Arc::new(AtomicBool::new(false)), + enabled: AtomicBool::new(false), probe, wallet_manager, state: Arc::new(Mutex::new(ResolverState::default())), + delivering: Arc::new(AtomicBool::new(false)), wallets: Mutex::new(HashMap::new()), passes: Arc::new(Mutex::new(PassQueue::default())), events: Mutex::new(Weak::new()), @@ -637,26 +853,28 @@ impl BroadcastResolver { } } - /// Where verdicts are published. Weak: the event manager holds this + /// Where verdicts are delivered. Weak: the event manager holds this /// resolver as one of its handlers. pub(crate) fn set_event_manager(&self, events: Weak) { *self.events.lock().expect("events mutex poisoned") = events; } + fn deliver(&self) { + let events = self.events.lock().expect("events mutex poisoned").clone(); + deliver(&self.state, &self.delivering, &events); + } + /// Turning probing off forgets every send and tells the host to drop every /// verdict it holds: with no passes running, nothing would ever clear them. - /// Done under the state lock, so no verdict a running pass recorded can - /// reach the host after its clear. + /// Passes still running stop before their next probe and record nothing. pub(crate) fn set_enabled(&self, enabled: bool) { let was = self.enabled.swap(enabled, Ordering::SeqCst); if was && !enabled { - let events = self.events.lock().expect("events mutex poisoned").upgrade(); - let mut state = self.state.lock().expect("resolver state poisoned"); - for (wallet_id, txid) in state.forget_all() { - if let Some(events) = &events { - events.on_outgoing_transaction_cleared(&wallet_id, &txid); - } - } + self.state + .lock() + .expect("resolver state poisoned") + .forget_all(); + self.deliver(); } } @@ -664,26 +882,27 @@ impl BroadcastResolver { self.enabled.load(Ordering::SeqCst) } - /// Forget a removed wallet — call after it has left the wallet manager, so - /// no pass can read it again: its queued pass, its entry and every verdict, - /// each of which the host is told is cleared. + /// Forget a removed wallet — call after it has left the wallet manager: + /// its queued pass, its entry and every verdict, each of which the host is + /// told to drop. A pass that read the wallet before the removal stops + /// before recording anything. pub(crate) fn wallet_removed(&self, wallet_id: &WalletId) { { let mut passes = self.passes.lock().expect("pass queue poisoned"); passes.pending.remove(wallet_id); - passes.idle.remove(wallet_id); + // A wake, not a removal of the counter: a pass still running for + // the wallet must see it changed and not mark the wallet idle. + passes.wake(wallet_id); } self.wallets .lock() .expect("wallets mutex poisoned") .remove(wallet_id); - let events = self.events.lock().expect("events mutex poisoned").upgrade(); - let mut state = self.state.lock().expect("resolver state poisoned"); - for txid in state.forget_wallet(wallet_id) { - if let Some(events) = &events { - events.on_outgoing_transaction_cleared(wallet_id, &txid); - } - } + self.state + .lock() + .expect("resolver state poisoned") + .forget_wallet(wallet_id); + self.deliver(); } /// Stop admitting passes and stop the running ones. Idempotent. @@ -697,7 +916,7 @@ impl BroadcastResolver { { let mut passes = self.passes.lock().expect("pass queue poisoned"); if force.is_some() { - passes.idle.remove(&wallet_id); + passes.wake(&wallet_id); } else if passes.idle.contains(&wallet_id) { return; } @@ -712,44 +931,51 @@ impl BroadcastResolver { let probe = Arc::clone(&self.probe); let state = Arc::clone(&self.state); let passes = Arc::clone(&self.passes); - let enabled = Arc::clone(&self.enabled); + let delivering = Arc::clone(&self.delivering); let events = self.events.lock().expect("events mutex poisoned").clone(); self.tasks.spawn(async move { - let running = Running { + let mut running = Running { passes: Arc::clone(&passes), wallet_id, + armed: true, }; loop { // Take the next request, or stop — under the same lock a // request_pass would take, so a request can never land between // "queue empty" and "no longer running" and be left unserved. - // Probing turned off meanwhile: drop what is queued. - let next = { + let (next, wakes) = { let mut queue = passes.lock().expect("pass queue poisoned"); let next = queue.pending.remove(&wallet_id); - let next = next.filter(|_| enabled.load(Ordering::SeqCst)); if next.is_none() { queue.running.remove(&wallet_id); } - next + (next, queue.wakes(&wallet_id)) }; let Some(PendingPass { height, forced }) = next else { // Already left the running set under the lock; the guard // must not remove a marker a new worker may have set since. - std::mem::forget(running); + running.armed = false; break; }; + let started = state + .lock() + .expect("resolver state poisoned") + .generation(&wallet_id); // Read under the lock, probe without it: a probe is network I/O. - let views = { + let read = { let manager = wallet_manager.read().await; - manager.get_wallet_info(&wallet_id).map(collect_views) + manager.get_wallet_info(&wallet_id).map(|info| { + // A request queued before any height was reported + // carries 0; the wallet's own synced height is better. + ( + collect_views(info), + height.max(info.core_wallet.synced_height()), + ) + }) }; - let Some(views) = views else { - // Removed without wallet_removed having run yet: nothing - // to probe; wallet_removed does the clearing. - continue; + let Some((views, height)) = read else { + continue; // removed; wallet_removed does the clearing }; - let keep_going = || enabled.load(Ordering::SeqCst); let outcome = resolve_pass( &state, probe.as_ref(), @@ -757,97 +983,36 @@ impl BroadcastResolver { height, &views, &forced, - &keep_going, + started, ) .await; - let nothing_left = views.iter().all(|view| !view.spends_own_coins) - && !state - .lock() - .expect("resolver state poisoned") - .has_published(&wallet_id); - if nothing_left { - passes - .lock() - .expect("pass queue poisoned") - .idle - .insert(wallet_id); + if outcome.nothing_to_probe { + let mut queue = passes.lock().expect("pass queue poisoned"); + if queue.wakes(&wallet_id) == wakes { + queue.idle.insert(wallet_id); + } } - publish( - &state, - &events, - wallet_id, - height, - !forced.is_empty(), - &outcome, - ); + deliver(&state, &delivering, &events); } }); } } -/// Hand a pass's outcome to the host and the log — under the state lock, and -/// only verdicts still recorded, so a clear that ran after the pass recorded a -/// verdict (probing off, wallet removed) always wins. -fn publish( - state: &Mutex, - events: &Weak, - wallet_id: WalletId, - height: u32, - forced: bool, - outcome: &[ResolverEvent], -) { - if outcome.is_empty() { - return; - } - let events = events.upgrade(); - // What started this pass: dash-spv's Uncertain result, or an advance of - // the wallet's synced height. - let trigger = if forced { "uncertain" } else { "height" }; - let guard = state.lock().expect("resolver state poisoned"); - for event in outcome { - match event { - ResolverEvent::Verdict(txid, verdict) => { - if !guard.still_published(&(wallet_id, *txid), verdict) { - continue; - } - tracing::info!( - wallet_id = %hex::encode(wallet_id), - txid = %txid, - height, - trigger, - ?verdict, - "broadcast probe: verdict for an unconfirmed send" - ); - if let Some(events) = &events { - events.on_outgoing_transaction_probed(&wallet_id, txid, verdict); - } - } - ResolverEvent::Cleared(txid) => { - tracing::info!( - wallet_id = %hex::encode(wallet_id), - txid = %txid, - height, - "broadcast probe: send no longer unconfirmed, verdict cleared" - ); - if let Some(events) = &events { - events.on_outgoing_transaction_cleared(&wallet_id, txid); - } - } - } - } -} - /// Takes a wallet's worker out of the running set if it ends without reaching /// its own exit — an abort at shutdown — so a later request is not refused -/// forever. Forgotten on the normal exit, which leaves the set under the queue +/// forever. Disarmed on the normal exit, which leaves the set under the queue /// lock itself. struct Running { passes: Arc>, wallet_id: WalletId, + armed: bool, } impl Drop for Running { fn drop(&mut self) { + if !self.armed { + return; + } if let Ok(mut passes) = self.passes.lock() { passes.running.remove(&self.wallet_id); } @@ -857,39 +1022,30 @@ impl Drop for Running { impl EventHandler for BroadcastResolver { fn on_wallet_event(&self, event: &WalletEvent) { let wallet_id = event.wallet_id(); - match event { - WalletEvent::SyncHeightAdvanced { height, .. } => { - let previous = self - .wallets - .lock() - .expect("wallets mutex poisoned") - .insert(wallet_id, Some(*height)) - .flatten(); - if self.is_enabled() && !is_catch_up_step(previous, *height) { - self.request_pass(wallet_id, *height, None); - } - } - WalletEvent::TransactionDetected { .. } => { - // A new transaction may be something to probe: wake the wallet. - self.wallets - .lock() - .expect("wallets mutex poisoned") - .entry(wallet_id) - .or_insert(None); - self.passes - .lock() - .expect("pass queue poisoned") - .idle - .remove(&wallet_id); - } - _ => { - self.wallets - .lock() - .expect("wallets mutex poisoned") - .entry(wallet_id) - .or_insert(None); + if let WalletEvent::SyncHeightAdvanced { height, .. } = event { + let previous = self + .wallets + .lock() + .expect("wallets mutex poisoned") + .insert(wallet_id, Some(*height)) + .flatten(); + if self.is_enabled() && !is_catch_up_step(previous, *height) { + self.request_pass(wallet_id, *height, None); } + return; } + // Anything else that happens to the wallet — a new transaction, a + // block that changed its records, a reorg, a sweep — may leave + // something to probe: wake it. + self.wallets + .lock() + .expect("wallets mutex poisoned") + .entry(wallet_id) + .or_insert(None); + self.passes + .lock() + .expect("pass queue poisoned") + .wake(&wallet_id); } fn on_sync_event(&self, event: &SyncEvent) { @@ -981,10 +1137,6 @@ mod tests { HashSet::new() } - fn always() -> impl Fn() -> bool + Sync { - || true - } - // ---- merge_records ------------------------------------------------------ fn facts(n: u8, settled: bool, own: bool, transaction: &Transaction) -> RecordFacts<'_> { @@ -1173,7 +1325,32 @@ mod tests { height: u32, views: &[OutgoingView], ) -> Vec { - resolve_pass(state, probe, wallet(), height, views, &none(), &always()).await + run(state, probe, wallet(), height, views, &none()) + .await + .events + } + + /// A pass started under the current generation, as the worker runs it. + async fn run( + state: &Mutex, + probe: &dyn AcceptanceProbe, + wallet_id: WalletId, + height: u32, + views: &[OutgoingView], + forced: &HashSet, + ) -> PassOutcome { + let started = state.lock().expect("state").generation(&wallet_id); + resolve_pass(state, probe, wallet_id, height, views, forced, started).await + } + + fn outbox(state: &Mutex) -> Vec { + state + .lock() + .expect("state") + .take_outbox() + .into_iter() + .map(|item| item.event) + .collect() } #[tokio::test] @@ -1251,14 +1428,13 @@ mod tests { let views = [send(1, &[outpoint(90, 0)])]; pass(&state, &probe, 100, &views).await; - resolve_pass( + run( &state, &probe, wallet(), 100, &views, &HashSet::from([txid(1)]), - &always(), ) .await; @@ -1274,31 +1450,34 @@ mod tests { let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; pass(&state, &probe, 100, &views).await; - resolve_pass( + run( &state, &probe, wallet(), 100, &views, &HashSet::from([txid(2)]), - &always(), ) .await; assert_eq!(probe.probed(), vec![txid(1), txid(1)]); } - /// Probing turned off mid-pass: the pass sends nothing more to the network. + /// Probing turned off (or the wallet removed) after the worker read the + /// wallet: the pass sends nothing to the network and records nothing. #[tokio::test] - async fn should_stop_probing_once_told_to() { + async fn should_do_nothing_in_a_pass_started_before_a_clear() { let state = Mutex::new(ResolverState::default()); let probe = ScriptedProbe::new(&[]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])]; + let started = state.lock().expect("state").generation(&wallet()); + state.lock().expect("state").forget_all(); - let events = resolve_pass(&state, &probe, wallet(), 100, &views, &none(), &|| false).await; + let outcome = resolve_pass(&state, &probe, wallet(), 100, &views, &none(), started).await; assert!(probe.probed().is_empty()); - assert!(events.is_empty()); + assert!(outcome.events.is_empty()); + assert!(state.lock().expect("state").schedules.is_empty()); } /// A send that settled or left the wallet is forgotten, and the host is @@ -1326,7 +1505,7 @@ mod tests { let views = [send(1, &[outpoint(90, 0)])]; pass(&state, &probe, 100, &views).await; - resolve_pass(&state, &probe, [9u8; 32], 100, &[], &none(), &always()).await; + run(&state, &probe, [9u8; 32], 100, &[], &none()).await; assert_eq!(state.lock().expect("state").schedules.len(), 1); } @@ -1433,66 +1612,117 @@ mod tests { state: Arc::clone(&state), }; - let events = resolve_pass( + let outcome = run( &state, &probe, wallet(), 100, &[send(1, &[outpoint(90, 0)])], &none(), - &always(), ) .await; - assert!(events.is_empty()); + assert!(outcome.events.is_empty()); assert!(state.lock().expect("state").published.is_empty()); } + /// A send built on a dead root's change after the root was found dead is + /// dead too, and hears it without the root being asked again. + #[tokio::test] + async fn should_publish_a_dead_root_to_a_send_built_on_it_later() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), dead())]); + + pass(&state, &probe, 100, &[send(1, &[outpoint(90, 0)])]).await; + let later = pass( + &state, + &probe, + 101, + &[send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])], + ) + .await; + + assert_eq!(later, vec![ResolverEvent::Verdict(txid(2), dead())]); + assert_eq!(probe.probed(), vec![txid(1)]); + } + + /// A wallet whose every root has a final verdict has nothing to probe and + /// may go idle; one with a root still open may not. + #[tokio::test] + async fn should_report_nothing_to_probe_only_when_every_root_is_final() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), dead()), (txid(2), unresolved())]); + + let empty = run(&state, &probe, wallet(), 100, &[], &none()).await; + let dead_only = run( + &state, + &probe, + wallet(), + 100, + &[send(1, &[outpoint(90, 0)])], + &none(), + ) + .await; + let open = run( + &state, + &probe, + wallet(), + 101, + &[send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + &none(), + ) + .await; + + assert!(empty.nothing_to_probe); + assert!(dead_only.nothing_to_probe); + assert!(!open.nothing_to_probe); + } + // ---- ResolverState ------------------------------------------------------ #[tokio::test] - async fn should_forget_one_wallet_and_return_its_published_sends() { + async fn should_forget_one_wallet_and_queue_a_clear_for_its_published_sends() { let state = Mutex::new(ResolverState::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved()), (txid(2), unresolved())]); pass(&state, &probe, 100, &[send(1, &[outpoint(90, 0)])]).await; - resolve_pass( + run( &state, &probe, [9u8; 32], 100, &[send(2, &[outpoint(91, 0)])], &none(), - &always(), ) .await; + outbox(&state); - let cleared = state.lock().expect("state").forget_wallet(&wallet()); + state.lock().expect("state").forget_wallet(&wallet()); - assert_eq!(cleared, vec![txid(1)]); + assert_eq!(outbox(&state), vec![ResolverEvent::Cleared(txid(1))]); let guard = state.lock().expect("state"); assert_eq!(guard.published.len(), 1, "the other wallet is untouched"); assert_eq!(guard.schedules.len(), 1); } #[tokio::test] - async fn should_forget_everything_and_return_every_published_send() { + async fn should_forget_everything_and_queue_a_clear_for_every_published_send() { let state = Mutex::new(ResolverState::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved()), (txid(2), unresolved())]); pass(&state, &probe, 100, &[send(1, &[outpoint(90, 0)])]).await; - resolve_pass( + run( &state, &probe, [9u8; 32], 100, &[send(2, &[outpoint(91, 0)])], &none(), - &always(), ) .await; + outbox(&state); - let cleared = state.lock().expect("state").forget_all(); + state.lock().expect("state").forget_all(); - assert_eq!(cleared.len(), 2); + assert_eq!(outbox(&state).len(), 2); let guard = state.lock().expect("state"); assert!( guard.published.is_empty() && guard.schedules.is_empty() && guard.finished.is_empty() @@ -1513,19 +1743,29 @@ mod tests { assert!(!state.publish(key, &ProbeVerdict::Mined)); assert!(!state.publish(key, &ProbeVerdict::Accepted)); assert!(state.publish(key, &dead())); + assert!( + !state.publish(key, &unresolved()), + "Unresolved never replaces Dead either" + ); + assert!(!state.publish( + key, + &ProbeVerdict::Dead { + reason: "missing inputs".into() + } + )); } - /// A verdict recorded by a pass is published only while it is still what - /// the state holds — a clear that ran in between wins. - #[test] - fn should_hold_back_a_verdict_cleared_after_it_was_recorded() { - let mut state = ResolverState::default(); - let key = (wallet(), txid(1)); - state.publish(key, &ProbeVerdict::Accepted); - assert!(state.still_published(&key, &ProbeVerdict::Accepted)); + /// Events reach the host in the order they were queued, and a pass + /// queues them as it returns them. + #[tokio::test] + async fn should_queue_every_event_a_pass_returns_in_order() { + let state = Mutex::new(ResolverState::default()); + let probe = ScriptedProbe::new(&[(txid(1), dead())]); + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; - state.forget_all(); + let events = pass(&state, &probe, 100, &views).await; - assert!(!state.still_published(&key, &ProbeVerdict::Accepted)); + assert_eq!(outbox(&state), events); + assert!(outbox(&state).is_empty(), "taken once"); } } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift index e18f3d25390..198f17aafa1 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift @@ -457,7 +457,9 @@ public class PlatformWalletManager: ObservableObject { /// The latest broadcast-probe verdict per unconfirmed send (wallet + txid). /// Filled only while `setBroadcastProbeEnabled(true)` is on; an entry is - /// removed once its send settles or leaves the wallet. See + /// removed when its send settles or leaves the wallet, when its wallet is + /// deleted, and all entries when probing is turned off — a removal does + /// not mean the send settled. See /// `OutgoingTransactionProbeEvent`. @Published public internal(set) var outgoingTransactionVerdicts: [OutgoingTransactionKey: OutgoingTransactionProbeEvent] = [:] diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index b5c58f0bad9..a00aca0d8a8 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -103,12 +103,16 @@ func outgoingTransactionProbedCallback( extension PlatformWalletManager { /// Turn automatic probing of unconfirmed sends on or off (off by default). /// - /// While on, the SDK resubmits every unconfirmed send whose broadcast - /// outcome went quiet to evonodes over DAPI — right after SPV reports it - /// uncertain, then every block for 24 blocks and every 10 after — and - /// publishes each *change* of - /// verdict in `outgoingTransactionVerdicts` / `lastOutgoingTransactionProbe`. - /// A send that settles or leaves the wallet is removed from the map. + /// While on, the SDK resubmits to evonodes over DAPI the root of every + /// chain of unconfirmed sends — the first unsettled transaction the chain + /// depends on, which may be an incoming payment the wallet spent — right + /// after SPV reports a broadcast uncertain, then every block for 24 blocks + /// and every 10 after. It publishes each *change* of verdict for the + /// wallet's own sends in `outgoingTransactionVerdicts` / + /// `lastOutgoingTransactionProbe`. An entry is removed when its send + /// settles or leaves the wallet, when its wallet is deleted, and every + /// entry when probing is turned off; a removal does not mean the send + /// settled. /// Resubmitting sends the same signed bytes: it can deliver a payment, it /// can never create a second one. Nothing in the wallet changes. public func setBroadcastProbeEnabled(_ enabled: Bool) throws { @@ -125,7 +129,8 @@ extension PlatformWalletManager { lastOutgoingTransactionProbe = event } - /// The send settled or left the wallet: its verdict no longer applies. + /// Drop the send's verdict: the send settled or left the wallet, its wallet + /// was removed, or probing was turned off. Not a statement that it settled. @MainActor func handleOutgoingTransactionCleared(_ key: OutgoingTransactionKey) { guard !shutdownRequested, isConfigured else { return } From c17b5766c6326f0beedd13caf976d52d4c9151c4 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 20:00:49 +0300 Subject: [PATCH 012/169] fix(platform-wallet): sixth review pass on the broadcast resolver - Probing off is recorded in the resolver state under its lock; a pass that read its generation after the switch stops too, so a queued pass can no longer probe or publish while probing is off. Turning it off also drops queued passes, and any switch clears the idle set so turning it back on re-publishes what was cleared. - A pass delivers what it queued after each locked stretch, so a clear or a verdict does not wait for the probes of other roots. - Only wallet events that touched records wake an idle wallet; chainlocks that locked nothing (one per block per wallet) no longer do. - An Uncertain result is acted on only by the wallet that holds the transaction (a map lookup, no record walk); others skip the pass. - A worker that finds its wallet gone drops the wallet's entries, so a late event for a removed wallet cannot keep it registered. - FFI: set_broadcast_probe_enabled switches after releasing the handle storage lock (new BroadcastProbeSwitch), since turning probing off calls the host once per published send. - PassOutcome no longer duplicates the outbox; own_in_chain walks a parent-to-children index once instead of a fixpoint rescan. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet-ffi/src/manager.rs | 11 +- packages/rs-platform-wallet/src/lib.rs | 1 + .../src/manager/accessors.rs | 12 +- .../src/wallet/core/broadcast_resolver.rs | 467 +++++++++++++----- 4 files changed, 371 insertions(+), 120 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 1b9c2f2dfb0..df57a8c9a38 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -2160,10 +2160,13 @@ pub unsafe extern "C" fn platform_wallet_manager_set_broadcast_probe_enabled( handle: Handle, enabled: bool, ) -> PlatformWalletFFIResult { - let option = PLATFORM_WALLET_MANAGER_STORAGE.with_item(handle, |manager| { - manager.set_broadcast_probe_enabled(enabled); - }); - unwrap_option_or_return!(option); + // Released before switching: turning probing off calls the host once per + // published send, and a host that calls back into the FFI from there must + // not do so under the storage lock. + let option = PLATFORM_WALLET_MANAGER_STORAGE + .with_item(handle, |manager| manager.broadcast_probe_switch()); + let switch = unwrap_option_or_return!(option); + switch.set_enabled(enabled); PlatformWalletFFIResult::ok() } diff --git a/packages/rs-platform-wallet/src/lib.rs b/packages/rs-platform-wallet/src/lib.rs index 40da468297b..fee0880a01e 100644 --- a/packages/rs-platform-wallet/src/lib.rs +++ b/packages/rs-platform-wallet/src/lib.rs @@ -59,6 +59,7 @@ pub use spv::SpvRuntime; pub use wallet::asset_lock::manager::AssetLockManager; pub use wallet::asset_lock::tracked::{AssetLockStatus, TrackedAssetLock}; pub use wallet::asset_lock::AssetLockFunding; +pub use wallet::core::broadcast_resolver::BroadcastProbeSwitch; pub use wallet::core::WalletBalance; pub use wallet::core::{ CoreWallet, SignedCoreTransaction, ASSET_LOCK_FUNDING_SOURCES, SEND_FUNDING_SOURCES, diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index 4655df5babf..e51747a99cc 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -20,6 +20,7 @@ use crate::manager::platform_address_sync::PlatformAddressSyncManager; #[cfg(feature = "shielded")] use crate::manager::shielded_sync::ShieldedSyncManager; use crate::spv::SpvRuntime; +use crate::wallet::core::broadcast_resolver::BroadcastProbeSwitch; use crate::wallet::platform_wallet::WalletId; use crate::wallet::PlatformWallet; @@ -530,11 +531,20 @@ impl PlatformWalletManager

{ /// each change of a send's verdict is published as /// `on_outgoing_transaction_probed`, each send that no longer needs one /// as `on_outgoing_transaction_cleared`. Nothing in the wallet changes. - /// Off by default; turning it off clears every published verdict. + /// Off by default; turning it off clears every published verdict, and the + /// clears reach the host on the calling thread before this returns. pub fn set_broadcast_probe_enabled(&self, enabled: bool) { self.broadcast_resolver.set_enabled(enabled); } + /// A handle for [`set_broadcast_probe_enabled`](Self::set_broadcast_probe_enabled) + /// that outlives a borrow of the manager — for callers that hold a lock + /// around it, since turning probing off calls the host once per published + /// send before it returns. + pub fn broadcast_probe_switch(&self) -> BroadcastProbeSwitch { + BroadcastProbeSwitch(Arc::clone(&self.broadcast_resolver)) + } + /// Whether automatic probing of unconfirmed sends is on. pub fn is_broadcast_probe_enabled(&self) -> bool { self.broadcast_resolver.is_enabled() diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 88313aaeb67..7cbf429908a 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -37,8 +37,9 @@ //! and the first step after launch, are catch-up and skipped — every block //! for [`EVERY_BLOCK_WINDOW`] blocks and every [`SLOW_INTERVAL`] blocks after. //! A wallet whose last pass had nothing left to probe is idle and not scanned -//! again until another wallet event (a new transaction, a block that touched -//! its records, a reorg) or an `Uncertain` result wakes it. One +//! again until a wallet event that touched its records (a new or +//! InstantSend-locked transaction, a block or chainlock that changed records, a +//! sweep) or an `Uncertain` result for one of its transactions wakes it. One //! worker per wallet drains a queue of merged requests. `Dead` and `Mined` end //! the probing of a root; `Accepted` does not — a mempool is not settlement. //! Probes run only while the SPV client delivers blocks, i.e. while the app is @@ -162,6 +163,8 @@ pub(crate) struct ChainGraph<'a> { /// Unsettled for the purpose of picking roots: every view except those a /// probe found mined. unsettled: BTreeSet, + /// Unsettled transactions spending an output of each unsettled one. + children: HashMap>, } impl<'a> ChainGraph<'a> { @@ -171,12 +174,29 @@ impl<'a> ChainGraph<'a> { pub(crate) fn new(views: &'a [OutgoingView], mined: &HashSet) -> Self { let by_txid: HashMap = views.iter().map(|view| (view.txid, view)).collect(); - let unsettled = by_txid + let unsettled: BTreeSet = by_txid .keys() .filter(|txid| !mined.contains(*txid)) .copied() .collect(); - Self { by_txid, unsettled } + let mut children: HashMap> = HashMap::new(); + for txid in &unsettled { + let parents: BTreeSet = by_txid[txid] + .transaction + .input + .iter() + .map(|input| input.previous_output.txid) + .filter(|parent| unsettled.contains(parent)) + .collect(); + for parent in parents { + children.entry(parent).or_default().push(*txid); + } + } + Self { + by_txid, + unsettled, + children, + } } fn is_own(&self, txid: &Txid) -> bool { @@ -249,18 +269,14 @@ impl<'a> ChainGraph<'a> { /// and every unsettled send built on it, transitively. A dead root takes /// all of them with it. pub(crate) fn own_in_chain(&self, root: &Txid) -> BTreeSet { - let mut chain = BTreeSet::from([*root]); - loop { - let before = chain.len(); - for txid in &self.unsettled { - if !chain.contains(txid) - && self.by_txid[txid].spends_output_of(&|parent| chain.contains(parent)) - { - chain.insert(*txid); - } + let mut chain = BTreeSet::new(); + let mut frontier = vec![*root]; + while let Some(txid) = frontier.pop() { + if !chain.insert(txid) { + continue; } - if chain.len() == before { - break; + if let Some(children) = self.children.get(&txid) { + frontier.extend(children.iter().copied()); } } chain.into_iter().filter(|txid| self.is_own(txid)).collect() @@ -369,7 +385,13 @@ pub(crate) struct ResolverState { finished: HashMap<(WalletId, Txid), Final>, published: HashMap<(WalletId, Txid), ProbeVerdict>, generation_all: u64, + /// Kept for removed wallets too: a wallet re-added under the same id must + /// not match a generation read before its removal. generations: HashMap, + /// Probing is off. Set under this lock together with the clears, so a + /// pass started after probing was turned off stops as surely as one that + /// started before. + off: bool, outbox: VecDeque, } @@ -381,6 +403,11 @@ impl ResolverState { } } + /// Whether a pass started under `started` may still probe and record. + fn is_current(&self, wallet_id: &WalletId, started: Generation) -> bool { + !self.off && self.generation(wallet_id) == started + } + fn enqueue( &mut self, wallet_id: WalletId, @@ -461,11 +488,9 @@ pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { previous.is_none_or(|previous| height.saturating_sub(previous) > CATCH_UP_STEP) } -/// What a pass leaves behind besides the queued events. +/// What a pass leaves behind besides the events it queued. #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct PassOutcome { - /// The events this pass queued, in order. - pub events: Vec, /// Nothing left to probe: no roots, or every root has a final verdict. /// The wallet can go idle until something new happens. pub nothing_to_probe: bool, @@ -477,12 +502,16 @@ pub(crate) struct PassOutcome { /// `Uncertain`). `started` is the [`Generation`] the caller read the wallet /// under; once it is no longer current (wallet removed, probing turned off), /// the pass records nothing more and sends nothing more to the network. +/// `flush` delivers what is queued so far; the pass calls it with no lock +/// held after each locked stretch, so a clear or a verdict does not wait for +/// the probes of other roots. /// /// A root's verdict is published for the wallet's own sends: the root itself /// when it is one, and — when the root is dead — every own send built on it, /// on every pass, so a send built on a dead root's change later is covered /// too. A root found mined counts as settled when picking roots, so its /// children are probed next. +#[allow(clippy::too_many_arguments)] pub(crate) async fn resolve_pass( state: &Mutex, probe: &dyn AcceptanceProbe, @@ -491,6 +520,7 @@ pub(crate) async fn resolve_pass( views: &[OutgoingView], forced: &HashSet, started: Generation, + flush: &(dyn Fn() + Sync), ) -> PassOutcome { let trigger = if forced.is_empty() { "height" @@ -504,17 +534,16 @@ pub(crate) async fn resolve_pass( .filter(|view| view.spends_own_coins) .map(|view| view.txid) .collect(); + let stopped = PassOutcome { + nothing_to_probe: false, + }; - let mut events = Vec::new(); // Each locked stretch is its own block: a guard must not live across the // probes' awaits. let mined: HashSet = { let mut guard = state.lock().expect("resolver state poisoned"); - if guard.generation(&wallet_id) != started { - return PassOutcome { - events, - nothing_to_probe: false, - }; + if !guard.is_current(&wallet_id, started) { + return stopped; } // Forget what settled or left the wallet; clear the sends the host had a // verdict for. @@ -543,7 +572,6 @@ pub(crate) async fn resolve_pass( Some(height), trigger, ); - events.push(ResolverEvent::Cleared(txid)); } guard @@ -562,6 +590,9 @@ pub(crate) async fn resolve_pass( let due = { let mut guard = state.lock().expect("resolver state poisoned"); + if !guard.is_current(&wallet_id, started) { + return stopped; + } // A dead root takes every own send built on it — including ones built on // its change after it was found dead. for view in &roots { @@ -577,7 +608,6 @@ pub(crate) async fn resolve_pass( Some(height), trigger, ); - events.push(ResolverEvent::Verdict(send, verdict.clone())); } } } @@ -600,66 +630,68 @@ pub(crate) async fn resolve_pass( } due }; + flush(); for (root, transaction) in due { - if state + if !state .lock() .expect("resolver state poisoned") - .generation(&wallet_id) - != started + .is_current(&wallet_id, started) { - break; + return stopped; } let verdict = probe.probe(&transaction).await; let key = (wallet_id, root); - let mut guard = state.lock().expect("resolver state poisoned"); - // Stopped meanwhile, or the root dropped by a newer pass: recording - // now would resurrect state that was cleared. - if guard.generation(&wallet_id) != started || !guard.schedules.contains_key(&key) { - continue; - } - match &verdict { - ProbeVerdict::Dead { reason } => { - guard.finished.insert( - key, - Final::Dead { - reason: reason.clone(), - }, - ); + { + let mut guard = state.lock().expect("resolver state poisoned"); + if !guard.is_current(&wallet_id, started) { + return stopped; } - ProbeVerdict::Mined => { - guard.finished.insert(key, Final::Mined); + // Dropped by a newer pass meanwhile (it settled or left): recording + // now would resurrect state that was cleared. + if !guard.schedules.contains_key(&key) { + continue; } - _ => {} - } - let sends: BTreeSet = if matches!(verdict, ProbeVerdict::Dead { .. }) { - graph.own_in_chain(&root) - } else if graph.is_own(&root) { - BTreeSet::from([root]) - } else { - BTreeSet::new() - }; - for send in sends { - if guard.publish((wallet_id, send), &verdict) { - guard.enqueue( - wallet_id, - ResolverEvent::Verdict(send, verdict.clone()), - Some(height), - trigger, - ); - events.push(ResolverEvent::Verdict(send, verdict.clone())); + match &verdict { + ProbeVerdict::Dead { reason } => { + guard.finished.insert( + key, + Final::Dead { + reason: reason.clone(), + }, + ); + } + ProbeVerdict::Mined => { + guard.finished.insert(key, Final::Mined); + } + _ => {} + } + let sends: BTreeSet = if matches!(verdict, ProbeVerdict::Dead { .. }) { + graph.own_in_chain(&root) + } else if graph.is_own(&root) { + BTreeSet::from([root]) + } else { + BTreeSet::new() + }; + for send in sends { + if guard.publish((wallet_id, send), &verdict) { + guard.enqueue( + wallet_id, + ResolverEvent::Verdict(send, verdict.clone()), + Some(height), + trigger, + ); + } } } + flush(); } let guard = state.lock().expect("resolver state poisoned"); let nothing_to_probe = roots .iter() .all(|view| guard.finished.contains_key(&(wallet_id, view.txid))); - PassOutcome { - events, - nothing_to_probe, - } + PassOutcome { nothing_to_probe } } /// Deliver queued events to the host, in order, with no lock held while a @@ -809,9 +841,22 @@ impl PassQueue { #[derive(Default)] struct PendingPass { height: u32, + /// A height advance asked for this pass; otherwise only `Uncertain` + /// results did, and a wallet that holds none of `forced` is skipped. + by_height: bool, forced: HashSet, } +/// Whether any account of the wallet records `txid` — a map lookup per +/// account, not a walk over the records. +fn holds(info: &PlatformWalletInfo, txid: &Txid) -> bool { + info.core_wallet + .accounts + .all_accounts() + .iter() + .any(|account| account.transactions().contains_key(txid)) +} + /// Runs probe passes off the wallet-event fan-out and delivers each change of /// a send's verdict as [`PlatformEventHandler::on_outgoing_transaction_probed`], /// and each verdict the host must drop as @@ -828,8 +873,9 @@ pub(crate) struct BroadcastResolver { delivering: Arc, /// Every wallet seen in an event, with its last synced height if one has /// been reported: an SPV-level `Uncertain` carries no wallet id, so it is - /// queued for every known wallet. - wallets: Mutex>>, + /// queued for every known wallet. A worker that finds its wallet gone + /// drops the entry, so a late event for a removed wallet cannot keep it. + wallets: Arc>>>, passes: Arc>, events: Mutex>, tasks: ProbeTasks, @@ -844,9 +890,12 @@ impl BroadcastResolver { enabled: AtomicBool::new(false), probe, wallet_manager, - state: Arc::new(Mutex::new(ResolverState::default())), + state: Arc::new(Mutex::new(ResolverState { + off: true, + ..ResolverState::default() + })), delivering: Arc::new(AtomicBool::new(false)), - wallets: Mutex::new(HashMap::new()), + wallets: Arc::new(Mutex::new(HashMap::new())), passes: Arc::new(Mutex::new(PassQueue::default())), events: Mutex::new(Weak::new()), tasks: ProbeTasks::new(), @@ -866,16 +915,29 @@ impl BroadcastResolver { /// Turning probing off forgets every send and tells the host to drop every /// verdict it holds: with no passes running, nothing would ever clear them. - /// Passes still running stop before their next probe and record nothing. + /// Passes still running, and ones already queued, stop before their next + /// probe and record nothing. Either way every wallet leaves idle, so + /// turning probing back on re-publishes what was cleared. + /// + /// The clears are delivered on the calling thread before this returns. pub(crate) fn set_enabled(&self, enabled: bool) { - let was = self.enabled.swap(enabled, Ordering::SeqCst); - if was && !enabled { - self.state - .lock() - .expect("resolver state poisoned") - .forget_all(); - self.deliver(); + let changed = { + let mut state = self.state.lock().expect("resolver state poisoned"); + let was = self.enabled.swap(enabled, Ordering::SeqCst); + state.off = !enabled; + if was && !enabled { + state.forget_all(); + } + was != enabled + }; + if changed { + let mut passes = self.passes.lock().expect("pass queue poisoned"); + passes.idle.clear(); + if !enabled { + passes.pending.clear(); + } } + self.deliver(); } pub(crate) fn is_enabled(&self) -> bool { @@ -911,17 +973,18 @@ impl BroadcastResolver { } /// Queue a pass for `wallet_id`; start a worker for it unless one runs. - /// A height-driven request (no `force`) is dropped for an idle wallet. + /// A height-driven request (no `force`) is dropped for an idle wallet; a + /// forced one is queued, and the worker skips it cheaply if the wallet + /// does not hold the transaction. fn request_pass(&self, wallet_id: WalletId, height: u32, force: Option) { { let mut passes = self.passes.lock().expect("pass queue poisoned"); - if force.is_some() { - passes.wake(&wallet_id); - } else if passes.idle.contains(&wallet_id) { + if force.is_none() && passes.idle.contains(&wallet_id) { return; } let pending = passes.pending.entry(wallet_id).or_default(); pending.height = pending.height.max(height); + pending.by_height |= force.is_none(); pending.forced.extend(force); if !passes.running.insert(wallet_id) { return; // the running worker picks it up @@ -931,6 +994,7 @@ impl BroadcastResolver { let probe = Arc::clone(&self.probe); let state = Arc::clone(&self.state); let passes = Arc::clone(&self.passes); + let wallets = Arc::clone(&self.wallets); let delivering = Arc::clone(&self.delivering); let events = self.events.lock().expect("events mutex poisoned").clone(); self.tasks.spawn(async move { @@ -939,6 +1003,7 @@ impl BroadcastResolver { wallet_id, armed: true, }; + let flush = || deliver(&state, &delivering, &events); loop { // Take the next request, or stop — under the same lock a // request_pass would take, so a request can never land between @@ -951,7 +1016,12 @@ impl BroadcastResolver { } (next, queue.wakes(&wallet_id)) }; - let Some(PendingPass { height, forced }) = next else { + let Some(PendingPass { + height, + by_height, + forced, + }) = next + else { // Already left the running set under the lock; the guard // must not remove a marker a new worker may have set since. running.armed = false; @@ -965,17 +1035,44 @@ impl BroadcastResolver { let read = { let manager = wallet_manager.read().await; manager.get_wallet_info(&wallet_id).map(|info| { + // An `Uncertain` result is queued for every wallet; + // only the one holding the transaction acts on it. + let forced: HashSet = forced + .into_iter() + .filter(|txid| holds(info, txid)) + .collect(); + if !by_height && forced.is_empty() { + return None; + } // A request queued before any height was reported // carries 0; the wallet's own synced height is better. - ( - collect_views(info), - height.max(info.core_wallet.synced_height()), - ) + let height = height.max(info.core_wallet.synced_height()); + Some((collect_views(info), height, forced)) }) }; - let Some((views, height)) = read else { - continue; // removed; wallet_removed does the clearing + let (views, height, forced) = match read { + Some(Some(pass)) => pass, + Some(None) => continue, // not this wallet's transaction + None => { + // Removed (wallet_removed did the clearing), or an id a + // late event brought back after the removal. + wallets + .lock() + .expect("wallets mutex poisoned") + .remove(&wallet_id); + let mut queue = passes.lock().expect("pass queue poisoned"); + queue.idle.remove(&wallet_id); + queue.wakes.remove(&wallet_id); + continue; + } }; + if !forced.is_empty() { + passes + .lock() + .expect("pass queue poisoned") + .idle + .remove(&wallet_id); + } let outcome = resolve_pass( &state, probe.as_ref(), @@ -984,6 +1081,7 @@ impl BroadcastResolver { &views, &forced, started, + &flush, ) .await; if outcome.nothing_to_probe { @@ -992,12 +1090,25 @@ impl BroadcastResolver { queue.idle.insert(wallet_id); } } - deliver(&state, &delivering, &events); + flush(); } }); } } +/// A handle that turns automatic probing on or off, usable after the lock that +/// guards the manager is released: turning probing off delivers a clear per +/// published send to the host on the calling thread. +#[derive(Clone)] +pub struct BroadcastProbeSwitch(pub(crate) Arc); + +impl BroadcastProbeSwitch { + /// See [`PlatformWalletManager::set_broadcast_probe_enabled`](crate::PlatformWalletManager::set_broadcast_probe_enabled). + pub fn set_enabled(&self, enabled: bool) { + self.0.set_enabled(enabled); + } +} + /// Takes a wallet's worker out of the running set if it ends without reaching /// its own exit — an abort at shutdown — so a later request is not refused /// forever. Disarmed on the normal exit, which leaves the set under the queue @@ -1019,6 +1130,25 @@ impl Drop for Running { } } +/// Whether a wallet event may have left something new to probe. A chainlock +/// that locked nothing, or a block that only matured coins or moved balances, +/// does not — chainlocks arrive about once per block for every wallet. +fn may_leave_work(event: &WalletEvent) -> bool { + match event { + WalletEvent::TransactionDetected { .. } + | WalletEvent::TransactionInstantLocked { .. } + | WalletEvent::TransactionsSwept { .. } => true, + WalletEvent::BlockProcessed { + inserted, updated, .. + } => !inserted.is_empty() || !updated.is_empty(), + WalletEvent::ChainLockProcessed { + locked_transactions, + .. + } => locked_transactions.values().any(|txids| !txids.is_empty()), + WalletEvent::SyncHeightAdvanced { .. } => false, + } +} + impl EventHandler for BroadcastResolver { fn on_wallet_event(&self, event: &WalletEvent) { let wallet_id = event.wallet_id(); @@ -1034,18 +1164,17 @@ impl EventHandler for BroadcastResolver { } return; } - // Anything else that happens to the wallet — a new transaction, a - // block that changed its records, a reorg, a sweep — may leave - // something to probe: wake it. self.wallets .lock() .expect("wallets mutex poisoned") .entry(wallet_id) .or_insert(None); - self.passes - .lock() - .expect("pass queue poisoned") - .wake(&wallet_id); + if may_leave_work(event) { + self.passes + .lock() + .expect("pass queue poisoned") + .wake(&wallet_id); + } } fn on_sync_event(&self, event: &SyncEvent) { @@ -1325,9 +1454,8 @@ mod tests { height: u32, views: &[OutgoingView], ) -> Vec { - run(state, probe, wallet(), height, views, &none()) - .await - .events + run(state, probe, wallet(), height, views, &none()).await; + outbox(state) } /// A pass started under the current generation, as the worker runs it. @@ -1340,7 +1468,17 @@ mod tests { forced: &HashSet, ) -> PassOutcome { let started = state.lock().expect("state").generation(&wallet_id); - resolve_pass(state, probe, wallet_id, height, views, forced, started).await + resolve_pass( + state, + probe, + wallet_id, + height, + views, + forced, + started, + &|| {}, + ) + .await } fn outbox(state: &Mutex) -> Vec { @@ -1473,10 +1611,20 @@ mod tests { let started = state.lock().expect("state").generation(&wallet()); state.lock().expect("state").forget_all(); - let outcome = resolve_pass(&state, &probe, wallet(), 100, &views, &none(), started).await; + resolve_pass( + &state, + &probe, + wallet(), + 100, + &views, + &none(), + started, + &|| {}, + ) + .await; assert!(probe.probed().is_empty()); - assert!(outcome.events.is_empty()); + assert!(outbox(&state).is_empty()); assert!(state.lock().expect("state").schedules.is_empty()); } @@ -1612,7 +1760,7 @@ mod tests { state: Arc::clone(&state), }; - let outcome = run( + run( &state, &probe, wallet(), @@ -1622,7 +1770,7 @@ mod tests { ) .await; - assert!(outcome.events.is_empty()); + assert!(outbox(&state).is_empty()); assert!(state.lock().expect("state").published.is_empty()); } @@ -1755,17 +1903,106 @@ mod tests { )); } - /// Events reach the host in the order they were queued, and a pass - /// queues them as it returns them. + /// Probing off: a pass that read its generation after the switch stops + /// as surely as one that started before it. #[tokio::test] - async fn should_queue_every_event_a_pass_returns_in_order() { + async fn should_do_nothing_while_probing_is_off() { + let state = Mutex::new(ResolverState { + off: true, + ..ResolverState::default() + }); + let probe = ScriptedProbe::new(&[]); + + run( + &state, + &probe, + wallet(), + 100, + &[send(1, &[outpoint(90, 0)])], + &none(), + ) + .await; + + assert!(probe.probed().is_empty()); + assert!(outbox(&state).is_empty()); + assert!(state.lock().expect("state").schedules.is_empty()); + } + + /// A probe that records what the host had been told by the time it ran. + struct SnapshotProbe { + delivered: Arc>>, + seen: StdMutex>, + } + + #[async_trait] + impl AcceptanceProbe for SnapshotProbe { + async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { + *self.seen.lock().expect("seen") = self.delivered.lock().expect("delivered").clone(); + unresolved() + } + } + + /// A clear reaches the host before the pass goes out to the network, not + /// after every root's probe. + #[tokio::test] + async fn should_deliver_clears_before_probing() { let state = Mutex::new(ResolverState::default()); - let probe = ScriptedProbe::new(&[(txid(1), dead())]); - let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; + pass( + &state, + &ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]), + 100, + &[send(1, &[outpoint(90, 0)])], + ) + .await; + let delivered = Arc::new(StdMutex::new(Vec::new())); + let probe = SnapshotProbe { + delivered: Arc::clone(&delivered), + seen: StdMutex::new(Vec::new()), + }; + let flush = || { + let batch = outbox(&state); + delivered.lock().expect("delivered").extend(batch); + }; + let started = state.lock().expect("state").generation(&wallet()); - let events = pass(&state, &probe, 100, &views).await; + resolve_pass( + &state, + &probe, + wallet(), + 101, + &[send(2, &[outpoint(91, 0)])], + &none(), + started, + &flush, + ) + .await; - assert_eq!(outbox(&state), events); - assert!(outbox(&state).is_empty(), "taken once"); + assert_eq!( + *probe.seen.lock().expect("seen"), + vec![ResolverEvent::Cleared(txid(1))] + ); + assert_eq!( + *delivered.lock().expect("delivered"), + vec![ + ResolverEvent::Cleared(txid(1)), + ResolverEvent::Verdict(txid(2), unresolved()), + ] + ); + } + + /// A long chain on one root: every own send in it, found in one walk. + #[test] + fn should_find_every_own_send_in_a_long_chain() { + let mut views = vec![send(1, &[outpoint(90, 0)])]; + for n in 2..=20u8 { + views.push(send(n, &[outpoint(n - 1, 1)])); + } + views.push(incoming(30, &[outpoint(80, 0)])); + let graph = ChainGraph::new(&views, &none()); + + assert_eq!( + graph.own_in_chain(&txid(1)), + (1..=20u8).map(txid).collect::>() + ); } } From b8bbda741561909ebd3b96142c65371d250dcd1a Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 20:31:51 +0300 Subject: [PATCH 013/169] refactor(platform-wallet): run the broadcast resolver as one actor task Seven review passes kept finding races between the resolver's separately locked pieces (enable flag, state + generations, pass queue, wallet map, delivery flag). The state now lives in one task that handles commands (height, wallet event, Uncertain, switch, removal) and the results of its own reads and probes one at a time, so those orders are sequential by construction and the generation / wake / delivering bookkeeping is gone. - Every host callback comes from the resolver's task: switching probing off or removing a wallet only queues a command, so no callback runs under a caller's lock (FFI handle storage included), and none after shutdown aborts and joins the task. - A pass in flight is dropped when probing goes off or its wallet is removed; its results are ignored by run id. - Switching probing either way wakes every wallet, so turning it back on re-publishes what was cleared. - A pass that finds its wallet gone drops the entry only if no command for that wallet arrived since it started. - The pass logic is split into begin_pass / record_probe / nothing_to_probe on plain state; the actor reads wallets through a WalletSource and delivers through a VerdictSink, both faked in the new actor tests (off, Uncertain routing, idle and wake, off/on re-publication, dropped pass, removal, gone wallet, clears before probing, may_leave_work). - BroadcastProbeSwitch is removed; set_broadcast_probe_enabled returns at once and never calls the host. - Swift: shutdown() empties outgoingTransactionVerdicts and lastOutgoingTransactionProbe, whose native clears it no longer receives. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet-ffi/src/manager.rs | 13 +- packages/rs-platform-wallet/src/lib.rs | 1 - .../src/manager/accessors.rs | 14 +- .../rs-platform-wallet/src/manager/mod.rs | 6 +- .../src/manager/wallet_lifecycle.rs | 7 +- .../src/wallet/core/broadcast_resolver.rs | 1962 ++++++++++------- .../PlatformWalletManager.swift | 4 + 7 files changed, 1185 insertions(+), 822 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index df57a8c9a38..8a7c7a9879e 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -2160,13 +2160,12 @@ pub unsafe extern "C" fn platform_wallet_manager_set_broadcast_probe_enabled( handle: Handle, enabled: bool, ) -> PlatformWalletFFIResult { - // Released before switching: turning probing off calls the host once per - // published send, and a host that calls back into the FFI from there must - // not do so under the storage lock. - let option = PLATFORM_WALLET_MANAGER_STORAGE - .with_item(handle, |manager| manager.broadcast_probe_switch()); - let switch = unwrap_option_or_return!(option); - switch.set_enabled(enabled); + // Only queues the switch: the clears that turning probing off causes reach + // the host later, from the resolver's task, not under the storage lock. + let option = PLATFORM_WALLET_MANAGER_STORAGE.with_item(handle, |manager| { + manager.set_broadcast_probe_enabled(enabled); + }); + unwrap_option_or_return!(option); PlatformWalletFFIResult::ok() } diff --git a/packages/rs-platform-wallet/src/lib.rs b/packages/rs-platform-wallet/src/lib.rs index fee0880a01e..40da468297b 100644 --- a/packages/rs-platform-wallet/src/lib.rs +++ b/packages/rs-platform-wallet/src/lib.rs @@ -59,7 +59,6 @@ pub use spv::SpvRuntime; pub use wallet::asset_lock::manager::AssetLockManager; pub use wallet::asset_lock::tracked::{AssetLockStatus, TrackedAssetLock}; pub use wallet::asset_lock::AssetLockFunding; -pub use wallet::core::broadcast_resolver::BroadcastProbeSwitch; pub use wallet::core::WalletBalance; pub use wallet::core::{ CoreWallet, SignedCoreTransaction, ASSET_LOCK_FUNDING_SOURCES, SEND_FUNDING_SOURCES, diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index e51747a99cc..0db284c1d04 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -20,7 +20,6 @@ use crate::manager::platform_address_sync::PlatformAddressSyncManager; #[cfg(feature = "shielded")] use crate::manager::shielded_sync::ShieldedSyncManager; use crate::spv::SpvRuntime; -use crate::wallet::core::broadcast_resolver::BroadcastProbeSwitch; use crate::wallet::platform_wallet::WalletId; use crate::wallet::PlatformWallet; @@ -531,20 +530,13 @@ impl PlatformWalletManager

{ /// each change of a send's verdict is published as /// `on_outgoing_transaction_probed`, each send that no longer needs one /// as `on_outgoing_transaction_cleared`. Nothing in the wallet changes. - /// Off by default; turning it off clears every published verdict, and the - /// clears reach the host on the calling thread before this returns. + /// Off by default; turning it off clears every published verdict. Returns + /// at once: verdicts and clears reach the host from the resolver's own + /// task, never on the calling thread. pub fn set_broadcast_probe_enabled(&self, enabled: bool) { self.broadcast_resolver.set_enabled(enabled); } - /// A handle for [`set_broadcast_probe_enabled`](Self::set_broadcast_probe_enabled) - /// that outlives a borrow of the manager — for callers that hold a lock - /// around it, since turning probing off calls the host once per published - /// send before it returns. - pub fn broadcast_probe_switch(&self) -> BroadcastProbeSwitch { - BroadcastProbeSwitch(Arc::clone(&self.broadcast_resolver)) - } - /// Whether automatic probing of unconfirmed sends is on. pub fn is_broadcast_probe_enabled(&self) -> bool { self.broadcast_resolver.is_enabled() diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 4f12f6d1941..892781efb55 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -965,9 +965,9 @@ impl PlatformWalletManager

{ // they clone the FFI persister, so a straggler is exactly the // callback-after-destroy hazard the report exists to catch. // - // Broadcast-probe passes publish through the host's event callbacks - // too, so they must be gone before destroy returns; they are safe to - // abort. Both drains run concurrently under one shared budget. + // The broadcast resolver's task publishes through the host's event + // callbacks too, so it must be gone before destroy returns; it and its + // probes are safe to abort. Both drains run concurrently under one shared budget. let (payments_drained, probes_drained) = tokio::join!( self.dashpay_payment_handler .quiesce_within(PAYMENT_DRAIN_BUDGET), diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 4bea185d2ed..eddf33da1e4 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -887,9 +887,10 @@ impl PlatformWalletManager

{ } // Broadcast-probe state for the wallet's sends goes too, and the host - // is told to drop the verdicts it shows for them. After the inner - // removal, so no probe pass can read the wallet and rebuild that state; - // before the midpoint, so a same-id recreation's state is never touched. + // is told to drop the verdicts it shows for them (from the resolver's + // task). After the inner removal, so no later pass can read the wallet + // and rebuild that state; before the midpoint, so the removal is queued + // ahead of every event of a same-id recreation and never touches it. self.broadcast_resolver.wallet_removed(wallet_id); // Test-only rendezvous: the window a concurrent same-id registration can diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 7cbf429908a..4edf96f6589 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -28,8 +28,7 @@ //! and Accepted and Mined count as the same. The host is told to drop a //! send's verdict (cleared) when the send settles or leaves the wallet, when //! its wallet is removed and, for every send, when probing is turned off; -//! cleared says only "forget the verdict", not that the send settled. Events -//! are queued under the state lock and delivered with no lock held. +//! cleared says only "forget the verdict", not that the send settled. //! //! When: once as soon as dash-spv reports an `Uncertain` broadcast (the root //! of that transaction's chain), then on each advance of the wallet's synced @@ -39,26 +38,32 @@ //! A wallet whose last pass had nothing left to probe is idle and not scanned //! again until a wallet event that touched its records (a new or //! InstantSend-locked transaction, a block or chainlock that changed records, a -//! sweep) or an `Uncertain` result for one of its transactions wakes it. One -//! worker per wallet drains a queue of merged requests. `Dead` and `Mined` end +//! sweep) or an `Uncertain` result for one of its transactions wakes it. `Dead` and `Mined` end //! the probing of a root; `Accepted` does not — a mempool is not settlement. //! Probes run only while the SPV client delivers blocks, i.e. while the app is //! in the foreground. +//! +//! How: one task (the actor) owns all of this state and handles commands — +//! from wallet and SPV events, the switch and wallet removal — and the +//! results of its reads and probes one at a time, in order, so none of them +//! race. At most one pass per wallet runs; requests that arrive meanwhile +//! are merged into the next. Every host callback comes from that task, never +//! from a caller that may hold its own locks. use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; -use std::future::Future; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, Weak}; use std::time::Duration; +use async_trait::async_trait; use dash_spv::sync::SyncEvent; use dash_spv::{BroadcastResult, EventHandler}; use dashcore::{Transaction, Txid}; use key_wallet::transaction_checking::TransactionContext; use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use key_wallet_manager::{WalletEvent, WalletId, WalletManager}; -use tokio::sync::RwLock; -use tokio::task::JoinHandle; +use tokio::sync::{mpsc, RwLock}; +use tokio::task::{AbortHandle, JoinHandle, JoinSet}; use crate::broadcast_probe::{AcceptanceProbe, ProbeVerdict}; use crate::events::{PlatformEventHandler, PlatformEventManager}; @@ -341,7 +346,7 @@ fn same_for_host(a: &ProbeVerdict, b: &ProbeVerdict) -> bool { ) } -/// What a pass has to tell the host, per own send. +/// What the host must hear, per own send. #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum ResolverEvent { /// A send's verdict changed. @@ -352,81 +357,41 @@ pub(crate) enum ResolverEvent { Cleared(Txid), } -/// One event waiting to reach the host, with what the log line needs. -#[derive(Debug, Clone)] -struct Outgoing { +/// One event for the host, with what the log line needs. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Outgoing { wallet_id: WalletId, event: ResolverEvent, height: Option, trigger: &'static str, } -/// Which clears and removals a pass started under. A pass compares it with -/// the current one under the state lock before recording anything: a wallet -/// removed, or probing turned off, since the pass read the wallet makes it -/// stop instead of rebuilding state that was just dropped. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) struct Generation { - all: u64, - wallet: u64, +/// What started a pass: dash-spv's `Uncertain` result for one of the +/// wallet's transactions, or an advance of its synced height. +fn trigger_of(forced: &HashSet) -> &'static str { + if forced.is_empty() { + "height" + } else { + "uncertain" + } } -/// Bookkeeping shared by every pass. Schedules and final verdicts are per -/// probed root; published verdicts are per own send, which is what the host -/// shows. Everything for a transaction is kept while it stays unsettled in the -/// wallet, and dropped once it settles or leaves. -/// -/// Events for the host are queued in `outbox` under the same lock that records -/// them, so they reach the host in the order the state changed; they are -/// delivered afterwards with no lock held (see [`deliver`]). +/// Bookkeeping for every wallet. Schedules and final verdicts are per probed +/// root; published verdicts are per own send, which is what the host shows. +/// Everything for a transaction is kept while it stays unsettled in the +/// wallet, and dropped once it settles or leaves. Owned by the resolver's +/// actor, so every change happens in one sequence. #[derive(Debug, Default)] pub(crate) struct ResolverState { schedules: HashMap<(WalletId, Txid), ProbeSchedule>, finished: HashMap<(WalletId, Txid), Final>, published: HashMap<(WalletId, Txid), ProbeVerdict>, - generation_all: u64, - /// Kept for removed wallets too: a wallet re-added under the same id must - /// not match a generation read before its removal. - generations: HashMap, - /// Probing is off. Set under this lock together with the clears, so a - /// pass started after probing was turned off stops as surely as one that - /// started before. - off: bool, - outbox: VecDeque, } impl ResolverState { - pub(crate) fn generation(&self, wallet_id: &WalletId) -> Generation { - Generation { - all: self.generation_all, - wallet: self.generations.get(wallet_id).copied().unwrap_or(0), - } - } - - /// Whether a pass started under `started` may still probe and record. - fn is_current(&self, wallet_id: &WalletId, started: Generation) -> bool { - !self.off && self.generation(wallet_id) == started - } - - fn enqueue( - &mut self, - wallet_id: WalletId, - event: ResolverEvent, - height: Option, - trigger: &'static str, - ) { - self.outbox.push_back(Outgoing { - wallet_id, - event, - height, - trigger, - }); - } - - /// Drop everything kept for `wallet_id` and queue a clear for every send - /// the host had a verdict for. Passes that started before are stopped. - fn forget_wallet(&mut self, wallet_id: &WalletId) { - *self.generations.entry(*wallet_id).or_insert(0) += 1; + /// Drop everything kept for `wallet_id`; a clear for every send the host + /// had a verdict for. + fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { self.schedules.retain(|(wallet, _), _| wallet != wallet_id); self.finished.retain(|(wallet, _), _| wallet != wallet_id); let mut cleared: Vec = self @@ -437,23 +402,33 @@ impl ResolverState { .collect(); cleared.sort(); self.published.retain(|(wallet, _), _| wallet != wallet_id); - for txid in cleared { - self.enqueue(*wallet_id, ResolverEvent::Cleared(txid), None, "removed"); - } + cleared + .into_iter() + .map(|txid| Outgoing { + wallet_id: *wallet_id, + event: ResolverEvent::Cleared(txid), + height: None, + trigger: "removed", + }) + .collect() } - /// Drop everything and queue a clear for every published send. Passes - /// that started before are stopped. - fn forget_all(&mut self) { - self.generation_all += 1; + /// Drop everything; a clear for every published send. + fn forget_all(&mut self) -> Vec { self.schedules.clear(); self.finished.clear(); let mut cleared: Vec<(WalletId, Txid)> = self.published.drain().map(|(key, _)| key).collect(); cleared.sort(); - for (wallet_id, txid) in cleared { - self.enqueue(wallet_id, ResolverEvent::Cleared(txid), None, "disabled"); - } + cleared + .into_iter() + .map(|(wallet_id, txid)| Outgoing { + wallet_id, + event: ResolverEvent::Cleared(txid), + height: None, + trigger: "disabled", + }) + .collect() } /// Record `verdict` for `send`; returns whether the host must hear it. @@ -476,8 +451,35 @@ impl ResolverState { changed } - fn take_outbox(&mut self) -> Vec { - self.outbox.drain(..).collect() + /// Publish `verdict` for each of `sends`, collecting what changed. + fn publish_all( + &mut self, + wallet_id: WalletId, + sends: impl IntoIterator, + verdict: &ProbeVerdict, + height: u32, + trigger: &'static str, + out: &mut Vec, + ) { + for send in sends { + if self.publish((wallet_id, send), verdict) { + out.push(Outgoing { + wallet_id, + event: ResolverEvent::Verdict(send, verdict.clone()), + height: Some(height), + trigger, + }); + } + } + } + + /// Roots of `wallet_id` a probe found in a block the wallet has not seen. + fn mined(&self, wallet_id: &WalletId) -> HashSet { + self.finished + .iter() + .filter(|((wallet, _), result)| wallet == wallet_id && **result == Final::Mined) + .map(|((_, txid), _)| *txid) + .collect() } } @@ -488,45 +490,29 @@ pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { previous.is_none_or(|previous| height.saturating_sub(previous) > CATCH_UP_STEP) } -/// What a pass leaves behind besides the events it queued. -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct PassOutcome { - /// Nothing left to probe: no roots, or every root has a final verdict. - /// The wallet can go idle until something new happens. - pub nothing_to_probe: bool, +/// The start of a pass: what the host must hear now, and the roots to probe. +#[derive(Debug)] +pub(crate) struct PassStart { + pub events: Vec, + pub due: VecDeque<(Txid, Transaction)>, } -/// One pass over one wallet at `height`: probe every due root, record and -/// queue what the host must hear. The roots of every `forced` transaction's -/// chain are due regardless of their schedule (dash-spv has just reported it -/// `Uncertain`). `started` is the [`Generation`] the caller read the wallet -/// under; once it is no longer current (wallet removed, probing turned off), -/// the pass records nothing more and sends nothing more to the network. -/// `flush` delivers what is queued so far; the pass calls it with no lock -/// held after each locked stretch, so a clear or a verdict does not wait for -/// the probes of other roots. -/// -/// A root's verdict is published for the wallet's own sends: the root itself -/// when it is one, and — when the root is dead — every own send built on it, -/// on every pass, so a send built on a dead root's change later is covered -/// too. A root found mined counts as settled when picking roots, so its -/// children are probed next. -#[allow(clippy::too_many_arguments)] -pub(crate) async fn resolve_pass( - state: &Mutex, - probe: &dyn AcceptanceProbe, +/// Start a pass over one wallet at `height`: forget what settled or left +/// (clearing the sends the host had a verdict for), re-publish every dead +/// root to every own send built on it — a send built on its change after it +/// was found dead is dead too — and pick the roots due for a probe. The +/// roots of every `forced` transaction's chain are due regardless of their +/// schedule (dash-spv has just reported it `Uncertain`). A root found mined +/// counts as settled when picking roots, so its children are probed next. +pub(crate) fn begin_pass( + state: &mut ResolverState, wallet_id: WalletId, height: u32, views: &[OutgoingView], forced: &HashSet, - started: Generation, - flush: &(dyn Fn() + Sync), -) -> PassOutcome { - let trigger = if forced.is_empty() { - "height" - } else { - "uncertain" - }; +) -> PassStart { + let trigger = trigger_of(forced); + let mut events = Vec::new(); // What the wallet itself still holds unsettled — what state is kept for. let present: HashSet = views.iter().map(|view| view.txid).collect(); let own_present: HashSet = views @@ -534,53 +520,33 @@ pub(crate) async fn resolve_pass( .filter(|view| view.spends_own_coins) .map(|view| view.txid) .collect(); - let stopped = PassOutcome { - nothing_to_probe: false, - }; - - // Each locked stretch is its own block: a guard must not live across the - // probes' awaits. - let mined: HashSet = { - let mut guard = state.lock().expect("resolver state poisoned"); - if !guard.is_current(&wallet_id, started) { - return stopped; - } - // Forget what settled or left the wallet; clear the sends the host had a - // verdict for. - let is_gone = - |wallet: &WalletId, txid: &Txid| *wallet == wallet_id && !present.contains(txid); - guard - .schedules - .retain(|(wallet, txid), _| !is_gone(wallet, txid)); - guard - .finished - .retain(|(wallet, txid), _| !is_gone(wallet, txid)); - let mut cleared: Vec = guard - .published - .keys() - .filter(|(wallet, txid)| *wallet == wallet_id && !own_present.contains(txid)) - .map(|(_, txid)| *txid) - .collect(); - cleared.sort(); - guard - .published - .retain(|(wallet, txid), _| *wallet != wallet_id || own_present.contains(txid)); - for txid in cleared { - guard.enqueue( - wallet_id, - ResolverEvent::Cleared(txid), - Some(height), - trigger, - ); - } + let is_gone = |wallet: &WalletId, txid: &Txid| *wallet == wallet_id && !present.contains(txid); + state + .schedules + .retain(|(wallet, txid), _| !is_gone(wallet, txid)); + state + .finished + .retain(|(wallet, txid), _| !is_gone(wallet, txid)); + let mut cleared: Vec = state + .published + .keys() + .filter(|(wallet, txid)| *wallet == wallet_id && !own_present.contains(txid)) + .map(|(_, txid)| *txid) + .collect(); + cleared.sort(); + state + .published + .retain(|(wallet, txid), _| *wallet != wallet_id || own_present.contains(txid)); + for txid in cleared { + events.push(Outgoing { + wallet_id, + event: ResolverEvent::Cleared(txid), + height: Some(height), + trigger, + }); + } - guard - .finished - .iter() - .filter(|((wallet, _), result)| *wallet == wallet_id && **result == Final::Mined) - .map(|((_, txid), _)| *txid) - .collect() - }; + let mined = state.mined(&wallet_id); let graph = ChainGraph::new(views, &mined); let roots = graph.roots(); let forced_roots: BTreeSet = forced @@ -588,157 +554,96 @@ pub(crate) async fn resolve_pass( .flat_map(|txid| graph.roots_of(txid)) .collect(); - let due = { - let mut guard = state.lock().expect("resolver state poisoned"); - if !guard.is_current(&wallet_id, started) { - return stopped; + for view in &roots { + if let Some(Final::Dead { reason }) = state.finished.get(&(wallet_id, view.txid)).cloned() { + let verdict = ProbeVerdict::Dead { reason }; + let sends = graph.own_in_chain(&view.txid); + state.publish_all(wallet_id, sends, &verdict, height, trigger, &mut events); } - // A dead root takes every own send built on it — including ones built on - // its change after it was found dead. - for view in &roots { - if let Some(Final::Dead { reason }) = - guard.finished.get(&(wallet_id, view.txid)).cloned() - { - let verdict = ProbeVerdict::Dead { reason }; - for send in graph.own_in_chain(&view.txid) { - if guard.publish((wallet_id, send), &verdict) { - guard.enqueue( - wallet_id, - ResolverEvent::Verdict(send, verdict.clone()), - Some(height), - trigger, - ); - } - } - } - } - - let mut due = Vec::new(); - for view in &roots { - let key = (wallet_id, view.txid); - if guard.finished.contains_key(&key) { - continue; - } - let schedule = guard - .schedules - .entry(key) - .or_insert_with(|| ProbeSchedule::new(height)); - if forced_roots.contains(&view.txid) || schedule.is_due(height) { - schedule.probed_at(height); - due.push((view.txid, view.transaction.clone())); - } - } - due - }; - flush(); + } - for (root, transaction) in due { - if !state - .lock() - .expect("resolver state poisoned") - .is_current(&wallet_id, started) - { - return stopped; + let mut due = VecDeque::new(); + for view in &roots { + let key = (wallet_id, view.txid); + if state.finished.contains_key(&key) { + continue; } - let verdict = probe.probe(&transaction).await; - let key = (wallet_id, root); - { - let mut guard = state.lock().expect("resolver state poisoned"); - if !guard.is_current(&wallet_id, started) { - return stopped; - } - // Dropped by a newer pass meanwhile (it settled or left): recording - // now would resurrect state that was cleared. - if !guard.schedules.contains_key(&key) { - continue; - } - match &verdict { - ProbeVerdict::Dead { reason } => { - guard.finished.insert( - key, - Final::Dead { - reason: reason.clone(), - }, - ); - } - ProbeVerdict::Mined => { - guard.finished.insert(key, Final::Mined); - } - _ => {} - } - let sends: BTreeSet = if matches!(verdict, ProbeVerdict::Dead { .. }) { - graph.own_in_chain(&root) - } else if graph.is_own(&root) { - BTreeSet::from([root]) - } else { - BTreeSet::new() - }; - for send in sends { - if guard.publish((wallet_id, send), &verdict) { - guard.enqueue( - wallet_id, - ResolverEvent::Verdict(send, verdict.clone()), - Some(height), - trigger, - ); - } - } + let schedule = state + .schedules + .entry(key) + .or_insert_with(|| ProbeSchedule::new(height)); + if forced_roots.contains(&view.txid) || schedule.is_due(height) { + schedule.probed_at(height); + due.push_back((view.txid, view.transaction.clone())); } - flush(); } - - let guard = state.lock().expect("resolver state poisoned"); - let nothing_to_probe = roots - .iter() - .all(|view| guard.finished.contains_key(&(wallet_id, view.txid))); - PassOutcome { nothing_to_probe } + PassStart { events, due } } -/// Deliver queued events to the host, in order, with no lock held while a -/// host callback runs — a callback may turn probing off or remove a wallet, -/// which queues more events and returns at once; the running delivery sends -/// those too. `delivering` makes sure only one thread delivers at a time. -fn deliver( - state: &Mutex, - delivering: &AtomicBool, - events: &Weak, -) { - loop { - if delivering.swap(true, Ordering::SeqCst) { - return; // the thread already delivering will send ours - } - let events = events.upgrade(); - loop { - let batch = state.lock().expect("resolver state poisoned").take_outbox(); - if batch.is_empty() { - break; - } - for item in batch { - log_outgoing(&item); - if let Some(events) = &events { - match &item.event { - ResolverEvent::Verdict(txid, verdict) => { - events.on_outgoing_transaction_probed(&item.wallet_id, txid, verdict) - } - ResolverEvent::Cleared(txid) => { - events.on_outgoing_transaction_cleared(&item.wallet_id, txid) - } - } - } - } +/// Record one root's probe result: a final verdict ends its probing, and the +/// verdict goes to the wallet's own sends — the root when it is one, and, +/// when it is dead, every own send built on it. Nothing is recorded for a +/// root a newer pass has dropped (it settled or left) or whose wallet was +/// forgotten meanwhile. +pub(crate) fn record_probe( + state: &mut ResolverState, + wallet_id: WalletId, + height: u32, + views: &[OutgoingView], + forced: &HashSet, + root: Txid, + verdict: &ProbeVerdict, +) -> Vec { + let mut events = Vec::new(); + let key = (wallet_id, root); + if !state.schedules.contains_key(&key) { + return events; + } + match verdict { + ProbeVerdict::Dead { reason } => { + state.finished.insert( + key, + Final::Dead { + reason: reason.clone(), + }, + ); } - delivering.store(false, Ordering::SeqCst); - // Something queued between the last empty check and the flag reset - // would otherwise wait for the next delivery. - if state - .lock() - .expect("resolver state poisoned") - .outbox - .is_empty() - { - return; + ProbeVerdict::Mined => { + state.finished.insert(key, Final::Mined); } + _ => {} } + let graph = ChainGraph::new(views, &HashSet::new()); + let sends: BTreeSet = if matches!(verdict, ProbeVerdict::Dead { .. }) { + graph.own_in_chain(&root) + } else if graph.is_own(&root) { + BTreeSet::from([root]) + } else { + BTreeSet::new() + }; + state.publish_all( + wallet_id, + sends, + verdict, + height, + trigger_of(forced), + &mut events, + ); + events +} + +/// Nothing left to probe for the wallet: no roots, or every root has a final +/// verdict. The wallet can go idle until something new happens. +pub(crate) fn nothing_to_probe( + state: &ResolverState, + wallet_id: WalletId, + views: &[OutgoingView], +) -> bool { + let mined = state.mined(&wallet_id); + ChainGraph::new(views, &mined) + .roots() + .iter() + .all(|view| state.finished.contains_key(&(wallet_id, view.txid))) } fn log_outgoing(item: &Outgoing) { @@ -761,372 +666,559 @@ fn log_outgoing(item: &Outgoing) { } } -/// Admission gate for probe tasks. A probe only reads the wallet and -/// resubmits bytes that were already sent, so a task may be aborted at any -/// point without losing anything. -struct ProbeTasks { - state: Mutex<(bool, Vec>)>, +// ---- The actor ------------------------------------------------------------- + +/// What a pass reads from a wallet. +pub(crate) struct WalletRead { + pub views: Vec, + pub synced_height: u32, + /// The requested `Uncertain` txids the wallet actually holds. + pub forced: HashSet, } -impl ProbeTasks { - fn new() -> Self { - Self { - state: Mutex::new((true, Vec::new())), - } - } +/// An `Uncertain` result is queued for every wallet; only one holding the +/// transaction acts on it. `None`: nothing to do for this wallet — no height +/// asked for the pass and it holds none of `forced`. +fn forced_held( + by_height: bool, + forced: HashSet, + holds: impl Fn(&Txid) -> bool, +) -> Option> { + let forced: HashSet = forced.into_iter().filter(|txid| holds(txid)).collect(); + (by_height || !forced.is_empty()).then_some(forced) +} - fn spawn(&self, future: F) - where - F: Future + Send + 'static, - { - let mut state = self.state.lock().expect("probe task mutex poisoned"); - if !state.0 { - return; - } - state.1.retain(|handle| !handle.is_finished()); - state.1.push(tokio::spawn(future)); +/// Where passes read wallets from. +#[async_trait] +pub(crate) trait WalletSource: Send + Sync { + /// `None`: no such wallet. `Some(None)`: nothing to do (see + /// [`forced_held`]) — decided without walking the records. + async fn read( + &self, + wallet_id: WalletId, + by_height: bool, + forced: HashSet, + ) -> Option>; +} + +struct ManagerSource(Arc>>); + +#[async_trait] +impl WalletSource for ManagerSource { + async fn read( + &self, + wallet_id: WalletId, + by_height: bool, + forced: HashSet, + ) -> Option> { + let manager = self.0.read().await; + let info = manager.get_wallet_info(&wallet_id)?; + let forced = if forced.is_empty() { + by_height.then_some(forced) + } else { + let accounts = info.core_wallet.accounts.all_accounts(); + forced_held(by_height, forced, |txid| { + accounts + .iter() + .any(|account| account.transactions().contains_key(txid)) + }) + }; + Some(forced.map(|forced| WalletRead { + views: collect_views(info), + synced_height: info.core_wallet.synced_height(), + forced, + })) } +} + +/// Where events for the host go. +pub(crate) trait VerdictSink: Send + Sync { + fn deliver(&self, item: &Outgoing); +} - /// Close admission and stop every task, waiting at most `budget` for each - /// aborted task to unwind. Returns `false` if one outlived it. - async fn quiesce_within(&self, budget: Duration) -> bool { - let handles = { - let mut state = self.state.lock().expect("probe task mutex poisoned"); - state.0 = false; - std::mem::take(&mut state.1) +/// The host's event callbacks, reached through the event manager this +/// resolver is registered with (weakly: the manager holds the resolver). +#[derive(Default)] +struct EventSink(Mutex>); + +impl VerdictSink for EventSink { + fn deliver(&self, item: &Outgoing) { + log_outgoing(item); + let Some(events) = self.0.lock().expect("event sink poisoned").upgrade() else { + return; }; - let deadline = tokio::time::Instant::now() + budget; - let mut clean = true; - for handle in &handles { - handle.abort(); - } - for mut handle in handles { - if tokio::time::timeout_at(deadline, &mut handle) - .await - .is_err() - { - clean = false; + match &item.event { + ResolverEvent::Verdict(txid, verdict) => { + events.on_outgoing_transaction_probed(&item.wallet_id, txid, verdict) + } + ResolverEvent::Cleared(txid) => { + events.on_outgoing_transaction_cleared(&item.wallet_id, txid) } } - clean } } -/// Which wallets have a pass running, what the next pass for each must do, -/// and which have nothing to probe. A pass for a wallet never runs twice at -/// once; requests that arrive while one runs are merged into the next. -#[derive(Default)] -struct PassQueue { - running: HashSet, - pending: HashMap, - /// Wallets whose last pass had nothing to probe: height advances skip - /// them (no scan of every record per block) until something wakes them. - idle: HashSet, - /// Bumped by every wake-up, so a pass that read the wallet before one - /// happened does not mark it idle after it. - wakes: HashMap, +fn deliver(sink: &Arc, events: Vec) { + for item in &events { + sink.deliver(item); + } } -impl PassQueue { - fn wake(&mut self, wallet_id: &WalletId) { - self.idle.remove(wallet_id); - *self.wakes.entry(*wallet_id).or_insert(0) += 1; - } +/// What the resolver is told. Sent from event handlers and the manager; the +/// actor handles them one at a time, in order. +#[derive(Debug)] +enum Command { + Height { + wallet_id: WalletId, + height: u32, + }, + /// Any other wallet event; `touched` when it may have left something new + /// to probe (see [`may_leave_work`]). + Seen { + wallet_id: WalletId, + touched: bool, + }, + Uncertain(Txid), + SetEnabled(bool), + WalletRemoved(WalletId), +} - fn wakes(&self, wallet_id: &WalletId) -> u64 { - self.wakes.get(wallet_id).copied().unwrap_or(0) - } +/// What a finished job reports back to the actor. +enum JobDone { + Read { + wallet_id: WalletId, + run: u64, + height: u32, + read: Option>, + }, + Probed { + wallet_id: WalletId, + run: u64, + root: Txid, + verdict: ProbeVerdict, + }, } #[derive(Default)] struct PendingPass { height: u32, /// A height advance asked for this pass; otherwise only `Uncertain` - /// results did, and a wallet that holds none of `forced` is skipped. + /// results did. by_height: bool, forced: HashSet, } -/// Whether any account of the wallet records `txid` — a map lookup per -/// account, not a walk over the records. -fn holds(info: &PlatformWalletInfo, txid: &Txid) -> bool { - info.core_wallet - .accounts - .all_accounts() - .iter() - .any(|account| account.transactions().contains_key(txid)) +/// A pass in flight: reading the wallet, then probing its due roots one at +/// a time. Results carry the run id; a result for a run that was dropped +/// (probing off, wallet removed) is ignored. +struct Run { + id: u64, + /// The wallet's command count when the run started. + seq: u64, + /// Something touched the wallet during the run: do not mark it idle. + woken: bool, + abort: AbortHandle, + probing: Option, } -/// Runs probe passes off the wallet-event fan-out and delivers each change of -/// a send's verdict as [`PlatformEventHandler::on_outgoing_transaction_probed`], -/// and each verdict the host must drop as -/// [`PlatformEventHandler::on_outgoing_transaction_cleared`]. -/// -/// Off until the host turns it on with [`set_enabled`](Self::set_enabled): a -/// probe sends the signed transaction to evonodes over DAPI, which is a -/// product decision, not something a wallet should start doing on upgrade. -pub(crate) struct BroadcastResolver { - enabled: AtomicBool, +struct Probing { + views: Vec, + height: u32, + forced: HashSet, + due: VecDeque<(Txid, Transaction)>, +} + +#[derive(Default)] +struct WalletEntry { + /// Last synced height reported, for telling catch-up from following. + height: Option, + /// The last pass had nothing to probe: height advances skip the wallet + /// (no walk over its records per block) until something wakes it. + idle: bool, + pending: Option, + run: Option, + /// Commands seen for this wallet. A run that found the wallet gone drops + /// the entry only if none arrived since it started — a same-id wallet + /// added meanwhile keeps its entry. + seq: u64, +} + +/// Owns all of the resolver's state. One task; commands and job results are +/// handled one at a time, so no two of them race, and every host callback +/// comes from this task — never from a caller holding its own locks. +struct Actor { + enabled: bool, + state: ResolverState, + wallets: HashMap, + next_run: u64, probe: Arc, - wallet_manager: Arc>>, - state: Arc>, - delivering: Arc, - /// Every wallet seen in an event, with its last synced height if one has - /// been reported: an SPV-level `Uncertain` carries no wallet id, so it is - /// queued for every known wallet. A worker that finds its wallet gone - /// drops the entry, so a late event for a removed wallet cannot keep it. - wallets: Arc>>>, - passes: Arc>, - events: Mutex>, - tasks: ProbeTasks, + source: Arc, + sink: Arc, + jobs: JoinSet, } -impl BroadcastResolver { - pub(crate) fn new( +impl Actor { + fn new( probe: Arc, - wallet_manager: Arc>>, + source: Arc, + sink: Arc, ) -> Self { Self { - enabled: AtomicBool::new(false), + enabled: false, + state: ResolverState::default(), + wallets: HashMap::new(), + next_run: 0, probe, - wallet_manager, - state: Arc::new(Mutex::new(ResolverState { - off: true, - ..ResolverState::default() - })), - delivering: Arc::new(AtomicBool::new(false)), - wallets: Arc::new(Mutex::new(HashMap::new())), - passes: Arc::new(Mutex::new(PassQueue::default())), - events: Mutex::new(Weak::new()), - tasks: ProbeTasks::new(), + source, + sink, + jobs: JoinSet::new(), } } - /// Where verdicts are delivered. Weak: the event manager holds this - /// resolver as one of its handlers. - pub(crate) fn set_event_manager(&self, events: Weak) { - *self.events.lock().expect("events mutex poisoned") = events; + async fn run(mut self, mut commands: mpsc::UnboundedReceiver) { + loop { + tokio::select! { + command = commands.recv() => match command { + Some(command) => self.handle(command), + None => return, + }, + Some(done) = self.jobs.join_next(), if !self.jobs.is_empty() => { + // An aborted job (its run was dropped) reports nothing. + if let Ok(done) = done { + self.job_done(done); + } + } + } + } } - fn deliver(&self) { - let events = self.events.lock().expect("events mutex poisoned").clone(); - deliver(&self.state, &self.delivering, &events); + /// The wallet's entry, created on first sight, with this command counted. + fn entry(&mut self, wallet_id: WalletId) -> &mut WalletEntry { + let entry = self.wallets.entry(wallet_id).or_default(); + entry.seq += 1; + entry } - /// Turning probing off forgets every send and tells the host to drop every - /// verdict it holds: with no passes running, nothing would ever clear them. - /// Passes still running, and ones already queued, stop before their next - /// probe and record nothing. Either way every wallet leaves idle, so - /// turning probing back on re-publishes what was cleared. - /// - /// The clears are delivered on the calling thread before this returns. - pub(crate) fn set_enabled(&self, enabled: bool) { - let changed = { - let mut state = self.state.lock().expect("resolver state poisoned"); - let was = self.enabled.swap(enabled, Ordering::SeqCst); - state.off = !enabled; - if was && !enabled { - state.forget_all(); + fn handle(&mut self, command: Command) { + match command { + Command::Height { wallet_id, height } => { + let enabled = self.enabled; + let entry = self.entry(wallet_id); + let previous = entry.height.replace(height); + if !enabled || entry.idle || is_catch_up_step(previous, height) { + return; + } + let pending = entry.pending.get_or_insert_with(PendingPass::default); + pending.height = pending.height.max(height); + pending.by_height = true; + self.start(wallet_id); + } + Command::Seen { wallet_id, touched } => { + let entry = self.entry(wallet_id); + if touched { + entry.idle = false; + if let Some(run) = entry.run.as_mut() { + run.woken = true; + } + } } - was != enabled + Command::Uncertain(txid) => { + if !self.enabled { + return; + } + let wallets: Vec = self.wallets.keys().copied().collect(); + for wallet_id in wallets { + if let Some(entry) = self.wallets.get_mut(&wallet_id) { + let pending = entry.pending.get_or_insert_with(PendingPass::default); + pending.height = pending.height.max(entry.height.unwrap_or(0)); + pending.forced.insert(txid); + } + self.start(wallet_id); + } + } + Command::SetEnabled(enabled) => { + if enabled == self.enabled { + return; + } + self.enabled = enabled; + // Either way every wallet leaves idle, so turning probing + // back on re-publishes what turning it off cleared. + for entry in self.wallets.values_mut() { + entry.idle = false; + if !enabled { + entry.pending = None; + if let Some(run) = entry.run.take() { + run.abort.abort(); + } + } + } + if !enabled { + let events = self.state.forget_all(); + deliver(&self.sink, events); + } + } + Command::WalletRemoved(wallet_id) => { + if let Some(run) = self.wallets.remove(&wallet_id).and_then(|entry| entry.run) { + run.abort.abort(); + } + let events = self.state.forget_wallet(&wallet_id); + deliver(&self.sink, events); + } + } + } + + /// Start the wallet's pending pass unless one is already running. + fn start(&mut self, wallet_id: WalletId) { + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; }; - if changed { - let mut passes = self.passes.lock().expect("pass queue poisoned"); - passes.idle.clear(); - if !enabled { - passes.pending.clear(); + if entry.run.is_some() { + return; // the running pass takes it up when it ends + } + let Some(pending) = entry.pending.take() else { + return; + }; + self.next_run += 1; + let run = self.next_run; + let source = Arc::clone(&self.source); + let abort = self.jobs.spawn(async move { + let read = source + .read(wallet_id, pending.by_height, pending.forced) + .await; + JobDone::Read { + wallet_id, + run, + height: pending.height, + read, + } + }); + entry.run = Some(Run { + id: run, + seq: entry.seq, + woken: false, + abort, + probing: None, + }); + } + + fn job_done(&mut self, done: JobDone) { + match done { + JobDone::Read { + wallet_id, + run, + height, + read, + } => { + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; + let Some(current) = entry.run.as_mut().filter(|current| current.id == run) else { + return; + }; + match read { + None => { + // Gone from the wallet manager (its removal clears), + // or an id a late event brought back after removal. + if current.seq == entry.seq { + self.wallets.remove(&wallet_id); + } else { + entry.run = None; + self.start(wallet_id); + } + } + Some(None) => { + entry.run = None; + self.start(wallet_id); + } + Some(Some(read)) => { + if !read.forced.is_empty() { + entry.idle = false; + } + // A request queued before any height was reported + // carries 0; the wallet's own synced height is better. + let height = height.max(read.synced_height); + let start = begin_pass( + &mut self.state, + wallet_id, + height, + &read.views, + &read.forced, + ); + current.probing = Some(Probing { + views: read.views, + height, + forced: read.forced, + due: start.due, + }); + // Clears and re-published dead verdicts reach the host + // before the pass goes out to the network. + deliver(&self.sink, start.events); + self.probe_next(wallet_id); + } + } + } + JobDone::Probed { + wallet_id, + run, + root, + verdict, + } => { + let Some(probing) = self + .wallets + .get_mut(&wallet_id) + .and_then(|entry| entry.run.as_mut()) + .filter(|current| current.id == run) + .and_then(|current| current.probing.as_ref()) + else { + return; + }; + let events = record_probe( + &mut self.state, + wallet_id, + probing.height, + &probing.views, + &probing.forced, + root, + &verdict, + ); + deliver(&self.sink, events); + self.probe_next(wallet_id); } } - self.deliver(); } - pub(crate) fn is_enabled(&self) -> bool { - self.enabled.load(Ordering::SeqCst) + /// Probe the run's next due root, or end the run. + fn probe_next(&mut self, wallet_id: WalletId) { + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; + let Some(run) = entry.run.as_mut() else { + return; + }; + let Some(probing) = run.probing.as_mut() else { + return; + }; + if let Some((root, transaction)) = probing.due.pop_front() { + let probe = Arc::clone(&self.probe); + let id = run.id; + run.abort = self.jobs.spawn(async move { + let verdict = probe.probe(&transaction).await; + JobDone::Probed { + wallet_id, + run: id, + root, + verdict, + } + }); + return; + } + if !run.woken && nothing_to_probe(&self.state, wallet_id, &probing.views) { + entry.idle = true; + } + entry.run = None; + self.start(wallet_id); } +} - /// Forget a removed wallet — call after it has left the wallet manager: - /// its queued pass, its entry and every verdict, each of which the host is - /// told to drop. A pass that read the wallet before the removal stops - /// before recording anything. - pub(crate) fn wallet_removed(&self, wallet_id: &WalletId) { - { - let mut passes = self.passes.lock().expect("pass queue poisoned"); - passes.pending.remove(wallet_id); - // A wake, not a removal of the counter: a pass still running for - // the wallet must see it changed and not mark the wallet idle. - passes.wake(wallet_id); +enum ActorSlot { + /// Waiting for the first command sent from inside a Tokio runtime. + NotStarted(Box, mpsc::UnboundedReceiver), + Running(JoinHandle<()>), + Closed, +} + +/// Probes unconfirmed sends and delivers each change of a send's verdict as +/// [`PlatformEventHandler::on_outgoing_transaction_probed`], and each verdict +/// the host must drop as +/// [`PlatformEventHandler::on_outgoing_transaction_cleared`] — always from +/// the resolver's own task, asynchronously to whoever caused it. +/// +/// Off until the host turns it on with [`set_enabled`](Self::set_enabled): a +/// probe sends the signed transaction to evonodes over DAPI, which is a +/// product decision, not something a wallet should start doing on upgrade. +pub(crate) struct BroadcastResolver { + enabled: AtomicBool, + commands: mpsc::UnboundedSender, + actor: Mutex, + sink: Arc, +} + +impl BroadcastResolver { + pub(crate) fn new( + probe: Arc, + wallet_manager: Arc>>, + ) -> Self { + let sink = Arc::new(EventSink::default()); + let actor = Actor::new( + probe, + Arc::new(ManagerSource(wallet_manager)), + Arc::clone(&sink) as Arc, + ); + let (commands, receiver) = mpsc::unbounded_channel(); + Self { + enabled: AtomicBool::new(false), + commands, + actor: Mutex::new(ActorSlot::NotStarted(Box::new(actor), receiver)), + sink, } - self.wallets - .lock() - .expect("wallets mutex poisoned") - .remove(wallet_id); - self.state - .lock() - .expect("resolver state poisoned") - .forget_wallet(wallet_id); - self.deliver(); - } - - /// Stop admitting passes and stop the running ones. Idempotent. - pub(crate) async fn quiesce_within(&self, budget: Duration) -> bool { - self.tasks.quiesce_within(budget).await } - /// Queue a pass for `wallet_id`; start a worker for it unless one runs. - /// A height-driven request (no `force`) is dropped for an idle wallet; a - /// forced one is queued, and the worker skips it cheaply if the wallet - /// does not hold the transaction. - fn request_pass(&self, wallet_id: WalletId, height: u32, force: Option) { - { - let mut passes = self.passes.lock().expect("pass queue poisoned"); - if force.is_none() && passes.idle.contains(&wallet_id) { - return; - } - let pending = passes.pending.entry(wallet_id).or_default(); - pending.height = pending.height.max(height); - pending.by_height |= force.is_none(); - pending.forced.extend(force); - if !passes.running.insert(wallet_id) { - return; // the running worker picks it up - } - } - let wallet_manager = Arc::clone(&self.wallet_manager); - let probe = Arc::clone(&self.probe); - let state = Arc::clone(&self.state); - let passes = Arc::clone(&self.passes); - let wallets = Arc::clone(&self.wallets); - let delivering = Arc::clone(&self.delivering); - let events = self.events.lock().expect("events mutex poisoned").clone(); - self.tasks.spawn(async move { - let mut running = Running { - passes: Arc::clone(&passes), - wallet_id, - armed: true, - }; - let flush = || deliver(&state, &delivering, &events); - loop { - // Take the next request, or stop — under the same lock a - // request_pass would take, so a request can never land between - // "queue empty" and "no longer running" and be left unserved. - let (next, wakes) = { - let mut queue = passes.lock().expect("pass queue poisoned"); - let next = queue.pending.remove(&wallet_id); - if next.is_none() { - queue.running.remove(&wallet_id); - } - (next, queue.wakes(&wallet_id)) - }; - let Some(PendingPass { - height, - by_height, - forced, - }) = next - else { - // Already left the running set under the lock; the guard - // must not remove a marker a new worker may have set since. - running.armed = false; - break; - }; - let started = state - .lock() - .expect("resolver state poisoned") - .generation(&wallet_id); - // Read under the lock, probe without it: a probe is network I/O. - let read = { - let manager = wallet_manager.read().await; - manager.get_wallet_info(&wallet_id).map(|info| { - // An `Uncertain` result is queued for every wallet; - // only the one holding the transaction acts on it. - let forced: HashSet = forced - .into_iter() - .filter(|txid| holds(info, txid)) - .collect(); - if !by_height && forced.is_empty() { - return None; - } - // A request queued before any height was reported - // carries 0; the wallet's own synced height is better. - let height = height.max(info.core_wallet.synced_height()); - Some((collect_views(info), height, forced)) - }) - }; - let (views, height, forced) = match read { - Some(Some(pass)) => pass, - Some(None) => continue, // not this wallet's transaction - None => { - // Removed (wallet_removed did the clearing), or an id a - // late event brought back after the removal. - wallets - .lock() - .expect("wallets mutex poisoned") - .remove(&wallet_id); - let mut queue = passes.lock().expect("pass queue poisoned"); - queue.idle.remove(&wallet_id); - queue.wakes.remove(&wallet_id); - continue; - } - }; - if !forced.is_empty() { - passes - .lock() - .expect("pass queue poisoned") - .idle - .remove(&wallet_id); - } - let outcome = resolve_pass( - &state, - probe.as_ref(), - wallet_id, - height, - &views, - &forced, - started, - &flush, - ) - .await; - if outcome.nothing_to_probe { - let mut queue = passes.lock().expect("pass queue poisoned"); - if queue.wakes(&wallet_id) == wakes { - queue.idle.insert(wallet_id); - } + /// Where verdicts are delivered. Weak: the event manager holds this + /// resolver as one of its handlers. + pub(crate) fn set_event_manager(&self, events: Weak) { + *self.sink.0.lock().expect("event sink poisoned") = events; + } + + fn send(&self, command: Command) { + // Fails only once the actor is gone (shutdown); nothing to do then. + let _ = self.commands.send(command); + let mut slot = self.actor.lock().expect("resolver actor slot poisoned"); + if matches!(*slot, ActorSlot::NotStarted(..)) { + if let Ok(runtime) = tokio::runtime::Handle::try_current() { + if let ActorSlot::NotStarted(actor, receiver) = + std::mem::replace(&mut *slot, ActorSlot::Closed) + { + *slot = ActorSlot::Running(runtime.spawn(actor.run(receiver))); } - flush(); } - }); + } } -} -/// A handle that turns automatic probing on or off, usable after the lock that -/// guards the manager is released: turning probing off delivers a clear per -/// published send to the host on the calling thread. -#[derive(Clone)] -pub struct BroadcastProbeSwitch(pub(crate) Arc); + /// Turn probing on or off. Turning it off forgets every send and has the + /// host told to drop every verdict it holds — with no passes running, + /// nothing would ever clear them — and drops the passes in flight. Either + /// way every wallet leaves idle, so turning it back on re-publishes what + /// was cleared. Returns at once; the clears follow from the resolver's + /// task. + pub(crate) fn set_enabled(&self, enabled: bool) { + self.enabled.store(enabled, Ordering::SeqCst); + self.send(Command::SetEnabled(enabled)); + } -impl BroadcastProbeSwitch { - /// See [`PlatformWalletManager::set_broadcast_probe_enabled`](crate::PlatformWalletManager::set_broadcast_probe_enabled). - pub fn set_enabled(&self, enabled: bool) { - self.0.set_enabled(enabled); + pub(crate) fn is_enabled(&self) -> bool { + self.enabled.load(Ordering::SeqCst) } -} -/// Takes a wallet's worker out of the running set if it ends without reaching -/// its own exit — an abort at shutdown — so a later request is not refused -/// forever. Disarmed on the normal exit, which leaves the set under the queue -/// lock itself. -struct Running { - passes: Arc>, - wallet_id: WalletId, - armed: bool, -} + /// Forget a removed wallet — call after it has left the wallet manager: + /// its pass in flight, its entry and every verdict, each of which the host + /// is told to drop (from the resolver's task, not the caller's). + pub(crate) fn wallet_removed(&self, wallet_id: &WalletId) { + self.send(Command::WalletRemoved(*wallet_id)); + } -impl Drop for Running { - fn drop(&mut self) { - if !self.armed { - return; - } - if let Ok(mut passes) = self.passes.lock() { - passes.running.remove(&self.wallet_id); - } + /// Stop the resolver's task, and with it every probe, waiting at most + /// `budget` for it to end; after this no host callback comes from the + /// resolver. Returns `false` if the task outlived the budget. Idempotent. + pub(crate) async fn quiesce_within(&self, budget: Duration) -> bool { + let slot = std::mem::replace( + &mut *self.actor.lock().expect("resolver actor slot poisoned"), + ActorSlot::Closed, + ); + let ActorSlot::Running(handle) = slot else { + return true; + }; + handle.abort(); + tokio::time::timeout(budget, handle).await.is_ok() } } @@ -1152,51 +1244,28 @@ fn may_leave_work(event: &WalletEvent) -> bool { impl EventHandler for BroadcastResolver { fn on_wallet_event(&self, event: &WalletEvent) { let wallet_id = event.wallet_id(); - if let WalletEvent::SyncHeightAdvanced { height, .. } = event { - let previous = self - .wallets - .lock() - .expect("wallets mutex poisoned") - .insert(wallet_id, Some(*height)) - .flatten(); - if self.is_enabled() && !is_catch_up_step(previous, *height) { - self.request_pass(wallet_id, *height, None); - } - return; - } - self.wallets - .lock() - .expect("wallets mutex poisoned") - .entry(wallet_id) - .or_insert(None); - if may_leave_work(event) { - self.passes - .lock() - .expect("pass queue poisoned") - .wake(&wallet_id); - } + let command = match event { + WalletEvent::SyncHeightAdvanced { height, .. } => Command::Height { + wallet_id, + height: *height, + }, + other => Command::Seen { + wallet_id, + touched: may_leave_work(other), + }, + }; + self.send(command); } fn on_sync_event(&self, event: &SyncEvent) { - let SyncEvent::TransactionBroadcastResult { + if let SyncEvent::TransactionBroadcastResult { txid, result: BroadcastResult::Uncertain, } = event - else { - return; - }; - if !self.is_enabled() { - return; - } - let wallets: Vec<(WalletId, u32)> = self - .wallets - .lock() - .expect("wallets mutex poisoned") - .iter() - .map(|(wallet, height)| (*wallet, height.unwrap_or(0))) - .collect(); - for (wallet_id, height) in wallets { - self.request_pass(wallet_id, height, Some(*txid)); + { + if self.is_enabled() { + self.send(Command::Uncertain(*txid)); + } } } } @@ -1209,8 +1278,13 @@ mod tests { use std::sync::Mutex as StdMutex; use async_trait::async_trait; + use dashcore::bls_sig_utils::BLSSignature; + use dashcore::ephemerealdata::chain_lock::ChainLock; use dashcore::hashes::Hash; - use dashcore::{OutPoint, TxIn}; + use dashcore::{BlockHash, OutPoint, TxIn}; + use key_wallet::account::AccountType; + use key_wallet::managed_account::transaction_record::TransactionRecord; + use key_wallet::WalletCoreBalance; use super::*; @@ -1397,7 +1471,7 @@ mod tests { assert!(is_catch_up_step(Some(1_000 - CATCH_UP_STEP - 1), 1_000)); } - // ---- resolve_pass ------------------------------------------------------- + // ---- begin_pass / record_probe ------------------------------------------ /// Answers per txid and records every probe it received. struct ScriptedProbe { @@ -1448,8 +1522,32 @@ mod tests { } } + /// The resolver's bookkeeping plus what the host was sent, driven the way + /// the actor drives it but without the task. + #[derive(Default)] + struct Harness { + state: ResolverState, + sent: Vec, + } + + impl Harness { + fn push(&mut self, events: Vec) { + self.sent.extend(events.into_iter().map(|item| item.event)); + } + + fn forget_wallet(&mut self, wallet_id: &WalletId) { + let events = self.state.forget_wallet(wallet_id); + self.push(events); + } + + fn forget_all(&mut self) { + let events = self.state.forget_all(); + self.push(events); + } + } + async fn pass( - state: &Mutex, + state: &Mutex, probe: &dyn AcceptanceProbe, height: u32, views: &[OutgoingView], @@ -1458,42 +1556,47 @@ mod tests { outbox(state) } - /// A pass started under the current generation, as the worker runs it. + /// One pass: begin, probe each due root with no lock held, record. + /// Returns whether nothing is left to probe. async fn run( - state: &Mutex, + state: &Mutex, probe: &dyn AcceptanceProbe, wallet_id: WalletId, height: u32, views: &[OutgoingView], forced: &HashSet, - ) -> PassOutcome { - let started = state.lock().expect("state").generation(&wallet_id); - resolve_pass( - state, - probe, - wallet_id, - height, - views, - forced, - started, - &|| {}, - ) - .await + ) -> bool { + let due = { + let mut guard = state.lock().expect("state"); + let start = begin_pass(&mut guard.state, wallet_id, height, views, forced); + guard.push(start.events); + start.due + }; + for (root, transaction) in due { + let verdict = probe.probe(&transaction).await; + let mut guard = state.lock().expect("state"); + let events = record_probe( + &mut guard.state, + wallet_id, + height, + views, + forced, + root, + &verdict, + ); + guard.push(events); + } + let guard = state.lock().expect("state"); + nothing_to_probe(&guard.state, wallet_id, views) } - fn outbox(state: &Mutex) -> Vec { - state - .lock() - .expect("state") - .take_outbox() - .into_iter() - .map(|item| item.event) - .collect() + fn outbox(state: &Mutex) -> Vec { + std::mem::take(&mut state.lock().expect("state").sent) } #[tokio::test] async fn should_probe_only_roots_and_report_each_verdict() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; @@ -1505,7 +1608,7 @@ mod tests { #[tokio::test] async fn should_stop_probing_a_root_proven_dead() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), dead())]); let views = [send(1, &[outpoint(90, 0)])]; @@ -1520,7 +1623,7 @@ mod tests { /// being asked until it settles, but the host hears about it only once. #[tokio::test] async fn should_keep_probing_an_accepted_root_but_publish_it_once() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); let views = [send(1, &[outpoint(90, 0)])]; @@ -1540,7 +1643,7 @@ mod tests { #[tokio::test] async fn should_keep_probing_an_unresolved_root_on_each_new_block() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)])]; @@ -1561,7 +1664,7 @@ mod tests { /// the same block as an earlier probe. #[tokio::test] async fn should_probe_a_forced_root_regardless_of_its_schedule() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)])]; @@ -1583,7 +1686,7 @@ mod tests { /// earlier unsettled send. Forcing the child probes its root at once. #[tokio::test] async fn should_probe_the_root_when_a_child_is_forced() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; @@ -1601,38 +1704,11 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1), txid(1)]); } - /// Probing turned off (or the wallet removed) after the worker read the - /// wallet: the pass sends nothing to the network and records nothing. - #[tokio::test] - async fn should_do_nothing_in_a_pass_started_before_a_clear() { - let state = Mutex::new(ResolverState::default()); - let probe = ScriptedProbe::new(&[]); - let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])]; - let started = state.lock().expect("state").generation(&wallet()); - state.lock().expect("state").forget_all(); - - resolve_pass( - &state, - &probe, - wallet(), - 100, - &views, - &none(), - started, - &|| {}, - ) - .await; - - assert!(probe.probed().is_empty()); - assert!(outbox(&state).is_empty()); - assert!(state.lock().expect("state").schedules.is_empty()); - } - /// A send that settled or left the wallet is forgotten, and the host is /// told to drop the verdict it was shown. #[tokio::test] async fn should_clear_a_published_send_that_is_no_longer_unconfirmed() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]); let views = [send(1, &[outpoint(90, 0)])]; @@ -1641,28 +1717,28 @@ mod tests { assert_eq!(gone, vec![ResolverEvent::Cleared(txid(1))]); let guard = state.lock().expect("state"); - assert!(guard.schedules.is_empty()); - assert!(guard.finished.is_empty()); - assert!(guard.published.is_empty()); + assert!(guard.state.schedules.is_empty()); + assert!(guard.state.finished.is_empty()); + assert!(guard.state.published.is_empty()); } #[tokio::test] async fn should_not_let_one_wallets_pass_forget_another_wallets_roots() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved())]); let views = [send(1, &[outpoint(90, 0)])]; pass(&state, &probe, 100, &views).await; run(&state, &probe, [9u8; 32], 100, &[], &none()).await; - assert_eq!(state.lock().expect("state").schedules.len(), 1); + assert_eq!(state.lock().expect("state").state.schedules.len(), 1); } /// A mined transaction the wallet has not caught up with yet: nothing left /// to ask, so it is not probed again. #[tokio::test] async fn should_stop_probing_a_root_found_in_a_block() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined)]); let views = [send(1, &[outpoint(90, 0)])]; @@ -1677,7 +1753,7 @@ mod tests { /// built on a dead root is published dead, not just the root. #[tokio::test] async fn should_publish_a_dead_root_for_every_own_send_built_on_it() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), dead())]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; @@ -1701,7 +1777,7 @@ mod tests { /// about the wallet's own send — the only row it can attach a verdict to. #[tokio::test] async fn should_publish_a_dead_incoming_parent_on_the_own_send_only() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(5), dead())]); let views = [incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])]; @@ -1713,7 +1789,7 @@ mod tests { #[tokio::test] async fn should_publish_nothing_while_an_incoming_parent_is_only_accepted() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(5), ProbeVerdict::Accepted)]); let views = [incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])]; @@ -1724,7 +1800,7 @@ mod tests { /// settled for picking roots, so the send built on it is probed next. #[tokio::test] async fn should_probe_the_children_of_a_root_found_in_a_block() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined), (txid(2), dead())]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; @@ -1742,7 +1818,7 @@ mod tests { /// A probe that, while it is out on the network, lets a concurrent pass /// clear the root (it settled): the late verdict must not be published. struct ClearingProbe { - state: Arc>, + state: Arc>, } #[async_trait] @@ -1755,7 +1831,7 @@ mod tests { #[tokio::test] async fn should_not_publish_a_verdict_for_a_root_cleared_during_the_probe() { - let state = Arc::new(Mutex::new(ResolverState::default())); + let state = Arc::new(Mutex::new(Harness::default())); let probe = ClearingProbe { state: Arc::clone(&state), }; @@ -1771,14 +1847,14 @@ mod tests { .await; assert!(outbox(&state).is_empty()); - assert!(state.lock().expect("state").published.is_empty()); + assert!(state.lock().expect("state").state.published.is_empty()); } /// A send built on a dead root's change after the root was found dead is /// dead too, and hears it without the root being asked again. #[tokio::test] async fn should_publish_a_dead_root_to_a_send_built_on_it_later() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), dead())]); pass(&state, &probe, 100, &[send(1, &[outpoint(90, 0)])]).await; @@ -1798,7 +1874,7 @@ mod tests { /// may go idle; one with a root still open may not. #[tokio::test] async fn should_report_nothing_to_probe_only_when_every_root_is_final() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), dead()), (txid(2), unresolved())]); let empty = run(&state, &probe, wallet(), 100, &[], &none()).await; @@ -1821,16 +1897,16 @@ mod tests { ) .await; - assert!(empty.nothing_to_probe); - assert!(dead_only.nothing_to_probe); - assert!(!open.nothing_to_probe); + assert!(empty); + assert!(dead_only); + assert!(!open); } // ---- ResolverState ------------------------------------------------------ #[tokio::test] async fn should_forget_one_wallet_and_queue_a_clear_for_its_published_sends() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved()), (txid(2), unresolved())]); pass(&state, &probe, 100, &[send(1, &[outpoint(90, 0)])]).await; run( @@ -1848,13 +1924,17 @@ mod tests { assert_eq!(outbox(&state), vec![ResolverEvent::Cleared(txid(1))]); let guard = state.lock().expect("state"); - assert_eq!(guard.published.len(), 1, "the other wallet is untouched"); - assert_eq!(guard.schedules.len(), 1); + assert_eq!( + guard.state.published.len(), + 1, + "the other wallet is untouched" + ); + assert_eq!(guard.state.schedules.len(), 1); } #[tokio::test] async fn should_forget_everything_and_queue_a_clear_for_every_published_send() { - let state = Mutex::new(ResolverState::default()); + let state = Mutex::new(Harness::default()); let probe = ScriptedProbe::new(&[(txid(1), unresolved()), (txid(2), unresolved())]); pass(&state, &probe, 100, &[send(1, &[outpoint(90, 0)])]).await; run( @@ -1873,7 +1953,9 @@ mod tests { assert_eq!(outbox(&state).len(), 2); let guard = state.lock().expect("state"); assert!( - guard.published.is_empty() && guard.schedules.is_empty() && guard.finished.is_empty() + guard.state.published.is_empty() + && guard.state.schedules.is_empty() + && guard.state.finished.is_empty() ); } @@ -1903,106 +1985,392 @@ mod tests { )); } - /// Probing off: a pass that read its generation after the switch stops - /// as surely as one that started before it. + /// A long chain on one root: every own send in it, found in one walk. + #[test] + fn should_find_every_own_send_in_a_long_chain() { + let mut views = vec![send(1, &[outpoint(90, 0)])]; + for n in 2..=20u8 { + views.push(send(n, &[outpoint(n - 1, 1)])); + } + views.push(incoming(30, &[outpoint(80, 0)])); + let graph = ChainGraph::new(&views, &none()); + + assert_eq!( + graph.own_in_chain(&txid(1)), + (1..=20u8).map(txid).collect::>() + ); + } + + // ---- the actor ---------------------------------------------------------- + + /// Unsettled views per wallet; records every walk over a wallet's records. + #[derive(Default)] + struct FakeSource { + views: StdMutex>>, + walks: StdMutex>, + } + + #[async_trait] + impl WalletSource for FakeSource { + async fn read( + &self, + wallet_id: WalletId, + by_height: bool, + forced: HashSet, + ) -> Option> { + let views = self.views.lock().expect("views").get(&wallet_id)?.clone(); + let forced = forced_held(by_height, forced, |txid| { + views.iter().any(|view| view.txid == *txid) + }); + Some(forced.map(|forced| { + self.walks.lock().expect("walks").push(wallet_id); + WalletRead { + views, + synced_height: 0, + forced, + } + })) + } + } + + #[derive(Default)] + struct CollectSink(StdMutex>); + + impl VerdictSink for CollectSink { + fn deliver(&self, item: &Outgoing) { + self.0.lock().expect("sink").push(item.event.clone()); + } + } + + struct Rig { + actor: Actor, + source: Arc, + sink: Arc, + } + + impl Rig { + fn new(probe: Arc) -> Self { + Self::with_sink(probe, Arc::new(CollectSink::default())) + } + + fn with_sink(probe: Arc, sink: Arc) -> Self { + let source = Arc::new(FakeSource::default()); + let actor = Actor::new( + probe, + Arc::clone(&source) as Arc, + Arc::clone(&sink) as Arc, + ); + Self { + actor, + source, + sink, + } + } + + fn views(&self, wallet_id: WalletId, views: Vec) { + self.source + .views + .lock() + .expect("views") + .insert(wallet_id, views); + } + + /// Handle `command`, then every job it caused, as the task would. + async fn send(&mut self, command: Command) { + self.actor.handle(command); + self.settle().await; + } + + async fn settle(&mut self) { + while let Some(done) = self.actor.jobs.join_next().await { + if let Ok(done) = done { + self.actor.job_done(done); + } + } + } + + /// Two height steps: the first after launch is catch-up. + async fn follow(&mut self, wallet_id: WalletId, from: u32) { + self.send(Command::Height { + wallet_id, + height: from, + }) + .await; + self.send(Command::Height { + wallet_id, + height: from + 1, + }) + .await; + } + + async fn height(&mut self, wallet_id: WalletId, height: u32) { + self.send(Command::Height { wallet_id, height }).await; + } + + fn sent(&self) -> Vec { + std::mem::take(&mut *self.sink.0.lock().expect("sink")) + } + + fn walks(&self) -> usize { + self.source.walks.lock().expect("walks").len() + } + } + + async fn enabled(probe: Arc) -> Rig { + let mut rig = Rig::new(probe); + rig.send(Command::SetEnabled(true)).await; + rig + } + + #[tokio::test] + async fn should_ignore_heights_and_uncertain_results_while_off() { + let mut rig = Rig::new(Arc::new(ScriptedProbe::new(&[]))); + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + + rig.follow(wallet(), 100).await; + rig.send(Command::Uncertain(txid(1))).await; + + assert_eq!(rig.walks(), 0); + assert!(rig.sent().is_empty()); + } + + /// An `Uncertain` result carries no wallet id: every wallet hears it, but + /// only the one holding the transaction walks its records. + #[tokio::test] + async fn should_act_on_an_uncertain_result_only_in_the_wallet_that_holds_it() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + let other = [9u8; 32]; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.views(other, vec![send(2, &[outpoint(91, 0)])]); + for wallet_id in [wallet(), other] { + rig.send(Command::Seen { + wallet_id, + touched: false, + }) + .await; + } + + rig.send(Command::Uncertain(txid(1))).await; + + assert_eq!(*rig.source.walks.lock().expect("walks"), vec![wallet()]); + assert_eq!(probe.probed(), vec![txid(1)]); + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), unresolved())] + ); + } + + /// Once every root is final the wallet is idle: height advances do not walk + /// its records again until an event that touched them. + #[tokio::test] + async fn should_go_idle_when_every_root_is_final_until_a_touch() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + + rig.follow(wallet(), 100).await; + assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(1), dead())]); + assert_eq!(rig.walks(), 1); + + rig.height(wallet(), 102).await; + rig.send(Command::Seen { + wallet_id: wallet(), + touched: false, + }) + .await; + rig.height(wallet(), 103).await; + assert_eq!( + rig.walks(), + 1, + "idle: no walk, even after an untouching event" + ); + + rig.send(Command::Seen { + wallet_id: wallet(), + touched: true, + }) + .await; + rig.height(wallet(), 104).await; + assert_eq!(rig.walks(), 2); + } + + /// Something touched the wallet while its pass ran: the pass may have read + /// it before, so it must not leave the wallet idle. #[tokio::test] - async fn should_do_nothing_while_probing_is_off() { - let state = Mutex::new(ResolverState { - off: true, - ..ResolverState::default() + async fn should_not_leave_idle_a_wallet_touched_during_its_pass() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + rig.actor.handle(Command::Seen { + wallet_id: wallet(), + touched: true, }); - let probe = ScriptedProbe::new(&[]); + rig.settle().await; + rig.height(wallet(), 102).await; - run( - &state, - &probe, - wallet(), - 100, - &[send(1, &[outpoint(90, 0)])], - &none(), - ) + assert_eq!(rig.walks(), 2); + } + + /// Off clears every verdict and wakes every wallet; back on, the next + /// height re-publishes what was cleared. + #[tokio::test] + async fn should_republish_after_probing_goes_off_and_on() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), dead())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.sent(); + + rig.send(Command::SetEnabled(false)).await; + assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); + + rig.send(Command::SetEnabled(true)).await; + rig.height(wallet(), 102).await; + assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(1), dead())]); + assert_eq!(probe.probed(), vec![txid(1), txid(1)]); + } + + /// Probing turned off while a pass is out on the network: its result is + /// dropped, nothing is published after the clears. + #[tokio::test] + async fn should_drop_a_pass_in_flight_when_probing_goes_off() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + rig.actor.handle(Command::SetEnabled(false)); + rig.settle().await; + + assert!(rig.sent().is_empty()); + assert!(rig.actor.state.published.is_empty()); + } + + #[tokio::test] + async fn should_clear_a_removed_wallets_verdicts_and_forget_it() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.sent(); + + rig.send(Command::WalletRemoved(wallet())).await; + + assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); + assert!(!rig.actor.wallets.contains_key(&wallet())); + } + + /// A late event can register an id the wallet manager no longer holds; + /// the first pass that finds it gone drops it. + #[tokio::test] + async fn should_drop_a_wallet_the_manager_no_longer_holds() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + rig.send(Command::Seen { + wallet_id: wallet(), + touched: false, + }) .await; - assert!(probe.probed().is_empty()); - assert!(outbox(&state).is_empty()); - assert!(state.lock().expect("state").schedules.is_empty()); + rig.send(Command::Uncertain(txid(1))).await; + + assert!(!rig.actor.wallets.contains_key(&wallet())); } - /// A probe that records what the host had been told by the time it ran. + /// A probe that records what the host had been sent by the time it ran. struct SnapshotProbe { - delivered: Arc>>, + sink: Arc, seen: StdMutex>, } #[async_trait] impl AcceptanceProbe for SnapshotProbe { async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { - *self.seen.lock().expect("seen") = self.delivered.lock().expect("delivered").clone(); - unresolved() + *self.seen.lock().expect("seen") = self.sink.0.lock().expect("sink").clone(); + ProbeVerdict::Accepted } } - /// A clear reaches the host before the pass goes out to the network, not - /// after every root's probe. + /// A clear reaches the host before the pass goes out to the network. #[tokio::test] async fn should_deliver_clears_before_probing() { - let state = Mutex::new(ResolverState::default()); - pass( - &state, - &ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)]), - 100, - &[send(1, &[outpoint(90, 0)])], - ) - .await; - let delivered = Arc::new(StdMutex::new(Vec::new())); - let probe = SnapshotProbe { - delivered: Arc::clone(&delivered), + let sink = Arc::new(CollectSink::default()); + let probe = Arc::new(SnapshotProbe { + sink: Arc::clone(&sink), seen: StdMutex::new(Vec::new()), - }; - let flush = || { - let batch = outbox(&state); - delivered.lock().expect("delivered").extend(batch); - }; - let started = state.lock().expect("state").generation(&wallet()); + }); + let mut rig = Rig::with_sink(probe.clone(), sink); + rig.send(Command::SetEnabled(true)).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.sent(); - resolve_pass( - &state, - &probe, - wallet(), - 101, - &[send(2, &[outpoint(91, 0)])], - &none(), - started, - &flush, - ) - .await; + rig.views(wallet(), vec![send(2, &[outpoint(91, 0)])]); + rig.height(wallet(), 102).await; assert_eq!( *probe.seen.lock().expect("seen"), vec![ResolverEvent::Cleared(txid(1))] ); - assert_eq!( - *delivered.lock().expect("delivered"), - vec![ - ResolverEvent::Cleared(txid(1)), - ResolverEvent::Verdict(txid(2), unresolved()), - ] - ); } - /// A long chain on one root: every own send in it, found in one walk. - #[test] - fn should_find_every_own_send_in_a_long_chain() { - let mut views = vec![send(1, &[outpoint(90, 0)])]; - for n in 2..=20u8 { - views.push(send(n, &[outpoint(n - 1, 1)])); + fn chain_lock() -> ChainLock { + ChainLock { + block_height: 100, + block_hash: BlockHash::all_zeros(), + signature: BLSSignature::from([0u8; 96]), } - views.push(incoming(30, &[outpoint(80, 0)])); - let graph = ChainGraph::new(&views, &none()); + } + + /// A chainlock arrives about once per block for every wallet; only one that + /// locked something, or a block that changed records, may leave work. + #[test] + fn should_wake_only_on_events_that_touched_records() { + let block = |inserted: Vec| WalletEvent::BlockProcessed { + wallet_id: wallet(), + height: 100, + chain_lock: None, + inserted, + updated: Vec::new(), + matured: Vec::new(), + balance: WalletCoreBalance::default(), + account_balances: BTreeMap::new(), + addresses_derived: Vec::new(), + }; + let lock = |txids: Vec| WalletEvent::ChainLockProcessed { + wallet_id: wallet(), + chain_lock: chain_lock(), + locked_transactions: BTreeMap::from([(AccountType::CoinJoin { index: 0 }, txids)]), + }; + + assert!(!may_leave_work(&block(Vec::new()))); + assert!(!may_leave_work(&lock(Vec::new()))); + assert!(may_leave_work(&lock(vec![txid(1)]))); + assert!(!may_leave_work(&WalletEvent::SyncHeightAdvanced { + wallet_id: wallet(), + height: 100, + })); + } + + #[test] + fn should_act_on_forced_txids_only_when_held() { + let held = |txid: &Txid| *txid == self::txid(1); + let forced = HashSet::from([txid(1), txid(2)]); assert_eq!( - graph.own_in_chain(&txid(1)), - (1..=20u8).map(txid).collect::>() + forced_held(false, forced.clone(), held), + Some(HashSet::from([txid(1)])) + ); + assert_eq!(forced_held(false, HashSet::from([txid(2)]), held), None); + assert_eq!( + forced_held(true, HashSet::from([txid(2)]), held), + Some(HashSet::new()) ); } } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift index 198f17aafa1..c656f2213e7 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift @@ -931,6 +931,10 @@ public class PlatformWalletManager: ObservableObject { drainingNativeHandle = h handle = NULL_HANDLE isConfigured = false + // The native side forgets every probe verdict on teardown, and its + // clears are dropped once `shutdownRequested` is set: drop them here. + outgoingTransactionVerdicts = [:] + lastOutgoingTransactionProbe = nil // Poll/reconcile cancellation is independent of admitted local-read // generations. Stop remaining reads and reconcile persistence steps // before the blocking shielded stop. From 150355a423066a1c9d2058901cb12c2d2b97bb3d Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 20:44:41 +0300 Subject: [PATCH 014/169] fix(platform-wallet): eighth review pass on the broadcast resolver - A read or probe job that panics ends its run (jobs are tracked by task id), so the wallet's next pass still starts; a host handler that panics loses its event, not the resolver task. - An Uncertain result is routed to every wallet the manager holds, not only to wallets some event has introduced, and the switch is decided in the actor, in order with SetEnabled, instead of by the sender. - Shutdown stops the task through a signal; it aborts and awaits every job it started before it ends, and a panicked task is reported, not Ok. - A wallet waits for the height at which its next root is due, so heights in between do not walk its records (idle is the u32::MAX case). - send() skips the actor slot's lock once the task has started, and only a not-yet-started slot is ever replaced. - record_probe drops a guard for a case the actor made impossible; ManagerSource uses forced_held for every request; stale shutdown docs. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/manager/mod.rs | 8 +- .../src/wallet/core/broadcast_resolver.rs | 470 +++++++++++++----- 2 files changed, 339 insertions(+), 139 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 892781efb55..8b50a0e0f8e 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -74,10 +74,10 @@ pub enum WalletWorker { /// tasks; its drain outcome is folded into the report because those /// tasks clone the FFI persister and can fire host callbacks. DashPayPayments, - /// Broadcast-probe passes spawned by `BroadcastResolver` in response to - /// SPV events. Not a registry worker: the resolver's own tracker closes - /// admission and aborts the passes; its drain outcome is folded into the - /// report because a pass publishes through the host's event callbacks. + /// The `BroadcastResolver` task and the reads and probes it runs. Not a + /// registry worker: shutdown stops the task, which aborts and awaits its + /// jobs; the outcome is folded into the report because the task publishes + /// through the host's event callbacks. BroadcastProbes, /// The wallet-event adapter task — the sink coordinator stores feed /// into. Not a registry worker: joined by diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 4edf96f6589..1d5f0754cea 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -35,7 +35,8 @@ //! height that follows the tip — steps of more than [`CATCH_UP_STEP`] blocks, //! and the first step after launch, are catch-up and skipped — every block //! for [`EVERY_BLOCK_WINDOW`] blocks and every [`SLOW_INTERVAL`] blocks after. -//! A wallet whose last pass had nothing left to probe is idle and not scanned +//! Between passes a wallet waits for the height at which its next root is due; +//! one whose last pass had nothing left to probe is idle and not scanned //! again until a wallet event that touched its records (a new or //! InstantSend-locked transaction, a block or chainlock that changed records, a //! sweep) or an `Uncertain` result for one of its transactions wakes it. `Dead` and `Mined` end @@ -50,9 +51,11 @@ //! are merged into the next. Every host callback comes from that task, never //! from a caller that may hold its own locks. +use std::cell::OnceCell; use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; +use std::panic::{catch_unwind, AssertUnwindSafe}; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Mutex, Weak}; +use std::sync::{Arc, Mutex, PoisonError, Weak}; use std::time::Duration; use async_trait::async_trait; @@ -62,8 +65,8 @@ use dashcore::{Transaction, Txid}; use key_wallet::transaction_checking::TransactionContext; use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use key_wallet_manager::{WalletEvent, WalletId, WalletManager}; -use tokio::sync::{mpsc, RwLock}; -use tokio::task::{AbortHandle, JoinHandle, JoinSet}; +use tokio::sync::{mpsc, oneshot, RwLock}; +use tokio::task::{self, AbortHandle, JoinError, JoinHandle, JoinSet}; use crate::broadcast_probe::{AcceptanceProbe, ProbeVerdict}; use crate::events::{PlatformEventHandler, PlatformEventManager}; @@ -321,6 +324,19 @@ impl ProbeSchedule { fn probed_at(&mut self, height: u32) { self.last_probe = Some(height); } + + /// The lowest height at which [`is_due`](Self::is_due) holds. + pub(crate) fn next_due(&self) -> u32 { + match self.last_probe { + None => 0, + Some(last) + if last.saturating_add(1).saturating_sub(self.first_seen) < EVERY_BLOCK_WINDOW => + { + last.saturating_add(1) + } + Some(last) => last.saturating_add(SLOW_INTERVAL), + } + } } /// A root's final verdict: nothing is left to ask. @@ -582,9 +598,7 @@ pub(crate) fn begin_pass( /// Record one root's probe result: a final verdict ends its probing, and the /// verdict goes to the wallet's own sends — the root when it is one, and, -/// when it is dead, every own send built on it. Nothing is recorded for a -/// root a newer pass has dropped (it settled or left) or whose wallet was -/// forgotten meanwhile. +/// when it is dead, every own send built on it. pub(crate) fn record_probe( state: &mut ResolverState, wallet_id: WalletId, @@ -596,9 +610,6 @@ pub(crate) fn record_probe( ) -> Vec { let mut events = Vec::new(); let key = (wallet_id, root); - if !state.schedules.contains_key(&key) { - return events; - } match verdict { ProbeVerdict::Dead { reason } => { state.finished.insert( @@ -632,18 +643,24 @@ pub(crate) fn record_probe( events } -/// Nothing left to probe for the wallet: no roots, or every root has a final -/// verdict. The wallet can go idle until something new happens. -pub(crate) fn nothing_to_probe( +/// The lowest height at which a root of the wallet can be due, if nothing new +/// happens to it: a root without a schedule yet (the child of one just found +/// mined) is due at once; `u32::MAX` when every root has a final verdict +/// (nothing left to probe) or there are none. +pub(crate) fn next_pass_height( state: &ResolverState, wallet_id: WalletId, views: &[OutgoingView], -) -> bool { +) -> u32 { let mined = state.mined(&wallet_id); ChainGraph::new(views, &mined) .roots() .iter() - .all(|view| state.finished.contains_key(&(wallet_id, view.txid))) + .map(|view| (wallet_id, view.txid)) + .filter(|key| !state.finished.contains_key(key)) + .map(|key| state.schedules.get(&key).map_or(0, ProbeSchedule::next_due)) + .min() + .unwrap_or(u32::MAX) } fn log_outgoing(item: &Outgoing) { @@ -691,6 +708,9 @@ fn forced_held( /// Where passes read wallets from. #[async_trait] pub(crate) trait WalletSource: Send + Sync { + /// The ids of every wallet held now. + async fn wallet_ids(&self) -> Vec; + /// `None`: no such wallet. `Some(None)`: nothing to do (see /// [`forced_held`]) — decided without walking the records. async fn read( @@ -705,6 +725,16 @@ struct ManagerSource(Arc>>); #[async_trait] impl WalletSource for ManagerSource { + async fn wallet_ids(&self) -> Vec { + self.0 + .read() + .await + .list_wallets() + .into_iter() + .copied() + .collect() + } + async fn read( &self, wallet_id: WalletId, @@ -713,16 +743,14 @@ impl WalletSource for ManagerSource { ) -> Option> { let manager = self.0.read().await; let info = manager.get_wallet_info(&wallet_id)?; - let forced = if forced.is_empty() { - by_height.then_some(forced) - } else { - let accounts = info.core_wallet.accounts.all_accounts(); - forced_held(by_height, forced, |txid| { - accounts - .iter() - .any(|account| account.transactions().contains_key(txid)) - }) - }; + // Built at most once, and only if there is a txid to look up. + let accounts = OnceCell::new(); + let forced = forced_held(by_height, forced, |txid| { + accounts + .get_or_init(|| info.core_wallet.accounts.all_accounts()) + .iter() + .any(|account| account.transactions().contains_key(txid)) + }); Some(forced.map(|forced| WalletRead { views: collect_views(info), synced_height: info.core_wallet.synced_height(), @@ -744,7 +772,12 @@ struct EventSink(Mutex>); impl VerdictSink for EventSink { fn deliver(&self, item: &Outgoing) { log_outgoing(item); - let Some(events) = self.0.lock().expect("event sink poisoned").upgrade() else { + let events = self + .0 + .lock() + .unwrap_or_else(PoisonError::into_inner) + .upgrade(); + let Some(events) = events else { return; }; match &item.event { @@ -758,9 +791,17 @@ impl VerdictSink for EventSink { } } +/// Hand `events` to the host. A handler that panics loses its own event, not +/// the resolver: the actor task must outlive a faulty host callback. fn deliver(sink: &Arc, events: Vec) { for item in &events { - sink.deliver(item); + if catch_unwind(AssertUnwindSafe(|| sink.deliver(item))).is_err() { + tracing::error!( + wallet_id = %hex::encode(item.wallet_id), + event = ?item.event, + "broadcast probe: a host handler panicked delivering an event" + ); + } } } @@ -785,6 +826,8 @@ enum Command { /// What a finished job reports back to the actor. enum JobDone { + /// The wallets to route an `Uncertain` result to. + Listed { txid: Txid, wallets: Vec }, Read { wallet_id: WalletId, run: u64, @@ -815,7 +858,8 @@ struct Run { id: u64, /// The wallet's command count when the run started. seq: u64, - /// Something touched the wallet during the run: do not mark it idle. + /// Something touched the wallet during the run: its read may be stale, so + /// the next height must not be skipped. woken: bool, abort: AbortHandle, probing: Option, @@ -832,9 +876,10 @@ struct Probing { struct WalletEntry { /// Last synced height reported, for telling catch-up from following. height: Option, - /// The last pass had nothing to probe: height advances skip the wallet - /// (no walk over its records per block) until something wakes it. - idle: bool, + /// Height advances below this are skipped — no walk over the wallet's + /// records while no root can be due. `u32::MAX` when nothing is left to + /// probe (idle). Reset by anything that may have left new work. + wait_until: Option, pending: Option, run: Option, /// Commands seen for this wallet. A run that found the wallet gone drops @@ -855,6 +900,9 @@ struct Actor { source: Arc, sink: Arc, jobs: JoinSet, + /// Which run each read or probe job belongs to, so a job that panicked + /// can end its run instead of leaving the wallet stuck behind it. + job_runs: HashMap, } impl Actor { @@ -872,20 +920,53 @@ impl Actor { source, sink, jobs: JoinSet::new(), + job_runs: HashMap::new(), } } - async fn run(mut self, mut commands: mpsc::UnboundedReceiver) { + async fn run( + mut self, + mut commands: mpsc::UnboundedReceiver, + mut stop: oneshot::Receiver<()>, + ) { loop { tokio::select! { + biased; + _ = &mut stop => break, command = commands.recv() => match command { Some(command) => self.handle(command), - None => return, + None => break, }, - Some(done) = self.jobs.join_next(), if !self.jobs.is_empty() => { - // An aborted job (its run was dropped) reports nothing. - if let Ok(done) = done { - self.job_done(done); + Some(joined) = self.jobs.join_next_with_id(), if !self.jobs.is_empty() => { + self.joined(joined); + } + } + } + // Abort every read and probe and wait for them, so nothing the + // resolver started outlives it. + self.jobs.shutdown().await; + } + + fn joined(&mut self, joined: Result<(task::Id, JobDone), JoinError>) { + match joined { + Ok((id, done)) => { + self.job_runs.remove(&id); + self.job_done(done); + } + Err(error) => { + let owner = self.job_runs.remove(&error.id()); + if !error.is_panic() { + return; // aborted: its run was dropped on purpose + } + tracing::error!(?owner, %error, "broadcast probe: a job panicked"); + // End the run it belonged to, as if it had found nothing, + // so the wallet's next pass can start. + if let Some((wallet_id, run)) = owner { + if let Some(entry) = self.wallets.get_mut(&wallet_id) { + if entry.run.as_ref().is_some_and(|current| current.id == run) { + entry.run = None; + self.start(wallet_id); + } } } } @@ -905,7 +986,10 @@ impl Actor { let enabled = self.enabled; let entry = self.entry(wallet_id); let previous = entry.height.replace(height); - if !enabled || entry.idle || is_catch_up_step(previous, height) { + if !enabled + || entry.wait_until.is_some_and(|until| height < until) + || is_catch_up_step(previous, height) + { return; } let pending = entry.pending.get_or_insert_with(PendingPass::default); @@ -916,35 +1000,37 @@ impl Actor { Command::Seen { wallet_id, touched } => { let entry = self.entry(wallet_id); if touched { - entry.idle = false; + entry.wait_until = None; if let Some(run) = entry.run.as_mut() { run.woken = true; } } } Command::Uncertain(txid) => { + // The switch is decided here, in order with SetEnabled — not + // by the sender, which may see the flag before the actor does. if !self.enabled { return; } - let wallets: Vec = self.wallets.keys().copied().collect(); - for wallet_id in wallets { - if let Some(entry) = self.wallets.get_mut(&wallet_id) { - let pending = entry.pending.get_or_insert_with(PendingPass::default); - pending.height = pending.height.max(entry.height.unwrap_or(0)); - pending.forced.insert(txid); + // Routed to every wallet the manager holds, including ones no + // event has introduced yet. + let source = Arc::clone(&self.source); + self.jobs.spawn(async move { + JobDone::Listed { + txid, + wallets: source.wallet_ids().await, } - self.start(wallet_id); - } + }); } Command::SetEnabled(enabled) => { if enabled == self.enabled { return; } self.enabled = enabled; - // Either way every wallet leaves idle, so turning probing - // back on re-publishes what turning it off cleared. + // Either way every wallet waits for nothing, so turning + // probing back on re-publishes what turning it off cleared. for entry in self.wallets.values_mut() { - entry.idle = false; + entry.wait_until = None; if !enabled { entry.pending = None; if let Some(run) = entry.run.take() { @@ -992,6 +1078,7 @@ impl Actor { read, } }); + self.job_runs.insert(abort.id(), (wallet_id, run)); entry.run = Some(Run { id: run, seq: entry.seq, @@ -1003,6 +1090,18 @@ impl Actor { fn job_done(&mut self, done: JobDone) { match done { + JobDone::Listed { txid, wallets } => { + if !self.enabled { + return; + } + for wallet_id in wallets { + let entry = self.wallets.entry(wallet_id).or_default(); + let pending = entry.pending.get_or_insert_with(PendingPass::default); + pending.height = pending.height.max(entry.height.unwrap_or(0)); + pending.forced.insert(txid); + self.start(wallet_id); + } + } JobDone::Read { wallet_id, run, @@ -1012,14 +1111,15 @@ impl Actor { let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; - let Some(current) = entry.run.as_mut().filter(|current| current.id == run) else { - return; + let run_seq = match entry.run.as_ref() { + Some(current) if current.id == run => current.seq, + _ => return, }; match read { None => { // Gone from the wallet manager (its removal clears), // or an id a late event brought back after removal. - if current.seq == entry.seq { + if run_seq == entry.seq { self.wallets.remove(&wallet_id); } else { entry.run = None; @@ -1032,7 +1132,7 @@ impl Actor { } Some(Some(read)) => { if !read.forced.is_empty() { - entry.idle = false; + entry.wait_until = None; } // A request queued before any height was reported // carries 0; the wallet's own synced height is better. @@ -1044,12 +1144,14 @@ impl Actor { &read.views, &read.forced, ); - current.probing = Some(Probing { - views: read.views, - height, - forced: read.forced, - due: start.due, - }); + if let Some(current) = entry.run.as_mut() { + current.probing = Some(Probing { + views: read.views, + height, + forced: read.forced, + due: start.due, + }); + } // Clears and re-published dead verdicts reach the host // before the pass goes out to the network. deliver(&self.sink, start.events); @@ -1110,11 +1212,11 @@ impl Actor { verdict, } }); + self.job_runs.insert(run.abort.id(), (wallet_id, id)); return; } - if !run.woken && nothing_to_probe(&self.state, wallet_id, &probing.views) { - entry.idle = true; - } + entry.wait_until = + (!run.woken).then(|| next_pass_height(&self.state, wallet_id, &probing.views)); entry.run = None; self.start(wallet_id); } @@ -1123,7 +1225,7 @@ impl Actor { enum ActorSlot { /// Waiting for the first command sent from inside a Tokio runtime. NotStarted(Box, mpsc::UnboundedReceiver), - Running(JoinHandle<()>), + Running(JoinHandle<()>, oneshot::Sender<()>), Closed, } @@ -1139,6 +1241,9 @@ enum ActorSlot { pub(crate) struct BroadcastResolver { enabled: AtomicBool, commands: mpsc::UnboundedSender, + /// The actor has been started (or the resolver closed): `send` no longer + /// needs the slot. + started: AtomicBool, actor: Mutex, sink: Arc, } @@ -1158,6 +1263,7 @@ impl BroadcastResolver { Self { enabled: AtomicBool::new(false), commands, + started: AtomicBool::new(false), actor: Mutex::new(ActorSlot::NotStarted(Box::new(actor), receiver)), sink, } @@ -1166,35 +1272,41 @@ impl BroadcastResolver { /// Where verdicts are delivered. Weak: the event manager holds this /// resolver as one of its handlers. pub(crate) fn set_event_manager(&self, events: Weak) { - *self.sink.0.lock().expect("event sink poisoned") = events; + *self.sink.0.lock().unwrap_or_else(PoisonError::into_inner) = events; } fn send(&self, command: Command) { // Fails only once the actor is gone (shutdown); nothing to do then. let _ = self.commands.send(command); - let mut slot = self.actor.lock().expect("resolver actor slot poisoned"); + if self.started.load(Ordering::Acquire) { + return; + } + let Ok(runtime) = tokio::runtime::Handle::try_current() else { + return; // queued; started by the first command sent from a runtime + }; + let mut slot = self.actor.lock().unwrap_or_else(PoisonError::into_inner); if matches!(*slot, ActorSlot::NotStarted(..)) { - if let Ok(runtime) = tokio::runtime::Handle::try_current() { - if let ActorSlot::NotStarted(actor, receiver) = - std::mem::replace(&mut *slot, ActorSlot::Closed) - { - *slot = ActorSlot::Running(runtime.spawn(actor.run(receiver))); - } + if let ActorSlot::NotStarted(actor, receiver) = + std::mem::replace(&mut *slot, ActorSlot::Closed) + { + let (stop, stopped) = oneshot::channel(); + *slot = ActorSlot::Running(runtime.spawn(actor.run(receiver, stopped)), stop); } } + self.started.store(true, Ordering::Release); } /// Turn probing on or off. Turning it off forgets every send and has the /// host told to drop every verdict it holds — with no passes running, /// nothing would ever clear them — and drops the passes in flight. Either - /// way every wallet leaves idle, so turning it back on re-publishes what - /// was cleared. Returns at once; the clears follow from the resolver's - /// task. + /// way every wallet is woken, so turning it back on re-publishes what was + /// cleared. Returns at once; the clears follow from the resolver's task. pub(crate) fn set_enabled(&self, enabled: bool) { self.enabled.store(enabled, Ordering::SeqCst); self.send(Command::SetEnabled(enabled)); } + /// The last value passed to [`set_enabled`](Self::set_enabled). pub(crate) fn is_enabled(&self) -> bool { self.enabled.load(Ordering::SeqCst) } @@ -1206,19 +1318,31 @@ impl BroadcastResolver { self.send(Command::WalletRemoved(*wallet_id)); } - /// Stop the resolver's task, and with it every probe, waiting at most - /// `budget` for it to end; after this no host callback comes from the - /// resolver. Returns `false` if the task outlived the budget. Idempotent. + /// Stop the resolver's task: it aborts every read and probe it started and + /// waits for them, all within `budget`; after this no host callback comes + /// from the resolver. Returns `false` if that did not finish in time (the + /// task is then aborted) or the task had died of a panic. Idempotent. pub(crate) async fn quiesce_within(&self, budget: Duration) -> bool { + self.started.store(true, Ordering::Release); let slot = std::mem::replace( - &mut *self.actor.lock().expect("resolver actor slot poisoned"), + &mut *self.actor.lock().unwrap_or_else(PoisonError::into_inner), ActorSlot::Closed, ); - let ActorSlot::Running(handle) = slot else { + let ActorSlot::Running(mut handle, stop) = slot else { return true; }; - handle.abort(); - tokio::time::timeout(budget, handle).await.is_ok() + let _ = stop.send(()); + match tokio::time::timeout(budget, &mut handle).await { + Ok(Ok(())) => true, + Ok(Err(error)) => { + tracing::error!(%error, "broadcast probe: the resolver task had died"); + false + } + Err(_) => { + handle.abort(); + false + } + } } } @@ -1263,9 +1387,7 @@ impl EventHandler for BroadcastResolver { result: BroadcastResult::Uncertain, } = event { - if self.is_enabled() { - self.send(Command::Uncertain(*txid)); - } + self.send(Command::Uncertain(*txid)); } } } @@ -1460,6 +1582,26 @@ mod tests { assert!(s.is_due(late + SLOW_INTERVAL)); } + /// `next_due` is exactly the first height `is_due` accepts. + #[test] + fn should_agree_on_the_next_due_height_with_is_due() { + let first = 100; + let mut schedule = ProbeSchedule::new(first); + for probed in [ + first, + first + 5, + first + EVERY_BLOCK_WINDOW - 1, + first + EVERY_BLOCK_WINDOW + 3, + ] { + schedule.probed_at(probed); + let next = schedule.next_due(); + assert!(schedule.is_due(next), "due at {next}"); + for height in probed + 1..next { + assert!(!schedule.is_due(height), "not due at {height}"); + } + } + } + #[test] fn should_treat_only_large_height_steps_as_catch_up() { assert!( @@ -1557,7 +1699,7 @@ mod tests { } /// One pass: begin, probe each due root with no lock held, record. - /// Returns whether nothing is left to probe. + /// Returns the height the wallet may wait for. async fn run( state: &Mutex, probe: &dyn AcceptanceProbe, @@ -1565,7 +1707,7 @@ mod tests { height: u32, views: &[OutgoingView], forced: &HashSet, - ) -> bool { + ) -> u32 { let due = { let mut guard = state.lock().expect("state"); let start = begin_pass(&mut guard.state, wallet_id, height, views, forced); @@ -1587,7 +1729,7 @@ mod tests { guard.push(events); } let guard = state.lock().expect("state"); - nothing_to_probe(&guard.state, wallet_id, views) + next_pass_height(&guard.state, wallet_id, views) } fn outbox(state: &Mutex) -> Vec { @@ -1815,41 +1957,6 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1), txid(2)]); } - /// A probe that, while it is out on the network, lets a concurrent pass - /// clear the root (it settled): the late verdict must not be published. - struct ClearingProbe { - state: Arc>, - } - - #[async_trait] - impl AcceptanceProbe for ClearingProbe { - async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { - self.state.lock().expect("state").forget_wallet(&wallet()); - ProbeVerdict::Accepted - } - } - - #[tokio::test] - async fn should_not_publish_a_verdict_for_a_root_cleared_during_the_probe() { - let state = Arc::new(Mutex::new(Harness::default())); - let probe = ClearingProbe { - state: Arc::clone(&state), - }; - - run( - &state, - &probe, - wallet(), - 100, - &[send(1, &[outpoint(90, 0)])], - &none(), - ) - .await; - - assert!(outbox(&state).is_empty()); - assert!(state.lock().expect("state").state.published.is_empty()); - } - /// A send built on a dead root's change after the root was found dead is /// dead too, and hears it without the root being asked again. #[tokio::test] @@ -1897,9 +2004,9 @@ mod tests { ) .await; - assert!(empty); - assert!(dead_only); - assert!(!open); + assert_eq!(empty, u32::MAX); + assert_eq!(dead_only, u32::MAX); + assert_eq!(open, 102, "the open root is due next block"); } // ---- ResolverState ------------------------------------------------------ @@ -2012,6 +2119,10 @@ mod tests { #[async_trait] impl WalletSource for FakeSource { + async fn wallet_ids(&self) -> Vec { + self.views.lock().expect("views").keys().copied().collect() + } + async fn read( &self, wallet_id: WalletId, @@ -2082,10 +2193,8 @@ mod tests { } async fn settle(&mut self) { - while let Some(done) = self.actor.jobs.join_next().await { - if let Ok(done) = done { - self.actor.job_done(done); - } + while let Some(joined) = self.actor.jobs.join_next_with_id().await { + self.actor.joined(joined); } } @@ -2272,15 +2381,106 @@ mod tests { #[tokio::test] async fn should_drop_a_wallet_the_manager_no_longer_holds() { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; - rig.send(Command::Seen { + + rig.height(wallet(), 100).await; + rig.actor.handle(Command::Height { wallet_id: wallet(), - touched: false, - }) - .await; + height: 101, + }); + rig.settle().await; + + assert!(!rig.actor.wallets.contains_key(&wallet())); + } + + /// An `Uncertain` result right after launch, before any event introduced + /// the wallet, still gets its immediate probe. + #[tokio::test] + async fn should_route_an_uncertain_result_to_a_wallet_no_event_introduced() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.send(Command::Uncertain(txid(1))).await; - assert!(!rig.actor.wallets.contains_key(&wallet())); + assert_eq!(probe.probed(), vec![txid(1)]); + } + + /// A probe that panics. + struct PanickingProbe; + + #[async_trait] + impl AcceptanceProbe for PanickingProbe { + async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { + panic!("probe bug"); + } + } + + /// A job that panics ends its run; the wallet's next pass still starts. + #[tokio::test] + async fn should_not_leave_a_wallet_stuck_behind_a_panicked_job() { + let mut rig = enabled(Arc::new(PanickingProbe)).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + + rig.follow(wallet(), 100).await; + rig.height(wallet(), 102).await; + + assert_eq!(rig.walks(), 2); + assert!(rig.actor.wallets[&wallet()].run.is_none()); + } + + /// After the every-block window a root is due only every SLOW_INTERVAL + /// blocks: the heights in between do not walk the wallet's records. + #[tokio::test] + async fn should_not_read_the_wallet_on_heights_where_no_root_is_due() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[( + txid(1), + ProbeVerdict::Accepted, + )]))) + .await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + for height in 102..=101 + EVERY_BLOCK_WINDOW { + rig.height(wallet(), height).await; + } + let walks = rig.walks(); + + for height in 102 + EVERY_BLOCK_WINDOW..=101 + EVERY_BLOCK_WINDOW + SLOW_INTERVAL { + rig.height(wallet(), height).await; + } + + assert_eq!(rig.walks(), walks + 1, "one walk in SLOW_INTERVAL blocks"); + } + + /// A probe that never answers. + struct HangingProbe; + + #[async_trait] + impl AcceptanceProbe for HangingProbe { + async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { + std::future::pending().await + } + } + + /// Stopping the task aborts and awaits every job it started, so nothing + /// outlives it. + #[tokio::test] + async fn should_end_with_every_job_when_stopped() { + let rig = Rig::new(Arc::new(HangingProbe)); + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + let (commands, receiver) = mpsc::unbounded_channel(); + let (stop, stopped) = oneshot::channel(); + let task = tokio::spawn(rig.actor.run(receiver, stopped)); + for command in [Command::SetEnabled(true), Command::Uncertain(txid(1))] { + commands.send(command).expect("send"); + } + tokio::time::sleep(Duration::from_millis(50)).await; + + stop.send(()).expect("stop"); + + tokio::time::timeout(Duration::from_secs(5), task) + .await + .expect("stopped in time") + .expect("no panic"); } /// A probe that records what the host had been sent by the time it ran. From de8a8df4526f65bb79b5bbb463d02b8fed74eb1f Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 20:59:11 +0300 Subject: [PATCH 015/169] fix(platform-wallet): ninth review pass on the broadcast resolver - A pass that finds its wallet gone forgets the wallet and clears its verdicts: rolled-back registrations and loads drop a wallet without wallet_removed, and their verdicts used to stay with the host. - An Uncertain result is routed through one lookup of the wallets holding the transaction (one lock, no reads of the others), and a forced pass skips a root already sent to the network at its height after the result arrived, so a height pass racing the lookup does not double the probes. - A job that panics ends its run and wakes the wallet (every wallet for a panicked lookup); a verdict a host handler panicked on is unpublished so a later pass sends it again. Documented that the iOS profiles abort instead. - The shutdown report gets the resolver's real status (Ok, NotRunning, Panicked, Stopped, Timeout) instead of a bool folded into Ok/Timeout. - A forced read no longer resets the wallet's wait, which only queued a repeat pass; the three copies of the clear logic are one clear_where. - broadcast_probe imports dashcore::consensus instead of a qualified call. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/broadcast_probe.rs | 3 +- .../rs-platform-wallet/src/manager/mod.rs | 15 +- .../src/wallet/core/broadcast_resolver.rs | 276 ++++++++++++------ 3 files changed, 198 insertions(+), 96 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index ac0b871fbb4..895fad7ecff 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -46,6 +46,7 @@ use dash_sdk::dapi_client::transport::TransportError; use dash_sdk::dapi_client::{DapiClientError, DapiRequestExecutor, RequestSettings}; use dash_sdk::dapi_grpc::core::v0::{BroadcastTransactionRequest, GetTransactionRequest}; use dash_sdk::dapi_grpc::tonic::Code; +use dashcore::consensus; use dashcore::{Transaction, Txid}; /// Core reject reasons that prove the transaction can never be mined as it @@ -281,7 +282,7 @@ pub(crate) struct DapiNodeSubmitter { impl NodeSubmitter for DapiNodeSubmitter { async fn submit(&self, transaction: &Transaction) -> (Option, NodeVerdict) { let request = BroadcastTransactionRequest { - transaction: dashcore::consensus::serialize(transaction), + transaction: consensus::serialize(transaction), allow_high_fees: false, bypass_limits: false, }; diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 8b50a0e0f8e..7938b4444c7 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -968,10 +968,10 @@ impl PlatformWalletManager

{ // The broadcast resolver's task publishes through the host's event // callbacks too, so it must be gone before destroy returns; it and its // probes are safe to abort. Both drains run concurrently under one shared budget. - let (payments_drained, probes_drained) = tokio::join!( + let (payments_drained, probes_status) = tokio::join!( self.dashpay_payment_handler .quiesce_within(PAYMENT_DRAIN_BUDGET), - self.broadcast_resolver.quiesce_within(PAYMENT_DRAIN_BUDGET), + self.broadcast_resolver.stop_within(PAYMENT_DRAIN_BUDGET), ); // Drain the coordinators concurrently against one shared budget so @@ -1049,14 +1049,9 @@ impl PlatformWalletManager

{ WorkerStatus::Timeout }, ); - report.per_worker.insert( - WalletWorker::BroadcastProbes, - if probes_drained { - WorkerStatus::Ok - } else { - WorkerStatus::Timeout - }, - ); + report + .per_worker + .insert(WalletWorker::BroadcastProbes, probes_status); // The wallet-event adapter is the sink the coordinators' stores // feed into, so it drains AFTER them. It is a plain tokio task, diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 1d5f0754cea..2bf3aaafac0 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -59,6 +59,7 @@ use std::sync::{Arc, Mutex, PoisonError, Weak}; use std::time::Duration; use async_trait::async_trait; +use dash_async::WorkerStatus; use dash_spv::sync::SyncEvent; use dash_spv::{BroadcastResult, EventHandler}; use dashcore::{Transaction, Txid}; @@ -410,39 +411,39 @@ impl ResolverState { fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { self.schedules.retain(|(wallet, _), _| wallet != wallet_id); self.finished.retain(|(wallet, _), _| wallet != wallet_id); - let mut cleared: Vec = self - .published - .keys() - .filter(|(wallet, _)| wallet == wallet_id) - .map(|(_, txid)| *txid) - .collect(); - cleared.sort(); - self.published.retain(|(wallet, _), _| wallet != wallet_id); - cleared - .into_iter() - .map(|txid| Outgoing { - wallet_id: *wallet_id, - event: ResolverEvent::Cleared(txid), - height: None, - trigger: "removed", - }) - .collect() + self.clear_where(|wallet, _| wallet == wallet_id, None, "removed") } /// Drop everything; a clear for every published send. fn forget_all(&mut self) -> Vec { self.schedules.clear(); self.finished.clear(); - let mut cleared: Vec<(WalletId, Txid)> = - self.published.drain().map(|(key, _)| key).collect(); + self.clear_where(|_, _| true, None, "disabled") + } + + /// Unpublish every send `drop` matches; a clear for each, in a stable order. + fn clear_where( + &mut self, + drop: impl Fn(&WalletId, &Txid) -> bool, + height: Option, + trigger: &'static str, + ) -> Vec { + let mut cleared: Vec<(WalletId, Txid)> = self + .published + .keys() + .filter(|(wallet, txid)| drop(wallet, txid)) + .copied() + .collect(); cleared.sort(); + self.published + .retain(|(wallet, txid), _| !drop(wallet, txid)); cleared .into_iter() .map(|(wallet_id, txid)| Outgoing { wallet_id, event: ResolverEvent::Cleared(txid), - height: None, - trigger: "disabled", + height, + trigger, }) .collect() } @@ -543,24 +544,11 @@ pub(crate) fn begin_pass( state .finished .retain(|(wallet, txid), _| !is_gone(wallet, txid)); - let mut cleared: Vec = state - .published - .keys() - .filter(|(wallet, txid)| *wallet == wallet_id && !own_present.contains(txid)) - .map(|(_, txid)| *txid) - .collect(); - cleared.sort(); - state - .published - .retain(|(wallet, txid), _| *wallet != wallet_id || own_present.contains(txid)); - for txid in cleared { - events.push(Outgoing { - wallet_id, - event: ResolverEvent::Cleared(txid), - height: Some(height), - trigger, - }); - } + events.extend(state.clear_where( + |wallet, txid| *wallet == wallet_id && !own_present.contains(txid), + Some(height), + trigger, + )); let mined = state.mined(&wallet_id); let graph = ChainGraph::new(views, &mined); @@ -708,8 +696,8 @@ fn forced_held( /// Where passes read wallets from. #[async_trait] pub(crate) trait WalletSource: Send + Sync { - /// The ids of every wallet held now. - async fn wallet_ids(&self) -> Vec; + /// The wallets whose records hold `txid` — one look under one lock. + async fn holders(&self, txid: Txid) -> Vec; /// `None`: no such wallet. `Some(None)`: nothing to do (see /// [`forced_held`]) — decided without walking the records. @@ -725,12 +713,20 @@ struct ManagerSource(Arc>>); #[async_trait] impl WalletSource for ManagerSource { - async fn wallet_ids(&self) -> Vec { - self.0 - .read() - .await + async fn holders(&self, txid: Txid) -> Vec { + let manager = self.0.read().await; + manager .list_wallets() .into_iter() + .filter(|wallet_id| { + manager.get_wallet_info(wallet_id).is_some_and(|info| { + info.core_wallet + .accounts + .all_accounts() + .iter() + .any(|account| account.transactions().contains_key(&txid)) + }) + }) .copied() .collect() } @@ -791,9 +787,11 @@ impl VerdictSink for EventSink { } } -/// Hand `events` to the host. A handler that panics loses its own event, not -/// the resolver: the actor task must outlive a faulty host callback. -fn deliver(sink: &Arc, events: Vec) { +/// Hand `events` to the host. A handler that panics does not take the +/// resolver down, and a verdict it failed to take is unpublished so a later +/// pass sends it again. (Where panics abort — the iOS profiles — none of this +/// runs; it matters where they unwind.) +fn deliver(sink: &Arc, state: &mut ResolverState, events: Vec) { for item in &events { if catch_unwind(AssertUnwindSafe(|| sink.deliver(item))).is_err() { tracing::error!( @@ -801,6 +799,9 @@ fn deliver(sink: &Arc, events: Vec) { event = ?item.event, "broadcast probe: a host handler panicked delivering an event" ); + if let ResolverEvent::Verdict(txid, _) = &item.event { + state.published.remove(&(item.wallet_id, *txid)); + } } } } @@ -826,12 +827,18 @@ enum Command { /// What a finished job reports back to the actor. enum JobDone { - /// The wallets to route an `Uncertain` result to. - Listed { txid: Txid, wallets: Vec }, + /// The wallets holding a transaction reported `Uncertain` at command + /// `since`. + Listed { + txid: Txid, + since: u64, + wallets: Vec, + }, Read { wallet_id: WalletId, run: u64, height: u32, + forced_since: Option, read: Option>, }, Probed { @@ -849,6 +856,8 @@ struct PendingPass { /// results did. by_height: bool, forced: HashSet, + /// The earliest command at which one of `forced` was reported. + forced_since: Option, } /// A pass in flight: reading the wallet, then probing its due roots one at @@ -878,7 +887,10 @@ struct WalletEntry { height: Option, /// Height advances below this are skipped — no walk over the wallet's /// records while no root can be due. `u32::MAX` when nothing is left to - /// probe (idle). Reset by anything that may have left new work. + /// probe (idle). Reset by anything that may have left new work — which + /// assumes every change to the wallet's records arrives as a touching + /// wallet event (see [`may_leave_work`]); a path that changes records + /// without emitting one must send the resolver such an event too. wait_until: Option, pending: Option, run: Option, @@ -903,6 +915,12 @@ struct Actor { /// Which run each read or probe job belongs to, so a job that panicked /// can end its run instead of leaving the wallet stuck behind it. job_runs: HashMap, + /// Commands handled so far: orders an `Uncertain` result against probes. + tick: u64, + /// When each root was last sent to the network: the command count and the + /// pass height. A forced pass skips a root already probed at its height + /// since the `Uncertain` result arrived. + probed: HashMap<(WalletId, Txid), (u64, u32)>, } impl Actor { @@ -921,6 +939,8 @@ impl Actor { sink, jobs: JoinSet::new(), job_runs: HashMap::new(), + tick: 0, + probed: HashMap::new(), } } @@ -959,13 +979,24 @@ impl Actor { return; // aborted: its run was dropped on purpose } tracing::error!(?owner, %error, "broadcast probe: a job panicked"); - // End the run it belonged to, as if it had found nothing, - // so the wallet's next pass can start. - if let Some((wallet_id, run)) = owner { - if let Some(entry) = self.wallets.get_mut(&wallet_id) { - if entry.run.as_ref().is_some_and(|current| current.id == run) { - entry.run = None; - self.start(wallet_id); + // End the run it belonged to and let the next height probe the + // wallet again — what the run carried (a forced txid) is lost, + // but its roots are due to the schedule. A panicked lookup + // for an `Uncertain` result belongs to no run: wake every + // wallet instead. + match owner { + Some((wallet_id, run)) => { + if let Some(entry) = self.wallets.get_mut(&wallet_id) { + if entry.run.as_ref().is_some_and(|current| current.id == run) { + entry.run = None; + entry.wait_until = None; + self.start(wallet_id); + } + } + } + None => { + for entry in self.wallets.values_mut() { + entry.wait_until = None; } } } @@ -981,6 +1012,7 @@ impl Actor { } fn handle(&mut self, command: Command) { + self.tick += 1; match command { Command::Height { wallet_id, height } => { let enabled = self.enabled; @@ -1012,13 +1044,15 @@ impl Actor { if !self.enabled { return; } - // Routed to every wallet the manager holds, including ones no - // event has introduced yet. + // Routed to the wallets holding it, including ones no event + // has introduced yet. let source = Arc::clone(&self.source); + let since = self.tick; self.jobs.spawn(async move { JobDone::Listed { txid, - wallets: source.wallet_ids().await, + since, + wallets: source.holders(txid).await, } }); } @@ -1039,16 +1073,18 @@ impl Actor { } } if !enabled { + self.probed.clear(); let events = self.state.forget_all(); - deliver(&self.sink, events); + deliver(&self.sink, &mut self.state, events); } } Command::WalletRemoved(wallet_id) => { if let Some(run) = self.wallets.remove(&wallet_id).and_then(|entry| entry.run) { run.abort.abort(); } + self.probed.retain(|(wallet, _), _| *wallet != wallet_id); let events = self.state.forget_wallet(&wallet_id); - deliver(&self.sink, events); + deliver(&self.sink, &mut self.state, events); } } } @@ -1075,6 +1111,7 @@ impl Actor { wallet_id, run, height: pending.height, + forced_since: pending.forced_since, read, } }); @@ -1090,7 +1127,11 @@ impl Actor { fn job_done(&mut self, done: JobDone) { match done { - JobDone::Listed { txid, wallets } => { + JobDone::Listed { + txid, + since, + wallets, + } => { if !self.enabled { return; } @@ -1099,6 +1140,8 @@ impl Actor { let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(entry.height.unwrap_or(0)); pending.forced.insert(txid); + pending.forced_since = + Some(pending.forced_since.map_or(since, |s| s.min(since))); self.start(wallet_id); } } @@ -1106,6 +1149,7 @@ impl Actor { wallet_id, run, height, + forced_since, read, } => { let Some(entry) = self.wallets.get_mut(&wallet_id) else { @@ -1117,33 +1161,53 @@ impl Actor { }; match read { None => { - // Gone from the wallet manager (its removal clears), - // or an id a late event brought back after removal. + // Gone from the wallet manager — not always through + // `wallet_removed` (a failed registration or load is + // rolled back without it) — so forget it here too; + // clearing twice is harmless. Keep the entry if a + // command for a same-id wallet arrived meanwhile. if run_seq == entry.seq { self.wallets.remove(&wallet_id); } else { entry.run = None; - self.start(wallet_id); } + self.probed.retain(|(wallet, _), _| *wallet != wallet_id); + let events = self.state.forget_wallet(&wallet_id); + deliver(&self.sink, &mut self.state, events); + self.start(wallet_id); } Some(None) => { entry.run = None; self.start(wallet_id); } Some(Some(read)) => { - if !read.forced.is_empty() { - entry.wait_until = None; - } // A request queued before any height was reported // carries 0; the wallet's own synced height is better. let height = height.max(read.synced_height); - let start = begin_pass( + let mut start = begin_pass( &mut self.state, wallet_id, height, &read.views, &read.forced, ); + // Forget send times of roots that settled or left. + self.probed.retain(|(wallet, txid), _| { + *wallet != wallet_id || read.views.iter().any(|view| view.txid == *txid) + }); + // A root already sent to the network at this height + // after the `Uncertain` result arrived needs no second + // probe — the forced one would only repeat it. + if let Some(since) = forced_since { + let probed = &self.probed; + start.due.retain(|(root, _)| { + !probed.get(&(wallet_id, *root)).is_some_and( + |&(tick, probed_height)| { + tick >= since && probed_height == height + }, + ) + }); + } if let Some(current) = entry.run.as_mut() { current.probing = Some(Probing { views: read.views, @@ -1154,7 +1218,7 @@ impl Actor { } // Clears and re-published dead verdicts reach the host // before the pass goes out to the network. - deliver(&self.sink, start.events); + deliver(&self.sink, &mut self.state, start.events); self.probe_next(wallet_id); } } @@ -1183,7 +1247,7 @@ impl Actor { root, &verdict, ); - deliver(&self.sink, events); + deliver(&self.sink, &mut self.state, events); self.probe_next(wallet_id); } } @@ -1213,6 +1277,8 @@ impl Actor { } }); self.job_runs.insert(run.abort.id(), (wallet_id, id)); + self.probed + .insert((wallet_id, root), (self.tick, probing.height)); return; } entry.wait_until = @@ -1320,27 +1386,26 @@ impl BroadcastResolver { /// Stop the resolver's task: it aborts every read and probe it started and /// waits for them, all within `budget`; after this no host callback comes - /// from the resolver. Returns `false` if that did not finish in time (the - /// task is then aborted) or the task had died of a panic. Idempotent. - pub(crate) async fn quiesce_within(&self, budget: Duration) -> bool { + /// from the resolver. `NotRunning` if it never started or was already + /// stopped; `Timeout` if it did not end in time (it is then aborted); + /// `Panicked` if it had died. Idempotent. + pub(crate) async fn stop_within(&self, budget: Duration) -> WorkerStatus { self.started.store(true, Ordering::Release); let slot = std::mem::replace( &mut *self.actor.lock().unwrap_or_else(PoisonError::into_inner), ActorSlot::Closed, ); let ActorSlot::Running(mut handle, stop) = slot else { - return true; + return WorkerStatus::NotRunning; }; let _ = stop.send(()); match tokio::time::timeout(budget, &mut handle).await { - Ok(Ok(())) => true, - Ok(Err(error)) => { - tracing::error!(%error, "broadcast probe: the resolver task had died"); - false - } + Ok(Ok(())) => WorkerStatus::Ok, + Ok(Err(error)) if error.is_panic() => WorkerStatus::Panicked(error.to_string()), + Ok(Err(error)) => WorkerStatus::Stopped(Some(error.to_string())), Err(_) => { handle.abort(); - false + WorkerStatus::Timeout } } } @@ -2119,8 +2184,14 @@ mod tests { #[async_trait] impl WalletSource for FakeSource { - async fn wallet_ids(&self) -> Vec { - self.views.lock().expect("views").keys().copied().collect() + async fn holders(&self, txid: Txid) -> Vec { + self.views + .lock() + .expect("views") + .iter() + .filter(|(_, views)| views.iter().any(|view| view.txid == txid)) + .map(|(wallet_id, _)| *wallet_id) + .collect() } async fn read( @@ -2405,6 +2476,41 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1)]); } + /// A wallet the manager dropped without `wallet_removed` (a rolled-back + /// registration or load): the pass that finds it gone clears its verdicts. + #[tokio::test] + async fn should_clear_the_verdicts_of_a_wallet_gone_without_a_removal() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.sent(); + + rig.source.views.lock().expect("views").remove(&wallet()); + rig.height(wallet(), 102).await; + + assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); + assert!(rig.actor.state.schedules.is_empty()); + } + + /// An `Uncertain` result that lands while a height pass already probes the + /// root at that height does not send it to the network a second time. + #[tokio::test] + async fn should_not_probe_a_root_twice_at_one_height_for_an_uncertain_result() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + rig.actor.handle(Command::Uncertain(txid(1))); + rig.settle().await; + + assert_eq!(probe.probed(), vec![txid(1)]); + } + /// A probe that panics. struct PanickingProbe; From 9ceb6c42a6ea820e81fd8f79e0cfc538ddef6b59 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 21:07:21 +0300 Subject: [PATCH 016/169] fix(platform-wallet): tenth review pass on the broadcast resolver - Registration, setup and load rollbacks call wallet_removed after they drop a wallet, so its verdicts are cleared at the source, not only when a later pass happens to read the id as gone. - A stop that runs out of budget keeps the task handle; a retried shutdown waits for it again instead of reporting the resolver NotRunning while a host callback may still be running. - A height that arrives while a pass runs is queued and dropped at the end of the pass only if no root is due at it, so a forced pass on an idle wallet no longer costs the new root its next-block probe. - The forced-probe dedup keys on the latest Uncertain result, forgets the send time of a probe that panicked, and counts a lookup's result as a command for the wallet (a stale gone-read cannot drop its request). - The host-handler panic guard no longer claims a re-send: the event a panicking handler was given is lost, and the docs say so. - One holds() helper for the holders lookup and the read; the send-time pruning uses a set of present txids. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/manager/load.rs | 7 +- .../src/manager/wallet_lifecycle.rs | 14 +- .../src/wallet/core/broadcast_resolver.rs | 195 +++++++++++++----- 3 files changed, 151 insertions(+), 65 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 1bbd0330172..16948d64f58 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -465,12 +465,13 @@ impl PlatformWalletManager

{ } continue; } - if let Err(e) = wm.remove_wallet(id) { - tracing::warn!( + match wm.remove_wallet(id) { + Ok(_) => self.broadcast_resolver.wallet_removed(id), + Err(e) => tracing::warn!( wallet_id = %hex::encode(id), error = %e, "rollback after load failure: remove_wallet failed" - ); + ), } } } diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index eddf33da1e4..c90065b9ae2 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -543,12 +543,13 @@ impl PlatformWalletManager

{ "failed to persist wallet registration changeset" ); let mut wm = self.wallet_manager.write().await; - if let Err(remove_err) = wm.remove_wallet(&wallet_id) { - tracing::warn!( + match wm.remove_wallet(&wallet_id) { + Ok(_) => self.broadcast_resolver.wallet_removed(&wallet_id), + Err(remove_err) => tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %remove_err, "rollback: remove_wallet failed while unwinding a failed wallet registration" - ); + ), } return Err(PlatformWalletError::from_store_failure(&*self.persister, e)); } @@ -591,12 +592,13 @@ impl PlatformWalletManager

{ "failed to restore persisted platform-address state" ); let mut wm = self.wallet_manager.write().await; - if let Err(remove_err) = wm.remove_wallet(&wallet_id) { - tracing::warn!( + match wm.remove_wallet(&wallet_id) { + Ok(_) => self.broadcast_resolver.wallet_removed(&wallet_id), + Err(remove_err) => tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %remove_err, "rollback: remove_wallet failed while unwinding a failed wallet setup" - ); + ), } // Wrap the already-typed error rather than stringify it, so // its concrete variant and source chain survive. diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 2bf3aaafac0..d06b96ec1b8 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -51,7 +51,6 @@ //! are merged into the next. Every host callback comes from that task, never //! from a caller that may hold its own locks. -use std::cell::OnceCell; use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; use std::panic::{catch_unwind, AssertUnwindSafe}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -709,6 +708,16 @@ pub(crate) trait WalletSource: Send + Sync { ) -> Option>; } +/// Whether any account of the wallet records `txid` — a map lookup per +/// account, not a walk over the records. +fn holds(info: &PlatformWalletInfo, txid: &Txid) -> bool { + info.core_wallet + .accounts + .all_accounts() + .iter() + .any(|account| account.transactions().contains_key(txid)) +} + struct ManagerSource(Arc>>); #[async_trait] @@ -719,13 +728,9 @@ impl WalletSource for ManagerSource { .list_wallets() .into_iter() .filter(|wallet_id| { - manager.get_wallet_info(wallet_id).is_some_and(|info| { - info.core_wallet - .accounts - .all_accounts() - .iter() - .any(|account| account.transactions().contains_key(&txid)) - }) + manager + .get_wallet_info(wallet_id) + .is_some_and(|info| holds(info, &txid)) }) .copied() .collect() @@ -739,14 +744,9 @@ impl WalletSource for ManagerSource { ) -> Option> { let manager = self.0.read().await; let info = manager.get_wallet_info(&wallet_id)?; - // Built at most once, and only if there is a txid to look up. - let accounts = OnceCell::new(); - let forced = forced_held(by_height, forced, |txid| { - accounts - .get_or_init(|| info.core_wallet.accounts.all_accounts()) - .iter() - .any(|account| account.transactions().contains_key(txid)) - }); + // Checked again under this lock: the record may have left since the + // holders lookup (a sweep, a removal). + let forced = forced_held(by_height, forced, |txid| holds(info, txid)); Some(forced.map(|forced| WalletRead { views: collect_views(info), synced_height: info.core_wallet.synced_height(), @@ -788,20 +788,17 @@ impl VerdictSink for EventSink { } /// Hand `events` to the host. A handler that panics does not take the -/// resolver down, and a verdict it failed to take is unpublished so a later -/// pass sends it again. (Where panics abort — the iOS profiles — none of this -/// runs; it matters where they unwind.) -fn deliver(sink: &Arc, state: &mut ResolverState, events: Vec) { +/// resolver down, but the event it panicked on is lost — it is not sent +/// again. A panicking handler is a host bug; this only keeps probing alive +/// where panics unwind (the iOS profiles abort instead). +fn deliver(sink: &Arc, events: Vec) { for item in &events { if catch_unwind(AssertUnwindSafe(|| sink.deliver(item))).is_err() { tracing::error!( wallet_id = %hex::encode(item.wallet_id), event = ?item.event, - "broadcast probe: a host handler panicked delivering an event" + "broadcast probe: a host handler panicked; its event is lost" ); - if let ResolverEvent::Verdict(txid, _) = &item.event { - state.published.remove(&(item.wallet_id, *txid)); - } } } } @@ -856,7 +853,7 @@ struct PendingPass { /// results did. by_height: bool, forced: HashSet, - /// The earliest command at which one of `forced` was reported. + /// The latest command at which one of `forced` was reported. forced_since: Option, } @@ -914,7 +911,8 @@ struct Actor { jobs: JoinSet, /// Which run each read or probe job belongs to, so a job that panicked /// can end its run instead of leaving the wallet stuck behind it. - job_runs: HashMap, + /// A probe job also names its root, whose send time a panic withdraws. + job_runs: HashMap)>, /// Commands handled so far: orders an `Uncertain` result against probes. tick: u64, /// When each root was last sent to the network: the command count and the @@ -985,7 +983,12 @@ impl Actor { // for an `Uncertain` result belongs to no run: wake every // wallet instead. match owner { - Some((wallet_id, run)) => { + Some((wallet_id, run, root)) => { + // The probe never reached a verdict: it must not + // count as sent for the forced-probe dedup. + if let Some(root) = root { + self.probed.remove(&(wallet_id, root)); + } if let Some(entry) = self.wallets.get_mut(&wallet_id) { if entry.run.as_ref().is_some_and(|current| current.id == run) { entry.run = None; @@ -1018,8 +1021,10 @@ impl Actor { let enabled = self.enabled; let entry = self.entry(wallet_id); let previous = entry.height.replace(height); + // While a pass runs, its end decides the wait: queue the height + // and let `probe_next` drop it if no root turns out due. if !enabled - || entry.wait_until.is_some_and(|until| height < until) + || (entry.run.is_none() && entry.wait_until.is_some_and(|until| height < until)) || is_catch_up_step(previous, height) { return; @@ -1075,7 +1080,7 @@ impl Actor { if !enabled { self.probed.clear(); let events = self.state.forget_all(); - deliver(&self.sink, &mut self.state, events); + deliver(&self.sink, events); } } Command::WalletRemoved(wallet_id) => { @@ -1084,7 +1089,7 @@ impl Actor { } self.probed.retain(|(wallet, _), _| *wallet != wallet_id); let events = self.state.forget_wallet(&wallet_id); - deliver(&self.sink, &mut self.state, events); + deliver(&self.sink, events); } } } @@ -1115,7 +1120,7 @@ impl Actor { read, } }); - self.job_runs.insert(abort.id(), (wallet_id, run)); + self.job_runs.insert(abort.id(), (wallet_id, run, None)); entry.run = Some(Run { id: run, seq: entry.seq, @@ -1136,12 +1141,16 @@ impl Actor { return; } for wallet_id in wallets { - let entry = self.wallets.entry(wallet_id).or_default(); + // Counted like a command: a run that read the id as gone + // before this must not drop the entry and its request. + let entry = self.entry(wallet_id); let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(entry.height.unwrap_or(0)); pending.forced.insert(txid); + // The latest result: a probe sent before it must not + // stand in for the forced probe it asks for. pending.forced_since = - Some(pending.forced_since.map_or(since, |s| s.min(since))); + Some(pending.forced_since.map_or(since, |s| s.max(since))); self.start(wallet_id); } } @@ -1173,7 +1182,7 @@ impl Actor { } self.probed.retain(|(wallet, _), _| *wallet != wallet_id); let events = self.state.forget_wallet(&wallet_id); - deliver(&self.sink, &mut self.state, events); + deliver(&self.sink, events); self.start(wallet_id); } Some(None) => { @@ -1192,8 +1201,10 @@ impl Actor { &read.forced, ); // Forget send times of roots that settled or left. + let present: HashSet = + read.views.iter().map(|view| view.txid).collect(); self.probed.retain(|(wallet, txid), _| { - *wallet != wallet_id || read.views.iter().any(|view| view.txid == *txid) + *wallet != wallet_id || present.contains(txid) }); // A root already sent to the network at this height // after the `Uncertain` result arrived needs no second @@ -1218,7 +1229,7 @@ impl Actor { } // Clears and re-published dead verdicts reach the host // before the pass goes out to the network. - deliver(&self.sink, &mut self.state, start.events); + deliver(&self.sink, start.events); self.probe_next(wallet_id); } } @@ -1247,7 +1258,7 @@ impl Actor { root, &verdict, ); - deliver(&self.sink, &mut self.state, events); + deliver(&self.sink, events); self.probe_next(wallet_id); } } @@ -1276,7 +1287,8 @@ impl Actor { verdict, } }); - self.job_runs.insert(run.abort.id(), (wallet_id, id)); + self.job_runs + .insert(run.abort.id(), (wallet_id, id, Some(root))); self.probed .insert((wallet_id, root), (self.tick, probing.height)); return; @@ -1284,6 +1296,13 @@ impl Actor { entry.wait_until = (!run.woken).then(|| next_pass_height(&self.state, wallet_id, &probing.views)); entry.run = None; + // A height queued during the run that no root is due at is dropped; + // a forced request never is. + if let (Some(pending), Some(until)) = (&entry.pending, entry.wait_until) { + if pending.forced.is_empty() && pending.height < until { + entry.pending = None; + } + } self.start(wallet_id); } } @@ -1292,6 +1311,9 @@ enum ActorSlot { /// Waiting for the first command sent from inside a Tokio runtime. NotStarted(Box, mpsc::UnboundedReceiver), Running(JoinHandle<()>, oneshot::Sender<()>), + /// Told to stop but not yet ended within a budget: a retried stop joins + /// it again rather than reporting it gone. + Stopping(JoinHandle<()>), Closed, } @@ -1387,26 +1409,38 @@ impl BroadcastResolver { /// Stop the resolver's task: it aborts every read and probe it started and /// waits for them, all within `budget`; after this no host callback comes /// from the resolver. `NotRunning` if it never started or was already - /// stopped; `Timeout` if it did not end in time (it is then aborted); - /// `Panicked` if it had died. Idempotent. + /// stopped; `Timeout` if it did not end in time (it is then aborted, and + /// a later call waits for it again); `Panicked` if it had died. pub(crate) async fn stop_within(&self, budget: Duration) -> WorkerStatus { self.started.store(true, Ordering::Release); - let slot = std::mem::replace( - &mut *self.actor.lock().unwrap_or_else(PoisonError::into_inner), - ActorSlot::Closed, - ); - let ActorSlot::Running(mut handle, stop) = slot else { - return WorkerStatus::NotRunning; - }; - let _ = stop.send(()); - match tokio::time::timeout(budget, &mut handle).await { - Ok(Ok(())) => WorkerStatus::Ok, - Ok(Err(error)) if error.is_panic() => WorkerStatus::Panicked(error.to_string()), - Ok(Err(error)) => WorkerStatus::Stopped(Some(error.to_string())), - Err(_) => { - handle.abort(); - WorkerStatus::Timeout - } + stop_slot(&self.actor, budget).await + } +} + +/// See [`BroadcastResolver::stop_within`]. +async fn stop_slot(actor: &Mutex, budget: Duration) -> WorkerStatus { + let slot = std::mem::replace( + &mut *actor.lock().unwrap_or_else(PoisonError::into_inner), + ActorSlot::Closed, + ); + let mut handle = match slot { + ActorSlot::Running(handle, stop) => { + let _ = stop.send(()); + handle + } + ActorSlot::Stopping(handle) => handle, + ActorSlot::NotStarted(..) | ActorSlot::Closed => return WorkerStatus::NotRunning, + }; + match tokio::time::timeout(budget, &mut handle).await { + Ok(Ok(())) => WorkerStatus::Ok, + Ok(Err(error)) if error.is_panic() => WorkerStatus::Panicked(error.to_string()), + Ok(Err(error)) => WorkerStatus::Stopped(Some(error.to_string())), + Err(_) => { + // An abort cannot interrupt a host callback in progress: keep + // the handle so a retried stop waits for it again. + handle.abort(); + *actor.lock().unwrap_or_else(PoisonError::into_inner) = ActorSlot::Stopping(handle); + WorkerStatus::Timeout } } } @@ -2511,6 +2545,55 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1)]); } + /// A wallet waiting for nothing (idle) is forced by an `Uncertain` result; + /// a height that arrives while that pass runs is kept, so the new root + /// gets its next-block probe. + #[tokio::test] + async fn should_keep_a_height_that_arrives_during_a_forced_pass() { + let probe = Arc::new(ScriptedProbe::new(&[ + (txid(1), dead()), + (txid(2), unresolved()), + ])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.height(wallet(), 102).await; + assert_eq!(probe.probed(), vec![txid(1)], "idle after the dead root"); + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); + + rig.actor.handle(Command::Uncertain(txid(2))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.actor.joined(listed); + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 103, + }); + rig.settle().await; + + assert_eq!(probe.probed(), vec![txid(1), txid(2), txid(2)]); + } + + /// A stop that runs out of budget keeps the task: a retried stop waits for + /// it again and never reports it gone while it still runs. + #[tokio::test] + async fn should_not_report_a_task_that_outlived_a_stop_as_not_running() { + let (stop, _stopped) = oneshot::channel(); + // A blocking task stands in for a host callback that abort cannot cut. + let handle = tokio::task::spawn_blocking(|| std::thread::sleep(Duration::from_millis(300))); + let slot = Mutex::new(ActorSlot::Running(handle, stop)); + + let first = stop_slot(&slot, Duration::from_millis(20)).await; + let second = stop_slot(&slot, Duration::from_secs(5)).await; + let third = stop_slot(&slot, Duration::from_secs(5)).await; + + assert_eq!(first, WorkerStatus::Timeout); + assert_eq!(second, WorkerStatus::Ok); + assert_eq!(third, WorkerStatus::NotRunning); + } + /// A probe that panics. struct PanickingProbe; From ce4d6aa0d7ba4aa04fe44bcdd982efe644901e7d Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 21:17:09 +0300 Subject: [PATCH 017/169] fix(platform-wallet): eleventh review pass on the broadcast resolver - An event a host handler panicked on is queued and handed over again before the actor's next step, while it still matches the state (a verdict still published, a clear of a send still unpublished), up to three times. Replaces the tenth pass's "the event is lost". - A stop that runs out of budget no longer aborts the task, so a retried stop can report it clean once it ends; stops take turns, so a concurrent stop waits instead of reporting a live task NotRunning. - Every way a run ends goes through end_run, which drops a height queued during the run that no root is due at. - The forced-probe dedup is per reported txid: a root is not forced again when it was sent at this height after its own txid was reported. Done in begin_pass, which also returns the present set the actor prunes with. - The gone-wallet comment describes what can still reach it now that every rollback sends WalletRemoved. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 335 +++++++++++++----- 1 file changed, 252 insertions(+), 83 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index d06b96ec1b8..f0f80d75db4 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -65,8 +65,9 @@ use dashcore::{Transaction, Txid}; use key_wallet::transaction_checking::TransactionContext; use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use key_wallet_manager::{WalletEvent, WalletId, WalletManager}; -use tokio::sync::{mpsc, oneshot, RwLock}; +use tokio::sync::{mpsc, oneshot, Mutex as AsyncMutex, RwLock}; use tokio::task::{self, AbortHandle, JoinError, JoinHandle, JoinSet}; +use tokio::time::{timeout_at, Instant}; use crate::broadcast_probe::{AcceptanceProbe, ProbeVerdict}; use crate::events::{PlatformEventHandler, PlatformEventManager}; @@ -511,6 +512,8 @@ pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { pub(crate) struct PassStart { pub events: Vec, pub due: VecDeque<(Txid, Transaction)>, + /// The txids the wallet still holds unsettled. + pub present: HashSet, } /// Start a pass over one wallet at `height`: forget what settled or left @@ -526,6 +529,7 @@ pub(crate) fn begin_pass( height: u32, views: &[OutgoingView], forced: &HashSet, + already_sent: impl Fn(&Txid, &Txid) -> bool, ) -> PassStart { let trigger = trigger_of(forced); let mut events = Vec::new(); @@ -552,9 +556,16 @@ pub(crate) fn begin_pass( let mined = state.mined(&wallet_id); let graph = ChainGraph::new(views, &mined); let roots = graph.roots(); + // `already_sent(txid, root)`: the root was sent to the network at this + // height after `txid` was reported — forcing it again would only repeat. let forced_roots: BTreeSet = forced .iter() - .flat_map(|txid| graph.roots_of(txid)) + .flat_map(|txid| { + graph + .roots_of(txid) + .into_iter() + .filter(|root| !already_sent(txid, root)) + }) .collect(); for view in &roots { @@ -580,7 +591,11 @@ pub(crate) fn begin_pass( due.push_back((view.txid, view.transaction.clone())); } } - PassStart { events, due } + PassStart { + events, + due, + present, + } } /// Record one root's probe result: a final verdict ends its probing, and the @@ -787,19 +802,37 @@ impl VerdictSink for EventSink { } } +/// How many times one event is handed to a host handler that panics on it. +const DELIVERY_ATTEMPTS: u8 = 3; + +/// Events a host handler panicked on, with the attempts made so far. +type Retries = VecDeque<(Outgoing, u8)>; + /// Hand `events` to the host. A handler that panics does not take the -/// resolver down, but the event it panicked on is lost — it is not sent -/// again. A panicking handler is a host bug; this only keeps probing alive -/// where panics unwind (the iOS profiles abort instead). -fn deliver(sink: &Arc, events: Vec) { - for item in &events { - if catch_unwind(AssertUnwindSafe(|| sink.deliver(item))).is_err() { - tracing::error!( - wallet_id = %hex::encode(item.wallet_id), - event = ?item.event, - "broadcast probe: a host handler panicked; its event is lost" - ); - } +/// resolver down: the event is queued and handed over again before the +/// actor's next step, while it still says what the state says, up to +/// [`DELIVERY_ATTEMPTS`] times. (Where panics abort — the iOS profiles — the +/// app ends instead.) +fn deliver(sink: &Arc, retries: &mut Retries, events: Vec) { + for item in events { + attempt(sink, retries, item, 1); + } +} + +fn attempt(sink: &Arc, retries: &mut Retries, item: Outgoing, attempts: u8) { + if catch_unwind(AssertUnwindSafe(|| sink.deliver(&item))).is_ok() { + return; + } + let giving_up = attempts >= DELIVERY_ATTEMPTS; + tracing::error!( + wallet_id = %hex::encode(item.wallet_id), + event = ?item.event, + attempts, + giving_up, + "broadcast probe: a host handler panicked delivering an event" + ); + if !giving_up { + retries.push_back((item, attempts)); } } @@ -835,7 +868,7 @@ enum JobDone { wallet_id: WalletId, run: u64, height: u32, - forced_since: Option, + forced: HashMap, read: Option>, }, Probed { @@ -852,9 +885,8 @@ struct PendingPass { /// A height advance asked for this pass; otherwise only `Uncertain` /// results did. by_height: bool, - forced: HashSet, - /// The latest command at which one of `forced` was reported. - forced_since: Option, + /// Each txid reported `Uncertain`, with the command it was reported at. + forced: HashMap, } /// A pass in flight: reading the wallet, then probing its due roots one at @@ -919,6 +951,9 @@ struct Actor { /// pass height. A forced pass skips a root already probed at its height /// since the `Uncertain` result arrived. probed: HashMap<(WalletId, Txid), (u64, u32)>, + /// Events a host handler panicked on, handed over again before the next + /// command or job result. + retries: Retries, } impl Actor { @@ -939,6 +974,7 @@ impl Actor { job_runs: HashMap::new(), tick: 0, probed: HashMap::new(), + retries: VecDeque::new(), } } @@ -966,6 +1002,7 @@ impl Actor { } fn joined(&mut self, joined: Result<(task::Id, JobDone), JoinError>) { + self.retry_deliveries(); match joined { Ok((id, done)) => { self.job_runs.remove(&id); @@ -991,9 +1028,8 @@ impl Actor { } if let Some(entry) = self.wallets.get_mut(&wallet_id) { if entry.run.as_ref().is_some_and(|current| current.id == run) { - entry.run = None; entry.wait_until = None; - self.start(wallet_id); + self.end_run(wallet_id); } } } @@ -1015,6 +1051,7 @@ impl Actor { } fn handle(&mut self, command: Command) { + self.retry_deliveries(); self.tick += 1; match command { Command::Height { wallet_id, height } => { @@ -1080,7 +1117,7 @@ impl Actor { if !enabled { self.probed.clear(); let events = self.state.forget_all(); - deliver(&self.sink, events); + deliver(&self.sink, &mut self.retries, events); } } Command::WalletRemoved(wallet_id) => { @@ -1089,7 +1126,7 @@ impl Actor { } self.probed.retain(|(wallet, _), _| *wallet != wallet_id); let events = self.state.forget_wallet(&wallet_id); - deliver(&self.sink, events); + deliver(&self.sink, &mut self.retries, events); } } } @@ -1110,13 +1147,17 @@ impl Actor { let source = Arc::clone(&self.source); let abort = self.jobs.spawn(async move { let read = source - .read(wallet_id, pending.by_height, pending.forced) + .read( + wallet_id, + pending.by_height, + pending.forced.keys().copied().collect(), + ) .await; JobDone::Read { wallet_id, run, height: pending.height, - forced_since: pending.forced_since, + forced: pending.forced, read, } }); @@ -1146,11 +1187,10 @@ impl Actor { let entry = self.entry(wallet_id); let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(entry.height.unwrap_or(0)); - pending.forced.insert(txid); - // The latest result: a probe sent before it must not - // stand in for the forced probe it asks for. - pending.forced_since = - Some(pending.forced_since.map_or(since, |s| s.max(since))); + // The latest report of the txid counts: a probe sent + // before it must not stand in for the one it asks for. + let at = pending.forced.entry(txid).or_insert(since); + *at = (*at).max(since); self.start(wallet_id); } } @@ -1158,7 +1198,7 @@ impl Actor { wallet_id, run, height, - forced_since, + forced: reported, read, } => { let Some(entry) = self.wallets.get_mut(&wallet_id) else { @@ -1170,55 +1210,49 @@ impl Actor { }; match read { None => { - // Gone from the wallet manager — not always through - // `wallet_removed` (a failed registration or load is - // rolled back without it) — so forget it here too; - // clearing twice is harmless. Keep the entry if a - // command for a same-id wallet arrived meanwhile. - if run_seq == entry.seq { + // Gone from the wallet manager. Every removal path + // also sends `WalletRemoved`, but that command may + // still be queued, or a lookup may have raced it: + // forget the wallet here too — clearing twice is + // harmless. Keep the entry if a command for a same-id + // wallet arrived meanwhile. + let keep = run_seq != entry.seq; + if !keep { self.wallets.remove(&wallet_id); - } else { - entry.run = None; } self.probed.retain(|(wallet, _), _| *wallet != wallet_id); let events = self.state.forget_wallet(&wallet_id); - deliver(&self.sink, events); - self.start(wallet_id); - } - Some(None) => { - entry.run = None; - self.start(wallet_id); + deliver(&self.sink, &mut self.retries, events); + if keep { + self.end_run(wallet_id); + } } + Some(None) => self.end_run(wallet_id), Some(Some(read)) => { // A request queued before any height was reported // carries 0; the wallet's own synced height is better. let height = height.max(read.synced_height); - let mut start = begin_pass( + let probed = &self.probed; + let start = begin_pass( &mut self.state, wallet_id, height, &read.views, &read.forced, + |txid, root| { + reported.get(txid).is_some_and(|&since| { + probed.get(&(wallet_id, *root)).is_some_and( + |&(tick, probed_height)| { + tick >= since && probed_height == height + }, + ) + }) + }, ); // Forget send times of roots that settled or left. - let present: HashSet = - read.views.iter().map(|view| view.txid).collect(); self.probed.retain(|(wallet, txid), _| { - *wallet != wallet_id || present.contains(txid) + *wallet != wallet_id || start.present.contains(txid) }); - // A root already sent to the network at this height - // after the `Uncertain` result arrived needs no second - // probe — the forced one would only repeat it. - if let Some(since) = forced_since { - let probed = &self.probed; - start.due.retain(|(root, _)| { - !probed.get(&(wallet_id, *root)).is_some_and( - |&(tick, probed_height)| { - tick >= since && probed_height == height - }, - ) - }); - } if let Some(current) = entry.run.as_mut() { current.probing = Some(Probing { views: read.views, @@ -1229,7 +1263,7 @@ impl Actor { } // Clears and re-published dead verdicts reach the host // before the pass goes out to the network. - deliver(&self.sink, start.events); + deliver(&self.sink, &mut self.retries, start.events); self.probe_next(wallet_id); } } @@ -1258,7 +1292,7 @@ impl Actor { root, &verdict, ); - deliver(&self.sink, events); + deliver(&self.sink, &mut self.retries, events); self.probe_next(wallet_id); } } @@ -1295,9 +1329,17 @@ impl Actor { } entry.wait_until = (!run.woken).then(|| next_pass_height(&self.state, wallet_id, &probing.views)); + self.end_run(wallet_id); + } + + /// End the wallet's run and start what is queued — except a height + /// queued during the run that no root is due at; a forced request always + /// runs. + fn end_run(&mut self, wallet_id: WalletId) { + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; entry.run = None; - // A height queued during the run that no root is due at is dropped; - // a forced request never is. if let (Some(pending), Some(until)) = (&entry.pending, entry.wait_until) { if pending.forced.is_empty() && pending.height < until { entry.pending = None; @@ -1305,6 +1347,27 @@ impl Actor { } self.start(wallet_id); } + + /// Hand the host again what a panicking handler did not take, while it + /// still says what the state says: a verdict still published, a clear of + /// a send still unpublished. + fn retry_deliveries(&mut self) { + for (item, attempts) in std::mem::take(&mut self.retries) { + let current = match &item.event { + ResolverEvent::Verdict(txid, verdict) => self + .state + .published + .get(&(item.wallet_id, *txid)) + .is_some_and(|published| same_for_host(published, verdict)), + ResolverEvent::Cleared(txid) => { + !self.state.published.contains_key(&(item.wallet_id, *txid)) + } + }; + if current { + attempt(&self.sink, &mut self.retries, item, attempts + 1); + } + } + } } enum ActorSlot { @@ -1333,6 +1396,8 @@ pub(crate) struct BroadcastResolver { /// needs the slot. started: AtomicBool, actor: Mutex, + /// Serialises [`stop_within`](Self::stop_within) calls. + stopping: AsyncMutex<()>, sink: Arc, } @@ -1353,6 +1418,7 @@ impl BroadcastResolver { commands, started: AtomicBool::new(false), actor: Mutex::new(ActorSlot::NotStarted(Box::new(actor), receiver)), + stopping: AsyncMutex::new(()), sink, } } @@ -1409,16 +1475,26 @@ impl BroadcastResolver { /// Stop the resolver's task: it aborts every read and probe it started and /// waits for them, all within `budget`; after this no host callback comes /// from the resolver. `NotRunning` if it never started or was already - /// stopped; `Timeout` if it did not end in time (it is then aborted, and - /// a later call waits for it again); `Panicked` if it had died. + /// stopped; `Timeout` if it did not end in time (a later call waits for + /// it again); `Panicked` if it had died. Concurrent calls take turns. pub(crate) async fn stop_within(&self, budget: Duration) -> WorkerStatus { self.started.store(true, Ordering::Release); - stop_slot(&self.actor, budget).await + stop_slot(&self.actor, &self.stopping, budget).await } } /// See [`BroadcastResolver::stop_within`]. -async fn stop_slot(actor: &Mutex, budget: Duration) -> WorkerStatus { +async fn stop_slot( + actor: &Mutex, + stopping: &AsyncMutex<()>, + budget: Duration, +) -> WorkerStatus { + let deadline = Instant::now() + budget; + // One stop at a time: a second caller waits for the first instead of + // finding the slot emptied and reporting a live task as not running. + let Ok(_one_at_a_time) = timeout_at(deadline, stopping.lock()).await else { + return WorkerStatus::Timeout; + }; let slot = std::mem::replace( &mut *actor.lock().unwrap_or_else(PoisonError::into_inner), ActorSlot::Closed, @@ -1431,14 +1507,14 @@ async fn stop_slot(actor: &Mutex, budget: Duration) -> WorkerStatus { ActorSlot::Stopping(handle) => handle, ActorSlot::NotStarted(..) | ActorSlot::Closed => return WorkerStatus::NotRunning, }; - match tokio::time::timeout(budget, &mut handle).await { + match timeout_at(deadline, &mut handle).await { Ok(Ok(())) => WorkerStatus::Ok, Ok(Err(error)) if error.is_panic() => WorkerStatus::Panicked(error.to_string()), Ok(Err(error)) => WorkerStatus::Stopped(Some(error.to_string())), Err(_) => { - // An abort cannot interrupt a host callback in progress: keep - // the handle so a retried stop waits for it again. - handle.abort(); + // Told to stop, still going — likely inside a host callback. Not + // aborted: a retried stop waits for it to end on its own and can + // then report it clean. *actor.lock().unwrap_or_else(PoisonError::into_inner) = ActorSlot::Stopping(handle); WorkerStatus::Timeout } @@ -1809,7 +1885,14 @@ mod tests { ) -> u32 { let due = { let mut guard = state.lock().expect("state"); - let start = begin_pass(&mut guard.state, wallet_id, height, views, forced); + let start = begin_pass( + &mut guard.state, + wallet_id, + height, + views, + forced, + |_, _| false, + ); guard.push(start.events); start.due }; @@ -2576,24 +2659,110 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1), txid(2), txid(2)]); } - /// A stop that runs out of budget keeps the task: a retried stop waits for - /// it again and never reports it gone while it still runs. + /// A stop that runs out of budget keeps the task without aborting it: a + /// retried stop waits for it to end and reports it clean. #[tokio::test] - async fn should_not_report_a_task_that_outlived_a_stop_as_not_running() { + async fn should_report_a_task_that_outlived_a_stop_clean_on_retry() { let (stop, _stopped) = oneshot::channel(); - // A blocking task stands in for a host callback that abort cannot cut. - let handle = tokio::task::spawn_blocking(|| std::thread::sleep(Duration::from_millis(300))); + let handle = tokio::spawn(tokio::time::sleep(Duration::from_millis(300))); let slot = Mutex::new(ActorSlot::Running(handle, stop)); + let stopping = AsyncMutex::new(()); - let first = stop_slot(&slot, Duration::from_millis(20)).await; - let second = stop_slot(&slot, Duration::from_secs(5)).await; - let third = stop_slot(&slot, Duration::from_secs(5)).await; + let first = stop_slot(&slot, &stopping, Duration::from_millis(20)).await; + let second = stop_slot(&slot, &stopping, Duration::from_secs(5)).await; + let third = stop_slot(&slot, &stopping, Duration::from_secs(5)).await; assert_eq!(first, WorkerStatus::Timeout); assert_eq!(second, WorkerStatus::Ok); assert_eq!(third, WorkerStatus::NotRunning); } + /// Two stops at once: the second waits for the first and never reports + /// the task gone while it still runs. + #[tokio::test] + async fn should_not_report_a_running_task_as_not_running_to_a_concurrent_stop() { + let (stop, _stopped) = oneshot::channel(); + let handle = tokio::spawn(tokio::time::sleep(Duration::from_millis(200))); + let slot = Mutex::new(ActorSlot::Running(handle, stop)); + let stopping = AsyncMutex::new(()); + let started = Instant::now(); + + let (first, second) = tokio::join!( + stop_slot(&slot, &stopping, Duration::from_secs(5)), + stop_slot(&slot, &stopping, Duration::from_secs(5)), + ); + + assert_eq!(first, WorkerStatus::Ok); + assert_eq!(second, WorkerStatus::NotRunning); + assert!( + started.elapsed() >= Duration::from_millis(150), + "waited for the task" + ); + } + + /// A host handler that panics on the first delivery only. + #[derive(Default)] + struct FlakySink { + calls: StdMutex, + taken: StdMutex>, + } + + impl VerdictSink for FlakySink { + fn deliver(&self, item: &Outgoing) { + let first = { + let mut calls = self.calls.lock().expect("calls"); + *calls += 1; + *calls == 1 + }; + if first { + panic!("host handler bug"); + } + self.taken.lock().expect("taken").push(item.event.clone()); + } + } + + /// An event a handler panicked on is handed over again before the next + /// step, while it still holds. + #[tokio::test] + async fn should_hand_over_again_an_event_a_handler_panicked_on() { + let sink = Arc::new(FlakySink::default()); + let source = Arc::new(FakeSource::default()); + let mut actor = Actor::new( + Arc::new(ScriptedProbe::new(&[(txid(1), dead())])), + Arc::clone(&source) as Arc, + Arc::clone(&sink) as Arc, + ); + source + .views + .lock() + .expect("views") + .insert(wallet(), vec![send(1, &[outpoint(90, 0)])]); + actor.handle(Command::SetEnabled(true)); + for height in [100, 101] { + actor.handle(Command::Height { + wallet_id: wallet(), + height, + }); + while let Some(joined) = actor.jobs.join_next_with_id().await { + actor.joined(joined); + } + } + assert!( + sink.taken.lock().expect("taken").is_empty(), + "the first try panicked" + ); + + actor.handle(Command::Seen { + wallet_id: wallet(), + touched: false, + }); + + assert_eq!( + *sink.taken.lock().expect("taken"), + vec![ResolverEvent::Verdict(txid(1), dead())] + ); + } + /// A probe that panics. struct PanickingProbe; From 750e720d508268bb7d49313e6a7ed7bebafe2a2d Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 21:52:23 +0300 Subject: [PATCH 018/169] fix(platform-wallet): twelfth review pass on the broadcast resolver - A probe that came back Unresolved (every node failed) no longer counts as sent for the forced-probe dedup, so an Uncertain result still gets its probe at that height. - A root's schedule is stamped when its probe goes out (mark_sent), not when the pass is planned, so roots a run never sent stay due. - The delivery retry queue is dropped again: the only production handler (FFI) cannot panic and the release profiles abort; the guard logs and drops the event for a Rust embedder whose handler panics, as documented. - begin_pass takes the reported txids with their report ticks and a sent_at lookup instead of a set plus a two-key closure; record_probe takes the trigger. - The gone-wallet path resets the wallet's wait before ending the run; the shutdown comment says the task is stopped, not aborted. - Tests: per-txid dedup with two txids, Unresolved not deduped, unsent roots stay due, a queued height dropped when a forced read finds nothing. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/manager/mod.rs | 6 +- .../src/wallet/core/broadcast_resolver.rs | 342 +++++++++--------- 2 files changed, 174 insertions(+), 174 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 7938b4444c7..f3b14884fd9 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -966,8 +966,10 @@ impl PlatformWalletManager

{ // callback-after-destroy hazard the report exists to catch. // // The broadcast resolver's task publishes through the host's event - // callbacks too, so it must be gone before destroy returns; it and its - // probes are safe to abort. Both drains run concurrently under one shared budget. + // callbacks too, so it must be gone before destroy returns: it is told + // to stop, aborts and awaits its probes, and ends. It is not aborted + // itself — it may be inside a host callback — so a Timeout here means + // it may still call the host. Both drains share one budget. let (payments_drained, probes_status) = tokio::join!( self.dashpay_payment_handler .quiesce_within(PAYMENT_DRAIN_BUDGET), diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index f0f80d75db4..396f3515681 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -385,8 +385,8 @@ pub(crate) struct Outgoing { /// What started a pass: dash-spv's `Uncertain` result for one of the /// wallet's transactions, or an advance of its synced height. -fn trigger_of(forced: &HashSet) -> &'static str { - if forced.is_empty() { +fn trigger_of(forced: bool) -> &'static str { + if !forced { "height" } else { "uncertain" @@ -528,10 +528,10 @@ pub(crate) fn begin_pass( wallet_id: WalletId, height: u32, views: &[OutgoingView], - forced: &HashSet, - already_sent: impl Fn(&Txid, &Txid) -> bool, + forced: &HashMap, + sent_at: impl Fn(&Txid) -> Option<(u64, u32)>, ) -> PassStart { - let trigger = trigger_of(forced); + let trigger = trigger_of(!forced.is_empty()); let mut events = Vec::new(); // What the wallet itself still holds unsettled — what state is kept for. let present: HashSet = views.iter().map(|view| view.txid).collect(); @@ -556,15 +556,18 @@ pub(crate) fn begin_pass( let mined = state.mined(&wallet_id); let graph = ChainGraph::new(views, &mined); let roots = graph.roots(); - // `already_sent(txid, root)`: the root was sent to the network at this - // height after `txid` was reported — forcing it again would only repeat. + // `forced` maps each txid reported `Uncertain` to the command it was + // reported at; `sent_at(root)` is when the root was last sent to the + // network (command, height). A root sent at this height after its txid + // was reported is not forced again — that would only repeat the probe. + let sent_at = &sent_at; let forced_roots: BTreeSet = forced .iter() - .flat_map(|txid| { - graph - .roots_of(txid) - .into_iter() - .filter(|root| !already_sent(txid, root)) + .flat_map(|(txid, &reported)| { + graph.roots_of(txid).into_iter().filter(move |root| { + !sent_at(root) + .is_some_and(|(sent, sent_height)| sent >= reported && sent_height == height) + }) }) .collect(); @@ -587,7 +590,6 @@ pub(crate) fn begin_pass( .entry(key) .or_insert_with(|| ProbeSchedule::new(height)); if forced_roots.contains(&view.txid) || schedule.is_due(height) { - schedule.probed_at(height); due.push_back((view.txid, view.transaction.clone())); } } @@ -598,6 +600,15 @@ pub(crate) fn begin_pass( } } +/// A root is being sent to the network at `height`: its schedule counts from +/// here. Marked when the probe goes out, not when the pass is planned, so a +/// root the pass never reached (its run ended early) stays due. +pub(crate) fn mark_sent(state: &mut ResolverState, wallet_id: WalletId, root: Txid, height: u32) { + if let Some(schedule) = state.schedules.get_mut(&(wallet_id, root)) { + schedule.probed_at(height); + } +} + /// Record one root's probe result: a final verdict ends its probing, and the /// verdict goes to the wallet's own sends — the root when it is one, and, /// when it is dead, every own send built on it. @@ -606,7 +617,7 @@ pub(crate) fn record_probe( wallet_id: WalletId, height: u32, views: &[OutgoingView], - forced: &HashSet, + trigger: &'static str, root: Txid, verdict: &ProbeVerdict, ) -> Vec { @@ -634,14 +645,7 @@ pub(crate) fn record_probe( } else { BTreeSet::new() }; - state.publish_all( - wallet_id, - sends, - verdict, - height, - trigger_of(forced), - &mut events, - ); + state.publish_all(wallet_id, sends, verdict, height, trigger, &mut events); events } @@ -802,37 +806,19 @@ impl VerdictSink for EventSink { } } -/// How many times one event is handed to a host handler that panics on it. -const DELIVERY_ATTEMPTS: u8 = 3; - -/// Events a host handler panicked on, with the attempts made so far. -type Retries = VecDeque<(Outgoing, u8)>; - -/// Hand `events` to the host. A handler that panics does not take the -/// resolver down: the event is queued and handed over again before the -/// actor's next step, while it still says what the state says, up to -/// [`DELIVERY_ATTEMPTS`] times. (Where panics abort — the iOS profiles — the -/// app ends instead.) -fn deliver(sink: &Arc, retries: &mut Retries, events: Vec) { - for item in events { - attempt(sink, retries, item, 1); - } -} - -fn attempt(sink: &Arc, retries: &mut Retries, item: Outgoing, attempts: u8) { - if catch_unwind(AssertUnwindSafe(|| sink.deliver(&item))).is_ok() { - return; - } - let giving_up = attempts >= DELIVERY_ATTEMPTS; - tracing::error!( - wallet_id = %hex::encode(item.wallet_id), - event = ?item.event, - attempts, - giving_up, - "broadcast probe: a host handler panicked delivering an event" - ); - if !giving_up { - retries.push_back((item, attempts)); +/// Hand `events` to the host. The one production handler — the FFI bridge — +/// cannot panic, and the release profiles abort on a panic anyway; for a Rust +/// embedder whose handler panics under unwinding, the event it panicked on is +/// logged and dropped rather than taking the resolver task down. +fn deliver(sink: &Arc, events: Vec) { + for item in &events { + if catch_unwind(AssertUnwindSafe(|| sink.deliver(item))).is_err() { + tracing::error!( + wallet_id = %hex::encode(item.wallet_id), + event = ?item.event, + "broadcast probe: a host handler panicked; its event is dropped" + ); + } } } @@ -906,7 +892,7 @@ struct Run { struct Probing { views: Vec, height: u32, - forced: HashSet, + trigger: &'static str, due: VecDeque<(Txid, Transaction)>, } @@ -951,9 +937,6 @@ struct Actor { /// pass height. A forced pass skips a root already probed at its height /// since the `Uncertain` result arrived. probed: HashMap<(WalletId, Txid), (u64, u32)>, - /// Events a host handler panicked on, handed over again before the next - /// command or job result. - retries: Retries, } impl Actor { @@ -974,7 +957,6 @@ impl Actor { job_runs: HashMap::new(), tick: 0, probed: HashMap::new(), - retries: VecDeque::new(), } } @@ -1002,7 +984,6 @@ impl Actor { } fn joined(&mut self, joined: Result<(task::Id, JobDone), JoinError>) { - self.retry_deliveries(); match joined { Ok((id, done)) => { self.job_runs.remove(&id); @@ -1016,7 +997,7 @@ impl Actor { tracing::error!(?owner, %error, "broadcast probe: a job panicked"); // End the run it belonged to and let the next height probe the // wallet again — what the run carried (a forced txid) is lost, - // but its roots are due to the schedule. A panicked lookup + // but the roots it had not sent yet are still due. A panicked lookup // for an `Uncertain` result belongs to no run: wake every // wallet instead. match owner { @@ -1051,7 +1032,6 @@ impl Actor { } fn handle(&mut self, command: Command) { - self.retry_deliveries(); self.tick += 1; match command { Command::Height { wallet_id, height } => { @@ -1117,7 +1097,7 @@ impl Actor { if !enabled { self.probed.clear(); let events = self.state.forget_all(); - deliver(&self.sink, &mut self.retries, events); + deliver(&self.sink, events); } } Command::WalletRemoved(wallet_id) => { @@ -1126,7 +1106,7 @@ impl Actor { } self.probed.retain(|(wallet, _), _| *wallet != wallet_id); let events = self.state.forget_wallet(&wallet_id); - deliver(&self.sink, &mut self.retries, events); + deliver(&self.sink, events); } } } @@ -1222,8 +1202,12 @@ impl Actor { } self.probed.retain(|(wallet, _), _| *wallet != wallet_id); let events = self.state.forget_wallet(&wallet_id); - deliver(&self.sink, &mut self.retries, events); + deliver(&self.sink, events); if keep { + // The wait belonged to the state just forgotten. + if let Some(entry) = self.wallets.get_mut(&wallet_id) { + entry.wait_until = None; + } self.end_run(wallet_id); } } @@ -1232,22 +1216,17 @@ impl Actor { // A request queued before any height was reported // carries 0; the wallet's own synced height is better. let height = height.max(read.synced_height); + // The reports for txids the wallet still holds. + let mut reported = reported; + reported.retain(|txid, _| read.forced.contains(txid)); let probed = &self.probed; let start = begin_pass( &mut self.state, wallet_id, height, &read.views, - &read.forced, - |txid, root| { - reported.get(txid).is_some_and(|&since| { - probed.get(&(wallet_id, *root)).is_some_and( - |&(tick, probed_height)| { - tick >= since && probed_height == height - }, - ) - }) - }, + &reported, + |root| probed.get(&(wallet_id, *root)).copied(), ); // Forget send times of roots that settled or left. self.probed.retain(|(wallet, txid), _| { @@ -1257,13 +1236,13 @@ impl Actor { current.probing = Some(Probing { views: read.views, height, - forced: read.forced, + trigger: trigger_of(!reported.is_empty()), due: start.due, }); } // Clears and re-published dead verdicts reach the host // before the pass goes out to the network. - deliver(&self.sink, &mut self.retries, start.events); + deliver(&self.sink, start.events); self.probe_next(wallet_id); } } @@ -1288,11 +1267,16 @@ impl Actor { wallet_id, probing.height, &probing.views, - &probing.forced, + probing.trigger, root, &verdict, ); - deliver(&self.sink, &mut self.retries, events); + // Every node failed: the transaction may have reached none, so + // this probe must not stand in for a forced one. + if matches!(verdict, ProbeVerdict::Unresolved { .. }) { + self.probed.remove(&(wallet_id, root)); + } + deliver(&self.sink, events); self.probe_next(wallet_id); } } @@ -1325,6 +1309,7 @@ impl Actor { .insert(run.abort.id(), (wallet_id, id, Some(root))); self.probed .insert((wallet_id, root), (self.tick, probing.height)); + mark_sent(&mut self.state, wallet_id, root, probing.height); return; } entry.wait_until = @@ -1347,27 +1332,6 @@ impl Actor { } self.start(wallet_id); } - - /// Hand the host again what a panicking handler did not take, while it - /// still says what the state says: a verdict still published, a clear of - /// a send still unpublished. - fn retry_deliveries(&mut self) { - for (item, attempts) in std::mem::take(&mut self.retries) { - let current = match &item.event { - ResolverEvent::Verdict(txid, verdict) => self - .state - .published - .get(&(item.wallet_id, *txid)) - .is_some_and(|published| same_for_host(published, verdict)), - ResolverEvent::Cleared(txid) => { - !self.state.published.contains_key(&(item.wallet_id, *txid)) - } - }; - if current { - attempt(&self.sink, &mut self.retries, item, attempts + 1); - } - } - } } enum ActorSlot { @@ -1885,18 +1849,21 @@ mod tests { ) -> u32 { let due = { let mut guard = state.lock().expect("state"); - let start = begin_pass( - &mut guard.state, - wallet_id, - height, - views, - forced, - |_, _| false, - ); + let forced: HashMap = forced.iter().map(|txid| (*txid, 0)).collect(); + let start = begin_pass(&mut guard.state, wallet_id, height, views, &forced, |_| { + None + }); guard.push(start.events); start.due }; + let trigger = trigger_of(!forced.is_empty()); for (root, transaction) in due { + mark_sent( + &mut state.lock().expect("state").state, + wallet_id, + root, + height, + ); let verdict = probe.probe(&transaction).await; let mut guard = state.lock().expect("state"); let events = record_probe( @@ -1904,7 +1871,7 @@ mod tests { wallet_id, height, views, - forced, + trigger, root, &verdict, ); @@ -2613,7 +2580,7 @@ mod tests { /// root at that height does not send it to the network a second time. #[tokio::test] async fn should_not_probe_a_root_twice_at_one_height_for_an_uncertain_result() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); let mut rig = enabled(probe.clone()).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.height(wallet(), 100).await; @@ -2628,6 +2595,100 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1)]); } + /// A probe where every node failed may have reached none of them: it does + /// not stand in for the forced probe an `Uncertain` result asks for. + #[tokio::test] + async fn should_still_force_a_root_whose_probe_at_this_height_was_unresolved() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + rig.actor.handle(Command::Uncertain(txid(1))); + rig.settle().await; + + assert_eq!(probe.probed(), vec![txid(1), txid(1)]); + } + + /// The dedup is per reported txid: a root sent after one txid of its chain + /// was reported, but before another was, is forced again for the later + /// report. + #[test] + fn should_force_a_root_again_for_a_txid_reported_after_it_was_sent() { + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; + // Root 1 went out at command 7, height 100, and is not due again at + // 100 by its schedule — only a force can send it. + let sent_at = |root: &Txid| (*root == txid(1)).then_some((7, 100)); + let due_for = |forced: HashMap| { + let mut state = ResolverState::default(); + state + .schedules + .insert((wallet(), txid(1)), ProbeSchedule::new(100)); + mark_sent(&mut state, wallet(), txid(1), 100); + begin_pass(&mut state, wallet(), 100, &views, &forced, sent_at) + .due + .len() + }; + + assert_eq!( + due_for(HashMap::from([(txid(1), 5)])), + 0, + "sent after its report" + ); + assert_eq!( + due_for(HashMap::from([(txid(2), 9)])), + 1, + "sent before the child's report" + ); + } + + /// A run that ends early (a probe panicked) leaves the roots it never sent + /// due at once, so the next pass probes them. + #[test] + fn should_keep_due_the_roots_a_run_never_sent() { + let mut state = ResolverState::default(); + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])]; + let start = begin_pass(&mut state, wallet(), 100, &views, &HashMap::new(), |_| None); + let planned: Vec = start.due.iter().map(|(root, _)| *root).collect(); + assert_eq!(planned.len(), 2); + + // Only the first root goes out before the run ends. + mark_sent(&mut state, wallet(), planned[0], 100); + let again = begin_pass(&mut state, wallet(), 100, &views, &HashMap::new(), |_| None); + + assert_eq!( + again.due.iter().map(|(root, _)| *root).collect::>(), + vec![planned[1]] + ); + } + + /// A run whose read finds nothing to do still drops a height queued + /// during it that no root is due at. + #[tokio::test] + async fn should_drop_a_queued_height_when_a_forced_read_finds_nothing() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + let walks = rig.walks(); + + // An `Uncertain` for a txid the wallet no longer holds by read time. + rig.actor.handle(Command::Uncertain(txid(1))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.views(wallet(), vec![send(3, &[outpoint(93, 0)])]); + rig.actor.joined(listed); + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + rig.settle().await; + + assert_eq!(rig.walks(), walks, "idle: neither pass walked the records"); + } + /// A wallet waiting for nothing (idle) is forced by an `Uncertain` result; /// a height that arrives while that pass runs is kept, so the new root /// gets its next-block probe. @@ -2700,69 +2761,6 @@ mod tests { ); } - /// A host handler that panics on the first delivery only. - #[derive(Default)] - struct FlakySink { - calls: StdMutex, - taken: StdMutex>, - } - - impl VerdictSink for FlakySink { - fn deliver(&self, item: &Outgoing) { - let first = { - let mut calls = self.calls.lock().expect("calls"); - *calls += 1; - *calls == 1 - }; - if first { - panic!("host handler bug"); - } - self.taken.lock().expect("taken").push(item.event.clone()); - } - } - - /// An event a handler panicked on is handed over again before the next - /// step, while it still holds. - #[tokio::test] - async fn should_hand_over_again_an_event_a_handler_panicked_on() { - let sink = Arc::new(FlakySink::default()); - let source = Arc::new(FakeSource::default()); - let mut actor = Actor::new( - Arc::new(ScriptedProbe::new(&[(txid(1), dead())])), - Arc::clone(&source) as Arc, - Arc::clone(&sink) as Arc, - ); - source - .views - .lock() - .expect("views") - .insert(wallet(), vec![send(1, &[outpoint(90, 0)])]); - actor.handle(Command::SetEnabled(true)); - for height in [100, 101] { - actor.handle(Command::Height { - wallet_id: wallet(), - height, - }); - while let Some(joined) = actor.jobs.join_next_with_id().await { - actor.joined(joined); - } - } - assert!( - sink.taken.lock().expect("taken").is_empty(), - "the first try panicked" - ); - - actor.handle(Command::Seen { - wallet_id: wallet(), - touched: false, - }); - - assert_eq!( - *sink.taken.lock().expect("taken"), - vec![ResolverEvent::Verdict(txid(1), dead())] - ); - } - /// A probe that panics. struct PanickingProbe; From 4d607e6e5de9bb5ea58cf06e7c978ea088f196d1 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 29 Sep 2026 22:05:05 +0300 Subject: [PATCH 019/169] fix(platform-wallet): thirteenth review pass on the broadcast resolver - The send time of a root's last probe lives in its ProbeSchedule (sent_tick) instead of a second actor map: mark_sent sets it, an Unresolved result drops it, withdraw_send (a panicked probe job) resets the root to due-at-once. begin_pass reads it directly; three cleanup sites and the sent_at closure are gone. - The Unresolved rule is documented as what it is: every node failing and split answers both drop the send, at the cost of at most one repeat probe per later Uncertain report at the same height. - Verdicts are labelled per root: "uncertain" only for roots a report forced, "height" for roots due by schedule; begin_pass returns the forced roots instead of the caller re-deriving the trigger. - The test harness runs the same per-root steps as the actor (mark_sent, probe, record_probe); begin_pass docs describe the dedup. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 168 ++++++++++-------- 1 file changed, 98 insertions(+), 70 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 396f3515681..c16459710b5 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -303,6 +303,11 @@ fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { pub(crate) struct ProbeSchedule { first_seen: u32, last_probe: Option, + /// The resolver command count when the last probe went out, kept while + /// that probe may have reached a node (dropped when it came back + /// Unresolved): an `Uncertain` result reported before it is answered by + /// it, one reported after is not. + sent_tick: Option, } impl ProbeSchedule { @@ -310,9 +315,15 @@ impl ProbeSchedule { Self { first_seen, last_probe: None, + sent_tick: None, } } + /// Sent at `height` after a report made at command `reported`. + fn sent_since(&self, reported: u64, height: u32) -> bool { + self.last_probe == Some(height) && self.sent_tick.is_some_and(|sent| sent >= reported) + } + pub(crate) fn is_due(&self, height: u32) -> bool { match self.last_probe { None => true, @@ -512,26 +523,26 @@ pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { pub(crate) struct PassStart { pub events: Vec, pub due: VecDeque<(Txid, Transaction)>, - /// The txids the wallet still holds unsettled. - pub present: HashSet, + /// The due roots that are due because an `Uncertain` result forced them. + pub forced_roots: BTreeSet, } /// Start a pass over one wallet at `height`: forget what settled or left /// (clearing the sends the host had a verdict for), re-publish every dead /// root to every own send built on it — a send built on its change after it /// was found dead is dead too — and pick the roots due for a probe. The -/// roots of every `forced` transaction's chain are due regardless of their -/// schedule (dash-spv has just reported it `Uncertain`). A root found mined -/// counts as settled when picking roots, so its children are probed next. +/// roots of every `forced` transaction's chain (dash-spv reported it +/// `Uncertain` at the command it maps to) are due regardless of their +/// schedule, unless the root was already sent at this height after that +/// report. A root found mined counts as settled when picking roots, so its +/// children are probed next. pub(crate) fn begin_pass( state: &mut ResolverState, wallet_id: WalletId, height: u32, views: &[OutgoingView], forced: &HashMap, - sent_at: impl Fn(&Txid) -> Option<(u64, u32)>, ) -> PassStart { - let trigger = trigger_of(!forced.is_empty()); let mut events = Vec::new(); // What the wallet itself still holds unsettled — what state is kept for. let present: HashSet = views.iter().map(|view| view.txid).collect(); @@ -550,26 +561,26 @@ pub(crate) fn begin_pass( events.extend(state.clear_where( |wallet, txid| *wallet == wallet_id && !own_present.contains(txid), Some(height), - trigger, + trigger_of(!forced.is_empty()), )); let mined = state.mined(&wallet_id); let graph = ChainGraph::new(views, &mined); let roots = graph.roots(); - // `forced` maps each txid reported `Uncertain` to the command it was - // reported at; `sent_at(root)` is when the root was last sent to the - // network (command, height). A root sent at this height after its txid - // was reported is not forced again — that would only repeat the probe. - let sent_at = &sent_at; + // Forcing a root already sent at this height after its txid was + // reported would only repeat that probe. + let schedules = &state.schedules; let forced_roots: BTreeSet = forced .iter() .flat_map(|(txid, &reported)| { graph.roots_of(txid).into_iter().filter(move |root| { - !sent_at(root) - .is_some_and(|(sent, sent_height)| sent >= reported && sent_height == height) + !schedules + .get(&(wallet_id, *root)) + .is_some_and(|schedule| schedule.sent_since(reported, height)) }) }) .collect(); + let trigger = trigger_of(!forced_roots.is_empty()); for view in &roots { if let Some(Final::Dead { reason }) = state.finished.get(&(wallet_id, view.txid)).cloned() { @@ -596,19 +607,41 @@ pub(crate) fn begin_pass( PassStart { events, due, - present, + forced_roots, } } -/// A root is being sent to the network at `height`: its schedule counts from -/// here. Marked when the probe goes out, not when the pass is planned, so a -/// root the pass never reached (its run ended early) stays due. -pub(crate) fn mark_sent(state: &mut ResolverState, wallet_id: WalletId, root: Txid, height: u32) { +/// A root is being sent to the network at `height`, at resolver command +/// `tick`: its schedule counts from here. Marked when the probe goes out, not +/// when the pass is planned, so a root the pass never reached stays due. +pub(crate) fn mark_sent( + state: &mut ResolverState, + wallet_id: WalletId, + root: Txid, + height: u32, + tick: u64, +) { if let Some(schedule) = state.schedules.get_mut(&(wallet_id, root)) { schedule.probed_at(height); + schedule.sent_tick = Some(tick); } } +/// A root's probe never produced a verdict (its job panicked): nothing was +/// learned, so it is due again at once and counts as not sent. +pub(crate) fn withdraw_send(state: &mut ResolverState, wallet_id: WalletId, root: Txid) { + if let Some(schedule) = state.schedules.get_mut(&(wallet_id, root)) { + schedule.last_probe = None; + schedule.sent_tick = None; + } +} + +/// How a verdict for `root` is labelled in the log: forced by an `Uncertain` +/// result, or due by its schedule. +fn root_trigger(forced_roots: &BTreeSet, root: &Txid) -> &'static str { + trigger_of(forced_roots.contains(root)) +} + /// Record one root's probe result: a final verdict ends its probing, and the /// verdict goes to the wallet's own sends — the root when it is one, and, /// when it is dead, every own send built on it. @@ -635,7 +668,16 @@ pub(crate) fn record_probe( ProbeVerdict::Mined => { state.finished.insert(key, Final::Mined); } - _ => {} + // Unresolved when every node failed (it may have reached none) and + // when answers split; either way nothing settled, so this probe does + // not stand in for one a later `Uncertain` result forces — at worst a + // root is asked again at the same height, once per such report. + ProbeVerdict::Unresolved { .. } => { + if let Some(schedule) = state.schedules.get_mut(&key) { + schedule.sent_tick = None; + } + } + ProbeVerdict::Accepted => {} } let graph = ChainGraph::new(views, &HashSet::new()); let sends: BTreeSet = if matches!(verdict, ProbeVerdict::Dead { .. }) { @@ -892,7 +934,7 @@ struct Run { struct Probing { views: Vec, height: u32, - trigger: &'static str, + forced_roots: BTreeSet, due: VecDeque<(Txid, Transaction)>, } @@ -933,10 +975,6 @@ struct Actor { job_runs: HashMap)>, /// Commands handled so far: orders an `Uncertain` result against probes. tick: u64, - /// When each root was last sent to the network: the command count and the - /// pass height. A forced pass skips a root already probed at its height - /// since the `Uncertain` result arrived. - probed: HashMap<(WalletId, Txid), (u64, u32)>, } impl Actor { @@ -956,7 +994,6 @@ impl Actor { jobs: JoinSet::new(), job_runs: HashMap::new(), tick: 0, - probed: HashMap::new(), } } @@ -997,15 +1034,14 @@ impl Actor { tracing::error!(?owner, %error, "broadcast probe: a job panicked"); // End the run it belonged to and let the next height probe the // wallet again — what the run carried (a forced txid) is lost, - // but the roots it had not sent yet are still due. A panicked lookup + // but its roots are due: the panicked one is withdrawn, the + // ones it had not sent yet were never marked. A panicked lookup // for an `Uncertain` result belongs to no run: wake every // wallet instead. match owner { Some((wallet_id, run, root)) => { - // The probe never reached a verdict: it must not - // count as sent for the forced-probe dedup. if let Some(root) = root { - self.probed.remove(&(wallet_id, root)); + withdraw_send(&mut self.state, wallet_id, root); } if let Some(entry) = self.wallets.get_mut(&wallet_id) { if entry.run.as_ref().is_some_and(|current| current.id == run) { @@ -1095,7 +1131,6 @@ impl Actor { } } if !enabled { - self.probed.clear(); let events = self.state.forget_all(); deliver(&self.sink, events); } @@ -1104,7 +1139,6 @@ impl Actor { if let Some(run) = self.wallets.remove(&wallet_id).and_then(|entry| entry.run) { run.abort.abort(); } - self.probed.retain(|(wallet, _), _| *wallet != wallet_id); let events = self.state.forget_wallet(&wallet_id); deliver(&self.sink, events); } @@ -1200,7 +1234,6 @@ impl Actor { if !keep { self.wallets.remove(&wallet_id); } - self.probed.retain(|(wallet, _), _| *wallet != wallet_id); let events = self.state.forget_wallet(&wallet_id); deliver(&self.sink, events); if keep { @@ -1219,24 +1252,13 @@ impl Actor { // The reports for txids the wallet still holds. let mut reported = reported; reported.retain(|txid, _| read.forced.contains(txid)); - let probed = &self.probed; - let start = begin_pass( - &mut self.state, - wallet_id, - height, - &read.views, - &reported, - |root| probed.get(&(wallet_id, *root)).copied(), - ); - // Forget send times of roots that settled or left. - self.probed.retain(|(wallet, txid), _| { - *wallet != wallet_id || start.present.contains(txid) - }); + let start = + begin_pass(&mut self.state, wallet_id, height, &read.views, &reported); if let Some(current) = entry.run.as_mut() { current.probing = Some(Probing { views: read.views, height, - trigger: trigger_of(!reported.is_empty()), + forced_roots: start.forced_roots, due: start.due, }); } @@ -1267,15 +1289,10 @@ impl Actor { wallet_id, probing.height, &probing.views, - probing.trigger, + root_trigger(&probing.forced_roots, &root), root, &verdict, ); - // Every node failed: the transaction may have reached none, so - // this probe must not stand in for a forced one. - if matches!(verdict, ProbeVerdict::Unresolved { .. }) { - self.probed.remove(&(wallet_id, root)); - } deliver(&self.sink, events); self.probe_next(wallet_id); } @@ -1307,9 +1324,7 @@ impl Actor { }); self.job_runs .insert(run.abort.id(), (wallet_id, id, Some(root))); - self.probed - .insert((wallet_id, root), (self.tick, probing.height)); - mark_sent(&mut self.state, wallet_id, root, probing.height); + mark_sent(&mut self.state, wallet_id, root, probing.height, self.tick); return; } entry.wait_until = @@ -1847,22 +1862,21 @@ mod tests { views: &[OutgoingView], forced: &HashSet, ) -> u32 { - let due = { + let (due, forced_roots) = { let mut guard = state.lock().expect("state"); let forced: HashMap = forced.iter().map(|txid| (*txid, 0)).collect(); - let start = begin_pass(&mut guard.state, wallet_id, height, views, &forced, |_| { - None - }); + let start = begin_pass(&mut guard.state, wallet_id, height, views, &forced); guard.push(start.events); - start.due + (start.due, start.forced_roots) }; - let trigger = trigger_of(!forced.is_empty()); + // The same per-root steps as the actor's: mark sent, probe, record. for (root, transaction) in due { mark_sent( &mut state.lock().expect("state").state, wallet_id, root, height, + 0, ); let verdict = probe.probe(&transaction).await; let mut guard = state.lock().expect("state"); @@ -1871,7 +1885,7 @@ mod tests { wallet_id, height, views, - trigger, + root_trigger(&forced_roots, &root), root, &verdict, ); @@ -2622,14 +2636,13 @@ mod tests { let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; // Root 1 went out at command 7, height 100, and is not due again at // 100 by its schedule — only a force can send it. - let sent_at = |root: &Txid| (*root == txid(1)).then_some((7, 100)); let due_for = |forced: HashMap| { let mut state = ResolverState::default(); state .schedules .insert((wallet(), txid(1)), ProbeSchedule::new(100)); - mark_sent(&mut state, wallet(), txid(1), 100); - begin_pass(&mut state, wallet(), 100, &views, &forced, sent_at) + mark_sent(&mut state, wallet(), txid(1), 100, 7); + begin_pass(&mut state, wallet(), 100, &views, &forced) .due .len() }; @@ -2652,13 +2665,13 @@ mod tests { fn should_keep_due_the_roots_a_run_never_sent() { let mut state = ResolverState::default(); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])]; - let start = begin_pass(&mut state, wallet(), 100, &views, &HashMap::new(), |_| None); + let start = begin_pass(&mut state, wallet(), 100, &views, &HashMap::new()); let planned: Vec = start.due.iter().map(|(root, _)| *root).collect(); assert_eq!(planned.len(), 2); // Only the first root goes out before the run ends. - mark_sent(&mut state, wallet(), planned[0], 100); - let again = begin_pass(&mut state, wallet(), 100, &views, &HashMap::new(), |_| None); + mark_sent(&mut state, wallet(), planned[0], 100, 1); + let again = begin_pass(&mut state, wallet(), 100, &views, &HashMap::new()); assert_eq!( again.due.iter().map(|(root, _)| *root).collect::>(), @@ -2666,6 +2679,21 @@ mod tests { ); } + /// A probe that never produced a verdict (its job panicked) is withdrawn: + /// the root is due again at once, not after the slow interval. + #[test] + fn should_make_a_root_due_at_once_when_its_probe_is_withdrawn() { + let mut state = ResolverState::default(); + let views = [send(1, &[outpoint(90, 0)])]; + begin_pass(&mut state, wallet(), 100, &views, &HashMap::new()); + mark_sent(&mut state, wallet(), txid(1), 100, 1); + assert!(!state.schedules[&(wallet(), txid(1))].is_due(100)); + + withdraw_send(&mut state, wallet(), txid(1)); + + assert!(state.schedules[&(wallet(), txid(1))].is_due(100)); + } + /// A run whose read finds nothing to do still drops a height queued /// during it that no root is due at. #[tokio::test] From d1acc1b855bea766bbe1f319243ba843706827a4 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 13:56:20 +0300 Subject: [PATCH 020/169] refactor(platform-wallet): drop the forced-probe dedup from the broadcast resolver The dedup (a command tick carried through every Uncertain report, a send tick per root, and reset rules for Unresolved and panics) saved at most one repeated DAPI probe per report at the same height, and each review pass had to add another reset site. A reported root is now forced unconditionally; the occasional repeat probe is the documented price. Fourteenth review pass, also: - Each due root carries its own-send and chain sets, worked out once from the pass's graph; record_probe no longer rebuilds a ChainGraph per root. - Log labels are per root throughout: re-published dead verdicts use their root's label, clears are labelled "settled". - withdraw_send is a ProbeSchedule method and runs only for the wallet's current run, so a stale panic cannot undo a newer run's send. - An actor-level test covers a panicked probe in the slow interval being retried on the next block (fails without withdraw_send). Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 484 ++++++------------ 1 file changed, 151 insertions(+), 333 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index c16459710b5..4653d0ba04d 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -303,11 +303,6 @@ fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { pub(crate) struct ProbeSchedule { first_seen: u32, last_probe: Option, - /// The resolver command count when the last probe went out, kept while - /// that probe may have reached a node (dropped when it came back - /// Unresolved): an `Uncertain` result reported before it is answered by - /// it, one reported after is not. - sent_tick: Option, } impl ProbeSchedule { @@ -315,15 +310,9 @@ impl ProbeSchedule { Self { first_seen, last_probe: None, - sent_tick: None, } } - /// Sent at `height` after a report made at command `reported`. - fn sent_since(&self, reported: u64, height: u32) -> bool { - self.last_probe == Some(height) && self.sent_tick.is_some_and(|sent| sent >= reported) - } - pub(crate) fn is_due(&self, height: u32) -> bool { match self.last_probe { None => true, @@ -337,6 +326,11 @@ impl ProbeSchedule { self.last_probe = Some(height); } + /// The last probe produced nothing: due again at once. + fn withdraw(&mut self) { + self.last_probe = None; + } + /// The lowest height at which [`is_due`](Self::is_due) holds. pub(crate) fn next_due(&self) -> u32 { match self.last_probe { @@ -518,30 +512,44 @@ pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { previous.is_none_or(|previous| height.saturating_sub(previous) > CATCH_UP_STEP) } +/// A root chosen for a probe, with what its verdict will be published to — +/// worked out once, from the pass's graph. +#[derive(Debug, Clone)] +pub(crate) struct DueRoot { + pub txid: Txid, + pub transaction: Transaction, + /// The root is the wallet's own send. + pub own: bool, + /// The wallet's own sends in the root's chain, itself included if own — + /// what a Dead verdict is published to. + pub chain_own: BTreeSet, + /// An `Uncertain` result forced it (for the log; it may also be due by + /// its schedule). + pub forced: bool, +} + /// The start of a pass: what the host must hear now, and the roots to probe. #[derive(Debug)] pub(crate) struct PassStart { pub events: Vec, - pub due: VecDeque<(Txid, Transaction)>, - /// The due roots that are due because an `Uncertain` result forced them. - pub forced_roots: BTreeSet, + pub due: VecDeque, } /// Start a pass over one wallet at `height`: forget what settled or left /// (clearing the sends the host had a verdict for), re-publish every dead /// root to every own send built on it — a send built on its change after it /// was found dead is dead too — and pick the roots due for a probe. The -/// roots of every `forced` transaction's chain (dash-spv reported it -/// `Uncertain` at the command it maps to) are due regardless of their -/// schedule, unless the root was already sent at this height after that -/// report. A root found mined counts as settled when picking roots, so its -/// children are probed next. +/// roots of every `forced` transaction's chain (dash-spv has just reported +/// it `Uncertain`) are due regardless of their schedule — even if one went +/// out at this height already: a repeat probe is cheaper than bookkeeping +/// that tells the two apart. A root found mined counts as settled when +/// picking roots, so its children are probed next. pub(crate) fn begin_pass( state: &mut ResolverState, wallet_id: WalletId, height: u32, views: &[OutgoingView], - forced: &HashMap, + forced: &HashSet, ) -> PassStart { let mut events = Vec::new(); // What the wallet itself still holds unsettled — what state is kept for. @@ -558,34 +566,26 @@ pub(crate) fn begin_pass( state .finished .retain(|(wallet, txid), _| !is_gone(wallet, txid)); + // Cleared because the send settled or left the wallet. events.extend(state.clear_where( |wallet, txid| *wallet == wallet_id && !own_present.contains(txid), Some(height), - trigger_of(!forced.is_empty()), + "settled", )); let mined = state.mined(&wallet_id); let graph = ChainGraph::new(views, &mined); let roots = graph.roots(); - // Forcing a root already sent at this height after its txid was - // reported would only repeat that probe. - let schedules = &state.schedules; let forced_roots: BTreeSet = forced .iter() - .flat_map(|(txid, &reported)| { - graph.roots_of(txid).into_iter().filter(move |root| { - !schedules - .get(&(wallet_id, *root)) - .is_some_and(|schedule| schedule.sent_since(reported, height)) - }) - }) + .flat_map(|txid| graph.roots_of(txid)) .collect(); - let trigger = trigger_of(!forced_roots.is_empty()); for view in &roots { if let Some(Final::Dead { reason }) = state.finished.get(&(wallet_id, view.txid)).cloned() { let verdict = ProbeVerdict::Dead { reason }; let sends = graph.own_in_chain(&view.txid); + let trigger = trigger_of(forced_roots.contains(&view.txid)); state.publish_all(wallet_id, sends, &verdict, height, trigger, &mut events); } } @@ -600,48 +600,37 @@ pub(crate) fn begin_pass( .schedules .entry(key) .or_insert_with(|| ProbeSchedule::new(height)); - if forced_roots.contains(&view.txid) || schedule.is_due(height) { - due.push_back((view.txid, view.transaction.clone())); + let forced = forced_roots.contains(&view.txid); + if forced || schedule.is_due(height) { + due.push_back(DueRoot { + txid: view.txid, + transaction: view.transaction.clone(), + own: graph.is_own(&view.txid), + chain_own: graph.own_in_chain(&view.txid), + forced, + }); } } - PassStart { - events, - due, - forced_roots, - } + PassStart { events, due } } -/// A root is being sent to the network at `height`, at resolver command -/// `tick`: its schedule counts from here. Marked when the probe goes out, not -/// when the pass is planned, so a root the pass never reached stays due. -pub(crate) fn mark_sent( - state: &mut ResolverState, - wallet_id: WalletId, - root: Txid, - height: u32, - tick: u64, -) { +/// A root is being sent to the network at `height`: its schedule counts from +/// here. Marked when the probe goes out, not when the pass is planned, so a +/// root the pass never reached stays due. +pub(crate) fn mark_sent(state: &mut ResolverState, wallet_id: WalletId, root: Txid, height: u32) { if let Some(schedule) = state.schedules.get_mut(&(wallet_id, root)) { schedule.probed_at(height); - schedule.sent_tick = Some(tick); } } /// A root's probe never produced a verdict (its job panicked): nothing was -/// learned, so it is due again at once and counts as not sent. +/// learned, so it is due again at once. pub(crate) fn withdraw_send(state: &mut ResolverState, wallet_id: WalletId, root: Txid) { if let Some(schedule) = state.schedules.get_mut(&(wallet_id, root)) { - schedule.last_probe = None; - schedule.sent_tick = None; + schedule.withdraw(); } } -/// How a verdict for `root` is labelled in the log: forced by an `Uncertain` -/// result, or due by its schedule. -fn root_trigger(forced_roots: &BTreeSet, root: &Txid) -> &'static str { - trigger_of(forced_roots.contains(root)) -} - /// Record one root's probe result: a final verdict ends its probing, and the /// verdict goes to the wallet's own sends — the root when it is one, and, /// when it is dead, every own send built on it. @@ -649,13 +638,11 @@ pub(crate) fn record_probe( state: &mut ResolverState, wallet_id: WalletId, height: u32, - views: &[OutgoingView], - trigger: &'static str, - root: Txid, + root: &DueRoot, verdict: &ProbeVerdict, ) -> Vec { let mut events = Vec::new(); - let key = (wallet_id, root); + let key = (wallet_id, root.txid); match verdict { ProbeVerdict::Dead { reason } => { state.finished.insert( @@ -668,26 +655,23 @@ pub(crate) fn record_probe( ProbeVerdict::Mined => { state.finished.insert(key, Final::Mined); } - // Unresolved when every node failed (it may have reached none) and - // when answers split; either way nothing settled, so this probe does - // not stand in for one a later `Uncertain` result forces — at worst a - // root is asked again at the same height, once per such report. - ProbeVerdict::Unresolved { .. } => { - if let Some(schedule) = state.schedules.get_mut(&key) { - schedule.sent_tick = None; - } - } - ProbeVerdict::Accepted => {} + ProbeVerdict::Accepted | ProbeVerdict::Unresolved { .. } => {} } - let graph = ChainGraph::new(views, &HashSet::new()); let sends: BTreeSet = if matches!(verdict, ProbeVerdict::Dead { .. }) { - graph.own_in_chain(&root) - } else if graph.is_own(&root) { - BTreeSet::from([root]) + root.chain_own.clone() + } else if root.own { + BTreeSet::from([root.txid]) } else { BTreeSet::new() }; - state.publish_all(wallet_id, sends, verdict, height, trigger, &mut events); + state.publish_all( + wallet_id, + sends, + verdict, + height, + trigger_of(root.forced), + &mut events, + ); events } @@ -885,24 +869,17 @@ enum Command { /// What a finished job reports back to the actor. enum JobDone { - /// The wallets holding a transaction reported `Uncertain` at command - /// `since`. - Listed { - txid: Txid, - since: u64, - wallets: Vec, - }, + /// The wallets holding a transaction reported `Uncertain`. + Listed { txid: Txid, wallets: Vec }, Read { wallet_id: WalletId, run: u64, height: u32, - forced: HashMap, read: Option>, }, Probed { wallet_id: WalletId, run: u64, - root: Txid, verdict: ProbeVerdict, }, } @@ -913,8 +890,7 @@ struct PendingPass { /// A height advance asked for this pass; otherwise only `Uncertain` /// results did. by_height: bool, - /// Each txid reported `Uncertain`, with the command it was reported at. - forced: HashMap, + forced: HashSet, } /// A pass in flight: reading the wallet, then probing its due roots one at @@ -934,8 +910,9 @@ struct Run { struct Probing { views: Vec, height: u32, - forced_roots: BTreeSet, - due: VecDeque<(Txid, Transaction)>, + due: VecDeque, + /// The root whose probe is out now. + in_flight: Option, } #[derive(Default)] @@ -973,8 +950,6 @@ struct Actor { /// can end its run instead of leaving the wallet stuck behind it. /// A probe job also names its root, whose send time a panic withdraws. job_runs: HashMap)>, - /// Commands handled so far: orders an `Uncertain` result against probes. - tick: u64, } impl Actor { @@ -993,7 +968,6 @@ impl Actor { sink, jobs: JoinSet::new(), job_runs: HashMap::new(), - tick: 0, } } @@ -1040,14 +1014,19 @@ impl Actor { // wallet instead. match owner { Some((wallet_id, run, root)) => { - if let Some(root) = root { - withdraw_send(&mut self.state, wallet_id, root); - } - if let Some(entry) = self.wallets.get_mut(&wallet_id) { - if entry.run.as_ref().is_some_and(|current| current.id == run) { + // Only the wallet's current run: a stale panic (its run + // was dropped) must not undo a newer run's send. + let current = self.wallets.get(&wallet_id).is_some_and(|entry| { + entry.run.as_ref().is_some_and(|current| current.id == run) + }); + if current { + if let Some(root) = root { + withdraw_send(&mut self.state, wallet_id, root); + } + if let Some(entry) = self.wallets.get_mut(&wallet_id) { entry.wait_until = None; - self.end_run(wallet_id); } + self.end_run(wallet_id); } } None => { @@ -1068,7 +1047,6 @@ impl Actor { } fn handle(&mut self, command: Command) { - self.tick += 1; match command { Command::Height { wallet_id, height } => { let enabled = self.enabled; @@ -1105,11 +1083,9 @@ impl Actor { // Routed to the wallets holding it, including ones no event // has introduced yet. let source = Arc::clone(&self.source); - let since = self.tick; self.jobs.spawn(async move { JobDone::Listed { txid, - since, wallets: source.holders(txid).await, } }); @@ -1161,17 +1137,12 @@ impl Actor { let source = Arc::clone(&self.source); let abort = self.jobs.spawn(async move { let read = source - .read( - wallet_id, - pending.by_height, - pending.forced.keys().copied().collect(), - ) + .read(wallet_id, pending.by_height, pending.forced) .await; JobDone::Read { wallet_id, run, height: pending.height, - forced: pending.forced, read, } }); @@ -1187,11 +1158,7 @@ impl Actor { fn job_done(&mut self, done: JobDone) { match done { - JobDone::Listed { - txid, - since, - wallets, - } => { + JobDone::Listed { txid, wallets } => { if !self.enabled { return; } @@ -1201,10 +1168,7 @@ impl Actor { let entry = self.entry(wallet_id); let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(entry.height.unwrap_or(0)); - // The latest report of the txid counts: a probe sent - // before it must not stand in for the one it asks for. - let at = pending.forced.entry(txid).or_insert(since); - *at = (*at).max(since); + pending.forced.insert(txid); self.start(wallet_id); } } @@ -1212,7 +1176,6 @@ impl Actor { wallet_id, run, height, - forced: reported, read, } => { let Some(entry) = self.wallets.get_mut(&wallet_id) else { @@ -1249,17 +1212,19 @@ impl Actor { // A request queued before any height was reported // carries 0; the wallet's own synced height is better. let height = height.max(read.synced_height); - // The reports for txids the wallet still holds. - let mut reported = reported; - reported.retain(|txid, _| read.forced.contains(txid)); - let start = - begin_pass(&mut self.state, wallet_id, height, &read.views, &reported); + let start = begin_pass( + &mut self.state, + wallet_id, + height, + &read.views, + &read.forced, + ); if let Some(current) = entry.run.as_mut() { current.probing = Some(Probing { views: read.views, height, - forced_roots: start.forced_roots, due: start.due, + in_flight: None, }); } // Clears and re-published dead verdicts reach the host @@ -1272,7 +1237,6 @@ impl Actor { JobDone::Probed { wallet_id, run, - root, verdict, } => { let Some(probing) = self @@ -1280,19 +1244,15 @@ impl Actor { .get_mut(&wallet_id) .and_then(|entry| entry.run.as_mut()) .filter(|current| current.id == run) - .and_then(|current| current.probing.as_ref()) + .and_then(|current| current.probing.as_mut()) else { return; }; - let events = record_probe( - &mut self.state, - wallet_id, - probing.height, - &probing.views, - root_trigger(&probing.forced_roots, &root), - root, - &verdict, - ); + let Some(root) = probing.in_flight.take() else { + return; + }; + let events = + record_probe(&mut self.state, wallet_id, probing.height, &root, &verdict); deliver(&self.sink, events); self.probe_next(wallet_id); } @@ -1310,21 +1270,22 @@ impl Actor { let Some(probing) = run.probing.as_mut() else { return; }; - if let Some((root, transaction)) = probing.due.pop_front() { + if let Some(root) = probing.due.pop_front() { let probe = Arc::clone(&self.probe); let id = run.id; + let transaction = root.transaction.clone(); run.abort = self.jobs.spawn(async move { let verdict = probe.probe(&transaction).await; JobDone::Probed { wallet_id, run: id, - root, verdict, } }); self.job_runs - .insert(run.abort.id(), (wallet_id, id, Some(root))); - mark_sent(&mut self.state, wallet_id, root, probing.height, self.tick); + .insert(run.abort.id(), (wallet_id, id, Some(root.txid))); + mark_sent(&mut self.state, wallet_id, root.txid, probing.height); + probing.in_flight = Some(root); return; } entry.wait_until = @@ -1862,33 +1823,23 @@ mod tests { views: &[OutgoingView], forced: &HashSet, ) -> u32 { - let (due, forced_roots) = { + let due = { let mut guard = state.lock().expect("state"); - let forced: HashMap = forced.iter().map(|txid| (*txid, 0)).collect(); - let start = begin_pass(&mut guard.state, wallet_id, height, views, &forced); + let start = begin_pass(&mut guard.state, wallet_id, height, views, forced); guard.push(start.events); - (start.due, start.forced_roots) + start.due }; // The same per-root steps as the actor's: mark sent, probe, record. - for (root, transaction) in due { + for root in due { mark_sent( &mut state.lock().expect("state").state, wallet_id, - root, + root.txid, height, - 0, ); - let verdict = probe.probe(&transaction).await; + let verdict = probe.probe(&root.transaction).await; let mut guard = state.lock().expect("state"); - let events = record_probe( - &mut guard.state, - wallet_id, - height, - views, - root_trigger(&forced_roots, &root), - root, - &verdict, - ); + let events = record_probe(&mut guard.state, wallet_id, height, &root, &verdict); guard.push(events); } let guard = state.lock().expect("state"); @@ -2590,202 +2541,69 @@ mod tests { assert!(rig.actor.state.schedules.is_empty()); } - /// An `Uncertain` result that lands while a height pass already probes the - /// root at that height does not send it to the network a second time. + /// An `Uncertain` result forces its root even if a height pass sent it at + /// this height already: one repeat probe is the accepted price of not + /// tracking which report came first. #[tokio::test] - async fn should_not_probe_a_root_twice_at_one_height_for_an_uncertain_result() { + async fn should_force_a_reported_root_even_if_probed_at_this_height() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); let mut rig = enabled(probe.clone()).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.height(wallet(), 100).await; - - rig.actor.handle(Command::Height { - wallet_id: wallet(), - height: 101, - }); - rig.actor.handle(Command::Uncertain(txid(1))); - rig.settle().await; - - assert_eq!(probe.probed(), vec![txid(1)]); - } - - /// A probe where every node failed may have reached none of them: it does - /// not stand in for the forced probe an `Uncertain` result asks for. - #[tokio::test] - async fn should_still_force_a_root_whose_probe_at_this_height_was_unresolved() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.height(wallet(), 100).await; + rig.follow(wallet(), 100).await; - rig.actor.handle(Command::Height { - wallet_id: wallet(), - height: 101, - }); - rig.actor.handle(Command::Uncertain(txid(1))); - rig.settle().await; + rig.send(Command::Uncertain(txid(1))).await; assert_eq!(probe.probed(), vec![txid(1), txid(1)]); } - /// The dedup is per reported txid: a root sent after one txid of its chain - /// was reported, but before another was, is forced again for the later - /// report. - #[test] - fn should_force_a_root_again_for_a_txid_reported_after_it_was_sent() { - let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])]; - // Root 1 went out at command 7, height 100, and is not due again at - // 100 by its schedule — only a force can send it. - let due_for = |forced: HashMap| { - let mut state = ResolverState::default(); - state - .schedules - .insert((wallet(), txid(1)), ProbeSchedule::new(100)); - mark_sent(&mut state, wallet(), txid(1), 100, 7); - begin_pass(&mut state, wallet(), 100, &views, &forced) - .due - .len() - }; - - assert_eq!( - due_for(HashMap::from([(txid(1), 5)])), - 0, - "sent after its report" - ); - assert_eq!( - due_for(HashMap::from([(txid(2), 9)])), - 1, - "sent before the child's report" - ); - } - - /// A run that ends early (a probe panicked) leaves the roots it never sent - /// due at once, so the next pass probes them. - #[test] - fn should_keep_due_the_roots_a_run_never_sent() { - let mut state = ResolverState::default(); - let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])]; - let start = begin_pass(&mut state, wallet(), 100, &views, &HashMap::new()); - let planned: Vec = start.due.iter().map(|(root, _)| *root).collect(); - assert_eq!(planned.len(), 2); - - // Only the first root goes out before the run ends. - mark_sent(&mut state, wallet(), planned[0], 100, 1); - let again = begin_pass(&mut state, wallet(), 100, &views, &HashMap::new()); - - assert_eq!( - again.due.iter().map(|(root, _)| *root).collect::>(), - vec![planned[1]] - ); + /// A probe that panics on its `nth` call and accepts otherwise. + struct PanicOnCall { + nth: usize, + calls: StdMutex, } - /// A probe that never produced a verdict (its job panicked) is withdrawn: - /// the root is due again at once, not after the slow interval. - #[test] - fn should_make_a_root_due_at_once_when_its_probe_is_withdrawn() { - let mut state = ResolverState::default(); - let views = [send(1, &[outpoint(90, 0)])]; - begin_pass(&mut state, wallet(), 100, &views, &HashMap::new()); - mark_sent(&mut state, wallet(), txid(1), 100, 1); - assert!(!state.schedules[&(wallet(), txid(1))].is_due(100)); - - withdraw_send(&mut state, wallet(), txid(1)); - - assert!(state.schedules[&(wallet(), txid(1))].is_due(100)); + #[async_trait] + impl AcceptanceProbe for PanicOnCall { + async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { + let call = { + let mut calls = self.calls.lock().expect("calls"); + *calls += 1; + *calls + }; + if call == self.nth { + panic!("probe bug"); + } + ProbeVerdict::Accepted + } } - /// A run whose read finds nothing to do still drops a height queued - /// during it that no root is due at. + /// A root in its slow interval whose probe panicked is probed again on + /// the next block, not SLOW_INTERVAL blocks later. #[tokio::test] - async fn should_drop_a_queued_height_when_a_forced_read_finds_nothing() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.follow(wallet(), 100).await; - let walks = rig.walks(); - - // An `Uncertain` for a txid the wallet no longer holds by read time. - rig.actor.handle(Command::Uncertain(txid(1))); - let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); - rig.views(wallet(), vec![send(3, &[outpoint(93, 0)])]); - rig.actor.joined(listed); - rig.actor.handle(Command::Height { - wallet_id: wallet(), - height: 102, + async fn should_probe_again_on_the_next_block_after_a_panicked_slow_probe() { + let first = 101; + let slow = first + EVERY_BLOCK_WINDOW - 1 + SLOW_INTERVAL; + let probe = Arc::new(PanicOnCall { + nth: EVERY_BLOCK_WINDOW as usize + 1, + calls: StdMutex::new(0), }); - rig.settle().await; - - assert_eq!(rig.walks(), walks, "idle: neither pass walked the records"); - } - - /// A wallet waiting for nothing (idle) is forced by an `Uncertain` result; - /// a height that arrives while that pass runs is kept, so the new root - /// gets its next-block probe. - #[tokio::test] - async fn should_keep_a_height_that_arrives_during_a_forced_pass() { - let probe = Arc::new(ScriptedProbe::new(&[ - (txid(1), dead()), - (txid(2), unresolved()), - ])); let mut rig = enabled(probe.clone()).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.follow(wallet(), 100).await; - rig.height(wallet(), 102).await; - assert_eq!(probe.probed(), vec![txid(1)], "idle after the dead root"); - rig.views( - wallet(), - vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + rig.follow(wallet(), first - 1).await; + for height in first + 1..=slow { + rig.height(wallet(), height).await; + } + assert_eq!( + *probe.calls.lock().expect("calls"), + EVERY_BLOCK_WINDOW as usize + 1, + "the slow probe went out and panicked" ); - rig.actor.handle(Command::Uncertain(txid(2))); - let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); - rig.actor.joined(listed); - rig.actor.handle(Command::Height { - wallet_id: wallet(), - height: 103, - }); - rig.settle().await; - - assert_eq!(probe.probed(), vec![txid(1), txid(2), txid(2)]); - } - - /// A stop that runs out of budget keeps the task without aborting it: a - /// retried stop waits for it to end and reports it clean. - #[tokio::test] - async fn should_report_a_task_that_outlived_a_stop_clean_on_retry() { - let (stop, _stopped) = oneshot::channel(); - let handle = tokio::spawn(tokio::time::sleep(Duration::from_millis(300))); - let slot = Mutex::new(ActorSlot::Running(handle, stop)); - let stopping = AsyncMutex::new(()); - - let first = stop_slot(&slot, &stopping, Duration::from_millis(20)).await; - let second = stop_slot(&slot, &stopping, Duration::from_secs(5)).await; - let third = stop_slot(&slot, &stopping, Duration::from_secs(5)).await; + rig.height(wallet(), slow + 1).await; - assert_eq!(first, WorkerStatus::Timeout); - assert_eq!(second, WorkerStatus::Ok); - assert_eq!(third, WorkerStatus::NotRunning); - } - - /// Two stops at once: the second waits for the first and never reports - /// the task gone while it still runs. - #[tokio::test] - async fn should_not_report_a_running_task_as_not_running_to_a_concurrent_stop() { - let (stop, _stopped) = oneshot::channel(); - let handle = tokio::spawn(tokio::time::sleep(Duration::from_millis(200))); - let slot = Mutex::new(ActorSlot::Running(handle, stop)); - let stopping = AsyncMutex::new(()); - let started = Instant::now(); - - let (first, second) = tokio::join!( - stop_slot(&slot, &stopping, Duration::from_secs(5)), - stop_slot(&slot, &stopping, Duration::from_secs(5)), - ); - - assert_eq!(first, WorkerStatus::Ok); - assert_eq!(second, WorkerStatus::NotRunning); - assert!( - started.elapsed() >= Duration::from_millis(150), - "waited for the task" + assert_eq!( + *probe.calls.lock().expect("calls"), + EVERY_BLOCK_WINDOW as usize + 2 ); } From dfb881100dfae412d8f3ad604eb9cc56d4e4d2c1 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 14:04:45 +0300 Subject: [PATCH 021/169] fix(platform-wallet): fifteenth review pass on the broadcast resolver - Restore six tests the dedup removal deleted by mistake: both stop_slot tests (retry after a timeout, concurrent stops), the queued-height rules of end_run, mark-on-send, and withdraw. - A root forced before the wallet follows the tip (an Uncertain right after launch, counted from the stored tip) gets its every-block window once the wallet follows again: the first non-catch-up height restarts the windows. - A probe result with no root in flight ends the run with an error instead of leaving it to block the wallet. - The panicked root is taken from the run's in-flight slot; job_runs keeps only (wallet, run). - A due root carries an Arc of its transaction and no precomputed chain: the own sends of a dead root's chain are walked only on a Dead verdict. - Clears are labelled "settled-or-left", not "settled". Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 260 +++++++++++++++--- 1 file changed, 225 insertions(+), 35 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 4653d0ba04d..8e4334f5ef2 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -495,6 +495,16 @@ impl ResolverState { } } + /// Start the every-block window of every root of `wallet_id` at `height` + /// at the latest. + fn restart_windows(&mut self, wallet_id: &WalletId, height: u32) { + for ((wallet, _), schedule) in self.schedules.iter_mut() { + if wallet == wallet_id { + schedule.first_seen = schedule.first_seen.max(height); + } + } + } + /// Roots of `wallet_id` a probe found in a block the wallet has not seen. fn mined(&self, wallet_id: &WalletId) -> HashSet { self.finished @@ -512,17 +522,14 @@ pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { previous.is_none_or(|previous| height.saturating_sub(previous) > CATCH_UP_STEP) } -/// A root chosen for a probe, with what its verdict will be published to — -/// worked out once, from the pass's graph. +/// A root chosen for a probe. #[derive(Debug, Clone)] pub(crate) struct DueRoot { pub txid: Txid, - pub transaction: Transaction, + /// Shared with the probe job: sent as is, never changed. + pub transaction: Arc, /// The root is the wallet's own send. pub own: bool, - /// The wallet's own sends in the root's chain, itself included if own — - /// what a Dead verdict is published to. - pub chain_own: BTreeSet, /// An `Uncertain` result forced it (for the log; it may also be due by /// its schedule). pub forced: bool, @@ -566,11 +573,12 @@ pub(crate) fn begin_pass( state .finished .retain(|(wallet, txid), _| !is_gone(wallet, txid)); - // Cleared because the send settled or left the wallet. + // Cleared because the send is no longer an unsettled own send: it + // settled, or left the wallet — not a statement that it settled. events.extend(state.clear_where( |wallet, txid| *wallet == wallet_id && !own_present.contains(txid), Some(height), - "settled", + "settled-or-left", )); let mined = state.mined(&wallet_id); @@ -604,9 +612,8 @@ pub(crate) fn begin_pass( if forced || schedule.is_due(height) { due.push_back(DueRoot { txid: view.txid, - transaction: view.transaction.clone(), + transaction: Arc::new(view.transaction.clone()), own: graph.is_own(&view.txid), - chain_own: graph.own_in_chain(&view.txid), forced, }); } @@ -633,11 +640,13 @@ pub(crate) fn withdraw_send(state: &mut ResolverState, wallet_id: WalletId, root /// Record one root's probe result: a final verdict ends its probing, and the /// verdict goes to the wallet's own sends — the root when it is one, and, -/// when it is dead, every own send built on it. +/// when it is dead, every own send built on it (the pass's `views`, walked +/// only then: Dead is terminal and rare). pub(crate) fn record_probe( state: &mut ResolverState, wallet_id: WalletId, height: u32, + views: &[OutgoingView], root: &DueRoot, verdict: &ProbeVerdict, ) -> Vec { @@ -658,7 +667,7 @@ pub(crate) fn record_probe( ProbeVerdict::Accepted | ProbeVerdict::Unresolved { .. } => {} } let sends: BTreeSet = if matches!(verdict, ProbeVerdict::Dead { .. }) { - root.chain_own.clone() + ChainGraph::new(views, &state.mined(&wallet_id)).own_in_chain(&root.txid) } else if root.own { BTreeSet::from([root.txid]) } else { @@ -919,6 +928,8 @@ struct Probing { struct WalletEntry { /// Last synced height reported, for telling catch-up from following. height: Option, + /// The last height step followed the tip (was not catch-up). + following: bool, /// Height advances below this are skipped — no walk over the wallet's /// records while no root can be due. `u32::MAX` when nothing is left to /// probe (idle). Reset by anything that may have left new work — which @@ -948,8 +959,7 @@ struct Actor { jobs: JoinSet, /// Which run each read or probe job belongs to, so a job that panicked /// can end its run instead of leaving the wallet stuck behind it. - /// A probe job also names its root, whose send time a panic withdraws. - job_runs: HashMap)>, + job_runs: HashMap, } impl Actor { @@ -1013,21 +1023,26 @@ impl Actor { // for an `Uncertain` result belongs to no run: wake every // wallet instead. match owner { - Some((wallet_id, run, root)) => { + Some((wallet_id, run)) => { // Only the wallet's current run: a stale panic (its run - // was dropped) must not undo a newer run's send. - let current = self.wallets.get(&wallet_id).is_some_and(|entry| { - entry.run.as_ref().is_some_and(|current| current.id == run) - }); - if current { - if let Some(root) = root { - withdraw_send(&mut self.state, wallet_id, root); - } - if let Some(entry) = self.wallets.get_mut(&wallet_id) { - entry.wait_until = None; - } - self.end_run(wallet_id); + // was dropped) must not undo a newer run's send. The + // root whose probe panicked is the one in flight. + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; + let Some(current) = entry.run.as_mut().filter(|current| current.id == run) + else { + return; + }; + let in_flight = current + .probing + .as_mut() + .and_then(|probing| probing.in_flight.take()); + entry.wait_until = None; + if let Some(root) = in_flight { + withdraw_send(&mut self.state, wallet_id, root.txid); } + self.end_run(wallet_id); } None => { for entry in self.wallets.values_mut() { @@ -1052,11 +1067,24 @@ impl Actor { let enabled = self.enabled; let entry = self.entry(wallet_id); let previous = entry.height.replace(height); + let catch_up = is_catch_up_step(previous, height); + // Back to following the tip: roots first seen while the wallet + // was behind (a forced pass right after launch counts heights + // from the stored tip) get their every-block window from here. + let caught_up = !catch_up && !entry.following; + entry.following = !catch_up; + if caught_up { + entry.wait_until = None; + self.state.restart_windows(&wallet_id, height); + } + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; // While a pass runs, its end decides the wait: queue the height // and let `probe_next` drop it if no root turns out due. if !enabled + || catch_up || (entry.run.is_none() && entry.wait_until.is_some_and(|until| height < until)) - || is_catch_up_step(previous, height) { return; } @@ -1146,7 +1174,7 @@ impl Actor { read, } }); - self.job_runs.insert(abort.id(), (wallet_id, run, None)); + self.job_runs.insert(abort.id(), (wallet_id, run)); entry.run = Some(Run { id: run, seq: entry.seq, @@ -1249,10 +1277,24 @@ impl Actor { return; }; let Some(root) = probing.in_flight.take() else { + // Cannot happen: a probe job is spawned only with its root + // in flight. Fail safe — end the run rather than leave it + // blocking every later pass for the wallet. + tracing::error!( + wallet_id = %hex::encode(wallet_id), + "broadcast probe: a probe result with no root in flight" + ); + self.end_run(wallet_id); return; }; - let events = - record_probe(&mut self.state, wallet_id, probing.height, &root, &verdict); + let events = record_probe( + &mut self.state, + wallet_id, + probing.height, + &probing.views, + &root, + &verdict, + ); deliver(&self.sink, events); self.probe_next(wallet_id); } @@ -1273,7 +1315,7 @@ impl Actor { if let Some(root) = probing.due.pop_front() { let probe = Arc::clone(&self.probe); let id = run.id; - let transaction = root.transaction.clone(); + let transaction = Arc::clone(&root.transaction); run.abort = self.jobs.spawn(async move { let verdict = probe.probe(&transaction).await; JobDone::Probed { @@ -1282,8 +1324,7 @@ impl Actor { verdict, } }); - self.job_runs - .insert(run.abort.id(), (wallet_id, id, Some(root.txid))); + self.job_runs.insert(run.abort.id(), (wallet_id, id)); mark_sent(&mut self.state, wallet_id, root.txid, probing.height); probing.in_flight = Some(root); return; @@ -1839,7 +1880,7 @@ mod tests { ); let verdict = probe.probe(&root.transaction).await; let mut guard = state.lock().expect("state"); - let events = record_probe(&mut guard.state, wallet_id, height, &root, &verdict); + let events = record_probe(&mut guard.state, wallet_id, height, views, &root, &verdict); guard.push(events); } let guard = state.lock().expect("state"); @@ -2556,6 +2597,155 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1), txid(1)]); } + /// A run whose read finds nothing to do still drops a height queued + /// during it that no root is due at. + #[tokio::test] + async fn should_drop_a_queued_height_when_a_forced_read_finds_nothing() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + let walks = rig.walks(); + + // An `Uncertain` for a txid the wallet no longer holds by read time. + rig.actor.handle(Command::Uncertain(txid(1))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.views(wallet(), vec![send(3, &[outpoint(93, 0)])]); + rig.actor.joined(listed); + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + rig.settle().await; + + assert_eq!(rig.walks(), walks, "idle: neither pass walked the records"); + } + + /// A wallet waiting for nothing (idle) is forced by an `Uncertain` result; + /// a height that arrives while that pass runs is kept, so the new root + /// gets its next-block probe. + #[tokio::test] + async fn should_keep_a_height_that_arrives_during_a_forced_pass() { + let probe = Arc::new(ScriptedProbe::new(&[ + (txid(1), dead()), + (txid(2), unresolved()), + ])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.height(wallet(), 102).await; + assert_eq!(probe.probed(), vec![txid(1)], "idle after the dead root"); + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); + + rig.actor.handle(Command::Uncertain(txid(2))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.actor.joined(listed); + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 103, + }); + rig.settle().await; + + assert_eq!(probe.probed(), vec![txid(1), txid(2), txid(2)]); + } + + /// A run that ends early (a probe panicked) leaves the roots it never sent + /// due at once, so the next pass probes them. + #[test] + fn should_keep_due_the_roots_a_run_never_sent() { + let mut state = ResolverState::default(); + let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])]; + let start = begin_pass(&mut state, wallet(), 100, &views, &HashSet::new()); + let planned: Vec = start.due.iter().map(|root| root.txid).collect(); + assert_eq!(planned.len(), 2); + + // Only the first root goes out before the run ends. + mark_sent(&mut state, wallet(), planned[0], 100); + let again = begin_pass(&mut state, wallet(), 100, &views, &HashSet::new()); + + assert_eq!( + again.due.iter().map(|root| root.txid).collect::>(), + vec![planned[1]] + ); + } + + /// A probe that never produced a verdict (its job panicked) is withdrawn: + /// the root is due again at once, not after the slow interval. + #[test] + fn should_make_a_root_due_at_once_when_its_probe_is_withdrawn() { + let mut state = ResolverState::default(); + let views = [send(1, &[outpoint(90, 0)])]; + begin_pass(&mut state, wallet(), 100, &views, &HashSet::new()); + mark_sent(&mut state, wallet(), txid(1), 100); + assert!(!state.schedules[&(wallet(), txid(1))].is_due(100)); + + withdraw_send(&mut state, wallet(), txid(1)); + + assert!(state.schedules[&(wallet(), txid(1))].is_due(100)); + } + + /// Two stops at once: the second waits for the first and never reports + /// the task gone while it still runs. + #[tokio::test] + async fn should_not_report_a_running_task_as_not_running_to_a_concurrent_stop() { + let (stop, _stopped) = oneshot::channel(); + let handle = tokio::spawn(tokio::time::sleep(Duration::from_millis(200))); + let slot = Mutex::new(ActorSlot::Running(handle, stop)); + let stopping = AsyncMutex::new(()); + let started = Instant::now(); + + let (first, second) = tokio::join!( + stop_slot(&slot, &stopping, Duration::from_secs(5)), + stop_slot(&slot, &stopping, Duration::from_secs(5)), + ); + + assert_eq!(first, WorkerStatus::Ok); + assert_eq!(second, WorkerStatus::NotRunning); + assert!( + started.elapsed() >= Duration::from_millis(150), + "waited for the task" + ); + } + + /// A stop that runs out of budget keeps the task without aborting it: a + /// retried stop waits for it to end and reports it clean. + #[tokio::test] + async fn should_report_a_task_that_outlived_a_stop_clean_on_retry() { + let (stop, _stopped) = oneshot::channel(); + let handle = tokio::spawn(tokio::time::sleep(Duration::from_millis(300))); + let slot = Mutex::new(ActorSlot::Running(handle, stop)); + let stopping = AsyncMutex::new(()); + + let first = stop_slot(&slot, &stopping, Duration::from_millis(20)).await; + let second = stop_slot(&slot, &stopping, Duration::from_secs(5)).await; + let third = stop_slot(&slot, &stopping, Duration::from_secs(5)).await; + + assert_eq!(first, WorkerStatus::Timeout); + assert_eq!(second, WorkerStatus::Ok); + assert_eq!(third, WorkerStatus::NotRunning); + } + + /// An `Uncertain` result right after launch runs a pass at the stale stored + /// tip; once the wallet follows the tip again, the root still gets its + /// every-block window. + #[tokio::test] + async fn should_give_a_root_forced_before_the_first_height_its_every_block_window() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.send(Command::Uncertain(txid(1))).await; + assert_eq!(probe.probed().len(), 1, "forced at the stored tip (0 here)"); + + rig.follow(wallet(), 500).await; + for height in 502..=505 { + rig.height(wallet(), height).await; + } + + assert_eq!(probe.probed().len(), 6, "every block from 501 to 505"); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From e5544186ebdcdb58d3545ccd9a09d1fda6cb9cb6 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 14:14:05 +0300 Subject: [PATCH 022/169] fix(platform-wallet): sixteenth review pass on the broadcast resolver - Replace the window restart with lazy anchoring: a root's every-block window starts at the first pass that sees it while the wallet follows the tip, once. A root seen only behind the tip (forced right after launch) is probed every block until anchored; roots already past their window keep the slow cadence across foreground returns, and an idle wallet is no longer woken by them. - A forced pass runs at the latest height known when its read lands, so a read that races the return to the tip anchors at the tip. - A probe result for a current run that is not probing ends the run with an error, like the no-root-in-flight case. - OutgoingView holds its transaction in an Arc built once per read; passes and probe jobs share it instead of cloning. - Module docs say where the window counts from and that each Uncertain report adds one probe. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 171 ++++++++++++------ 1 file changed, 119 insertions(+), 52 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 8e4334f5ef2..4bcc27f45c1 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -34,7 +34,9 @@ //! of that transaction's chain), then on each advance of the wallet's synced //! height that follows the tip — steps of more than [`CATCH_UP_STEP`] blocks, //! and the first step after launch, are catch-up and skipped — every block -//! for [`EVERY_BLOCK_WINDOW`] blocks and every [`SLOW_INTERVAL`] blocks after. +//! for [`EVERY_BLOCK_WINDOW`] blocks, counted from the first pass that sees +//! the root while the wallet follows the tip, and every [`SLOW_INTERVAL`] +//! blocks after. Each Uncertain report adds one immediate probe. //! Between passes a wallet waits for the height at which its next root is due; //! one whose last pass had nothing left to probe is idle and not scanned //! again until a wallet event that touched its records (a new or @@ -92,7 +94,8 @@ pub(crate) struct OutgoingView { /// The transaction spends at least one of this wallet's coins, i.e. the /// wallet signed it. pub spends_own_coins: bool, - pub transaction: Transaction, + /// Shared with the passes and probe jobs that send it; never changed. + pub transaction: Arc, } impl OutgoingView { @@ -140,7 +143,7 @@ pub(crate) fn merge_records<'a>( .map(|(txid, (own, transaction))| OutgoingView { txid, spends_own_coins: own, - transaction: transaction.clone(), + transaction: Arc::new(transaction.clone()), }) .collect() } @@ -301,23 +304,40 @@ fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { /// When a root is next due for a probe. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) struct ProbeSchedule { - first_seen: u32, + /// Where the every-block window starts: the first pass that saw the root + /// while the wallet followed the tip. `None` while it has only been seen + /// behind the tip (a forced pass right after launch, at the stored tip): + /// probed every block until anchored. + first_seen: Option, last_probe: Option, } impl ProbeSchedule { - pub(crate) fn new(first_seen: u32) -> Self { + /// A schedule seen first at `height`, anchored there when the wallet + /// `following` the tip. + pub(crate) fn new(height: u32, following: bool) -> Self { Self { - first_seen, + first_seen: following.then_some(height), last_probe: None, } } + /// Start the window here if it has not started — once, at the first pass + /// that follows the tip. + fn anchor(&mut self, height: u32) { + self.first_seen.get_or_insert(height); + } + + fn in_window(&self, height: u32) -> bool { + self.first_seen + .is_none_or(|first| height.saturating_sub(first) < EVERY_BLOCK_WINDOW) + } + pub(crate) fn is_due(&self, height: u32) -> bool { match self.last_probe { None => true, Some(last) if height <= last => false, - Some(_) if height.saturating_sub(self.first_seen) < EVERY_BLOCK_WINDOW => true, + Some(_) if self.in_window(height) => true, Some(last) => height - last >= SLOW_INTERVAL, } } @@ -335,11 +355,7 @@ impl ProbeSchedule { pub(crate) fn next_due(&self) -> u32 { match self.last_probe { None => 0, - Some(last) - if last.saturating_add(1).saturating_sub(self.first_seen) < EVERY_BLOCK_WINDOW => - { - last.saturating_add(1) - } + Some(last) if self.in_window(last.saturating_add(1)) => last.saturating_add(1), Some(last) => last.saturating_add(SLOW_INTERVAL), } } @@ -495,16 +511,6 @@ impl ResolverState { } } - /// Start the every-block window of every root of `wallet_id` at `height` - /// at the latest. - fn restart_windows(&mut self, wallet_id: &WalletId, height: u32) { - for ((wallet, _), schedule) in self.schedules.iter_mut() { - if wallet == wallet_id { - schedule.first_seen = schedule.first_seen.max(height); - } - } - } - /// Roots of `wallet_id` a probe found in a block the wallet has not seen. fn mined(&self, wallet_id: &WalletId) -> HashSet { self.finished @@ -550,13 +556,16 @@ pub(crate) struct PassStart { /// it `Uncertain`) are due regardless of their schedule — even if one went /// out at this height already: a repeat probe is cheaper than bookkeeping /// that tells the two apart. A root found mined counts as settled when -/// picking roots, so its children are probed next. +/// picking roots, so its children are probed next. A root's every-block +/// window starts at the first pass made while the wallet is `following` the +/// tip; one seen only behind it is probed every block until then. pub(crate) fn begin_pass( state: &mut ResolverState, wallet_id: WalletId, height: u32, views: &[OutgoingView], forced: &HashSet, + following: bool, ) -> PassStart { let mut events = Vec::new(); // What the wallet itself still holds unsettled — what state is kept for. @@ -607,12 +616,15 @@ pub(crate) fn begin_pass( let schedule = state .schedules .entry(key) - .or_insert_with(|| ProbeSchedule::new(height)); + .or_insert_with(|| ProbeSchedule::new(height, following)); + if following { + schedule.anchor(height); + } let forced = forced_roots.contains(&view.txid); if forced || schedule.is_due(height) { due.push_back(DueRoot { txid: view.txid, - transaction: Arc::new(view.transaction.clone()), + transaction: Arc::clone(&view.transaction), own: graph.is_own(&view.txid), forced, }); @@ -1068,18 +1080,7 @@ impl Actor { let entry = self.entry(wallet_id); let previous = entry.height.replace(height); let catch_up = is_catch_up_step(previous, height); - // Back to following the tip: roots first seen while the wallet - // was behind (a forced pass right after launch counts heights - // from the stored tip) get their every-block window from here. - let caught_up = !catch_up && !entry.following; entry.following = !catch_up; - if caught_up { - entry.wait_until = None; - self.state.restart_windows(&wallet_id, height); - } - let Some(entry) = self.wallets.get_mut(&wallet_id) else { - return; - }; // While a pass runs, its end decides the wait: queue the height // and let `probe_next` drop it if no root turns out due. if !enabled @@ -1238,14 +1239,19 @@ impl Actor { Some(None) => self.end_run(wallet_id), Some(Some(read)) => { // A request queued before any height was reported - // carries 0; the wallet's own synced height is better. - let height = height.max(read.synced_height); + // carries 0; the wallet's own synced height, or one + // reported while this read ran, is better. + let height = height + .max(read.synced_height) + .max(entry.height.unwrap_or(0)); + let following = entry.following; let start = begin_pass( &mut self.state, wallet_id, height, &read.views, &read.forced, + following, ); if let Some(current) = entry.run.as_mut() { current.probing = Some(Probing { @@ -1267,19 +1273,26 @@ impl Actor { run, verdict, } => { - let Some(probing) = self + let Some(current) = self .wallets .get_mut(&wallet_id) .and_then(|entry| entry.run.as_mut()) .filter(|current| current.id == run) - .and_then(|current| current.probing.as_mut()) else { + return; // a dropped run's result + }; + // Cannot happen: a probe job is spawned only by a probing run + // with its root in flight. Fail safe — end the run rather than + // leave it blocking every later pass for the wallet. + let Some(probing) = current.probing.as_mut() else { + tracing::error!( + wallet_id = %hex::encode(wallet_id), + "broadcast probe: a probe result for a run that is not probing" + ); + self.end_run(wallet_id); return; }; let Some(root) = probing.in_flight.take() else { - // Cannot happen: a probe job is spawned only with its root - // in flight. Fail safe — end the run rather than leave it - // blocking every later pass for the wallet. tracing::error!( wallet_id = %hex::encode(wallet_id), "broadcast probe: a probe result with no root in flight" @@ -1599,7 +1612,7 @@ mod tests { OutgoingView { txid: txid(n), spends_own_coins: true, - transaction: tx(n, inputs), + transaction: Arc::new(tx(n, inputs)), } } @@ -1715,12 +1728,12 @@ mod tests { #[test] fn should_be_due_immediately_when_never_probed() { - assert!(ProbeSchedule::new(100).is_due(100)); + assert!(ProbeSchedule::new(100, true).is_due(100)); } #[test] fn should_probe_at_most_once_per_block() { - let mut s = ProbeSchedule::new(100); + let mut s = ProbeSchedule::new(100, true); s.probed_at(100); assert!(!s.is_due(100)); assert!(s.is_due(101)); @@ -1729,7 +1742,7 @@ mod tests { #[test] fn should_slow_down_after_the_every_block_window() { let first = 100; - let mut s = ProbeSchedule::new(first); + let mut s = ProbeSchedule::new(first, true); let late = first + EVERY_BLOCK_WINDOW; s.probed_at(late); for height in late + 1..late + SLOW_INTERVAL { @@ -1742,7 +1755,7 @@ mod tests { #[test] fn should_agree_on_the_next_due_height_with_is_due() { let first = 100; - let mut schedule = ProbeSchedule::new(first); + let mut schedule = ProbeSchedule::new(first, true); for probed in [ first, first + 5, @@ -1866,7 +1879,7 @@ mod tests { ) -> u32 { let due = { let mut guard = state.lock().expect("state"); - let start = begin_pass(&mut guard.state, wallet_id, height, views, forced); + let start = begin_pass(&mut guard.state, wallet_id, height, views, forced, true); guard.push(start.events); start.due }; @@ -2642,6 +2655,9 @@ mod tests { rig.actor.handle(Command::Uncertain(txid(2))); let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); rig.actor.joined(listed); + // The forced pass has read the wallet; its probe of root 2 is out. + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); rig.actor.handle(Command::Height { wallet_id: wallet(), height: 103, @@ -2657,13 +2673,13 @@ mod tests { fn should_keep_due_the_roots_a_run_never_sent() { let mut state = ResolverState::default(); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])]; - let start = begin_pass(&mut state, wallet(), 100, &views, &HashSet::new()); + let start = begin_pass(&mut state, wallet(), 100, &views, &HashSet::new(), true); let planned: Vec = start.due.iter().map(|root| root.txid).collect(); assert_eq!(planned.len(), 2); // Only the first root goes out before the run ends. mark_sent(&mut state, wallet(), planned[0], 100); - let again = begin_pass(&mut state, wallet(), 100, &views, &HashSet::new()); + let again = begin_pass(&mut state, wallet(), 100, &views, &HashSet::new(), true); assert_eq!( again.due.iter().map(|root| root.txid).collect::>(), @@ -2677,7 +2693,7 @@ mod tests { fn should_make_a_root_due_at_once_when_its_probe_is_withdrawn() { let mut state = ResolverState::default(); let views = [send(1, &[outpoint(90, 0)])]; - begin_pass(&mut state, wallet(), 100, &views, &HashSet::new()); + begin_pass(&mut state, wallet(), 100, &views, &HashSet::new(), true); mark_sent(&mut state, wallet(), txid(1), 100); assert!(!state.schedules[&(wallet(), txid(1))].is_due(100)); @@ -2746,6 +2762,57 @@ mod tests { assert_eq!(probe.probed().len(), 6, "every block from 501 to 505"); } + /// A return to following the tip does not restart the every-block window + /// of a root already past it. + #[tokio::test] + async fn should_not_restart_the_window_of_a_root_past_it_after_a_catch_up() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + let past = 101 + EVERY_BLOCK_WINDOW + SLOW_INTERVAL; + for height in 102..=past { + rig.height(wallet(), height).await; + } + let before = probe.probed().len(); + + // Away for a while, then following again. + rig.height(wallet(), past + 10).await; + for height in past + 11..=past + 14 { + rig.height(wallet(), height).await; + } + + assert!( + probe.probed().len() <= before + 1, + "slow cadence kept: at most one probe in four blocks" + ); + } + + /// A forced read whose result is joined only after the wallet follows the + /// tip again anchors the root at the tip, not at the stale stored height. + #[tokio::test] + async fn should_anchor_a_root_read_before_the_wallet_followed_at_the_tip() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + + rig.actor.handle(Command::Uncertain(txid(1))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.actor.joined(listed); + for height in [500, 501] { + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height, + }); + } + rig.settle().await; + for height in 502..=505 { + rig.height(wallet(), height).await; + } + + assert!(probe.probed().len() >= 5, "every block from 501 to 505"); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From 0bb14a268081b9acbca962dfa349351bedc91c23 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 14:23:13 +0300 Subject: [PATCH 023/169] fix(platform-wallet): seventeenth review pass on the broadcast resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Only a height-driven pass while the wallet follows the tip starts a root's every-block window. A forced pass may run at a stale height — the one from before the app went to the background, or the stored tip right after launch — so it no longer anchors; the next following height does. A forced pass runs at max(queued, synced) height again, not at a catch-up height reported while it read. - ProbeSchedule::new() starts unanchored; the field is window_start and the EVERY_BLOCK_WINDOW doc counts from it. - The not-probing fail-safe aborts the run's job before ending it. - Tests assert exact probe counts: slow cadence kept after a catch-up, a window of exactly EVERY_BLOCK_WINDOW blocks from the tip after a racing forced read, and a new one for a send reported right after a return from the background (fails if forced passes anchor). - Rust, FFI and Swift docs describe when the 24-block window runs. The follow-the-tip signal stays the height-step rule: dash-spv's SyncComplete fires on every not-synced to synced transition, each new block included, so it does not tell catch-up from following. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet-ffi/src/manager.rs | 6 +- .../src/manager/accessors.rs | 4 +- .../src/wallet/core/broadcast_resolver.rs | 115 ++++++++++++------ .../PlatformWalletManagerBroadcastProbe.swift | 7 +- 4 files changed, 87 insertions(+), 45 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 8a7c7a9879e..e966ed4cfb8 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -2147,8 +2147,10 @@ mod remove_wallet_lifecycle_tests { /// Turn automatic probing of unconfirmed sends on or off (off by default). /// /// While on, the roots of every unconfirmed chain are resubmitted to evonodes -/// over DAPI — right after SPV reports a send uncertain, then every block for -/// 24 blocks and every 10 after — and each change of a send's verdict, and +/// over DAPI — right after SPV reports a send uncertain (each report), then, +/// while the wallet follows the chain tip, every block for 24 blocks from when +/// it first did so for that root, and every 10 blocks after (a return from the +/// background does not restart the 24) — and each change of a send's verdict, and /// each clear, reaches the host through /// `EventHandlerCallbacksExtension::on_outgoing_transaction_probed_fn`. /// Nothing in the wallet changes; turning it off clears every verdict. diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index 0db284c1d04..fee4b591545 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -526,7 +526,9 @@ impl PlatformWalletManager

{ /// /// When on, the roots of a registered wallet's unconfirmed chains are /// resubmitted to evonodes over DAPI — right after dash-spv reports a send - /// `Uncertain`, then every block for 24 blocks and every 10 after — and + /// `Uncertain` (each report), then, while the wallet follows the chain tip, + /// every block for 24 blocks from when it first did so for that root, and + /// every 10 blocks after, a return from the background included — and /// each change of a send's verdict is published as /// `on_outgoing_transaction_probed`, each send that no longer needs one /// as `on_outgoing_transaction_cleared`. Nothing in the wallet changes. diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 4bcc27f45c1..e14582ede50 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -75,7 +75,8 @@ use crate::broadcast_probe::{AcceptanceProbe, ProbeVerdict}; use crate::events::{PlatformEventHandler, PlatformEventManager}; use crate::wallet::platform_wallet::PlatformWalletInfo; -/// Blocks after a root is first seen during which it is probed on every block. +/// Blocks from the start of a root's window (see [`ProbeSchedule`]) during +/// which it is probed on every block. pub(crate) const EVERY_BLOCK_WINDOW: u32 = 24; /// Blocks between probes once [`EVERY_BLOCK_WINDOW`] has passed. @@ -304,32 +305,29 @@ fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { /// When a root is next due for a probe. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) struct ProbeSchedule { - /// Where the every-block window starts: the first pass that saw the root - /// while the wallet followed the tip. `None` while it has only been seen - /// behind the tip (a forced pass right after launch, at the stored tip): - /// probed every block until anchored. - first_seen: Option, + /// Where the every-block window starts: the first height-driven pass that + /// saw the root while the wallet followed the tip. `None` until then — + /// the root was only seen by a forced pass, whose height may be a stale + /// stored tip — and it is due at the next pass once the wallet follows. + window_start: Option, last_probe: Option, } impl ProbeSchedule { - /// A schedule seen first at `height`, anchored there when the wallet - /// `following` the tip. - pub(crate) fn new(height: u32, following: bool) -> Self { + pub(crate) fn new() -> Self { Self { - first_seen: following.then_some(height), + window_start: None, last_probe: None, } } - /// Start the window here if it has not started — once, at the first pass - /// that follows the tip. + /// Start the window here if it has not started — once. fn anchor(&mut self, height: u32) { - self.first_seen.get_or_insert(height); + self.window_start.get_or_insert(height); } fn in_window(&self, height: u32) -> bool { - self.first_seen + self.window_start .is_none_or(|first| height.saturating_sub(first) < EVERY_BLOCK_WINDOW) } @@ -556,16 +554,16 @@ pub(crate) struct PassStart { /// it `Uncertain`) are due regardless of their schedule — even if one went /// out at this height already: a repeat probe is cheaper than bookkeeping /// that tells the two apart. A root found mined counts as settled when -/// picking roots, so its children are probed next. A root's every-block -/// window starts at the first pass made while the wallet is `following` the -/// tip; one seen only behind it is probed every block until then. +/// picking roots, so its children are probed next. `anchoring`: a +/// height-driven pass while the wallet follows the tip — it starts the +/// every-block window of every root that has none yet. pub(crate) fn begin_pass( state: &mut ResolverState, wallet_id: WalletId, height: u32, views: &[OutgoingView], forced: &HashSet, - following: bool, + anchoring: bool, ) -> PassStart { let mut events = Vec::new(); // What the wallet itself still holds unsettled — what state is kept for. @@ -616,8 +614,8 @@ pub(crate) fn begin_pass( let schedule = state .schedules .entry(key) - .or_insert_with(|| ProbeSchedule::new(height, following)); - if following { + .or_insert_with(ProbeSchedule::new); + if anchoring { schedule.anchor(height); } let forced = forced_roots.contains(&view.txid); @@ -896,6 +894,7 @@ enum JobDone { wallet_id: WalletId, run: u64, height: u32, + by_height: bool, read: Option>, }, Probed { @@ -1172,6 +1171,7 @@ impl Actor { wallet_id, run, height: pending.height, + by_height: pending.by_height, read, } }); @@ -1205,6 +1205,7 @@ impl Actor { wallet_id, run, height, + by_height, read, } => { let Some(entry) = self.wallets.get_mut(&wallet_id) else { @@ -1239,19 +1240,18 @@ impl Actor { Some(None) => self.end_run(wallet_id), Some(Some(read)) => { // A request queued before any height was reported - // carries 0; the wallet's own synced height, or one - // reported while this read ran, is better. - let height = height - .max(read.synced_height) - .max(entry.height.unwrap_or(0)); - let following = entry.following; + // carries 0; the wallet's own synced height is better. + let height = height.max(read.synced_height); + // Only a height-driven pass while following knows the + // tip: a forced pass may run at a stale height. + let anchoring = by_height && entry.following; let start = begin_pass( &mut self.state, wallet_id, height, &read.views, &read.forced, - following, + anchoring, ); if let Some(current) = entry.run.as_mut() { current.probing = Some(Probing { @@ -1289,6 +1289,7 @@ impl Actor { wallet_id = %hex::encode(wallet_id), "broadcast probe: a probe result for a run that is not probing" ); + current.abort.abort(); self.end_run(wallet_id); return; }; @@ -1726,14 +1727,20 @@ mod tests { // ---- ProbeSchedule / catch-up ------------------------------------------ + fn anchored(height: u32) -> ProbeSchedule { + let mut schedule = ProbeSchedule::new(); + schedule.anchor(height); + schedule + } + #[test] fn should_be_due_immediately_when_never_probed() { - assert!(ProbeSchedule::new(100, true).is_due(100)); + assert!(anchored(100).is_due(100)); } #[test] fn should_probe_at_most_once_per_block() { - let mut s = ProbeSchedule::new(100, true); + let mut s = anchored(100); s.probed_at(100); assert!(!s.is_due(100)); assert!(s.is_due(101)); @@ -1742,7 +1749,7 @@ mod tests { #[test] fn should_slow_down_after_the_every_block_window() { let first = 100; - let mut s = ProbeSchedule::new(first, true); + let mut s = anchored(first); let late = first + EVERY_BLOCK_WINDOW; s.probed_at(late); for height in late + 1..late + SLOW_INTERVAL { @@ -1755,7 +1762,7 @@ mod tests { #[test] fn should_agree_on_the_next_due_height_with_is_due() { let first = 100; - let mut schedule = ProbeSchedule::new(first, true); + let mut schedule = anchored(first); for probed in [ first, first + 5, @@ -2763,33 +2770,38 @@ mod tests { } /// A return to following the tip does not restart the every-block window - /// of a root already past it. + /// of a root already past it: exactly one probe, when the slow interval + /// is up. #[tokio::test] async fn should_not_restart_the_window_of_a_root_past_it_after_a_catch_up() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); let mut rig = enabled(probe.clone()).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.follow(wallet(), 100).await; + // Window 101..=124, then 134. let past = 101 + EVERY_BLOCK_WINDOW + SLOW_INTERVAL; for height in 102..=past { rig.height(wallet(), height).await; } - let before = probe.probed().len(); + assert_eq!(probe.probed().len(), EVERY_BLOCK_WINDOW as usize + 1); - // Away for a while, then following again. + // Away for a while (a catch-up step), then following again. rig.height(wallet(), past + 10).await; for height in past + 11..=past + 14 { rig.height(wallet(), height).await; } - assert!( - probe.probed().len() <= before + 1, - "slow cadence kept: at most one probe in four blocks" + assert_eq!( + probe.probed().len(), + EVERY_BLOCK_WINDOW as usize + 2, + "one probe at the first following height past the slow interval" ); } /// A forced read whose result is joined only after the wallet follows the - /// tip again anchors the root at the tip, not at the stale stored height. + /// tip again does not start the window at the stale stored height: the + /// next height-driven pass starts it at the tip, for exactly + /// EVERY_BLOCK_WINDOW blocks. #[tokio::test] async fn should_anchor_a_root_read_before_the_wallet_followed_at_the_tip() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); @@ -2806,11 +2818,34 @@ mod tests { }); } rig.settle().await; - for height in 502..=505 { + for height in 502..=530 { + rig.height(wallet(), height).await; + } + + // The forced probe, then 501..=524 every block, nothing up to 530. + assert_eq!(probe.probed().len(), 1 + EVERY_BLOCK_WINDOW as usize); + } + + /// Back from the background with no height event yet, a send reported + /// `Uncertain` is probed at once but its window waits for the wallet to + /// follow the tip again — it does not start at the height from before. + #[tokio::test] + async fn should_not_start_a_window_at_the_height_from_before_the_background() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), Vec::new()); + rig.follow(wallet(), 100).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + + rig.send(Command::Uncertain(txid(1))).await; + assert_eq!(probe.probed().len(), 1, "forced at once"); + // 30 blocks later: a catch-up step, then following. + rig.height(wallet(), 131).await; + for height in 132..=136 { rig.height(wallet(), height).await; } - assert!(probe.probed().len() >= 5, "every block from 501 to 505"); + assert_eq!(probe.probed().len(), 1 + 5, "every block from 132"); } /// A probe that panics on its `nth` call and accepts otherwise. diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index a00aca0d8a8..c4ac0037af7 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -106,8 +106,11 @@ extension PlatformWalletManager { /// While on, the SDK resubmits to evonodes over DAPI the root of every /// chain of unconfirmed sends — the first unsettled transaction the chain /// depends on, which may be an incoming payment the wallet spent — right - /// after SPV reports a broadcast uncertain, then every block for 24 blocks - /// and every 10 after. It publishes each *change* of verdict for the + /// after SPV reports a broadcast uncertain (each report), then, while the + /// wallet follows the chain tip, every block for 24 blocks from when it + /// first did so for that root, and every 10 blocks after — a return from + /// the background does not restart the 24. It publishes each *change* of + /// verdict for the /// wallet's own sends in `outgoingTransactionVerdicts` / /// `lastOutgoingTransactionProbe`. An entry is removed when its send /// settles or leaves the wallet, when its wallet is deleted, and every From f44785f59948eb517392faf33e3a258caa142878 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 14:32:53 +0300 Subject: [PATCH 024/169] fix(platform-wallet): eighteenth review pass on the broadcast resolver - A step back in synced height (a rescan after a rewind, e.g. a new account) counts as catch-up, so it cannot anchor windows at replayed heights. - A height pass anchors only if no catch-up happened since its height was queued: a pass overtaken by a return from the background runs at a stale height and leaves anchoring to the next one. - ProbeSchedule derives Default; its window_start doc says an unanchored root is due at every pass meanwhile; the module doc says only a height-driven pass starts a window. - FFI imports CString for the reason conversion; the Swift doc reflow is fixed. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/event_handler.rs | 4 +- .../src/wallet/core/broadcast_resolver.rs | 85 ++++++++++++++----- .../PlatformWalletManagerBroadcastProbe.swift | 3 +- 3 files changed, 67 insertions(+), 25 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/event_handler.rs b/packages/rs-platform-wallet-ffi/src/event_handler.rs index c6850261304..7d234864085 100644 --- a/packages/rs-platform-wallet-ffi/src/event_handler.rs +++ b/packages/rs-platform-wallet-ffi/src/event_handler.rs @@ -11,6 +11,7 @@ use platform_wallet::manager::dpns_sync::{DpnsSyncPassSummary, WalletDpnsSyncOut #[cfg(feature = "shielded")] use platform_wallet::manager::shielded_sync::{ShieldedSyncPassSummary, WalletShieldedOutcome}; use platform_wallet::{PlatformAddressSyncSummary, WalletSyncOutcome}; +use std::ffi::CString; use std::os::raw::{c_char, c_void}; /// Current layout version of [`EventHandlerCallbacksExtension`]. @@ -326,8 +327,7 @@ impl PlatformEventHandler for FFIEventHandler { } }; // A reason is a node's text: strip interior NULs rather than lose it. - let reason = - reason.and_then(|reason| std::ffi::CString::new(reason.replace('\0', "")).ok()); + let reason = reason.and_then(|reason| CString::new(reason.replace('\0', "")).ok()); let txid_bytes: &[u8] = txid.as_ref(); unsafe { callback( diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index e14582ede50..524271a568e 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -34,8 +34,9 @@ //! of that transaction's chain), then on each advance of the wallet's synced //! height that follows the tip — steps of more than [`CATCH_UP_STEP`] blocks, //! and the first step after launch, are catch-up and skipped — every block -//! for [`EVERY_BLOCK_WINDOW`] blocks, counted from the first pass that sees -//! the root while the wallet follows the tip, and every [`SLOW_INTERVAL`] +//! for [`EVERY_BLOCK_WINDOW`] blocks, counted from the first height-driven +//! pass that sees the root while the wallet follows the tip (a forced pass +//! does not start it), and every [`SLOW_INTERVAL`] //! blocks after. Each Uncertain report adds one immediate probe. //! Between passes a wallet waits for the height at which its next root is due; //! one whose last pass had nothing left to probe is idle and not scanned @@ -303,24 +304,17 @@ fn ambiguous_roots(views: &[OutgoingView]) -> Vec<&OutgoingView> { } /// When a root is next due for a probe. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub(crate) struct ProbeSchedule { /// Where the every-block window starts: the first height-driven pass that /// saw the root while the wallet followed the tip. `None` until then — /// the root was only seen by a forced pass, whose height may be a stale - /// stored tip — and it is due at the next pass once the wallet follows. + /// stored tip — and it is due at every pass that runs meanwhile. window_start: Option, last_probe: Option, } impl ProbeSchedule { - pub(crate) fn new() -> Self { - Self { - window_start: None, - last_probe: None, - } - } - /// Start the window here if it has not started — once. fn anchor(&mut self, height: u32) { self.window_start.get_or_insert(height); @@ -521,9 +515,10 @@ impl ResolverState { /// The first advance after launch has no previous height to compare with, and /// it is usually the stored tip from before the scan: treat it as catch-up -/// too. Probing starts from the next step. +/// too. So is a step back — a rescan after a rewind (a new account) replays +/// old heights. Probing starts from the next step that follows the tip. pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { - previous.is_none_or(|previous| height.saturating_sub(previous) > CATCH_UP_STEP) + previous.is_none_or(|previous| height < previous || height - previous > CATCH_UP_STEP) } /// A root chosen for a probe. @@ -611,10 +606,7 @@ pub(crate) fn begin_pass( if state.finished.contains_key(&key) { continue; } - let schedule = state - .schedules - .entry(key) - .or_insert_with(ProbeSchedule::new); + let schedule = state.schedules.entry(key).or_default(); if anchoring { schedule.anchor(height); } @@ -895,6 +887,7 @@ enum JobDone { run: u64, height: u32, by_height: bool, + catch_ups: u64, read: Option>, }, Probed { @@ -911,6 +904,9 @@ struct PendingPass { /// results did. by_height: bool, forced: HashSet, + /// The wallet's catch-up count when the last height joined this pass: a + /// catch-up since then makes the pass's height stale for anchoring. + catch_ups: u64, } /// A pass in flight: reading the wallet, then probing its due roots one at @@ -941,6 +937,8 @@ struct WalletEntry { height: Option, /// The last height step followed the tip (was not catch-up). following: bool, + /// Catch-up steps seen so far. + catch_ups: u64, /// Height advances below this are skipped — no walk over the wallet's /// records while no root can be due. `u32::MAX` when nothing is left to /// probe (idle). Reset by anything that may have left new work — which @@ -1080,6 +1078,10 @@ impl Actor { let previous = entry.height.replace(height); let catch_up = is_catch_up_step(previous, height); entry.following = !catch_up; + if catch_up { + entry.catch_ups += 1; + } + let catch_ups = entry.catch_ups; // While a pass runs, its end decides the wait: queue the height // and let `probe_next` drop it if no root turns out due. if !enabled @@ -1091,6 +1093,7 @@ impl Actor { let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(height); pending.by_height = true; + pending.catch_ups = catch_ups; self.start(wallet_id); } Command::Seen { wallet_id, touched } => { @@ -1172,6 +1175,7 @@ impl Actor { run, height: pending.height, by_height: pending.by_height, + catch_ups: pending.catch_ups, read, } }); @@ -1206,6 +1210,7 @@ impl Actor { run, height, by_height, + catch_ups, read, } => { let Some(entry) = self.wallets.get_mut(&wallet_id) else { @@ -1242,9 +1247,11 @@ impl Actor { // A request queued before any height was reported // carries 0; the wallet's own synced height is better. let height = height.max(read.synced_height); - // Only a height-driven pass while following knows the - // tip: a forced pass may run at a stale height. - let anchoring = by_height && entry.following; + // Only a height-driven pass queued while following, with + // no catch-up since, knows the tip: a forced pass, or + // one overtaken by a catch-up, may run at a stale height. + let anchoring = + by_height && entry.following && entry.catch_ups == catch_ups; let start = begin_pass( &mut self.state, wallet_id, @@ -1728,7 +1735,7 @@ mod tests { // ---- ProbeSchedule / catch-up ------------------------------------------ fn anchored(height: u32) -> ProbeSchedule { - let mut schedule = ProbeSchedule::new(); + let mut schedule = ProbeSchedule::default(); schedule.anchor(height); schedule } @@ -1787,6 +1794,11 @@ mod tests { assert!(!is_catch_up_step(Some(999), 1_000)); assert!(!is_catch_up_step(Some(1_000 - CATCH_UP_STEP), 1_000)); assert!(is_catch_up_step(Some(1_000 - CATCH_UP_STEP - 1), 1_000)); + assert!( + is_catch_up_step(Some(1_000), 999), + "a step back is a rescan, not the tip" + ); + assert!(!is_catch_up_step(Some(1_000), 1_000)); } // ---- begin_pass / record_probe ------------------------------------------ @@ -2848,6 +2860,37 @@ mod tests { assert_eq!(probe.probed().len(), 1 + 5, "every block from 132"); } + /// A height pass queued before the app went to the background, whose read + /// is handled only after the wallet follows the tip again, does not start + /// a window at its old height; the next pass starts it at the tip. + #[tokio::test] + async fn should_not_anchor_a_pass_overtaken_by_a_catch_up() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), Vec::new()); + rig.follow(wallet(), 100).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.send(Command::Seen { + wallet_id: wallet(), + touched: true, + }) + .await; + + for height in [102, 132, 133] { + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height, + }); + } + rig.settle().await; + for height in 134..=137 { + rig.height(wallet(), height).await; + } + + // 102 (stale, not anchored), then every block from 133. + assert_eq!(probe.probed().len(), 1 + 5); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index c4ac0037af7..8e0d7201316 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -110,8 +110,7 @@ extension PlatformWalletManager { /// wallet follows the chain tip, every block for 24 blocks from when it /// first did so for that root, and every 10 blocks after — a return from /// the background does not restart the 24. It publishes each *change* of - /// verdict for the - /// wallet's own sends in `outgoingTransactionVerdicts` / + /// verdict for the wallet's own sends in `outgoingTransactionVerdicts` / /// `lastOutgoingTransactionProbe`. An entry is removed when its send /// settles or leaves the wallet, when its wallet is deleted, and every /// entry when probing is turned off; a removal does not mean the send From fc6606ba00c58941cd15d69c686cee642e212b8c Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 14:43:36 +0300 Subject: [PATCH 025/169] fix(platform-wallet): nineteenth review pass on the broadcast resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - A pass carries the height it may start windows at (anchor_at), set by the latest following height that joined it; a catch-up step clears it on the queued pass and marks a running pass stale. This replaces the following flag and the catch-up counters. - A stale pass — queued before a catch-up, or whose read finds the wallet more than CATCH_UP_STEP past its queued height (a catch-up whose height step has not arrived yet) — probes only the roots an Uncertain result forced and starts no windows: catch-up is skipped, as documented. - Windows start at the anchor height itself, not at the read's synced height. - Module and CATCH_UP_STEP docs list steps back as catch-up. - Test: a pass whose read is far ahead is stale (fails without the check); the overtaken-pass test now expects no probe at the stale height. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 168 +++++++++++++----- 1 file changed, 125 insertions(+), 43 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 524271a568e..b41a867d272 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -33,7 +33,8 @@ //! When: once as soon as dash-spv reports an `Uncertain` broadcast (the root //! of that transaction's chain), then on each advance of the wallet's synced //! height that follows the tip — steps of more than [`CATCH_UP_STEP`] blocks, -//! and the first step after launch, are catch-up and skipped — every block +//! steps back (a rescan) and the first step after launch are catch-up and +//! skipped, along with passes queued before them — every block //! for [`EVERY_BLOCK_WINDOW`] blocks, counted from the first height-driven //! pass that sees the root while the wallet follows the tip (a forced pass //! does not start it), and every [`SLOW_INTERVAL`] @@ -84,8 +85,9 @@ pub(crate) const EVERY_BLOCK_WINDOW: u32 = 24; pub(crate) const SLOW_INTERVAL: u32 = 10; /// How far a wallet's synced height may advance in one step and still count -/// as following the tip. Larger steps are catch-up after time offline: the -/// scan itself settles most roots, so a height-driven pass waits for it. +/// as following the tip. Larger steps, and steps back (a rescan after a +/// rewind), are catch-up: the scan itself settles most roots, so a +/// height-driven pass waits for it. pub(crate) const CATCH_UP_STEP: u32 = 3; /// One unsettled transaction of the wallet, merged across the accounts that @@ -549,16 +551,16 @@ pub(crate) struct PassStart { /// it `Uncertain`) are due regardless of their schedule — even if one went /// out at this height already: a repeat probe is cheaper than bookkeeping /// that tells the two apart. A root found mined counts as settled when -/// picking roots, so its children are probed next. `anchoring`: a -/// height-driven pass while the wallet follows the tip — it starts the -/// every-block window of every root that has none yet. +/// picking roots, so its children are probed next. `anchor`: the tip height +/// a height-driven pass knows the wallet follows — it starts the every-block +/// window of every root that has none yet. pub(crate) fn begin_pass( state: &mut ResolverState, wallet_id: WalletId, height: u32, views: &[OutgoingView], forced: &HashSet, - anchoring: bool, + anchor: Option, ) -> PassStart { let mut events = Vec::new(); // What the wallet itself still holds unsettled — what state is kept for. @@ -607,8 +609,8 @@ pub(crate) fn begin_pass( continue; } let schedule = state.schedules.entry(key).or_default(); - if anchoring { - schedule.anchor(height); + if let Some(anchor) = anchor { + schedule.anchor(anchor); } let forced = forced_roots.contains(&view.txid); if forced || schedule.is_due(height) { @@ -886,8 +888,6 @@ enum JobDone { wallet_id: WalletId, run: u64, height: u32, - by_height: bool, - catch_ups: u64, read: Option>, }, Probed { @@ -904,9 +904,9 @@ struct PendingPass { /// results did. by_height: bool, forced: HashSet, - /// The wallet's catch-up count when the last height joined this pass: a - /// catch-up since then makes the pass's height stale for anchoring. - catch_ups: u64, + /// The latest height that joined this pass while the wallet followed the + /// tip; a catch-up step clears it. + anchor_at: Option, } /// A pass in flight: reading the wallet, then probing its due roots one at @@ -919,6 +919,12 @@ struct Run { /// Something touched the wallet during the run: its read may be stale, so /// the next height must not be skipped. woken: bool, + /// Where this pass starts windows (see [`PendingPass::anchor_at`]). + anchor_at: Option, + /// A catch-up step arrived since the pass was queued: its height is + /// stale, and it probes only the roots an `Uncertain` result forced — + /// catch-up is skipped, the scan settles most roots. + stale: bool, abort: AbortHandle, probing: Option, } @@ -935,10 +941,6 @@ struct Probing { struct WalletEntry { /// Last synced height reported, for telling catch-up from following. height: Option, - /// The last height step followed the tip (was not catch-up). - following: bool, - /// Catch-up steps seen so far. - catch_ups: u64, /// Height advances below this are skipped — no walk over the wallet's /// records while no root can be due. `u32::MAX` when nothing is left to /// probe (idle). Reset by anything that may have left new work — which @@ -1076,16 +1078,26 @@ impl Actor { let enabled = self.enabled; let entry = self.entry(wallet_id); let previous = entry.height.replace(height); - let catch_up = is_catch_up_step(previous, height); - entry.following = !catch_up; - if catch_up { - entry.catch_ups += 1; + if is_catch_up_step(previous, height) { + // Behind the tip: passes queued or running from before are + // stale — they keep only what an `Uncertain` result forced, + // and start no windows. + if let Some(run) = entry.run.as_mut() { + run.stale = true; + run.anchor_at = None; + } + if let Some(pending) = entry.pending.as_mut() { + pending.by_height = false; + pending.anchor_at = None; + if pending.forced.is_empty() { + entry.pending = None; + } + } + return; } - let catch_ups = entry.catch_ups; // While a pass runs, its end decides the wait: queue the height // and let `probe_next` drop it if no root turns out due. if !enabled - || catch_up || (entry.run.is_none() && entry.wait_until.is_some_and(|until| height < until)) { return; @@ -1093,7 +1105,7 @@ impl Actor { let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(height); pending.by_height = true; - pending.catch_ups = catch_ups; + pending.anchor_at = Some(height); self.start(wallet_id); } Command::Seen { wallet_id, touched } => { @@ -1163,6 +1175,7 @@ impl Actor { let Some(pending) = entry.pending.take() else { return; }; + let anchor_at = pending.anchor_at; self.next_run += 1; let run = self.next_run; let source = Arc::clone(&self.source); @@ -1174,8 +1187,6 @@ impl Actor { wallet_id, run, height: pending.height, - by_height: pending.by_height, - catch_ups: pending.catch_ups, read, } }); @@ -1184,6 +1195,8 @@ impl Actor { id: run, seq: entry.seq, woken: false, + anchor_at, + stale: false, abort, probing: None, }); @@ -1209,8 +1222,6 @@ impl Actor { wallet_id, run, height, - by_height, - catch_ups, read, } => { let Some(entry) = self.wallets.get_mut(&wallet_id) else { @@ -1247,19 +1258,30 @@ impl Actor { // A request queued before any height was reported // carries 0; the wallet's own synced height is better. let height = height.max(read.synced_height); - // Only a height-driven pass queued while following, with - // no catch-up since, knows the tip: a forced pass, or - // one overtaken by a catch-up, may run at a stale height. - let anchoring = - by_height && entry.following && entry.catch_ups == catch_ups; - let start = begin_pass( + let Some(current) = entry.run.as_mut() else { + return; + }; + // The wallet is further along than the height this + // pass was queued at: a catch-up whose height step has + // not reached the resolver yet. Stale all the same. + if current + .anchor_at + .is_some_and(|at| read.synced_height > at.saturating_add(CATCH_UP_STEP)) + { + current.stale = true; + current.anchor_at = None; + } + let mut start = begin_pass( &mut self.state, wallet_id, height, &read.views, &read.forced, - anchoring, + current.anchor_at, ); + if current.stale { + start.due.retain(|root| root.forced); + } if let Some(current) = entry.run.as_mut() { current.probing = Some(Probing { views: read.views, @@ -1333,6 +1355,9 @@ impl Actor { let Some(probing) = run.probing.as_mut() else { return; }; + if run.stale { + probing.due.retain(|root| root.forced); + } if let Some(root) = probing.due.pop_front() { let probe = Arc::clone(&self.probe); let id = run.id; @@ -1898,7 +1923,14 @@ mod tests { ) -> u32 { let due = { let mut guard = state.lock().expect("state"); - let start = begin_pass(&mut guard.state, wallet_id, height, views, forced, true); + let start = begin_pass( + &mut guard.state, + wallet_id, + height, + views, + forced, + Some(height), + ); guard.push(start.events); start.due }; @@ -2302,6 +2334,8 @@ mod tests { struct FakeSource { views: StdMutex>>, walks: StdMutex>, + /// The synced height every read reports. + synced: StdMutex, } #[async_trait] @@ -2330,7 +2364,7 @@ mod tests { self.walks.lock().expect("walks").push(wallet_id); WalletRead { views, - synced_height: 0, + synced_height: *self.synced.lock().expect("synced"), forced, } })) @@ -2692,13 +2726,27 @@ mod tests { fn should_keep_due_the_roots_a_run_never_sent() { let mut state = ResolverState::default(); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])]; - let start = begin_pass(&mut state, wallet(), 100, &views, &HashSet::new(), true); + let start = begin_pass( + &mut state, + wallet(), + 100, + &views, + &HashSet::new(), + Some(100), + ); let planned: Vec = start.due.iter().map(|root| root.txid).collect(); assert_eq!(planned.len(), 2); // Only the first root goes out before the run ends. mark_sent(&mut state, wallet(), planned[0], 100); - let again = begin_pass(&mut state, wallet(), 100, &views, &HashSet::new(), true); + let again = begin_pass( + &mut state, + wallet(), + 100, + &views, + &HashSet::new(), + Some(100), + ); assert_eq!( again.due.iter().map(|root| root.txid).collect::>(), @@ -2712,7 +2760,14 @@ mod tests { fn should_make_a_root_due_at_once_when_its_probe_is_withdrawn() { let mut state = ResolverState::default(); let views = [send(1, &[outpoint(90, 0)])]; - begin_pass(&mut state, wallet(), 100, &views, &HashSet::new(), true); + begin_pass( + &mut state, + wallet(), + 100, + &views, + &HashSet::new(), + Some(100), + ); mark_sent(&mut state, wallet(), txid(1), 100); assert!(!state.schedules[&(wallet(), txid(1))].is_due(100)); @@ -2887,8 +2942,35 @@ mod tests { rig.height(wallet(), height).await; } - // 102 (stale, not anchored), then every block from 133. - assert_eq!(probe.probed().len(), 1 + 5); + // Nothing at 102 (stale: catch-up skips it), then every block from 133. + assert_eq!(probe.probed().len(), 5); + } + + /// A height pass whose read finds the wallet far ahead of the height it was + /// queued at — a catch-up whose height step has not arrived yet — is + /// stale: it neither probes by schedule nor starts a window. + #[tokio::test] + async fn should_treat_a_pass_whose_read_is_far_ahead_as_stale() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), Vec::new()); + rig.follow(wallet(), 100).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.send(Command::Seen { + wallet_id: wallet(), + touched: true, + }) + .await; + *rig.source.synced.lock().expect("synced") = 1_101; + + rig.height(wallet(), 102).await; + assert!(probe.probed().is_empty(), "stale: no probe during the scan"); + rig.height(wallet(), 1_101).await; + for height in 1_102..=1_105 { + rig.height(wallet(), height).await; + } + + assert_eq!(probe.probed().len(), 4, "every block from 1102"); } /// A probe that panics on its `nth` call and accepts otherwise. From 8978342587817c78bf8eb953feef91470c3c0bd5 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 14:54:40 +0300 Subject: [PATCH 026/169] fix(platform-wallet): twentieth review pass on the broadcast resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One rule replaces the stale flag: a pass without an anchor height probes only the roots an Uncertain result forced. That covers passes overtaken by a catch-up, forced passes queued across one, and forced passes that start during one — which used to carry every schedule-due root to the network mid-scan. - A read that finds the wallet behind its anchor (a rewind for a rescan) drops the anchor, as one far ahead already did. - A catch-up step aborts a pass still reading with nothing forced. - PendingPass.by_height is gone (anchor_at.is_some()); the Read handler keeps one borrow of the run and the confinement lives in probe_next only; DueRoot.forced is documented as deciding what an unanchored pass probes. - The test rig moves the wallet's synced height with each height step, as the SPV client does. New tests (each fails without its fix): forced-only pass during catch-up, read behind the anchor, reading pass stopped by a catch-up. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 180 +++++++++++++----- 1 file changed, 128 insertions(+), 52 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index b41a867d272..5705ae93024 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -531,8 +531,9 @@ pub(crate) struct DueRoot { pub transaction: Arc, /// The root is the wallet's own send. pub own: bool, - /// An `Uncertain` result forced it (for the log; it may also be due by - /// its schedule). + /// An `Uncertain` result forced it (it may also be due by its schedule). + /// Labels its verdict in the log, and is the only kind of root a pass + /// without an anchor probes. pub forced: bool, } @@ -900,12 +901,11 @@ enum JobDone { #[derive(Default)] struct PendingPass { height: u32, - /// A height advance asked for this pass; otherwise only `Uncertain` - /// results did. - by_height: bool, forced: HashSet, /// The latest height that joined this pass while the wallet followed the - /// tip; a catch-up step clears it. + /// tip; a catch-up step clears it. A pass without one — only `Uncertain` + /// results asked for it, or a catch-up overtook it — probes only the + /// roots those results forced and starts no windows. anchor_at: Option, } @@ -919,12 +919,11 @@ struct Run { /// Something touched the wallet during the run: its read may be stale, so /// the next height must not be skipped. woken: bool, - /// Where this pass starts windows (see [`PendingPass::anchor_at`]). + /// Where this pass starts windows; `None` confines it to forced roots + /// (see [`PendingPass::anchor_at`]). anchor_at: Option, - /// A catch-up step arrived since the pass was queued: its height is - /// stale, and it probes only the roots an `Uncertain` result forced — - /// catch-up is skipped, the scan settles most roots. - stale: bool, + /// It carries txids an `Uncertain` result forced. + has_forced: bool, abort: AbortHandle, probing: Option, } @@ -1079,20 +1078,25 @@ impl Actor { let entry = self.entry(wallet_id); let previous = entry.height.replace(height); if is_catch_up_step(previous, height) { - // Behind the tip: passes queued or running from before are - // stale — they keep only what an `Uncertain` result forced, - // and start no windows. - if let Some(run) = entry.run.as_mut() { - run.stale = true; - run.anchor_at = None; - } + // Behind the tip: passes queued or running from before keep + // only what an `Uncertain` result forced — the scan + // settles most roots — and start no windows. if let Some(pending) = entry.pending.as_mut() { - pending.by_height = false; pending.anchor_at = None; if pending.forced.is_empty() { entry.pending = None; } } + let Some(run) = entry.run.as_mut() else { + return; + }; + run.anchor_at = None; + // Still reading, with nothing forced: nothing it could + // probe — stop the walk over the records now. + if run.probing.is_none() && !run.has_forced { + run.abort.abort(); + self.end_run(wallet_id); + } return; } // While a pass runs, its end decides the wait: queue the height @@ -1104,7 +1108,6 @@ impl Actor { } let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(height); - pending.by_height = true; pending.anchor_at = Some(height); self.start(wallet_id); } @@ -1176,12 +1179,13 @@ impl Actor { return; }; let anchor_at = pending.anchor_at; + let has_forced = !pending.forced.is_empty(); self.next_run += 1; let run = self.next_run; let source = Arc::clone(&self.source); let abort = self.jobs.spawn(async move { let read = source - .read(wallet_id, pending.by_height, pending.forced) + .read(wallet_id, anchor_at.is_some(), pending.forced) .await; JobDone::Read { wallet_id, @@ -1196,7 +1200,7 @@ impl Actor { seq: entry.seq, woken: false, anchor_at, - stale: false, + has_forced, abort, probing: None, }); @@ -1259,19 +1263,19 @@ impl Actor { // carries 0; the wallet's own synced height is better. let height = height.max(read.synced_height); let Some(current) = entry.run.as_mut() else { - return; + return; // matched by id above }; - // The wallet is further along than the height this - // pass was queued at: a catch-up whose height step has - // not reached the resolver yet. Stale all the same. - if current - .anchor_at - .is_some_and(|at| read.synced_height > at.saturating_add(CATCH_UP_STEP)) - { - current.stale = true; + // The wallet is not where this pass was queued: far + // ahead (a catch-up whose height step has not arrived + // yet) or behind (a rewind for a rescan). Either way a + // catch-up — confine the pass to forced roots. + if current.anchor_at.is_some_and(|at| { + read.synced_height < at + || read.synced_height > at.saturating_add(CATCH_UP_STEP) + }) { current.anchor_at = None; } - let mut start = begin_pass( + let start = begin_pass( &mut self.state, wallet_id, height, @@ -1279,17 +1283,12 @@ impl Actor { &read.forced, current.anchor_at, ); - if current.stale { - start.due.retain(|root| root.forced); - } - if let Some(current) = entry.run.as_mut() { - current.probing = Some(Probing { - views: read.views, - height, - due: start.due, - in_flight: None, - }); - } + current.probing = Some(Probing { + views: read.views, + height, + due: start.due, + in_flight: None, + }); // Clears and re-published dead verdicts reach the host // before the pass goes out to the network. deliver(&self.sink, start.events); @@ -1355,7 +1354,7 @@ impl Actor { let Some(probing) = run.probing.as_mut() else { return; }; - if run.stale { + if run.anchor_at.is_none() { probing.due.retain(|root| root.forced); } if let Some(root) = probing.due.pop_front() { @@ -2415,10 +2414,21 @@ mod tests { /// Handle `command`, then every job it caused, as the task would. async fn send(&mut self, command: Command) { - self.actor.handle(command); + self.handle(command); self.settle().await; } + /// Handle `command` without running its jobs. A height step moves the + /// wallet's synced height along, as the SPV client does before it + /// reports the step. + fn handle(&mut self, command: Command) { + if let Command::Height { height, .. } = command { + let mut synced = self.source.synced.lock().expect("synced"); + *synced = (*synced).max(height); + } + self.actor.handle(command); + } + async fn settle(&mut self) { while let Some(joined) = self.actor.jobs.join_next_with_id().await { self.actor.joined(joined); @@ -2538,7 +2548,7 @@ mod tests { rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.height(wallet(), 100).await; - rig.actor.handle(Command::Height { + rig.handle(Command::Height { wallet_id: wallet(), height: 101, }); @@ -2579,7 +2589,7 @@ mod tests { rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.height(wallet(), 100).await; - rig.actor.handle(Command::Height { + rig.handle(Command::Height { wallet_id: wallet(), height: 101, }); @@ -2610,7 +2620,7 @@ mod tests { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; rig.height(wallet(), 100).await; - rig.actor.handle(Command::Height { + rig.handle(Command::Height { wallet_id: wallet(), height: 101, }); @@ -2677,7 +2687,7 @@ mod tests { let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); rig.views(wallet(), vec![send(3, &[outpoint(93, 0)])]); rig.actor.joined(listed); - rig.actor.handle(Command::Height { + rig.handle(Command::Height { wallet_id: wallet(), height: 102, }); @@ -2711,7 +2721,7 @@ mod tests { // The forced pass has read the wallet; its probe of root 2 is out. let read = rig.actor.jobs.join_next_with_id().await.expect("read"); rig.actor.joined(read); - rig.actor.handle(Command::Height { + rig.handle(Command::Height { wallet_id: wallet(), height: 103, }); @@ -2879,7 +2889,7 @@ mod tests { let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); rig.actor.joined(listed); for height in [500, 501] { - rig.actor.handle(Command::Height { + rig.handle(Command::Height { wallet_id: wallet(), height, }); @@ -2932,7 +2942,7 @@ mod tests { .await; for height in [102, 132, 133] { - rig.actor.handle(Command::Height { + rig.handle(Command::Height { wallet_id: wallet(), height, }); @@ -2973,6 +2983,72 @@ mod tests { assert_eq!(probe.probed().len(), 4, "every block from 1102"); } + /// A pass without an anchor — here a forced one during a catch-up — + /// probes only the root the `Uncertain` result forced, not every root + /// due by schedule. + #[tokio::test] + async fn should_probe_only_forced_roots_in_a_pass_without_an_anchor() { + let probe = Arc::new(ScriptedProbe::new(&[ + (txid(1), ProbeVerdict::Accepted), + (txid(2), ProbeVerdict::Accepted), + ])); + let mut rig = enabled(probe.clone()).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); + rig.follow(wallet(), 100).await; + assert_eq!(probe.probed().len(), 2); + + rig.height(wallet(), 140).await; // catch-up + rig.send(Command::Uncertain(txid(1))).await; + + assert_eq!(probe.probed()[2..], [txid(1)]); + } + + /// A pass whose read finds the wallet behind the height it was queued at — + /// a rewind for a rescan — is confined to forced roots too. + #[tokio::test] + async fn should_treat_a_pass_whose_read_is_behind_as_catch_up() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + let before = probe.probed().len(); + + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + *rig.source.synced.lock().expect("synced") = 50; // rewound + rig.settle().await; + + assert_eq!(probe.probed().len(), before); + } + + /// A catch-up step stops a pass that is still reading and carries nothing + /// forced: its walk over the records could only be thrown away. + #[tokio::test] + async fn should_stop_a_reading_pass_with_nothing_forced_on_a_catch_up() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + let walks = rig.walks(); + + rig.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + rig.handle(Command::Height { + wallet_id: wallet(), + height: 140, + }); + rig.settle().await; + + assert_eq!(rig.walks(), walks); + assert!(rig.actor.wallets[&wallet()].run.is_none()); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From cf557903875868982ba77f8e51a22dbade4ae786 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 15:07:30 +0300 Subject: [PATCH 027/169] fix(platform-wallet): twenty-first review pass on the broadcast resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - A catch-up no longer stops a reading pass while the host holds verdicts for the wallet: the pass still delivers the clears (a send that settled in the scanned blocks stops showing "unknown") — it is only confined to forced roots. - The read job shares a walk flag with its run and reads it once it has the wallet: a catch-up clears it when the pass has nothing left to do there, so a pass whose forced txids left the wallet skips the walk. - begin_pass confines an unanchored pass to forced roots itself; a probing run is confined once, when the catch-up arrives, instead of on every probe_next. - Names say what they mean now: forced_held/read take walk; the far-ahead test is a catch-up test; window_start documents that only anchored passes probe unforced roots. - The test rig keeps a synced height per wallet. - Tests (each fails without its fix): clears delivered through a catch-up; a forced pass overtaken by a catch-up runs unanchored and skips the walk. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 186 +++++++++++++++--- 1 file changed, 154 insertions(+), 32 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 5705ae93024..4c5e47339c3 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -311,7 +311,8 @@ pub(crate) struct ProbeSchedule { /// Where the every-block window starts: the first height-driven pass that /// saw the root while the wallet followed the tip. `None` until then — /// the root was only seen by a forced pass, whose height may be a stale - /// stored tip — and it is due at every pass that runs meanwhile. + /// stored tip. Only anchored passes probe unforced roots, and they set the + /// window first; `None` counting as in-window matters for `next_due`. window_start: Option, last_probe: Option, } @@ -614,7 +615,8 @@ pub(crate) fn begin_pass( schedule.anchor(anchor); } let forced = forced_roots.contains(&view.txid); - if forced || schedule.is_due(height) { + // Without an anchor the pass is confined to forced roots. + if forced || (anchor.is_some() && schedule.is_due(height)) { due.push_back(DueRoot { txid: view.txid, transaction: Arc::clone(&view.transaction), @@ -740,15 +742,16 @@ pub(crate) struct WalletRead { } /// An `Uncertain` result is queued for every wallet; only one holding the -/// transaction acts on it. `None`: nothing to do for this wallet — no height -/// asked for the pass and it holds none of `forced`. +/// transaction acts on it. `None`: nothing to do for this wallet — the pass +/// need not `walk` the records (it is confined to forced roots and has no +/// verdicts to clear) and the wallet holds none of `forced`. fn forced_held( - by_height: bool, + walk: bool, forced: HashSet, holds: impl Fn(&Txid) -> bool, ) -> Option> { let forced: HashSet = forced.into_iter().filter(|txid| holds(txid)).collect(); - (by_height || !forced.is_empty()).then_some(forced) + (walk || !forced.is_empty()).then_some(forced) } /// Where passes read wallets from. @@ -758,11 +761,12 @@ pub(crate) trait WalletSource: Send + Sync { async fn holders(&self, txid: Txid) -> Vec; /// `None`: no such wallet. `Some(None)`: nothing to do (see - /// [`forced_held`]) — decided without walking the records. + /// [`forced_held`]) — decided without walking the records. `walk` is read + /// once the wallet is at hand: a catch-up may clear it meanwhile. async fn read( &self, wallet_id: WalletId, - by_height: bool, + walk: &AtomicBool, forced: HashSet, ) -> Option>; } @@ -798,14 +802,16 @@ impl WalletSource for ManagerSource { async fn read( &self, wallet_id: WalletId, - by_height: bool, + walk: &AtomicBool, forced: HashSet, ) -> Option> { let manager = self.0.read().await; let info = manager.get_wallet_info(&wallet_id)?; // Checked again under this lock: the record may have left since the // holders lookup (a sweep, a removal). - let forced = forced_held(by_height, forced, |txid| holds(info, txid)); + let forced = forced_held(walk.load(Ordering::SeqCst), forced, |txid| { + holds(info, txid) + }); Some(forced.map(|forced| WalletRead { views: collect_views(info), synced_height: info.core_wallet.synced_height(), @@ -924,6 +930,10 @@ struct Run { anchor_at: Option, /// It carries txids an `Uncertain` result forced. has_forced: bool, + /// Whether the read walks the records even when none of the forced txids + /// is held: shared with the read job, and cleared by a catch-up when the + /// pass has nothing left to do there — no anchor, no verdicts to clear. + walk: Arc, abort: AbortHandle, probing: Option, } @@ -1087,13 +1097,29 @@ impl Actor { entry.pending = None; } } - let Some(run) = entry.run.as_mut() else { + // With verdicts out the pass runs on, for the clears. + let has_verdicts = self + .state + .published + .keys() + .any(|(wallet, _)| *wallet == wallet_id); + let Some(run) = self + .wallets + .get_mut(&wallet_id) + .and_then(|entry| entry.run.as_mut()) + else { return; }; run.anchor_at = None; - // Still reading, with nothing forced: nothing it could - // probe — stop the walk over the records now. - if run.probing.is_none() && !run.has_forced { + if !has_verdicts { + run.walk.store(false, Ordering::SeqCst); + } + if let Some(probing) = run.probing.as_mut() { + probing.due.retain(|root| root.forced); + } + // Still reading, with nothing forced and nothing to clear: + // stop its walk over the records now. + if run.probing.is_none() && !run.has_forced && !has_verdicts { run.abort.abort(); self.end_run(wallet_id); } @@ -1180,13 +1206,13 @@ impl Actor { }; let anchor_at = pending.anchor_at; let has_forced = !pending.forced.is_empty(); + let walk = Arc::new(AtomicBool::new(anchor_at.is_some())); self.next_run += 1; let run = self.next_run; let source = Arc::clone(&self.source); + let job_walk = Arc::clone(&walk); let abort = self.jobs.spawn(async move { - let read = source - .read(wallet_id, anchor_at.is_some(), pending.forced) - .await; + let read = source.read(wallet_id, &job_walk, pending.forced).await; JobDone::Read { wallet_id, run, @@ -1201,6 +1227,7 @@ impl Actor { woken: false, anchor_at, has_forced, + walk, abort, probing: None, }); @@ -1354,9 +1381,6 @@ impl Actor { let Some(probing) = run.probing.as_mut() else { return; }; - if run.anchor_at.is_none() { - probing.due.retain(|root| root.forced); - } if let Some(root) = probing.due.pop_front() { let probe = Arc::clone(&self.probe); let id = run.id; @@ -2333,8 +2357,8 @@ mod tests { struct FakeSource { views: StdMutex>>, walks: StdMutex>, - /// The synced height every read reports. - synced: StdMutex, + /// The synced height each wallet's reads report. + synced: StdMutex>, } #[async_trait] @@ -2352,18 +2376,24 @@ mod tests { async fn read( &self, wallet_id: WalletId, - by_height: bool, + walk: &AtomicBool, forced: HashSet, ) -> Option> { let views = self.views.lock().expect("views").get(&wallet_id)?.clone(); - let forced = forced_held(by_height, forced, |txid| { + let forced = forced_held(walk.load(Ordering::SeqCst), forced, |txid| { views.iter().any(|view| view.txid == *txid) }); Some(forced.map(|forced| { self.walks.lock().expect("walks").push(wallet_id); WalletRead { views, - synced_height: *self.synced.lock().expect("synced"), + synced_height: self + .synced + .lock() + .expect("synced") + .get(&wallet_id) + .copied() + .unwrap_or(0), forced, } })) @@ -2422,9 +2452,10 @@ mod tests { /// wallet's synced height along, as the SPV client does before it /// reports the step. fn handle(&mut self, command: Command) { - if let Command::Height { height, .. } = command { + if let Command::Height { wallet_id, height } = command { let mut synced = self.source.synced.lock().expect("synced"); - *synced = (*synced).max(height); + let at = synced.entry(wallet_id).or_insert(0); + *at = (*at).max(height); } self.actor.handle(command); } @@ -2960,7 +2991,7 @@ mod tests { /// queued at — a catch-up whose height step has not arrived yet — is /// stale: it neither probes by schedule nor starts a window. #[tokio::test] - async fn should_treat_a_pass_whose_read_is_far_ahead_as_stale() { + async fn should_treat_a_pass_whose_read_is_far_ahead_as_catch_up() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); let mut rig = enabled(probe.clone()).await; rig.views(wallet(), Vec::new()); @@ -2971,10 +3002,17 @@ mod tests { touched: true, }) .await; - *rig.source.synced.lock().expect("synced") = 1_101; + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 1_101); rig.height(wallet(), 102).await; - assert!(probe.probed().is_empty(), "stale: no probe during the scan"); + assert!( + probe.probed().is_empty(), + "catch-up: no probe during the scan" + ); rig.height(wallet(), 1_101).await; for height in 1_102..=1_105 { rig.height(wallet(), height).await; @@ -3020,7 +3058,11 @@ mod tests { wallet_id: wallet(), height: 102, }); - *rig.source.synced.lock().expect("synced") = 50; // rewound + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 50); // rewound rig.settle().await; assert_eq!(probe.probed().len(), before); @@ -3031,8 +3073,15 @@ mod tests { #[tokio::test] async fn should_stop_a_reading_pass_with_nothing_forced_on_a_catch_up() { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + // No verdict published: nothing for the pass to clear. + rig.views(wallet(), Vec::new()); rig.follow(wallet(), 100).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.send(Command::Seen { + wallet_id: wallet(), + touched: true, + }) + .await; let walks = rig.walks(); rig.handle(Command::Height { @@ -3049,6 +3098,79 @@ mod tests { assert!(rig.actor.wallets[&wallet()].run.is_none()); } + /// A catch-up does not stop a reading pass while the host holds verdicts + /// for the wallet: the pass still clears the send that settled. + #[tokio::test] + async fn should_let_a_pass_clear_verdicts_through_a_catch_up() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), unresolved())] + ); + rig.views(wallet(), Vec::new()); // settled + + rig.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + rig.handle(Command::Height { + wallet_id: wallet(), + height: 140, + }); + rig.settle().await; + + assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); + } + + /// A catch-up takes the anchor from a queued pass that carries a forced + /// txid; the pass still runs, confined to what was forced, and — with the + /// txid no longer held — without walking the records. + #[tokio::test] + async fn should_run_a_forced_pass_overtaken_by_a_catch_up_without_an_anchor() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + // No verdict published: nothing for the pass to clear. + rig.views(wallet(), Vec::new()); + rig.follow(wallet(), 100).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.send(Command::Seen { + wallet_id: wallet(), + touched: true, + }) + .await; + let walks = rig.walks(); + + rig.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + rig.handle(Command::Height { + wallet_id: wallet(), + height: 103, + }); + rig.actor + .wallets + .get_mut(&wallet()) + .and_then(|entry| entry.pending.as_mut()) + .expect("queued") + .forced + .insert(txid(9)); + rig.handle(Command::Height { + wallet_id: wallet(), + height: 140, + }); + + let run = rig.actor.wallets[&wallet()] + .run + .as_ref() + .expect("the forced pass"); + assert_eq!(run.anchor_at, None); + assert!(!run.walk.load(Ordering::SeqCst)); + rig.settle().await; + assert_eq!(rig.walks(), walks, "txid 9 is not held: no walk"); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From 4de07e24025debe0e21ecdcd2b7148cb0f82f4b7 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 15:17:22 +0300 Subject: [PATCH 028/169] fix(platform-wallet): twenty-second review pass on the broadcast resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One rule for clears during a catch-up: while the host holds verdicts for a wallet, a catch-up keeps (or queues) a pass that reads the records, so a send settled in the scanned blocks is cleared during the scan — whether a pass was reading, probing on older views, queued, or absent. The pass is still confined to forced roots and starts no windows. - PendingPass.clears marks such a pass; end_run does not drop it. - A run walks the records if it is anchored, queued for clears, or its wallet has verdicts out — so a forced pass on such a wallet clears too. - ResolverState keeps a per-wallet count of published sends, so the check is O(1) on every catch-up step of a long rescan. - Comments and module docs describe the confinement and the clears. - The test source can hold reads, so the overtaken-forced-pass test builds its state through Uncertain -> Listed instead of editing private fields. - Tests (each fails without its fix): clears during a catch-up with no pass queued, and through a queued pass while the running one probes old views. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 196 ++++++++++++++---- 1 file changed, 151 insertions(+), 45 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 4c5e47339c3..056ed574549 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -34,7 +34,9 @@ //! of that transaction's chain), then on each advance of the wallet's synced //! height that follows the tip — steps of more than [`CATCH_UP_STEP`] blocks, //! steps back (a rescan) and the first step after launch are catch-up and -//! skipped, along with passes queued before them — every block +//! skipped: passes queued before them probe only what an `Uncertain` result +//! forced (and still read the records, for the clears, while the host holds +//! verdicts for the wallet) — every block //! for [`EVERY_BLOCK_WINDOW`] blocks, counted from the first height-driven //! pass that sees the root while the wallet follows the tip (a forced pass //! does not start it), and every [`SLOW_INTERVAL`] @@ -419,9 +421,16 @@ pub(crate) struct ResolverState { schedules: HashMap<(WalletId, Txid), ProbeSchedule>, finished: HashMap<(WalletId, Txid), Final>, published: HashMap<(WalletId, Txid), ProbeVerdict>, + /// How many sends of each wallet have a published verdict. + published_per_wallet: HashMap, } impl ResolverState { + /// The host holds a verdict for some send of `wallet_id`. + fn has_published(&self, wallet_id: &WalletId) -> bool { + self.published_per_wallet.contains_key(wallet_id) + } + /// Drop everything kept for `wallet_id`; a clear for every send the host /// had a verdict for. fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { @@ -453,6 +462,14 @@ impl ResolverState { cleared.sort(); self.published .retain(|(wallet, txid), _| !drop(wallet, txid)); + for (wallet_id, _) in &cleared { + if let Some(count) = self.published_per_wallet.get_mut(wallet_id) { + *count -= 1; + if *count == 0 { + self.published_per_wallet.remove(wallet_id); + } + } + } cleared .into_iter() .map(|(wallet_id, txid)| Outgoing { @@ -478,8 +495,8 @@ impl ResolverState { } Some(previous) => !same_for_host(previous, verdict), }; - if changed { - self.published.insert(key, verdict.clone()); + if changed && self.published.insert(key, verdict.clone()).is_none() { + *self.published_per_wallet.entry(key.0).or_insert(0) += 1; } changed } @@ -913,6 +930,10 @@ struct PendingPass { /// results asked for it, or a catch-up overtook it — probes only the /// roots those results forced and starts no windows. anchor_at: Option, + /// Queued by a catch-up while the host holds verdicts for the wallet: it + /// runs for the clears (sends settled in the scanned blocks) even with + /// nothing to probe. + clears: bool, } /// A pass in flight: reading the wallet, then probing its due roots one at @@ -1088,41 +1109,45 @@ impl Actor { let entry = self.entry(wallet_id); let previous = entry.height.replace(height); if is_catch_up_step(previous, height) { - // Behind the tip: passes queued or running from before keep - // only what an `Uncertain` result forced — the scan - // settles most roots — and start no windows. + // Behind the tip: passes queued or running from before are + // confined to what an `Uncertain` result forced — the scan + // settles most roots — and start no windows. While the host + // holds verdicts for the wallet, a pass still reads the + // records, for the clears of sends the scan settles. + let has_verdicts = self.state.has_published(&wallet_id); + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; if let Some(pending) = entry.pending.as_mut() { pending.anchor_at = None; - if pending.forced.is_empty() { + pending.clears |= has_verdicts; + if pending.forced.is_empty() && !pending.clears { entry.pending = None; } + } else if has_verdicts && enabled { + entry.pending = Some(PendingPass { + height, + clears: true, + ..PendingPass::default() + }); } - // With verdicts out the pass runs on, for the clears. - let has_verdicts = self - .state - .published - .keys() - .any(|(wallet, _)| *wallet == wallet_id); - let Some(run) = self - .wallets - .get_mut(&wallet_id) - .and_then(|entry| entry.run.as_mut()) - else { - return; - }; - run.anchor_at = None; - if !has_verdicts { - run.walk.store(false, Ordering::SeqCst); - } - if let Some(probing) = run.probing.as_mut() { - probing.due.retain(|root| root.forced); - } - // Still reading, with nothing forced and nothing to clear: - // stop its walk over the records now. - if run.probing.is_none() && !run.has_forced && !has_verdicts { - run.abort.abort(); - self.end_run(wallet_id); + if let Some(run) = entry.run.as_mut() { + run.anchor_at = None; + if !has_verdicts { + run.walk.store(false, Ordering::SeqCst); + } + if let Some(probing) = run.probing.as_mut() { + probing.due.retain(|root| root.forced); + } + // Still reading, with nothing forced and nothing to + // clear: stop its walk over the records now. + if run.probing.is_none() && !run.has_forced && !has_verdicts { + run.abort.abort(); + self.end_run(wallet_id); + return; + } } + self.start(wallet_id); return; } // While a pass runs, its end decides the wait: queue the height @@ -1206,7 +1231,11 @@ impl Actor { }; let anchor_at = pending.anchor_at; let has_forced = !pending.forced.is_empty(); - let walk = Arc::new(AtomicBool::new(anchor_at.is_some())); + // Walk the records if the pass may probe by schedule or has verdicts + // to clear; a pass confined to forced roots with none held skips it. + let walk = Arc::new(AtomicBool::new( + anchor_at.is_some() || pending.clears || self.state.has_published(&wallet_id), + )); self.next_run += 1; let run = self.next_run; let source = Arc::clone(&self.source); @@ -1412,7 +1441,7 @@ impl Actor { }; entry.run = None; if let (Some(pending), Some(until)) = (&entry.pending, entry.wait_until) { - if pending.forced.is_empty() && pending.height < until { + if pending.forced.is_empty() && !pending.clears && pending.height < until { entry.pending = None; } } @@ -2359,6 +2388,28 @@ mod tests { walks: StdMutex>, /// The synced height each wallet's reads report. synced: StdMutex>, + /// Reads wait while set, so a test can hold a pass in its read. + paused: StdMutex, + resume: tokio::sync::Notify, + } + + impl FakeSource { + async fn wait_if_paused(&self) { + loop { + let resumed = self.resume.notified(); + if !*self.paused.lock().expect("paused") { + return; + } + resumed.await; + } + } + + fn pause(&self, paused: bool) { + *self.paused.lock().expect("paused") = paused; + if !paused { + self.resume.notify_waiters(); + } + } } #[async_trait] @@ -2379,6 +2430,7 @@ mod tests { walk: &AtomicBool, forced: HashSet, ) -> Option> { + self.wait_if_paused().await; let views = self.views.lock().expect("views").get(&wallet_id)?.clone(); let forced = forced_held(walk.load(Ordering::SeqCst), forced, |txid| { views.iter().any(|view| view.txid == *txid) @@ -2708,15 +2760,17 @@ mod tests { /// during it that no root is due at. #[tokio::test] async fn should_drop_a_queued_height_when_a_forced_read_finds_nothing() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + // Idle with no verdicts out: nothing to probe, nothing to clear. + rig.views(wallet(), Vec::new()); rig.follow(wallet(), 100).await; let walks = rig.walks(); // An `Uncertain` for a txid the wallet no longer holds by read time. + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.actor.handle(Command::Uncertain(txid(1))); let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); - rig.views(wallet(), vec![send(3, &[outpoint(93, 0)])]); + rig.views(wallet(), Vec::new()); rig.actor.joined(listed); rig.handle(Command::Height { wallet_id: wallet(), @@ -3133,7 +3187,10 @@ mod tests { // No verdict published: nothing for the pass to clear. rig.views(wallet(), Vec::new()); rig.follow(wallet(), 100).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(9, &[outpoint(99, 0)])], + ); rig.send(Command::Seen { wallet_id: wallet(), touched: true, @@ -3141,21 +3198,22 @@ mod tests { .await; let walks = rig.walks(); + // A height pass holds in its read; an `Uncertain` result and the next + // height queue behind it. + rig.source.pause(true); rig.handle(Command::Height { wallet_id: wallet(), height: 102, }); + rig.actor.handle(Command::Uncertain(txid(9))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.actor.joined(listed); rig.handle(Command::Height { wallet_id: wallet(), height: 103, }); - rig.actor - .wallets - .get_mut(&wallet()) - .and_then(|entry| entry.pending.as_mut()) - .expect("queued") - .forced - .insert(txid(9)); + // The forced txid leaves the wallet; then a catch-up. + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.handle(Command::Height { wallet_id: wallet(), height: 140, @@ -3167,10 +3225,58 @@ mod tests { .expect("the forced pass"); assert_eq!(run.anchor_at, None); assert!(!run.walk.load(Ordering::SeqCst)); + rig.source.pause(false); rig.settle().await; assert_eq!(rig.walks(), walks, "txid 9 is not held: no walk"); } + /// A catch-up on a wallet whose host holds verdicts reads the records for + /// the clears even when no pass was queued or running: a send settled in + /// the scanned blocks stops showing its verdict during the scan. + #[tokio::test] + async fn should_clear_verdicts_during_a_catch_up_with_no_pass_queued() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(1), dead())]); + rig.views(wallet(), Vec::new()); // settled in the blocks being scanned + + rig.height(wallet(), 140).await; // catch-up; idle, nothing queued + + assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); + } + + /// A queued pass overtaken by a catch-up is kept while verdicts are out, + /// and clears what the scan settled. + #[tokio::test] + async fn should_keep_a_queued_pass_for_the_clears_through_a_catch_up() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.sent(); + + rig.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + // The running pass has read the records from before the scan and is + // probing; only the queued one can see the send settle. + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); + rig.handle(Command::Height { + wallet_id: wallet(), + height: 103, + }); + rig.views(wallet(), Vec::new()); // settled in the scan + rig.handle(Command::Height { + wallet_id: wallet(), + height: 140, + }); + rig.settle().await; + + assert!(rig.sent().contains(&ResolverEvent::Cleared(txid(1)))); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From 4008d9017d1c68697eb9aa6842db2ced08e65717 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 15:28:25 +0300 Subject: [PATCH 029/169] refactor(platform-wallet): clear broadcast verdicts from wallet events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Clears no longer depend on a pass walking every record. The wallet events already name what settled or left — BlockProcessed inserted/updated records that are confirmed or InstantSend-locked, TransactionDetected likewise, TransactionInstantLocked, ChainLockProcessed, TransactionsSwept — so each becomes a Settled command that clears exactly those verdicts, at once and during a catch-up too, with no walk and no pass. A pass still clears whatever else left the wallet when it next reads it. This removes the catch-up special cases the twenty-second pass added: PendingPass.clears, the clearing pass queued on every catch-up step while verdicts were out (back-to-back walks under the manager read lock on a large wallet), the verdict-dependent walk flag, and the per-wallet published counter. Twenty-third review pass, also: - A catch-up lowers a queued forced pass to the step's height, so after a rewind its probe is not marked above the replayed heights. - Module docs describe event-driven clears and the catch-up confinement. - Tests: an event clears only its send's verdict mid-scan without a walk; settled/gone txids read from chainlock, sweep and height events; a rewind lowers the queued forced pass (fails without the fix). Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 270 +++++++++++------- 1 file changed, 160 insertions(+), 110 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 056ed574549..0f008620066 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -28,15 +28,17 @@ //! and Accepted and Mined count as the same. The host is told to drop a //! send's verdict (cleared) when the send settles or leaves the wallet, when //! its wallet is removed and, for every send, when probing is turned off; -//! cleared says only "forget the verdict", not that the send settled. +//! cleared says only "forget the verdict", not that the send settled. A +//! wallet event that reports a send settled (in a block, InstantSend- or +//! chain-locked) or swept clears its verdict at once, even mid-scan; a pass +//! clears whatever else left the wallet when it next reads it. //! //! When: once as soon as dash-spv reports an `Uncertain` broadcast (the root //! of that transaction's chain), then on each advance of the wallet's synced //! height that follows the tip — steps of more than [`CATCH_UP_STEP`] blocks, //! steps back (a rescan) and the first step after launch are catch-up and //! skipped: passes queued before them probe only what an `Uncertain` result -//! forced (and still read the records, for the clears, while the host holds -//! verdicts for the wallet) — every block +//! forced — every block //! for [`EVERY_BLOCK_WINDOW`] blocks, counted from the first height-driven //! pass that sees the root while the wallet follows the tip (a forced pass //! does not start it), and every [`SLOW_INTERVAL`] @@ -68,6 +70,7 @@ use dash_async::WorkerStatus; use dash_spv::sync::SyncEvent; use dash_spv::{BroadcastResult, EventHandler}; use dashcore::{Transaction, Txid}; +use key_wallet::managed_account::transaction_record::TransactionRecord; use key_wallet::transaction_checking::TransactionContext; use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use key_wallet_manager::{WalletEvent, WalletId, WalletManager}; @@ -421,16 +424,9 @@ pub(crate) struct ResolverState { schedules: HashMap<(WalletId, Txid), ProbeSchedule>, finished: HashMap<(WalletId, Txid), Final>, published: HashMap<(WalletId, Txid), ProbeVerdict>, - /// How many sends of each wallet have a published verdict. - published_per_wallet: HashMap, } impl ResolverState { - /// The host holds a verdict for some send of `wallet_id`. - fn has_published(&self, wallet_id: &WalletId) -> bool { - self.published_per_wallet.contains_key(wallet_id) - } - /// Drop everything kept for `wallet_id`; a clear for every send the host /// had a verdict for. fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { @@ -462,14 +458,6 @@ impl ResolverState { cleared.sort(); self.published .retain(|(wallet, txid), _| !drop(wallet, txid)); - for (wallet_id, _) in &cleared { - if let Some(count) = self.published_per_wallet.get_mut(wallet_id) { - *count -= 1; - if *count == 0 { - self.published_per_wallet.remove(wallet_id); - } - } - } cleared .into_iter() .map(|(wallet_id, txid)| Outgoing { @@ -495,8 +483,8 @@ impl ResolverState { } Some(previous) => !same_for_host(previous, verdict), }; - if changed && self.published.insert(key, verdict.clone()).is_none() { - *self.published_per_wallet.entry(key.0).or_insert(0) += 1; + if changed { + self.published.insert(key, verdict.clone()); } changed } @@ -899,6 +887,13 @@ enum Command { wallet_id: WalletId, touched: bool, }, + /// Transactions a wallet event reports settled (in a block, InstantSend- + /// or chain-locked) or gone (swept): their verdicts are cleared at once, + /// without a pass — during a catch-up too. + Settled { + wallet_id: WalletId, + txids: HashSet, + }, Uncertain(Txid), SetEnabled(bool), WalletRemoved(WalletId), @@ -930,10 +925,6 @@ struct PendingPass { /// results asked for it, or a catch-up overtook it — probes only the /// roots those results forced and starts no windows. anchor_at: Option, - /// Queued by a catch-up while the host holds verdicts for the wallet: it - /// runs for the clears (sends settled in the scanned blocks) even with - /// nothing to probe. - clears: bool, } /// A pass in flight: reading the wallet, then probing its due roots one at @@ -1111,43 +1102,30 @@ impl Actor { if is_catch_up_step(previous, height) { // Behind the tip: passes queued or running from before are // confined to what an `Uncertain` result forced — the scan - // settles most roots — and start no windows. While the host - // holds verdicts for the wallet, a pass still reads the - // records, for the clears of sends the scan settles. - let has_verdicts = self.state.has_published(&wallet_id); - let Some(entry) = self.wallets.get_mut(&wallet_id) else { - return; - }; + // settles most roots, and its events clear their verdicts + // (see `Command::Settled`) — and start no windows. if let Some(pending) = entry.pending.as_mut() { pending.anchor_at = None; - pending.clears |= has_verdicts; - if pending.forced.is_empty() && !pending.clears { + // A rewind replays lower heights: never run above them. + pending.height = height; + if pending.forced.is_empty() { entry.pending = None; } - } else if has_verdicts && enabled { - entry.pending = Some(PendingPass { - height, - clears: true, - ..PendingPass::default() - }); } - if let Some(run) = entry.run.as_mut() { - run.anchor_at = None; - if !has_verdicts { - run.walk.store(false, Ordering::SeqCst); - } - if let Some(probing) = run.probing.as_mut() { - probing.due.retain(|root| root.forced); - } - // Still reading, with nothing forced and nothing to - // clear: stop its walk over the records now. - if run.probing.is_none() && !run.has_forced && !has_verdicts { - run.abort.abort(); - self.end_run(wallet_id); - return; - } + let Some(run) = entry.run.as_mut() else { + return; + }; + run.anchor_at = None; + run.walk.store(false, Ordering::SeqCst); + if let Some(probing) = run.probing.as_mut() { + probing.due.retain(|root| root.forced); + } + // Still reading, with nothing forced: nothing it could + // probe — stop its walk over the records now. + if run.probing.is_none() && !run.has_forced { + run.abort.abort(); + self.end_run(wallet_id); } - self.start(wallet_id); return; } // While a pass runs, its end decides the wait: queue the height @@ -1162,6 +1140,15 @@ impl Actor { pending.anchor_at = Some(height); self.start(wallet_id); } + Command::Settled { wallet_id, txids } => { + self.entry(wallet_id); + let events = self.state.clear_where( + |wallet, txid| *wallet == wallet_id && txids.contains(txid), + None, + "settled-or-left", + ); + deliver(&self.sink, events); + } Command::Seen { wallet_id, touched } => { let entry = self.entry(wallet_id); if touched { @@ -1231,11 +1218,9 @@ impl Actor { }; let anchor_at = pending.anchor_at; let has_forced = !pending.forced.is_empty(); - // Walk the records if the pass may probe by schedule or has verdicts - // to clear; a pass confined to forced roots with none held skips it. - let walk = Arc::new(AtomicBool::new( - anchor_at.is_some() || pending.clears || self.state.has_published(&wallet_id), - )); + // Walk the records if the pass may probe by schedule; one confined to + // forced roots with none of them held has nothing to read for. + let walk = Arc::new(AtomicBool::new(anchor_at.is_some())); self.next_run += 1; let run = self.next_run; let source = Arc::clone(&self.source); @@ -1441,7 +1426,7 @@ impl Actor { }; entry.run = None; if let (Some(pending), Some(until)) = (&entry.pending, entry.wait_until) { - if pending.forced.is_empty() && !pending.clears && pending.height < until { + if pending.forced.is_empty() && pending.height < until { entry.pending = None; } } @@ -1619,9 +1604,46 @@ fn may_leave_work(event: &WalletEvent) -> bool { } } +/// The transactions a wallet event reports settled — in a block, +/// InstantSend- or chain-locked — or gone from the wallet (swept). +fn settled_or_gone(event: &WalletEvent) -> HashSet { + match event { + WalletEvent::TransactionDetected { record, .. } => is_settled(record) + .then_some(record.txid) + .into_iter() + .collect(), + WalletEvent::TransactionInstantLocked { txid, .. } => HashSet::from([*txid]), + WalletEvent::TransactionsSwept { txids, .. } => txids.iter().copied().collect(), + WalletEvent::BlockProcessed { + inserted, updated, .. + } => inserted + .iter() + .chain(updated) + .filter(|record| is_settled(record)) + .map(|record| record.txid) + .collect(), + WalletEvent::ChainLockProcessed { + locked_transactions, + .. + } => locked_transactions.values().flatten().copied().collect(), + WalletEvent::SyncHeightAdvanced { .. } => HashSet::new(), + } +} + +fn is_settled(record: &TransactionRecord) -> bool { + record.is_confirmed() || matches!(record.context, TransactionContext::InstantSend(_)) +} + impl EventHandler for BroadcastResolver { fn on_wallet_event(&self, event: &WalletEvent) { let wallet_id = event.wallet_id(); + let settled = settled_or_gone(event); + if !settled.is_empty() { + self.send(Command::Settled { + wallet_id, + txids: settled, + }); + } let command = match event { WalletEvent::SyncHeightAdvanced { height, .. } => Command::Height { wallet_id, @@ -3152,32 +3174,6 @@ mod tests { assert!(rig.actor.wallets[&wallet()].run.is_none()); } - /// A catch-up does not stop a reading pass while the host holds verdicts - /// for the wallet: the pass still clears the send that settled. - #[tokio::test] - async fn should_let_a_pass_clear_verdicts_through_a_catch_up() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())]))).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.follow(wallet(), 100).await; - assert_eq!( - rig.sent(), - vec![ResolverEvent::Verdict(txid(1), unresolved())] - ); - rig.views(wallet(), Vec::new()); // settled - - rig.handle(Command::Height { - wallet_id: wallet(), - height: 102, - }); - rig.handle(Command::Height { - wallet_id: wallet(), - height: 140, - }); - rig.settle().await; - - assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); - } - /// A catch-up takes the anchor from a queued pass that carries a forced /// txid; the pass still runs, confined to what was forced, and — with the /// txid no longer held — without walking the records. @@ -3230,51 +3226,105 @@ mod tests { assert_eq!(rig.walks(), walks, "txid 9 is not held: no walk"); } - /// A catch-up on a wallet whose host holds verdicts reads the records for - /// the clears even when no pass was queued or running: a send settled in - /// the scanned blocks stops showing its verdict during the scan. + /// A wallet event that settles a send clears its verdict at once — during + /// a catch-up too — without a pass or a walk over the records, and only + /// that send's. #[tokio::test] - async fn should_clear_verdicts_during_a_catch_up_with_no_pass_queued() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + async fn should_clear_a_verdict_as_soon_as_an_event_settles_its_send() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[ + (txid(1), dead()), + (txid(2), unresolved()), + ]))) + .await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); rig.follow(wallet(), 100).await; - assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(1), dead())]); - rig.views(wallet(), Vec::new()); // settled in the blocks being scanned + rig.sent(); + rig.height(wallet(), 140).await; // catch-up + let walks = rig.walks(); - rig.height(wallet(), 140).await; // catch-up; idle, nothing queued + rig.send(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(1)]), + }) + .await; assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); + assert_eq!(rig.walks(), walks); + assert!(rig.actor.state.published.contains_key(&(wallet(), txid(2)))); + } + + /// Which transactions a wallet event reports settled or gone. + #[test] + fn should_read_settled_and_gone_txids_from_wallet_events() { + let lock = WalletEvent::ChainLockProcessed { + wallet_id: wallet(), + chain_lock: chain_lock(), + locked_transactions: BTreeMap::from([( + AccountType::CoinJoin { index: 0 }, + vec![txid(1)], + )]), + }; + let swept = WalletEvent::TransactionsSwept { + wallet_id: wallet(), + txids: vec![txid(2)], + superseded_by: txid(3), + winner_mined_height: None, + released_outpoints: Vec::new(), + balance: WalletCoreBalance::default(), + account_balances: BTreeMap::new(), + }; + let height = WalletEvent::SyncHeightAdvanced { + wallet_id: wallet(), + height: 100, + }; + + assert_eq!(settled_or_gone(&lock), HashSet::from([txid(1)])); + assert_eq!(settled_or_gone(&swept), HashSet::from([txid(2)])); + assert!(settled_or_gone(&height).is_empty()); } - /// A queued pass overtaken by a catch-up is kept while verdicts are out, - /// and clears what the scan settled. + /// A step back (a rewind for a rescan) lowers a queued forced pass to the + /// replayed height: a probe marked above it would keep the root from + /// being due until the rescan passed that height again. #[tokio::test] - async fn should_keep_a_queued_pass_for_the_clears_through_a_catch_up() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())]))).await; + async fn should_lower_a_queued_pass_to_the_height_of_a_rewind() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.follow(wallet(), 100).await; - rig.sent(); - + rig.source.pause(true); rig.handle(Command::Height { wallet_id: wallet(), height: 102, }); - // The running pass has read the records from before the scan and is - // probing; only the queued one can see the send settle. - let read = rig.actor.jobs.join_next_with_id().await.expect("read"); - rig.actor.joined(read); + rig.actor.handle(Command::Uncertain(txid(1))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.actor.joined(listed); rig.handle(Command::Height { wallet_id: wallet(), height: 103, }); - rig.views(wallet(), Vec::new()); // settled in the scan - rig.handle(Command::Height { + + // The rewind: synced height goes back, then the step is reported. + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 50); + rig.actor.handle(Command::Height { wallet_id: wallet(), - height: 140, + height: 50, }); + rig.source.pause(false); rig.settle().await; - assert!(rig.sent().contains(&ResolverEvent::Cleared(txid(1)))); + // The forced probe went out at the replayed height, not at 103. + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].last_probe, + Some(50) + ); } /// A probe that panics on its `nth` call and accepts otherwise. From 7f91f5ccf44c46adb0ee757e39c268c845a0ca9a Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 15:38:08 +0300 Subject: [PATCH 030/169] fix(platform-wallet): twenty-fourth review pass on the broadcast resolver - A pass under way when an event settles or removes a send no longer probes it or publishes a verdict for it afterwards: the run records the settled txids, drops them from its views and due roots, filters a read that lands later, and ignores a probe answer that was already out. Before, the late answer re-published the verdict the event had just cleared, and during a catch-up nothing cleared it again. - A step back reaches a running pass too: a reading pass runs at the rewind height, a probing one marks its remaining probes there. - Settled commands are sent only while probing is on (nothing is published otherwise). - collect_views and the event path share is_settled; Run.walk and forced_held docs no longer mention verdict-driven walks. - Tests (each of the first two fails without its fix): no re-publish after a settle during an in-flight probe; a reading pass runs at the rewind height; block and new-transaction events name only settled records. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 178 ++++++++++++++++-- 1 file changed, 167 insertions(+), 11 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 0f008620066..61870a9ee3f 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -168,9 +168,7 @@ pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { .values() .map(move |record| RecordFacts { txid: record.txid, - settled: record.is_confirmed() - || matches!(record.context, TransactionContext::InstantSend(_)) - || account.transaction_is_finalized(&record.txid), + settled: is_settled(record) || account.transaction_is_finalized(&record.txid), own: !record.input_details.is_empty(), transaction: &record.transaction, }) @@ -748,8 +746,8 @@ pub(crate) struct WalletRead { /// An `Uncertain` result is queued for every wallet; only one holding the /// transaction acts on it. `None`: nothing to do for this wallet — the pass -/// need not `walk` the records (it is confined to forced roots and has no -/// verdicts to clear) and the wallet holds none of `forced`. +/// need not `walk` the records (it is confined to forced roots) and the +/// wallet holds none of `forced`. fn forced_held( walk: bool, forced: HashSet, @@ -943,9 +941,15 @@ struct Run { /// It carries txids an `Uncertain` result forced. has_forced: bool, /// Whether the read walks the records even when none of the forced txids - /// is held: shared with the read job, and cleared by a catch-up when the - /// pass has nothing left to do there — no anchor, no verdicts to clear. + /// is held (`anchor_at.is_some()`): shared with the read job, cleared by a + /// catch-up. walk: Arc, + /// Txids a wallet event settled or removed during the run: the run's + /// views are older than that, so it neither probes nor publishes them. + settled: HashSet, + /// A step back (a rewind for a rescan) arrived during the run: it runs at + /// this height, not at the one it was queued at. + rewound_to: Option, abort: AbortHandle, probing: Option, } @@ -1117,8 +1121,15 @@ impl Actor { }; run.anchor_at = None; run.walk.store(false, Ordering::SeqCst); + let rewind = previous.is_some_and(|previous| height < previous); + if rewind { + run.rewound_to = Some(height); + } if let Some(probing) = run.probing.as_mut() { probing.due.retain(|root| root.forced); + if rewind { + probing.height = probing.height.min(height); + } } // Still reading, with nothing forced: nothing it could // probe — stop its walk over the records now. @@ -1141,7 +1152,16 @@ impl Actor { self.start(wallet_id); } Command::Settled { wallet_id, txids } => { - self.entry(wallet_id); + let entry = self.entry(wallet_id); + // A pass under way read the wallet before this: it must not + // probe these again or publish a verdict for them afterwards. + if let Some(run) = entry.run.as_mut() { + run.settled.extend(txids.iter().copied()); + if let Some(probing) = run.probing.as_mut() { + probing.due.retain(|root| !txids.contains(&root.txid)); + probing.views.retain(|view| !txids.contains(&view.txid)); + } + } let events = self.state.clear_where( |wallet, txid| *wallet == wallet_id && txids.contains(txid), None, @@ -1242,6 +1262,8 @@ impl Actor { anchor_at, has_forced, walk, + settled: HashSet::new(), + rewound_to: None, abort, probing: None, }); @@ -1302,10 +1324,16 @@ impl Actor { Some(Some(read)) => { // A request queued before any height was reported // carries 0; the wallet's own synced height is better. - let height = height.max(read.synced_height); let Some(current) = entry.run.as_mut() else { return; // matched by id above }; + let height = current + .rewound_to + .unwrap_or_else(|| height.max(read.synced_height)); + // What settled since the read is not the pass's any more. + let mut read = read; + read.views + .retain(|view| !current.settled.contains(&view.txid)); // The wallet is not where this pass was queued: far // ahead (a catch-up whose height step has not arrived // yet) or behind (a rewind for a rescan). Either way a @@ -1370,6 +1398,15 @@ impl Actor { self.end_run(wallet_id); return; }; + if current.settled.contains(&root.txid) { + // Settled while its probe was out: the event already + // cleared it; the answer is moot. + self.probe_next(wallet_id); + return; + } + let Some(probing) = current.probing.as_ref() else { + return; + }; let events = record_probe( &mut self.state, wallet_id, @@ -1637,7 +1674,13 @@ fn is_settled(record: &TransactionRecord) -> bool { impl EventHandler for BroadcastResolver { fn on_wallet_event(&self, event: &WalletEvent) { let wallet_id = event.wallet_id(); - let settled = settled_or_gone(event); + // While probing is off nothing is published, so there is nothing to + // clear (turning it off cleared everything). + let settled = if self.is_enabled() { + settled_or_gone(event) + } else { + HashSet::new() + }; if !settled.is_empty() { self.send(Command::Settled { wallet_id, @@ -1681,7 +1724,11 @@ mod tests { use dashcore::hashes::Hash; use dashcore::{BlockHash, OutPoint, TxIn}; use key_wallet::account::AccountType; - use key_wallet::managed_account::transaction_record::TransactionRecord; + use key_wallet::managed_account::transaction_record::{ + TransactionDirection, TransactionRecord, + }; + use key_wallet::transaction_checking::transaction_router::TransactionType; + use key_wallet::transaction_checking::BlockInfo; use key_wallet::WalletCoreBalance; use super::*; @@ -3327,6 +3374,115 @@ mod tests { ); } + /// A send settled while its probe was out: the event clears it, and the + /// probe's late answer publishes nothing for it again. + #[tokio::test] + async fn should_not_republish_a_send_settled_while_its_probe_was_out() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.sent(); + + rig.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // its probe is out now + rig.handle(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(1)]), + }); + rig.settle().await; + + assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); + } + + /// A step back while a forced pass is reading runs that pass at the + /// replayed height, not at the one it was queued at. + #[tokio::test] + async fn should_run_a_reading_pass_at_the_height_of_a_rewind() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.source.pause(true); + rig.actor.handle(Command::Uncertain(txid(1))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.actor.joined(listed); // the forced pass is reading, queued at 101 + + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 50); + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 50, + }); + rig.source.pause(false); + rig.settle().await; + + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].last_probe, + Some(50) + ); + } + + fn record(n: u8, context: TransactionContext) -> TransactionRecord { + TransactionRecord::new( + tx(n, &[outpoint(90, 0)]), + AccountType::CoinJoin { index: 0 }, + context, + TransactionType::Standard, + TransactionDirection::Outgoing, + Vec::new(), + Vec::new(), + 0, + ) + } + + fn in_block() -> TransactionContext { + TransactionContext::InBlock(BlockInfo::new(100, BlockHash::all_zeros(), 0)) + } + + /// Block and new-transaction events name only the records that are + /// settled; a mempool record is not. + #[test] + fn should_read_settled_records_from_block_and_transaction_events() { + let block = WalletEvent::BlockProcessed { + wallet_id: wallet(), + height: 100, + chain_lock: None, + inserted: vec![ + record(1, in_block()), + record(2, TransactionContext::Mempool), + ], + updated: vec![record(3, in_block())], + matured: Vec::new(), + balance: WalletCoreBalance::default(), + account_balances: BTreeMap::new(), + addresses_derived: Vec::new(), + }; + let detected = |context| WalletEvent::TransactionDetected { + wallet_id: wallet(), + record: Box::new(record(4, context)), + balance: WalletCoreBalance::default(), + account_balances: BTreeMap::new(), + addresses_derived: Vec::new(), + }; + let txid_of = |n: u8| tx(n, &[outpoint(90, 0)]).txid(); + + assert_eq!( + settled_or_gone(&block), + HashSet::from([txid_of(1), txid_of(3)]) + ); + assert_eq!( + settled_or_gone(&detected(in_block())), + HashSet::from([txid_of(4)]) + ); + assert!(settled_or_gone(&detected(TransactionContext::Mempool)).is_empty()); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From 7c1a285bf0d71e4c5935aa99bd98bc241827890b Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 15:49:57 +0300 Subject: [PATCH 031/169] fix(platform-wallet): twenty-fifth review pass on the broadcast resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Settled txids become tombstones in ResolverState: publish refuses them, record_probe ignores a late answer for them, and begin_pass leaves them out of a read older than the event; a tombstone ends once a read no longer lists the txid. One choke point replaces the run-scoped set and its four filter sites. - A send published Dead because its root was dead remembers that root (dead_from). When the root is no longer known dead — settled by an event, gone from a pass's read, or forgotten — the inherited Dead is withdrawn, so a later Unresolved is not blocked by a Dead that no longer holds. - A step back restarts the wallet's schedules (due at once, window from the next following pass) and a root's last probe never moves back; the sticky rewind height on a running pass is gone. - ChainLockProcessed no longer produces Settled: it only promotes records whose block already reported them. - set_enabled stores the flag and queues the switch under one lock, so the flag event handlers gate on cannot disagree with the actor. - Module doc sentence on the cadence is readable again. - Tests: inherited Dead withdrawn on a settle event and on a pass that finds the root gone (both fail without the fix); a rewind restarts schedules; a chain lock settles nothing new. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 353 +++++++++++++----- 1 file changed, 262 insertions(+), 91 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 61870a9ee3f..52973f58322 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -37,12 +37,11 @@ //! of that transaction's chain), then on each advance of the wallet's synced //! height that follows the tip — steps of more than [`CATCH_UP_STEP`] blocks, //! steps back (a rescan) and the first step after launch are catch-up and -//! skipped: passes queued before them probe only what an `Uncertain` result -//! forced — every block -//! for [`EVERY_BLOCK_WINDOW`] blocks, counted from the first height-driven -//! pass that sees the root while the wallet follows the tip (a forced pass -//! does not start it), and every [`SLOW_INTERVAL`] -//! blocks after. Each Uncertain report adds one immediate probe. +//! skipped (passes queued before them probe only what an `Uncertain` result +//! forced) — a root is probed every block for [`EVERY_BLOCK_WINDOW`] blocks, +//! counted from the first height-driven pass that sees it while the wallet +//! follows the tip (a forced pass does not start it), and every +//! [`SLOW_INTERVAL`] blocks after. Each Uncertain report adds one immediate probe. //! Between passes a wallet waits for the height at which its next root is due; //! one whose last pass had nothing left to probe is idle and not scanned //! again until a wallet event that touched its records (a new or @@ -340,8 +339,10 @@ impl ProbeSchedule { } } + /// Never moves back: a probe marked at a stale height (a pass that ran + /// across a catch-up) must not make the root due again below its last. fn probed_at(&mut self, height: u32) { - self.last_probe = Some(height); + self.last_probe = Some(self.last_probe.map_or(height, |last| last.max(height))); } /// The last probe produced nothing: due again at once. @@ -422,6 +423,14 @@ pub(crate) struct ResolverState { schedules: HashMap<(WalletId, Txid), ProbeSchedule>, finished: HashMap<(WalletId, Txid), Final>, published: HashMap<(WalletId, Txid), ProbeVerdict>, + /// Transactions a wallet event reported settled or gone. Nothing is + /// published for them — whatever a pass that read the wallet earlier + /// still holds — until a fresh read no longer lists them. + settled: HashSet<(WalletId, Txid)>, + /// Own sends published Dead because a dead root they are built on is: + /// the root each inherited it from. Their Dead is withdrawn once that + /// root is no longer known dead (it settled, left, or was forgotten). + dead_from: HashMap<(WalletId, Txid), Txid>, } impl ResolverState { @@ -430,6 +439,7 @@ impl ResolverState { fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { self.schedules.retain(|(wallet, _), _| wallet != wallet_id); self.finished.retain(|(wallet, _), _| wallet != wallet_id); + self.settled.retain(|(wallet, _)| wallet != wallet_id); self.clear_where(|wallet, _| wallet == wallet_id, None, "removed") } @@ -437,9 +447,91 @@ impl ResolverState { fn forget_all(&mut self) -> Vec { self.schedules.clear(); self.finished.clear(); + self.settled.clear(); self.clear_where(|_, _| true, None, "disabled") } + /// A wallet event reported `txids` settled or gone: forget them and clear + /// their verdicts — and the Dead any send inherited from one of them. + fn settle(&mut self, wallet_id: WalletId, txids: &HashSet) -> Vec { + for txid in txids { + let key = (wallet_id, *txid); + self.settled.insert(key); + self.schedules.remove(&key); + self.finished.remove(&key); + } + let mut events = self.clear_where( + |wallet, txid| *wallet == wallet_id && txids.contains(txid), + None, + "settled-or-left", + ); + events.extend(self.clear_orphaned_dead(wallet_id, None, "settled-or-left")); + events + } + + /// Withdraw every Dead a send of `wallet_id` inherited from a root that is + /// no longer known dead. + fn clear_orphaned_dead( + &mut self, + wallet_id: WalletId, + height: Option, + trigger: &'static str, + ) -> Vec { + let orphans: HashSet = self + .dead_from + .iter() + .filter(|((wallet, _), root)| { + *wallet == wallet_id + && !matches!( + self.finished.get(&(wallet_id, **root)), + Some(Final::Dead { .. }) + ) + }) + .map(|((_, send), _)| *send) + .collect(); + if orphans.is_empty() { + return Vec::new(); + } + self.dead_from + .retain(|(wallet, send), _| *wallet != wallet_id || !orphans.contains(send)); + let published = &self.published; + let dead: HashSet = orphans + .into_iter() + .filter(|send| { + matches!( + published.get(&(wallet_id, *send)), + Some(ProbeVerdict::Dead { .. }) + ) + }) + .collect(); + self.clear_where( + |wallet, txid| *wallet == wallet_id && dead.contains(txid), + height, + trigger, + ) + } + + /// Publish a root's Dead to the own sends in its chain, remembering which + /// root each send other than the root itself inherited it from. + #[allow(clippy::too_many_arguments)] + fn publish_dead( + &mut self, + wallet_id: WalletId, + root: Txid, + sends: BTreeSet, + verdict: &ProbeVerdict, + height: u32, + trigger: &'static str, + out: &mut Vec, + ) { + for send in &sends { + if *send != root && !self.settled.contains(&(wallet_id, *send)) { + self.dead_from.insert((wallet_id, *send), root); + } + } + self.publish_all(wallet_id, sends, verdict, height, trigger, out); + } + /// Unpublish every send `drop` matches; a clear for each, in a stable order. fn clear_where( &mut self, @@ -456,6 +548,8 @@ impl ResolverState { cleared.sort(); self.published .retain(|(wallet, txid), _| !drop(wallet, txid)); + self.dead_from + .retain(|(wallet, txid), _| !drop(wallet, txid)); cleared .into_iter() .map(|(wallet_id, txid)| Outgoing { @@ -471,6 +565,9 @@ impl ResolverState { /// Unresolved never replaces a decided verdict (Accepted, Mined, Dead) — /// one probe that only met transport errors says nothing new. fn publish(&mut self, key: (WalletId, Txid), verdict: &ProbeVerdict) -> bool { + if self.settled.contains(&key) { + return false; + } let changed = match self.published.get(&key) { None => true, Some(previous) @@ -509,6 +606,16 @@ impl ResolverState { } } + /// A rewind replays the chain: every root of `wallet_id` starts its + /// schedule over (due at once, window from the next following pass). + fn restart_schedules(&mut self, wallet_id: &WalletId) { + for ((wallet, _), schedule) in self.schedules.iter_mut() { + if wallet == wallet_id { + *schedule = ProbeSchedule::default(); + } + } + } + /// Roots of `wallet_id` a probe found in a block the wallet has not seen. fn mined(&self, wallet_id: &WalletId) -> HashSet { self.finished @@ -568,6 +675,18 @@ pub(crate) fn begin_pass( anchor: Option, ) -> PassStart { let mut events = Vec::new(); + // A tombstone lasts until a read no longer lists its txid; while it does + // (a read older than the event), the txid is left out of the pass. + let listed: HashSet = views.iter().map(|view| view.txid).collect(); + state + .settled + .retain(|(wallet, txid)| *wallet != wallet_id || listed.contains(txid)); + let views: Vec = views + .iter() + .filter(|view| !state.settled.contains(&(wallet_id, view.txid))) + .cloned() + .collect(); + let views = views.as_slice(); // What the wallet itself still holds unsettled — what state is kept for. let present: HashSet = views.iter().map(|view| view.txid).collect(); let own_present: HashSet = views @@ -589,6 +708,7 @@ pub(crate) fn begin_pass( Some(height), "settled-or-left", )); + events.extend(state.clear_orphaned_dead(wallet_id, Some(height), "settled-or-left")); let mined = state.mined(&wallet_id); let graph = ChainGraph::new(views, &mined); @@ -603,7 +723,15 @@ pub(crate) fn begin_pass( let verdict = ProbeVerdict::Dead { reason }; let sends = graph.own_in_chain(&view.txid); let trigger = trigger_of(forced_roots.contains(&view.txid)); - state.publish_all(wallet_id, sends, &verdict, height, trigger, &mut events); + state.publish_dead( + wallet_id, + view.txid, + sends, + &verdict, + height, + trigger, + &mut events, + ); } } @@ -662,6 +790,10 @@ pub(crate) fn record_probe( ) -> Vec { let mut events = Vec::new(); let key = (wallet_id, root.txid); + // Settled or gone while its probe was out: the answer is moot. + if state.settled.contains(&key) { + return events; + } match verdict { ProbeVerdict::Dead { reason } => { state.finished.insert( @@ -676,21 +808,28 @@ pub(crate) fn record_probe( } ProbeVerdict::Accepted | ProbeVerdict::Unresolved { .. } => {} } - let sends: BTreeSet = if matches!(verdict, ProbeVerdict::Dead { .. }) { - ChainGraph::new(views, &state.mined(&wallet_id)).own_in_chain(&root.txid) + let trigger = trigger_of(root.forced); + if matches!(verdict, ProbeVerdict::Dead { .. }) { + let sends = ChainGraph::new(views, &state.mined(&wallet_id)).own_in_chain(&root.txid); + state.publish_dead( + wallet_id, + root.txid, + sends, + verdict, + height, + trigger, + &mut events, + ); } else if root.own { - BTreeSet::from([root.txid]) - } else { - BTreeSet::new() - }; - state.publish_all( - wallet_id, - sends, - verdict, - height, - trigger_of(root.forced), - &mut events, - ); + state.publish_all( + wallet_id, + [root.txid], + verdict, + height, + trigger, + &mut events, + ); + } events } @@ -944,12 +1083,6 @@ struct Run { /// is held (`anchor_at.is_some()`): shared with the read job, cleared by a /// catch-up. walk: Arc, - /// Txids a wallet event settled or removed during the run: the run's - /// views are older than that, so it neither probes nor publishes them. - settled: HashSet, - /// A step back (a rewind for a rescan) arrived during the run: it runs at - /// this height, not at the one it was queued at. - rewound_to: Option, abort: AbortHandle, probing: Option, } @@ -1103,6 +1236,12 @@ impl Actor { let enabled = self.enabled; let entry = self.entry(wallet_id); let previous = entry.height.replace(height); + if previous.is_some_and(|previous| height < previous) { + self.state.restart_schedules(&wallet_id); + } + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; if is_catch_up_step(previous, height) { // Behind the tip: passes queued or running from before are // confined to what an `Uncertain` result forced — the scan @@ -1121,15 +1260,8 @@ impl Actor { }; run.anchor_at = None; run.walk.store(false, Ordering::SeqCst); - let rewind = previous.is_some_and(|previous| height < previous); - if rewind { - run.rewound_to = Some(height); - } if let Some(probing) = run.probing.as_mut() { probing.due.retain(|root| root.forced); - if rewind { - probing.height = probing.height.min(height); - } } // Still reading, with nothing forced: nothing it could // probe — stop its walk over the records now. @@ -1153,20 +1285,13 @@ impl Actor { } Command::Settled { wallet_id, txids } => { let entry = self.entry(wallet_id); - // A pass under way read the wallet before this: it must not - // probe these again or publish a verdict for them afterwards. - if let Some(run) = entry.run.as_mut() { - run.settled.extend(txids.iter().copied()); - if let Some(probing) = run.probing.as_mut() { - probing.due.retain(|root| !txids.contains(&root.txid)); - probing.views.retain(|view| !txids.contains(&view.txid)); - } + // A pass under way read the wallet before this: it does not + // probe these any more (and publishes nothing for them — the + // tombstones `settle` leaves see to that). + if let Some(probing) = entry.run.as_mut().and_then(|run| run.probing.as_mut()) { + probing.due.retain(|root| !txids.contains(&root.txid)); } - let events = self.state.clear_where( - |wallet, txid| *wallet == wallet_id && txids.contains(txid), - None, - "settled-or-left", - ); + let events = self.state.settle(wallet_id, &txids); deliver(&self.sink, events); } Command::Seen { wallet_id, touched } => { @@ -1262,8 +1387,6 @@ impl Actor { anchor_at, has_forced, walk, - settled: HashSet::new(), - rewound_to: None, abort, probing: None, }); @@ -1327,13 +1450,9 @@ impl Actor { let Some(current) = entry.run.as_mut() else { return; // matched by id above }; - let height = current - .rewound_to - .unwrap_or_else(|| height.max(read.synced_height)); - // What settled since the read is not the pass's any more. - let mut read = read; - read.views - .retain(|view| !current.settled.contains(&view.txid)); + // A request queued before any height was reported + // carries 0; the wallet's own synced height is better. + let height = height.max(read.synced_height); // The wallet is not where this pass was queued: far // ahead (a catch-up whose height step has not arrived // yet) or behind (a rewind for a rescan). Either way a @@ -1398,15 +1517,6 @@ impl Actor { self.end_run(wallet_id); return; }; - if current.settled.contains(&root.txid) { - // Settled while its probe was out: the event already - // cleared it; the answer is moot. - self.probe_next(wallet_id); - return; - } - let Some(probing) = current.probing.as_ref() else { - return; - }; let events = record_probe( &mut self.state, wallet_id, @@ -1492,6 +1602,10 @@ enum ActorSlot { /// product decision, not something a wallet should start doing on upgrade. pub(crate) struct BroadcastResolver { enabled: AtomicBool, + /// Held while the flag is stored and the switch is queued, so the flag + /// and the actor end in the same state under concurrent calls — event + /// handlers gate on the flag. + switching: Mutex<()>, commands: mpsc::UnboundedSender, /// The actor has been started (or the resolver closed): `send` no longer /// needs the slot. @@ -1520,6 +1634,7 @@ impl BroadcastResolver { started: AtomicBool::new(false), actor: Mutex::new(ActorSlot::NotStarted(Box::new(actor), receiver)), stopping: AsyncMutex::new(()), + switching: Mutex::new(()), sink, } } @@ -1557,6 +1672,10 @@ impl BroadcastResolver { /// way every wallet is woken, so turning it back on re-publishes what was /// cleared. Returns at once; the clears follow from the resolver's task. pub(crate) fn set_enabled(&self, enabled: bool) { + let _switching = self + .switching + .lock() + .unwrap_or_else(PoisonError::into_inner); self.enabled.store(enabled, Ordering::SeqCst); self.send(Command::SetEnabled(enabled)); } @@ -1659,11 +1778,11 @@ fn settled_or_gone(event: &WalletEvent) -> HashSet { .filter(|record| is_settled(record)) .map(|record| record.txid) .collect(), - WalletEvent::ChainLockProcessed { - locked_transactions, - .. - } => locked_transactions.values().flatten().copied().collect(), - WalletEvent::SyncHeightAdvanced { .. } => HashSet::new(), + // A chain lock only promotes records already in a block — reported + // settled when their block was processed. + WalletEvent::ChainLockProcessed { .. } | WalletEvent::SyncHeightAdvanced { .. } => { + HashSet::new() + } } } @@ -3328,7 +3447,10 @@ mod tests { height: 100, }; - assert_eq!(settled_or_gone(&lock), HashSet::from([txid(1)])); + assert!( + settled_or_gone(&lock).is_empty(), + "a chain lock settles nothing new: its block did" + ); assert_eq!(settled_or_gone(&swept), HashSet::from([txid(2)])); assert!(settled_or_gone(&height).is_empty()); } @@ -3398,36 +3520,85 @@ mod tests { assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); } - /// A step back while a forced pass is reading runs that pass at the - /// replayed height, not at the one it was queued at. + /// A step back (a rewind for a rescan) starts every root's schedule over: + /// due at once, its window from the next following pass. #[tokio::test] - async fn should_run_a_reading_pass_at_the_height_of_a_rewind() { + async fn should_restart_schedules_on_a_rewind() { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.follow(wallet(), 100).await; - rig.source.pause(true); - rig.actor.handle(Command::Uncertain(txid(1))); - let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); - rig.actor.joined(listed); // the forced pass is reading, queued at 101 + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].last_probe, + Some(101) + ); - rig.source - .synced - .lock() - .expect("synced") - .insert(wallet(), 50); - rig.actor.handle(Command::Height { + rig.height(wallet(), 50).await; + + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))], + ProbeSchedule::default() + ); + } + + /// A child published Dead because its root was dead loses that Dead when + /// the root settles — it is not left showing the worst verdict for a send + /// whose parent is confirmed. + #[tokio::test] + async fn should_withdraw_an_inherited_dead_when_the_dead_root_settles() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])], + ); + rig.follow(wallet(), 100).await; + assert_eq!( + rig.sent(), + vec![ + ResolverEvent::Verdict(txid(1), dead()), + ResolverEvent::Verdict(txid(2), dead()), + ] + ); + + rig.send(Command::Settled { wallet_id: wallet(), - height: 50, - }); - rig.source.pause(false); - rig.settle().await; + txids: HashSet::from([txid(1)]), + }) + .await; assert_eq!( - rig.actor.state.schedules[&(wallet(), txid(1))].last_probe, - Some(50) + rig.sent(), + vec![ + ResolverEvent::Cleared(txid(1)), + ResolverEvent::Cleared(txid(2)), + ] ); } + /// The Dead a child inherited goes when the root leaves the wallet in a + /// pass's read too, not only on an event. + #[tokio::test] + async fn should_withdraw_an_inherited_dead_when_a_pass_finds_the_root_gone() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])], + ); + rig.follow(wallet(), 100).await; + rig.sent(); + + rig.views(wallet(), vec![send(2, &[outpoint(1, 1)])]); // root mined + rig.send(Command::Seen { + wallet_id: wallet(), + touched: true, + }) + .await; + rig.height(wallet(), 102).await; + + let sent = rig.sent(); + assert!(sent.contains(&ResolverEvent::Cleared(txid(1)))); + assert!(sent.contains(&ResolverEvent::Cleared(txid(2)))); + } + fn record(n: u8, context: TransactionContext) -> TransactionRecord { TransactionRecord::new( tx(n, &[outpoint(90, 0)]), From dcb2d7b65973c7f31cdad639432cc7ecb9ef97ad Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 16:00:44 +0300 Subject: [PATCH 032/169] fix(platform-wallet): twenty-sixth review pass on the broadcast resolver - Tombstones carry the settle clock, and a read that started after one drops it: a send a reorg brings back unsettled is probed again instead of being filtered out for as long as it stays unsettled. Reads are admitted once in the actor (ResolverState::admit) before begin_pass and before the views are kept for the run, so the run's later graphs see the same views. - Only txids the resolver tracks, or any while a read is in flight, are tombstoned: a catch-up no longer piles up the wallet's whole history. - An inherited Dead remembers every dead root it came from and is withdrawn only when none is still dead. - A rewind bumps the wallet's restart count; a pass that started before it marks no probes into the restarted schedules, and the wallet's wait is reset along with them. - A chain lock no longer wakes a wallet (it promotes only records its block reported); docs no longer say it settles anything; a duplicated comment is gone. - Tests (each of the stale-mark and two-roots tests fails without its fix): reorg re-probes a settled send, untracked settles leave no tombstone, no probe marked from before a rewind, Dead kept while another root is dead. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 280 ++++++++++++++---- 1 file changed, 215 insertions(+), 65 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 52973f58322..1d10be2b56b 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -29,8 +29,8 @@ //! send's verdict (cleared) when the send settles or leaves the wallet, when //! its wallet is removed and, for every send, when probing is turned off; //! cleared says only "forget the verdict", not that the send settled. A -//! wallet event that reports a send settled (in a block, InstantSend- or -//! chain-locked) or swept clears its verdict at once, even mid-scan; a pass +//! wallet event that reports a send settled (in a block or InstantSend- +//! locked) or swept clears its verdict at once, even mid-scan; a pass //! clears whatever else left the wallet when it next reads it. //! //! When: once as soon as dash-spv reports an `Uncertain` broadcast (the root @@ -423,14 +423,21 @@ pub(crate) struct ResolverState { schedules: HashMap<(WalletId, Txid), ProbeSchedule>, finished: HashMap<(WalletId, Txid), Final>, published: HashMap<(WalletId, Txid), ProbeVerdict>, - /// Transactions a wallet event reported settled or gone. Nothing is - /// published for them — whatever a pass that read the wallet earlier - /// still holds — until a fresh read no longer lists them. - settled: HashSet<(WalletId, Txid)>, - /// Own sends published Dead because a dead root they are built on is: - /// the root each inherited it from. Their Dead is withdrawn once that - /// root is no longer known dead (it settled, left, or was forgotten). - dead_from: HashMap<(WalletId, Txid), Txid>, + /// Transactions a wallet event reported settled or gone, with the + /// `clock` at which it did. A read that started before that is older than + /// the event: it leaves them out and nothing is published for them. A read + /// that started after it drops the tombstone — whatever it lists now (a + /// reorg can bring a mined send back) is current. + settled: HashMap<(WalletId, Txid), u64>, + /// Advanced by every settle; runs note it when their read starts. + clock: u64, + /// Own sends published Dead because dead roots they are built on are: + /// those roots. Their Dead is withdrawn once none of them is known dead + /// any more (settled, left, or forgotten). + dead_from: HashMap<(WalletId, Txid), BTreeSet>, + /// Bumped per wallet by a rewind that restarts its schedules: a pass that + /// started before does not mark its probes into the new ones. + restarts: HashMap, } impl ResolverState { @@ -439,7 +446,7 @@ impl ResolverState { fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { self.schedules.retain(|(wallet, _), _| wallet != wallet_id); self.finished.retain(|(wallet, _), _| wallet != wallet_id); - self.settled.retain(|(wallet, _)| wallet != wallet_id); + self.settled.retain(|(wallet, _), _| wallet != wallet_id); self.clear_where(|wallet, _| wallet == wallet_id, None, "removed") } @@ -452,13 +459,25 @@ impl ResolverState { } /// A wallet event reported `txids` settled or gone: forget them and clear - /// their verdicts — and the Dead any send inherited from one of them. - fn settle(&mut self, wallet_id: WalletId, txids: &HashSet) -> Vec { + /// their verdicts — and the Dead any send inherited from one of them. A + /// txid is tombstoned if the resolver tracks it, or if `read_in_flight` + /// (that read may list any of them); others need nothing. + fn settle( + &mut self, + wallet_id: WalletId, + txids: &HashSet, + read_in_flight: bool, + ) -> Vec { + self.clock += 1; for txid in txids { let key = (wallet_id, *txid); - self.settled.insert(key); - self.schedules.remove(&key); - self.finished.remove(&key); + let tracked = self.schedules.remove(&key).is_some() + | self.finished.remove(&key).is_some() + | self.published.contains_key(&key) + | self.dead_from.contains_key(&key); + if tracked || read_in_flight { + self.settled.insert(key, self.clock); + } } let mut events = self.clear_where( |wallet, txid| *wallet == wallet_id && txids.contains(txid), @@ -477,23 +496,24 @@ impl ResolverState { height: Option, trigger: &'static str, ) -> Vec { - let orphans: HashSet = self - .dead_from - .iter() - .filter(|((wallet, _), root)| { - *wallet == wallet_id - && !matches!( - self.finished.get(&(wallet_id, **root)), - Some(Final::Dead { .. }) - ) - }) - .map(|((_, send), _)| *send) - .collect(); + let finished = &self.finished; + let mut orphans: HashSet = HashSet::new(); + self.dead_from.retain(|(wallet, send), roots| { + if *wallet != wallet_id { + return true; + } + roots.retain(|root| { + matches!(finished.get(&(wallet_id, *root)), Some(Final::Dead { .. })) + }); + if roots.is_empty() { + orphans.insert(*send); + return false; + } + true + }); if orphans.is_empty() { return Vec::new(); } - self.dead_from - .retain(|(wallet, send), _| *wallet != wallet_id || !orphans.contains(send)); let published = &self.published; let dead: HashSet = orphans .into_iter() @@ -525,8 +545,11 @@ impl ResolverState { out: &mut Vec, ) { for send in &sends { - if *send != root && !self.settled.contains(&(wallet_id, *send)) { - self.dead_from.insert((wallet_id, *send), root); + if *send != root && !self.settled.contains_key(&(wallet_id, *send)) { + self.dead_from + .entry((wallet_id, *send)) + .or_default() + .insert(root); } } self.publish_all(wallet_id, sends, verdict, height, trigger, out); @@ -565,7 +588,7 @@ impl ResolverState { /// Unresolved never replaces a decided verdict (Accepted, Mined, Dead) — /// one probe that only met transport errors says nothing new. fn publish(&mut self, key: (WalletId, Txid), verdict: &ProbeVerdict) -> bool { - if self.settled.contains(&key) { + if self.settled.contains_key(&key) { return false; } let changed = match self.published.get(&key) { @@ -614,6 +637,28 @@ impl ResolverState { *schedule = ProbeSchedule::default(); } } + *self.restarts.entry(*wallet_id).or_insert(0) += 1; + } + + fn restarts(&self, wallet_id: &WalletId) -> u64 { + self.restarts.get(wallet_id).copied().unwrap_or(0) + } + + /// Admit a read that started at `since` (the clock then): drop the + /// wallet's tombstones it is newer than, and leave out what the rest + /// still cover. + fn admit( + &mut self, + wallet_id: WalletId, + since: u64, + views: Vec, + ) -> Vec { + self.settled + .retain(|(wallet, _), at| *wallet != wallet_id || *at > since); + views + .into_iter() + .filter(|view| !self.settled.contains_key(&(wallet_id, view.txid))) + .collect() } /// Roots of `wallet_id` a probe found in a block the wallet has not seen. @@ -675,18 +720,6 @@ pub(crate) fn begin_pass( anchor: Option, ) -> PassStart { let mut events = Vec::new(); - // A tombstone lasts until a read no longer lists its txid; while it does - // (a read older than the event), the txid is left out of the pass. - let listed: HashSet = views.iter().map(|view| view.txid).collect(); - state - .settled - .retain(|(wallet, txid)| *wallet != wallet_id || listed.contains(txid)); - let views: Vec = views - .iter() - .filter(|view| !state.settled.contains(&(wallet_id, view.txid))) - .cloned() - .collect(); - let views = views.as_slice(); // What the wallet itself still holds unsettled — what state is kept for. let present: HashSet = views.iter().map(|view| view.txid).collect(); let own_present: HashSet = views @@ -791,7 +824,7 @@ pub(crate) fn record_probe( let mut events = Vec::new(); let key = (wallet_id, root.txid); // Settled or gone while its probe was out: the answer is moot. - if state.settled.contains(&key) { + if state.settled.contains_key(&key) { return events; } match verdict { @@ -1024,8 +1057,8 @@ enum Command { wallet_id: WalletId, touched: bool, }, - /// Transactions a wallet event reports settled (in a block, InstantSend- - /// or chain-locked) or gone (swept): their verdicts are cleared at once, + /// Transactions a wallet event reports settled (in a block or + /// InstantSend-locked) or gone (swept): their verdicts are cleared at once, /// without a pass — during a catch-up too. Settled { wallet_id: WalletId, @@ -1069,6 +1102,11 @@ struct PendingPass { /// (probing off, wallet removed) is ignored. struct Run { id: u64, + /// The settle clock when its read started (see [`ResolverState::admit`]). + since: u64, + /// The wallet's schedule restarts when it started: after a rewind it + /// marks no probes into the restarted schedules. + restarts: u64, /// The wallet's command count when the run started. seq: u64, /// Something touched the wallet during the run: its read may be stale, so @@ -1242,6 +1280,10 @@ impl Actor { let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; + if previous.is_some_and(|previous| height < previous) { + // The wait was worked out from the schedules just restarted. + entry.wait_until = None; + } if is_catch_up_step(previous, height) { // Behind the tip: passes queued or running from before are // confined to what an `Uncertain` result forced — the scan @@ -1291,7 +1333,8 @@ impl Actor { if let Some(probing) = entry.run.as_mut().and_then(|run| run.probing.as_mut()) { probing.due.retain(|root| !txids.contains(&root.txid)); } - let events = self.state.settle(wallet_id, &txids); + let read_in_flight = entry.run.as_ref().is_some_and(|run| run.probing.is_none()); + let events = self.state.settle(wallet_id, &txids, read_in_flight); deliver(&self.sink, events); } Command::Seen { wallet_id, touched } => { @@ -1382,6 +1425,8 @@ impl Actor { self.job_runs.insert(abort.id(), (wallet_id, run)); entry.run = Some(Run { id: run, + since: self.state.clock, + restarts: self.state.restarts(&wallet_id), seq: entry.seq, woken: false, anchor_at, @@ -1445,8 +1490,6 @@ impl Actor { } Some(None) => self.end_run(wallet_id), Some(Some(read)) => { - // A request queued before any height was reported - // carries 0; the wallet's own synced height is better. let Some(current) = entry.run.as_mut() else { return; // matched by id above }; @@ -1463,16 +1506,17 @@ impl Actor { }) { current.anchor_at = None; } + let views = self.state.admit(wallet_id, current.since, read.views); let start = begin_pass( &mut self.state, wallet_id, height, - &read.views, + &views, &read.forced, current.anchor_at, ); current.probing = Some(Probing { - views: read.views, + views, height, due: start.due, in_flight: None, @@ -1555,7 +1599,9 @@ impl Actor { } }); self.job_runs.insert(run.abort.id(), (wallet_id, id)); - mark_sent(&mut self.state, wallet_id, root.txid, probing.height); + if run.restarts == self.state.restarts(&wallet_id) { + mark_sent(&mut self.state, wallet_id, root.txid, probing.height); + } probing.in_flight = Some(root); return; } @@ -1741,9 +1787,9 @@ async fn stop_slot( } } -/// Whether a wallet event may have left something new to probe. A chainlock -/// that locked nothing, or a block that only matured coins or moved balances, -/// does not — chainlocks arrive about once per block for every wallet. +/// Whether a wallet event may have left something new to probe. A chain lock +/// (it only promotes records their block already reported), or a block that +/// only matured coins or moved balances, does not. fn may_leave_work(event: &WalletEvent) -> bool { match event { WalletEvent::TransactionDetected { .. } @@ -1752,16 +1798,14 @@ fn may_leave_work(event: &WalletEvent) -> bool { WalletEvent::BlockProcessed { inserted, updated, .. } => !inserted.is_empty() || !updated.is_empty(), - WalletEvent::ChainLockProcessed { - locked_transactions, - .. - } => locked_transactions.values().any(|txids| !txids.is_empty()), - WalletEvent::SyncHeightAdvanced { .. } => false, + // A chain lock only promotes records their block already reported. + WalletEvent::ChainLockProcessed { .. } | WalletEvent::SyncHeightAdvanced { .. } => false, } } -/// The transactions a wallet event reports settled — in a block, -/// InstantSend- or chain-locked — or gone from the wallet (swept). +/// The transactions a wallet event reports settled — in a block or +/// InstantSend-locked — or gone from the wallet (swept). A chain lock adds +/// nothing: it only promotes records their block already reported. fn settled_or_gone(event: &WalletEvent) -> HashSet { match event { WalletEvent::TransactionDetected { record, .. } => is_settled(record) @@ -3538,6 +3582,7 @@ mod tests { rig.actor.state.schedules[&(wallet(), txid(1))], ProbeSchedule::default() ); + assert_eq!(rig.actor.wallets[&wallet()].wait_until, None); } /// A child published Dead because its root was dead loses that Dead when @@ -3654,6 +3699,108 @@ mod tests { assert!(settled_or_gone(&detected(TransactionContext::Mempool)).is_empty()); } + /// A reorg brings a settled send back unsettled: a read that started after + /// the settle event drops the tombstone, and the send is probed again. + #[tokio::test] + async fn should_probe_a_send_again_after_a_reorg_unsettles_it() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.send(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(1)]), + }) + .await; + rig.sent(); + + // Reorged back to the mempool: the next read lists it again. + rig.send(Command::Seen { + wallet_id: wallet(), + touched: true, + }) + .await; + rig.height(wallet(), 102).await; + + assert_eq!(probe.probed(), vec![txid(1), txid(1)]); + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), unresolved())] + ); + } + + /// A settle for something the resolver never tracked, with no read in + /// flight, leaves no tombstone behind. + #[tokio::test] + async fn should_not_tombstone_untracked_txids_with_no_read_in_flight() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + rig.views(wallet(), Vec::new()); + rig.follow(wallet(), 100).await; + + rig.send(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(77)]), + }) + .await; + + assert!(rig.actor.state.settled.is_empty()); + } + + /// A pass that started before a rewind does not mark its probe into the + /// restarted schedules. + #[tokio::test] + async fn should_not_mark_a_probe_from_before_a_rewind() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.source.pause(true); + rig.actor.handle(Command::Uncertain(txid(1))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.actor.joined(listed); // forced pass reading, queued at 101 + + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 50, + }); + rig.source.pause(false); + rig.settle().await; + + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].last_probe, + None, + "the restarted schedule is untouched by the older pass" + ); + } + + /// A send built on two dead roots keeps its Dead while either is still + /// dead. + #[tokio::test] + async fn should_keep_an_inherited_dead_while_another_dead_root_remains() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[ + (txid(1), dead()), + (txid(2), dead()), + ]))) + .await; + rig.views( + wallet(), + vec![ + incoming(1, &[outpoint(80, 0)]), + incoming(2, &[outpoint(81, 0)]), + send(3, &[outpoint(1, 0), outpoint(2, 0)]), + ], + ); + rig.follow(wallet(), 100).await; + assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(3), dead())]); + + rig.send(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(2)]), + }) + .await; + + assert!(rig.sent().is_empty(), "root 1 is still dead"); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, @@ -3851,7 +3998,10 @@ mod tests { assert!(!may_leave_work(&block(Vec::new()))); assert!(!may_leave_work(&lock(Vec::new()))); - assert!(may_leave_work(&lock(vec![txid(1)]))); + assert!( + !may_leave_work(&lock(vec![txid(1)])), + "a chain lock promotes what its block reported" + ); assert!(!may_leave_work(&WalletEvent::SyncHeightAdvanced { wallet_id: wallet(), height: 100, From 7dbf83b30cf5fe6a811916f431fe26effb1eca35 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 16:11:44 +0300 Subject: [PATCH 033/169] fix(platform-wallet): twenty-seventh review pass on the broadcast resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tombstones move back onto the run, filled whenever a run exists: a run older than a settle event neither probes nor publishes what it settled, whether the event lands while it reads or while it probes (the probing case let a late Dead spread to a send settled mid-probe). The set ends with the run, so a later read is current — a send a reorg brings back included — without the settle clock, per-read admission or tracked-txid scan of the state-level version. - A rewind turns off probe marks on the run under way instead of bumping a per-wallet counter that was never pruned; the rewind check is computed once and resets the wait and the schedules together. - Docs no longer list chain locks among the events that wake a wallet. - Test: no Dead spread to a send settled while the root's probe was out (fails without the fix). Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 172 ++++++++---------- 1 file changed, 76 insertions(+), 96 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 1d10be2b56b..52e1ffc84f7 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -45,8 +45,7 @@ //! Between passes a wallet waits for the height at which its next root is due; //! one whose last pass had nothing left to probe is idle and not scanned //! again until a wallet event that touched its records (a new or -//! InstantSend-locked transaction, a block or chainlock that changed records, a -//! sweep) or an `Uncertain` result for one of its transactions wakes it. `Dead` and `Mined` end +//! InstantSend-locked transaction, a block that changed records, a sweep) or an `Uncertain` result for one of its transactions wakes it. `Dead` and `Mined` end //! the probing of a root; `Accepted` does not — a mempool is not settlement. //! Probes run only while the SPV client delivers blocks, i.e. while the app is //! in the foreground. @@ -423,21 +422,10 @@ pub(crate) struct ResolverState { schedules: HashMap<(WalletId, Txid), ProbeSchedule>, finished: HashMap<(WalletId, Txid), Final>, published: HashMap<(WalletId, Txid), ProbeVerdict>, - /// Transactions a wallet event reported settled or gone, with the - /// `clock` at which it did. A read that started before that is older than - /// the event: it leaves them out and nothing is published for them. A read - /// that started after it drops the tombstone — whatever it lists now (a - /// reorg can bring a mined send back) is current. - settled: HashMap<(WalletId, Txid), u64>, - /// Advanced by every settle; runs note it when their read starts. - clock: u64, /// Own sends published Dead because dead roots they are built on are: /// those roots. Their Dead is withdrawn once none of them is known dead /// any more (settled, left, or forgotten). dead_from: HashMap<(WalletId, Txid), BTreeSet>, - /// Bumped per wallet by a rewind that restarts its schedules: a pass that - /// started before does not mark its probes into the new ones. - restarts: HashMap, } impl ResolverState { @@ -446,7 +434,6 @@ impl ResolverState { fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { self.schedules.retain(|(wallet, _), _| wallet != wallet_id); self.finished.retain(|(wallet, _), _| wallet != wallet_id); - self.settled.retain(|(wallet, _), _| wallet != wallet_id); self.clear_where(|wallet, _| wallet == wallet_id, None, "removed") } @@ -454,30 +441,16 @@ impl ResolverState { fn forget_all(&mut self) -> Vec { self.schedules.clear(); self.finished.clear(); - self.settled.clear(); self.clear_where(|_, _| true, None, "disabled") } /// A wallet event reported `txids` settled or gone: forget them and clear - /// their verdicts — and the Dead any send inherited from one of them. A - /// txid is tombstoned if the resolver tracks it, or if `read_in_flight` - /// (that read may list any of them); others need nothing. - fn settle( - &mut self, - wallet_id: WalletId, - txids: &HashSet, - read_in_flight: bool, - ) -> Vec { - self.clock += 1; + /// their verdicts — and the Dead any send inherited from one of them. + fn settle(&mut self, wallet_id: WalletId, txids: &HashSet) -> Vec { for txid in txids { let key = (wallet_id, *txid); - let tracked = self.schedules.remove(&key).is_some() - | self.finished.remove(&key).is_some() - | self.published.contains_key(&key) - | self.dead_from.contains_key(&key); - if tracked || read_in_flight { - self.settled.insert(key, self.clock); - } + self.schedules.remove(&key); + self.finished.remove(&key); } let mut events = self.clear_where( |wallet, txid| *wallet == wallet_id && txids.contains(txid), @@ -545,7 +518,7 @@ impl ResolverState { out: &mut Vec, ) { for send in &sends { - if *send != root && !self.settled.contains_key(&(wallet_id, *send)) { + if *send != root { self.dead_from .entry((wallet_id, *send)) .or_default() @@ -588,9 +561,6 @@ impl ResolverState { /// Unresolved never replaces a decided verdict (Accepted, Mined, Dead) — /// one probe that only met transport errors says nothing new. fn publish(&mut self, key: (WalletId, Txid), verdict: &ProbeVerdict) -> bool { - if self.settled.contains_key(&key) { - return false; - } let changed = match self.published.get(&key) { None => true, Some(previous) @@ -637,28 +607,6 @@ impl ResolverState { *schedule = ProbeSchedule::default(); } } - *self.restarts.entry(*wallet_id).or_insert(0) += 1; - } - - fn restarts(&self, wallet_id: &WalletId) -> u64 { - self.restarts.get(wallet_id).copied().unwrap_or(0) - } - - /// Admit a read that started at `since` (the clock then): drop the - /// wallet's tombstones it is newer than, and leave out what the rest - /// still cover. - fn admit( - &mut self, - wallet_id: WalletId, - since: u64, - views: Vec, - ) -> Vec { - self.settled - .retain(|(wallet, _), at| *wallet != wallet_id || *at > since); - views - .into_iter() - .filter(|view| !self.settled.contains_key(&(wallet_id, view.txid))) - .collect() } /// Roots of `wallet_id` a probe found in a block the wallet has not seen. @@ -823,10 +771,6 @@ pub(crate) fn record_probe( ) -> Vec { let mut events = Vec::new(); let key = (wallet_id, root.txid); - // Settled or gone while its probe was out: the answer is moot. - if state.settled.contains_key(&key) { - return events; - } match verdict { ProbeVerdict::Dead { reason } => { state.finished.insert( @@ -1102,11 +1046,14 @@ struct PendingPass { /// (probing off, wallet removed) is ignored. struct Run { id: u64, - /// The settle clock when its read started (see [`ResolverState::admit`]). - since: u64, - /// The wallet's schedule restarts when it started: after a rewind it - /// marks no probes into the restarted schedules. - restarts: u64, + /// Txids a wallet event settled or removed while the run was under way: + /// its read is older than that, so it neither probes them nor publishes + /// anything for them. Gone with the run — a later read is current, a + /// send a reorg brings back included. + settled: HashSet, + /// Cleared by a rewind: the wallet's schedules were restarted, and this + /// run, older than that, marks no probes into them. + marks: bool, /// The wallet's command count when the run started. seq: u64, /// Something touched the wallet during the run: its read may be stale, so @@ -1274,16 +1221,18 @@ impl Actor { let enabled = self.enabled; let entry = self.entry(wallet_id); let previous = entry.height.replace(height); - if previous.is_some_and(|previous| height < previous) { + let rewound = previous.is_some_and(|previous| height < previous); + if rewound { + // The wait was worked out from the schedules restarted below. + entry.wait_until = None; + if let Some(run) = entry.run.as_mut() { + run.marks = false; + } self.state.restart_schedules(&wallet_id); } let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; - if previous.is_some_and(|previous| height < previous) { - // The wait was worked out from the schedules just restarted. - entry.wait_until = None; - } if is_catch_up_step(previous, height) { // Behind the tip: passes queued or running from before are // confined to what an `Uncertain` result forced — the scan @@ -1327,14 +1276,16 @@ impl Actor { } Command::Settled { wallet_id, txids } => { let entry = self.entry(wallet_id); - // A pass under way read the wallet before this: it does not - // probe these any more (and publishes nothing for them — the - // tombstones `settle` leaves see to that). - if let Some(probing) = entry.run.as_mut().and_then(|run| run.probing.as_mut()) { - probing.due.retain(|root| !txids.contains(&root.txid)); + // A pass under way read the wallet before this: it neither + // probes these any more nor publishes anything for them. + if let Some(run) = entry.run.as_mut() { + run.settled.extend(txids.iter().copied()); + if let Some(probing) = run.probing.as_mut() { + probing.due.retain(|root| !txids.contains(&root.txid)); + probing.views.retain(|view| !txids.contains(&view.txid)); + } } - let read_in_flight = entry.run.as_ref().is_some_and(|run| run.probing.is_none()); - let events = self.state.settle(wallet_id, &txids, read_in_flight); + let events = self.state.settle(wallet_id, &txids); deliver(&self.sink, events); } Command::Seen { wallet_id, touched } => { @@ -1425,8 +1376,8 @@ impl Actor { self.job_runs.insert(abort.id(), (wallet_id, run)); entry.run = Some(Run { id: run, - since: self.state.clock, - restarts: self.state.restarts(&wallet_id), + settled: HashSet::new(), + marks: true, seq: entry.seq, woken: false, anchor_at, @@ -1506,7 +1457,13 @@ impl Actor { }) { current.anchor_at = None; } - let views = self.state.admit(wallet_id, current.since, read.views); + // What an event settled since the read began is not + // the pass's any more. + let views: Vec = read + .views + .into_iter() + .filter(|view| !current.settled.contains(&view.txid)) + .collect(); let start = begin_pass( &mut self.state, wallet_id, @@ -1561,6 +1518,15 @@ impl Actor { self.end_run(wallet_id); return; }; + if current.settled.contains(&root.txid) { + // Settled or gone while its probe was out: moot. + self.probe_next(wallet_id); + return; + } + let Some(probing) = current.probing.as_ref() else { + self.end_run(wallet_id); + return; + }; let events = record_probe( &mut self.state, wallet_id, @@ -1599,7 +1565,7 @@ impl Actor { } }); self.job_runs.insert(run.abort.id(), (wallet_id, id)); - if run.restarts == self.state.restarts(&wallet_id) { + if run.marks { mark_sent(&mut self.state, wallet_id, root.txid, probing.height); } probing.in_flight = Some(root); @@ -3729,21 +3695,34 @@ mod tests { ); } - /// A settle for something the resolver never tracked, with no read in - /// flight, leaves no tombstone behind. + /// A send settled while a pass is already probing — here the child of a + /// root whose probe is out — gets no Dead from that root's late answer. #[tokio::test] - async fn should_not_tombstone_untracked_txids_with_no_read_in_flight() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; - rig.views(wallet(), Vec::new()); - rig.follow(wallet(), 100).await; + async fn should_not_spread_a_dead_to_a_send_settled_during_probing() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views( + wallet(), + vec![incoming(1, &[outpoint(80, 0)]), send(2, &[outpoint(1, 0)])], + ); + rig.height(wallet(), 100).await; + rig.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // root 1's probe is out - rig.send(Command::Settled { + rig.handle(Command::Settled { wallet_id: wallet(), - txids: HashSet::from([txid(77)]), - }) - .await; + txids: HashSet::from([txid(2)]), + }); + rig.settle().await; - assert!(rig.actor.state.settled.is_empty()); + assert!( + !rig.sent() + .contains(&ResolverEvent::Verdict(txid(2), dead())), + "send 2 settled before the answer came" + ); } /// A pass that started before a rewind does not mark its probe into the @@ -3975,8 +3954,9 @@ mod tests { } } - /// A chainlock arrives about once per block for every wallet; only one that - /// locked something, or a block that changed records, may leave work. + /// Only a block that changed records (or a new, locked or swept + /// transaction) may leave work; a chain lock — about one per block for + /// every wallet — only promotes what its block reported. #[test] fn should_wake_only_on_events_that_touched_records() { let block = |inserted: Vec| WalletEvent::BlockProcessed { From 2d0333c99f64d57f9bb604a09ac225536cafa7ec Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 16:21:43 +0300 Subject: [PATCH 034/169] fix(platform-wallet): twenty-eighth review pass on the broadcast resolver - One rule for what an event settles during a run: while the run reads, the txids go to its settled set and are left out of the read; once it probes, they are pruned from its views and due roots, and a root whose probe is out is moot once it is no longer in the views. The set no longer grows for the rest of a long probing run, and the second probing lookup (dead code) is gone. - Test: a send settled during the read is neither probed nor published (fails with the read filter removed). - The reorg test's doc no longer describes the removed tombstones; the module header wraps again. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 65 ++++++++++++++----- 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 52e1ffc84f7..2edef260801 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -45,8 +45,9 @@ //! Between passes a wallet waits for the height at which its next root is due; //! one whose last pass had nothing left to probe is idle and not scanned //! again until a wallet event that touched its records (a new or -//! InstantSend-locked transaction, a block that changed records, a sweep) or an `Uncertain` result for one of its transactions wakes it. `Dead` and `Mined` end -//! the probing of a root; `Accepted` does not — a mempool is not settlement. +//! InstantSend-locked transaction, a block that changed records, a sweep) or +//! an `Uncertain` result for one of its transactions wakes it. `Dead` and +//! `Mined` end the probing of a root; `Accepted` does not — a mempool is not settlement. //! Probes run only while the SPV client delivers blocks, i.e. while the app is //! in the foreground. //! @@ -1046,10 +1047,11 @@ struct PendingPass { /// (probing off, wallet removed) is ignored. struct Run { id: u64, - /// Txids a wallet event settled or removed while the run was under way: - /// its read is older than that, so it neither probes them nor publishes - /// anything for them. Gone with the run — a later read is current, a - /// send a reorg brings back included. + /// Txids a wallet event settled or removed while the run was reading: the + /// read may still list them, and they are left out of it. Once the run + /// probes, a settle prunes its views and due roots instead (a root whose + /// probe is out is moot once gone from the views). Gone with the run — a + /// later read is current, a send a reorg brings back included. settled: HashSet, /// Cleared by a rewind: the wallet's schedules were restarted, and this /// run, older than that, marks no probes into them. @@ -1279,10 +1281,12 @@ impl Actor { // A pass under way read the wallet before this: it neither // probes these any more nor publishes anything for them. if let Some(run) = entry.run.as_mut() { - run.settled.extend(txids.iter().copied()); - if let Some(probing) = run.probing.as_mut() { - probing.due.retain(|root| !txids.contains(&root.txid)); - probing.views.retain(|view| !txids.contains(&view.txid)); + match run.probing.as_mut() { + None => run.settled.extend(txids.iter().copied()), + Some(probing) => { + probing.due.retain(|root| !txids.contains(&root.txid)); + probing.views.retain(|view| !txids.contains(&view.txid)); + } } } let events = self.state.settle(wallet_id, &txids); @@ -1518,15 +1522,11 @@ impl Actor { self.end_run(wallet_id); return; }; - if current.settled.contains(&root.txid) { + if !probing.views.iter().any(|view| view.txid == root.txid) { // Settled or gone while its probe was out: moot. self.probe_next(wallet_id); return; } - let Some(probing) = current.probing.as_ref() else { - self.end_run(wallet_id); - return; - }; let events = record_probe( &mut self.state, wallet_id, @@ -3665,8 +3665,8 @@ mod tests { assert!(settled_or_gone(&detected(TransactionContext::Mempool)).is_empty()); } - /// A reorg brings a settled send back unsettled: a read that started after - /// the settle event drops the tombstone, and the send is probed again. + /// A reorg brings a settled send back unsettled: a run that starts after + /// the settle event reads it as current, and the send is probed again. #[tokio::test] async fn should_probe_a_send_again_after_a_reorg_unsettles_it() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); @@ -3780,6 +3780,37 @@ mod tests { assert!(rig.sent().is_empty(), "root 1 is still dead"); } + /// A send settled while the pass is still reading is left out of the read + /// that may still list it: not probed, nothing published. + #[tokio::test] + async fn should_leave_a_send_settled_during_the_read_out_of_the_pass() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(2), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), Vec::new()); + rig.follow(wallet(), 100).await; + rig.views(wallet(), vec![send(2, &[outpoint(91, 0)])]); + rig.send(Command::Seen { + wallet_id: wallet(), + touched: true, + }) + .await; + + rig.source.pause(true); + rig.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + rig.handle(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(2)]), + }); + rig.source.pause(false); + rig.settle().await; + + assert!(probe.probed().is_empty()); + assert!(rig.sent().is_empty()); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From 026c83f7e071bf5673859cedd1cbe8ef7e98c898 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 16:30:34 +0300 Subject: [PATCH 035/169] fix(platform-wallet): twenty-ninth review pass on the broadcast resolver - The root whose probe is out settles: the probe job is stopped and the pass goes on with the next root, instead of spending up to four submissions and four lookups on a moot answer. - A settle that changes a probing pass's views requeues the pass's forced txids still held, so a forced send whose unconfirmed parent was the root is probed at once rather than at the next block. - Settled and Seen only touch wallets the actor already knows: an event queued behind a removal no longer brings back an entry nothing drops. - Tests (each fails without its fix): a queued root settled while probing is not sent; the in-flight probe of a settled root is stopped; the forced send is probed at once when its parent root settles. - Module header rewrapped. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 158 ++++++++++++++++-- 1 file changed, 141 insertions(+), 17 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 2edef260801..4278f625ae8 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -41,13 +41,15 @@ //! forced) — a root is probed every block for [`EVERY_BLOCK_WINDOW`] blocks, //! counted from the first height-driven pass that sees it while the wallet //! follows the tip (a forced pass does not start it), and every -//! [`SLOW_INTERVAL`] blocks after. Each Uncertain report adds one immediate probe. +//! [`SLOW_INTERVAL`] blocks after. Each Uncertain report adds one immediate +//! probe. //! Between passes a wallet waits for the height at which its next root is due; //! one whose last pass had nothing left to probe is idle and not scanned //! again until a wallet event that touched its records (a new or //! InstantSend-locked transaction, a block that changed records, a sweep) or //! an `Uncertain` result for one of its transactions wakes it. `Dead` and -//! `Mined` end the probing of a root; `Accepted` does not — a mempool is not settlement. +//! `Mined` end the probing of a root; `Accepted` does not — a mempool is not +//! settlement. //! Probes run only while the SPV client delivers blocks, i.e. while the app is //! in the foreground. //! @@ -1064,8 +1066,8 @@ struct Run { /// Where this pass starts windows; `None` confines it to forced roots /// (see [`PendingPass::anchor_at`]). anchor_at: Option, - /// It carries txids an `Uncertain` result forced. - has_forced: bool, + /// The txids `Uncertain` results forced it for. + forced: HashSet, /// Whether the read walks the records even when none of the forced txids /// is held (`anchor_at.is_some()`): shared with the read job, cleared by a /// catch-up. @@ -1258,7 +1260,7 @@ impl Actor { } // Still reading, with nothing forced: nothing it could // probe — stop its walk over the records now. - if run.probing.is_none() && !run.has_forced { + if run.probing.is_none() && run.forced.is_empty() { run.abort.abort(); self.end_run(wallet_id); } @@ -1277,23 +1279,62 @@ impl Actor { self.start(wallet_id); } Command::Settled { wallet_id, txids } => { - let entry = self.entry(wallet_id); - // A pass under way read the wallet before this: it neither - // probes these any more nor publishes anything for them. - if let Some(run) = entry.run.as_mut() { - match run.probing.as_mut() { - None => run.settled.extend(txids.iter().copied()), - Some(probing) => { - probing.due.retain(|root| !txids.contains(&root.txid)); - probing.views.retain(|view| !txids.contains(&view.txid)); + let mut next_probe = false; + // Only a wallet already known: an event queued behind its + // removal must not bring back an entry nothing would drop. + if let Some(entry) = self.wallets.get_mut(&wallet_id) { + entry.seq += 1; + let mut requeue = HashSet::new(); + // A pass under way read the wallet before this: it neither + // probes these any more nor publishes anything for them. + if let Some(run) = entry.run.as_mut() { + match run.probing.as_mut() { + None => run.settled.extend(txids.iter().copied()), + Some(probing) => { + let before = probing.views.len(); + probing.due.retain(|root| !txids.contains(&root.txid)); + probing.views.retain(|view| !txids.contains(&view.txid)); + // The root whose probe is out settled: stop + // that probe and go on with the next root. + if probing + .in_flight + .as_ref() + .is_some_and(|root| txids.contains(&root.txid)) + { + probing.in_flight = None; + run.abort.abort(); + next_probe = true; + } + // A forced chain may have a new root now (its + // parent settled): ask for it in a new pass + // rather than wait for the next block. + if probing.views.len() != before { + requeue.extend(run.forced.iter().copied().filter(|txid| { + probing.views.iter().any(|view| view.txid == *txid) + })); + } + } } } + if !requeue.is_empty() { + let height = entry.height.unwrap_or(0); + let pending = entry.pending.get_or_insert_with(PendingPass::default); + pending.height = pending.height.max(height); + pending.forced.extend(requeue); + } } let events = self.state.settle(wallet_id, &txids); deliver(&self.sink, events); + if next_probe { + self.probe_next(wallet_id); + } } Command::Seen { wallet_id, touched } => { - let entry = self.entry(wallet_id); + // Only a wallet already known (see `Settled`). + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; + entry.seq += 1; if touched { entry.wait_until = None; if let Some(run) = entry.run.as_mut() { @@ -1360,7 +1401,7 @@ impl Actor { return; }; let anchor_at = pending.anchor_at; - let has_forced = !pending.forced.is_empty(); + let forced = pending.forced.clone(); // Walk the records if the pass may probe by schedule; one confined to // forced roots with none of them held has nothing to read for. let walk = Arc::new(AtomicBool::new(anchor_at.is_some())); @@ -1385,7 +1426,7 @@ impl Actor { seq: entry.seq, woken: false, anchor_at, - has_forced, + forced, walk, abort, probing: None, @@ -3811,6 +3852,89 @@ mod tests { assert!(rig.sent().is_empty()); } + /// Two roots due; while the first is out, the second settles: it is not + /// sent to the network. + #[tokio::test] + async fn should_not_probe_a_queued_root_settled_while_probing() { + let probe = Arc::new(ScriptedProbe::new(&[])); + let mut rig = enabled(probe.clone()).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); + rig.height(wallet(), 100).await; + rig.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // root 1 out, root 2 queued + + rig.handle(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(2)]), + }); + rig.settle().await; + + assert_eq!(probe.probed(), vec![txid(1)]); + } + + /// The root whose probe is out settles: that probe is stopped and the + /// pass goes on with the next root. + #[tokio::test] + async fn should_stop_the_probe_of_a_root_that_settles_while_out() { + let probe = Arc::new(ScriptedProbe::new(&[])); + let mut rig = enabled(probe.clone()).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); + rig.height(wallet(), 100).await; + rig.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // root 1's probe spawned, not yet run + + rig.handle(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(1)]), + }); + rig.settle().await; + + assert_eq!(probe.probed(), vec![txid(2)]); + } + + /// A forced send whose unconfirmed parent was the root settles in the + /// middle of the pass: the send, now a root, is probed at once — not at + /// the next block. + #[tokio::test] + async fn should_probe_a_forced_send_at_once_when_its_parent_root_settles() { + let probe = Arc::new(ScriptedProbe::new(&[])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), Vec::new()); + rig.follow(wallet(), 100).await; + rig.views( + wallet(), + vec![incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])], + ); + rig.actor.handle(Command::Uncertain(txid(6))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.actor.joined(listed); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // the parent's probe is out + + rig.views(wallet(), vec![send(6, &[outpoint(5, 0)])]); + rig.handle(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(5)]), + }); + rig.settle().await; + + assert_eq!(probe.probed(), vec![txid(6)]); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From f298cdb584548166e30dfd75588e385d4b61a625 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 16:39:03 +0300 Subject: [PATCH 036/169] fix(platform-wallet): thirtieth review pass on the broadcast resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - A probe result names its root, and only the root in flight takes it. Stopping the probe of a root that settled (pass 29) could come too late — the job had finished and its result was queued behind the settle — and the leftover answer was recorded against the next root: a healthy send could be marked Dead for good. The panic path checks the same identity. - A settle requeues a forced txid only when it settles one of that txid's unsettled ancestors, not on any unrelated settle during a forced pass. - A height step that introduces a wallet checks the manager still holds it; a step queued behind the wallet's removal no longer leaves an entry. - Tests (each fails without its fix): a stopped probe's answer is not recorded against the next root; an unrelated settle does not requeue a forced send; a height after removal leaves no entry. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 194 ++++++++++++++++-- 1 file changed, 179 insertions(+), 15 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 4278f625ae8..9a14ad98b7d 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -882,6 +882,9 @@ pub(crate) trait WalletSource: Send + Sync { /// The wallets whose records hold `txid` — one look under one lock. async fn holders(&self, txid: Txid) -> Vec; + /// Whether the manager holds the wallet. + async fn holds_wallet(&self, wallet_id: WalletId) -> bool; + /// `None`: no such wallet. `Some(None)`: nothing to do (see /// [`forced_held`]) — decided without walking the records. `walk` is read /// once the wallet is at hand: a catch-up may clear it meanwhile. @@ -907,6 +910,10 @@ struct ManagerSource(Arc>>); #[async_trait] impl WalletSource for ManagerSource { + async fn holds_wallet(&self, wallet_id: WalletId) -> bool { + self.0.read().await.get_wallet_info(&wallet_id).is_some() + } + async fn holders(&self, txid: Txid) -> Vec { let manager = self.0.read().await; manager @@ -1029,8 +1036,17 @@ enum JobDone { Probed { wallet_id: WalletId, run: u64, + /// The root it probed: a result for a root no longer in flight (its + /// probe was stopped, but had already finished) is not this one's. + root: Txid, verdict: ProbeVerdict, }, + /// Whether a wallet a height step introduced is in the manager. + Exists { + wallet_id: WalletId, + seq: u64, + exists: bool, + }, } #[derive(Default)] @@ -1117,7 +1133,8 @@ struct Actor { jobs: JoinSet, /// Which run each read or probe job belongs to, so a job that panicked /// can end its run instead of leaving the wallet stuck behind it. - job_runs: HashMap, + /// A probe job also names its root. + job_runs: HashMap)>, } impl Actor { @@ -1181,7 +1198,7 @@ impl Actor { // for an `Uncertain` result belongs to no run: wake every // wallet instead. match owner { - Some((wallet_id, run)) => { + Some((wallet_id, run, probed)) => { // Only the wallet's current run: a stale panic (its run // was dropped) must not undo a newer run's send. The // root whose probe panicked is the one in flight. @@ -1192,6 +1209,16 @@ impl Actor { else { return; }; + // A probe job's panic belongs to its own root only: the + // stopped probe of a settled root is not the one out now. + let in_flight_txid = current + .probing + .as_ref() + .and_then(|probing| probing.in_flight.as_ref()) + .map(|root| root.txid); + if probed.is_some() && probed != in_flight_txid { + return; + } let in_flight = current .probing .as_mut() @@ -1223,6 +1250,22 @@ impl Actor { match command { Command::Height { wallet_id, height } => { let enabled = self.enabled; + if !self.wallets.contains_key(&wallet_id) { + // A new entry — or a step queued behind the wallet's + // removal: check the manager still holds it. + let source = Arc::clone(&self.source); + let seq = self + .wallets + .get(&wallet_id) + .map_or(1, |entry| entry.seq + 1); + self.jobs.spawn(async move { + JobDone::Exists { + wallet_id, + seq, + exists: source.holds_wallet(wallet_id).await, + } + }); + } let entry = self.entry(wallet_id); let previous = entry.height.replace(height); let rewound = previous.is_some_and(|previous| height < previous); @@ -1291,7 +1334,21 @@ impl Actor { match run.probing.as_mut() { None => run.settled.extend(txids.iter().copied()), Some(probing) => { - let before = probing.views.len(); + // Forced txids whose unsettled ancestors include + // one of these: their chain gets a new root. + let graph = ChainGraph::new(&probing.views, &HashSet::new()); + let reroot: Vec = run + .forced + .iter() + .copied() + .filter(|txid| { + !txids.contains(txid) + && graph + .with_ancestors(txid) + .iter() + .any(|ancestor| txids.contains(ancestor)) + }) + .collect(); probing.due.retain(|root| !txids.contains(&root.txid)); probing.views.retain(|view| !txids.contains(&view.txid)); // The root whose probe is out settled: stop @@ -1308,11 +1365,7 @@ impl Actor { // A forced chain may have a new root now (its // parent settled): ask for it in a new pass // rather than wait for the next block. - if probing.views.len() != before { - requeue.extend(run.forced.iter().copied().filter(|txid| { - probing.views.iter().any(|view| view.txid == *txid) - })); - } + requeue.extend(reroot); } } } @@ -1418,7 +1471,7 @@ impl Actor { read, } }); - self.job_runs.insert(abort.id(), (wallet_id, run)); + self.job_runs.insert(abort.id(), (wallet_id, run, None)); entry.run = Some(Run { id: run, settled: HashSet::new(), @@ -1530,9 +1583,26 @@ impl Actor { } } } + JobDone::Exists { + wallet_id, + seq, + exists, + } => { + // A height step for a wallet removed meanwhile: drop the entry + // it made, unless something has happened to it since. + if !exists + && self + .wallets + .get(&wallet_id) + .is_some_and(|entry| entry.seq == seq && entry.run.is_none()) + { + self.wallets.remove(&wallet_id); + } + } JobDone::Probed { wallet_id, run, + root: probed, verdict, } => { let Some(current) = self @@ -1555,12 +1625,19 @@ impl Actor { self.end_run(wallet_id); return; }; + if probing + .in_flight + .as_ref() + .is_some_and(|root| root.txid != probed) + { + // The stopped probe of a settled root, finished before it + // could be stopped: not the root in flight now. + return; + } let Some(root) = probing.in_flight.take() else { - tracing::error!( - wallet_id = %hex::encode(wallet_id), - "broadcast probe: a probe result with no root in flight" - ); - self.end_run(wallet_id); + // Same: its root settled and nothing is out any more — + // or, impossibly, nothing was; carry on either way. + self.probe_next(wallet_id); return; }; if !probing.views.iter().any(|view| view.txid == root.txid) { @@ -1597,15 +1674,18 @@ impl Actor { let probe = Arc::clone(&self.probe); let id = run.id; let transaction = Arc::clone(&root.transaction); + let root_txid = root.txid; run.abort = self.jobs.spawn(async move { let verdict = probe.probe(&transaction).await; JobDone::Probed { wallet_id, run: id, + root: root_txid, verdict, } }); - self.job_runs.insert(run.abort.id(), (wallet_id, id)); + self.job_runs + .insert(run.abort.id(), (wallet_id, id, Some(root_txid))); if run.marks { mark_sent(&mut self.state, wallet_id, root.txid, probing.height); } @@ -2653,6 +2733,10 @@ mod tests { #[async_trait] impl WalletSource for FakeSource { + async fn holds_wallet(&self, wallet_id: WalletId) -> bool { + self.views.lock().expect("views").contains_key(&wallet_id) + } + async fn holders(&self, txid: Txid) -> Vec { self.views .lock() @@ -3935,6 +4019,86 @@ mod tests { assert_eq!(probe.probed(), vec![txid(6)]); } + /// The first root's probe finishes Dead, then the root settles before + /// the result is handled: the leftover answer is not recorded against + /// the next root in flight. + #[tokio::test] + async fn should_not_record_a_stopped_probe_answer_against_the_next_root() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); + rig.height(wallet(), 100).await; + rig.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // root 1's probe spawned + tokio::task::yield_now().await; // ...and finished, not yet handled + + rig.handle(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(1)]), + }); + rig.settle().await; + + assert!(!rig + .sent() + .contains(&ResolverEvent::Verdict(txid(2), dead()))); + assert!(!matches!( + rig.actor.state.finished.get(&(wallet(), txid(2))), + Some(Final::Dead { .. }) + )); + } + + /// A settle unrelated to a forced send's chain does not probe that send + /// again. + #[tokio::test] + async fn should_not_requeue_a_forced_send_for_an_unrelated_settle() { + let probe = Arc::new(ScriptedProbe::new(&[])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), Vec::new()); + rig.follow(wallet(), 100).await; + rig.views( + wallet(), + vec![ + incoming(5, &[outpoint(80, 0)]), + send(6, &[outpoint(5, 0)]), + incoming(8, &[outpoint(88, 0)]), + ], + ); + rig.actor.handle(Command::Uncertain(txid(6))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.actor.joined(listed); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // root 5 out + + rig.handle(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(8)]), + }); + rig.settle().await; + + assert_eq!(probe.probed(), vec![txid(5)]); + } + + /// A height step queued behind a wallet's removal does not leave an entry + /// behind. + #[tokio::test] + async fn should_not_keep_an_entry_for_a_height_after_removal() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.source.views.lock().expect("views").remove(&wallet()); + + rig.send(Command::WalletRemoved(wallet())).await; + rig.height(wallet(), 102).await; + + assert!(!rig.actor.wallets.contains_key(&wallet())); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From 4f0ba991774c3f4f17e5f35bed8b76ab5fc1f260 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 16:48:07 +0300 Subject: [PATCH 037/169] fix(platform-wallet): thirty-first review pass on the broadcast resolver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Wallet entries come from an explicit WalletAdded command, sent by registration and load right after the wallet is in the manager; a height step no longer creates one. This replaces the pass-30 existence check, whose sequence match any other queued event defeated: events queued behind a removal now find no entry, whatever their order. - A settle requeues a forced txid only when its chain gains a root it did not have, computed with the probe-found mined set as begin_pass does — not when an ancestor merely settles (another root still pending, or one already known mined). - The chain graph for that check is built only when the run was forced. - A probe result with no root in flight is logged and ends the run again (the invariant it checks cannot break silently). - Tests: no requeue when a settle leaves no new root (fails with the old rule); a height and a Seen queued behind a removal leave no entry (fails if a height creates entries). Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/manager/load.rs | 1 + .../src/manager/wallet_lifecycle.rs | 1 + .../src/wallet/core/broadcast_resolver.rs | 169 ++++++++++-------- 3 files changed, 101 insertions(+), 70 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 16948d64f58..df2f0c4db2d 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -368,6 +368,7 @@ impl PlatformWalletManager

{ wallets.insert(wallet_id, Arc::clone(&platform_wallet)); wallets }); + self.broadcast_resolver.wallet_added(&wallet_id); inserted_in_wallets.push((wallet_id, Arc::clone(platform_wallet.generation()))); // Re-seed the balance atomic now that the wallet is published. diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index c90065b9ae2..8fdca7847ca 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -618,6 +618,7 @@ impl PlatformWalletManager

{ wallets.insert(wallet_id, Arc::clone(&platform_wallet)); wallets }); + self.broadcast_resolver.wallet_added(&wallet_id); // Re-seed the lock-free balance atomic from the wallet's inner // balance now that the wallet is in `self.wallets`. diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 9a14ad98b7d..58bc6902c22 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -882,9 +882,6 @@ pub(crate) trait WalletSource: Send + Sync { /// The wallets whose records hold `txid` — one look under one lock. async fn holders(&self, txid: Txid) -> Vec; - /// Whether the manager holds the wallet. - async fn holds_wallet(&self, wallet_id: WalletId) -> bool; - /// `None`: no such wallet. `Some(None)`: nothing to do (see /// [`forced_held`]) — decided without walking the records. `walk` is read /// once the wallet is at hand: a catch-up may clear it meanwhile. @@ -910,10 +907,6 @@ struct ManagerSource(Arc>>); #[async_trait] impl WalletSource for ManagerSource { - async fn holds_wallet(&self, wallet_id: WalletId) -> bool { - self.0.read().await.get_wallet_info(&wallet_id).is_some() - } - async fn holders(&self, txid: Txid) -> Vec { let manager = self.0.read().await; manager @@ -1020,6 +1013,10 @@ enum Command { }, Uncertain(Txid), SetEnabled(bool), + /// The wallet was registered or loaded into the manager: only this + /// creates a wallet's entry, so events queued behind a removal cannot + /// bring one back. + WalletAdded(WalletId), WalletRemoved(WalletId), } @@ -1041,12 +1038,6 @@ enum JobDone { root: Txid, verdict: ProbeVerdict, }, - /// Whether a wallet a height step introduced is in the manager. - Exists { - wallet_id: WalletId, - seq: u64, - exists: bool, - }, } #[derive(Default)] @@ -1250,23 +1241,11 @@ impl Actor { match command { Command::Height { wallet_id, height } => { let enabled = self.enabled; - if !self.wallets.contains_key(&wallet_id) { - // A new entry — or a step queued behind the wallet's - // removal: check the manager still holds it. - let source = Arc::clone(&self.source); - let seq = self - .wallets - .get(&wallet_id) - .map_or(1, |entry| entry.seq + 1); - self.jobs.spawn(async move { - JobDone::Exists { - wallet_id, - seq, - exists: source.holds_wallet(wallet_id).await, - } - }); - } - let entry = self.entry(wallet_id); + // Only a wallet the actor was told about (see `WalletAdded`). + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; + entry.seq += 1; let previous = entry.height.replace(height); let rewound = previous.is_some_and(|previous| height < previous); if rewound { @@ -1334,21 +1313,20 @@ impl Actor { match run.probing.as_mut() { None => run.settled.extend(txids.iter().copied()), Some(probing) => { - // Forced txids whose unsettled ancestors include - // one of these: their chain gets a new root. - let graph = ChainGraph::new(&probing.views, &HashSet::new()); - let reroot: Vec = run - .forced - .iter() - .copied() - .filter(|txid| { - !txids.contains(txid) - && graph - .with_ancestors(txid) - .iter() - .any(|ancestor| txids.contains(ancestor)) - }) - .collect(); + // The roots of each forced chain before this + // settle: a root after it that was not one + // before is new, and worth probing at once. + let mined = self.state.mined(&wallet_id); + let roots_before: Vec<(Txid, BTreeSet)> = + if run.forced.is_empty() { + Vec::new() + } else { + let graph = ChainGraph::new(&probing.views, &mined); + run.forced + .iter() + .map(|txid| (*txid, graph.roots_of(txid))) + .collect() + }; probing.due.retain(|root| !txids.contains(&root.txid)); probing.views.retain(|view| !txids.contains(&view.txid)); // The root whose probe is out settled: stop @@ -1365,7 +1343,19 @@ impl Actor { // A forced chain may have a new root now (its // parent settled): ask for it in a new pass // rather than wait for the next block. - requeue.extend(reroot); + if !roots_before.is_empty() { + let graph = ChainGraph::new(&probing.views, &mined); + requeue.extend( + roots_before + .into_iter() + .filter(|(txid, before)| { + !txids.contains(txid) + && graph.is_unsettled(txid) + && !graph.roots_of(txid).is_subset(before) + }) + .map(|(txid, _)| txid), + ); + } } } } @@ -1432,6 +1422,9 @@ impl Actor { deliver(&self.sink, events); } } + Command::WalletAdded(wallet_id) => { + self.entry(wallet_id); + } Command::WalletRemoved(wallet_id) => { if let Some(run) = self.wallets.remove(&wallet_id).and_then(|entry| entry.run) { run.abort.abort(); @@ -1583,22 +1576,6 @@ impl Actor { } } } - JobDone::Exists { - wallet_id, - seq, - exists, - } => { - // A height step for a wallet removed meanwhile: drop the entry - // it made, unless something has happened to it since. - if !exists - && self - .wallets - .get(&wallet_id) - .is_some_and(|entry| entry.seq == seq && entry.run.is_none()) - { - self.wallets.remove(&wallet_id); - } - } JobDone::Probed { wallet_id, run, @@ -1635,9 +1612,13 @@ impl Actor { return; } let Some(root) = probing.in_flight.take() else { - // Same: its root settled and nothing is out any more — - // or, impossibly, nothing was; carry on either way. - self.probe_next(wallet_id); + // Cannot happen: a settle that clears the root in flight + // re-arms the run or ends it at once. Fail safe. + tracing::error!( + wallet_id = %hex::encode(wallet_id), + "broadcast probe: a probe result with no root in flight" + ); + self.end_run(wallet_id); return; }; if !probing.views.iter().any(|view| view.txid == root.txid) { @@ -1825,6 +1806,11 @@ impl BroadcastResolver { self.send(Command::WalletRemoved(*wallet_id)); } + /// A wallet was registered or loaded — call after it is in the manager. + pub(crate) fn wallet_added(&self, wallet_id: &WalletId) { + self.send(Command::WalletAdded(*wallet_id)); + } + /// Stop the resolver's task: it aborts every read and probe it started and /// waits for them, all within `budget`; after this no host callback comes /// from the resolver. `NotRunning` if it never started or was already @@ -2733,10 +2719,6 @@ mod tests { #[async_trait] impl WalletSource for FakeSource { - async fn holds_wallet(&self, wallet_id: WalletId) -> bool { - self.views.lock().expect("views").contains_key(&wallet_id) - } - async fn holders(&self, txid: Txid) -> Vec { self.views .lock() @@ -2827,6 +2809,13 @@ mod tests { /// wallet's synced height along, as the SPV client does before it /// reports the step. fn handle(&mut self, command: Command) { + // A wallet the tests step through is registered, as the manager + // does before any of its events. + if let Command::Height { wallet_id, .. } = command { + if !self.actor.wallets.contains_key(&wallet_id) { + self.actor.handle(Command::WalletAdded(wallet_id)); + } + } if let Command::Height { wallet_id, height } = command { let mut synced = self.source.synced.lock().expect("synced"); let at = synced.entry(wallet_id).or_insert(0); @@ -4094,11 +4083,51 @@ mod tests { rig.source.views.lock().expect("views").remove(&wallet()); rig.send(Command::WalletRemoved(wallet())).await; - rig.height(wallet(), 102).await; + // Straight to the actor: a step queued behind the removal. + rig.actor.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + rig.actor.handle(Command::Seen { + wallet_id: wallet(), + touched: true, + }); + rig.settle().await; assert!(!rig.actor.wallets.contains_key(&wallet())); } + /// A forced send built on two unsettled roots: one of them settling + /// leaves the other as its only root — no new root, no requeue. + #[tokio::test] + async fn should_not_requeue_when_a_settle_leaves_no_new_root() { + let probe = Arc::new(ScriptedProbe::new(&[])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), Vec::new()); + rig.follow(wallet(), 100).await; + rig.views( + wallet(), + vec![ + incoming(1, &[outpoint(80, 0)]), + incoming(2, &[outpoint(81, 0)]), + send(3, &[outpoint(1, 0), outpoint(2, 0)]), + ], + ); + rig.actor.handle(Command::Uncertain(txid(3))); + let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + rig.actor.joined(listed); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // root 1 out, root 2 queued + + rig.handle(Command::Settled { + wallet_id: wallet(), + txids: HashSet::from([txid(1)]), + }); + rig.settle().await; + + assert_eq!(probe.probed(), vec![txid(2)], "root 1 stopped, root 2 once"); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From 23c22dd4e55a5a7f93dceeb0e7ccd7bf7e5a403d Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 16:59:06 +0300 Subject: [PATCH 038/169] fix(platform-wallet): thirty-second review pass on the broadcast resolver - WalletAdded and WalletRemoved are sent under the wallet-manager guard that inserts or removes the wallet, so the resolver hears of a wallet before any of its events and a same-id re-registration cannot overtake a removal. An Uncertain lookup that lands after a removal no longer recreates the entry: only WalletAdded does. - A forced send whose parent root a probe finds in a block becomes a root and is probed in a pass right away (forced_with_new_roots, shared with the settle path, which now computes the mined set only for forced runs). - The no-root-in-flight fail-safe logs and carries on with the roots still due instead of ending the run. - The test rig registers a wallet when its records are first set, not on every height step, so a late step for a removed wallet stays late. - Tests (each fails without its fix): a forced send probed at once when its root is found mined; a lookup after removal brings no entry back. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/manager/load.rs | 3 +- .../src/manager/wallet_lifecycle.rs | 23 ++- .../src/wallet/core/broadcast_resolver.rs | 173 +++++++++++++----- 3 files changed, 138 insertions(+), 61 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index df2f0c4db2d..3bcfde948a3 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -277,6 +277,8 @@ impl PlatformWalletManager

{ ))); break 'load; } + // Under the insert's guard: before any of the wallet's events. + self.broadcast_resolver.wallet_added(&wallet_id); } inserted_in_manager.push(wallet_id); @@ -368,7 +370,6 @@ impl PlatformWalletManager

{ wallets.insert(wallet_id, Arc::clone(&platform_wallet)); wallets }); - self.broadcast_resolver.wallet_added(&wallet_id); inserted_in_wallets.push((wallet_id, Arc::clone(platform_wallet.generation()))); // Re-seed the balance atomic now that the wallet is published. diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 8fdca7847ca..05e762b6c9b 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -438,14 +438,19 @@ impl PlatformWalletManager

{ // stays `WalletCreation`. let wallet_id = { let mut wm = self.wallet_manager.write().await; - wm.insert_wallet(wallet, platform_info) + let wallet_id = wm + .insert_wallet(wallet, platform_info) .map_err(|e| match e { key_wallet_manager::WalletError::WalletExists(id) => already_registered(id), other => PlatformWalletError::WalletCreation(format!( "Failed to register wallet in WalletManager: {}", other )), - })? + })?; + // Under the same guard as the insert: the resolver hears of the + // wallet before any of its events, and in order with a removal. + self.broadcast_resolver.wallet_added(&wallet_id); + wallet_id }; // `insert_wallet` recomputes the id from the (now external-signable) @@ -618,7 +623,6 @@ impl PlatformWalletManager

{ wallets.insert(wallet_id, Arc::clone(&platform_wallet)); wallets }); - self.broadcast_resolver.wallet_added(&wallet_id); // Re-seed the lock-free balance atomic from the wallet's inner // balance now that the wallet is in `self.wallets`. @@ -887,15 +891,14 @@ impl PlatformWalletManager

{ "remove_wallet: inner wallet-manager removal failed (state may be inconsistent)" ); } + // Broadcast-probe state for the wallet's sends goes too, and the + // host is told to drop the verdicts it shows for them (from the + // resolver's task). After the inner removal, so no later pass can + // read the wallet and rebuild that state; under the same guard, so + // it is queued ahead of a same-id re-registration's WalletAdded. + self.broadcast_resolver.wallet_removed(wallet_id); } - // Broadcast-probe state for the wallet's sends goes too, and the host - // is told to drop the verdicts it shows for them (from the resolver's - // task). After the inner removal, so no later pass can read the wallet - // and rebuild that state; before the midpoint, so the removal is queued - // ahead of every event of a same-id recreation and never touches it. - self.broadcast_resolver.wallet_removed(wallet_id); - // Test-only rendezvous: the window a concurrent same-id registration can // publish a new generation into. Sits AFTER every id-keyed unregister, // so state a midpoint recreation installs is never torn down by this diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 58bc6902c22..9bbb075d5ea 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -853,6 +853,30 @@ fn log_outgoing(item: &Outgoing) { } } +/// The forced txids whose chain gains a root it did not have when the pass +/// saw `views` with `mined_before` found in blocks, now that `gone` left and +/// `mined_after` are known mined: a new root worth probing at once. +fn forced_with_new_roots( + views: &[OutgoingView], + mined_before: &HashSet, + mined_after: &HashSet, + gone: &HashSet, + forced: &HashSet, +) -> Vec { + let before = ChainGraph::new(views, mined_before); + let remaining: Vec = views + .iter() + .filter(|view| !gone.contains(&view.txid)) + .cloned() + .collect(); + let after = ChainGraph::new(&remaining, mined_after); + forced + .iter() + .filter(|txid| !after.roots_of(txid).is_subset(&before.roots_of(txid))) + .copied() + .collect() +} + // ---- The actor ------------------------------------------------------------- /// What a pass reads from a wallet. @@ -1313,20 +1337,19 @@ impl Actor { match run.probing.as_mut() { None => run.settled.extend(txids.iter().copied()), Some(probing) => { - // The roots of each forced chain before this - // settle: a root after it that was not one - // before is new, and worth probing at once. - let mined = self.state.mined(&wallet_id); - let roots_before: Vec<(Txid, BTreeSet)> = - if run.forced.is_empty() { - Vec::new() - } else { - let graph = ChainGraph::new(&probing.views, &mined); - run.forced - .iter() - .map(|txid| (*txid, graph.roots_of(txid))) - .collect() - }; + if !run.forced.is_empty() { + // A forced chain may gain a root (its parent + // settled): ask for it in a new pass rather + // than wait for the next block. + let mined = self.state.mined(&wallet_id); + requeue.extend(forced_with_new_roots( + &probing.views, + &mined, + &mined, + &txids, + &run.forced, + )); + } probing.due.retain(|root| !txids.contains(&root.txid)); probing.views.retain(|view| !txids.contains(&view.txid)); // The root whose probe is out settled: stop @@ -1340,22 +1363,6 @@ impl Actor { run.abort.abort(); next_probe = true; } - // A forced chain may have a new root now (its - // parent settled): ask for it in a new pass - // rather than wait for the next block. - if !roots_before.is_empty() { - let graph = ChainGraph::new(&probing.views, &mined); - requeue.extend( - roots_before - .into_iter() - .filter(|(txid, before)| { - !txids.contains(txid) - && graph.is_unsettled(txid) - && !graph.roots_of(txid).is_subset(before) - }) - .map(|(txid, _)| txid), - ); - } } } } @@ -1391,8 +1398,8 @@ impl Actor { if !self.enabled { return; } - // Routed to the wallets holding it, including ones no event - // has introduced yet. + // Routed to the registered wallets holding it, including ones + // no height step has reached yet. let source = Arc::clone(&self.source); self.jobs.spawn(async move { JobDone::Listed { @@ -1486,9 +1493,14 @@ impl Actor { return; } for wallet_id in wallets { + // Only a wallet the actor was told about (see + // `WalletAdded`): one removed since the lookup is gone. // Counted like a command: a run that read the id as gone // before this must not drop the entry and its request. - let entry = self.entry(wallet_id); + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + continue; + }; + entry.seq += 1; let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(entry.height.unwrap_or(0)); pending.forced.insert(txid); @@ -1613,12 +1625,13 @@ impl Actor { } let Some(root) = probing.in_flight.take() else { // Cannot happen: a settle that clears the root in flight - // re-arms the run or ends it at once. Fail safe. + // re-arms the run or ends it at once. Log it and carry on + // with the roots still due, forced ones included. tracing::error!( wallet_id = %hex::encode(wallet_id), "broadcast probe: a probe result with no root in flight" ); - self.end_run(wallet_id); + self.probe_next(wallet_id); return; }; if !probing.views.iter().any(|view| view.txid == root.txid) { @@ -1626,6 +1639,23 @@ impl Actor { self.probe_next(wallet_id); return; } + // Found in a block: the sends built on it become roots, and a + // forced one among them is worth probing at once. + let reroot = if matches!(verdict, ProbeVerdict::Mined) && !current.forced.is_empty() + { + let before = self.state.mined(&wallet_id); + let mut after = before.clone(); + after.insert(root.txid); + forced_with_new_roots( + &probing.views, + &before, + &after, + &HashSet::new(), + ¤t.forced, + ) + } else { + Vec::new() + }; let events = record_probe( &mut self.state, wallet_id, @@ -1635,6 +1665,14 @@ impl Actor { &verdict, ); deliver(&self.sink, events); + if !reroot.is_empty() { + if let Some(entry) = self.wallets.get_mut(&wallet_id) { + let height = entry.height.unwrap_or(0); + let pending = entry.pending.get_or_insert_with(PendingPass::default); + pending.height = pending.height.max(height); + pending.forced.extend(reroot); + } + } self.probe_next(wallet_id); } } @@ -2791,12 +2829,19 @@ mod tests { } } - fn views(&self, wallet_id: WalletId, views: Vec) { - self.source + /// The wallet's unsettled records; the first call registers the + /// wallet, as the manager does before any of its events. + fn views(&mut self, wallet_id: WalletId, views: Vec) { + let new = self + .source .views .lock() .expect("views") - .insert(wallet_id, views); + .insert(wallet_id, views) + .is_none(); + if new && !self.actor.wallets.contains_key(&wallet_id) { + self.actor.handle(Command::WalletAdded(wallet_id)); + } } /// Handle `command`, then every job it caused, as the task would. @@ -2809,13 +2854,6 @@ mod tests { /// wallet's synced height along, as the SPV client does before it /// reports the step. fn handle(&mut self, command: Command) { - // A wallet the tests step through is registered, as the manager - // does before any of its events. - if let Command::Height { wallet_id, .. } = command { - if !self.actor.wallets.contains_key(&wallet_id) { - self.actor.handle(Command::WalletAdded(wallet_id)); - } - } if let Command::Height { wallet_id, height } = command { let mut synced = self.source.synced.lock().expect("synced"); let at = synced.entry(wallet_id).or_insert(0); @@ -3008,13 +3046,16 @@ mod tests { assert!(!rig.actor.wallets.contains_key(&wallet())); } - /// A late event can register an id the wallet manager no longer holds; - /// the first pass that finds it gone drops it. + /// A registered wallet the manager no longer holds (removed without the + /// removal reaching the resolver yet): the first pass that finds it gone + /// drops it. #[tokio::test] async fn should_drop_a_wallet_the_manager_no_longer_holds() { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + rig.actor.handle(Command::WalletAdded(wallet())); // never in the source rig.height(wallet(), 100).await; + assert!(rig.actor.wallets.contains_key(&wallet())); rig.handle(Command::Height { wallet_id: wallet(), height: 101, @@ -3024,8 +3065,8 @@ mod tests { assert!(!rig.actor.wallets.contains_key(&wallet())); } - /// An `Uncertain` result right after launch, before any event introduced - /// the wallet, still gets its immediate probe. + /// An `Uncertain` result right after launch, before any height step + /// reached the wallet, still gets its immediate probe. #[tokio::test] async fn should_route_an_uncertain_result_to_a_wallet_no_event_introduced() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); @@ -4128,6 +4169,38 @@ mod tests { assert_eq!(probe.probed(), vec![txid(2)], "root 1 stopped, root 2 once"); } + /// The forced send's parent root is found in a block: the send becomes a + /// root and is probed at once, not at the next block. + #[tokio::test] + async fn should_probe_a_forced_send_at_once_when_its_root_is_found_mined() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), Vec::new()); + rig.follow(wallet(), 100).await; + rig.views( + wallet(), + vec![incoming(1, &[outpoint(80, 0)]), send(2, &[outpoint(1, 0)])], + ); + + rig.send(Command::Uncertain(txid(2))).await; + + assert_eq!(probe.probed(), vec![txid(1), txid(2)]); + } + + /// An `Uncertain` lookup that lands after its wallet was removed does not + /// bring the wallet's entry back. + #[tokio::test] + async fn should_not_bring_back_a_removed_wallet_from_a_lookup() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.actor.handle(Command::Uncertain(txid(1))); + rig.actor.handle(Command::WalletRemoved(wallet())); + + rig.settle().await; + + assert!(!rig.actor.wallets.contains_key(&wallet())); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, @@ -4239,7 +4312,7 @@ mod tests { /// outlives it. #[tokio::test] async fn should_end_with_every_job_when_stopped() { - let rig = Rig::new(Arc::new(HangingProbe)); + let mut rig = Rig::new(Arc::new(HangingProbe)); rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); let (commands, receiver) = mpsc::unbounded_channel(); let (stop, stopped) = oneshot::channel(); From 683ff6f33d132e91a21f3b6c13e6c5ebbc2832a4 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:08:05 +0300 Subject: [PATCH 039/169] refactor(platform-wallet): one probe per Uncertain report, no re-rooting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An Uncertain report forces one probe of the transaction's chain root at that moment; a send built on the root becomes due by its schedule once the root settles. The two special cases that tried to probe the send at once (a requeue when a root settled mid-pass, a reroot when a probe found the root mined) only worked while the forced pass was alive, and each review pass found another path around them — so they go, along with forced_with_new_roots. The cost is a block's delay for that send, never a wrong verdict; the module docs state the contract. Thirty-third review pass, also: - With WalletAdded/WalletRemoved ordered under the manager guard, a read that finds a registered wallet gone cannot happen; the per-wallet command count and the keep-the-entry logic go, the branch logs, forgets and ends the run as a fail-safe. - The test rig registers a wallet whenever the actor does not know it, so a same-id re-registration in a test is registered too. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 215 +++--------------- 1 file changed, 29 insertions(+), 186 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 9bbb075d5ea..b82606fd082 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -33,8 +33,9 @@ //! locked) or swept clears its verdict at once, even mid-scan; a pass //! clears whatever else left the wallet when it next reads it. //! -//! When: once as soon as dash-spv reports an `Uncertain` broadcast (the root -//! of that transaction's chain), then on each advance of the wallet's synced +//! When: once as soon as dash-spv reports an `Uncertain` broadcast — the +//! current root of that transaction's chain; a send built on it becomes due +//! by schedule once the root settles, not at once — then on each advance of the wallet's synced //! height that follows the tip — steps of more than [`CATCH_UP_STEP`] blocks, //! steps back (a rescan) and the first step after launch are catch-up and //! skipped (passes queued before them probe only what an `Uncertain` result @@ -853,30 +854,6 @@ fn log_outgoing(item: &Outgoing) { } } -/// The forced txids whose chain gains a root it did not have when the pass -/// saw `views` with `mined_before` found in blocks, now that `gone` left and -/// `mined_after` are known mined: a new root worth probing at once. -fn forced_with_new_roots( - views: &[OutgoingView], - mined_before: &HashSet, - mined_after: &HashSet, - gone: &HashSet, - forced: &HashSet, -) -> Vec { - let before = ChainGraph::new(views, mined_before); - let remaining: Vec = views - .iter() - .filter(|view| !gone.contains(&view.txid)) - .cloned() - .collect(); - let after = ChainGraph::new(&remaining, mined_after); - forced - .iter() - .filter(|txid| !after.roots_of(txid).is_subset(&before.roots_of(txid))) - .copied() - .collect() -} - // ---- The actor ------------------------------------------------------------- /// What a pass reads from a wallet. @@ -1089,16 +1066,14 @@ struct Run { /// Cleared by a rewind: the wallet's schedules were restarted, and this /// run, older than that, marks no probes into them. marks: bool, - /// The wallet's command count when the run started. - seq: u64, /// Something touched the wallet during the run: its read may be stale, so /// the next height must not be skipped. woken: bool, /// Where this pass starts windows; `None` confines it to forced roots /// (see [`PendingPass::anchor_at`]). anchor_at: Option, - /// The txids `Uncertain` results forced it for. - forced: HashSet, + /// `Uncertain` results forced it. + has_forced: bool, /// Whether the read walks the records even when none of the forced txids /// is held (`anchor_at.is_some()`): shared with the read job, cleared by a /// catch-up. @@ -1128,10 +1103,6 @@ struct WalletEntry { wait_until: Option, pending: Option, run: Option, - /// Commands seen for this wallet. A run that found the wallet gone drops - /// the entry only if none arrived since it started — a same-id wallet - /// added meanwhile keeps its entry. - seq: u64, } /// Owns all of the resolver's state. One task; commands and job results are @@ -1254,11 +1225,9 @@ impl Actor { } } - /// The wallet's entry, created on first sight, with this command counted. + /// The wallet's entry, created on first sight. fn entry(&mut self, wallet_id: WalletId) -> &mut WalletEntry { - let entry = self.wallets.entry(wallet_id).or_default(); - entry.seq += 1; - entry + self.wallets.entry(wallet_id).or_default() } fn handle(&mut self, command: Command) { @@ -1269,7 +1238,6 @@ impl Actor { let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; - entry.seq += 1; let previous = entry.height.replace(height); let rewound = previous.is_some_and(|previous| height < previous); if rewound { @@ -1306,7 +1274,7 @@ impl Actor { } // Still reading, with nothing forced: nothing it could // probe — stop its walk over the records now. - if run.probing.is_none() && run.forced.is_empty() { + if run.probing.is_none() && !run.has_forced { run.abort.abort(); self.end_run(wallet_id); } @@ -1329,27 +1297,12 @@ impl Actor { // Only a wallet already known: an event queued behind its // removal must not bring back an entry nothing would drop. if let Some(entry) = self.wallets.get_mut(&wallet_id) { - entry.seq += 1; - let mut requeue = HashSet::new(); // A pass under way read the wallet before this: it neither // probes these any more nor publishes anything for them. if let Some(run) = entry.run.as_mut() { match run.probing.as_mut() { None => run.settled.extend(txids.iter().copied()), Some(probing) => { - if !run.forced.is_empty() { - // A forced chain may gain a root (its parent - // settled): ask for it in a new pass rather - // than wait for the next block. - let mined = self.state.mined(&wallet_id); - requeue.extend(forced_with_new_roots( - &probing.views, - &mined, - &mined, - &txids, - &run.forced, - )); - } probing.due.retain(|root| !txids.contains(&root.txid)); probing.views.retain(|view| !txids.contains(&view.txid)); // The root whose probe is out settled: stop @@ -1366,12 +1319,6 @@ impl Actor { } } } - if !requeue.is_empty() { - let height = entry.height.unwrap_or(0); - let pending = entry.pending.get_or_insert_with(PendingPass::default); - pending.height = pending.height.max(height); - pending.forced.extend(requeue); - } } let events = self.state.settle(wallet_id, &txids); deliver(&self.sink, events); @@ -1384,7 +1331,6 @@ impl Actor { let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; - entry.seq += 1; if touched { entry.wait_until = None; if let Some(run) = entry.run.as_mut() { @@ -1454,7 +1400,7 @@ impl Actor { return; }; let anchor_at = pending.anchor_at; - let forced = pending.forced.clone(); + let has_forced = !pending.forced.is_empty(); // Walk the records if the pass may probe by schedule; one confined to // forced roots with none of them held has nothing to read for. let walk = Arc::new(AtomicBool::new(anchor_at.is_some())); @@ -1476,10 +1422,9 @@ impl Actor { id: run, settled: HashSet::new(), marks: true, - seq: entry.seq, woken: false, anchor_at, - forced, + has_forced, walk, abort, probing: None, @@ -1495,12 +1440,9 @@ impl Actor { for wallet_id in wallets { // Only a wallet the actor was told about (see // `WalletAdded`): one removed since the lookup is gone. - // Counted like a command: a run that read the id as gone - // before this must not drop the entry and its request. let Some(entry) = self.wallets.get_mut(&wallet_id) else { continue; }; - entry.seq += 1; let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(entry.height.unwrap_or(0)); pending.forced.insert(txid); @@ -1516,31 +1458,25 @@ impl Actor { let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; - let run_seq = match entry.run.as_ref() { - Some(current) if current.id == run => current.seq, - _ => return, - }; + if !entry.run.as_ref().is_some_and(|current| current.id == run) { + return; + } match read { None => { - // Gone from the wallet manager. Every removal path - // also sends `WalletRemoved`, but that command may - // still be queued, or a lookup may have raced it: - // forget the wallet here too — clearing twice is - // harmless. Keep the entry if a command for a same-id - // wallet arrived meanwhile. - let keep = run_seq != entry.seq; - if !keep { - self.wallets.remove(&wallet_id); - } + // Gone from the wallet manager. Cannot happen: its + // WalletRemoved is queued under the same guard that + // removed it, and commands are handled before job + // results. Fail safe: forget its state and end the run. + tracing::error!( + wallet_id = %hex::encode(wallet_id), + "broadcast probe: a registered wallet read as gone" + ); let events = self.state.forget_wallet(&wallet_id); deliver(&self.sink, events); - if keep { - // The wait belonged to the state just forgotten. - if let Some(entry) = self.wallets.get_mut(&wallet_id) { - entry.wait_until = None; - } - self.end_run(wallet_id); + if let Some(entry) = self.wallets.get_mut(&wallet_id) { + entry.wait_until = None; } + self.end_run(wallet_id); } Some(None) => self.end_run(wallet_id), Some(Some(read)) => { @@ -1639,23 +1575,6 @@ impl Actor { self.probe_next(wallet_id); return; } - // Found in a block: the sends built on it become roots, and a - // forced one among them is worth probing at once. - let reroot = if matches!(verdict, ProbeVerdict::Mined) && !current.forced.is_empty() - { - let before = self.state.mined(&wallet_id); - let mut after = before.clone(); - after.insert(root.txid); - forced_with_new_roots( - &probing.views, - &before, - &after, - &HashSet::new(), - ¤t.forced, - ) - } else { - Vec::new() - }; let events = record_probe( &mut self.state, wallet_id, @@ -1665,14 +1584,6 @@ impl Actor { &verdict, ); deliver(&self.sink, events); - if !reroot.is_empty() { - if let Some(entry) = self.wallets.get_mut(&wallet_id) { - let height = entry.height.unwrap_or(0); - let pending = entry.pending.get_or_insert_with(PendingPass::default); - pending.height = pending.height.max(height); - pending.forced.extend(reroot); - } - } self.probe_next(wallet_id); } } @@ -2829,17 +2740,15 @@ mod tests { } } - /// The wallet's unsettled records; the first call registers the - /// wallet, as the manager does before any of its events. + /// The wallet's unsettled records; registers the wallet if the actor + /// does not know it, as the manager does before any of its events. fn views(&mut self, wallet_id: WalletId, views: Vec) { - let new = self - .source + self.source .views .lock() .expect("views") - .insert(wallet_id, views) - .is_none(); - if new && !self.actor.wallets.contains_key(&wallet_id) { + .insert(wallet_id, views); + if !self.actor.wallets.contains_key(&wallet_id) { self.actor.handle(Command::WalletAdded(wallet_id)); } } @@ -3046,25 +2955,6 @@ mod tests { assert!(!rig.actor.wallets.contains_key(&wallet())); } - /// A registered wallet the manager no longer holds (removed without the - /// removal reaching the resolver yet): the first pass that finds it gone - /// drops it. - #[tokio::test] - async fn should_drop_a_wallet_the_manager_no_longer_holds() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; - rig.actor.handle(Command::WalletAdded(wallet())); // never in the source - - rig.height(wallet(), 100).await; - assert!(rig.actor.wallets.contains_key(&wallet())); - rig.handle(Command::Height { - wallet_id: wallet(), - height: 101, - }); - rig.settle().await; - - assert!(!rig.actor.wallets.contains_key(&wallet())); - } - /// An `Uncertain` result right after launch, before any height step /// reached the wallet, still gets its immediate probe. #[tokio::test] @@ -4020,35 +3910,6 @@ mod tests { assert_eq!(probe.probed(), vec![txid(2)]); } - /// A forced send whose unconfirmed parent was the root settles in the - /// middle of the pass: the send, now a root, is probed at once — not at - /// the next block. - #[tokio::test] - async fn should_probe_a_forced_send_at_once_when_its_parent_root_settles() { - let probe = Arc::new(ScriptedProbe::new(&[])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), Vec::new()); - rig.follow(wallet(), 100).await; - rig.views( - wallet(), - vec![incoming(5, &[outpoint(80, 0)]), send(6, &[outpoint(5, 0)])], - ); - rig.actor.handle(Command::Uncertain(txid(6))); - let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); - rig.actor.joined(listed); - let read = rig.actor.jobs.join_next_with_id().await.expect("read"); - rig.actor.joined(read); // the parent's probe is out - - rig.views(wallet(), vec![send(6, &[outpoint(5, 0)])]); - rig.handle(Command::Settled { - wallet_id: wallet(), - txids: HashSet::from([txid(5)]), - }); - rig.settle().await; - - assert_eq!(probe.probed(), vec![txid(6)]); - } - /// The first root's probe finishes Dead, then the root settles before /// the result is handled: the leftover answer is not recorded against /// the next root in flight. @@ -4169,24 +4030,6 @@ mod tests { assert_eq!(probe.probed(), vec![txid(2)], "root 1 stopped, root 2 once"); } - /// The forced send's parent root is found in a block: the send becomes a - /// root and is probed at once, not at the next block. - #[tokio::test] - async fn should_probe_a_forced_send_at_once_when_its_root_is_found_mined() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined)])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), Vec::new()); - rig.follow(wallet(), 100).await; - rig.views( - wallet(), - vec![incoming(1, &[outpoint(80, 0)]), send(2, &[outpoint(1, 0)])], - ); - - rig.send(Command::Uncertain(txid(2))).await; - - assert_eq!(probe.probed(), vec![txid(1), txid(2)]); - } - /// An `Uncertain` lookup that lands after its wallet was removed does not /// bring the wallet's entry back. #[tokio::test] From ebe148253b0d9890d68ccaa60d9a7cd5c88f515f Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:17:53 +0300 Subject: [PATCH 040/169] fix(platform-wallet): thirty-fourth review pass on the broadcast resolver - The wallet manager inserts and removes wallets in its inner manager only through insert_into_inner / remove_from_inner, which notify the broadcast resolver under the same write guard: a new path cannot forget the notification or get its order wrong. - The read-gone fail-safe leaves the wallet idle instead of re-reading (and re-logging) it every block; its test checks that. - Docs: an Uncertain report forces a probe of its chain's current root in the next pass (reports queued together share it); the sends built on a root found mined become roots due at the next pass. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/manager/load.rs | 11 ++-- .../src/manager/wallet_lifecycle.rs | 60 ++++++++++++------- .../src/wallet/core/broadcast_resolver.rs | 27 ++++++--- 3 files changed, 62 insertions(+), 36 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 3bcfde948a3..7f3ba96605b 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -270,15 +270,13 @@ impl PlatformWalletManager

{ if wm.get_wallet(&wallet_id).is_some() { continue 'load; } - if let Err(e) = wm.insert_wallet(wallet, platform_info) { + if let Err(e) = self.insert_into_inner(&mut wm, wallet, platform_info) { load_error = Some(PlatformWalletError::WalletCreation(format!( "Failed to register persisted wallet in WalletManager: {}", e ))); break 'load; } - // Under the insert's guard: before any of the wallet's events. - self.broadcast_resolver.wallet_added(&wallet_id); } inserted_in_manager.push(wallet_id); @@ -467,13 +465,12 @@ impl PlatformWalletManager

{ } continue; } - match wm.remove_wallet(id) { - Ok(_) => self.broadcast_resolver.wallet_removed(id), - Err(e) => tracing::warn!( + if let Err(e) = self.remove_from_inner(&mut wm, id) { + tracing::warn!( wallet_id = %hex::encode(id), error = %e, "rollback after load failure: remove_wallet failed" - ), + ); } } } diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 05e762b6c9b..df30ebc8eb1 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -8,6 +8,7 @@ use key_wallet::wallet::initialization::WalletAccountCreationOptions; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; use key_wallet::wallet::Wallet; use key_wallet::Network; +use key_wallet_manager::{WalletError, WalletManager}; #[cfg(any(feature = "bls", feature = "eddsa"))] use crate::changeset::ProviderKeyExtendedPubKey; @@ -71,6 +72,34 @@ pub(crate) static REMOVE_WALLET_MIDPOINT_HOOK: std::sync::Mutex PlatformWalletManager

{ + /// Insert into the inner wallet manager and tell the broadcast resolver, + /// under the caller's write guard — the one way a wallet enters it, so the + /// resolver hears of every wallet before its events and in order with + /// removals. + pub(super) fn insert_into_inner( + &self, + wm: &mut WalletManager, + wallet: Wallet, + info: PlatformWalletInfo, + ) -> Result { + let wallet_id = wm.insert_wallet(wallet, info)?; + self.broadcast_resolver.wallet_added(&wallet_id); + Ok(wallet_id) + } + + /// Remove from the inner wallet manager and tell the broadcast resolver, + /// under the caller's write guard (see + /// [`insert_into_inner`](Self::insert_into_inner)). + pub(super) fn remove_from_inner( + &self, + wm: &mut WalletManager, + wallet_id: &WalletId, + ) -> Result<(), WalletError> { + wm.remove_wallet(wallet_id)?; + self.broadcast_resolver.wallet_removed(wallet_id); + Ok(()) + } + /// Create a PlatformWallet from a BIP39 mnemonic phrase. /// /// The mnemonic's language is auto-detected by trying each @@ -438,8 +467,8 @@ impl PlatformWalletManager

{ // stays `WalletCreation`. let wallet_id = { let mut wm = self.wallet_manager.write().await; - let wallet_id = wm - .insert_wallet(wallet, platform_info) + let wallet_id = self + .insert_into_inner(&mut wm, wallet, platform_info) .map_err(|e| match e { key_wallet_manager::WalletError::WalletExists(id) => already_registered(id), other => PlatformWalletError::WalletCreation(format!( @@ -447,9 +476,6 @@ impl PlatformWalletManager

{ other )), })?; - // Under the same guard as the insert: the resolver hears of the - // wallet before any of its events, and in order with a removal. - self.broadcast_resolver.wallet_added(&wallet_id); wallet_id }; @@ -548,13 +574,12 @@ impl PlatformWalletManager

{ "failed to persist wallet registration changeset" ); let mut wm = self.wallet_manager.write().await; - match wm.remove_wallet(&wallet_id) { - Ok(_) => self.broadcast_resolver.wallet_removed(&wallet_id), - Err(remove_err) => tracing::warn!( + if let Err(remove_err) = self.remove_from_inner(&mut wm, &wallet_id) { + tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %remove_err, "rollback: remove_wallet failed while unwinding a failed wallet registration" - ), + ); } return Err(PlatformWalletError::from_store_failure(&*self.persister, e)); } @@ -597,13 +622,12 @@ impl PlatformWalletManager

{ "failed to restore persisted platform-address state" ); let mut wm = self.wallet_manager.write().await; - match wm.remove_wallet(&wallet_id) { - Ok(_) => self.broadcast_resolver.wallet_removed(&wallet_id), - Err(remove_err) => tracing::warn!( + if let Err(remove_err) = self.remove_from_inner(&mut wm, &wallet_id) { + tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %remove_err, "rollback: remove_wallet failed while unwinding a failed wallet setup" - ), + ); } // Wrap the already-typed error rather than stringify it, so // its concrete variant and source chain survive. @@ -884,19 +908,15 @@ impl PlatformWalletManager

{ their identity-sync rows are left for the next launch to reconcile" ); } - if let Err(e) = wm.remove_wallet(wallet_id) { + // The resolver drops the wallet's broadcast-probe state too, and + // the host is told to drop the verdicts it shows for its sends. + if let Err(e) = self.remove_from_inner(&mut wm, wallet_id) { tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %e, "remove_wallet: inner wallet-manager removal failed (state may be inconsistent)" ); } - // Broadcast-probe state for the wallet's sends goes too, and the - // host is told to drop the verdicts it shows for them (from the - // resolver's task). After the inner removal, so no later pass can - // read the wallet and rebuild that state; under the same guard, so - // it is queued ahead of a same-id re-registration's WalletAdded. - self.broadcast_resolver.wallet_removed(wallet_id); } // Test-only rendezvous: the window a concurrent same-id registration can diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index b82606fd082..5423c4c88da 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -23,7 +23,8 @@ //! dead root also takes every own send built on it (spending a dead //! transaction is dead too), including sends built on it after it was found //! dead. A root found in a block that the wallet has not -//! seen yet counts as settled, so the sends built on it are probed next. +//! seen yet counts as settled, so the sends built on it become roots, due at +//! the next pass. //! Only changes are published — Unresolved never replaces a decided verdict, //! and Accepted and Mined count as the same. The host is told to drop a //! send's verdict (cleared) when the send settles or leaves the wallet, when @@ -42,8 +43,9 @@ //! forced) — a root is probed every block for [`EVERY_BLOCK_WINDOW`] blocks, //! counted from the first height-driven pass that sees it while the wallet //! follows the tip (a forced pass does not start it), and every -//! [`SLOW_INTERVAL`] blocks after. Each Uncertain report adds one immediate -//! probe. +//! [`SLOW_INTERVAL`] blocks after. Each Uncertain report forces a probe of +//! its chain's current root in the next pass (reports queued together share +//! it). //! Between passes a wallet waits for the height at which its next root is due; //! one whose last pass had nothing left to probe is idle and not scanned //! again until a wallet event that touched its records (a new or @@ -660,7 +662,8 @@ pub(crate) struct PassStart { /// it `Uncertain`) are due regardless of their schedule — even if one went /// out at this height already: a repeat probe is cheaper than bookkeeping /// that tells the two apart. A root found mined counts as settled when -/// picking roots, so its children are probed next. `anchor`: the tip height +/// picking roots, so its children become roots, due at the next pass. +/// `anchor`: the tip height /// a height-driven pass knows the wallet follows — it starts the every-block /// window of every root that has none yet. pub(crate) fn begin_pass( @@ -1471,11 +1474,11 @@ impl Actor { wallet_id = %hex::encode(wallet_id), "broadcast probe: a registered wallet read as gone" ); + // Idle, not re-read every block: a same-id wallet's + // events or an Uncertain wake it. + entry.wait_until = Some(u32::MAX); let events = self.state.forget_wallet(&wallet_id); deliver(&self.sink, events); - if let Some(entry) = self.wallets.get_mut(&wallet_id) { - entry.wait_until = None; - } self.end_run(wallet_id); } Some(None) => self.end_run(wallet_id), @@ -2968,8 +2971,9 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1)]); } - /// A wallet the manager dropped without `wallet_removed` (a rolled-back - /// registration or load): the pass that finds it gone clears its verdicts. + /// The fail-safe for a registered wallet read as gone (every removal path + /// notifies the resolver, so this cannot happen today): its verdicts are + /// cleared and it goes idle rather than being re-read every block. #[tokio::test] async fn should_clear_the_verdicts_of_a_wallet_gone_without_a_removal() { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())]))).await; @@ -2982,6 +2986,11 @@ mod tests { assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); assert!(rig.actor.state.schedules.is_empty()); + assert_eq!( + rig.actor.wallets[&wallet()].wait_until, + Some(u32::MAX), + "idle: not re-read on the next block" + ); } /// An `Uncertain` result forces its root even if a height pass sent it at From 5eb00b602888ac557d87cff44c29ced9d93dbd08 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:28:27 +0300 Subject: [PATCH 041/169] fix(platform-wallet): thirty-fifth review pass on the broadcast resolver - remove_from_inner tells the resolver the wallet is gone whatever the inner manager answered (it can only fail for an id it no longer holds), as remove_wallet did before the helpers: a failed or partial inner removal no longer leaves stale verdicts that a same-id restore would never see re-published. - WalletAdded starts the wallet fresh: an existing entry (idle wait, stale height) and any state kept under the id are dropped, the host told to clear what it showed. - A rewind also forgets roots a probe found mined, so a block the replay drops cannot leave a root showing "going through" for good. - Docs name the one bypass (writing through wallet_manager_arc) instead of calling the read-gone fail-safe impossible; the register path returns the helper's result directly. - Tests (each fails without its fix): a re-registered wallet starts fresh; a rewind forgets probe-found mined roots. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/manager/wallet_lifecycle.rs | 22 +++--- .../src/wallet/core/broadcast_resolver.rs | 73 +++++++++++++++---- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index df30ebc8eb1..115f5a9071d 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -73,9 +73,11 @@ pub(crate) static REMOVE_WALLET_MIDPOINT_HOOK: std::sync::Mutex PlatformWalletManager

{ /// Insert into the inner wallet manager and tell the broadcast resolver, - /// under the caller's write guard — the one way a wallet enters it, so the - /// resolver hears of every wallet before its events and in order with - /// removals. + /// under the caller's write guard — the way this manager adds wallets, so + /// the resolver hears of every wallet before its events and in order with + /// removals. (An embedder writing through `wallet_manager_arc` bypasses + /// it; the resolver then ignores that wallet, or fails safe on its + /// removal.) pub(super) fn insert_into_inner( &self, wm: &mut WalletManager, @@ -95,9 +97,11 @@ impl PlatformWalletManager

{ wm: &mut WalletManager, wallet_id: &WalletId, ) -> Result<(), WalletError> { - wm.remove_wallet(wallet_id)?; + let removed = wm.remove_wallet(wallet_id).map(|_| ()); + // Whatever the inner manager said — it can only fail for an id it no + // longer holds — the wallet is gone: the resolver forgets it. self.broadcast_resolver.wallet_removed(wallet_id); - Ok(()) + removed } /// Create a PlatformWallet from a BIP39 mnemonic phrase. @@ -467,16 +471,14 @@ impl PlatformWalletManager

{ // stays `WalletCreation`. let wallet_id = { let mut wm = self.wallet_manager.write().await; - let wallet_id = self - .insert_into_inner(&mut wm, wallet, platform_info) + self.insert_into_inner(&mut wm, wallet, platform_info) .map_err(|e| match e { - key_wallet_manager::WalletError::WalletExists(id) => already_registered(id), + WalletError::WalletExists(id) => already_registered(id), other => PlatformWalletError::WalletCreation(format!( "Failed to register wallet in WalletManager: {}", other )), - })?; - wallet_id + })? }; // `insert_wallet` recomputes the id from the (now external-signable) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 5423c4c88da..c53808e08eb 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -613,6 +613,10 @@ impl ResolverState { *schedule = ProbeSchedule::default(); } } + // A block a probe saw but the wallet never did may be one the replay + // drops: roots found mined are asked again. + self.finished + .retain(|(wallet, _), result| wallet != wallet_id || *result != Final::Mined); } /// Roots of `wallet_id` a probe found in a block the wallet has not seen. @@ -1228,11 +1232,6 @@ impl Actor { } } - /// The wallet's entry, created on first sight. - fn entry(&mut self, wallet_id: WalletId) -> &mut WalletEntry { - self.wallets.entry(wallet_id).or_default() - } - fn handle(&mut self, command: Command) { match command { Command::Height { wallet_id, height } => { @@ -1379,7 +1378,14 @@ impl Actor { } } Command::WalletAdded(wallet_id) => { - self.entry(wallet_id); + // A fresh start: whatever was kept under this id (a same-id + // wallet whose removal never reached the resolver) is stale. + if let Some(run) = self.wallets.remove(&wallet_id).and_then(|entry| entry.run) { + run.abort.abort(); + } + self.wallets.insert(wallet_id, WalletEntry::default()); + let events = self.state.forget_wallet(&wallet_id); + deliver(&self.sink, events); } Command::WalletRemoved(wallet_id) => { if let Some(run) = self.wallets.remove(&wallet_id).and_then(|entry| entry.run) { @@ -1466,10 +1472,11 @@ impl Actor { } match read { None => { - // Gone from the wallet manager. Cannot happen: its - // WalletRemoved is queued under the same guard that - // removed it, and commands are handled before job - // results. Fail safe: forget its state and end the run. + // Gone from the wallet manager. This manager's own + // removals queue WalletRemoved under the same guard, + // and commands are handled before job results, so only + // a removal through `wallet_manager_arc` gets here. + // Fail safe: forget its state and end the run. tracing::error!( wallet_id = %hex::encode(wallet_id), "broadcast probe: a registered wallet read as gone" @@ -2971,9 +2978,10 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1)]); } - /// The fail-safe for a registered wallet read as gone (every removal path - /// notifies the resolver, so this cannot happen today): its verdicts are - /// cleared and it goes idle rather than being re-read every block. + /// The fail-safe for a registered wallet read as gone (removed by an + /// embedder through `wallet_manager_arc`, bypassing the notification): its + /// verdicts are cleared and it goes idle rather than being re-read every + /// block. #[tokio::test] async fn should_clear_the_verdicts_of_a_wallet_gone_without_a_removal() { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())]))).await; @@ -4053,6 +4061,45 @@ mod tests { assert!(!rig.actor.wallets.contains_key(&wallet())); } + /// A same-id wallet registered again starts fresh: a stale idle wait and + /// stale verdicts from before do not carry over. + #[tokio::test] + async fn should_start_a_re_registered_wallet_fresh() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(1), dead())]); + assert_eq!(rig.actor.wallets[&wallet()].wait_until, Some(u32::MAX)); + + rig.send(Command::WalletAdded(wallet())).await; + + assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); + let entry = &rig.actor.wallets[&wallet()]; + assert_eq!(entry.wait_until, None); + assert_eq!(entry.height, None); + } + + /// A rewind asks again about roots a probe found mined: the block it saw + /// may be one the replay drops. + #[tokio::test] + async fn should_forget_probe_found_mined_roots_on_a_rewind() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[( + txid(1), + ProbeVerdict::Mined, + )]))) + .await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + assert_eq!( + rig.actor.state.finished.get(&(wallet(), txid(1))), + Some(&Final::Mined) + ); + + rig.height(wallet(), 50).await; + + assert!(!rig.actor.state.finished.contains_key(&(wallet(), txid(1)))); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From 21bcccd7b878940e891434c078cbd24bd19fab12 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:40:07 +0300 Subject: [PATCH 042/169] fix(platform-wallet): thirty-sixth review pass on the broadcast resolver - A probe-found Mined carries the probe height and expires: a root the wallet still has not seen in a block MINED_GRACE (6) blocks later is asked again, and a wallet whose roots are all Mined waits for exactly that height instead of going idle for good. A reorged-out or stale-fork block never rewinds the wallet, so the rewind hook alone could not catch it. - A probe sent before a rewind still has its answer told, but records no final verdict (record_final = run.marks), so it cannot undo the rewind's forgetting. - One drop_wallet helper for WalletAdded (fresh start) and WalletRemoved; forget_wallet takes the log trigger, so a re-registration's clears are logged "re-registered" and the fail-safe's "gone", not "removed". - tokio's Handle and std::mem are imported rather than called by full path. - Tests (each fails without its fix): a never-seen Mined root is asked again after the grace; a pre-rewind probe records no final verdict. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/wallet/core/broadcast_resolver.rs | 156 ++++++++++++++---- 1 file changed, 124 insertions(+), 32 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index c53808e08eb..14ee865b1c1 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -64,6 +64,7 @@ //! from a caller that may hold its own locks. use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; +use std::mem; use std::panic::{catch_unwind, AssertUnwindSafe}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, PoisonError, Weak}; @@ -78,6 +79,7 @@ use key_wallet::managed_account::transaction_record::TransactionRecord; use key_wallet::transaction_checking::TransactionContext; use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use key_wallet_manager::{WalletEvent, WalletId, WalletManager}; +use tokio::runtime::Handle; use tokio::sync::{mpsc, oneshot, Mutex as AsyncMutex, RwLock}; use tokio::task::{self, AbortHandle, JoinError, JoinHandle, JoinSet}; use tokio::time::{timeout_at, Instant}; @@ -99,6 +101,11 @@ pub(crate) const SLOW_INTERVAL: u32 = 10; /// height-driven pass waits for it. pub(crate) const CATCH_UP_STEP: u32 = 3; +/// Blocks a root a probe found mined may stay unseen by the wallet before it +/// is asked again: the block a probe saw can be reorged out, or come from a +/// node on a stale fork, and the wallet's height never goes back for that. +pub(crate) const MINED_GRACE: u32 = 6; + /// One unsettled transaction of the wallet, merged across the accounts that /// record it. #[derive(Debug, Clone, PartialEq, Eq)] @@ -368,8 +375,13 @@ impl ProbeSchedule { /// A root's final verdict: nothing is left to ask. #[derive(Debug, Clone, PartialEq, Eq)] enum Final { - Dead { reason: String }, - Mined, + Dead { + reason: String, + }, + /// Found in a block by a probe at height `at`, before the wallet saw it. + Mined { + at: u32, + }, } /// Whether two verdicts say the same thing to the host. Accepted and Mined @@ -437,10 +449,10 @@ pub(crate) struct ResolverState { impl ResolverState { /// Drop everything kept for `wallet_id`; a clear for every send the host /// had a verdict for. - fn forget_wallet(&mut self, wallet_id: &WalletId) -> Vec { + fn forget_wallet(&mut self, wallet_id: &WalletId, trigger: &'static str) -> Vec { self.schedules.retain(|(wallet, _), _| wallet != wallet_id); self.finished.retain(|(wallet, _), _| wallet != wallet_id); - self.clear_where(|wallet, _| wallet == wallet_id, None, "removed") + self.clear_where(|wallet, _| wallet == wallet_id, None, trigger) } /// Drop everything; a clear for every published send. @@ -615,15 +627,18 @@ impl ResolverState { } // A block a probe saw but the wallet never did may be one the replay // drops: roots found mined are asked again. - self.finished - .retain(|(wallet, _), result| wallet != wallet_id || *result != Final::Mined); + self.finished.retain(|(wallet, _), result| { + wallet != wallet_id || !matches!(result, Final::Mined { .. }) + }); } /// Roots of `wallet_id` a probe found in a block the wallet has not seen. fn mined(&self, wallet_id: &WalletId) -> HashSet { self.finished .iter() - .filter(|((wallet, _), result)| wallet == wallet_id && **result == Final::Mined) + .filter(|((wallet, _), result)| { + wallet == wallet_id && matches!(result, Final::Mined { .. }) + }) .map(|((_, txid), _)| *txid) .collect() } @@ -702,6 +717,12 @@ pub(crate) fn begin_pass( )); events.extend(state.clear_orphaned_dead(wallet_id, Some(height), "settled-or-left")); + // A root found mined that the wallet still has not seen in a block well + // after the probe: the block may be gone — ask again. + state.finished.retain(|(wallet, _), result| { + *wallet != wallet_id + || !matches!(result, Final::Mined { at } if height > at.saturating_add(MINED_GRACE)) + }); let mined = state.mined(&wallet_id); let graph = ChainGraph::new(views, &mined); let roots = graph.roots(); @@ -779,11 +800,14 @@ pub(crate) fn record_probe( views: &[OutgoingView], root: &DueRoot, verdict: &ProbeVerdict, + record_final: bool, ) -> Vec { let mut events = Vec::new(); let key = (wallet_id, root.txid); + // `record_final` is off for a probe sent before a rewind: its answer is + // still told, but it does not end the root's probing across the replay. match verdict { - ProbeVerdict::Dead { reason } => { + ProbeVerdict::Dead { reason } if record_final => { state.finished.insert( key, Final::Dead { @@ -791,10 +815,10 @@ pub(crate) fn record_probe( }, ); } - ProbeVerdict::Mined => { - state.finished.insert(key, Final::Mined); + ProbeVerdict::Mined if record_final => { + state.finished.insert(key, Final::Mined { at: height }); } - ProbeVerdict::Accepted | ProbeVerdict::Unresolved { .. } => {} + _ => {} } let trigger = trigger_of(root.forced); if matches!(verdict, ProbeVerdict::Dead { .. }) { @@ -823,13 +847,24 @@ pub(crate) fn record_probe( /// The lowest height at which a root of the wallet can be due, if nothing new /// happens to it: a root without a schedule yet (the child of one just found -/// mined) is due at once; `u32::MAX` when every root has a final verdict -/// (nothing left to probe) or there are none. +/// mined) is due at once; a root a probe found mined, once its grace is up +/// (see [`MINED_GRACE`]); `u32::MAX` when every root is Dead or there are +/// none (nothing left to probe). pub(crate) fn next_pass_height( state: &ResolverState, wallet_id: WalletId, views: &[OutgoingView], ) -> u32 { + let mined_expiry = state + .finished + .iter() + .filter_map(|((wallet, _), result)| match result { + Final::Mined { at } if *wallet == wallet_id => { + Some(at.saturating_add(MINED_GRACE).saturating_add(1)) + } + _ => None, + }) + .filter(|_| !views.is_empty()); let mined = state.mined(&wallet_id); ChainGraph::new(views, &mined) .roots() @@ -837,6 +872,7 @@ pub(crate) fn next_pass_height( .map(|view| (wallet_id, view.txid)) .filter(|key| !state.finished.contains_key(key)) .map(|key| state.schedules.get(&key).map_or(0, ProbeSchedule::next_due)) + .chain(mined_expiry) .min() .unwrap_or(u32::MAX) } @@ -1380,23 +1416,23 @@ impl Actor { Command::WalletAdded(wallet_id) => { // A fresh start: whatever was kept under this id (a same-id // wallet whose removal never reached the resolver) is stale. - if let Some(run) = self.wallets.remove(&wallet_id).and_then(|entry| entry.run) { - run.abort.abort(); - } + self.drop_wallet(&wallet_id, "re-registered"); self.wallets.insert(wallet_id, WalletEntry::default()); - let events = self.state.forget_wallet(&wallet_id); - deliver(&self.sink, events); - } - Command::WalletRemoved(wallet_id) => { - if let Some(run) = self.wallets.remove(&wallet_id).and_then(|entry| entry.run) { - run.abort.abort(); - } - let events = self.state.forget_wallet(&wallet_id); - deliver(&self.sink, events); } + Command::WalletRemoved(wallet_id) => self.drop_wallet(&wallet_id, "removed"), } } + /// Drop the wallet's entry, stop its pass and forget its state, the host + /// told to clear what it showed (logged with `trigger`). + fn drop_wallet(&mut self, wallet_id: &WalletId, trigger: &'static str) { + if let Some(run) = self.wallets.remove(wallet_id).and_then(|entry| entry.run) { + run.abort.abort(); + } + let events = self.state.forget_wallet(wallet_id, trigger); + deliver(&self.sink, events); + } + /// Start the wallet's pending pass unless one is already running. fn start(&mut self, wallet_id: WalletId) { let Some(entry) = self.wallets.get_mut(&wallet_id) else { @@ -1484,7 +1520,7 @@ impl Actor { // Idle, not re-read every block: a same-id wallet's // events or an Uncertain wake it. entry.wait_until = Some(u32::MAX); - let events = self.state.forget_wallet(&wallet_id); + let events = self.state.forget_wallet(&wallet_id, "gone"); deliver(&self.sink, events); self.end_run(wallet_id); } @@ -1592,6 +1628,7 @@ impl Actor { &probing.views, &root, &verdict, + current.marks, ); deliver(&self.sink, events); self.probe_next(wallet_id); @@ -1724,13 +1761,13 @@ impl BroadcastResolver { if self.started.load(Ordering::Acquire) { return; } - let Ok(runtime) = tokio::runtime::Handle::try_current() else { + let Ok(runtime) = Handle::try_current() else { return; // queued; started by the first command sent from a runtime }; let mut slot = self.actor.lock().unwrap_or_else(PoisonError::into_inner); if matches!(*slot, ActorSlot::NotStarted(..)) { if let ActorSlot::NotStarted(actor, receiver) = - std::mem::replace(&mut *slot, ActorSlot::Closed) + mem::replace(&mut *slot, ActorSlot::Closed) { let (stop, stopped) = oneshot::channel(); *slot = ActorSlot::Running(runtime.spawn(actor.run(receiver, stopped)), stop); @@ -1793,7 +1830,7 @@ async fn stop_slot( let Ok(_one_at_a_time) = timeout_at(deadline, stopping.lock()).await else { return WorkerStatus::Timeout; }; - let slot = std::mem::replace( + let slot = mem::replace( &mut *actor.lock().unwrap_or_else(PoisonError::into_inner), ActorSlot::Closed, ); @@ -2207,7 +2244,7 @@ mod tests { } fn forget_wallet(&mut self, wallet_id: &WalletId) { - let events = self.state.forget_wallet(wallet_id); + let events = self.state.forget_wallet(wallet_id, "removed"); self.push(events); } @@ -2260,7 +2297,15 @@ mod tests { ); let verdict = probe.probe(&root.transaction).await; let mut guard = state.lock().expect("state"); - let events = record_probe(&mut guard.state, wallet_id, height, views, &root, &verdict); + let events = record_probe( + &mut guard.state, + wallet_id, + height, + views, + &root, + &verdict, + true, + ); guard.push(events); } let guard = state.lock().expect("state"); @@ -4092,7 +4137,7 @@ mod tests { rig.follow(wallet(), 100).await; assert_eq!( rig.actor.state.finished.get(&(wallet(), txid(1))), - Some(&Final::Mined) + Some(&Final::Mined { at: 101 }) ); rig.height(wallet(), 50).await; @@ -4100,6 +4145,53 @@ mod tests { assert!(!rig.actor.state.finished.contains_key(&(wallet(), txid(1)))); } + /// A root a probe found mined that the wallet still has not seen in a block + /// MINED_GRACE blocks later is asked again. + #[tokio::test] + async fn should_ask_again_about_a_probe_found_mined_root_the_wallet_never_saw() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; // found mined at 101 + for height in 102..=101 + MINED_GRACE { + rig.height(wallet(), height).await; + } + assert_eq!(probe.probed().len(), 1); + + // Past the grace, the wallet — waiting for exactly that height — asks + // again. + rig.height(wallet(), 102 + MINED_GRACE).await; + + assert_eq!(probe.probed().len(), 2); + } + + /// A probe sent before a rewind answers Mined after it: the answer is told + /// but does not end the root's probing across the replay. + #[tokio::test] + async fn should_not_record_a_final_verdict_from_a_probe_sent_before_a_rewind() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[( + txid(1), + ProbeVerdict::Mined, + )]))) + .await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + rig.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // root 1's probe is out + + rig.handle(Command::Height { + wallet_id: wallet(), + height: 50, + }); + rig.settle().await; + + assert!(!rig.actor.state.finished.contains_key(&(wallet(), txid(1)))); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, From fedad58aa6fcd4e69ea17d57633a70187dc7998c Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:49:56 +0300 Subject: [PATCH 043/169] fix(platform-wallet): Mined takes two nodes; thirty-seventh review pass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - A probe calls a transaction mined only when two distinct nodes report it in a block (MINED_QUORUM, by submission or lookup), as a Dead verdict already needed two. A single such answer — one node on a stale fork, or with a block about to be reorged out — counts as accepted: it exists, not that it is final. This fixes the false-Mined evidence at its source. - With that, the MINED_GRACE expiry of the thirty-sixth pass goes: Mined is final again, with no wasted passes after an expiry and no endless re-probing of a root the wallet never records as settled. - A rewind drops every final verdict of the wallet, Dead included: a root may have become one only because its parent counted as mined. - A Dead from a probe sent before a rewind is told for the root only; it is not spread to the chain, whose inherited Dead would hang on a root that is not recorded dead. - Tests use imported tokio items rather than full paths. - Tests (each fails without its fix): Mined needs two distinct nodes; a pre-rewind Dead reaches the root only. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/broadcast_probe.rs | 86 +++++++++++-- .../src/wallet/core/broadcast_resolver.rs | 115 ++++++++---------- 2 files changed, 128 insertions(+), 73 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index 895fad7ecff..1801819e98e 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -111,8 +111,8 @@ pub enum ProbeVerdict { /// A node holds the transaction in its mempool. Not settlement: it can /// still expire or lose to a conflict, so the resolver keeps asking. Accepted, - /// A node has the transaction in a block. Final — there is nothing left to - /// ask, even if this wallet has not seen that block yet. + /// Two different nodes have the transaction in a block. Final — there is + /// nothing left to ask, even if this wallet has not seen that block yet. Mined, /// Two different nodes refused it for a reason in [`DEAD_REASONS`] /// (missing or spent inputs, or a conflict with an InstantSend-locked @@ -158,6 +158,13 @@ const MAX_LOOKUPS_PER_PROBE: usize = 4; /// opinion is one more request. const DEAD_QUORUM: usize = 2; +/// How many *distinct* nodes must report a transaction in a block before it +/// counts as mined — final, never asked about again, its children probed in +/// its place. One node on a stale fork, or one whose block is about to be +/// reorged out, would otherwise settle it on false evidence. A single such +/// answer still shows the transaction exists: it counts as accepted. +const MINED_QUORUM: usize = 2; + /// Resubmits a transaction whose broadcast outcome is unknown and turns the /// nodes' answers into a verdict. /// @@ -180,6 +187,7 @@ pub(crate) async fn probe_with( ) -> ProbeVerdict { let mut dead_nodes: HashSet = HashSet::new(); let mut dead_reason: Option = None; + let mut mined = MinedEvidence::default(); let mut last_other = String::from("no submission was answered"); for _ in 0..MAX_SUBMISSIONS_PER_PROBE { @@ -192,7 +200,11 @@ pub(crate) async fn probe_with( ); match verdict { NodeVerdict::Accepted => return ProbeVerdict::Accepted, - NodeVerdict::Mined => return ProbeVerdict::Mined, + NodeVerdict::Mined => { + if mined.add(node) { + return ProbeVerdict::Mined; + } + } NodeVerdict::Dead { reason } => { // A dead verdict from an unidentified node cannot count // towards the quorum — it might be the node we already heard. @@ -205,6 +217,7 @@ pub(crate) async fn probe_with( submitter, &transaction.txid(), dead_reason.unwrap_or_default(), + mined, ) .await; } @@ -215,6 +228,9 @@ pub(crate) async fn probe_with( } } + if mined.seen { + return ProbeVerdict::Accepted; + } ProbeVerdict::Unresolved { reason: match dead_reason { Some(reason) => format!( @@ -226,11 +242,36 @@ pub(crate) async fn probe_with( } } +/// Nodes that reported the transaction in a block, by submission or lookup. +#[derive(Default)] +struct MinedEvidence { + nodes: HashSet, + /// Any node said so, identified or not: the transaction exists. + seen: bool, +} + +impl MinedEvidence { + /// Count one report; whether the quorum is reached. An unidentified node + /// cannot count — it might be one already heard. + fn add(&mut self, node: Option) -> bool { + self.seen = true; + if let Some(node) = node { + self.nodes.insert(node); + } + self.nodes.len() >= MINED_QUORUM + } +} + /// A rejection for missing or spent inputs is also what a *mined* transaction /// whose outputs are all spent gets (see the module docs). Ask nodes whether /// they know the txid: one that does settles it as accepted; only /// [`DEAD_QUORUM`] distinct nodes not knowing it confirm the dead verdict. -async fn confirm_dead(submitter: &dyn NodeSubmitter, txid: &Txid, reason: String) -> ProbeVerdict { +async fn confirm_dead( + submitter: &dyn NodeSubmitter, + txid: &Txid, + reason: String, + mut mined: MinedEvidence, +) -> ProbeVerdict { let mut not_found: HashSet = HashSet::new(); let mut last_unknown = String::from("no lookup was answered"); for _ in 0..MAX_LOOKUPS_PER_PROBE { @@ -242,7 +283,11 @@ async fn confirm_dead(submitter: &dyn NodeSubmitter, txid: &Txid, reason: String "broadcast probe: txid lookup answered" ); match answer { - LookupAnswer::Known { mined: true } => return ProbeVerdict::Mined, + LookupAnswer::Known { mined: true } => { + if mined.add(node) { + return ProbeVerdict::Mined; + } + } LookupAnswer::Known { mined: false } => return ProbeVerdict::Accepted, LookupAnswer::NotFound => { if let Some(node) = node { @@ -255,6 +300,9 @@ async fn confirm_dead(submitter: &dyn NodeSubmitter, txid: &Txid, reason: String LookupAnswer::Unknown { reason } => last_unknown = reason, } } + if mined.seen { + return ProbeVerdict::Accepted; + } ProbeVerdict::Unresolved { reason: format!( "refused as {reason}, but only {} of {DEAD_QUORUM} nodes confirmed the txid is unknown; last lookup answer: {last_unknown}", @@ -628,14 +676,38 @@ mod tests { async fn should_report_mined_for_a_refused_transaction_a_node_has_in_a_block() { let nodes = ScriptedNodes::with_lookups( vec![(Some("a"), dead()), (Some("b"), dead())], - vec![(Some("c"), LookupAnswer::Known { mined: true })], + vec![ + (Some("c"), LookupAnswer::Known { mined: true }), + (Some("d"), LookupAnswer::Known { mined: true }), + ], ); assert_eq!( probe_with(&nodes, &transaction()).await, ProbeVerdict::Mined ); - assert_eq!(nodes.lookups_made(), 1); + assert_eq!(nodes.lookups_made(), 2); + } + + /// One node on a stale fork (or with a block about to be reorged out) + /// must not settle a transaction as mined: two distinct nodes must. + #[tokio::test] + async fn should_report_mined_only_when_two_distinct_nodes_have_it_in_a_block() { + let two = ScriptedNodes::new(vec![ + (Some("a"), NodeVerdict::Mined), + (Some("b"), NodeVerdict::Mined), + ]); + let same_twice = ScriptedNodes::new(vec![ + (Some("a"), NodeVerdict::Mined), + (Some("a"), NodeVerdict::Mined), + ]); + + assert_eq!(probe_with(&two, &transaction()).await, ProbeVerdict::Mined); + assert_eq!( + probe_with(&same_twice, &transaction()).await, + ProbeVerdict::Accepted, + "one node's word: it exists, not that it is final" + ); } #[tokio::test] diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 14ee865b1c1..064b9f6e56d 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -101,11 +101,6 @@ pub(crate) const SLOW_INTERVAL: u32 = 10; /// height-driven pass waits for it. pub(crate) const CATCH_UP_STEP: u32 = 3; -/// Blocks a root a probe found mined may stay unseen by the wallet before it -/// is asked again: the block a probe saw can be reorged out, or come from a -/// node on a stale fork, and the wallet's height never goes back for that. -pub(crate) const MINED_GRACE: u32 = 6; - /// One unsettled transaction of the wallet, merged across the accounts that /// record it. #[derive(Debug, Clone, PartialEq, Eq)] @@ -378,10 +373,8 @@ enum Final { Dead { reason: String, }, - /// Found in a block by a probe at height `at`, before the wallet saw it. - Mined { - at: u32, - }, + /// Found in a block by a probe (two nodes), before the wallet saw it. + Mined, } /// Whether two verdicts say the same thing to the host. Accepted and Mined @@ -625,20 +618,17 @@ impl ResolverState { *schedule = ProbeSchedule::default(); } } - // A block a probe saw but the wallet never did may be one the replay - // drops: roots found mined are asked again. - self.finished.retain(|(wallet, _), result| { - wallet != wallet_id || !matches!(result, Final::Mined { .. }) - }); + // The replay may drop a block a probe saw: roots found mined are + // asked again, and so are roots found dead — one may have become a + // root only because its parent counted as mined. + self.finished.retain(|(wallet, _), _| wallet != wallet_id); } /// Roots of `wallet_id` a probe found in a block the wallet has not seen. fn mined(&self, wallet_id: &WalletId) -> HashSet { self.finished .iter() - .filter(|((wallet, _), result)| { - wallet == wallet_id && matches!(result, Final::Mined { .. }) - }) + .filter(|((wallet, _), result)| wallet == wallet_id && matches!(result, Final::Mined)) .map(|((_, txid), _)| *txid) .collect() } @@ -717,12 +707,6 @@ pub(crate) fn begin_pass( )); events.extend(state.clear_orphaned_dead(wallet_id, Some(height), "settled-or-left")); - // A root found mined that the wallet still has not seen in a block well - // after the probe: the block may be gone — ask again. - state.finished.retain(|(wallet, _), result| { - *wallet != wallet_id - || !matches!(result, Final::Mined { at } if height > at.saturating_add(MINED_GRACE)) - }); let mined = state.mined(&wallet_id); let graph = ChainGraph::new(views, &mined); let roots = graph.roots(); @@ -816,12 +800,14 @@ pub(crate) fn record_probe( ); } ProbeVerdict::Mined if record_final => { - state.finished.insert(key, Final::Mined { at: height }); + state.finished.insert(key, Final::Mined); } _ => {} } let trigger = trigger_of(root.forced); - if matches!(verdict, ProbeVerdict::Dead { .. }) { + // Spread a Dead down the chain only when it is recorded: a Dead the chain + // inherits needs a root known dead to hang on (see `dead_from`). + if matches!(verdict, ProbeVerdict::Dead { .. }) && record_final { let sends = ChainGraph::new(views, &state.mined(&wallet_id)).own_in_chain(&root.txid); state.publish_dead( wallet_id, @@ -847,24 +833,13 @@ pub(crate) fn record_probe( /// The lowest height at which a root of the wallet can be due, if nothing new /// happens to it: a root without a schedule yet (the child of one just found -/// mined) is due at once; a root a probe found mined, once its grace is up -/// (see [`MINED_GRACE`]); `u32::MAX` when every root is Dead or there are -/// none (nothing left to probe). +/// mined) is due at once; `u32::MAX` when every root has a final verdict +/// (nothing left to probe) or there are none. pub(crate) fn next_pass_height( state: &ResolverState, wallet_id: WalletId, views: &[OutgoingView], ) -> u32 { - let mined_expiry = state - .finished - .iter() - .filter_map(|((wallet, _), result)| match result { - Final::Mined { at } if *wallet == wallet_id => { - Some(at.saturating_add(MINED_GRACE).saturating_add(1)) - } - _ => None, - }) - .filter(|_| !views.is_empty()); let mined = state.mined(&wallet_id); ChainGraph::new(views, &mined) .roots() @@ -872,7 +847,6 @@ pub(crate) fn next_pass_height( .map(|view| (wallet_id, view.txid)) .filter(|key| !state.finished.contains_key(key)) .map(|key| state.schedules.get(&key).map_or(0, ProbeSchedule::next_due)) - .chain(mined_expiry) .min() .unwrap_or(u32::MAX) } @@ -1962,6 +1936,8 @@ mod tests { use key_wallet::transaction_checking::transaction_router::TransactionType; use key_wallet::transaction_checking::BlockInfo; use key_wallet::WalletCoreBalance; + use tokio::sync::Notify; + use tokio::time::{sleep, timeout}; use super::*; @@ -2699,7 +2675,7 @@ mod tests { synced: StdMutex>, /// Reads wait while set, so a test can hold a pass in its read. paused: StdMutex, - resume: tokio::sync::Notify, + resume: Notify, } impl FakeSource { @@ -3181,7 +3157,7 @@ mod tests { #[tokio::test] async fn should_not_report_a_running_task_as_not_running_to_a_concurrent_stop() { let (stop, _stopped) = oneshot::channel(); - let handle = tokio::spawn(tokio::time::sleep(Duration::from_millis(200))); + let handle = tokio::spawn(sleep(Duration::from_millis(200))); let slot = Mutex::new(ActorSlot::Running(handle, stop)); let stopping = AsyncMutex::new(()); let started = Instant::now(); @@ -3204,7 +3180,7 @@ mod tests { #[tokio::test] async fn should_report_a_task_that_outlived_a_stop_clean_on_retry() { let (stop, _stopped) = oneshot::channel(); - let handle = tokio::spawn(tokio::time::sleep(Duration::from_millis(300))); + let handle = tokio::spawn(sleep(Duration::from_millis(300))); let slot = Mutex::new(ActorSlot::Running(handle, stop)); let stopping = AsyncMutex::new(()); @@ -3989,7 +3965,7 @@ mod tests { }); let read = rig.actor.jobs.join_next_with_id().await.expect("read"); rig.actor.joined(read); // root 1's probe spawned - tokio::task::yield_now().await; // ...and finished, not yet handled + task::yield_now().await; // ...and finished, not yet handled rig.handle(Command::Settled { wallet_id: wallet(), @@ -4137,7 +4113,7 @@ mod tests { rig.follow(wallet(), 100).await; assert_eq!( rig.actor.state.finished.get(&(wallet(), txid(1))), - Some(&Final::Mined { at: 101 }) + Some(&Final::Mined) ); rig.height(wallet(), 50).await; @@ -4145,26 +4121,6 @@ mod tests { assert!(!rig.actor.state.finished.contains_key(&(wallet(), txid(1)))); } - /// A root a probe found mined that the wallet still has not seen in a block - /// MINED_GRACE blocks later is asked again. - #[tokio::test] - async fn should_ask_again_about_a_probe_found_mined_root_the_wallet_never_saw() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Mined)])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.follow(wallet(), 100).await; // found mined at 101 - for height in 102..=101 + MINED_GRACE { - rig.height(wallet(), height).await; - } - assert_eq!(probe.probed().len(), 1); - - // Past the grace, the wallet — waiting for exactly that height — asks - // again. - rig.height(wallet(), 102 + MINED_GRACE).await; - - assert_eq!(probe.probed().len(), 2); - } - /// A probe sent before a rewind answers Mined after it: the answer is told /// but does not end the root's probing across the replay. #[tokio::test] @@ -4192,6 +4148,33 @@ mod tests { assert!(!rig.actor.state.finished.contains_key(&(wallet(), txid(1)))); } + /// A probe sent before a rewind answers Dead after it: the root itself is + /// told, the sends built on it are not — nothing records the root dead for + /// their inherited Dead to hang on. + #[tokio::test] + async fn should_publish_a_pre_rewind_dead_to_the_root_only() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])], + ); + rig.height(wallet(), 100).await; + rig.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // root 1's probe is out + + rig.handle(Command::Height { + wallet_id: wallet(), + height: 50, + }); + rig.settle().await; + + assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(1), dead())]); + } + /// A probe that panics on its `nth` call and accepts otherwise. struct PanicOnCall { nth: usize, @@ -4311,11 +4294,11 @@ mod tests { for command in [Command::SetEnabled(true), Command::Uncertain(txid(1))] { commands.send(command).expect("send"); } - tokio::time::sleep(Duration::from_millis(50)).await; + sleep(Duration::from_millis(50)).await; stop.send(()).expect("stop"); - tokio::time::timeout(Duration::from_secs(5), task) + timeout(Duration::from_secs(5), task) .await .expect("stopped in time") .expect("no panic"); From dc84d0ae336d70bbf588cc70df338f3139d7f671 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:57:22 +0300 Subject: [PATCH 044/169] fix(platform-wallet): a node that has it in a block rules out Dead The thirty-seventh pass made Mined need two nodes and, in doing so, let a single "in a block" answer stop blocking a Dead verdict: two later NotFound lookups turned a payment that had gone through into Dead. Now any node reporting the transaction in a block rules Dead out. - A submission answered "already in block chain" no longer resubmits for a second opinion: the probe goes to getTransaction lookups, where a second distinct node in a block makes it Mined and none makes it Accepted. - confirm_dead and the new second-opinion path are one confirm_by_lookup: Dead needs the refusal quorum, DEAD_QUORUM nodes not knowing the txid, and no node knowing it in a block. - A rewind starts the wallet over: every verdict is cleared, the root's and the ones its chain inherited alike, so no root is left Dead with its children's Dead withdrawn. - Docs state the evidence rules, and as limitations: a network with one reachable evonode never reaches the Mined quorum (the wallet's own block processing settles those), and a block two nodes saw that a reorg drops before its ChainLock is not revisited. - Tests (each fails without its fix): no Dead after a node saw it in a block, either order; the second opinion is a lookup; a rewind clears the root's and the chain's verdicts. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/broadcast_probe.rs | 119 ++++++++++++------ packages/rs-platform-wallet/src/events.rs | 4 +- .../src/wallet/core/broadcast_resolver.rs | 52 ++++---- 3 files changed, 107 insertions(+), 68 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index 1801819e98e..d89ee7dbcc9 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -28,7 +28,15 @@ //! 1560020, both outputs spent → `-25`). Before a dead verdict the probe //! therefore asks nodes whether they know the txid at all (DAPI //! `getTransaction`, served from the evonode's transaction index); a node that -//! knows it settles the verdict as accepted. +//! knows it rules out a dead verdict. +//! +//! Mined needs two distinct nodes reporting the transaction in a block +//! ([`MINED_QUORUM`]); one such report makes it accepted — it exists — and the +//! second opinion is asked by lookup, not by resubmitting. On a network with +//! a single reachable evonode (a devnet pinned to one address) nothing is +//! ever mined by probe, only accepted; the wallet's own block processing +//! settles it. A block two nodes saw that a reorg drops before its ChainLock +//! (seconds on Dash) is not revisited: Mined is final. //! //! The two DAPI implementations surface those codes differently — the JS //! server maps `-25` to `InvalidArgument`, rs-dapi to `FailedPrecondition` — @@ -176,11 +184,13 @@ pub trait AcceptanceProbe: Send + Sync + 'static { async fn probe(&self, transaction: &Transaction) -> ProbeVerdict; } -/// Evidence rules shared by every probe: stop at the first node that holds -/// the transaction; call it dead only when [`DEAD_QUORUM`] distinct nodes -/// refuse it for its inputs **and** [`DEAD_QUORUM`] distinct nodes do not know -/// its txid; give up after [`MAX_SUBMISSIONS_PER_PROBE`] submissions and -/// [`MAX_LOOKUPS_PER_PROBE`] lookups. +/// Evidence rules shared by every probe: a node holding the transaction in its +/// mempool makes it accepted; one reporting it in a block rules out dead and +/// sends the probe to lookups for a second opinion ([`MINED_QUORUM`] distinct +/// nodes make it mined); dead needs [`DEAD_QUORUM`] distinct nodes refusing +/// it for its inputs **and** [`DEAD_QUORUM`] distinct nodes not knowing its +/// txid, with none knowing it; give up after [`MAX_SUBMISSIONS_PER_PROBE`] +/// submissions and [`MAX_LOOKUPS_PER_PROBE`] lookups. pub(crate) async fn probe_with( submitter: &dyn NodeSubmitter, transaction: &Transaction, @@ -201,9 +211,10 @@ pub(crate) async fn probe_with( match verdict { NodeVerdict::Accepted => return ProbeVerdict::Accepted, NodeVerdict::Mined => { - if mined.add(node) { - return ProbeVerdict::Mined; - } + // In a block by this node's word: not dead. A second opinion + // comes from a lookup, not from sending it again. + mined.add(node); + return confirm_by_lookup(submitter, &transaction.txid(), mined, None).await; } NodeVerdict::Dead { reason } => { // A dead verdict from an unidentified node cannot count @@ -213,13 +224,8 @@ pub(crate) async fn probe_with( } dead_reason.get_or_insert(reason); if dead_nodes.len() >= DEAD_QUORUM { - return confirm_dead( - submitter, - &transaction.txid(), - dead_reason.unwrap_or_default(), - mined, - ) - .await; + return confirm_by_lookup(submitter, &transaction.txid(), mined, dead_reason) + .await; } } NodeVerdict::Unknown { reason } => { @@ -228,9 +234,6 @@ pub(crate) async fn probe_with( } } - if mined.seen { - return ProbeVerdict::Accepted; - } ProbeVerdict::Unresolved { reason: match dead_reason { Some(reason) => format!( @@ -262,15 +265,17 @@ impl MinedEvidence { } } -/// A rejection for missing or spent inputs is also what a *mined* transaction -/// whose outputs are all spent gets (see the module docs). Ask nodes whether -/// they know the txid: one that does settles it as accepted; only -/// [`DEAD_QUORUM`] distinct nodes not knowing it confirm the dead verdict. -async fn confirm_dead( +/// Settle by `getTransaction` lookups. A node that knows the txid in its +/// mempool makes it accepted; in a block, counts towards [`MINED_QUORUM`] and +/// rules out dead. With `refused` — the dead quorum of refusals is met — only +/// [`DEAD_QUORUM`] distinct nodes not knowing the txid, and none knowing it, +/// make it dead: a rejection for missing or spent inputs is also what a +/// *mined* transaction whose outputs are all spent gets (see the module docs). +async fn confirm_by_lookup( submitter: &dyn NodeSubmitter, txid: &Txid, - reason: String, mut mined: MinedEvidence, + refused: Option, ) -> ProbeVerdict { let mut not_found: HashSet = HashSet::new(); let mut last_unknown = String::from("no lookup was answered"); @@ -293,8 +298,12 @@ async fn confirm_dead( if let Some(node) = node { not_found.insert(node); } - if not_found.len() >= DEAD_QUORUM { - return ProbeVerdict::Dead { reason }; + if let Some(reason) = &refused { + if !mined.seen && not_found.len() >= DEAD_QUORUM { + return ProbeVerdict::Dead { + reason: reason.clone(), + }; + } } } LookupAnswer::Unknown { reason } => last_unknown = reason, @@ -304,10 +313,13 @@ async fn confirm_dead( return ProbeVerdict::Accepted; } ProbeVerdict::Unresolved { - reason: format!( - "refused as {reason}, but only {} of {DEAD_QUORUM} nodes confirmed the txid is unknown; last lookup answer: {last_unknown}", - not_found.len() - ), + reason: match refused { + Some(reason) => format!( + "refused as {reason}, but only {} of {DEAD_QUORUM} nodes confirmed the txid is unknown; last lookup answer: {last_unknown}", + not_found.len() + ), + None => last_unknown, + }, } } @@ -693,16 +705,17 @@ mod tests { /// must not settle a transaction as mined: two distinct nodes must. #[tokio::test] async fn should_report_mined_only_when_two_distinct_nodes_have_it_in_a_block() { - let two = ScriptedNodes::new(vec![ - (Some("a"), NodeVerdict::Mined), - (Some("b"), NodeVerdict::Mined), - ]); - let same_twice = ScriptedNodes::new(vec![ - (Some("a"), NodeVerdict::Mined), - (Some("a"), NodeVerdict::Mined), - ]); + let two = ScriptedNodes::with_lookups( + vec![(Some("a"), NodeVerdict::Mined)], + vec![(Some("b"), LookupAnswer::Known { mined: true })], + ); + let same_twice = ScriptedNodes::with_lookups( + vec![(Some("a"), NodeVerdict::Mined)], + vec![(Some("a"), LookupAnswer::Known { mined: true })], + ); assert_eq!(probe_with(&two, &transaction()).await, ProbeVerdict::Mined); + assert_eq!(two.submissions(), 1, "the second opinion is a lookup"); assert_eq!( probe_with(&same_twice, &transaction()).await, ProbeVerdict::Accepted, @@ -710,6 +723,34 @@ mod tests { ); } + /// A node that has the transaction in a block rules out dead, whatever + /// the refusals and lookups that follow say. + #[tokio::test] + async fn should_never_call_dead_a_transaction_a_node_has_in_a_block() { + let refused_then_seen_mined = ScriptedNodes::with_lookups( + vec![(Some("a"), dead()), (Some("b"), dead())], + vec![ + (Some("c"), LookupAnswer::Known { mined: true }), + (Some("d"), LookupAnswer::NotFound), + (Some("e"), LookupAnswer::NotFound), + ], + ); + let mined_then_refused = ScriptedNodes::new(vec![ + (Some("a"), NodeVerdict::Mined), + (Some("b"), dead()), + (Some("c"), dead()), + ]); + + assert_eq!( + probe_with(&refused_then_seen_mined, &transaction()).await, + ProbeVerdict::Accepted + ); + assert_eq!( + probe_with(&mined_then_refused, &transaction()).await, + ProbeVerdict::Accepted + ); + } + #[tokio::test] async fn should_confirm_dead_only_when_two_distinct_nodes_do_not_know_the_txid() { let nodes = ScriptedNodes::with_lookups( diff --git a/packages/rs-platform-wallet/src/events.rs b/packages/rs-platform-wallet/src/events.rs index 828f6f5a909..06d8bea8486 100644 --- a/packages/rs-platform-wallet/src/events.rs +++ b/packages/rs-platform-wallet/src/events.rs @@ -50,8 +50,8 @@ pub trait PlatformEventHandler: EventHandler { /// Fired when the verdict on an unconfirmed send of this wallet changes — /// the network was asked about the root of its chain. `Accepted` means a - /// node holds the transaction in its mempool; `Mined` that a node has it - /// in a block; `Dead` that the transaction, or a parent it spends, can + /// node holds the transaction in its mempool (or one has it in a block); + /// `Mined` that two distinct nodes have it in a block; `Dead` that the transaction, or a parent it spends, can /// never be mined (two distinct nodes refused it and two do not know it); /// `Unresolved` that there is no verdict yet — the resolver asks again on /// a later block. Nothing in the wallet changes either way. diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 064b9f6e56d..cf68090c32d 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -610,20 +610,6 @@ impl ResolverState { } } - /// A rewind replays the chain: every root of `wallet_id` starts its - /// schedule over (due at once, window from the next following pass). - fn restart_schedules(&mut self, wallet_id: &WalletId) { - for ((wallet, _), schedule) in self.schedules.iter_mut() { - if wallet == wallet_id { - *schedule = ProbeSchedule::default(); - } - } - // The replay may drop a block a probe saw: roots found mined are - // asked again, and so are roots found dead — one may have become a - // root only because its parent counted as mined. - self.finished.retain(|(wallet, _), _| wallet != wallet_id); - } - /// Roots of `wallet_id` a probe found in a block the wallet has not seen. fn mined(&self, wallet_id: &WalletId) -> HashSet { self.finished @@ -1253,12 +1239,18 @@ impl Actor { let previous = entry.height.replace(height); let rewound = previous.is_some_and(|previous| height < previous); if rewound { - // The wait was worked out from the schedules restarted below. + // A rewind replays the chain: whatever the resolver kept + // for the wallet may rest on blocks the replay drops — a + // root found mined, a child that became a root only then — + // so it starts over: every verdict cleared, every root due + // at once, its window from the next following pass. A run + // under way marks nothing into that. entry.wait_until = None; if let Some(run) = entry.run.as_mut() { run.marks = false; } - self.state.restart_schedules(&wallet_id); + let events = self.state.forget_wallet(&wallet_id, "rewound"); + deliver(&self.sink, events); } let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; @@ -3613,24 +3605,30 @@ mod tests { assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); } - /// A step back (a rewind for a rescan) starts every root's schedule over: - /// due at once, its window from the next following pass. + /// A step back (a rewind for a rescan) starts the wallet over: every + /// verdict cleared — the root's and the ones its chain inherited alike — + /// every schedule gone, so every root is due at once. #[tokio::test] - async fn should_restart_schedules_on_a_rewind() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.follow(wallet(), 100).await; - assert_eq!( - rig.actor.state.schedules[&(wallet(), txid(1))].last_probe, - Some(101) + async fn should_start_over_on_a_rewind() { + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])], ); + rig.follow(wallet(), 100).await; + rig.sent(); rig.height(wallet(), 50).await; assert_eq!( - rig.actor.state.schedules[&(wallet(), txid(1))], - ProbeSchedule::default() + rig.sent(), + vec![ + ResolverEvent::Cleared(txid(1)), + ResolverEvent::Cleared(txid(2)), + ] ); + assert!(rig.actor.state.schedules.is_empty()); + assert!(rig.actor.state.finished.is_empty()); assert_eq!(rig.actor.wallets[&wallet()].wait_until, None); } From f84f2e6cf1387a0a07e517607de66a51a209869a Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 18:09:13 +0300 Subject: [PATCH 045/169] fix(platform-wallet): remember "seen in a block" across probes; thirty-ninth pass - A probe reports whether any node saw the transaction in a block (ProbeReport.seen_in_block, AcceptanceProbe::probe_report). The resolver keeps that per root while it stays unsettled, and a later probe that happens to ask other nodes can no longer turn it Dead. - A getTransaction answer from a block off the node's active chain (block hash, no height) is no longer read as "in the mempool". - An answer from a probe sent before a rewind is neither told nor recorded: it belongs to the state the rewind dropped. - confirm_by_lookup builds its own mined evidence from the node that answered "already in a block"; docs updated (Accepted, lookups, Dead in events.rs, Run::marks). - Tests (the memory test fails without its fix): a root once seen in a block is never turned Dead; a pre-rewind answer is not told. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/broadcast_probe.rs | 147 +++++++++++------- packages/rs-platform-wallet/src/events.rs | 3 +- .../src/wallet/core/broadcast_resolver.rs | 111 ++++++++++--- 3 files changed, 188 insertions(+), 73 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index d89ee7dbcc9..621b2809c9a 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -71,7 +71,7 @@ use dashcore::{Transaction, Txid}; /// (the wallet-side resolver only probes chain roots for that reason), what a /// node lagging behind the tip answers (hence [`DEAD_QUORUM`]: two distinct /// nodes), and what a mined transaction whose outputs are all spent gets -/// (hence the txid lookup in [`confirm_dead`]). +/// (hence the txid lookup in [`confirm_by_lookup`]). /// /// `txn-mempool-conflict` is deliberately absent: it means another, /// unconfirmed and not IS-locked, transaction spends the same input, and @@ -116,8 +116,9 @@ pub(crate) fn classify_failed_submission(code: Code, message: &str) -> NodeVerdi /// The answer a probe gives about one transaction. #[derive(Debug, Clone, PartialEq, Eq)] pub enum ProbeVerdict { - /// A node holds the transaction in its mempool. Not settlement: it can - /// still expire or lose to a conflict, so the resolver keeps asking. + /// A node holds the transaction in its mempool, or one node — short of + /// [`MINED_QUORUM`] — has it in a block. Not settlement: it can still + /// expire or lose to a conflict, so the resolver keeps asking. Accepted, /// Two different nodes have the transaction in a block. Final — there is /// nothing left to ask, even if this wallet has not seen that block yet. @@ -154,7 +155,8 @@ pub(crate) trait NodeSubmitter: Send + Sync { /// How many submissions one probe may spend looking for a verdict. const MAX_SUBMISSIONS_PER_PROBE: usize = 4; -/// How many txid lookups one probe may spend confirming a dead verdict. +/// How many txid lookups one probe may spend: to confirm a dead verdict, or to +/// get a second opinion on one node's "in a block". const MAX_LOOKUPS_PER_PROBE: usize = 4; /// How many *distinct* nodes must independently prove a transaction dead. @@ -182,6 +184,24 @@ const MINED_QUORUM: usize = 2; #[async_trait] pub trait AcceptanceProbe: Send + Sync + 'static { async fn probe(&self, transaction: &Transaction) -> ProbeVerdict; + + /// The verdict and what else the probe learned. By default it learns + /// nothing else. + async fn probe_report(&self, transaction: &Transaction) -> ProbeReport { + ProbeReport { + verdict: self.probe(transaction).await, + seen_in_block: false, + } + } +} + +/// A probe's verdict, and whether any node reported the transaction in a +/// block — which rules out Dead for good, not just for this probe: a later +/// probe that happens to ask other nodes must not overturn it. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ProbeReport { + pub verdict: ProbeVerdict, + pub seen_in_block: bool, } /// Evidence rules shared by every probe: a node holding the transaction in its @@ -194,11 +214,14 @@ pub trait AcceptanceProbe: Send + Sync + 'static { pub(crate) async fn probe_with( submitter: &dyn NodeSubmitter, transaction: &Transaction, -) -> ProbeVerdict { +) -> ProbeReport { let mut dead_nodes: HashSet = HashSet::new(); let mut dead_reason: Option = None; - let mut mined = MinedEvidence::default(); let mut last_other = String::from("no submission was answered"); + let report = |verdict| ProbeReport { + verdict, + seen_in_block: false, + }; for _ in 0..MAX_SUBMISSIONS_PER_PROBE { let (node, verdict) = submitter.submit(transaction).await; @@ -209,12 +232,11 @@ pub(crate) async fn probe_with( "broadcast probe: node answered" ); match verdict { - NodeVerdict::Accepted => return ProbeVerdict::Accepted, + NodeVerdict::Accepted => return report(ProbeVerdict::Accepted), NodeVerdict::Mined => { // In a block by this node's word: not dead. A second opinion // comes from a lookup, not from sending it again. - mined.add(node); - return confirm_by_lookup(submitter, &transaction.txid(), mined, None).await; + return confirm_by_lookup(submitter, &transaction.txid(), Some(node), None).await; } NodeVerdict::Dead { reason } => { // A dead verdict from an unidentified node cannot count @@ -224,7 +246,7 @@ pub(crate) async fn probe_with( } dead_reason.get_or_insert(reason); if dead_nodes.len() >= DEAD_QUORUM { - return confirm_by_lookup(submitter, &transaction.txid(), mined, dead_reason) + return confirm_by_lookup(submitter, &transaction.txid(), None, dead_reason) .await; } } @@ -234,7 +256,7 @@ pub(crate) async fn probe_with( } } - ProbeVerdict::Unresolved { + report(ProbeVerdict::Unresolved { reason: match dead_reason { Some(reason) => format!( "{} of {DEAD_QUORUM} nodes needed proved it dead ({reason}); last other answer: {last_other}", @@ -242,7 +264,7 @@ pub(crate) async fn probe_with( ), None => last_other, }, - } + }) } /// Nodes that reported the transaction in a block, by submission or lookup. @@ -271,12 +293,22 @@ impl MinedEvidence { /// [`DEAD_QUORUM`] distinct nodes not knowing the txid, and none knowing it, /// make it dead: a rejection for missing or spent inputs is also what a /// *mined* transaction whose outputs are all spent gets (see the module docs). +/// `seen_by` is the node whose submission answered "already in a block", when +/// one did (then `refused` is `None`). async fn confirm_by_lookup( submitter: &dyn NodeSubmitter, txid: &Txid, - mut mined: MinedEvidence, + seen_by: Option>, refused: Option, -) -> ProbeVerdict { +) -> ProbeReport { + let mut mined = MinedEvidence::default(); + if let Some(node) = seen_by { + mined.add(node); + } + let report = |verdict, mined: &MinedEvidence| ProbeReport { + verdict, + seen_in_block: mined.seen, + }; let mut not_found: HashSet = HashSet::new(); let mut last_unknown = String::from("no lookup was answered"); for _ in 0..MAX_LOOKUPS_PER_PROBE { @@ -290,37 +322,39 @@ async fn confirm_by_lookup( match answer { LookupAnswer::Known { mined: true } => { if mined.add(node) { - return ProbeVerdict::Mined; + return report(ProbeVerdict::Mined, &mined); } } - LookupAnswer::Known { mined: false } => return ProbeVerdict::Accepted, + LookupAnswer::Known { mined: false } => return report(ProbeVerdict::Accepted, &mined), LookupAnswer::NotFound => { if let Some(node) = node { not_found.insert(node); } if let Some(reason) = &refused { if !mined.seen && not_found.len() >= DEAD_QUORUM { - return ProbeVerdict::Dead { + let dead = ProbeVerdict::Dead { reason: reason.clone(), }; + return report(dead, &mined); } } } LookupAnswer::Unknown { reason } => last_unknown = reason, } } - if mined.seen { - return ProbeVerdict::Accepted; - } - ProbeVerdict::Unresolved { - reason: match refused { - Some(reason) => format!( + // A node's word that it is in a block: it exists, not settled as final. + let Some(reason) = refused.filter(|_| !mined.seen) else { + return report(ProbeVerdict::Accepted, &mined); + }; + report( + ProbeVerdict::Unresolved { + reason: format!( "refused as {reason}, but only {} of {DEAD_QUORUM} nodes confirmed the txid is unknown; last lookup answer: {last_unknown}", not_found.len() ), - None => last_unknown, }, - } + &mined, + ) } /// One node per request: the probe decides whether to ask another, and it has @@ -370,11 +404,18 @@ impl NodeSubmitter for DapiNodeSubmitter { }; match self.sdk.execute(request, single_node()).await { Ok(response) => { - let mined = response.inner.height > 0 || response.inner.confirmations > 0; - ( - Some(response.address.to_string()), - LookupAnswer::Known { mined }, - ) + let tx = &response.inner; + let mined = tx.height > 0 || tx.confirmations > 0; + let answer = if !mined && !tx.block_hash.is_empty() { + // Known from a block that is not on the node's active + // chain (a stale fork): neither mined nor in a mempool. + LookupAnswer::Unknown { + reason: "known only from a block off the active chain".to_string(), + } + } else { + LookupAnswer::Known { mined } + }; + (Some(response.address.to_string()), answer) } Err(error) => { let node = error.address.as_ref().map(ToString::to_string); @@ -410,6 +451,10 @@ impl DapiAcceptanceProbe { #[async_trait] impl AcceptanceProbe for DapiAcceptanceProbe { async fn probe(&self, transaction: &Transaction) -> ProbeVerdict { + self.probe_report(transaction).await.verdict + } + + async fn probe_report(&self, transaction: &Transaction) -> ProbeReport { probe_with(&self.submitter, transaction).await } } @@ -600,7 +645,7 @@ mod tests { ]); assert_eq!( - probe_with(&nodes, &transaction()).await, + probe_with(&nodes, &transaction()).await.verdict, ProbeVerdict::Accepted ); assert_eq!(nodes.submissions(), 2); @@ -611,7 +656,7 @@ mod tests { let nodes = ScriptedNodes::new(vec![(Some("a"), dead()), (Some("b"), dead())]); assert!(matches!( - probe_with(&nodes, &transaction()).await, + probe_with(&nodes, &transaction()).await.verdict, ProbeVerdict::Dead { .. } )); assert_eq!(nodes.submissions(), 2); @@ -629,7 +674,7 @@ mod tests { ]); assert!(matches!( - probe_with(&nodes, &transaction()).await, + probe_with(&nodes, &transaction()).await.verdict, ProbeVerdict::Unresolved { .. } )); assert_eq!(nodes.submissions(), MAX_SUBMISSIONS_PER_PROBE); @@ -640,7 +685,7 @@ mod tests { let nodes = ScriptedNodes::new(vec![(Some("a"), dead()), (None, dead())]); assert!(matches!( - probe_with(&nodes, &transaction()).await, + probe_with(&nodes, &transaction()).await.verdict, ProbeVerdict::Unresolved { .. } )); } @@ -660,7 +705,7 @@ mod tests { ]); assert!(matches!( - probe_with(&nodes, &transaction()).await, + probe_with(&nodes, &transaction()).await.verdict, ProbeVerdict::Unresolved { .. } )); assert_eq!(nodes.submissions(), MAX_SUBMISSIONS_PER_PROBE); @@ -675,7 +720,7 @@ mod tests { ]); assert!(matches!( - probe_with(&nodes, &transaction()).await, + probe_with(&nodes, &transaction()).await.verdict, ProbeVerdict::Dead { .. } )); assert_eq!(nodes.submissions(), 3); @@ -695,7 +740,7 @@ mod tests { ); assert_eq!( - probe_with(&nodes, &transaction()).await, + probe_with(&nodes, &transaction()).await.verdict, ProbeVerdict::Mined ); assert_eq!(nodes.lookups_made(), 2); @@ -714,10 +759,13 @@ mod tests { vec![(Some("a"), LookupAnswer::Known { mined: true })], ); - assert_eq!(probe_with(&two, &transaction()).await, ProbeVerdict::Mined); + assert_eq!( + probe_with(&two, &transaction()).await.verdict, + ProbeVerdict::Mined + ); assert_eq!(two.submissions(), 1, "the second opinion is a lookup"); assert_eq!( - probe_with(&same_twice, &transaction()).await, + probe_with(&same_twice, &transaction()).await.verdict, ProbeVerdict::Accepted, "one node's word: it exists, not that it is final" ); @@ -735,19 +783,12 @@ mod tests { (Some("e"), LookupAnswer::NotFound), ], ); - let mined_then_refused = ScriptedNodes::new(vec![ - (Some("a"), NodeVerdict::Mined), - (Some("b"), dead()), - (Some("c"), dead()), - ]); - assert_eq!( - probe_with(&refused_then_seen_mined, &transaction()).await, - ProbeVerdict::Accepted - ); - assert_eq!( - probe_with(&mined_then_refused, &transaction()).await, - ProbeVerdict::Accepted + let report = probe_with(&refused_then_seen_mined, &transaction()).await; + assert_eq!(report.verdict, ProbeVerdict::Accepted); + assert!( + report.seen_in_block, + "reported, so the resolver rules Dead out for later probes too" ); } @@ -769,7 +810,7 @@ mod tests { ); assert!(matches!( - probe_with(&nodes, &transaction()).await, + probe_with(&nodes, &transaction()).await.verdict, ProbeVerdict::Unresolved { .. } )); assert_eq!(nodes.lookups_made(), MAX_LOOKUPS_PER_PROBE); @@ -794,7 +835,7 @@ mod tests { ); assert_eq!( - probe_with(&nodes, &transaction()).await, + probe_with(&nodes, &transaction()).await.verdict, ProbeVerdict::Accepted ); } diff --git a/packages/rs-platform-wallet/src/events.rs b/packages/rs-platform-wallet/src/events.rs index 06d8bea8486..4366c1d58ba 100644 --- a/packages/rs-platform-wallet/src/events.rs +++ b/packages/rs-platform-wallet/src/events.rs @@ -52,7 +52,8 @@ pub trait PlatformEventHandler: EventHandler { /// the network was asked about the root of its chain. `Accepted` means a /// node holds the transaction in its mempool (or one has it in a block); /// `Mined` that two distinct nodes have it in a block; `Dead` that the transaction, or a parent it spends, can - /// never be mined (two distinct nodes refused it and two do not know it); + /// never be mined (two distinct nodes refused it, two do not know it, and + /// no node has ever reported it in a block); /// `Unresolved` that there is no verdict yet — the resolver asks again on /// a later block. Nothing in the wallet changes either way. /// diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index cf68090c32d..d76cf7c9b94 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -84,7 +84,7 @@ use tokio::sync::{mpsc, oneshot, Mutex as AsyncMutex, RwLock}; use tokio::task::{self, AbortHandle, JoinError, JoinHandle, JoinSet}; use tokio::time::{timeout_at, Instant}; -use crate::broadcast_probe::{AcceptanceProbe, ProbeVerdict}; +use crate::broadcast_probe::{AcceptanceProbe, ProbeReport, ProbeVerdict}; use crate::events::{PlatformEventHandler, PlatformEventManager}; use crate::wallet::platform_wallet::PlatformWalletInfo; @@ -437,6 +437,10 @@ pub(crate) struct ResolverState { /// those roots. Their Dead is withdrawn once none of them is known dead /// any more (settled, left, or forgotten). dead_from: HashMap<(WalletId, Txid), BTreeSet>, + /// Roots some node once reported in a block. Never Dead after that — a + /// later probe that asks other nodes cannot overturn it; kept while the + /// root stays unsettled in the wallet. + seen_in_block: HashSet<(WalletId, Txid)>, } impl ResolverState { @@ -445,6 +449,7 @@ impl ResolverState { fn forget_wallet(&mut self, wallet_id: &WalletId, trigger: &'static str) -> Vec { self.schedules.retain(|(wallet, _), _| wallet != wallet_id); self.finished.retain(|(wallet, _), _| wallet != wallet_id); + self.seen_in_block.retain(|(wallet, _)| wallet != wallet_id); self.clear_where(|wallet, _| wallet == wallet_id, None, trigger) } @@ -452,6 +457,7 @@ impl ResolverState { fn forget_all(&mut self) -> Vec { self.schedules.clear(); self.finished.clear(); + self.seen_in_block.clear(); self.clear_where(|_, _| true, None, "disabled") } @@ -462,6 +468,7 @@ impl ResolverState { let key = (wallet_id, *txid); self.schedules.remove(&key); self.finished.remove(&key); + self.seen_in_block.remove(&key); } let mut events = self.clear_where( |wallet, txid| *wallet == wallet_id && txids.contains(txid), @@ -684,6 +691,9 @@ pub(crate) fn begin_pass( state .finished .retain(|(wallet, txid), _| !is_gone(wallet, txid)); + state + .seen_in_block + .retain(|(wallet, txid)| !is_gone(wallet, txid)); // Cleared because the send is no longer an unsettled own send: it // settled, or left the wallet — not a statement that it settled. events.extend(state.clear_where( @@ -774,10 +784,13 @@ pub(crate) fn record_probe( ) -> Vec { let mut events = Vec::new(); let key = (wallet_id, root.txid); - // `record_final` is off for a probe sent before a rewind: its answer is - // still told, but it does not end the root's probing across the replay. + // `record_final` is off for a probe sent before a rewind: its answer + // belongs to the state the rewind dropped — neither recorded nor told. + if !record_final { + return events; + } match verdict { - ProbeVerdict::Dead { reason } if record_final => { + ProbeVerdict::Dead { reason } => { state.finished.insert( key, Final::Dead { @@ -785,15 +798,13 @@ pub(crate) fn record_probe( }, ); } - ProbeVerdict::Mined if record_final => { + ProbeVerdict::Mined => { state.finished.insert(key, Final::Mined); } _ => {} } let trigger = trigger_of(root.forced); - // Spread a Dead down the chain only when it is recorded: a Dead the chain - // inherits needs a root known dead to hang on (see `dead_from`). - if matches!(verdict, ProbeVerdict::Dead { .. }) && record_final { + if matches!(verdict, ProbeVerdict::Dead { .. }) { let sends = ChainGraph::new(views, &state.mined(&wallet_id)).own_in_chain(&root.txid); state.publish_dead( wallet_id, @@ -1040,7 +1051,7 @@ enum JobDone { /// The root it probed: a result for a root no longer in flight (its /// probe was stopped, but had already finished) is not this one's. root: Txid, - verdict: ProbeVerdict, + report: ProbeReport, }, } @@ -1066,8 +1077,9 @@ struct Run { /// probe is out is moot once gone from the views). Gone with the run — a /// later read is current, a send a reorg brings back included. settled: HashSet, - /// Cleared by a rewind: the wallet's schedules were restarted, and this - /// run, older than that, marks no probes into them. + /// Cleared by a rewind: the resolver forgot the wallet, and this run, + /// older than that, neither marks its probes nor tells or records their + /// answers. marks: bool, /// Something touched the wallet during the run: its read may be stale, so /// the next height must not be skipped. @@ -1540,7 +1552,7 @@ impl Actor { wallet_id, run, root: probed, - verdict, + report, } => { let Some(current) = self .wallets @@ -1587,6 +1599,18 @@ impl Actor { self.probe_next(wallet_id); return; } + // Once any node reported the root in a block, no later probe + // makes it Dead: it exists, even if these nodes do not know it. + let key = (wallet_id, root.txid); + if report.seen_in_block { + self.state.seen_in_block.insert(key); + } + let verdict = match report.verdict { + ProbeVerdict::Dead { .. } if self.state.seen_in_block.contains(&key) => { + ProbeVerdict::Accepted + } + verdict => verdict, + }; let events = record_probe( &mut self.state, wallet_id, @@ -1619,12 +1643,12 @@ impl Actor { let transaction = Arc::clone(&root.transaction); let root_txid = root.txid; run.abort = self.jobs.spawn(async move { - let verdict = probe.probe(&transaction).await; + let report = probe.probe_report(&transaction).await; JobDone::Probed { wallet_id, run: id, root: root_txid, - verdict, + report, } }); self.job_runs @@ -4146,11 +4170,10 @@ mod tests { assert!(!rig.actor.state.finished.contains_key(&(wallet(), txid(1)))); } - /// A probe sent before a rewind answers Dead after it: the root itself is - /// told, the sends built on it are not — nothing records the root dead for - /// their inherited Dead to hang on. + /// A probe sent before a rewind answers Dead after it: the answer belongs + /// to the state the rewind dropped — nothing is told or recorded. #[tokio::test] - async fn should_publish_a_pre_rewind_dead_to_the_root_only() { + async fn should_neither_tell_nor_record_a_pre_rewind_answer() { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; rig.views( wallet(), @@ -4170,7 +4193,57 @@ mod tests { }); rig.settle().await; - assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(1), dead())]); + assert!(rig.sent().is_empty()); + assert!(rig.actor.state.finished.is_empty()); + } + + /// A probe that reports the root in a block once, then Dead. + struct SeenThenDead { + calls: StdMutex, + } + + #[async_trait] + impl AcceptanceProbe for SeenThenDead { + async fn probe(&self, transaction: &Transaction) -> ProbeVerdict { + self.probe_report(transaction).await.verdict + } + + async fn probe_report(&self, _transaction: &Transaction) -> ProbeReport { + let first = { + let mut calls = self.calls.lock().expect("calls"); + *calls += 1; + *calls == 1 + }; + if first { + ProbeReport { + verdict: ProbeVerdict::Accepted, + seen_in_block: true, + } + } else { + ProbeReport { + verdict: dead(), + seen_in_block: false, + } + } + } + } + + /// Once a node reported a root in a block, a later probe asking other + /// nodes cannot make it Dead. + #[tokio::test] + async fn should_never_turn_dead_a_root_once_seen_in_a_block() { + let mut rig = enabled(Arc::new(SeenThenDead { + calls: StdMutex::new(0), + })) + .await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.height(wallet(), 102).await; + + assert!(!rig + .sent() + .contains(&ResolverEvent::Verdict(txid(1), dead()))); + assert!(!rig.actor.state.finished.contains_key(&(wallet(), txid(1)))); } /// A probe that panics on its `nth` call and accepts otherwise. From 50d1eec96470389a1e306309e8d2be431bced9bf Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 22:03:43 +0300 Subject: [PATCH 046/169] fix(platform-wallet): add up "in a block" reports into a Mined quorum; fortieth pass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The thirty-ninth pass let one node's "in a block" veto Dead for the root's whole unsettled life, and recorded it even from a probe sent before a rewind. Both majors go with one change: a probe reports the identified nodes that saw the transaction in a block (ProbeReport.in_block_by), and record_probe — after the rewind gate — adds them up per root; MINED_QUORUM distinct nodes over any number of probes make it Mined. A lone node, or an unidentified one, vetoes nothing beyond its own probe. - A rewind also drops the running pass's remaining due roots: their answers would be discarded anyway. - The getTransaction mapping is a pure lookup_answer, with a test for the stale-fork case; confirm_by_lookup takes a LookupFor mode instead of two Options; the DAPI probe no longer implements probe via probe_report, and the trait documents that trap. - events.rs describes Dead by the nodes asked in that probe. - Tests (the first fails without accumulation): two nodes over two probes make it Mined; one node repeatedly does not veto a later Dead. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../rs-platform-wallet/src/broadcast_probe.rs | 148 ++++++++++++------ packages/rs-platform-wallet/src/events.rs | 2 +- .../src/wallet/core/broadcast_resolver.rs | 122 +++++++++------ 3 files changed, 174 insertions(+), 98 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index 621b2809c9a..ccd7e83d549 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -46,13 +46,15 @@ //! //! [`BroadcastError::MaybeSent`]: crate::broadcaster::BroadcastError::MaybeSent -use std::collections::HashSet; +use std::collections::{BTreeSet, HashSet}; use std::sync::Arc; use async_trait::async_trait; use dash_sdk::dapi_client::transport::TransportError; use dash_sdk::dapi_client::{DapiClientError, DapiRequestExecutor, RequestSettings}; -use dash_sdk::dapi_grpc::core::v0::{BroadcastTransactionRequest, GetTransactionRequest}; +use dash_sdk::dapi_grpc::core::v0::{ + BroadcastTransactionRequest, GetTransactionRequest, GetTransactionResponse, +}; use dash_sdk::dapi_grpc::tonic::Code; use dashcore::consensus; use dashcore::{Transaction, Txid}; @@ -173,7 +175,7 @@ const DEAD_QUORUM: usize = 2; /// its place. One node on a stale fork, or one whose block is about to be /// reorged out, would otherwise settle it on false evidence. A single such /// answer still shows the transaction exists: it counts as accepted. -const MINED_QUORUM: usize = 2; +pub(crate) const MINED_QUORUM: usize = 2; /// Resubmits a transaction whose broadcast outcome is unknown and turns the /// nodes' answers into a verdict. @@ -185,23 +187,30 @@ const MINED_QUORUM: usize = 2; pub trait AcceptanceProbe: Send + Sync + 'static { async fn probe(&self, transaction: &Transaction) -> ProbeVerdict; - /// The verdict and what else the probe learned. By default it learns - /// nothing else. + /// The verdict and what else the probe learned. The default calls + /// [`probe`](Self::probe) and learns nothing else — so an implementation + /// must not write `probe` in terms of this one without overriding it too. async fn probe_report(&self, transaction: &Transaction) -> ProbeReport { - ProbeReport { - verdict: self.probe(transaction).await, - seen_in_block: false, - } + self.probe(transaction).await.into() } } -/// A probe's verdict, and whether any node reported the transaction in a -/// block — which rules out Dead for good, not just for this probe: a later -/// probe that happens to ask other nodes must not overturn it. +/// A probe's verdict, and the identified nodes that reported the transaction +/// in a block. The resolver adds these up across probes of one root: +/// [`MINED_QUORUM`] distinct nodes over several probes make it mined too. #[derive(Debug, Clone, PartialEq, Eq)] pub struct ProbeReport { pub verdict: ProbeVerdict, - pub seen_in_block: bool, + pub in_block_by: BTreeSet, +} + +impl From for ProbeReport { + fn from(verdict: ProbeVerdict) -> Self { + Self { + verdict, + in_block_by: BTreeSet::new(), + } + } } /// Evidence rules shared by every probe: a node holding the transaction in its @@ -218,10 +227,7 @@ pub(crate) async fn probe_with( let mut dead_nodes: HashSet = HashSet::new(); let mut dead_reason: Option = None; let mut last_other = String::from("no submission was answered"); - let report = |verdict| ProbeReport { - verdict, - seen_in_block: false, - }; + let report = ProbeReport::from; for _ in 0..MAX_SUBMISSIONS_PER_PROBE { let (node, verdict) = submitter.submit(transaction).await; @@ -236,7 +242,12 @@ pub(crate) async fn probe_with( NodeVerdict::Mined => { // In a block by this node's word: not dead. A second opinion // comes from a lookup, not from sending it again. - return confirm_by_lookup(submitter, &transaction.txid(), Some(node), None).await; + return confirm_by_lookup( + submitter, + &transaction.txid(), + LookupFor::SecondOpinion { seen_by: node }, + ) + .await; } NodeVerdict::Dead { reason } => { // A dead verdict from an unidentified node cannot count @@ -246,8 +257,13 @@ pub(crate) async fn probe_with( } dead_reason.get_or_insert(reason); if dead_nodes.len() >= DEAD_QUORUM { - return confirm_by_lookup(submitter, &transaction.txid(), None, dead_reason) - .await; + let reason = dead_reason.unwrap_or_default(); + return confirm_by_lookup( + submitter, + &transaction.txid(), + LookupFor::Dead { reason }, + ) + .await; } } NodeVerdict::Unknown { reason } => { @@ -267,6 +283,15 @@ pub(crate) async fn probe_with( }) } +/// Why a probe turns to lookups. +enum LookupFor { + /// A submission answered "already in a block" (from `seen_by`): a second + /// node is asked; dead is ruled out. + SecondOpinion { seen_by: Option }, + /// The refusal quorum for `reason` is met: dead, unless a node knows it. + Dead { reason: String }, +} + /// Nodes that reported the transaction in a block, by submission or lookup. #[derive(Default)] struct MinedEvidence { @@ -293,21 +318,22 @@ impl MinedEvidence { /// [`DEAD_QUORUM`] distinct nodes not knowing the txid, and none knowing it, /// make it dead: a rejection for missing or spent inputs is also what a /// *mined* transaction whose outputs are all spent gets (see the module docs). -/// `seen_by` is the node whose submission answered "already in a block", when -/// one did (then `refused` is `None`). async fn confirm_by_lookup( submitter: &dyn NodeSubmitter, txid: &Txid, - seen_by: Option>, - refused: Option, + purpose: LookupFor, ) -> ProbeReport { let mut mined = MinedEvidence::default(); - if let Some(node) = seen_by { - mined.add(node); - } + let refused = match purpose { + LookupFor::SecondOpinion { seen_by } => { + mined.add(seen_by); + None + } + LookupFor::Dead { reason } => Some(reason), + }; let report = |verdict, mined: &MinedEvidence| ProbeReport { verdict, - seen_in_block: mined.seen, + in_block_by: mined.nodes.iter().cloned().collect(), }; let mut not_found: HashSet = HashSet::new(); let mut last_unknown = String::from("no lookup was answered"); @@ -344,6 +370,7 @@ async fn confirm_by_lookup( } // A node's word that it is in a block: it exists, not settled as final. let Some(reason) = refused.filter(|_| !mined.seen) else { + // A second opinion that never came: it exists by one node's word. return report(ProbeVerdict::Accepted, &mined); }; report( @@ -357,6 +384,19 @@ async fn confirm_by_lookup( ) } +/// What a node's `getTransaction` answer says. A block hash with no height and +/// no confirmations is a block off the node's active chain (a stale fork; both +/// DAPI servers send height 0 for Core's -1): neither mined nor in a mempool. +fn lookup_answer(tx: &GetTransactionResponse) -> LookupAnswer { + let mined = tx.height > 0 || tx.confirmations > 0; + if !mined && !tx.block_hash.is_empty() { + return LookupAnswer::Unknown { + reason: "known only from a block off the active chain".to_string(), + }; + } + LookupAnswer::Known { mined } +} + /// One node per request: the probe decides whether to ask another, and it has /// to know which node each answer came from. fn single_node() -> RequestSettings { @@ -403,20 +443,10 @@ impl NodeSubmitter for DapiNodeSubmitter { id: txid.to_string(), }; match self.sdk.execute(request, single_node()).await { - Ok(response) => { - let tx = &response.inner; - let mined = tx.height > 0 || tx.confirmations > 0; - let answer = if !mined && !tx.block_hash.is_empty() { - // Known from a block that is not on the node's active - // chain (a stale fork): neither mined nor in a mempool. - LookupAnswer::Unknown { - reason: "known only from a block off the active chain".to_string(), - } - } else { - LookupAnswer::Known { mined } - }; - (Some(response.address.to_string()), answer) - } + Ok(response) => ( + Some(response.address.to_string()), + lookup_answer(&response.inner), + ), Err(error) => { let node = error.address.as_ref().map(ToString::to_string); let answer = match &error.inner { @@ -451,7 +481,7 @@ impl DapiAcceptanceProbe { #[async_trait] impl AcceptanceProbe for DapiAcceptanceProbe { async fn probe(&self, transaction: &Transaction) -> ProbeVerdict { - self.probe_report(transaction).await.verdict + probe_with(&self.submitter, transaction).await.verdict } async fn probe_report(&self, transaction: &Transaction) -> ProbeReport { @@ -786,12 +816,38 @@ mod tests { let report = probe_with(&refused_then_seen_mined, &transaction()).await; assert_eq!(report.verdict, ProbeVerdict::Accepted); - assert!( - report.seen_in_block, - "reported, so the resolver rules Dead out for later probes too" + assert_eq!( + report.in_block_by, + BTreeSet::from(["c".to_string()]), + "reported, for the resolver to add up across probes" ); } + #[test] + fn should_read_a_stale_fork_lookup_as_neither_mined_nor_mempool() { + let answer = |block_hash: Vec, height, confirmations| { + lookup_answer(&GetTransactionResponse { + block_hash, + height, + confirmations, + ..GetTransactionResponse::default() + }) + }; + + assert_eq!( + answer(Vec::new(), 0, 0), + LookupAnswer::Known { mined: false } + ); + assert_eq!( + answer(vec![1; 32], 100, 3), + LookupAnswer::Known { mined: true } + ); + assert!(matches!( + answer(vec![1; 32], 0, 0), + LookupAnswer::Unknown { .. } + )); + } + #[tokio::test] async fn should_confirm_dead_only_when_two_distinct_nodes_do_not_know_the_txid() { let nodes = ScriptedNodes::with_lookups( diff --git a/packages/rs-platform-wallet/src/events.rs b/packages/rs-platform-wallet/src/events.rs index 4366c1d58ba..a815cb01b8f 100644 --- a/packages/rs-platform-wallet/src/events.rs +++ b/packages/rs-platform-wallet/src/events.rs @@ -53,7 +53,7 @@ pub trait PlatformEventHandler: EventHandler { /// node holds the transaction in its mempool (or one has it in a block); /// `Mined` that two distinct nodes have it in a block; `Dead` that the transaction, or a parent it spends, can /// never be mined (two distinct nodes refused it, two do not know it, and - /// no node has ever reported it in a block); + /// none of the nodes asked in that probe has it in a block); /// `Unresolved` that there is no verdict yet — the resolver asks again on /// a later block. Nothing in the wallet changes either way. /// diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index d76cf7c9b94..f336025775f 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -84,7 +84,7 @@ use tokio::sync::{mpsc, oneshot, Mutex as AsyncMutex, RwLock}; use tokio::task::{self, AbortHandle, JoinError, JoinHandle, JoinSet}; use tokio::time::{timeout_at, Instant}; -use crate::broadcast_probe::{AcceptanceProbe, ProbeReport, ProbeVerdict}; +use crate::broadcast_probe::{AcceptanceProbe, ProbeReport, ProbeVerdict, MINED_QUORUM}; use crate::events::{PlatformEventHandler, PlatformEventManager}; use crate::wallet::platform_wallet::PlatformWalletInfo; @@ -437,10 +437,10 @@ pub(crate) struct ResolverState { /// those roots. Their Dead is withdrawn once none of them is known dead /// any more (settled, left, or forgotten). dead_from: HashMap<(WalletId, Txid), BTreeSet>, - /// Roots some node once reported in a block. Never Dead after that — a - /// later probe that asks other nodes cannot overturn it; kept while the - /// root stays unsettled in the wallet. - seen_in_block: HashSet<(WalletId, Txid)>, + /// The identified nodes that reported each root in a block, added up + /// across probes while it stays unsettled: [`MINED_QUORUM`] of them make + /// it mined even when no single probe heard two. + seen_in_block: HashMap<(WalletId, Txid), BTreeSet>, } impl ResolverState { @@ -449,7 +449,8 @@ impl ResolverState { fn forget_wallet(&mut self, wallet_id: &WalletId, trigger: &'static str) -> Vec { self.schedules.retain(|(wallet, _), _| wallet != wallet_id); self.finished.retain(|(wallet, _), _| wallet != wallet_id); - self.seen_in_block.retain(|(wallet, _)| wallet != wallet_id); + self.seen_in_block + .retain(|(wallet, _), _| wallet != wallet_id); self.clear_where(|wallet, _| wallet == wallet_id, None, trigger) } @@ -693,7 +694,7 @@ pub(crate) fn begin_pass( .retain(|(wallet, txid), _| !is_gone(wallet, txid)); state .seen_in_block - .retain(|(wallet, txid)| !is_gone(wallet, txid)); + .retain(|(wallet, txid), _| !is_gone(wallet, txid)); // Cleared because the send is no longer an unsettled own send: it // settled, or left the wallet — not a statement that it settled. events.extend(state.clear_where( @@ -779,7 +780,7 @@ pub(crate) fn record_probe( height: u32, views: &[OutgoingView], root: &DueRoot, - verdict: &ProbeVerdict, + report: &ProbeReport, record_final: bool, ) -> Vec { let mut events = Vec::new(); @@ -789,6 +790,15 @@ pub(crate) fn record_probe( if !record_final { return events; } + // Nodes that saw it in a block, over every probe of this root: enough of + // them make it mined whatever this probe's own verdict. + let seen = state.seen_in_block.entry(key).or_default(); + seen.extend(report.in_block_by.iter().cloned()); + let verdict = &if seen.len() >= MINED_QUORUM { + ProbeVerdict::Mined + } else { + report.verdict.clone() + }; match verdict { ProbeVerdict::Dead { reason } => { state.finished.insert( @@ -1260,6 +1270,10 @@ impl Actor { entry.wait_until = None; if let Some(run) = entry.run.as_mut() { run.marks = false; + // Its answers would be discarded: send no more probes. + if let Some(probing) = run.probing.as_mut() { + probing.due.clear(); + } } let events = self.state.forget_wallet(&wallet_id, "rewound"); deliver(&self.sink, events); @@ -1599,25 +1613,13 @@ impl Actor { self.probe_next(wallet_id); return; } - // Once any node reported the root in a block, no later probe - // makes it Dead: it exists, even if these nodes do not know it. - let key = (wallet_id, root.txid); - if report.seen_in_block { - self.state.seen_in_block.insert(key); - } - let verdict = match report.verdict { - ProbeVerdict::Dead { .. } if self.state.seen_in_block.contains(&key) => { - ProbeVerdict::Accepted - } - verdict => verdict, - }; let events = record_probe( &mut self.state, wallet_id, probing.height, &probing.views, &root, - &verdict, + &report, current.marks, ); deliver(&self.sink, events); @@ -2287,7 +2289,7 @@ mod tests { root.txid, height, ); - let verdict = probe.probe(&root.transaction).await; + let report = probe.probe_report(&root.transaction).await; let mut guard = state.lock().expect("state"); let events = record_probe( &mut guard.state, @@ -2295,7 +2297,7 @@ mod tests { height, views, &root, - &verdict, + &report, true, ); guard.push(events); @@ -4197,53 +4199,71 @@ mod tests { assert!(rig.actor.state.finished.is_empty()); } - /// A probe that reports the root in a block once, then Dead. - struct SeenThenDead { - calls: StdMutex, + /// A probe whose every answer is one node's "in a block", a different + /// node each time — then, after that, Dead. + struct OneNodeInBlockPerProbe { + answers: StdMutex>, } #[async_trait] - impl AcceptanceProbe for SeenThenDead { - async fn probe(&self, transaction: &Transaction) -> ProbeVerdict { - self.probe_report(transaction).await.verdict + impl AcceptanceProbe for OneNodeInBlockPerProbe { + async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { + unreachable!("the resolver asks for the report") } async fn probe_report(&self, _transaction: &Transaction) -> ProbeReport { - let first = { - let mut calls = self.calls.lock().expect("calls"); - *calls += 1; - *calls == 1 - }; - if first { - ProbeReport { - verdict: ProbeVerdict::Accepted, - seen_in_block: true, - } - } else { - ProbeReport { - verdict: dead(), - seen_in_block: false, - } - } + self.answers + .lock() + .expect("answers") + .pop_front() + .unwrap_or_else(|| dead().into()) } } - /// Once a node reported a root in a block, a later probe asking other - /// nodes cannot make it Dead. + fn seen_by(node: &str) -> ProbeReport { + ProbeReport { + verdict: ProbeVerdict::Accepted, + in_block_by: BTreeSet::from([node.to_string()]), + } + } + + /// Two distinct nodes reporting the root in a block over two probes make + /// it mined, final — a later probe cannot turn it Dead. #[tokio::test] - async fn should_never_turn_dead_a_root_once_seen_in_a_block() { - let mut rig = enabled(Arc::new(SeenThenDead { - calls: StdMutex::new(0), + async fn should_settle_as_mined_when_two_nodes_saw_it_across_probes() { + let mut rig = enabled(Arc::new(OneNodeInBlockPerProbe { + answers: StdMutex::new(VecDeque::from([seen_by("a"), seen_by("b")])), })) .await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.follow(wallet(), 100).await; rig.height(wallet(), 102).await; + rig.height(wallet(), 103).await; + assert_eq!( + rig.actor.state.finished.get(&(wallet(), txid(1))), + Some(&Final::Mined) + ); assert!(!rig .sent() .contains(&ResolverEvent::Verdict(txid(1), dead()))); - assert!(!rig.actor.state.finished.contains_key(&(wallet(), txid(1)))); + } + + /// One node's word, over and over, is not enough to veto Dead for good. + #[tokio::test] + async fn should_not_let_one_node_veto_a_later_dead() { + let mut rig = enabled(Arc::new(OneNodeInBlockPerProbe { + answers: StdMutex::new(VecDeque::from([seen_by("a"), seen_by("a")])), + })) + .await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.height(wallet(), 102).await; + rig.height(wallet(), 103).await; + + assert!(rig + .sent() + .contains(&ResolverEvent::Verdict(txid(1), dead()))); } /// A probe that panics on its `nth` call and accepts otherwise. From bb47d6a72d88901915974601c9a03221fc358dfb Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 30 Sep 2026 22:39:49 +0300 Subject: [PATCH 047/169] fix(platform-wallet): stop a running probe pass on a rewind A rewind now aborts the wallet's running pass outright instead of letting it finish unmarked: its in-flight probe is dropped, its remaining roots are not sent, and what `Uncertain` results forced it to probe carries over to the next pass at the rewound height. The `marks`/`record_final` plumbing that discarded pre-rewind answers goes away with it. AcceptanceProbe has a single method, `probe`, returning the full ProbeReport, so no implementation can lose the in-block evidence by overriding only the verdict. Also: record in-block nodes only when a probe reported some, keep the first dead reason without a default, and reword the Mined doc to say the quorum may build up over several probes. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../examples/broadcast_probe_testnet.rs | 2 +- .../rs-platform-wallet/src/broadcast_probe.rs | 20 +-- packages/rs-platform-wallet/src/events.rs | 5 +- .../src/wallet/core/broadcast_resolver.rs | 153 +++++++++++------- 4 files changed, 100 insertions(+), 80 deletions(-) diff --git a/packages/rs-platform-wallet/examples/broadcast_probe_testnet.rs b/packages/rs-platform-wallet/examples/broadcast_probe_testnet.rs index 0369e69b7e0..8c848418bdc 100644 --- a/packages/rs-platform-wallet/examples/broadcast_probe_testnet.rs +++ b/packages/rs-platform-wallet/examples/broadcast_probe_testnet.rs @@ -45,7 +45,7 @@ async fn main() -> Result<(), Box> { let probe = DapiAcceptanceProbe::new(sdk); for transaction in &transactions { - let verdict = probe.probe(transaction).await; + let verdict = probe.probe(transaction).await.verdict; println!("{} {verdict:?}", transaction.txid()); } Ok(()) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index ccd7e83d549..b021f066b4c 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -185,14 +185,8 @@ pub(crate) const MINED_QUORUM: usize = 2; /// it will not. It can never create a second payment. #[async_trait] pub trait AcceptanceProbe: Send + Sync + 'static { - async fn probe(&self, transaction: &Transaction) -> ProbeVerdict; - - /// The verdict and what else the probe learned. The default calls - /// [`probe`](Self::probe) and learns nothing else — so an implementation - /// must not write `probe` in terms of this one without overriding it too. - async fn probe_report(&self, transaction: &Transaction) -> ProbeReport { - self.probe(transaction).await.into() - } + /// The verdict, and what else the probe learned. + async fn probe(&self, transaction: &Transaction) -> ProbeReport; } /// A probe's verdict, and the identified nodes that reported the transaction @@ -255,9 +249,9 @@ pub(crate) async fn probe_with( if let Some(node) = node { dead_nodes.insert(node); } - dead_reason.get_or_insert(reason); + let first_reason = dead_reason.get_or_insert(reason); if dead_nodes.len() >= DEAD_QUORUM { - let reason = dead_reason.unwrap_or_default(); + let reason = first_reason.clone(); return confirm_by_lookup( submitter, &transaction.txid(), @@ -480,11 +474,7 @@ impl DapiAcceptanceProbe { #[async_trait] impl AcceptanceProbe for DapiAcceptanceProbe { - async fn probe(&self, transaction: &Transaction) -> ProbeVerdict { - probe_with(&self.submitter, transaction).await.verdict - } - - async fn probe_report(&self, transaction: &Transaction) -> ProbeReport { + async fn probe(&self, transaction: &Transaction) -> ProbeReport { probe_with(&self.submitter, transaction).await } } diff --git a/packages/rs-platform-wallet/src/events.rs b/packages/rs-platform-wallet/src/events.rs index a815cb01b8f..9f48549041e 100644 --- a/packages/rs-platform-wallet/src/events.rs +++ b/packages/rs-platform-wallet/src/events.rs @@ -51,8 +51,9 @@ pub trait PlatformEventHandler: EventHandler { /// Fired when the verdict on an unconfirmed send of this wallet changes — /// the network was asked about the root of its chain. `Accepted` means a /// node holds the transaction in its mempool (or one has it in a block); - /// `Mined` that two distinct nodes have it in a block; `Dead` that the transaction, or a parent it spends, can - /// never be mined (two distinct nodes refused it, two do not know it, and + /// `Mined` that two distinct nodes have it in a block — in one probe or + /// over several of the same send; `Dead` that the transaction, or a + /// parent it spends, can never be mined (two distinct nodes refused it, two do not know it, and /// none of the nodes asked in that probe has it in a block); /// `Unresolved` that there is no verdict yet — the resolver asks again on /// a later block. Nothing in the wallet changes either way. diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index f336025775f..98d32e2acc1 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -781,20 +781,20 @@ pub(crate) fn record_probe( views: &[OutgoingView], root: &DueRoot, report: &ProbeReport, - record_final: bool, ) -> Vec { let mut events = Vec::new(); let key = (wallet_id, root.txid); - // `record_final` is off for a probe sent before a rewind: its answer - // belongs to the state the rewind dropped — neither recorded nor told. - if !record_final { - return events; - } // Nodes that saw it in a block, over every probe of this root: enough of // them make it mined whatever this probe's own verdict. - let seen = state.seen_in_block.entry(key).or_default(); - seen.extend(report.in_block_by.iter().cloned()); - let verdict = &if seen.len() >= MINED_QUORUM { + if !report.in_block_by.is_empty() { + state + .seen_in_block + .entry(key) + .or_default() + .extend(report.in_block_by.iter().cloned()); + } + let seen = state.seen_in_block.get(&key).map_or(0, BTreeSet::len); + let verdict = &if seen >= MINED_QUORUM { ProbeVerdict::Mined } else { report.verdict.clone() @@ -1087,18 +1087,15 @@ struct Run { /// probe is out is moot once gone from the views). Gone with the run — a /// later read is current, a send a reorg brings back included. settled: HashSet, - /// Cleared by a rewind: the resolver forgot the wallet, and this run, - /// older than that, neither marks its probes nor tells or records their - /// answers. - marks: bool, /// Something touched the wallet during the run: its read may be stale, so /// the next height must not be skipped. woken: bool, /// Where this pass starts windows; `None` confines it to forced roots /// (see [`PendingPass::anchor_at`]). anchor_at: Option, - /// `Uncertain` results forced it. - has_forced: bool, + /// What `Uncertain` results forced it to probe: carried over to the next + /// pass when a rewind stops it. + forced: HashSet, /// Whether the read walks the records even when none of the forced txids /// is held (`anchor_at.is_some()`): shared with the read job, cleared by a /// catch-up. @@ -1266,13 +1263,17 @@ impl Actor { // root found mined, a child that became a root only then — // so it starts over: every verdict cleared, every root due // at once, its window from the next following pass. A run - // under way marks nothing into that. + // under way rests on that state too: it is stopped, and what + // `Uncertain` results forced it to probe goes to the next pass. entry.wait_until = None; - if let Some(run) = entry.run.as_mut() { - run.marks = false; - // Its answers would be discarded: send no more probes. - if let Some(probing) = run.probing.as_mut() { - probing.due.clear(); + if let Some(run) = entry.run.take() { + run.abort.abort(); + if !run.forced.is_empty() { + entry + .pending + .get_or_insert_with(PendingPass::default) + .forced + .extend(run.forced); } } let events = self.state.forget_wallet(&wallet_id, "rewound"); @@ -1295,6 +1296,8 @@ impl Actor { } } let Some(run) = entry.run.as_mut() else { + // A rewind stopped the run: start what it carried. + self.start(wallet_id); return; }; run.anchor_at = None; @@ -1304,7 +1307,7 @@ impl Actor { } // Still reading, with nothing forced: nothing it could // probe — stop its walk over the records now. - if run.probing.is_none() && !run.has_forced { + if run.probing.is_none() && run.forced.is_empty() { run.abort.abort(); self.end_run(wallet_id); } @@ -1437,7 +1440,7 @@ impl Actor { return; }; let anchor_at = pending.anchor_at; - let has_forced = !pending.forced.is_empty(); + let forced = pending.forced.clone(); // Walk the records if the pass may probe by schedule; one confined to // forced roots with none of them held has nothing to read for. let walk = Arc::new(AtomicBool::new(anchor_at.is_some())); @@ -1458,10 +1461,9 @@ impl Actor { entry.run = Some(Run { id: run, settled: HashSet::new(), - marks: true, woken: false, anchor_at, - has_forced, + forced, walk, abort, probing: None, @@ -1620,7 +1622,6 @@ impl Actor { &probing.views, &root, &report, - current.marks, ); deliver(&self.sink, events); self.probe_next(wallet_id); @@ -1645,7 +1646,7 @@ impl Actor { let transaction = Arc::clone(&root.transaction); let root_txid = root.txid; run.abort = self.jobs.spawn(async move { - let report = probe.probe_report(&transaction).await; + let report = probe.probe(&transaction).await; JobDone::Probed { wallet_id, run: id, @@ -1655,9 +1656,7 @@ impl Actor { }); self.job_runs .insert(run.abort.id(), (wallet_id, id, Some(root_txid))); - if run.marks { - mark_sent(&mut self.state, wallet_id, root.txid, probing.height); - } + mark_sent(&mut self.state, wallet_id, root.txid, probing.height); probing.in_flight = Some(root); return; } @@ -2196,7 +2195,7 @@ mod tests { #[async_trait] impl AcceptanceProbe for ScriptedProbe { - async fn probe(&self, transaction: &Transaction) -> ProbeVerdict { + async fn probe(&self, transaction: &Transaction) -> ProbeReport { let id = txid(transaction.lock_time as u8); self.probed.lock().expect("probed").push(id); self.answers @@ -2205,6 +2204,7 @@ mod tests { .unwrap_or(ProbeVerdict::Unresolved { reason: "scripted".to_string(), }) + .into() } } @@ -2289,17 +2289,9 @@ mod tests { root.txid, height, ); - let report = probe.probe_report(&root.transaction).await; + let report = probe.probe(&root.transaction).await; let mut guard = state.lock().expect("state"); - let events = record_probe( - &mut guard.state, - wallet_id, - height, - views, - &root, - &report, - true, - ); + let events = record_probe(&mut guard.state, wallet_id, height, views, &root, &report); guard.push(events); } let guard = state.lock().expect("state"); @@ -3832,18 +3824,25 @@ mod tests { ); } - /// A pass that started before a rewind does not mark its probe into the - /// restarted schedules. + /// A rewind stops a forced pass still reading; what was forced runs in + /// the pass after it, at the rewound height. #[tokio::test] - async fn should_not_mark_a_probe_from_before_a_rewind() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[]))).await; + async fn should_carry_a_forced_probe_over_a_rewind() { + let probe = Arc::new(ScriptedProbe::new(&[])); + let mut rig = enabled(probe.clone()).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.follow(wallet(), 100).await; + let before = probe.probed().len(); rig.source.pause(true); rig.actor.handle(Command::Uncertain(txid(1))); let listed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); rig.actor.joined(listed); // forced pass reading, queued at 101 + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 50); rig.actor.handle(Command::Height { wallet_id: wallet(), height: 50, @@ -3851,13 +3850,46 @@ mod tests { rig.source.pause(false); rig.settle().await; + assert_eq!( + probe.probed()[before..], + [txid(1)], + "probed once, after the rewind" + ); assert_eq!( rig.actor.state.schedules[&(wallet(), txid(1))].last_probe, - None, - "the restarted schedule is untouched by the older pass" + Some(50) ); } + /// A rewind stops the pass probing: the probe out is dropped and the + /// roots still due are not sent. + #[tokio::test] + async fn should_stop_probing_on_a_rewind() { + let probe = Arc::new(ScriptedProbe::new(&[])); + let mut rig = enabled(probe.clone()).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); + rig.height(wallet(), 100).await; + rig.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); // root 1's probe is out, root 2 due next + + rig.handle(Command::Height { + wallet_id: wallet(), + height: 50, + }); + rig.settle().await; + + assert!(probe.probed().is_empty()); + assert!(rig.actor.wallets[&wallet()].run.is_none()); + assert!(rig.actor.state.schedules.is_empty()); + } + /// A send built on two dead roots keeps its Dead while either is still /// dead. #[tokio::test] @@ -4145,8 +4177,8 @@ mod tests { assert!(!rig.actor.state.finished.contains_key(&(wallet(), txid(1)))); } - /// A probe sent before a rewind answers Mined after it: the answer is told - /// but does not end the root's probing across the replay. + /// A probe sent before a rewind would answer Mined: the rewind stops it, + /// so nothing ends the root's probing across the replay. #[tokio::test] async fn should_not_record_a_final_verdict_from_a_probe_sent_before_a_rewind() { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[( @@ -4172,8 +4204,9 @@ mod tests { assert!(!rig.actor.state.finished.contains_key(&(wallet(), txid(1)))); } - /// A probe sent before a rewind answers Dead after it: the answer belongs - /// to the state the rewind dropped — nothing is told or recorded. + /// A probe sent before a rewind would answer Dead: the answer belongs to + /// the state the rewind dropped — the rewind stops it, and nothing is told + /// or recorded. #[tokio::test] async fn should_neither_tell_nor_record_a_pre_rewind_answer() { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), dead())]))).await; @@ -4207,11 +4240,7 @@ mod tests { #[async_trait] impl AcceptanceProbe for OneNodeInBlockPerProbe { - async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { - unreachable!("the resolver asks for the report") - } - - async fn probe_report(&self, _transaction: &Transaction) -> ProbeReport { + async fn probe(&self, _transaction: &Transaction) -> ProbeReport { self.answers .lock() .expect("answers") @@ -4274,7 +4303,7 @@ mod tests { #[async_trait] impl AcceptanceProbe for PanicOnCall { - async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { + async fn probe(&self, _transaction: &Transaction) -> ProbeReport { let call = { let mut calls = self.calls.lock().expect("calls"); *calls += 1; @@ -4283,7 +4312,7 @@ mod tests { if call == self.nth { panic!("probe bug"); } - ProbeVerdict::Accepted + ProbeVerdict::Accepted.into() } } @@ -4322,7 +4351,7 @@ mod tests { #[async_trait] impl AcceptanceProbe for PanickingProbe { - async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { + async fn probe(&self, _transaction: &Transaction) -> ProbeReport { panic!("probe bug"); } } @@ -4368,7 +4397,7 @@ mod tests { #[async_trait] impl AcceptanceProbe for HangingProbe { - async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { + async fn probe(&self, _transaction: &Transaction) -> ProbeReport { std::future::pending().await } } @@ -4403,9 +4432,9 @@ mod tests { #[async_trait] impl AcceptanceProbe for SnapshotProbe { - async fn probe(&self, _transaction: &Transaction) -> ProbeVerdict { + async fn probe(&self, _transaction: &Transaction) -> ProbeReport { *self.seen.lock().expect("seen") = self.sink.0.lock().expect("sink").clone(); - ProbeVerdict::Accepted + ProbeVerdict::Accepted.into() } } From fbc1c6bd9ad97debc5f8c0a9a5e5bf5f9459cda5 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 09:10:19 +0300 Subject: [PATCH 048/169] fix(platform-wallet): hold the change of a send the network has not accepted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A send whose outcome is unknown leaves its change in the wallet as ordinary trusted mempool funds. If that send never reached the network the change is phantom, and every payment built on it is an orphan no node accepts — the shape behind "Transaction status unknown" followed by payments that never land. Such change (our own output, neither mined nor InstantSend-locked, of a send not marked accepted) is now withheld from selection in finalize and in the DashPay contact payment, and left out of pooled_spendable_balance and pooled_max_sendable. A send is marked accepted when a peer echoes it back or the broadcast resolver's probe finds it accepted or mined; an InstantSend lock or a block releases it on its own. The hold is a transient lock around add_funding under the manager write guard, so nothing persists on the UTXO. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/core/broadcast.rs | 6 + .../src/wallet/core/broadcast_resolver.rs | 70 +++++ .../src/wallet/core/generation.rs | 36 ++- .../rs-platform-wallet/src/wallet/core/mod.rs | 1 + .../src/wallet/core/transaction.rs | 278 +++++++++++++++++- .../src/wallet/identity/network/payments.rs | 7 +- 6 files changed, 394 insertions(+), 4 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs index 1879f18346c..2f152336894 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs @@ -156,6 +156,12 @@ impl CoreWallet { // guard, covers check-to-wire. }; let outcome = self.broadcaster.broadcast(transaction).await; + // An accepted send — a peer echoed it back — may fund the next payment + // with its change before the InstantSend lock arrives; an ambiguous one + // may not (`is_unresolved_change`). + if outcome.is_ok() { + self.generation().mark_send_accepted(transaction.txid()); + } // The pin already fences by default, so the inputs stay held on EVERY // exit from the await above — including one this code never observes: // the dispatching future being cancelled, or an unwind, mid-`broadcast`. diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 98d32e2acc1..f9ee67156b2 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -916,6 +916,11 @@ pub(crate) trait WalletSource: Send + Sync { walk: &AtomicBool, forced: HashSet, ) -> Option>; + + /// Record that a probe found these sends of the wallet accepted, so their + /// change may fund the next payment + /// ([`WalletGeneration::mark_send_accepted`](crate::wallet::core::WalletGeneration::mark_send_accepted)). + async fn mark_accepted(&self, wallet_id: WalletId, txids: Vec); } /// Whether any account of the wallet records `txid` — a map lookup per @@ -932,6 +937,16 @@ struct ManagerSource(Arc>>); #[async_trait] impl WalletSource for ManagerSource { + async fn mark_accepted(&self, wallet_id: WalletId, txids: Vec) { + let manager = self.0.read().await; + let Some(info) = manager.get_wallet_info(&wallet_id) else { + return; + }; + for txid in txids { + info.generation.mark_send_accepted(txid); + } + } + async fn holders(&self, txid: Txid) -> Vec { let manager = self.0.read().await; manager @@ -1623,12 +1638,34 @@ impl Actor { &root, &report, ); + self.mark_accepted(wallet_id, &events); deliver(&self.sink, events); self.probe_next(wallet_id); } } } + /// Hand the sends these events find accepted (or mined) to the wallet, so + /// their change is offered to payments again. Off the actor: the mark + /// takes the manager lock, and a set insert needs no ordering against + /// the actor's own work. + fn mark_accepted(&self, wallet_id: WalletId, events: &[Outgoing]) { + let txids: Vec = events + .iter() + .filter_map(|item| match &item.event { + ResolverEvent::Verdict(txid, ProbeVerdict::Accepted | ProbeVerdict::Mined) => { + Some(*txid) + } + _ => None, + }) + .collect(); + if txids.is_empty() { + return; + } + let source = Arc::clone(&self.source); + tokio::spawn(async move { source.mark_accepted(wallet_id, txids).await }); + } + /// Probe the run's next due root, or end the run. fn probe_next(&mut self, wallet_id: WalletId) { let Some(entry) = self.wallets.get_mut(&wallet_id) else { @@ -2686,6 +2723,8 @@ mod tests { /// Reads wait while set, so a test can hold a pass in its read. paused: StdMutex, resume: Notify, + /// Sends marked accepted, per wallet. + accepted: StdMutex>, } impl FakeSource { @@ -2709,6 +2748,11 @@ mod tests { #[async_trait] impl WalletSource for FakeSource { + async fn mark_accepted(&self, wallet_id: WalletId, txids: Vec) { + let mut accepted = self.accepted.lock().expect("accepted"); + accepted.extend(txids.into_iter().map(|txid| (wallet_id, txid))); + } + async fn holders(&self, txid: Txid) -> Vec { self.views .lock() @@ -2863,6 +2907,32 @@ mod tests { assert!(rig.sent().is_empty()); } + /// A send a probe finds accepted is marked so on the wallet, so its change + /// may fund the next payment; an unresolved one is not. + #[tokio::test] + async fn should_mark_a_send_found_accepted_and_only_that_one() { + let probe = Arc::new(ScriptedProbe::new(&[ + (txid(1), ProbeVerdict::Accepted), + (txid(2), unresolved()), + ])); + let mut rig = enabled(probe).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); + rig.send(Command::Uncertain(txid(1))).await; + rig.send(Command::Uncertain(txid(2))).await; + // The mark runs off the actor. + for _ in 0..10 { + tokio::task::yield_now().await; + } + + assert_eq!( + *rig.source.accepted.lock().expect("accepted"), + vec![(wallet(), txid(1))] + ); + } + /// An `Uncertain` result carries no wallet id: every wallet hears it, but /// only the one holding the transaction walks its records. #[tokio::test] diff --git a/packages/rs-platform-wallet/src/wallet/core/generation.rs b/packages/rs-platform-wallet/src/wallet/core/generation.rs index 5224985f838..6c99c9b5867 100644 --- a/packages/rs-platform-wallet/src/wallet/core/generation.rs +++ b/packages/rs-platform-wallet/src/wallet/core/generation.rs @@ -3,11 +3,11 @@ //! in-broadcast outpoint pins that fence a mid-dispatch transaction's inputs //! against concurrent re-selection. -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::ops::Deref; use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; -use dashcore::{OutPoint, Transaction}; +use dashcore::{OutPoint, Transaction, Txid}; use tokio::sync::{OwnedRwLockWriteGuard, RwLock, RwLockReadGuard}; use super::balance::WalletBalance; @@ -255,6 +255,18 @@ impl std::fmt::Debug for SettleBoundaryHook { #[derive(Debug, Default)] pub(crate) struct InBroadcastFences { fences: Mutex>, + /// Outgoing sends the network has been seen to accept before the wallet + /// saw them InstantSend-locked or mined — a peer echoed the broadcast back, + /// or the broadcast resolver's probe found them in a node's mempool or a + /// block. Their change may fund the next payment; the change of any other + /// unconfirmed send of ours may not (see + /// [`WalletGeneration::is_send_accepted`]). + /// + /// Shared like the fences: acceptance is a fact about the network, not + /// about one in-memory instance. Not persisted — after a restart a send + /// still unconfirmed and unlocked is held again until the lock, a block or + /// a fresh verdict arrives, which only errs on the side of not spending. + accepted_sends: Mutex>, } impl InBroadcastFences { @@ -263,6 +275,10 @@ impl InBroadcastFences { fn lock(&self) -> MutexGuard<'_, HashMap> { self.fences.lock().unwrap_or_else(PoisonError::into_inner) } + + fn accepted(&self) -> MutexGuard<'_, HashSet> { + self.accepted_sends.lock().unwrap_or_else(PoisonError::into_inner) + } } /// One outpoint's broadcast fence — see `WalletGeneration::in_broadcast`. @@ -397,6 +413,22 @@ impl WalletGeneration { } } + /// Record that the network accepted the outgoing send `txid` — see + /// [`is_send_accepted`](Self::is_send_accepted). + pub(crate) fn mark_send_accepted(&self, txid: Txid) { + self.in_broadcast.accepted().insert(txid); + } + + /// Whether the network has been seen to accept the outgoing send `txid`. + /// + /// Until it has — or the wallet sees it InstantSend-locked or mined — the + /// change of that send is not offered to a new payment: if the send never + /// reached the network, a payment built on its change is an orphan no node + /// accepts, and every later one built on that change is too. + pub(crate) fn is_send_accepted(&self, txid: &Txid) -> bool { + self.in_broadcast.accepted().contains(txid) + } + /// This generation's lock-free balance. pub fn balance(&self) -> &WalletBalance { &self.balance diff --git a/packages/rs-platform-wallet/src/wallet/core/mod.rs b/packages/rs-platform-wallet/src/wallet/core/mod.rs index 2b05316555b..d9a61a35412 100644 --- a/packages/rs-platform-wallet/src/wallet/core/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/core/mod.rs @@ -17,4 +17,5 @@ pub(crate) use generation::{InBroadcastFences, InBroadcastPin}; pub use spend_observer::SpendObservationHandler; pub(crate) use transaction::resolve_source_accounts; pub use transaction::{SignedCoreTransaction, ASSET_LOCK_FUNDING_SOURCES, SEND_FUNDING_SOURCES}; +pub(crate) use transaction::add_funding_withholding_unresolved_change; pub use wallet::CoreWallet; diff --git a/packages/rs-platform-wallet/src/wallet/core/transaction.rs b/packages/rs-platform-wallet/src/wallet/core/transaction.rs index de4bdb692c0..d1934100122 100644 --- a/packages/rs-platform-wallet/src/wallet/core/transaction.rs +++ b/packages/rs-platform-wallet/src/wallet/core/transaction.rs @@ -12,6 +12,7 @@ use dashcore::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction}; use key_wallet::account::AccountType; use key_wallet::managed_account::managed_account_collection::ManagedAccountCollection; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; +use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionError; use key_wallet::wallet::managed_wallet_info::fee::{estimate_tx_size, FeeRate}; use key_wallet::wallet::managed_wallet_info::transaction_builder::{ @@ -20,6 +21,7 @@ use key_wallet::wallet::managed_wallet_info::transaction_builder::{ use key_wallet::wallet::managed_wallet_info::transaction_building::AccountTypePreference; use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use key_wallet::wallet::Wallet; +use key_wallet::Account; use key_wallet::{DerivationPath, ReservationToken, Utxo}; use super::{CoreWallet, WalletGeneration}; @@ -83,6 +85,57 @@ fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> Platfo /// down instead of surfacing as an error the caller can show — and a profile /// without overflow checks is worse still, wrapping silently into a fee that /// makes the reported maximum nonsense. Both become a typed error here. +/// Whether `utxo` is the change of one of our sends that the network has not +/// been seen to accept: our own output (`is_trusted`), neither mined nor +/// InstantSend-locked, of a send not marked accepted on `generation`. +/// +/// Such a coin exists only while that send's outcome is unknown. If the send +/// never reached the network the coin is phantom, and a payment built on it is +/// an orphan no node accepts — the shape behind "Transaction status unknown" +/// followed by payments that never land. So it is not offered to a new +/// payment, and not counted as spendable, until the send is accepted. +pub(crate) fn is_unresolved_change(utxo: &Utxo, generation: &WalletGeneration) -> bool { + utxo.is_trusted + && !utxo.is_confirmed + && !utxo.is_instantlocked + && !generation.is_send_accepted(&utxo.outpoint.txid) +} + +/// [`TransactionBuilder::add_funding`] with the account's unresolved change +/// (see [`is_unresolved_change`]) withheld from selection. +/// +/// `add_funding` copies the account's UTXOs into the builder as candidates, +/// and selection drops a candidate that is locked. So the held coins are +/// locked for that one call and unlocked right after — the copies keep the +/// lock, the wallet does not. The caller holds the manager write guard across +/// it, so no other reader sees the transient lock. A coin already locked for +/// another reason is left as it is. +pub(crate) fn add_funding_withholding_unresolved_change( + builder: TransactionBuilder, + managed: &mut ManagedCoreFundsAccount, + account: &Account, + generation: &WalletGeneration, +) -> TransactionBuilder { + let held: Vec = managed + .utxos + .values() + .filter(|utxo| !utxo.is_locked && is_unresolved_change(utxo, generation)) + .map(|utxo| utxo.outpoint) + .collect(); + for outpoint in &held { + if let Some(utxo) = managed.utxos.get_mut(outpoint) { + utxo.lock(); + } + } + let builder = builder.add_funding(managed, account); + for outpoint in &held { + if let Some(utxo) = managed.utxos.get_mut(outpoint) { + utxo.unlock(); + } + } + builder +} + fn checked_fee(fee_rate: FeeRate, size_bytes: usize) -> Result { fee_rate .as_sat_per_kb() @@ -459,6 +512,7 @@ impl CoreWallet { total += managed .spendable_utxos(height) .iter() + .filter(|utxo| !is_unresolved_change(utxo, &info.generation)) .map(|utxo| utxo.value()) .sum::(); } @@ -534,6 +588,7 @@ impl CoreWallet { managed .spendable_utxos(height) .iter() + .filter(|utxo| !is_unresolved_change(utxo, &info.generation)) .map(|utxo| utxo.value()) .filter(|value| *value > input_cost), ); @@ -691,10 +746,17 @@ impl CoreWallet { paths.insert(utxo.address.clone(), path); } } + // `reservation_only` offers no candidates of its own: the + // caller seeded its inputs explicitly. builder = if reservation_only { builder.add_funding_reservation_only(managed, account) } else { - builder.add_funding(managed, account) + add_funding_withholding_unresolved_change( + builder, + managed, + account, + &info.generation, + ) }; offered_accounts.push(at); } @@ -2007,4 +2069,218 @@ mod tests { Err(PlatformWalletError::CoreInsufficientFunds { .. }) )); } + + // ---- Change of a send the network has not been seen to accept ----------- + + /// Turn the BIP44 fixture UTXO into what a send whose outcome is still + /// unknown leaves behind: our own change, in the mempool, neither + /// InstantSend-locked nor mined. Returns its outpoint. + async fn make_unresolved_change( + manager: &Arc>>, + wallet_id: &crate::wallet::platform_wallet::WalletId, + ) -> dashcore::OutPoint { + let mut manager = manager.write().await; + let info = manager.get_wallet_info_mut(wallet_id).expect("wallet"); + let managed = info + .core_wallet + .accounts + .funds_account_mut(&key_wallet::account::AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }) + .expect("bip44 account"); + let utxo = managed.utxos.values_mut().next().expect("bip44 utxo"); + utxo.is_trusted = true; + utxo.is_confirmed = false; + utxo.is_instantlocked = false; + utxo.outpoint + } + + async fn dual_core( + broadcaster_ok: bool, + ) -> ( + CoreWallet, + Arc>>, + crate::wallet::platform_wallet::WalletId, + Arc, + WalletSigner, + ) { + let (manager, wallet_id, generation, signer) = + funded_wallet_manager_dual_standard(&[700_000], &[700_000]).await; + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let broadcaster: Arc = if broadcaster_ok { + Arc::new(AlwaysOkBroadcaster) + } else { + Arc::new(AlwaysMaybeSentBroadcaster) + }; + let core = CoreWallet::new( + sdk, + Arc::clone(&manager), + wallet_id, + broadcaster, + Arc::clone(&generation), + ); + (core, manager, wallet_id, generation, signer) + } + + /// The customer's shape (support ticket 32347): the change of a send whose + /// outcome is unknown must not fund the next payment — if that send never + /// reached the network, the next one would be an orphan no node accepts. + /// It is left out of the spendable figure, of "max", and of selection. + #[tokio::test] + async fn unresolved_change_is_not_offered_to_a_new_payment() { + let (core, manager, wallet_id, _generation, signer) = dual_core(true).await; + make_unresolved_change(&manager, &wallet_id).await; + + let gross = core + .pooled_spendable_balance(&crate::SEND_FUNDING_SOURCES, 0) + .await + .expect("gross balance"); + assert_eq!(gross, 700_000, "only the BIP32 coin is spendable"); + let max = core + .pooled_max_sendable(&crate::SEND_FUNDING_SOURCES, 0, None) + .await + .expect("max sendable"); + assert!(max < 700_000 && max > 0, "max draws on the BIP32 coin only, got {max}"); + + // 1_000_000 needs both coins: the held one is not there to take. + let refused = core + .finalize_transaction(payment_builder(80), &crate::SEND_FUNDING_SOURCES, 0, &signer) + .await; + assert!( + matches!( + refused, + Err(PlatformWalletError::CorePooledInsufficientFunds { + available: Some(700_000), + .. + }) + ), + "a payment only the held change could fund must fail as insufficient funds, got {refused:?}" + ); + + // A payment the other coin covers builds, and does not touch the held one. + let small = TransactionBuilder::new() + .add_output(&DashAddress::dummy(Network::Testnet, 81), 300_000); + let finalized = core + .finalize_transaction(small, &crate::SEND_FUNDING_SOURCES, 0, &signer) + .await + .expect("the BIP32 coin covers it"); + assert_eq!(finalized.transaction().input.len(), 1); + core.abandon_transaction(&finalized).await; + } + + /// Withholding is not a lock left behind: after a build the held UTXO is + /// still unlocked in the wallet, so it reads as ordinary funds to every + /// other reader and is offered again the moment its send is accepted. + #[tokio::test] + async fn withholding_leaves_no_lock_on_the_wallet() { + let (core, manager, wallet_id, _generation, signer) = dual_core(true).await; + let held = make_unresolved_change(&manager, &wallet_id).await; + let small = TransactionBuilder::new() + .add_output(&DashAddress::dummy(Network::Testnet, 82), 300_000); + let finalized = core + .finalize_transaction(small, &crate::SEND_FUNDING_SOURCES, 0, &signer) + .await + .expect("the BIP32 coin covers it"); + core.abandon_transaction(&finalized).await; + + let manager = manager.read().await; + let info = manager.get_wallet_info(&wallet_id).expect("wallet"); + let utxo = info + .core_wallet + .accounts + .funds_account(&key_wallet::account::AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }) + .and_then(|managed| managed.utxos.get(&held)) + .expect("held utxo still recorded"); + assert!(!utxo.is_locked, "the hold must not persist as a UTXO lock"); + } + + /// Accepted by the network — the resolver's verdict or a peer echo marks + /// it — the change funds payments again. + #[tokio::test] + async fn unresolved_change_is_offered_once_its_send_is_accepted() { + let (core, manager, wallet_id, generation, signer) = dual_core(true).await; + let held = make_unresolved_change(&manager, &wallet_id).await; + generation.mark_send_accepted(held.txid); + + let gross = core + .pooled_spendable_balance(&crate::SEND_FUNDING_SOURCES, 0) + .await + .expect("gross balance"); + assert_eq!(gross, 1_400_000); + let finalized = core + .finalize_transaction(payment_builder(83), &crate::SEND_FUNDING_SOURCES, 0, &signer) + .await + .expect("both coins fund it once the send is accepted"); + assert_eq!(finalized.transaction().input.len(), 2); + core.abandon_transaction(&finalized).await; + } + + /// An InstantSend lock (or a block) is acceptance the wallet sees for + /// itself, with no mark needed. + #[tokio::test] + async fn unresolved_change_is_offered_once_instant_locked() { + let (core, manager, wallet_id, _generation, _signer) = dual_core(true).await; + let held = make_unresolved_change(&manager, &wallet_id).await; + { + let mut manager = manager.write().await; + let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet"); + let managed = info + .core_wallet + .accounts + .funds_account_mut(&key_wallet::account::AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }) + .expect("bip44 account"); + managed.utxos.get_mut(&held).expect("utxo").is_instantlocked = true; + } + let gross = core + .pooled_spendable_balance(&crate::SEND_FUNDING_SOURCES, 0) + .await + .expect("gross balance"); + assert_eq!(gross, 1_400_000); + } + + /// A send built on held change cannot happen through selection, but one + /// recorded before this rule (or restored from storage) is itself an + /// unresolved send: accepting its parent does not release the child's + /// own change. + #[tokio::test] + async fn the_change_of_a_child_stays_held_when_only_the_parent_is_accepted() { + let (core, manager, wallet_id, generation, _signer) = dual_core(true).await; + let child = make_unresolved_change(&manager, &wallet_id).await; + generation.mark_send_accepted(::from_byte_array([7; 32])); + let gross = core + .pooled_spendable_balance(&crate::SEND_FUNDING_SOURCES, 0) + .await + .expect("gross balance"); + assert_eq!(gross, 700_000, "the child {} is still unresolved", child.txid); + } + + /// A send the network echoed back is accepted on the spot: its change can + /// fund the next payment before the InstantSend lock arrives. An ambiguous + /// outcome marks nothing. + #[tokio::test] + async fn an_echoed_send_is_marked_accepted_and_an_ambiguous_one_is_not() { + for (ok, expected) in [(true, true), (false, false)] { + let (core, _manager, _wallet_id, generation, signer) = dual_core(ok).await; + let small = TransactionBuilder::new() + .add_output(&DashAddress::dummy(Network::Testnet, 84), 300_000); + let finalized = core + .finalize_transaction(small, &crate::SEND_FUNDING_SOURCES, 0, &signer) + .await + .expect("finalize"); + let txid = finalized.transaction().txid(); + let _ = core.broadcast_finalized_transaction(&finalized).await; + assert_eq!( + generation.is_send_accepted(&txid), + expected, + "broadcaster ok={ok}: accepted mark" + ); + } + } } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index a5b49b41bc6..7cb4675dd1f 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -1281,7 +1281,12 @@ impl DashPayView<'_, B> { funding_paths.insert(utxo.address.clone(), path); } } - builder = builder.add_funding(managed, account); + builder = crate::wallet::core::add_funding_withholding_unresolved_change( + builder, + managed, + account, + &info.generation, + ); offered_accounts.push(at); } } From cdc07ffd01c26931c465e5f2cf01920ac67d5219 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 09:46:00 +0300 Subject: [PATCH 049/169] fix(platform-wallet): hold every coin of an unresolved send, not only trusted change The first cut keyed the hold on key-wallet's `is_trusted` and an ever-growing set of accepted txids. That missed the receiving side of a transfer between our own accounts and the change of a send funded by an unconfirmed incoming coin, never let go of a send found dead after it was found accepted, and left the asset-lock builder and outpoint-seeded builds outside the hold. `UnresolvedSends` now derives the held set from the wallet's records on every read: every own send that is neither InstantSend-locked nor mined and that the network was not last seen to accept, plus every unsettled transaction spending one of its outputs. Restart behaves the same; only acceptance facts live in memory, as sequenced records (echo on broadcast, probe Accepted/Mined/Dead), so a later Dead wins over an earlier Accepted, and the record is pruned to the wallet's unsettled sends. Applied to finalize (selection and a backstop on seeded inputs), the DashPay contact payment, the asset-lock builder, and the spendable / max figures. A payment that only held coins would cover now fails with `CoreFundsAwaitingNetwork` (FFI code 59) instead of insufficient funds; a genuine shortfall stays insufficient. Echo marks are also recorded by the contact payment, the plain broadcast and the startup re-dispatch. Co-Authored-By: Claude Opus 5.5 --- packages/rs-platform-wallet-ffi/src/error.rs | 32 + packages/rs-platform-wallet/src/error.rs | 22 + .../rs-platform-wallet/src/manager/load.rs | 7 +- .../rs-platform-wallet/src/test_support.rs | 27 +- .../src/wallet/asset_lock/build.rs | 120 +++- .../src/wallet/core/broadcast.rs | 11 +- .../src/wallet/core/broadcast_resolver.rs | 103 ++- .../src/wallet/core/generation.rs | 81 ++- .../rs-platform-wallet/src/wallet/core/mod.rs | 3 +- .../src/wallet/core/transaction.rs | 613 ++++++++++++------ .../src/wallet/core/unresolved.rs | 164 +++++ .../src/wallet/identity/network/payments.rs | 27 +- .../PlatformWallet/PlatformWalletResult.swift | 11 + 13 files changed, 931 insertions(+), 290 deletions(-) create mode 100644 packages/rs-platform-wallet/src/wallet/core/unresolved.rs diff --git a/packages/rs-platform-wallet-ffi/src/error.rs b/packages/rs-platform-wallet-ffi/src/error.rs index 5e7bc342baf..c77e348b74d 100644 --- a/packages/rs-platform-wallet-ffi/src/error.rs +++ b/packages/rs-platform-wallet-ffi/src/error.rs @@ -599,6 +599,14 @@ pub enum PlatformWalletFFIResultCode { /// Platform returned no balance for a managed identity. Retrying this read /// is safe; this does not imply missing ownership or require registration. ErrorIdentityBalanceUnavailable = 58, + /// A Core payment needs coins held back because they came from one of the + /// wallet's own sends whose broadcast outcome is still unknown + /// ([`platform_wallet::PlatformWalletError::CoreFundsAwaitingNetwork`]). + /// Not a shortfall and nothing was sent: the coins become spendable once + /// that send is InstantSend-locked, mined, or found accepted, so the host + /// should say the money is waiting on the network and offer a retry + /// later — not "insufficient funds". + ErrorCoreFundsAwaitingNetwork = 59, /// The named thing does not exist. /// @@ -966,6 +974,9 @@ impl From for PlatformWalletFFIResult { PlatformWalletError::AddressNonceMismatch { .. } => { PlatformWalletFFIResultCode::ErrorAddressNonceMismatch } + PlatformWalletError::CoreFundsAwaitingNetwork { .. } => { + PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork + } // Both shapes are "the wallet cannot cover this payment"; hosts // classify and retry them identically, so the pooled variant rides // the same code rather than forcing every host to learn a second @@ -2578,6 +2589,27 @@ mod tests { } } +#[cfg(test)] +mod core_funds_awaiting_network_tests { + use super::*; + + #[test] + fn should_give_held_funds_their_own_code_apart_from_insufficient_funds() { + let result = PlatformWalletFFIResult::from(PlatformWalletError::CoreFundsAwaitingNetwork { + available: Some(700_000), + held: 599_000, + required: Some(1_000_000), + outpoint: None, + }); + assert_eq!( + result.code, + PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork + ); + assert_eq!(result.code as u32, 59); + assert!(!result.message.is_null()); + } +} + #[cfg(test)] mod identity_balance_error_tests { use super::*; diff --git a/packages/rs-platform-wallet/src/error.rs b/packages/rs-platform-wallet/src/error.rs index d87b3f1ba58..d6f1395a706 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -393,6 +393,28 @@ pub enum PlatformWalletError { required: Option, }, + /// A Core payment the wallet's balance covers, but only with coins held + /// back because they came from one of its own sends the network has not + /// been seen to accept yet (its broadcast outcome is unknown). Spending + /// them before that is settled builds a transaction no node accepts if + /// that send never reached the network. They become spendable once the + /// send is InstantSend-locked, mined, or found accepted. Not a shortfall: + /// the host should say the money is waiting on the network. + /// + /// `held` is the held value among the offered sources. `outpoint` names + /// a held coin the caller chose as an input itself (outpoint-seeded or + /// `reservation_only` builds); `None` when selection came up short. + #[error( + "Core funds held until an earlier send is accepted by the network: \ + available {available:?}, held {held}, required {required:?}" + )] + CoreFundsAwaitingNetwork { + available: Option, + held: u64, + required: Option, + outpoint: Option, + }, + #[error("no spendable inputs available on {account_type} account {account_index}: {context}")] NoSpendableInputs { account_type: StandardAccountType, diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 7f3ba96605b..dd2e579d823 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -618,7 +618,12 @@ pub(super) async fn resend_one( let txid = tx.txid(); match broadcaster.broadcast(tx).await { - Ok(_) => tracing::info!(%txid, "load: re-dispatched unconfirmed send, accepted"), + Ok(_) => { + // The one acceptance signal for a send still unconfirmed after a + // restart; its change may fund payments again (`UnresolvedSends`). + generation.mark_send_accepted(txid); + tracing::info!(%txid, "load: re-dispatched unconfirmed send, accepted") + } // Expected for the orphaned case: sent, no acceptance signal, now // owned by the rebroadcast timer. Err(BroadcastError::MaybeSent { reason }) => tracing::info!( diff --git a/packages/rs-platform-wallet/src/test_support.rs b/packages/rs-platform-wallet/src/test_support.rs index b9446432ee0..f3bd192df52 100644 --- a/packages/rs-platform-wallet/src/test_support.rs +++ b/packages/rs-platform-wallet/src/test_support.rs @@ -25,7 +25,7 @@ use key_wallet::bip32::ExtendedPubKey; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; use key_wallet::signer::{ExtendedPubKeySigner, Signer, SignerMethod}; use key_wallet::test_utils::TestWalletContext; -use key_wallet::transaction_checking::{BlockInfo, TransactionContext}; +use key_wallet::transaction_checking::{BlockInfo, TransactionContext, WalletTransactionChecker}; use key_wallet::{DerivationPath, Wallet}; use key_wallet_manager::WalletManager; use tokio::sync::RwLock; @@ -626,6 +626,31 @@ where target } +/// Apply `transaction` to the wallet in `context` — what SPV does when it sees +/// it in a peer's mempool (`Mempool`), InstantSend-locked or in a block. A send +/// this wallet signed, applied in `Mempool` and never echoed back, is what an +/// unknown broadcast outcome leaves behind. +pub async fn apply_to_wallet( + manager: &Arc>>, + wallet_id: &WalletId, + transaction: &Transaction, + context: TransactionContext, +) { + let mut wm = manager.write().await; + let (wallet, info) = wm + .get_wallet_and_info_mut(wallet_id) + .expect("wallet present in manager"); + let mut wallet = wallet.clone(); + let result = info + .core_wallet + .check_core_transaction(transaction, context, &mut wallet, true, true) + .await; + assert!( + result.is_relevant, + "the transaction must be the wallet's own" + ); +} + /// No-op persister satisfying [`PlatformWalletManager`] construction for tests /// that need a full [`PlatformWallet`] but no real persistence pipeline. pub struct NoopTestPersister; diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs index 951c1e21ce4..fed57872f6a 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs @@ -24,6 +24,7 @@ use key_wallet::wallet::Wallet; use crate::changeset::{AccountRegistrationEntry, PlatformWalletChangeSet}; use crate::error::PlatformWalletError; +use crate::wallet::core::UnresolvedSends; use crate::wallet::platform_wallet::PlatformWalletInfo; use crate::ASSET_LOCK_FUNDING_SOURCES; @@ -273,7 +274,14 @@ impl AssetLockManager { // pools the sources, enforces that CoinJoin funding is drain-only and // unpooled, and reserves the selected inputs in each contributing // account's own set under one owner token). - let result = info + // Coins of our own sends the network has not been seen to accept stay + // out of the builder's own selection (see `UnresolvedSends`): locked + // across the call, under the write guard held since `info` was taken, + // and unlocked before anything else reads the wallet. + let unresolved = UnresolvedSends::of(info); + let held_value = unresolved.held_value_in(&info.core_wallet.accounts); + let held_locks = unresolved.lock_in(&mut info.core_wallet.accounts); + let built = info .core_wallet .build_asset_lock_with_signer( wallet, @@ -284,7 +292,9 @@ impl AssetLockManager { drain, signer, ) - .await + .await; + UnresolvedSends::unlock_in(&mut info.core_wallet.accounts, &held_locks); + let result = built .map_err(|e| { // A drain's credit-output value is a zero placeholder, so it // must not be advertised as the `required` amount of a typed @@ -299,6 +309,20 @@ impl AssetLockManager { } }; map_builder_error(e, required) + }) + .map_err(|error| match error { + PlatformWalletError::AssetLockInsufficientFunds { + available, + required, + } if held_value > 0 && available.saturating_add(held_value) >= required => { + PlatformWalletError::CoreFundsAwaitingNetwork { + available: Some(available), + held: held_value, + required: Some(required), + outpoint: None, + } + } + other => other, })?; // Refuse a selection that picked an input pinned by an IN-FLIGHT @@ -336,6 +360,27 @@ impl AssetLockManager { return Err(PlatformWalletError::InputMidBroadcast { outpoint }); } + // Backstop for the hold above: the builder's selection cannot take a + // held coin while it is locked. + if let Some(outpoint) = unresolved.held_input(&result.transaction) { + for funding_account in &result.funding_accounts { + if let Some(account) = info.core_wallet.accounts.funds_account(funding_account) { + match result.reservation_token { + Some(token) => { + account.release_reservation_if_owner(&result.transaction, token) + } + None => account.release_reservation(&result.transaction), + } + } + } + return Err(PlatformWalletError::CoreFundsAwaitingNetwork { + available: None, + held: held_value, + required: None, + outpoint: Some(outpoint), + }); + } + // 4. Pull the (pubkey, path) for our single credit output. // // `build_asset_lock_with_signer` always returns the `Public` @@ -1379,16 +1424,20 @@ mod tests { ClientStartState, PersistenceError, PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::test_support::{ - funded_wallet_manager, funded_wallet_manager_dual_standard, + apply_to_wallet, funded_wallet_manager, funded_wallet_manager_dual_standard, funded_wallet_manager_with_contact, AlwaysMaybeSentBroadcaster, AlwaysOkBroadcaster, AlwaysRejectedBroadcaster, WalletSigner, }; use crate::wallet::asset_lock::manager::AssetLockManager; use crate::wallet::asset_lock::tracked::AssetLockStatus; + use crate::wallet::core::CoreWallet; use crate::wallet::persister::WalletPersister; use crate::wallet::platform_wallet::PlatformWalletInfo; use crate::wallet::platform_wallet::WalletId; + use crate::SEND_FUNDING_SOURCES; use crate::{AssetLockFundingType, PlatformWalletError}; + use dashcore::{Address as DashAddress, Network}; + use key_wallet::wallet::managed_wallet_info::transaction_builder::TransactionBuilder; /// The zero-spendable-candidate selection error must surface the SAME /// typed shortfall as a partial shortfall (not the generic string form), @@ -1901,6 +1950,71 @@ mod tests { (manager, signer) } + /// An asset lock that only the coins of an unresolved send of ours would + /// fund is refused as waiting on the network, not as a shortfall: those + /// coins become usable once that send settles. + #[tokio::test] + async fn should_report_held_coins_as_awaiting_network_in_an_asset_lock() { + let (wallet_manager, wallet_id, generation, signer) = + funded_wallet_manager(StandardAccountType::BIP44Account).await; + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let core = CoreWallet::new( + Arc::clone(&sdk), + Arc::clone(&wallet_manager), + wallet_id, + Arc::new(AlwaysOkBroadcaster), + generation, + ); + // Spend the one confirmed coin; its change is all the wallet has left. + let finalized = core + .finalize_transaction( + TransactionBuilder::new() + .add_output(&DashAddress::dummy(Network::Testnet, 95), 1_000_000), + &SEND_FUNDING_SOURCES, + 0, + &signer, + ) + .await + .expect("the send builds"); + apply_to_wallet( + &wallet_manager, + &wallet_id, + finalized.transaction(), + TransactionContext::Mempool, + ) + .await; + let manager = AssetLockManager::new( + sdk, + wallet_manager, + wallet_id, + Arc::new(Notify::new()), + Arc::new(AlwaysOkBroadcaster), + WalletPersister::new( + wallet_id, + Arc::new(CapturingPersistence::default()) as Arc, + ), + ); + + let refused = manager + .create_funded_asset_lock_proof( + 1_000_000, + 0, + AssetLockFundingType::IdentityRegistration, + 0, + &signer, + ) + .await; + + assert!( + matches!( + refused, + Err(PlatformWalletError::CoreFundsAwaitingNetwork { held, .. }) if held > 1_000_000 + ), + "got {:?}", + refused.map(|_| ()) + ); + } + /// Regression: a build must persist the funding account's address-pool /// snapshot with the newly-used index. The asset-lock funding accounts fund /// OP_RETURN-payload credit outputs that never appear as on-chain UTXOs, so diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs index 2f152336894..a008b498967 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs @@ -158,7 +158,7 @@ impl CoreWallet { let outcome = self.broadcaster.broadcast(transaction).await; // An accepted send — a peer echoed it back — may fund the next payment // with its change before the InstantSend lock arrives; an ambiguous one - // may not (`is_unresolved_change`). + // may not (`UnresolvedSends`). if outcome.is_ok() { self.generation().mark_send_accepted(transaction.txid()); } @@ -290,10 +290,11 @@ impl CoreWallet { &self, transaction: &Transaction, ) -> Result { - self.broadcaster - .broadcast(transaction) - .await - .map_err(Into::into) + let txid = self.broadcaster.broadcast(transaction).await?; + // Accepted: its change may fund the next payment before the + // InstantSend lock arrives (see `UnresolvedSends`). + self.generation().mark_send_accepted(txid); + Ok(txid) } /// Broadcast a signed transaction, reconciling the funding account's UTXO diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index f9ee67156b2..d5f0028d78e 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -86,6 +86,7 @@ use tokio::time::{timeout_at, Instant}; use crate::broadcast_probe::{AcceptanceProbe, ProbeReport, ProbeVerdict, MINED_QUORUM}; use crate::events::{PlatformEventHandler, PlatformEventManager}; +use crate::wallet::core::generation::next_acceptance_seq; use crate::wallet::platform_wallet::PlatformWalletInfo; /// Blocks from the start of a root's window (see [`ProbeSchedule`]) during @@ -917,10 +918,12 @@ pub(crate) trait WalletSource: Send + Sync { forced: HashSet, ) -> Option>; - /// Record that a probe found these sends of the wallet accepted, so their - /// change may fund the next payment - /// ([`WalletGeneration::mark_send_accepted`](crate::wallet::core::WalletGeneration::mark_send_accepted)). - async fn mark_accepted(&self, wallet_id: WalletId, txids: Vec); + /// Record what probes found about these sends of the wallet — accepted + /// (`true`) or dead (`false`) — observed at `seq`, so the coins of an + /// accepted send may fund the next payment and those of a dead one may + /// not + /// ([`WalletGeneration::record_acceptance`](crate::wallet::core::WalletGeneration::record_acceptance)). + async fn record_acceptance(&self, wallet_id: WalletId, verdicts: Vec<(Txid, bool)>, seq: u64); } /// Whether any account of the wallet records `txid` — a map lookup per @@ -937,13 +940,13 @@ struct ManagerSource(Arc>>); #[async_trait] impl WalletSource for ManagerSource { - async fn mark_accepted(&self, wallet_id: WalletId, txids: Vec) { + async fn record_acceptance(&self, wallet_id: WalletId, verdicts: Vec<(Txid, bool)>, seq: u64) { let manager = self.0.read().await; let Some(info) = manager.get_wallet_info(&wallet_id) else { return; }; - for txid in txids { - info.generation.mark_send_accepted(txid); + for (txid, accepted) in verdicts { + info.generation.record_acceptance(txid, accepted, seq); } } @@ -1638,32 +1641,35 @@ impl Actor { &root, &report, ); - self.mark_accepted(wallet_id, &events); + self.record_acceptance(wallet_id, &events); deliver(&self.sink, events); self.probe_next(wallet_id); } } } - /// Hand the sends these events find accepted (or mined) to the wallet, so - /// their change is offered to payments again. Off the actor: the mark - /// takes the manager lock, and a set insert needs no ordering against - /// the actor's own work. - fn mark_accepted(&self, wallet_id: WalletId, events: &[Outgoing]) { - let txids: Vec = events + /// Hand the sends these events find accepted (or mined) or dead to the + /// wallet, so the coins of an accepted one are offered to payments again + /// and those of a dead one are held again. Off the actor: the record + /// takes the manager lock. The sequence number is taken here, in the + /// actor's order, so a later verdict wins however the records land. + fn record_acceptance(&self, wallet_id: WalletId, events: &[Outgoing]) { + let verdicts: Vec<(Txid, bool)> = events .iter() .filter_map(|item| match &item.event { ResolverEvent::Verdict(txid, ProbeVerdict::Accepted | ProbeVerdict::Mined) => { - Some(*txid) + Some((*txid, true)) } + ResolverEvent::Verdict(txid, ProbeVerdict::Dead { .. }) => Some((*txid, false)), _ => None, }) .collect(); - if txids.is_empty() { + if verdicts.is_empty() { return; } + let seq = next_acceptance_seq(); let source = Arc::clone(&self.source); - tokio::spawn(async move { source.mark_accepted(wallet_id, txids).await }); + tokio::spawn(async move { source.record_acceptance(wallet_id, verdicts, seq).await }); } /// Probe the run's next due root, or end the run. @@ -2723,8 +2729,8 @@ mod tests { /// Reads wait while set, so a test can hold a pass in its read. paused: StdMutex, resume: Notify, - /// Sends marked accepted, per wallet. - accepted: StdMutex>, + /// Acceptance recorded per wallet: (wallet, txid, accepted, seq). + acceptance: StdMutex>, } impl FakeSource { @@ -2748,9 +2754,18 @@ mod tests { #[async_trait] impl WalletSource for FakeSource { - async fn mark_accepted(&self, wallet_id: WalletId, txids: Vec) { - let mut accepted = self.accepted.lock().expect("accepted"); - accepted.extend(txids.into_iter().map(|txid| (wallet_id, txid))); + async fn record_acceptance( + &self, + wallet_id: WalletId, + verdicts: Vec<(Txid, bool)>, + seq: u64, + ) { + let mut acceptance = self.acceptance.lock().expect("acceptance"); + acceptance.extend( + verdicts + .into_iter() + .map(|(txid, accepted)| (wallet_id, txid, accepted, seq)), + ); } async fn holders(&self, txid: Txid) -> Vec { @@ -2907,29 +2922,51 @@ mod tests { assert!(rig.sent().is_empty()); } - /// A send a probe finds accepted is marked so on the wallet, so its change - /// may fund the next payment; an unresolved one is not. + /// A probe's verdicts reach the wallet: accepted releases a send's coins, + /// dead holds them again; an unresolved verdict records nothing. #[tokio::test] - async fn should_mark_a_send_found_accepted_and_only_that_one() { + async fn should_record_accepted_and_dead_verdicts_and_not_unresolved_ones() { let probe = Arc::new(ScriptedProbe::new(&[ (txid(1), ProbeVerdict::Accepted), (txid(2), unresolved()), + (txid(3), dead()), ])); let mut rig = enabled(probe).await; rig.views( wallet(), - vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + vec![ + send(1, &[outpoint(90, 0)]), + send(2, &[outpoint(91, 0)]), + send(3, &[outpoint(92, 0)]), + ], ); - rig.send(Command::Uncertain(txid(1))).await; - rig.send(Command::Uncertain(txid(2))).await; - // The mark runs off the actor. - for _ in 0..10 { + for n in 1..=3 { + rig.send(Command::Uncertain(txid(n))).await; + } + // The record runs off the actor. + for _ in 0..20 { tokio::task::yield_now().await; } - assert_eq!( - *rig.source.accepted.lock().expect("accepted"), - vec![(wallet(), txid(1))] + let recorded: Vec<(WalletId, Txid, bool)> = rig + .source + .acceptance + .lock() + .expect("acceptance") + .iter() + .map(|(wallet_id, txid, accepted, _)| (*wallet_id, *txid, *accepted)) + .collect(); + assert!( + recorded.contains(&(wallet(), txid(1), true)), + "{recorded:?}" + ); + assert!( + recorded.contains(&(wallet(), txid(3), false)), + "{recorded:?}" + ); + assert!( + !recorded.iter().any(|(_, recorded, _)| *recorded == txid(2)), + "{recorded:?}" ); } diff --git a/packages/rs-platform-wallet/src/wallet/core/generation.rs b/packages/rs-platform-wallet/src/wallet/core/generation.rs index 6c99c9b5867..31dd8610aac 100644 --- a/packages/rs-platform-wallet/src/wallet/core/generation.rs +++ b/packages/rs-platform-wallet/src/wallet/core/generation.rs @@ -5,6 +5,7 @@ use std::collections::{HashMap, HashSet}; use std::ops::Deref; +use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; use dashcore::{OutPoint, Transaction, Txid}; @@ -255,18 +256,37 @@ impl std::fmt::Debug for SettleBoundaryHook { #[derive(Debug, Default)] pub(crate) struct InBroadcastFences { fences: Mutex>, - /// Outgoing sends the network has been seen to accept before the wallet - /// saw them InstantSend-locked or mined — a peer echoed the broadcast back, - /// or the broadcast resolver's probe found them in a node's mempool or a - /// block. Their change may fund the next payment; the change of any other - /// unconfirmed send of ours may not (see - /// [`WalletGeneration::is_send_accepted`]). + /// What the network was last seen to say about our unsettled sends: a + /// peer echoed the broadcast back or a probe found it accepted or mined + /// (accepted), or a probe found it dead. Only an accepted send's outputs + /// may fund the next payment before the wallet sees it InstantSend-locked + /// or mined (see [`WalletGeneration::accepted_among`]). /// /// Shared like the fences: acceptance is a fact about the network, not - /// about one in-memory instance. Not persisted — after a restart a send - /// still unconfirmed and unlocked is held again until the lock, a block or - /// a fresh verdict arrives, which only errs on the side of not spending. - accepted_sends: Mutex>, + /// about one in-memory instance. Kept to the wallet's unsettled sends — + /// every read forgets the rest. Not persisted: after a restart an + /// unsettled send is held again until its lock, a block or a fresh + /// verdict, which only errs on the side of not spending. + acceptance: Mutex>, +} + +/// The last word on one send, and when it was observed — see +/// [`next_acceptance_seq`]. A later observation replaces an earlier one, so a +/// send found dead after it was found accepted is held again. +#[derive(Debug, Clone, Copy)] +struct Acceptance { + seq: u64, + accepted: bool, +} + +static ACCEPTANCE_SEQ: AtomicU64 = AtomicU64::new(1); + +/// A sequence number for an acceptance fact, taken where the fact is observed +/// (a broadcast returning, a probe verdict published). Facts are recorded off +/// the observing task, so they can land out of order; the number keeps the +/// later one. +pub(crate) fn next_acceptance_seq() -> u64 { + ACCEPTANCE_SEQ.fetch_add(1, Ordering::Relaxed) } impl InBroadcastFences { @@ -276,8 +296,10 @@ impl InBroadcastFences { self.fences.lock().unwrap_or_else(PoisonError::into_inner) } - fn accepted(&self) -> MutexGuard<'_, HashSet> { - self.accepted_sends.lock().unwrap_or_else(PoisonError::into_inner) + fn acceptance(&self) -> MutexGuard<'_, HashMap> { + self.acceptance + .lock() + .unwrap_or_else(PoisonError::into_inner) } } @@ -413,20 +435,33 @@ impl WalletGeneration { } } - /// Record that the network accepted the outgoing send `txid` — see - /// [`is_send_accepted`](Self::is_send_accepted). + /// Record what the network said about our send `txid` (accepted, or + /// found dead), observed at `seq` ([`next_acceptance_seq`]). An older + /// observation than the one recorded is dropped. + pub(crate) fn record_acceptance(&self, txid: Txid, accepted: bool, seq: u64) { + let mut acceptance = self.in_broadcast.acceptance(); + let newer = acceptance.get(&txid).is_none_or(|known| known.seq < seq); + if newer { + acceptance.insert(txid, Acceptance { seq, accepted }); + } + } + + /// A broadcast of `txid` returned accepted: a peer echoed it back. pub(crate) fn mark_send_accepted(&self, txid: Txid) { - self.in_broadcast.accepted().insert(txid); + self.record_acceptance(txid, true, next_acceptance_seq()); } - /// Whether the network has been seen to accept the outgoing send `txid`. - /// - /// Until it has — or the wallet sees it InstantSend-locked or mined — the - /// change of that send is not offered to a new payment: if the send never - /// reached the network, a payment built on its change is an orphan no node - /// accepts, and every later one built on that change is too. - pub(crate) fn is_send_accepted(&self, txid: &Txid) -> bool { - self.in_broadcast.accepted().contains(txid) + /// The sends among `unsettled` the network was last seen to accept. + /// Forgets every recorded send not among them — settled or gone — so the + /// record stays the size of the wallet's unsettled sends. + pub(crate) fn accepted_among(&self, unsettled: &HashSet) -> HashSet { + let mut acceptance = self.in_broadcast.acceptance(); + acceptance.retain(|txid, _| unsettled.contains(txid)); + acceptance + .iter() + .filter(|(_, known)| known.accepted) + .map(|(txid, _)| *txid) + .collect() } /// This generation's lock-free balance. diff --git a/packages/rs-platform-wallet/src/wallet/core/mod.rs b/packages/rs-platform-wallet/src/wallet/core/mod.rs index d9a61a35412..0eba2e83050 100644 --- a/packages/rs-platform-wallet/src/wallet/core/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/core/mod.rs @@ -8,6 +8,7 @@ mod sign_message; pub mod spend_observer; pub(crate) use sign_message::is_signable_funding_account; mod transaction; +mod unresolved; pub mod wallet; pub use balance::WalletBalance; @@ -17,5 +18,5 @@ pub(crate) use generation::{InBroadcastFences, InBroadcastPin}; pub use spend_observer::SpendObservationHandler; pub(crate) use transaction::resolve_source_accounts; pub use transaction::{SignedCoreTransaction, ASSET_LOCK_FUNDING_SOURCES, SEND_FUNDING_SOURCES}; -pub(crate) use transaction::add_funding_withholding_unresolved_change; +pub(crate) use unresolved::UnresolvedSends; pub use wallet::CoreWallet; diff --git a/packages/rs-platform-wallet/src/wallet/core/transaction.rs b/packages/rs-platform-wallet/src/wallet/core/transaction.rs index d1934100122..a813e05418a 100644 --- a/packages/rs-platform-wallet/src/wallet/core/transaction.rs +++ b/packages/rs-platform-wallet/src/wallet/core/transaction.rs @@ -12,7 +12,6 @@ use dashcore::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction}; use key_wallet::account::AccountType; use key_wallet::managed_account::managed_account_collection::ManagedAccountCollection; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; -use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionError; use key_wallet::wallet::managed_wallet_info::fee::{estimate_tx_size, FeeRate}; use key_wallet::wallet::managed_wallet_info::transaction_builder::{ @@ -21,9 +20,9 @@ use key_wallet::wallet::managed_wallet_info::transaction_builder::{ use key_wallet::wallet::managed_wallet_info::transaction_building::AccountTypePreference; use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use key_wallet::wallet::Wallet; -use key_wallet::Account; use key_wallet::{DerivationPath, ReservationToken, Utxo}; +use super::unresolved::UnresolvedSends; use super::{CoreWallet, WalletGeneration}; use crate::broadcaster::TransactionBroadcaster; use crate::wallet::reservations::reservation_expired; @@ -76,6 +75,40 @@ fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> Platfo PlatformWalletError::TransactionBuild(error.to_string()) } +/// A shortfall that the held coins of an unresolved send (see +/// [`UnresolvedSends`]) would have covered becomes +/// [`PlatformWalletError::CoreFundsAwaitingNetwork`], so the host can say the +/// money is waiting on the network rather than missing. A genuine shortfall — +/// held coins or not, the funds are not there — stays insufficient funds. +fn awaiting_network(error: PlatformWalletError, held: u64) -> PlatformWalletError { + let (available, required) = match &error { + PlatformWalletError::CoreInsufficientFunds { + available, + required, + .. + } + | PlatformWalletError::CorePooledInsufficientFunds { + available, + required, + .. + } => (*available, *required), + _ => return error, + }; + let covered = match (available, required) { + (Some(available), Some(required)) => available.saturating_add(held) >= required, + _ => held > 0, + }; + if held == 0 || !covered { + return error; + } + PlatformWalletError::CoreFundsAwaitingNetwork { + available, + held, + required, + outpoint: None, + } +} + /// key-wallet's `FeeRate::calculate_fee`, with the multiplication checked. /// /// Upstream computes `(sat_per_kb * size_bytes).div_ceil(1000)` unchecked, and @@ -85,57 +118,6 @@ fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> Platfo /// down instead of surfacing as an error the caller can show — and a profile /// without overflow checks is worse still, wrapping silently into a fee that /// makes the reported maximum nonsense. Both become a typed error here. -/// Whether `utxo` is the change of one of our sends that the network has not -/// been seen to accept: our own output (`is_trusted`), neither mined nor -/// InstantSend-locked, of a send not marked accepted on `generation`. -/// -/// Such a coin exists only while that send's outcome is unknown. If the send -/// never reached the network the coin is phantom, and a payment built on it is -/// an orphan no node accepts — the shape behind "Transaction status unknown" -/// followed by payments that never land. So it is not offered to a new -/// payment, and not counted as spendable, until the send is accepted. -pub(crate) fn is_unresolved_change(utxo: &Utxo, generation: &WalletGeneration) -> bool { - utxo.is_trusted - && !utxo.is_confirmed - && !utxo.is_instantlocked - && !generation.is_send_accepted(&utxo.outpoint.txid) -} - -/// [`TransactionBuilder::add_funding`] with the account's unresolved change -/// (see [`is_unresolved_change`]) withheld from selection. -/// -/// `add_funding` copies the account's UTXOs into the builder as candidates, -/// and selection drops a candidate that is locked. So the held coins are -/// locked for that one call and unlocked right after — the copies keep the -/// lock, the wallet does not. The caller holds the manager write guard across -/// it, so no other reader sees the transient lock. A coin already locked for -/// another reason is left as it is. -pub(crate) fn add_funding_withholding_unresolved_change( - builder: TransactionBuilder, - managed: &mut ManagedCoreFundsAccount, - account: &Account, - generation: &WalletGeneration, -) -> TransactionBuilder { - let held: Vec = managed - .utxos - .values() - .filter(|utxo| !utxo.is_locked && is_unresolved_change(utxo, generation)) - .map(|utxo| utxo.outpoint) - .collect(); - for outpoint in &held { - if let Some(utxo) = managed.utxos.get_mut(outpoint) { - utxo.lock(); - } - } - let builder = builder.add_funding(managed, account); - for outpoint in &held { - if let Some(utxo) = managed.utxos.get_mut(outpoint) { - utxo.unlock(); - } - } - builder -} - fn checked_fee(fee_rate: FeeRate, size_bytes: usize) -> Result { fee_rate .as_sat_per_kb() @@ -494,6 +476,7 @@ impl CoreWallet { .get_wallet_and_info(&self.wallet_id) .ok_or_else(|| PlatformWalletError::WalletNotFound("wallet not found".into()))?; let height = info.core_wallet.last_processed_height(); + let unresolved = UnresolvedSends::of(info); // Same resolver, same strictness rule, as the funding path. let resolved = resolved_funding_accounts( @@ -512,7 +495,7 @@ impl CoreWallet { total += managed .spendable_utxos(height) .iter() - .filter(|utxo| !is_unresolved_change(utxo, &info.generation)) + .filter(|utxo| !unresolved.holds(&utxo.outpoint)) .map(|utxo| utxo.value()) .sum::(); } @@ -567,6 +550,7 @@ impl CoreWallet { .get_wallet_and_info(&self.wallet_id) .ok_or_else(|| PlatformWalletError::WalletNotFound("wallet not found".into()))?; let height = info.core_wallet.last_processed_height(); + let unresolved = UnresolvedSends::of(info); let resolved = resolved_funding_accounts( &info.core_wallet.accounts, @@ -588,7 +572,7 @@ impl CoreWallet { managed .spendable_utxos(height) .iter() - .filter(|utxo| !is_unresolved_change(utxo, &info.generation)) + .filter(|utxo| !unresolved.holds(&utxo.outpoint)) .map(|utxo| utxo.value()) .filter(|value| *value > input_cost), ); @@ -698,6 +682,8 @@ impl CoreWallet { .ok_or_else(|| PlatformWalletError::WalletNotFound("wallet not found".into()))?; let height = info.core_wallet.last_processed_height(); + let unresolved = UnresolvedSends::of(info); + let mut held_value = 0u64; // Fund from every resolved account, mirroring key-wallet's own // multi-source fold (`transaction_building::fund`): dedup overlapping @@ -746,17 +732,13 @@ impl CoreWallet { paths.insert(utxo.address.clone(), path); } } + held_value += unresolved.held_value(managed); // `reservation_only` offers no candidates of its own: the - // caller seeded its inputs explicitly. + // caller seeded its inputs explicitly (checked below). builder = if reservation_only { builder.add_funding_reservation_only(managed, account) } else { - add_funding_withholding_unresolved_change( - builder, - managed, - account, - &info.generation, - ) + unresolved.add_funding(builder, managed, account) }; offered_accounts.push(at); } @@ -774,9 +756,10 @@ impl CoreWallet { } else { FundingContext::Pooled(sources) }; - let (unsigned, fee, reservation_token) = builder - .build_unsigned_reserved() - .map_err(|error| map_builder_error(error, funding_context))?; + let (unsigned, fee, reservation_token) = + builder.build_unsigned_reserved().map_err(|error| { + awaiting_network(map_builder_error(error, funding_context), held_value) + })?; // Release across every contributing account on the error paths // below: the pooled reservation lives per account under the one @@ -816,6 +799,20 @@ impl CoreWallet { return Err(PlatformWalletError::InputMidBroadcast { outpoint }); } + // Refuse a held coin (see `UnresolvedSends`) the caller seeded + // itself — the `reservation_only` path and outpoint-chosen inputs, + // which skip the candidate filter above. Selection cannot pick one + // otherwise; this is also the backstop for that. + if let Some(outpoint) = unresolved.held_input(&unsigned) { + release_all!(offered_accounts, info.core_wallet.accounts, &unsigned); + return Err(PlatformWalletError::CoreFundsAwaitingNetwork { + available: None, + held: held_value, + required: None, + outpoint: Some(outpoint), + }); + } + // Map every selected input back to the account that owns it. That // mapping — not the offered list — is what the transaction carries: // selection routinely takes nothing from most offered sources, and @@ -1065,30 +1062,36 @@ impl CoreWallet { #[cfg(test)] mod tests { + use std::collections::HashSet; use std::sync::Arc; use async_trait::async_trait; + use dashcore::hashes::Hash; use dashcore::secp256k1::{ecdsa, PublicKey}; - use dashcore::{Address as DashAddress, Network}; + use dashcore::{Address as DashAddress, BlockHash, Network, OutPoint, Transaction, Txid}; use key_wallet::account::account_type::StandardAccountType; use key_wallet::signer::{Signer, SignerMethod}; + use key_wallet::transaction_checking::{BlockInfo, TransactionContext}; use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionStrategy; use key_wallet::wallet::managed_wallet_info::transaction_builder::TransactionBuilder; use key_wallet::wallet::managed_wallet_info::transaction_building::AccountTypePreference; - use key_wallet::DerivationPath; - use tokio::sync::Barrier; + use key_wallet::{DerivationPath, Utxo}; + use key_wallet_manager::WalletManager; + use tokio::sync::{Barrier, RwLock}; use crate::broadcaster::TransactionBroadcaster; use crate::test_support::{ - funded_wallet_manager, funded_wallet_manager_dual_standard, + apply_to_wallet, funded_wallet_manager, funded_wallet_manager_dual_standard, funded_wallet_manager_with_contact, AlwaysMaybeSentBroadcaster, AlwaysOkBroadcaster, AlwaysRejectedBroadcaster, WalletSigner, }; use key_wallet::wallet::managed_wallet_info::fee::{estimate_tx_size, FeeRate}; + use crate::wallet::core::generation::next_acceptance_seq; use crate::wallet::core::transaction::{modeled_output_script, MAX_STANDARD_TX_INPUTS}; - use crate::wallet::core::CoreWallet; - use crate::PlatformWalletError; + use crate::wallet::core::{CoreWallet, WalletGeneration}; + use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; + use crate::{PlatformWalletError, SEND_FUNDING_SOURCES}; fn preference(account_type: StandardAccountType) -> AccountTypePreference { match account_type { @@ -2070,217 +2073,395 @@ mod tests { )); } - // ---- Change of a send the network has not been seen to accept ----------- - - /// Turn the BIP44 fixture UTXO into what a send whose outcome is still - /// unknown leaves behind: our own change, in the mempool, neither - /// InstantSend-locked nor mined. Returns its outpoint. - async fn make_unresolved_change( - manager: &Arc>>, - wallet_id: &crate::wallet::platform_wallet::WalletId, - ) -> dashcore::OutPoint { - let mut manager = manager.write().await; - let info = manager.get_wallet_info_mut(wallet_id).expect("wallet"); - let managed = info - .core_wallet - .accounts - .funds_account_mut(&key_wallet::account::AccountType::Standard { - index: 0, - standard_account_type: StandardAccountType::BIP44Account, - }) - .expect("bip44 account"); - let utxo = managed.utxos.values_mut().next().expect("bip44 utxo"); - utxo.is_trusted = true; - utxo.is_confirmed = false; - utxo.is_instantlocked = false; - utxo.outpoint - } + // ---- Coins of a send the network has not been seen to accept ----------- + type Manager = Arc>>; + + /// BIP44 and BIP32 each funded with one confirmed 700 000-duff coin. async fn dual_core( - broadcaster_ok: bool, + broadcaster: Arc, ) -> ( CoreWallet, - Arc>>, - crate::wallet::platform_wallet::WalletId, - Arc, + Manager, + WalletId, WalletSigner, ) { let (manager, wallet_id, generation, signer) = funded_wallet_manager_dual_standard(&[700_000], &[700_000]).await; let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); - let broadcaster: Arc = if broadcaster_ok { - Arc::new(AlwaysOkBroadcaster) - } else { - Arc::new(AlwaysMaybeSentBroadcaster) - }; let core = CoreWallet::new( sdk, Arc::clone(&manager), wallet_id, broadcaster, - Arc::clone(&generation), + generation, ); - (core, manager, wallet_id, generation, signer) + (core, manager, wallet_id, signer) } - /// The customer's shape (support ticket 32347): the change of a send whose - /// outcome is unknown must not fund the next payment — if that send never - /// reached the network, the next one would be an orphan no node accepts. - /// It is left out of the spendable figure, of "max", and of selection. - #[tokio::test] - async fn unresolved_change_is_not_offered_to_a_new_payment() { - let (core, manager, wallet_id, _generation, signer) = dual_core(true).await; - make_unresolved_change(&manager, &wallet_id).await; + /// Sign a send of `amount` to `to` and put it in the wallet as SPV would + /// on seeing it in a peer's mempool — never echoed back, so its outcome is + /// unknown: the shape support ticket 32347 left behind. + async fn unresolved_send( + core: &CoreWallet, + manager: &Manager, + wallet_id: &WalletId, + signer: &WalletSigner, + to: &DashAddress, + amount: u64, + ) -> Transaction { + let finalized = core + .finalize_transaction( + TransactionBuilder::new().add_output(to, amount), + &SEND_FUNDING_SOURCES, + 0, + signer, + ) + .await + .expect("the send builds"); + let transaction = finalized.transaction().clone(); + apply_to_wallet( + manager, + wallet_id, + &transaction, + TransactionContext::Mempool, + ) + .await; + transaction + } - let gross = core - .pooled_spendable_balance(&crate::SEND_FUNDING_SOURCES, 0) + async fn spendable(core: &CoreWallet) -> u64 { + core.pooled_spendable_balance(&SEND_FUNDING_SOURCES, 0) .await - .expect("gross balance"); - assert_eq!(gross, 700_000, "only the BIP32 coin is spendable"); + .expect("spendable balance") + } + + async fn utxo(manager: &Manager, wallet_id: &WalletId, outpoint: OutPoint) -> Utxo { + let manager = manager.read().await; + let info = manager.get_wallet_info(wallet_id).expect("wallet"); + info.core_wallet + .accounts + .all_funding_accounts() + .into_iter() + .find_map(|managed| managed.utxos.get(&outpoint).cloned()) + .expect("the wallet holds the coin") + } + + async fn record_acceptance( + manager: &Manager, + wallet_id: &WalletId, + txid: Txid, + accepted: bool, + ) { + let manager = manager.read().await; + let info = manager.get_wallet_info(wallet_id).expect("wallet"); + info.generation + .record_acceptance(txid, accepted, next_acceptance_seq()); + } + + fn change_of(transaction: &Transaction, paid: &DashAddress) -> OutPoint { + let vout = transaction + .output + .iter() + .position(|output| output.script_pubkey != paid.script_pubkey()) + .expect("a change output"); + OutPoint { + txid: transaction.txid(), + vout: vout as u32, + } + } + + /// The coins of an unresolved send are not spendable: left out of the + /// spendable figure, of "max", and of selection. A payment only they would + /// cover is refused as waiting on the network, not as a shortfall. + #[tokio::test] + async fn should_hold_the_outputs_of_an_unresolved_send() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 80); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; + + assert_eq!(spendable(&core).await, 700_000, "only the untouched coin"); let max = core - .pooled_max_sendable(&crate::SEND_FUNDING_SOURCES, 0, None) + .pooled_max_sendable(&SEND_FUNDING_SOURCES, 0, None) .await .expect("max sendable"); - assert!(max < 700_000 && max > 0, "max draws on the BIP32 coin only, got {max}"); + assert!( + max > 0 && max < 700_000, + "max draws on the untouched coin only, got {max}" + ); - // 1_000_000 needs both coins: the held one is not there to take. let refused = core - .finalize_transaction(payment_builder(80), &crate::SEND_FUNDING_SOURCES, 0, &signer) + .finalize_transaction(payment_builder(81), &SEND_FUNDING_SOURCES, 0, &signer) .await; assert!( matches!( refused, - Err(PlatformWalletError::CorePooledInsufficientFunds { + Err(PlatformWalletError::CoreFundsAwaitingNetwork { available: Some(700_000), + held, + outpoint: None, .. - }) + }) if held == change.value() ), - "a payment only the held change could fund must fail as insufficient funds, got {refused:?}" + "got {refused:?}" ); - // A payment the other coin covers builds, and does not touch the held one. let small = TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 81), 300_000); + .add_output(&DashAddress::dummy(Network::Testnet, 82), 300_000); let finalized = core - .finalize_transaction(small, &crate::SEND_FUNDING_SOURCES, 0, &signer) + .finalize_transaction(small, &SEND_FUNDING_SOURCES, 0, &signer) .await - .expect("the BIP32 coin covers it"); - assert_eq!(finalized.transaction().input.len(), 1); + .expect("the untouched coin covers it"); + assert!(finalized + .transaction() + .input + .iter() + .all(|input| input.previous_output.txid != send.txid())); core.abandon_transaction(&finalized).await; } - /// Withholding is not a lock left behind: after a build the held UTXO is - /// still unlocked in the wallet, so it reads as ordinary funds to every - /// other reader and is offered again the moment its send is accepted. + /// Held coins or not, a payment the wallet cannot cover is a shortfall. + #[tokio::test] + async fn should_report_a_genuine_shortfall_as_insufficient_funds() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 83); + unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + let too_much = TransactionBuilder::new() + .add_output(&DashAddress::dummy(Network::Testnet, 84), 5_000_000); + + let refused = core + .finalize_transaction(too_much, &SEND_FUNDING_SOURCES, 0, &signer) + .await; + assert!( + matches!( + refused, + Err(PlatformWalletError::CorePooledInsufficientFunds { .. }) + ), + "got {refused:?}" + ); + } + + /// The hold is not a lock left behind: after a build the held coin is + /// still unlocked in the wallet. #[tokio::test] - async fn withholding_leaves_no_lock_on_the_wallet() { - let (core, manager, wallet_id, _generation, signer) = dual_core(true).await; - let held = make_unresolved_change(&manager, &wallet_id).await; + async fn should_leave_no_lock_on_the_wallet() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 85); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; let small = TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 82), 300_000); + .add_output(&DashAddress::dummy(Network::Testnet, 86), 300_000); let finalized = core - .finalize_transaction(small, &crate::SEND_FUNDING_SOURCES, 0, &signer) + .finalize_transaction(small, &SEND_FUNDING_SOURCES, 0, &signer) .await - .expect("the BIP32 coin covers it"); + .expect("the untouched coin covers it"); core.abandon_transaction(&finalized).await; - let manager = manager.read().await; - let info = manager.get_wallet_info(&wallet_id).expect("wallet"); - let utxo = info - .core_wallet - .accounts - .funds_account(&key_wallet::account::AccountType::Standard { - index: 0, - standard_account_type: StandardAccountType::BIP44Account, - }) - .and_then(|managed| managed.utxos.get(&held)) - .expect("held utxo still recorded"); - assert!(!utxo.is_locked, "the hold must not persist as a UTXO lock"); + let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; + assert!(!change.is_locked); } - /// Accepted by the network — the resolver's verdict or a peer echo marks - /// it — the change funds payments again. + /// Accepted by the network, the send's coins are offered again. #[tokio::test] - async fn unresolved_change_is_offered_once_its_send_is_accepted() { - let (core, manager, wallet_id, generation, signer) = dual_core(true).await; - let held = make_unresolved_change(&manager, &wallet_id).await; - generation.mark_send_accepted(held.txid); + async fn should_offer_the_outputs_once_the_send_is_accepted() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 87); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; - let gross = core - .pooled_spendable_balance(&crate::SEND_FUNDING_SOURCES, 0) - .await - .expect("gross balance"); - assert_eq!(gross, 1_400_000); - let finalized = core - .finalize_transaction(payment_builder(83), &crate::SEND_FUNDING_SOURCES, 0, &signer) - .await - .expect("both coins fund it once the send is accepted"); - assert_eq!(finalized.transaction().input.len(), 2); - core.abandon_transaction(&finalized).await; + record_acceptance(&manager, &wallet_id, send.txid(), true).await; + + assert_eq!(spendable(&core).await, 700_000 + change.value()); } - /// An InstantSend lock (or a block) is acceptance the wallet sees for - /// itself, with no mark needed. + /// Found dead after it was found accepted — evicted, or beaten by a + /// conflicting spend — the send's coins are held again. An observation + /// older than the last one recorded changes nothing. #[tokio::test] - async fn unresolved_change_is_offered_once_instant_locked() { - let (core, manager, wallet_id, _generation, _signer) = dual_core(true).await; - let held = make_unresolved_change(&manager, &wallet_id).await; + async fn should_hold_again_when_an_accepted_send_is_found_dead() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 88); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + let early = next_acceptance_seq(); + + record_acceptance(&manager, &wallet_id, send.txid(), true).await; + record_acceptance(&manager, &wallet_id, send.txid(), false).await; + assert_eq!(spendable(&core).await, 700_000); + + // A late-landing record of an earlier "accepted" loses to "dead". { - let mut manager = manager.write().await; - let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet"); - let managed = info - .core_wallet - .accounts - .funds_account_mut(&key_wallet::account::AccountType::Standard { - index: 0, - standard_account_type: StandardAccountType::BIP44Account, - }) - .expect("bip44 account"); - managed.utxos.get_mut(&held).expect("utxo").is_instantlocked = true; + let manager = manager.read().await; + let info = manager.get_wallet_info(&wallet_id).expect("wallet"); + info.generation.record_acceptance(send.txid(), true, early); } - let gross = core - .pooled_spendable_balance(&crate::SEND_FUNDING_SOURCES, 0) - .await - .expect("gross balance"); - assert_eq!(gross, 1_400_000); + assert_eq!(spendable(&core).await, 700_000); } - /// A send built on held change cannot happen through selection, but one - /// recorded before this rule (or restored from storage) is itself an - /// unresolved send: accepting its parent does not release the child's - /// own change. + /// Settled — here in a block — the send's coins are ordinary funds. #[tokio::test] - async fn the_change_of_a_child_stays_held_when_only_the_parent_is_accepted() { - let (core, manager, wallet_id, generation, _signer) = dual_core(true).await; - let child = make_unresolved_change(&manager, &wallet_id).await; - generation.mark_send_accepted(::from_byte_array([7; 32])); - let gross = core - .pooled_spendable_balance(&crate::SEND_FUNDING_SOURCES, 0) - .await - .expect("gross balance"); - assert_eq!(gross, 700_000, "the child {} is still unresolved", child.txid); + async fn should_offer_the_outputs_once_the_send_is_mined() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 89); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; + + apply_to_wallet( + &manager, + &wallet_id, + &send, + TransactionContext::InBlock(BlockInfo::new(2, BlockHash::all_zeros(), 1_700_000_100)), + ) + .await; + + assert_eq!(spendable(&core).await, 700_000 + change.value()); + } + + /// A transfer to our own receive address is a send like any other: its + /// receiving side is as phantom as its change if it never left. + #[tokio::test] + async fn should_hold_the_receiving_side_of_an_internal_transfer() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let own_receive = utxo_addresses(&manager, &wallet_id).await; + let send = + unresolved_send(&core, &manager, &wallet_id, &signer, &own_receive, 100_000).await; + + let received = OutPoint { + txid: send.txid(), + vout: send + .output + .iter() + .position(|output| output.script_pubkey == own_receive.script_pubkey()) + .expect("the transfer output") as u32, + }; + assert_eq!(utxo(&manager, &wallet_id, received).await.value(), 100_000); + assert_eq!( + spendable(&core).await, + 700_000, + "neither side of the transfer" + ); + } + + /// A send built on an accepted send is held again, with all its coins, + /// once the one under it is found dead: a node that never saw the parent + /// refuses the child. + #[tokio::test] + async fn should_hold_a_send_built_on_one_found_dead() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 90); + let parent = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + record_acceptance(&manager, &wallet_id, parent.txid(), true).await; + // More than the untouched coin: it must spend the parent's change too. + let child = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 1_000_000).await; + assert!(child + .input + .iter() + .any(|input| input.previous_output.txid == parent.txid())); + record_acceptance(&manager, &wallet_id, child.txid(), true).await; + assert!( + spendable(&core).await > 0, + "the child's change, both accepted" + ); + + record_acceptance(&manager, &wallet_id, parent.txid(), false).await; + + assert_eq!( + spendable(&core).await, + 0, + "the child's change goes with its parent" + ); + } + + /// A held coin the caller chose itself as an input — outpoint-seeded or + /// `reservation_only` builds skip selection — is refused, naming it. + #[tokio::test] + async fn should_refuse_a_held_coin_chosen_as_an_input() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 91); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + let held = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; + + let refused = core + .finalize_transaction_with_options( + TransactionBuilder::new() + .add_output(&DashAddress::dummy(Network::Testnet, 92), 100_000) + .add_inputs([held.clone()]), + &[AccountTypePreference::BIP44], + 0, + &signer, + true, + ) + .await; + assert!( + matches!( + refused, + Err(PlatformWalletError::CoreFundsAwaitingNetwork { + outpoint: Some(outpoint), + .. + }) if outpoint == held.outpoint + ), + "got {refused:?}" + ); + } + + /// Acceptance is not persisted. After a restart — a fresh generation over + /// the same records — an unsettled send is held again until the network + /// is seen to accept it anew. + #[tokio::test] + async fn should_hold_after_a_restart_until_accepted_again() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 93); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + record_acceptance(&manager, &wallet_id, send.txid(), true).await; + assert!(spendable(&core).await > 700_000); + + { + let mut manager = manager.write().await; + let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet"); + info.generation = Arc::new(WalletGeneration::new()); + } + + assert_eq!(spendable(&core).await, 700_000); } - /// A send the network echoed back is accepted on the spot: its change can - /// fund the next payment before the InstantSend lock arrives. An ambiguous - /// outcome marks nothing. + /// A send the network echoed back is accepted on the spot; an ambiguous + /// outcome records nothing. #[tokio::test] - async fn an_echoed_send_is_marked_accepted_and_an_ambiguous_one_is_not() { - for (ok, expected) in [(true, true), (false, false)] { - let (core, _manager, _wallet_id, generation, signer) = dual_core(ok).await; + async fn should_mark_an_echoed_send_accepted_and_not_an_ambiguous_one() { + let broadcasters: [(Arc, bool); 2] = [ + (Arc::new(AlwaysOkBroadcaster), true), + (Arc::new(AlwaysMaybeSentBroadcaster), false), + ]; + for (broadcaster, expected) in broadcasters { + let (core, manager, wallet_id, signer) = dual_core(broadcaster).await; let small = TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 84), 300_000); + .add_output(&DashAddress::dummy(Network::Testnet, 94), 300_000); let finalized = core - .finalize_transaction(small, &crate::SEND_FUNDING_SOURCES, 0, &signer) + .finalize_transaction(small, &SEND_FUNDING_SOURCES, 0, &signer) .await .expect("finalize"); let txid = finalized.transaction().txid(); let _ = core.broadcast_finalized_transaction(&finalized).await; - assert_eq!( - generation.is_send_accepted(&txid), - expected, - "broadcaster ok={ok}: accepted mark" - ); + + let manager = manager.read().await; + let info = manager.get_wallet_info(&wallet_id).expect("wallet"); + let accepted = info.generation.accepted_among(&HashSet::from([txid])); + assert_eq!(accepted.contains(&txid), expected, "accepted={expected}"); } } + + /// The address of the wallet's BIP32 coin: one of our own receive + /// addresses. + async fn utxo_addresses(manager: &Manager, wallet_id: &WalletId) -> DashAddress { + let manager = manager.read().await; + let info = manager.get_wallet_info(wallet_id).expect("wallet"); + info.core_wallet + .first_bip32_managed_account() + .expect("bip32 account") + .utxos + .values() + .next() + .expect("bip32 coin") + .address + .clone() + } } diff --git a/packages/rs-platform-wallet/src/wallet/core/unresolved.rs b/packages/rs-platform-wallet/src/wallet/core/unresolved.rs new file mode 100644 index 00000000000..80ff32af536 --- /dev/null +++ b/packages/rs-platform-wallet/src/wallet/core/unresolved.rs @@ -0,0 +1,164 @@ +//! The coins of our own sends the network has not been seen to accept. +//! +//! A send whose broadcast outcome is unknown leaves its outputs in the wallet +//! as ordinary mempool funds — its change, or the receiving side of a transfer +//! between our own accounts. If that send never reached the network those +//! coins are phantom, and every payment built on them is an orphan no node +//! accepts: the shape behind "Transaction status unknown" followed by payments +//! that never land. So they are neither offered to a new payment nor counted +//! as spendable until the send is known to be accepted. + +use std::collections::HashSet; + +use dashcore::{OutPoint, Transaction, Txid}; +use key_wallet::managed_account::managed_account_collection::ManagedAccountCollection; +use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; +use key_wallet::wallet::managed_wallet_info::transaction_builder::TransactionBuilder; +use key_wallet::Account; + +use super::broadcast_resolver::collect_views; +use crate::wallet::platform_wallet::PlatformWalletInfo; + +/// The wallet's unsettled transactions whose outputs are held: every own send +/// — one spending the wallet's coins — that is neither InstantSend-locked nor +/// mined and that the network was not last seen to accept, and every unsettled +/// transaction spending an output of one of those, transitively. +/// +/// Derived from the wallet's own records on every read, so it holds the same +/// after a restart. Only the acceptance facts (a peer echoing a broadcast, a +/// probe verdict) live in memory: until one arrives again, an unsettled send +/// stays held, which only errs on the side of not spending. A send found dead +/// after it was found accepted is held again. +pub(crate) struct UnresolvedSends { + txids: HashSet, +} + +impl UnresolvedSends { + pub(crate) fn of(info: &PlatformWalletInfo) -> Self { + let views = collect_views(info); + let unsettled: HashSet = views.iter().map(|view| view.txid).collect(); + let accepted = info.generation.accepted_among(&unsettled); + let mut held: HashSet = views + .iter() + .filter(|view| view.spends_own_coins && !accepted.contains(&view.txid)) + .map(|view| view.txid) + .collect(); + // A transaction built on a held output is no better than its parent: + // a node that has not seen the parent refuses it. + loop { + let before = held.len(); + for view in &views { + if !held.contains(&view.txid) + && view + .transaction + .input + .iter() + .any(|input| held.contains(&input.previous_output.txid)) + { + held.insert(view.txid); + } + } + if held.len() == before { + break; + } + } + Self { txids: held } + } + + /// Whether the coin at `outpoint` is an output of a held transaction. + pub(crate) fn holds(&self, outpoint: &OutPoint) -> bool { + self.txids.contains(&outpoint.txid) + } + + /// The value of `managed`'s held coins. + pub(crate) fn held_value(&self, managed: &ManagedCoreFundsAccount) -> u64 { + managed + .utxos + .values() + .filter(|utxo| self.holds(&utxo.outpoint)) + .map(|utxo| utxo.value()) + .sum() + } + + /// The first input of `transaction` that spends a held coin. + pub(crate) fn held_input(&self, transaction: &Transaction) -> Option { + transaction + .input + .iter() + .map(|input| input.previous_output) + .find(|outpoint| self.holds(outpoint)) + } + + /// [`TransactionBuilder::add_funding`] with `managed`'s held coins left + /// out of selection. + /// + /// `add_funding` copies the account's UTXOs into the builder as candidates, + /// and selection drops a candidate that is locked. So the held coins are + /// locked for that one call and unlocked right after — the copies keep the + /// lock, the wallet does not. The caller holds the manager write guard + /// across it, so no other reader sees the transient lock. A coin already + /// locked for another reason is left as it is. + pub(crate) fn add_funding( + &self, + builder: TransactionBuilder, + managed: &mut ManagedCoreFundsAccount, + account: &Account, + ) -> TransactionBuilder { + let locked = self.lock_in_account(managed); + let builder = builder.add_funding(managed, account); + unlock_in_account(managed, &locked); + builder + } + + /// Lock every held coin of every funds account in `accounts` that is not + /// locked already, for a builder that selects across accounts on its own + /// (the asset-lock builder). Returns what was locked, for + /// [`unlock_in`](Self::unlock_in). The caller holds the manager write guard + /// until the unlock. + pub(crate) fn lock_in(&self, accounts: &mut ManagedAccountCollection) -> Vec { + accounts + .all_funding_accounts_mut() + .into_iter() + .flat_map(|managed| self.lock_in_account(managed)) + .collect() + } + + /// Undo [`lock_in`](Self::lock_in). + pub(crate) fn unlock_in(accounts: &mut ManagedAccountCollection, locked: &[OutPoint]) { + for managed in accounts.all_funding_accounts_mut() { + unlock_in_account(managed, locked); + } + } + + /// The held value across every funds account in `accounts`. + pub(crate) fn held_value_in(&self, accounts: &ManagedAccountCollection) -> u64 { + accounts + .all_funding_accounts() + .into_iter() + .map(|managed| self.held_value(managed)) + .sum() + } + + fn lock_in_account(&self, managed: &mut ManagedCoreFundsAccount) -> Vec { + let locked: Vec = managed + .utxos + .values() + .filter(|utxo| !utxo.is_locked && self.holds(&utxo.outpoint)) + .map(|utxo| utxo.outpoint) + .collect(); + for outpoint in &locked { + if let Some(utxo) = managed.utxos.get_mut(outpoint) { + utxo.lock(); + } + } + locked + } +} + +fn unlock_in_account(managed: &mut ManagedCoreFundsAccount, locked: &[OutPoint]) { + for outpoint in locked { + if let Some(utxo) = managed.utxos.get_mut(outpoint) { + utxo.unlock(); + } + } +} diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 7cb4675dd1f..1b9e0941ae9 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -12,6 +12,7 @@ use key_wallet_manager::WalletManager; use super::*; use crate::broadcaster::TransactionBroadcaster; use crate::error::PlatformWalletError; +use crate::wallet::core::UnresolvedSends; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; // --------------------------------------------------------------------------- @@ -1127,7 +1128,15 @@ impl DashPayView<'_, B> { self.drain_pending_contact_crypto_verified(provider, None) .await?; - let (payment_address, used_flip_changeset, tx, fee, funding_accounts, in_broadcast_pin) = { + let ( + payment_address, + used_flip_changeset, + tx, + fee, + funding_accounts, + in_broadcast_pin, + generation, + ) = { let mut wm = self.wallet_manager.write().await; // Resolve the external account's xpub so we can derive addresses. @@ -1245,6 +1254,9 @@ impl DashPayView<'_, B> { .set_current_height(current_height) .set_selection_strategy(SelectionStrategy::LargestFirst) .add_output(&payment_address, amount_duffs); + // Coins of our own sends the network has not been seen to accept + // stay out of selection (see `UnresolvedSends`). + let unresolved = UnresolvedSends::of(info); // Derivation paths for every offered UTXO, since the signer closure // below cannot resolve them from one account. @@ -1281,12 +1293,7 @@ impl DashPayView<'_, B> { funding_paths.insert(utxo.address.clone(), path); } } - builder = crate::wallet::core::add_funding_withholding_unresolved_change( - builder, - managed, - account, - &info.generation, - ); + builder = unresolved.add_funding(builder, managed, account); offered_accounts.push(at); } } @@ -1406,6 +1413,7 @@ impl DashPayView<'_, B> { fee, offered_accounts, in_broadcast_pin, + Arc::clone(&info.generation), ) }; @@ -1509,6 +1517,11 @@ impl DashPayView<'_, B> { // still selectable here and must stay fenced until an observed // spend says otherwise. in_broadcast_pin.settle_pending_spend(); + // Accepted: its change may fund the next payment before the + // InstantSend lock arrives (see `UnresolvedSends`). + if other.is_ok() { + generation.mark_send_accepted(tx.txid()); + } other } }; diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift index 7fcfa941f44..aa69117fdb3 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift @@ -258,6 +258,11 @@ public enum PlatformWalletResultCode: Int32, Sendable { case errorShieldedRecoveryKeysRequired = 57 /// Platform returned no balance. Retrying the read is safe; ownership is unchanged. case errorIdentityBalanceUnavailable = 58 + /// The payment needs coins held back because they came from one of the + /// wallet's own sends whose broadcast outcome is still unknown. Not a + /// shortfall and nothing was sent: they become spendable once that send is + /// InstantSend-locked, mined, or found accepted. + case errorCoreFundsAwaitingNetwork = 59 /// The named thing does not exist. Besides the handle/lookup failures this /// has always covered, BOTH deferred-send paths report the /// wallet-was-REMOVED case here. @@ -385,6 +390,8 @@ public enum PlatformWalletResultCode: Int32, Sendable { self = .errorShieldedRecoveryKeysRequired case PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_IDENTITY_BALANCE_UNAVAILABLE: self = .errorIdentityBalanceUnavailable + case PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_CORE_FUNDS_AWAITING_NETWORK: + self = .errorCoreFundsAwaitingNetwork case PLATFORM_WALLET_FFI_RESULT_CODE_NOT_FOUND: self = .notFound case PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_UNKNOWN: @@ -554,6 +561,8 @@ public enum PlatformWalletError: LocalizedError { case arithmeticOverflow(String) case noSelectableInputs(String) case coreInsufficientFunds(String) + /// See `PlatformWalletResultCode.errorCoreFundsAwaitingNetwork`. + case coreFundsAwaitingNetwork(String) case assetLockNotTracked(String) /// The one-shot output cannot be reused. This may come from a retained /// local tombstone or an unauthenticated Platform report, so it does not @@ -786,6 +795,7 @@ public enum PlatformWalletError: LocalizedError { .serialization(let m), .deserialization(let m), .memoryAllocation(let m), .arithmeticOverflow(let m), .noSelectableInputs(let m), .coreInsufficientFunds(let m), + .coreFundsAwaitingNetwork(let m), .assetLockNotTracked(let m), .assetLockAlreadyConsumed(let m), .assetLockFundingMismatch(let m), .assetLockInsufficientFunds(let m), .walletAlreadyExists(let m), .shieldedBroadcastFailed(let m), @@ -898,6 +908,7 @@ public enum PlatformWalletError: LocalizedError { case .errorArithmeticOverflow: self = .arithmeticOverflow(detail) case .errorNoSelectableInputs: self = .noSelectableInputs(detail) case .errorCoreInsufficientFunds: self = .coreInsufficientFunds(detail) + case .errorCoreFundsAwaitingNetwork: self = .coreFundsAwaitingNetwork(detail) case .errorAssetLockNotTracked: self = .assetLockNotTracked(detail) case .errorAssetLockAlreadyConsumed: self = .assetLockAlreadyConsumed(detail) case .errorAssetLockFundingMismatch: self = .assetLockFundingMismatch(detail) From 5fcf610b3b5d3d5f3a69fc84febc709c24423ee8 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 14:05:47 +0300 Subject: [PATCH 050/169] fix(platform-wallet): exclude unresolved-send coins through key-wallet, only while probing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces the lock/unlock wrapper around coin selection with key-wallet's TransactionBuilder::exclude_outpoints (rust-dashcore feat/funding-exclude- outpoints): the held coins are left out of selection and a seeded one is refused with BuilderError::ExcludedInput, mapped to CoreFundsAwaitingNetwork. Nothing is written to the wallet, so no path can strand a lock. - The hold follows the broadcast-probe switch: the resolver's enabled flag is shared into every wallet's fences. With probing off nothing is held and building is exactly as before — a record stuck in mempool context can no longer freeze its coins. - The held set is one BFS over a children map of the unsettled records, borrowed, with no transaction clones. - An acceptance fact is kept for a grace period after it is observed, so a broadcast returning before its record lands still releases the send. - CoreFundsAwaitingNetwork only when the held coins, net of their input fee, cover a shortfall of known size; otherwise insufficient funds. The DashPay contact payment maps the same way. - Asset-lock builds keep key-wallet's final-inputs rule, which already leaves every unresolved output out; the asset-lock hold, its typed error and test are removed. Asset-lock broadcasts, their resume re-broadcasts and the release-on-rejection broadcast record an echoed send as accepted. - Code 59 recorded in ERROR_CODE_REGISTRY.md and mirrored in Kotlin. Does not build until the rust-dashcore pin moves to a rev with TransactionBuilder::exclude_outpoints (local commit f096c013 on feat/funding-exclude-outpoints, based on the current pin e4208c90). Co-Authored-By: Claude Opus 5.5 --- .../dashsdk/errors/DashSdkError.kt | 11 + .../dashsdk/errors/DashSdkErrorTest.kt | 4 + .../ERROR_CODE_REGISTRY.md | 1 + .../rs-platform-wallet/src/manager/mod.rs | 6 +- .../src/wallet/asset_lock/build.rs | 151 +++-------- .../src/wallet/asset_lock/sync/recovery.rs | 11 +- .../src/wallet/core/broadcast_resolver.rs | 38 ++- .../src/wallet/core/generation.rs | 68 ++++- .../rs-platform-wallet/src/wallet/core/mod.rs | 4 +- .../src/wallet/core/transaction.rs | 247 ++++++++++++++++-- .../src/wallet/core/unresolved.rs | 188 ++++++------- .../src/wallet/identity/network/payments.rs | 16 +- .../src/wallet/reservations.rs | 23 +- 13 files changed, 497 insertions(+), 271 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt index 420ad1a6bbb..e645ade57e8 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt @@ -117,6 +117,16 @@ sealed class DashSdkError( class CoreInsufficientFunds(message: String, cause: Throwable? = null) : PlatformWallet(message, cause) + /** + * `ErrorCoreFundsAwaitingNetwork` (native code 59). The payment is + * covered only by coins of an earlier send of this wallet whose + * broadcast outcome is not known yet; they become spendable once the + * network is seen to accept that send. Not a shortfall — retry after + * the earlier payment settles. + */ + class CoreFundsAwaitingNetwork(message: String, cause: Throwable? = null) : + PlatformWallet(message, cause) + /** * `ErrorShieldedInsufficientBalance` (native code 41; historical FFI * spelling). A Platform Payment account's deterministic shield input @@ -733,6 +743,7 @@ sealed class DashSdkError( 20 -> PlatformWallet.TransactionBroadcastUnconfirmed(message, cause) // ErrorTransactionBroadcastUnconfirmed 42 -> PlatformWallet.MasternodeWithdrawalUnconfirmed(message, cause) // ErrorMasternodeWithdrawalUnconfirmed 22 -> PlatformWallet.CoreInsufficientFunds(message, cause) // ErrorCoreInsufficientFunds + 59 -> PlatformWallet.CoreFundsAwaitingNetwork(message, cause) // ErrorCoreFundsAwaitingNetwork 23 -> PlatformWallet.AssetLockNotTracked(message, cause) // ErrorAssetLockNotTracked 24 -> PlatformWallet.AssetLockAlreadyConsumed(message, cause) // ErrorAssetLockAlreadyConsumed 25 -> PlatformWallet.AssetLockFundingMismatch(message, cause) // ErrorAssetLockFundingMismatch diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt index 7129c55f633..1e5c1700a0a 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt @@ -129,6 +129,10 @@ class DashSdkErrorTest { DashSdkError.fromNative(DashSDKException(offset + 22, "inputs reserved")) assertTrue(coreInsufficientFunds is DashSdkError.PlatformWallet.CoreInsufficientFunds) + val awaitingNetwork = + DashSdkError.fromNative(DashSDKException(offset + 59, "funds awaiting network")) + assertTrue(awaitingNetwork is DashSdkError.PlatformWallet.CoreFundsAwaitingNetwork) + // The asset-lock coin-selection shortfall (29) must reach callers as its // own type rather than Generic, and must stay DISTINCT from the atomic // Core-send shortfall (22) — the two selectors report over different diff --git a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md index 3399060f53b..330ee83d699 100644 --- a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md +++ b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md @@ -172,6 +172,7 @@ Fork-era numbers remain in the collision history, which is immutable record. | 56 | `ErrorShieldedRecoveryCorrupted` | #4715 | Proposed — durable shielded recovery data is malformed or invalid; preserved for diagnosis. Rust, Swift and Kotlin preserve this typed error | | 57 | `ErrorShieldedRecoveryKeysRequired` | #4715 | Proposed — recovery needs the account and compatible keys; ciphertext damage can produce the same symptom. Rust, Swift and Kotlin preserve this typed error | | 58 | `ErrorIdentityBalanceUnavailable` | #4799 | Proposed — Platform returned no balance for a managed identity. Retrying the read is safe; this is distinct from missing wallet ownership and must not trigger registration or funding | +| 59 | `ErrorCoreFundsAwaitingNetwork` | `fix/resolve-unknown-broadcast` (PR not opened yet) | Proposed — claimed from the frontier (58). A Core payment that only the coins of an earlier send of this wallet would cover, while that send's broadcast outcome is unresolved; those coins become spendable once the network is seen to accept it. Emitted only while the manager probes unresolved broadcasts. Not a shortfall: retry after the earlier payment settles. Rust value + pin test, Swift raw case + typed `coreFundsAwaitingNetwork`, Kotlin typed `PlatformWallet.CoreFundsAwaitingNetwork` + `fromPlatformWalletNative` arm + `DashSdkErrorTest` pin | **Code 31 left this table on 2026-08-04.** `ErrorSigningKeyUnavailable` sat here as #4183's proposal until #4183 merged (`189a3abb1c`); it is now in the merged diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index f3b14884fd9..d8cbd607271 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -649,7 +649,11 @@ impl PlatformWalletManager

{ .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .entry(*wallet_id) - .or_default(), + .or_insert_with(|| { + Arc::new(crate::wallet::core::InBroadcastFences::with_hold_flag( + self.broadcast_resolver.enabled_flag(), + )) + }), ) } diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs index fed57872f6a..6f15b2a2625 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs @@ -24,8 +24,8 @@ use key_wallet::wallet::Wallet; use crate::changeset::{AccountRegistrationEntry, PlatformWalletChangeSet}; use crate::error::PlatformWalletError; -use crate::wallet::core::UnresolvedSends; use crate::wallet::platform_wallet::PlatformWalletInfo; +use crate::wallet::reservations::record_send_accepted; use crate::ASSET_LOCK_FUNDING_SOURCES; use super::manager::{AssetLockManager, DEFAULT_FEE_PER_KB}; @@ -274,14 +274,7 @@ impl AssetLockManager { // pools the sources, enforces that CoinJoin funding is drain-only and // unpooled, and reserves the selected inputs in each contributing // account's own set under one owner token). - // Coins of our own sends the network has not been seen to accept stay - // out of the builder's own selection (see `UnresolvedSends`): locked - // across the call, under the write guard held since `info` was taken, - // and unlocked before anything else reads the wallet. - let unresolved = UnresolvedSends::of(info); - let held_value = unresolved.held_value_in(&info.core_wallet.accounts); - let held_locks = unresolved.lock_in(&mut info.core_wallet.accounts); - let built = info + let result = info .core_wallet .build_asset_lock_with_signer( wallet, @@ -292,9 +285,7 @@ impl AssetLockManager { drain, signer, ) - .await; - UnresolvedSends::unlock_in(&mut info.core_wallet.accounts, &held_locks); - let result = built + .await .map_err(|e| { // A drain's credit-output value is a zero placeholder, so it // must not be advertised as the `required` amount of a typed @@ -309,20 +300,6 @@ impl AssetLockManager { } }; map_builder_error(e, required) - }) - .map_err(|error| match error { - PlatformWalletError::AssetLockInsufficientFunds { - available, - required, - } if held_value > 0 && available.saturating_add(held_value) >= required => { - PlatformWalletError::CoreFundsAwaitingNetwork { - available: Some(available), - held: held_value, - required: Some(required), - outpoint: None, - } - } - other => other, })?; // Refuse a selection that picked an input pinned by an IN-FLIGHT @@ -360,27 +337,6 @@ impl AssetLockManager { return Err(PlatformWalletError::InputMidBroadcast { outpoint }); } - // Backstop for the hold above: the builder's selection cannot take a - // held coin while it is locked. - if let Some(outpoint) = unresolved.held_input(&result.transaction) { - for funding_account in &result.funding_accounts { - if let Some(account) = info.core_wallet.accounts.funds_account(funding_account) { - match result.reservation_token { - Some(token) => { - account.release_reservation_if_owner(&result.transaction, token) - } - None => account.release_reservation(&result.transaction), - } - } - } - return Err(PlatformWalletError::CoreFundsAwaitingNetwork { - available: None, - held: held_value, - required: None, - outpoint: Some(outpoint), - }); - } - // 4. Pull the (pubkey, path) for our single credit output. // // `build_asset_lock_with_signer` always returns the `Public` @@ -1185,6 +1141,9 @@ impl AssetLockManager { // spend. A cancellation or unwind inside `broadcast` reaches no arm // at all and settles as pending through `InBroadcastPin::drop`. let broadcast_outcome = self.broadcaster.broadcast(&tx).await; + if let Ok(txid) = &broadcast_outcome { + record_send_accepted(&self.wallet_manager, &self.wallet_id, *txid).await; + } if let Err(e) = broadcast_outcome { if matches!(e, crate::broadcaster::BroadcastError::Rejected { .. }) { // The rejection alone does NOT establish the released verdict @@ -1424,20 +1383,16 @@ mod tests { ClientStartState, PersistenceError, PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::test_support::{ - apply_to_wallet, funded_wallet_manager, funded_wallet_manager_dual_standard, + funded_wallet_manager, funded_wallet_manager_dual_standard, funded_wallet_manager_with_contact, AlwaysMaybeSentBroadcaster, AlwaysOkBroadcaster, AlwaysRejectedBroadcaster, WalletSigner, }; use crate::wallet::asset_lock::manager::AssetLockManager; use crate::wallet::asset_lock::tracked::AssetLockStatus; - use crate::wallet::core::CoreWallet; use crate::wallet::persister::WalletPersister; use crate::wallet::platform_wallet::PlatformWalletInfo; use crate::wallet::platform_wallet::WalletId; - use crate::SEND_FUNDING_SOURCES; use crate::{AssetLockFundingType, PlatformWalletError}; - use dashcore::{Address as DashAddress, Network}; - use key_wallet::wallet::managed_wallet_info::transaction_builder::TransactionBuilder; /// The zero-spendable-candidate selection error must surface the SAME /// typed shortfall as a partial shortfall (not the generic string form), @@ -1950,71 +1905,6 @@ mod tests { (manager, signer) } - /// An asset lock that only the coins of an unresolved send of ours would - /// fund is refused as waiting on the network, not as a shortfall: those - /// coins become usable once that send settles. - #[tokio::test] - async fn should_report_held_coins_as_awaiting_network_in_an_asset_lock() { - let (wallet_manager, wallet_id, generation, signer) = - funded_wallet_manager(StandardAccountType::BIP44Account).await; - let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); - let core = CoreWallet::new( - Arc::clone(&sdk), - Arc::clone(&wallet_manager), - wallet_id, - Arc::new(AlwaysOkBroadcaster), - generation, - ); - // Spend the one confirmed coin; its change is all the wallet has left. - let finalized = core - .finalize_transaction( - TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 95), 1_000_000), - &SEND_FUNDING_SOURCES, - 0, - &signer, - ) - .await - .expect("the send builds"); - apply_to_wallet( - &wallet_manager, - &wallet_id, - finalized.transaction(), - TransactionContext::Mempool, - ) - .await; - let manager = AssetLockManager::new( - sdk, - wallet_manager, - wallet_id, - Arc::new(Notify::new()), - Arc::new(AlwaysOkBroadcaster), - WalletPersister::new( - wallet_id, - Arc::new(CapturingPersistence::default()) as Arc, - ), - ); - - let refused = manager - .create_funded_asset_lock_proof( - 1_000_000, - 0, - AssetLockFundingType::IdentityRegistration, - 0, - &signer, - ) - .await; - - assert!( - matches!( - refused, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { held, .. }) if held > 1_000_000 - ), - "got {:?}", - refused.map(|_| ()) - ); - } - /// Regression: a build must persist the funding account's address-pool /// snapshot with the newly-used index. The asset-lock funding accounts fund /// OP_RETURN-payload credit outputs that never appear as on-chain UTXOs, so @@ -2128,6 +2018,33 @@ mod tests { /// reservation and the resumable `Built` row: the transaction may already /// be propagating, so a retry must not double-spend and a resume must /// stay possible. + /// An echoed asset-lock broadcast is recorded as accepted, so its change + /// is not held as an unresolved send's while its InstantSend lock is due. + #[tokio::test] + async fn should_mark_an_echoed_asset_lock_accepted() { + let (manager, signer, _persistence) = + funded_asset_lock_manager(Arc::new(AlwaysOkBroadcaster)).await; + + let (_, out_point) = manager + .broadcast_funded_asset_lock( + 1_000_000, + 0, + AssetLockFundingType::IdentityRegistration, + 0, + &signer, + ) + .await + .expect("broadcast"); + + let wm = manager.wallet_manager.read().await; + let (_, info) = wm.get_wallet_and_info(&manager.wallet_id).expect("wallet"); + let txid = out_point.txid; + assert!(info + .generation + .accepted_among(&std::collections::HashSet::from([txid])) + .contains(&txid)); + } + #[tokio::test] async fn ambiguous_asset_lock_broadcast_keeps_reservation_and_built_row() { let (manager, signer, persistence) = diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs index 7e886ea7da6..b880706b839 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs @@ -21,6 +21,7 @@ use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoIn use crate::changeset::changeset::AssetLockChangeSet; use crate::error::PlatformWalletError; use crate::wallet::platform_wallet::PlatformWalletInfo; +use crate::wallet::reservations::record_send_accepted; use super::super::manager::AssetLockManager; use super::super::orchestration::UNCONFIRMED_BROADCAST_PROOF_TIMEOUT; @@ -1174,7 +1175,9 @@ impl AssetLockManager { .expect("Built resumes hold a cleanup-exclusion claim") .preserve_on_drop(); match self.broadcaster.broadcast(&tx).await { - Ok(_) => {} + Ok(txid) => { + record_send_accepted(&self.wallet_manager, &self.wallet_id, txid).await; + } Err(BroadcastError::MaybeSent { reason }) => { tracing::warn!( outpoint = %out_point, @@ -1488,7 +1491,11 @@ impl AssetLockManager { // the status advance), which is precisely why the `Built` arm // above reaches the same verdict from the same position. let mut local_proof = None; - if let Err(e) = self.broadcaster.broadcast(&tx).await { + let rebroadcast = self.broadcaster.broadcast(&tx).await; + if let Ok(txid) = &rebroadcast { + record_send_accepted(&self.wallet_manager, &self.wallet_id, *txid).await; + } + if let Err(e) = rebroadcast { if matches!(e, BroadcastError::Rejected { .. }) { match self.wait_for_proof(out_point, Some(Duration::ZERO)).await { Ok(proof) => { diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index d5f0028d78e..6b202deead2 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -182,6 +182,33 @@ pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { })) } +/// This wallet's unsettled transactions, borrowed: for each, whether it spends +/// the wallet's own coins, and the transaction. The same multi-account rule as +/// [`merge_records`] — settled if any account holds it settled, own if any +/// records it spending the wallet's coins — without cloning anything, for a +/// reader that walks them on every balance or build call. +pub(crate) fn unsettled_transactions( + info: &PlatformWalletInfo, +) -> HashMap { + let mut settled: HashSet = HashSet::new(); + let mut unsettled: HashMap = HashMap::new(); + for account in info.core_wallet.accounts.all_accounts() { + for record in account.transactions().values() { + if is_settled(record) || account.transaction_is_finalized(&record.txid) { + settled.insert(record.txid); + continue; + } + let own = !record.input_details.is_empty(); + unsettled + .entry(record.txid) + .and_modify(|(was_own, _)| *was_own |= own) + .or_insert((own, &record.transaction)); + } + } + unsettled.retain(|txid, _| !settled.contains(txid)); + unsettled +} + /// This wallet's unsettled transactions as chains. `views` hold one entry per /// transaction (see [`merge_records`]). pub(crate) struct ChainGraph<'a> { @@ -1745,7 +1772,9 @@ enum ActorSlot { /// probe sends the signed transaction to evonodes over DAPI, which is a /// product decision, not something a wallet should start doing on upgrade. pub(crate) struct BroadcastResolver { - enabled: AtomicBool, + /// Shared with every wallet's fences: the coins of unresolved sends are + /// held exactly while probing is on (see `WalletGeneration::holds_unresolved_sends`). + enabled: Arc, /// Held while the flag is stored and the switch is queued, so the flag /// and the actor end in the same state under concurrent calls — event /// handlers gate on the flag. @@ -1773,7 +1802,7 @@ impl BroadcastResolver { ); let (commands, receiver) = mpsc::unbounded_channel(); Self { - enabled: AtomicBool::new(false), + enabled: Arc::new(AtomicBool::new(false)), commands, started: AtomicBool::new(false), actor: Mutex::new(ActorSlot::NotStarted(Box::new(actor), receiver)), @@ -1829,6 +1858,11 @@ impl BroadcastResolver { self.enabled.load(Ordering::SeqCst) } + /// The switch itself, for the wallets whose held coins follow it. + pub(crate) fn enabled_flag(&self) -> Arc { + Arc::clone(&self.enabled) + } + /// Forget a removed wallet — call after it has left the wallet manager: /// its pass in flight, its entry and every verdict, each of which the host /// is told to drop (from the resolver's task, not the caller's). diff --git a/packages/rs-platform-wallet/src/wallet/core/generation.rs b/packages/rs-platform-wallet/src/wallet/core/generation.rs index 31dd8610aac..9ebe74e872a 100644 --- a/packages/rs-platform-wallet/src/wallet/core/generation.rs +++ b/packages/rs-platform-wallet/src/wallet/core/generation.rs @@ -5,8 +5,9 @@ use std::collections::{HashMap, HashSet}; use std::ops::Deref; -use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; +use std::time::{Duration, Instant}; use dashcore::{OutPoint, Transaction, Txid}; use tokio::sync::{OwnedRwLockWriteGuard, RwLock, RwLockReadGuard}; @@ -263,13 +264,26 @@ pub(crate) struct InBroadcastFences { /// or mined (see [`WalletGeneration::accepted_among`]). /// /// Shared like the fences: acceptance is a fact about the network, not - /// about one in-memory instance. Kept to the wallet's unsettled sends — - /// every read forgets the rest. Not persisted: after a restart an - /// unsettled send is held again until its lock, a block or a fresh - /// verdict, which only errs on the side of not spending. + /// about one in-memory instance. A fact is kept while its send is among + /// the wallet's unsettled transactions, and for [`ACCEPTANCE_GRACE`] after + /// it was observed otherwise — a broadcast can return before its record + /// reaches the wallet. Not persisted: after a restart an unsettled send is + /// held again until its lock, a block or a fresh verdict, which only errs + /// on the side of not spending. acceptance: Mutex>, + /// Whether the coins of unresolved sends are held at all + /// ([`WalletGeneration::holds_unresolved_sends`]): the manager's + /// broadcast-probe switch, shared. Without probing nothing would ever find + /// such a send dead or mined beyond its own lock or block, so with it off + /// nothing is held and building behaves as it did before the hold. + hold_unresolved: Arc, } +/// How long an acceptance fact outlives its send's absence from the wallet's +/// unsettled records: covers a broadcast that returns before the wallet has +/// applied the transaction. +const ACCEPTANCE_GRACE: Duration = Duration::from_secs(10 * 60); + /// The last word on one send, and when it was observed — see /// [`next_acceptance_seq`]. A later observation replaces an earlier one, so a /// send found dead after it was found accepted is held again. @@ -277,6 +291,7 @@ pub(crate) struct InBroadcastFences { struct Acceptance { seq: u64, accepted: bool, + observed_at: Instant, } static ACCEPTANCE_SEQ: AtomicU64 = AtomicU64::new(1); @@ -290,6 +305,15 @@ pub(crate) fn next_acceptance_seq() -> u64 { } impl InBroadcastFences { + /// Fences for one wallet that hold the coins of its unresolved sends + /// whenever `hold_unresolved` — the manager's probe switch — is on. + pub(crate) fn with_hold_flag(hold_unresolved: Arc) -> Self { + Self { + hold_unresolved, + ..Self::default() + } + } + /// Recovers from a poisoned mutex rather than panicking — see /// [`WalletGeneration::in_broadcast_lock`]. fn lock(&self) -> MutexGuard<'_, HashMap> { @@ -442,7 +466,14 @@ impl WalletGeneration { let mut acceptance = self.in_broadcast.acceptance(); let newer = acceptance.get(&txid).is_none_or(|known| known.seq < seq); if newer { - acceptance.insert(txid, Acceptance { seq, accepted }); + acceptance.insert( + txid, + Acceptance { + seq, + accepted, + observed_at: Instant::now(), + }, + ); } } @@ -451,12 +482,31 @@ impl WalletGeneration { self.record_acceptance(txid, true, next_acceptance_seq()); } + /// Whether the coins of this wallet's unresolved sends are held from new + /// payments — on exactly while the manager probes unresolved broadcasts. + pub(crate) fn holds_unresolved_sends(&self) -> bool { + self.in_broadcast.hold_unresolved.load(Ordering::SeqCst) + } + + /// Turn the hold on or off for this wallet alone, as the manager's probe + /// switch would. + #[cfg(test)] + pub(crate) fn set_holds_unresolved_sends(&self, on: bool) { + self.in_broadcast + .hold_unresolved + .store(on, Ordering::SeqCst); + } + /// The sends among `unsettled` the network was last seen to accept. - /// Forgets every recorded send not among them — settled or gone — so the - /// record stays the size of the wallet's unsettled sends. + /// Forgets a recorded send that is not among them once + /// [`ACCEPTANCE_GRACE`] has passed since it was observed — settled or gone + /// by then — so the record stays about the size of the wallet's unsettled + /// sends, yet a fact that arrived before its send's record is kept. pub(crate) fn accepted_among(&self, unsettled: &HashSet) -> HashSet { let mut acceptance = self.in_broadcast.acceptance(); - acceptance.retain(|txid, _| unsettled.contains(txid)); + acceptance.retain(|txid, known| { + unsettled.contains(txid) || known.observed_at.elapsed() < ACCEPTANCE_GRACE + }); acceptance .iter() .filter(|(_, known)| known.accepted) diff --git a/packages/rs-platform-wallet/src/wallet/core/mod.rs b/packages/rs-platform-wallet/src/wallet/core/mod.rs index 0eba2e83050..ee54481864a 100644 --- a/packages/rs-platform-wallet/src/wallet/core/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/core/mod.rs @@ -16,7 +16,9 @@ pub use balance_handler::BalanceUpdateHandler; pub use generation::WalletGeneration; pub(crate) use generation::{InBroadcastFences, InBroadcastPin}; pub use spend_observer::SpendObservationHandler; -pub(crate) use transaction::resolve_source_accounts; +pub(crate) use transaction::{ + awaiting_network_or_build_error, held_input_cost, resolve_source_accounts, +}; pub use transaction::{SignedCoreTransaction, ASSET_LOCK_FUNDING_SOURCES, SEND_FUNDING_SOURCES}; pub(crate) use unresolved::UnresolvedSends; pub use wallet::CoreWallet; diff --git a/packages/rs-platform-wallet/src/wallet/core/transaction.rs b/packages/rs-platform-wallet/src/wallet/core/transaction.rs index a813e05418a..60f239b0c27 100644 --- a/packages/rs-platform-wallet/src/wallet/core/transaction.rs +++ b/packages/rs-platform-wallet/src/wallet/core/transaction.rs @@ -78,9 +78,12 @@ fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> Platfo /// A shortfall that the held coins of an unresolved send (see /// [`UnresolvedSends`]) would have covered becomes /// [`PlatformWalletError::CoreFundsAwaitingNetwork`], so the host can say the -/// money is waiting on the network rather than missing. A genuine shortfall — -/// held coins or not, the funds are not there — stays insufficient funds. -fn awaiting_network(error: PlatformWalletError, held: u64) -> PlatformWalletError { +/// money is waiting on the network rather than missing. `held` is what those +/// coins would add, net of the fee their own inputs cost +/// ([`UnresolvedSends::held_net_value`]). A genuine shortfall — held coins or +/// not, the funds are not there — and one whose size selection did not report +/// stay insufficient funds. +pub(crate) fn awaiting_network(error: PlatformWalletError, held: u64) -> PlatformWalletError { let (available, required) = match &error { PlatformWalletError::CoreInsufficientFunds { available, @@ -96,7 +99,7 @@ fn awaiting_network(error: PlatformWalletError, held: u64) -> PlatformWalletErro }; let covered = match (available, required) { (Some(available), Some(required)) => available.saturating_add(held) >= required, - _ => held > 0, + _ => false, }; if held == 0 || !covered { return error; @@ -131,6 +134,32 @@ fn checked_fee(fee_rate: FeeRate, size_bytes: usize) -> Result PlatformWalletError { + match awaiting_network( + map_builder_error(error.clone(), FundingContext::Pooled(sources)), + held, + ) { + awaiting @ PlatformWalletError::CoreFundsAwaitingNetwork { .. } => awaiting, + _ => PlatformWalletError::TransactionBuild(error.to_string()), + } +} + +/// The fee one more input adds at the default rate: what spending a held coin +/// would cost, for [`awaiting_network`]'s coverage test. A host on a higher +/// rate sees that test lean slightly towards "waiting on the network". +pub(crate) fn held_input_cost() -> Result { + let per_input = estimate_tx_size(1, 1, false).saturating_sub(estimate_tx_size(0, 1, false)); + checked_fee(FeeRate::normal(), per_input) +} + /// The output [`CoreWallet::pooled_max_sendable`] prices: one P2PKH, matching /// the `estimate_tx_size(_, 1, false)` model it sizes the transaction with. /// @@ -683,6 +712,7 @@ impl CoreWallet { let height = info.core_wallet.last_processed_height(); let unresolved = UnresolvedSends::of(info); + let held_input_cost = held_input_cost()?; let mut held_value = 0u64; // Fund from every resolved account, mirroring key-wallet's own @@ -732,14 +762,16 @@ impl CoreWallet { paths.insert(utxo.address.clone(), path); } } - held_value += unresolved.held_value(managed); + held_value += unresolved.held_net_value(managed, height, held_input_cost); // `reservation_only` offers no candidates of its own: the - // caller seeded its inputs explicitly (checked below). + // caller seeded its inputs explicitly. Either way the held + // coins are excluded — a seeded one fails the build. builder = if reservation_only { builder.add_funding_reservation_only(managed, account) } else { - unresolved.add_funding(builder, managed, account) + builder.add_funding(managed, account) }; + builder = unresolved.exclude_from(builder, managed); offered_accounts.push(at); } if offered_accounts.is_empty() { @@ -757,9 +789,23 @@ impl CoreWallet { FundingContext::Pooled(sources) }; let (unsigned, fee, reservation_token) = - builder.build_unsigned_reserved().map_err(|error| { - awaiting_network(map_builder_error(error, funding_context), held_value) - })?; + builder + .build_unsigned_reserved() + .map_err(|error| match error { + // A coin the caller seeded is held: refused before + // anything was reserved. + BuilderError::ExcludedInput { outpoint } => { + PlatformWalletError::CoreFundsAwaitingNetwork { + available: None, + held: held_value, + required: None, + outpoint: Some(outpoint), + } + } + error => { + awaiting_network(map_builder_error(error, funding_context), held_value) + } + })?; // Release across every contributing account on the error paths // below: the pooled reservation lives per account under the one @@ -799,20 +845,6 @@ impl CoreWallet { return Err(PlatformWalletError::InputMidBroadcast { outpoint }); } - // Refuse a held coin (see `UnresolvedSends`) the caller seeded - // itself — the `reservation_only` path and outpoint-chosen inputs, - // which skip the candidate filter above. Selection cannot pick one - // otherwise; this is also the backstop for that. - if let Some(outpoint) = unresolved.held_input(&unsigned) { - release_all!(offered_accounts, info.core_wallet.accounts, &unsigned); - return Err(PlatformWalletError::CoreFundsAwaitingNetwork { - available: None, - held: held_value, - required: None, - outpoint: Some(outpoint), - }); - } - // Map every selected input back to the account that owns it. That // mapping — not the offered list — is what the transaction carries: // selection routinely takes nothing from most offered sources, and @@ -1088,10 +1120,15 @@ mod tests { use key_wallet::wallet::managed_wallet_info::fee::{estimate_tx_size, FeeRate}; use crate::wallet::core::generation::next_acceptance_seq; - use crate::wallet::core::transaction::{modeled_output_script, MAX_STANDARD_TX_INPUTS}; + use crate::wallet::core::transaction::{ + awaiting_network, awaiting_network_or_build_error, held_input_cost, modeled_output_script, + MAX_STANDARD_TX_INPUTS, + }; use crate::wallet::core::{CoreWallet, WalletGeneration}; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; use crate::{PlatformWalletError, SEND_FUNDING_SOURCES}; + use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionError; + use key_wallet::wallet::managed_wallet_info::transaction_builder::BuilderError; fn preference(account_type: StandardAccountType) -> AccountTypePreference { match account_type { @@ -2088,6 +2125,8 @@ mod tests { ) { let (manager, wallet_id, generation, signer) = funded_wallet_manager_dual_standard(&[700_000], &[700_000]).await; + // As a manager with broadcast probing on: the hold follows it. + generation.set_holds_unresolved_sends(true); let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); let core = CoreWallet::new( sdk, @@ -2171,6 +2210,37 @@ mod tests { } } + /// With broadcast probing off nothing is held: building behaves exactly as + /// it did before the hold — nothing beyond a lock or a block would ever + /// release a held send, so a record stuck in mempool context would freeze + /// its coins for good. + #[tokio::test] + async fn should_hold_nothing_while_probing_is_off() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + { + let manager = manager.read().await; + let info = manager.get_wallet_info(&wallet_id).expect("wallet"); + info.generation.set_holds_unresolved_sends(false); + } + let paid = DashAddress::dummy(Network::Testnet, 80); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; + + assert_eq!( + spendable(&core).await, + 700_000 + change.value(), + "the change counts as before" + ); + core.finalize_transaction( + TransactionBuilder::new().add_output(&paid, 700_000 + change.value() / 2), + &SEND_FUNDING_SOURCES, + 0, + &signer, + ) + .await + .expect("the change funds a payment as before"); + } + /// The coins of an unresolved send are not spendable: left out of the /// spendable figure, of "max", and of selection. A payment only they would /// cover is refused as waiting on the network, not as a shortfall. @@ -2202,7 +2272,7 @@ mod tests { held, outpoint: None, .. - }) if held == change.value() + }) if held == change.value() - held_input_cost().expect("input cost") ), "got {refused:?}" ); @@ -2417,7 +2487,10 @@ mod tests { { let mut manager = manager.write().await; let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet"); - info.generation = Arc::new(WalletGeneration::new()); + // A restarted host turns probing on again. + let fresh = WalletGeneration::new(); + fresh.set_holds_unresolved_sends(true); + info.generation = Arc::new(fresh); } assert_eq!(spendable(&core).await, 700_000); @@ -2449,6 +2522,126 @@ mod tests { } } + /// An acceptance that arrives before the wallet has applied its send — a + /// broadcast returning ahead of the record — survives the reads in + /// between and releases the send once the record lands. + #[tokio::test] + async fn should_keep_an_acceptance_that_arrives_before_its_record() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 95); + let send = core + .finalize_transaction( + TransactionBuilder::new().add_output(&paid, 100_000), + &SEND_FUNDING_SOURCES, + 0, + &signer, + ) + .await + .expect("the send builds") + .transaction() + .clone(); + record_acceptance(&manager, &wallet_id, send.txid(), true).await; + // A balance read before the record lands walks the unsettled records. + let _ = spendable(&core).await; + apply_to_wallet(&manager, &wallet_id, &send, TransactionContext::Mempool).await; + + let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; + assert_eq!( + spendable(&core).await, + 700_000 + change.value(), + "the echoed send's change is spendable" + ); + } + + /// Only a shortfall the held coins would actually cover is "waiting on the + /// network". One they would not cover, or one whose size selection did not + /// report, stays insufficient funds. + #[test] + fn should_report_awaiting_network_only_when_the_held_coins_cover_the_shortfall() { + let shortfall = |available, required| PlatformWalletError::CorePooledInsufficientFunds { + sources: SEND_FUNDING_SOURCES.to_vec(), + available, + required, + }; + assert!(matches!( + awaiting_network(shortfall(Some(400), Some(1_000)), 600), + PlatformWalletError::CoreFundsAwaitingNetwork { .. } + )); + assert!(matches!( + awaiting_network(shortfall(Some(400), Some(1_000)), 599), + PlatformWalletError::CorePooledInsufficientFunds { .. } + )); + assert!(matches!( + awaiting_network(shortfall(Some(0), None), 1_000_000), + PlatformWalletError::CorePooledInsufficientFunds { .. } + )); + assert!(matches!( + awaiting_network(shortfall(Some(400), Some(1_000)), 0), + PlatformWalletError::CorePooledInsufficientFunds { .. } + )); + } + + /// The DashPay contact payment's build failure: a shortfall the held coins + /// cover is "waiting on the network"; any other failure keeps the build + /// error that path has always reported. + #[test] + fn should_map_a_contact_payment_shortfall_the_held_coins_cover() { + let shortfall = || { + BuilderError::CoinSelection(SelectionError::InsufficientFunds { + available: 400, + required: 1_000, + }) + }; + assert!(matches!( + awaiting_network_or_build_error(shortfall(), &SEND_FUNDING_SOURCES, 600), + PlatformWalletError::CoreFundsAwaitingNetwork { .. } + )); + assert!(matches!( + awaiting_network_or_build_error(shortfall(), &SEND_FUNDING_SOURCES, 10), + PlatformWalletError::TransactionBuild(_) + )); + assert!(matches!( + awaiting_network_or_build_error( + BuilderError::CoinSelection(SelectionError::NoUtxosAvailable), + &SEND_FUNDING_SOURCES, + 1_000_000, + ), + PlatformWalletError::TransactionBuild(_) + )); + } + + /// A broadcast that releases its reservation on a rejection records an + /// echoed send as accepted too. + #[tokio::test] + async fn should_mark_a_send_accepted_when_a_releasing_broadcast_is_echoed() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let finalized = core + .finalize_transaction( + TransactionBuilder::new() + .add_output(&DashAddress::dummy(Network::Testnet, 96), 300_000), + &SEND_FUNDING_SOURCES, + 0, + &signer, + ) + .await + .expect("finalize"); + let txid = finalized.transaction().txid(); + core.broadcast_transaction_releasing_reservation( + StandardAccountType::BIP44Account, + 0, + finalized.transaction(), + ) + .await + .expect("broadcast"); + + let manager = manager.read().await; + let info = manager.get_wallet_info(&wallet_id).expect("wallet"); + assert!(info + .generation + .accepted_among(&HashSet::from([txid])) + .contains(&txid)); + } + /// The address of the wallet's BIP32 coin: one of our own receive /// addresses. async fn utxo_addresses(manager: &Manager, wallet_id: &WalletId) -> DashAddress { diff --git a/packages/rs-platform-wallet/src/wallet/core/unresolved.rs b/packages/rs-platform-wallet/src/wallet/core/unresolved.rs index 80ff32af536..bc98324fc34 100644 --- a/packages/rs-platform-wallet/src/wallet/core/unresolved.rs +++ b/packages/rs-platform-wallet/src/wallet/core/unresolved.rs @@ -5,18 +5,17 @@ //! between our own accounts. If that send never reached the network those //! coins are phantom, and every payment built on them is an orphan no node //! accepts: the shape behind "Transaction status unknown" followed by payments -//! that never land. So they are neither offered to a new payment nor counted -//! as spendable until the send is known to be accepted. +//! that never land. So, while the manager probes unresolved broadcasts, they +//! are neither offered to a new payment nor counted as spendable until the +//! send is known to be accepted. -use std::collections::HashSet; +use std::collections::{HashMap, HashSet, VecDeque}; -use dashcore::{OutPoint, Transaction, Txid}; -use key_wallet::managed_account::managed_account_collection::ManagedAccountCollection; +use dashcore::{OutPoint, Txid}; use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; use key_wallet::wallet::managed_wallet_info::transaction_builder::TransactionBuilder; -use key_wallet::Account; -use super::broadcast_resolver::collect_views; +use super::broadcast_resolver::unsettled_transactions; use crate::wallet::platform_wallet::PlatformWalletInfo; /// The wallet's unsettled transactions whose outputs are held: every own send @@ -24,6 +23,12 @@ use crate::wallet::platform_wallet::PlatformWalletInfo; /// mined and that the network was not last seen to accept, and every unsettled /// transaction spending an output of one of those, transitively. /// +/// Empty while the manager does not probe unresolved broadcasts +/// ([`WalletGeneration::holds_unresolved_sends`](super::WalletGeneration::holds_unresolved_sends)): +/// without probing nothing beyond a lock or a block would ever release a held +/// send, and a record stuck in mempool context would freeze its coins for +/// good. +/// /// Derived from the wallet's own records on every read, so it holds the same /// after a restart. Only the acceptance facts (a peer echoing a broadcast, a /// probe verdict) live in memory: until one arrives again, an unsettled send @@ -35,32 +40,44 @@ pub(crate) struct UnresolvedSends { impl UnresolvedSends { pub(crate) fn of(info: &PlatformWalletInfo) -> Self { - let views = collect_views(info); - let unsettled: HashSet = views.iter().map(|view| view.txid).collect(); - let accepted = info.generation.accepted_among(&unsettled); - let mut held: HashSet = views - .iter() - .filter(|view| view.spends_own_coins && !accepted.contains(&view.txid)) - .map(|view| view.txid) - .collect(); + if !info.generation.holds_unresolved_sends() { + return Self { + txids: HashSet::new(), + }; + } + let unsettled = unsettled_transactions(info); + let unsettled_txids: HashSet = unsettled.keys().copied().collect(); + let accepted = info.generation.accepted_among(&unsettled_txids); + + // Each unsettled transaction under every unsettled parent it spends, + // so the held set is one walk down from the held roots. + let mut children: HashMap> = HashMap::new(); + for (txid, (_, transaction)) in &unsettled { + let parents: HashSet = transaction + .input + .iter() + .map(|input| input.previous_output.txid) + .filter(|parent| unsettled.contains_key(parent)) + .collect(); + for parent in parents { + children.entry(parent).or_default().push(*txid); + } + } + // A transaction built on a held output is no better than its parent: // a node that has not seen the parent refuses it. - loop { - let before = held.len(); - for view in &views { - if !held.contains(&view.txid) - && view - .transaction - .input - .iter() - .any(|input| held.contains(&input.previous_output.txid)) - { - held.insert(view.txid); + let mut held: HashSet = HashSet::new(); + let mut queue: VecDeque = unsettled + .iter() + .filter(|(txid, (own, _))| *own && !accepted.contains(*txid)) + .map(|(txid, _)| *txid) + .collect(); + while let Some(txid) = queue.pop_front() { + if held.insert(txid) { + if let Some(spenders) = children.get(&txid) { + queue.extend(spenders.iter().copied()); } } - if held.len() == before { - break; - } } Self { txids: held } } @@ -70,95 +87,50 @@ impl UnresolvedSends { self.txids.contains(&outpoint.txid) } - /// The value of `managed`'s held coins. - pub(crate) fn held_value(&self, managed: &ManagedCoreFundsAccount) -> u64 { + /// `managed`'s held coins. + pub(crate) fn held_in<'a>( + &'a self, + managed: &'a ManagedCoreFundsAccount, + ) -> impl Iterator + 'a { managed .utxos - .values() - .filter(|utxo| self.holds(&utxo.outpoint)) - .map(|utxo| utxo.value()) - .sum() + .keys() + .copied() + .filter(move |outpoint| self.holds(outpoint)) } - /// The first input of `transaction` that spends a held coin. - pub(crate) fn held_input(&self, transaction: &Transaction) -> Option { - transaction - .input - .iter() - .map(|input| input.previous_output) - .find(|outpoint| self.holds(outpoint)) - } - - /// [`TransactionBuilder::add_funding`] with `managed`'s held coins left - /// out of selection. - /// - /// `add_funding` copies the account's UTXOs into the builder as candidates, - /// and selection drops a candidate that is locked. So the held coins are - /// locked for that one call and unlocked right after — the copies keep the - /// lock, the wallet does not. The caller holds the manager write guard - /// across it, so no other reader sees the transient lock. A coin already - /// locked for another reason is left as it is. - pub(crate) fn add_funding( + /// `builder` told never to spend `managed`'s held coins: a funding + /// candidate is left out of selection, an input the caller seeded is + /// refused (key-wallet's `BuilderError::ExcludedInput`). Nothing is + /// recorded on the wallet. + pub(crate) fn exclude_from( &self, builder: TransactionBuilder, - managed: &mut ManagedCoreFundsAccount, - account: &Account, + managed: &ManagedCoreFundsAccount, ) -> TransactionBuilder { - let locked = self.lock_in_account(managed); - let builder = builder.add_funding(managed, account); - unlock_in_account(managed, &locked); - builder - } - - /// Lock every held coin of every funds account in `accounts` that is not - /// locked already, for a builder that selects across accounts on its own - /// (the asset-lock builder). Returns what was locked, for - /// [`unlock_in`](Self::unlock_in). The caller holds the manager write guard - /// until the unlock. - pub(crate) fn lock_in(&self, accounts: &mut ManagedAccountCollection) -> Vec { - accounts - .all_funding_accounts_mut() - .into_iter() - .flat_map(|managed| self.lock_in_account(managed)) - .collect() - } - - /// Undo [`lock_in`](Self::lock_in). - pub(crate) fn unlock_in(accounts: &mut ManagedAccountCollection, locked: &[OutPoint]) { - for managed in accounts.all_funding_accounts_mut() { - unlock_in_account(managed, locked); + if self.txids.is_empty() { + return builder; } + builder.exclude_outpoints(self.held_in(managed).collect::>()) } - /// The held value across every funds account in `accounts`. - pub(crate) fn held_value_in(&self, accounts: &ManagedAccountCollection) -> u64 { - accounts - .all_funding_accounts() - .into_iter() - .map(|managed| self.held_value(managed)) - .sum() - } - - fn lock_in_account(&self, managed: &mut ManagedCoreFundsAccount) -> Vec { - let locked: Vec = managed - .utxos - .values() - .filter(|utxo| !utxo.is_locked && self.holds(&utxo.outpoint)) - .map(|utxo| utxo.outpoint) - .collect(); - for outpoint in &locked { - if let Some(utxo) = managed.utxos.get_mut(outpoint) { - utxo.lock(); - } - } - locked - } -} - -fn unlock_in_account(managed: &mut ManagedCoreFundsAccount, locked: &[OutPoint]) { - for outpoint in locked { - if let Some(utxo) = managed.utxos.get_mut(outpoint) { - utxo.unlock(); + /// What `managed`'s held coins could add to a payment once released: the + /// spendable ones at `height`, each net of `input_cost`, the fee its own + /// input adds. The test for whether a shortfall is the held coins' doing. + pub(crate) fn held_net_value( + &self, + managed: &ManagedCoreFundsAccount, + height: u32, + input_cost: u64, + ) -> u64 { + if self.txids.is_empty() { + return 0; } + managed + .spendable_utxos(height) + .iter() + .filter(|utxo| self.holds(&utxo.outpoint)) + .map(|utxo| utxo.value().saturating_sub(input_cost)) + .sum() } } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 1b9e0941ae9..02eeca46ce2 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -12,8 +12,9 @@ use key_wallet_manager::WalletManager; use super::*; use crate::broadcaster::TransactionBroadcaster; use crate::error::PlatformWalletError; -use crate::wallet::core::UnresolvedSends; +use crate::wallet::core::{awaiting_network_or_build_error, held_input_cost, UnresolvedSends}; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; +use crate::SEND_FUNDING_SOURCES; // --------------------------------------------------------------------------- // Incoming payment recording + reconcile @@ -1257,6 +1258,8 @@ impl DashPayView<'_, B> { // Coins of our own sends the network has not been seen to accept // stay out of selection (see `UnresolvedSends`). let unresolved = UnresolvedSends::of(info); + let held_input_cost = held_input_cost()?; + let mut held_value = 0u64; // Derivation paths for every offered UTXO, since the signer closure // below cannot resolve them from one account. @@ -1293,7 +1296,10 @@ impl DashPayView<'_, B> { funding_paths.insert(utxo.address.clone(), path); } } - builder = unresolved.add_funding(builder, managed, account); + held_value += + unresolved.held_net_value(managed, current_height, held_input_cost); + builder = + unresolved.exclude_from(builder.add_funding(managed, account), managed); offered_accounts.push(at); } } @@ -1342,7 +1348,11 @@ impl DashPayView<'_, B> { { return_contact_payment_address_to_pool(external_account, &payment_address); } - return Err(PlatformWalletError::TransactionBuild(e.to_string())); + return Err(awaiting_network_or_build_error( + e, + &SEND_FUNDING_SOURCES, + held_value, + )); } }; diff --git a/packages/rs-platform-wallet/src/wallet/reservations.rs b/packages/rs-platform-wallet/src/wallet/reservations.rs index fb4ddd7676b..a2a61370ca7 100644 --- a/packages/rs-platform-wallet/src/wallet/reservations.rs +++ b/packages/rs-platform-wallet/src/wallet/reservations.rs @@ -33,6 +33,7 @@ use key_wallet_manager::WalletManager; use tokio::sync::RwLock; use crate::broadcaster::{BroadcastError, TransactionBroadcaster}; +use crate::wallet::core::generation::next_acceptance_seq; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; /// Maximum age, in `last_processed_height` blocks, of a held funding @@ -126,6 +127,23 @@ pub(crate) fn reservation_expired(registered_height: u32, current_height: Option } } +/// A broadcast of ours returned accepted — a peer echoed it back. Record it, +/// so its outputs are not held as an unresolved send's +/// ([`UnresolvedSends`](crate::wallet::core::UnresolvedSends)). The sequence +/// number is taken here, where the fact is observed, before the lock is +/// awaited. +pub(crate) async fn record_send_accepted( + wallet_manager: &RwLock>, + wallet_id: &WalletId, + txid: Txid, +) { + let seq = next_acceptance_seq(); + let manager = wallet_manager.read().await; + if let Some((_, info)) = manager.get_wallet_and_info(wallet_id) { + info.generation.record_acceptance(txid, true, seq); + } +} + /// Broadcast `tx` and reconcile the funding account's UTXO reservation on /// failure. /// @@ -152,7 +170,10 @@ pub(crate) async fn broadcast_releasing_on_rejection Result { match broadcaster.broadcast(tx).await { - Ok(txid) => Ok(txid), + Ok(txid) => { + record_send_accepted(wallet_manager, wallet_id, txid).await; + Ok(txid) + } Err(e) => { if matches!(e, BroadcastError::Rejected { .. }) { release_reservation_after_rejected_broadcast( From 3634e3a81c091323c69f9c41269b1531ac4510d0 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 14:31:49 +0300 Subject: [PATCH 051/169] fix(platform-wallet): review follow-ups to the unresolved-send hold - A wallet whose only coins are held fails selection with no size (NoUtxosAvailable); that shortfall is now measured against what the build requested (its outputs plus the fee of a transaction carrying them, key-wallet's TransactionBuilder::outputs) and reported as CoreFundsAwaitingNetwork when the held coins cover it. - A reservation_only build spends only its seeded inputs, so held coins count for nothing in its shortfall. - A node that accepted a transaction has its parents: acceptance of a child (e.g. an echo of the startup re-dispatch) counts for every unsettled ancestor, unless that ancestor's own record is newer (found dead since). - The multi-account settled/own rule lives in one place (merge_unsettled), used by the resolver's views and the hold alike; it collects the unsettled records first and only drops matches on a second pass, so no set of the whole history is built per balance or build call. - Acceptance facts are not kept while the hold is off (probing disabled): only the hold reads and prunes them. - held_input_cost is infallible (fixed rate, one input), so the contact payment can no longer bail out after marking its address used. - Asset locks: no hold is wired because key-wallet's require_final_inputs never selects an unconfirmed, non-InstantSend-locked coin; a test pins it. - Doc comment restored on the ambiguous asset-lock broadcast test; InBroadcastFences imported at the top of manager/mod.rs. Builds only against rust-dashcore feat/funding-exclude-outpoints (local f096c013..1cbb19a0) until the rust-dashcore pin moves. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/mod.rs | 14 +- .../src/wallet/asset_lock/build.rs | 90 ++++++- .../src/wallet/core/broadcast_resolver.rs | 107 ++++---- .../src/wallet/core/generation.rs | 29 +- .../rs-platform-wallet/src/wallet/core/mod.rs | 2 +- .../src/wallet/core/transaction.rs | 253 +++++++++++++++--- .../src/wallet/core/unresolved.rs | 37 ++- .../src/wallet/identity/network/payments.rs | 8 +- 8 files changed, 431 insertions(+), 109 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index d8cbd607271..01b941cb503 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -36,7 +36,7 @@ use crate::manager::shielded_sync::ShieldedSyncManager; use crate::spv::SpvRuntime; use crate::wallet::asset_lock::LockNotifyHandler; use crate::wallet::core::broadcast_resolver::BroadcastResolver; -use crate::wallet::core::{BalanceUpdateHandler, SpendObservationHandler}; +use crate::wallet::core::{BalanceUpdateHandler, InBroadcastFences, SpendObservationHandler}; use crate::wallet::identity::network::DashPayPaymentHandler; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; use crate::wallet::PlatformWallet; @@ -472,9 +472,8 @@ pub struct PlatformWalletManager { /// /// A `std::sync::Mutex`: touched only at wallet registration and load, for /// one map lookup, and never held across an await. - pub(super) in_broadcast_fences: std::sync::Mutex< - std::collections::BTreeMap>, - >, + pub(super) in_broadcast_fences: + std::sync::Mutex>>, } impl PlatformWalletManager

{ @@ -640,17 +639,14 @@ impl PlatformWalletManager

{ /// replaces another under the same id inherits its pending-spend fences — /// see the [`in_broadcast_fences`](Self#structfield.in_broadcast_fences) /// field docs. - pub(super) fn in_broadcast_fences_for( - &self, - wallet_id: &WalletId, - ) -> Arc { + pub(super) fn in_broadcast_fences_for(&self, wallet_id: &WalletId) -> Arc { Arc::clone( self.in_broadcast_fences .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .entry(*wallet_id) .or_insert_with(|| { - Arc::new(crate::wallet::core::InBroadcastFences::with_hold_flag( + Arc::new(InBroadcastFences::with_hold_flag( self.broadcast_resolver.enabled_flag(), )) }), diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs index 6f15b2a2625..fa17badf874 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs @@ -274,6 +274,9 @@ impl AssetLockManager { // pools the sources, enforces that CoinJoin funding is drain-only and // unpooled, and reserves the selected inputs in each contributing // account's own set under one owner token). + // No unresolved-send hold here (see `UnresolvedSends`): the builder + // calls `require_final_inputs`, so only confirmed or InstantSend-locked + // coins are ever selected, and an unresolved send's outputs are neither. let result = info .core_wallet .build_asset_lock_with_signer( @@ -1383,16 +1386,20 @@ mod tests { ClientStartState, PersistenceError, PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::test_support::{ - funded_wallet_manager, funded_wallet_manager_dual_standard, + apply_to_wallet, funded_wallet_manager, funded_wallet_manager_dual_standard, funded_wallet_manager_with_contact, AlwaysMaybeSentBroadcaster, AlwaysOkBroadcaster, AlwaysRejectedBroadcaster, WalletSigner, }; use crate::wallet::asset_lock::manager::AssetLockManager; use crate::wallet::asset_lock::tracked::AssetLockStatus; + use crate::wallet::core::CoreWallet; use crate::wallet::persister::WalletPersister; use crate::wallet::platform_wallet::PlatformWalletInfo; use crate::wallet::platform_wallet::WalletId; + use crate::SEND_FUNDING_SOURCES; use crate::{AssetLockFundingType, PlatformWalletError}; + use dashcore::{Address as DashAddress, Network}; + use key_wallet::wallet::managed_wallet_info::transaction_builder::TransactionBuilder; /// The zero-spendable-candidate selection error must surface the SAME /// typed shortfall as a partial shortfall (not the generic string form), @@ -2014,16 +2021,85 @@ mod tests { ); } - /// An *ambiguous* asset-lock broadcast failure must keep both the funding - /// reservation and the resumable `Built` row: the transaction may already - /// be propagating, so a retry must not double-spend and a resume must - /// stay possible. + /// An asset lock never funds from the coins of an unresolved send of + /// ours: key-wallet's `require_final_inputs` keeps only confirmed or + /// InstantSend-locked coins, so with the hold on and the send's change the + /// wallet's only coin, the lock is a plain shortfall, never built on it. + #[tokio::test] + async fn should_never_fund_an_asset_lock_from_an_unresolved_sends_change() { + let (wallet_manager, wallet_id, generation, signer) = + funded_wallet_manager(StandardAccountType::BIP44Account).await; + generation.set_holds_unresolved_sends(true); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let core = CoreWallet::new( + Arc::clone(&sdk), + Arc::clone(&wallet_manager), + wallet_id, + Arc::new(AlwaysOkBroadcaster), + generation, + ); + // Spend the one confirmed coin; its change is all the wallet has left. + let finalized = core + .finalize_transaction( + TransactionBuilder::new() + .add_output(&DashAddress::dummy(Network::Testnet, 95), 1_000_000), + &SEND_FUNDING_SOURCES, + 0, + &signer, + ) + .await + .expect("the send builds"); + apply_to_wallet( + &wallet_manager, + &wallet_id, + finalized.transaction(), + TransactionContext::Mempool, + ) + .await; + let manager = AssetLockManager::new( + sdk, + wallet_manager, + wallet_id, + Arc::new(Notify::new()), + Arc::new(AlwaysOkBroadcaster), + WalletPersister::new( + wallet_id, + Arc::new(CapturingPersistence::default()) as Arc, + ), + ); + + let refused = manager + .create_funded_asset_lock_proof( + 100_000, + 0, + AssetLockFundingType::IdentityRegistration, + 0, + &signer, + ) + .await; + + assert!( + matches!( + refused, + Err(PlatformWalletError::AssetLockInsufficientFunds { .. }) + ), + "got {:?}", + refused.map(|_| ()) + ); + } + /// An echoed asset-lock broadcast is recorded as accepted, so its change /// is not held as an unresolved send's while its InstantSend lock is due. #[tokio::test] async fn should_mark_an_echoed_asset_lock_accepted() { let (manager, signer, _persistence) = funded_asset_lock_manager(Arc::new(AlwaysOkBroadcaster)).await; + { + // Facts are kept only while the hold is on, as with probing on. + let wm = manager.wallet_manager.read().await; + let (_, info) = wm.get_wallet_and_info(&manager.wallet_id).expect("wallet"); + info.generation.set_holds_unresolved_sends(true); + } let (_, out_point) = manager .broadcast_funded_asset_lock( @@ -2045,6 +2121,10 @@ mod tests { .contains(&txid)); } + /// An *ambiguous* asset-lock broadcast failure must keep both the funding + /// reservation and the resumable `Built` row: the transaction may already + /// be propagating, so a retry must not double-spend and a resume must + /// stay possible. #[tokio::test] async fn ambiguous_asset_lock_broadcast_keeps_reservation_and_built_row() { let (manager, signer, persistence) = diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 6b202deead2..cc8a04999e6 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -124,6 +124,7 @@ impl OutgoingView { } /// One account's record of a transaction, as far as the resolver cares. +#[derive(Clone, Copy)] pub(crate) struct RecordFacts<'a> { pub txid: Txid, /// In a block, InstantSend-locked, or finalized in this account. @@ -137,25 +138,47 @@ pub(crate) struct RecordFacts<'a> { /// transaction however many accounts record it; it is settled if any account /// holds it settled, and the wallet's own send if any account records it /// spending the wallet's coins (a transfer between accounts is "incoming" in -/// the receiving one). Returns the unsettled ones. +/// the receiving one). Returns the unsettled ones, borrowed: for each, whether +/// it spends the wallet's own coins, and the transaction. +/// +/// Two passes over `records()`: the unsettled ones are collected first, then +/// each settled record only drops a matching entry — no set of the whole +/// history is built, since a healthy wallet has none or a handful unsettled. +fn merge_unsettled<'a, I>(records: impl Fn() -> I) -> HashMap +where + I: IntoIterator>, +{ + let mut unsettled: HashMap = HashMap::new(); + for record in records() { + if !record.settled { + unsettled + .entry(record.txid) + .and_modify(|(own, _)| *own |= record.own) + .or_insert((record.own, record.transaction)); + } + } + if !unsettled.is_empty() { + for record in records() { + if record.settled { + unsettled.remove(&record.txid); + } + } + } + unsettled +} + +/// [`merge_unsettled`] over a list of records, as views. +#[cfg(test)] pub(crate) fn merge_records<'a>( records: impl IntoIterator>, ) -> Vec { - let mut settled: HashSet = HashSet::new(); - let mut unsettled: HashMap = HashMap::new(); - for record in records { - if record.settled { - settled.insert(record.txid); - continue; - } - unsettled - .entry(record.txid) - .and_modify(|(own, _)| *own |= record.own) - .or_insert((record.own, record.transaction)); - } + let records: Vec> = records.into_iter().collect(); + into_views(merge_unsettled(|| records.iter().copied())) +} + +fn into_views(unsettled: HashMap) -> Vec { unsettled .into_iter() - .filter(|(txid, _)| !settled.contains(txid)) .map(|(txid, (own, transaction))| OutgoingView { txid, spends_own_coins: own, @@ -164,49 +187,31 @@ pub(crate) fn merge_records<'a>( .collect() } -/// This wallet's unsettled transactions. One walk over the records; only -/// unsettled ones are cloned — on a healthy wallet there are none or a -/// handful. +/// This wallet's unsettled transactions, as views: only those are cloned — on +/// a healthy wallet there are none or a handful. pub(crate) fn collect_views(info: &PlatformWalletInfo) -> Vec { - let accounts = info.core_wallet.accounts.all_accounts(); - merge_records(accounts.iter().flat_map(|account| { - account - .transactions() - .values() - .map(move |record| RecordFacts { - txid: record.txid, - settled: is_settled(record) || account.transaction_is_finalized(&record.txid), - own: !record.input_details.is_empty(), - transaction: &record.transaction, - }) - })) + into_views(unsettled_transactions(info)) } -/// This wallet's unsettled transactions, borrowed: for each, whether it spends -/// the wallet's own coins, and the transaction. The same multi-account rule as -/// [`merge_records`] — settled if any account holds it settled, own if any -/// records it spending the wallet's coins — without cloning anything, for a -/// reader that walks them on every balance or build call. +/// This wallet's unsettled transactions, borrowed, under [`merge_unsettled`]'s +/// rule — for a reader that walks them on every balance or build call. pub(crate) fn unsettled_transactions( info: &PlatformWalletInfo, ) -> HashMap { - let mut settled: HashSet = HashSet::new(); - let mut unsettled: HashMap = HashMap::new(); - for account in info.core_wallet.accounts.all_accounts() { - for record in account.transactions().values() { - if is_settled(record) || account.transaction_is_finalized(&record.txid) { - settled.insert(record.txid); - continue; - } - let own = !record.input_details.is_empty(); - unsettled - .entry(record.txid) - .and_modify(|(was_own, _)| *was_own |= own) - .or_insert((own, &record.transaction)); - } - } - unsettled.retain(|txid, _| !settled.contains(txid)); - unsettled + let accounts = info.core_wallet.accounts.all_accounts(); + merge_unsettled(|| { + accounts.iter().flat_map(|account| { + account + .transactions() + .values() + .map(move |record| RecordFacts { + txid: record.txid, + settled: is_settled(record) || account.transaction_is_finalized(&record.txid), + own: !record.input_details.is_empty(), + transaction: &record.transaction, + }) + }) + }) } /// This wallet's unsettled transactions as chains. `views` hold one entry per diff --git a/packages/rs-platform-wallet/src/wallet/core/generation.rs b/packages/rs-platform-wallet/src/wallet/core/generation.rs index 9ebe74e872a..be7dfe642f1 100644 --- a/packages/rs-platform-wallet/src/wallet/core/generation.rs +++ b/packages/rs-platform-wallet/src/wallet/core/generation.rs @@ -261,7 +261,7 @@ pub(crate) struct InBroadcastFences { /// peer echoed the broadcast back or a probe found it accepted or mined /// (accepted), or a probe found it dead. Only an accepted send's outputs /// may fund the next payment before the wallet sees it InstantSend-locked - /// or mined (see [`WalletGeneration::accepted_among`]). + /// or mined (see [`WalletGeneration::acceptance_among`]). /// /// Shared like the fences: acceptance is a fact about the network, not /// about one in-memory instance. A fact is kept while its send is among @@ -461,9 +461,15 @@ impl WalletGeneration { /// Record what the network said about our send `txid` (accepted, or /// found dead), observed at `seq` ([`next_acceptance_seq`]). An older - /// observation than the one recorded is dropped. + /// observation than the one recorded is dropped. Nothing is kept while the + /// hold is off: only the hold reads these facts, and only its reads prune + /// them. pub(crate) fn record_acceptance(&self, txid: Txid, accepted: bool, seq: u64) { let mut acceptance = self.in_broadcast.acceptance(); + if !self.holds_unresolved_sends() { + acceptance.clear(); + return; + } let newer = acceptance.get(&txid).is_none_or(|known| known.seq < seq); if newer { acceptance.insert( @@ -497,20 +503,31 @@ impl WalletGeneration { .store(on, Ordering::SeqCst); } - /// The sends among `unsettled` the network was last seen to accept. + /// The latest fact recorded about each send among `unsettled`: whether + /// the network was seen to accept it, and the sequence it was observed at. /// Forgets a recorded send that is not among them once /// [`ACCEPTANCE_GRACE`] has passed since it was observed — settled or gone /// by then — so the record stays about the size of the wallet's unsettled /// sends, yet a fact that arrived before its send's record is kept. - pub(crate) fn accepted_among(&self, unsettled: &HashSet) -> HashSet { + pub(crate) fn acceptance_among(&self, unsettled: &HashSet) -> HashMap { let mut acceptance = self.in_broadcast.acceptance(); acceptance.retain(|txid, known| { unsettled.contains(txid) || known.observed_at.elapsed() < ACCEPTANCE_GRACE }); acceptance .iter() - .filter(|(_, known)| known.accepted) - .map(|(txid, _)| *txid) + .filter(|(txid, _)| unsettled.contains(*txid)) + .map(|(txid, known)| (*txid, (known.accepted, known.seq))) + .collect() + } + + /// The sends among `unsettled` whose latest fact is accepted. + #[cfg(test)] + pub(crate) fn accepted_among(&self, unsettled: &HashSet) -> HashSet { + self.acceptance_among(unsettled) + .into_iter() + .filter(|(_, (accepted, _))| *accepted) + .map(|(txid, _)| txid) .collect() } diff --git a/packages/rs-platform-wallet/src/wallet/core/mod.rs b/packages/rs-platform-wallet/src/wallet/core/mod.rs index ee54481864a..9774c9dba3e 100644 --- a/packages/rs-platform-wallet/src/wallet/core/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/core/mod.rs @@ -17,7 +17,7 @@ pub use generation::WalletGeneration; pub(crate) use generation::{InBroadcastFences, InBroadcastPin}; pub use spend_observer::SpendObservationHandler; pub(crate) use transaction::{ - awaiting_network_or_build_error, held_input_cost, resolve_source_accounts, + awaiting_network_or_build_error, held_input_cost, requested_amount, resolve_source_accounts, }; pub use transaction::{SignedCoreTransaction, ASSET_LOCK_FUNDING_SOURCES, SEND_FUNDING_SOURCES}; pub(crate) use unresolved::UnresolvedSends; diff --git a/packages/rs-platform-wallet/src/wallet/core/transaction.rs b/packages/rs-platform-wallet/src/wallet/core/transaction.rs index 60f239b0c27..56b3bb955f7 100644 --- a/packages/rs-platform-wallet/src/wallet/core/transaction.rs +++ b/packages/rs-platform-wallet/src/wallet/core/transaction.rs @@ -8,7 +8,7 @@ use std::collections::{HashMap, HashSet}; use std::sync::Arc; -use dashcore::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction}; +use dashcore::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction, TxOut}; use key_wallet::account::AccountType; use key_wallet::managed_account::managed_account_collection::ManagedAccountCollection; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; @@ -80,10 +80,15 @@ fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> Platfo /// [`PlatformWalletError::CoreFundsAwaitingNetwork`], so the host can say the /// money is waiting on the network rather than missing. `held` is what those /// coins would add, net of the fee their own inputs cost -/// ([`UnresolvedSends::held_net_value`]). A genuine shortfall — held coins or -/// not, the funds are not there — and one whose size selection did not report -/// stay insufficient funds. -pub(crate) fn awaiting_network(error: PlatformWalletError, held: u64) -> PlatformWalletError { +/// ([`UnresolvedSends::held_net_value`]). When selection did not report the +/// size of the shortfall — every candidate was held, say — it is measured +/// against `requested` ([`requested_amount`]). A genuine shortfall — held +/// coins or not, the funds are not there — stays insufficient funds. +pub(crate) fn awaiting_network( + error: PlatformWalletError, + held: u64, + requested: u64, +) -> PlatformWalletError { let (available, required) = match &error { PlatformWalletError::CoreInsufficientFunds { available, @@ -97,21 +102,31 @@ pub(crate) fn awaiting_network(error: PlatformWalletError, held: u64) -> Platfor } => (*available, *required), _ => return error, }; - let covered = match (available, required) { - (Some(available), Some(required)) => available.saturating_add(held) >= required, - _ => false, - }; + let required = required.unwrap_or(requested); + let covered = available.unwrap_or(0).saturating_add(held) >= required; if held == 0 || !covered { return error; } PlatformWalletError::CoreFundsAwaitingNetwork { available, held, - required, + required: Some(required), outpoint: None, } } +/// What a build asks of selection: its outputs plus the fee of a transaction +/// carrying them and a change output, before any input — the inputs' share is +/// already netted out of the held value ([`held_input_cost`]). +pub(crate) fn requested_amount(outputs: &[TxOut]) -> u64 { + let total: u64 = outputs.iter().map(|output| output.value).sum(); + total.saturating_add(FeeRate::normal().calculate_fee(estimate_tx_size( + 0, + outputs.len() + 1, + false, + ))) +} + /// key-wallet's `FeeRate::calculate_fee`, with the multiplication checked. /// /// Upstream computes `(sat_per_kb * size_bytes).div_ceil(1000)` unchecked, and @@ -142,10 +157,12 @@ pub(crate) fn awaiting_network_or_build_error( error: BuilderError, sources: &[AccountTypePreference], held: u64, + requested: u64, ) -> PlatformWalletError { match awaiting_network( map_builder_error(error.clone(), FundingContext::Pooled(sources)), held, + requested, ) { awaiting @ PlatformWalletError::CoreFundsAwaitingNetwork { .. } => awaiting, _ => PlatformWalletError::TransactionBuild(error.to_string()), @@ -154,10 +171,11 @@ pub(crate) fn awaiting_network_or_build_error( /// The fee one more input adds at the default rate: what spending a held coin /// would cost, for [`awaiting_network`]'s coverage test. A host on a higher -/// rate sees that test lean slightly towards "waiting on the network". -pub(crate) fn held_input_cost() -> Result { +/// rate sees that test lean slightly towards "waiting on the network". A fixed +/// rate and a one-input size, so it cannot overflow. +pub(crate) fn held_input_cost() -> u64 { let per_input = estimate_tx_size(1, 1, false).saturating_sub(estimate_tx_size(0, 1, false)); - checked_fee(FeeRate::normal(), per_input) + FeeRate::normal().calculate_fee(per_input) } /// The output [`CoreWallet::pooled_max_sendable`] prices: one P2PKH, matching @@ -712,7 +730,8 @@ impl CoreWallet { let height = info.core_wallet.last_processed_height(); let unresolved = UnresolvedSends::of(info); - let held_input_cost = held_input_cost()?; + let held_input_cost = held_input_cost(); + let requested = requested_amount(builder.outputs()); let mut held_value = 0u64; // Fund from every resolved account, mirroring key-wallet's own @@ -762,10 +781,14 @@ impl CoreWallet { paths.insert(utxo.address.clone(), path); } } - held_value += unresolved.held_net_value(managed, height, held_input_cost); // `reservation_only` offers no candidates of its own: the - // caller seeded its inputs explicitly. Either way the held - // coins are excluded — a seeded one fails the build. + // caller seeded its inputs explicitly, so held coins could + // never fund it and count for nothing in a shortfall. Either + // way the held coins are excluded — a seeded one fails the + // build. + if !reservation_only { + held_value += unresolved.held_net_value(managed, height, held_input_cost); + } builder = if reservation_only { builder.add_funding_reservation_only(managed, account) } else { @@ -802,9 +825,11 @@ impl CoreWallet { outpoint: Some(outpoint), } } - error => { - awaiting_network(map_builder_error(error, funding_context), held_value) - } + error => awaiting_network( + map_builder_error(error, funding_context), + held_value, + requested, + ), })?; // Release across every contributing account on the error paths @@ -1124,7 +1149,7 @@ mod tests { awaiting_network, awaiting_network_or_build_error, held_input_cost, modeled_output_script, MAX_STANDARD_TX_INPUTS, }; - use crate::wallet::core::{CoreWallet, WalletGeneration}; + use crate::wallet::core::{CoreWallet, UnresolvedSends, WalletGeneration}; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; use crate::{PlatformWalletError, SEND_FUNDING_SOURCES}; use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionError; @@ -2272,7 +2297,7 @@ mod tests { held, outpoint: None, .. - }) if held == change.value() - held_input_cost().expect("input cost") + }) if held == change.value() - held_input_cost() ), "got {refused:?}" ); @@ -2553,9 +2578,159 @@ mod tests { ); } + /// A wallet whose only coin is the change of an unresolved send: selection + /// has nothing left to offer (no size to report), and the payment is + /// measured against what it requested — waiting on the network, not a + /// shortfall. + #[tokio::test] + async fn should_report_awaiting_network_when_the_only_coin_is_held() { + let (manager, wallet_id, generation, signer) = + funded_wallet_manager(StandardAccountType::BIP44Account).await; + generation.set_holds_unresolved_sends(true); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let core: CoreWallet = CoreWallet::new( + sdk, + Arc::clone(&manager), + wallet_id, + Arc::new(AlwaysOkBroadcaster), + generation, + ); + let paid = DashAddress::dummy(Network::Testnet, 70); + unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 1_000_000).await; + + let refused = core + .finalize_transaction( + TransactionBuilder::new().add_output(&paid, 500_000), + &SEND_FUNDING_SOURCES, + 0, + &signer, + ) + .await; + assert!( + matches!( + refused, + Err(PlatformWalletError::CoreFundsAwaitingNetwork { + required: Some(required), + outpoint: None, + .. + }) if required > 500_000 + ), + "got {refused:?}" + ); + } + + /// A `reservation_only` build spends only the inputs the caller seeded, so + /// held coins could never fund it: its shortfall stays insufficient funds + /// however much the account holds back. + #[tokio::test] + async fn should_not_count_held_coins_in_a_reservation_only_shortfall() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 71); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + let spent: Vec = send + .input + .iter() + .map(|input| input.previous_output) + .collect(); + let untouched = { + let manager = manager.read().await; + let info = manager.get_wallet_info(&wallet_id).expect("wallet"); + info.core_wallet + .accounts + .all_funding_accounts() + .into_iter() + .flat_map(|managed| managed.utxos.values().cloned()) + .find(|utxo| utxo.value() == 700_000 && !spent.contains(&utxo.outpoint)) + .expect("the untouched coin") + }; + + let refused = core + .finalize_transaction_with_options( + TransactionBuilder::new() + .add_output(&paid, 900_000) + .add_inputs([untouched]), + &SEND_FUNDING_SOURCES, + 0, + &signer, + true, + ) + .await; + assert!( + matches!( + refused, + Err(PlatformWalletError::CorePooledInsufficientFunds { .. }) + ), + "got {refused:?}" + ); + } + + /// Acceptance is not persisted, but the startup re-dispatch echoes a child + /// whose parent stays ambiguous. A node that accepted the child has its + /// parent, so the parent counts as accepted too and the child's coins are + /// free. + #[tokio::test] + async fn should_count_a_parent_accepted_when_its_child_is() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 72); + let parent = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + record_acceptance(&manager, &wallet_id, parent.txid(), true).await; + let child = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 1_000_000).await; + assert!(child + .input + .iter() + .any(|input| input.previous_output.txid == parent.txid())); + + { + let mut manager = manager.write().await; + let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet"); + // A restart: no acceptance facts survive. + let fresh = WalletGeneration::new(); + fresh.set_holds_unresolved_sends(true); + info.generation = Arc::new(fresh); + } + record_acceptance(&manager, &wallet_id, child.txid(), true).await; + + let manager = manager.read().await; + let info = manager.get_wallet_info(&wallet_id).expect("wallet"); + let unresolved = UnresolvedSends::of(info); + assert!( + !unresolved.holds(&change_of(&child, &paid)), + "the child's change is free" + ); + assert!( + !unresolved.holds(&OutPoint { + txid: parent.txid(), + vout: 0, + }), + "the parent counts as accepted" + ); + } + + /// With probing off nothing reads acceptance facts, so none are kept. + #[tokio::test] + async fn should_keep_no_acceptance_facts_while_probing_is_off() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 73); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + { + let manager = manager.read().await; + let info = manager.get_wallet_info(&wallet_id).expect("wallet"); + info.generation.set_holds_unresolved_sends(false); + } + record_acceptance(&manager, &wallet_id, send.txid(), true).await; + + let manager = manager.read().await; + let info = manager.get_wallet_info(&wallet_id).expect("wallet"); + assert!(info + .generation + .acceptance_among(&HashSet::from([send.txid()])) + .is_empty()); + } + /// Only a shortfall the held coins would actually cover is "waiting on the - /// network". One they would not cover, or one whose size selection did not - /// report, stays insufficient funds. + /// network". One they would not cover stays insufficient funds. When + /// selection did not report the shortfall's size — every candidate held — + /// it is measured against what the build requested. #[test] fn should_report_awaiting_network_only_when_the_held_coins_cover_the_shortfall() { let shortfall = |available, required| PlatformWalletError::CorePooledInsufficientFunds { @@ -2564,19 +2739,27 @@ mod tests { required, }; assert!(matches!( - awaiting_network(shortfall(Some(400), Some(1_000)), 600), + awaiting_network(shortfall(Some(400), Some(1_000)), 600, 0), PlatformWalletError::CoreFundsAwaitingNetwork { .. } )); assert!(matches!( - awaiting_network(shortfall(Some(400), Some(1_000)), 599), + awaiting_network(shortfall(Some(400), Some(1_000)), 599, 0), PlatformWalletError::CorePooledInsufficientFunds { .. } )); assert!(matches!( - awaiting_network(shortfall(Some(0), None), 1_000_000), + awaiting_network(shortfall(Some(400), Some(1_000)), 0, 0), PlatformWalletError::CorePooledInsufficientFunds { .. } )); + // Nothing left to select (every coin held): sized by the request. assert!(matches!( - awaiting_network(shortfall(Some(400), Some(1_000)), 0), + awaiting_network(shortfall(Some(0), None), 1_000_000, 900_000), + PlatformWalletError::CoreFundsAwaitingNetwork { + required: Some(900_000), + .. + } + )); + assert!(matches!( + awaiting_network(shortfall(Some(0), None), 1_000_000, 10_000_000), PlatformWalletError::CorePooledInsufficientFunds { .. } )); } @@ -2593,11 +2776,11 @@ mod tests { }) }; assert!(matches!( - awaiting_network_or_build_error(shortfall(), &SEND_FUNDING_SOURCES, 600), + awaiting_network_or_build_error(shortfall(), &SEND_FUNDING_SOURCES, 600, 0), PlatformWalletError::CoreFundsAwaitingNetwork { .. } )); assert!(matches!( - awaiting_network_or_build_error(shortfall(), &SEND_FUNDING_SOURCES, 10), + awaiting_network_or_build_error(shortfall(), &SEND_FUNDING_SOURCES, 10, 0), PlatformWalletError::TransactionBuild(_) )); assert!(matches!( @@ -2605,6 +2788,16 @@ mod tests { BuilderError::CoinSelection(SelectionError::NoUtxosAvailable), &SEND_FUNDING_SOURCES, 1_000_000, + 900_000, + ), + PlatformWalletError::CoreFundsAwaitingNetwork { .. } + )); + assert!(matches!( + awaiting_network_or_build_error( + BuilderError::CoinSelection(SelectionError::NoUtxosAvailable), + &SEND_FUNDING_SOURCES, + 1_000_000, + 10_000_000, ), PlatformWalletError::TransactionBuild(_) )); diff --git a/packages/rs-platform-wallet/src/wallet/core/unresolved.rs b/packages/rs-platform-wallet/src/wallet/core/unresolved.rs index bc98324fc34..4b2cb5551ec 100644 --- a/packages/rs-platform-wallet/src/wallet/core/unresolved.rs +++ b/packages/rs-platform-wallet/src/wallet/core/unresolved.rs @@ -47,20 +47,47 @@ impl UnresolvedSends { } let unsettled = unsettled_transactions(info); let unsettled_txids: HashSet = unsettled.keys().copied().collect(); - let accepted = info.generation.accepted_among(&unsettled_txids); - // Each unsettled transaction under every unsettled parent it spends, - // so the held set is one walk down from the held roots. + // Each unsettled transaction's unsettled parents, and the reverse. + let mut parents: HashMap> = HashMap::new(); let mut children: HashMap> = HashMap::new(); for (txid, (_, transaction)) in &unsettled { - let parents: HashSet = transaction + let spent: HashSet = transaction .input .iter() .map(|input| input.previous_output.txid) .filter(|parent| unsettled.contains_key(parent)) .collect(); - for parent in parents { + for parent in spent { children.entry(parent).or_default().push(*txid); + parents.entry(*txid).or_default().push(parent); + } + } + + // A node that accepted a transaction has its parents: acceptance of a + // child — an echo of the startup re-dispatch, say — is acceptance of + // every unsettled ancestor, unless that ancestor's own record is newer + // (found dead since). + let facts = info.generation.acceptance_among(&unsettled_txids); + let mut accepted: HashSet = HashSet::new(); + let mut up: VecDeque<(Txid, u64)> = facts + .iter() + .filter(|(_, (was_accepted, _))| *was_accepted) + .map(|(txid, (_, seq))| (*txid, *seq)) + .collect(); + while let Some((txid, seq)) = up.pop_front() { + if !accepted.insert(txid) { + continue; + } + for parent in parents.get(&txid).into_iter().flatten() { + let overridden = facts + .get(parent) + .is_some_and(|(parent_accepted, parent_seq)| { + !parent_accepted && *parent_seq > seq + }); + if !overridden { + up.push_back((*parent, seq)); + } } } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 02eeca46ce2..6fa87646faa 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -12,7 +12,9 @@ use key_wallet_manager::WalletManager; use super::*; use crate::broadcaster::TransactionBroadcaster; use crate::error::PlatformWalletError; -use crate::wallet::core::{awaiting_network_or_build_error, held_input_cost, UnresolvedSends}; +use crate::wallet::core::{ + awaiting_network_or_build_error, held_input_cost, requested_amount, UnresolvedSends, +}; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; use crate::SEND_FUNDING_SOURCES; @@ -1258,7 +1260,8 @@ impl DashPayView<'_, B> { // Coins of our own sends the network has not been seen to accept // stay out of selection (see `UnresolvedSends`). let unresolved = UnresolvedSends::of(info); - let held_input_cost = held_input_cost()?; + let held_input_cost = held_input_cost(); + let requested = requested_amount(builder.outputs()); let mut held_value = 0u64; // Derivation paths for every offered UTXO, since the signer closure @@ -1352,6 +1355,7 @@ impl DashPayView<'_, B> { e, &SEND_FUNDING_SOURCES, held_value, + requested, )); } }; From b59f0b8778ec1c80b9f2e20f9184671e84a9cc56 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 15:14:56 +0300 Subject: [PATCH 052/169] fix(platform-wallet): deterministic, synchronous acceptance and one shortfall estimator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-ups to the unresolved-send hold: - Acceptance of a child reaches its unsettled ancestors with the highest seq that reached each one, walking an ancestor again when a higher seq arrives — the held set no longer depends on HashMap order. - Every verdict the resolver publishes, a dead one re-published at the start of a pass included, is recorded on the wallet — synchronously and before the host hears it, through the in-broadcast fences the resolver now shares with the manager (FenceRegistry), so a host re-reading spendable or max on the callback sees the figure the verdict implies. - The account UTXO listing flags held coins (AccountUtxoSnapshot::is_held, AccountUtxoEntryFFI.is_held, Swift AccountUtxo.isHeld), and a refused seeded input is named in the CoreFundsAwaitingNetwork message. - One estimator: the shortfall basis prices inputs and the outputs-only transaction at the build's own fee rate with the helpers pooled_max_sendable uses; a drain or a special-payload build is never classified as awaiting the network. `shortfall()` is the one reader of a BuilderError's available/required. - ERROR_CODE_REGISTRY frontier moved to 60; test imports at the top. Builds against rust-dashcore feat/funding-exclude-outpoints (needs its TransactionBuilder::fee_rate/selection_strategy/has_special_payload) until the pin moves. Co-Authored-By: Claude Opus 5.5 --- .../ERROR_CODE_REGISTRY.md | 5 +- .../src/core_wallet_types.rs | 4 + .../src/manager_diagnostics.rs | 1 + packages/rs-platform-wallet/src/error.rs | 11 +- .../src/manager/accessors.rs | 66 +++-- .../rs-platform-wallet/src/manager/mod.rs | 10 +- .../src/wallet/asset_lock/build.rs | 3 +- .../src/wallet/core/broadcast_resolver.rs | 198 ++++++++++--- .../src/wallet/core/generation.rs | 39 +-- .../rs-platform-wallet/src/wallet/core/mod.rs | 2 +- .../src/wallet/core/transaction.rs | 266 ++++++++++++------ .../src/wallet/core/unresolved.rs | 94 +++++-- .../src/wallet/identity/network/payments.rs | 23 +- .../PlatformWalletManagerDiagnostics.swift | 8 +- 14 files changed, 519 insertions(+), 211 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md index 330ee83d699..73edb813f84 100644 --- a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md +++ b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md @@ -114,13 +114,14 @@ These are shipped ABI. Do not renumber. | 98 | `NotFound` | Sentinel — `Option` returned as an error | | 99 | `ErrorUnknown` | Sentinel — unmapped/flattened errors | -**Next allocatable integer: 59** — 27–58 are all claimed (27, 29, 31, 34–42 +**Next allocatable integer: 60** — 27–59 are all claimed (27, 29, 31, 34–42 and 46 merged; 43–45 proposed by active #4313 at head `0302b188ab`; 47 and 48 proposed by active #4356 (47 renumbered from 42, 48 from 43 — see their rows below); 49–54 proposed by active #4586 (the persister operation × kind block); 55–57 proposed by #4715 for pending identity-funded shield debits and durable recovery errors; 58 proposed by #4799 for an unavailable -identity balance response; 28, 30, +identity balance response; 59 proposed by the unresolved-broadcast resolver +PR for funds held until an earlier send is accepted; 28, 30, 32 and 33 reserved). **28, 30, 32 and 33 are RESERVED, not free**: 28 and 30 were vacated when the reservation trio moved to 34–36; 32 and 33 lapsed when their in-repo owners diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs index 68e3dd8ff8a..b89138c46e4 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs @@ -957,6 +957,10 @@ pub struct AccountUtxoEntryFFI { pub script_pubkey_len: usize, pub height: u32, pub is_locked: bool, + /// An output of a send the network has not been seen to accept: a build + /// seeded with it fails with `ErrorCoreFundsAwaitingNetwork`. Coin control + /// and drains should leave it out. + pub is_held: bool, } /// One transaction row in the per-account paginated drill-down. diff --git a/packages/rs-platform-wallet-ffi/src/manager_diagnostics.rs b/packages/rs-platform-wallet-ffi/src/manager_diagnostics.rs index 5630ad287be..f6d3e20f55e 100644 --- a/packages/rs-platform-wallet-ffi/src/manager_diagnostics.rs +++ b/packages/rs-platform-wallet-ffi/src/manager_diagnostics.rs @@ -580,6 +580,7 @@ pub unsafe extern "C" fn platform_wallet_account_utxos( script_pubkey_len: script_len, height: s.height, is_locked: s.is_locked, + is_held: s.is_held, } }) .collect(); diff --git a/packages/rs-platform-wallet/src/error.rs b/packages/rs-platform-wallet/src/error.rs index d6f1395a706..1d2e5302291 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -406,7 +406,8 @@ pub enum PlatformWalletError { /// `reservation_only` builds); `None` when selection came up short. #[error( "Core funds held until an earlier send is accepted by the network: \ - available {available:?}, held {held}, required {required:?}" + available {available:?}, held {held}, required {required:?}{}", + refused_input(outpoint) )] CoreFundsAwaitingNetwork { available: Option, @@ -1807,3 +1808,11 @@ mod asset_lock_already_consumed_tests { )); } } + +/// The refused input of a `CoreFundsAwaitingNetwork`, for its message: a host +/// doing coin control learns which coin to leave out. +fn refused_input(outpoint: &Option) -> String { + outpoint + .map(|outpoint| format!(", refused input {}:{}", outpoint.txid, outpoint.vout)) + .unwrap_or_default() +} diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index fee4b591545..d4a2678ec3a 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -20,7 +20,8 @@ use crate::manager::platform_address_sync::PlatformAddressSyncManager; #[cfg(feature = "shielded")] use crate::manager::shielded_sync::ShieldedSyncManager; use crate::spv::SpvRuntime; -use crate::wallet::platform_wallet::WalletId; +use crate::wallet::core::UnresolvedSends; +use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; use crate::wallet::PlatformWallet; /// Result of [`PlatformWalletManager::provider_masternode_txs_blocking`]: @@ -214,6 +215,10 @@ pub struct AccountUtxoSnapshot { pub script_pubkey: Vec, pub height: u32, pub is_locked: bool, + /// An output of a send the network has not been seen to accept (see + /// `UnresolvedSends`): no payment spends it until that send resolves, and + /// one seeded with it is refused. Always `false` while probing is off. + pub is_held: bool, } /// One row of a wallet's UTXO inventory page — see [`wallet_utxos_page`]. @@ -1135,30 +1140,7 @@ impl PlatformWalletManager

{ let Some(info) = wm.get_wallet_info(wallet_id) else { return Vec::new(); }; - let accounts = info.core_wallet.accounts.all_accounts(); - let Some(account) = accounts - .iter() - .find(|a| &a.managed_account_type().to_account_type() == target) - else { - return Vec::new(); - }; - // UTXOs only exist on the funds variant. Keys-only accounts - // (identity / asset-lock / provider) never carry UTXOs by - // construction, so an empty list is the correct snapshot. - let Some(funds) = account.as_funds() else { - return Vec::new(); - }; - funds - .utxos - .values() - .map(|utxo: &Utxo| AccountUtxoSnapshot { - outpoint: utxo.outpoint, - value_duffs: utxo.txout.value, - script_pubkey: utxo.txout.script_pubkey.as_bytes().to_vec(), - height: utxo.height, - is_locked: utxo.is_locked, - }) - .collect() + account_utxo_snapshots(info, target) } // ----------------------------------------------------------------- @@ -1509,6 +1491,40 @@ fn tx_record_snapshot(rec: &TransactionRecord) -> AccountTransactionSnapshot { } } +/// Snapshot of every UTXO row on the wallet's `target` account, each flagged +/// held when it is an output of an unresolved send. +pub(crate) fn account_utxo_snapshots( + info: &PlatformWalletInfo, + target: &AccountType, +) -> Vec { + let accounts = info.core_wallet.accounts.all_accounts(); + let Some(account) = accounts + .iter() + .find(|a| &a.managed_account_type().to_account_type() == target) + else { + return Vec::new(); + }; + // UTXOs only exist on the funds variant. Keys-only accounts + // (identity / asset-lock / provider) never carry UTXOs by + // construction, so an empty list is the correct snapshot. + let Some(funds) = account.as_funds() else { + return Vec::new(); + }; + let held = UnresolvedSends::of(info); + funds + .utxos + .values() + .map(|utxo: &Utxo| AccountUtxoSnapshot { + outpoint: utxo.outpoint, + value_duffs: utxo.txout.value, + script_pubkey: utxo.txout.script_pubkey.as_bytes().to_vec(), + height: utxo.height, + is_locked: utxo.is_locked, + is_held: held.holds(&utxo.outpoint), + }) + .collect() +} + #[cfg(test)] mod spv_rescan_tests { use std::sync::Arc; diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 01b941cb503..053ddf8d95f 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -35,7 +35,7 @@ use crate::manager::platform_address_sync::PlatformAddressSyncManager; use crate::manager::shielded_sync::ShieldedSyncManager; use crate::spv::SpvRuntime; use crate::wallet::asset_lock::LockNotifyHandler; -use crate::wallet::core::broadcast_resolver::BroadcastResolver; +use crate::wallet::core::broadcast_resolver::{BroadcastResolver, FenceRegistry}; use crate::wallet::core::{BalanceUpdateHandler, InBroadcastFences, SpendObservationHandler}; use crate::wallet::identity::network::DashPayPaymentHandler; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; @@ -472,8 +472,8 @@ pub struct PlatformWalletManager { /// /// A `std::sync::Mutex`: touched only at wallet registration and load, for /// one map lookup, and never held across an await. - pub(super) in_broadcast_fences: - std::sync::Mutex>>, + /// Shared with the broadcast resolver ([`FenceRegistry`]). + pub(super) in_broadcast_fences: FenceRegistry, } impl PlatformWalletManager

{ @@ -553,9 +553,11 @@ impl PlatformWalletManager

{ // was unknown (read-only; see its module docs). It publishes through // the event manager it is registered with, so it gets a weak handle // to it once that exists. + let in_broadcast_fences: FenceRegistry = Arc::default(); let broadcast_resolver = Arc::new(BroadcastResolver::new( Arc::new(DapiAcceptanceProbe::new(Arc::clone(&sdk))), Arc::clone(&wallet_manager), + Arc::clone(&in_broadcast_fences), )); let event_manager = Arc::new(PlatformEventManager::new(vec![ app_handler, @@ -628,7 +630,7 @@ impl PlatformWalletManager

{ event_adapter_join: tokio::sync::Mutex::new(Some(event_adapter_join)), registry, sync_fault, - in_broadcast_fences: std::sync::Mutex::new(std::collections::BTreeMap::new()), + in_broadcast_fences, } } diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs index fa17badf874..e96e1e44576 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs @@ -1362,6 +1362,7 @@ fn map_builder_error(e: AssetLockError, requested: u64) -> PlatformWalletError { #[cfg(test)] mod tests { + use std::collections::HashSet; use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -2117,7 +2118,7 @@ mod tests { let txid = out_point.txid; assert!(info .generation - .accepted_among(&std::collections::HashSet::from([txid])) + .accepted_among(&HashSet::from([txid])) .contains(&txid)); } diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index cc8a04999e6..5ba92ce20a3 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -63,7 +63,7 @@ //! are merged into the next. Every host callback comes from that task, never //! from a caller that may hold its own locks. -use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; use std::mem; use std::panic::{catch_unwind, AssertUnwindSafe}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -87,6 +87,7 @@ use tokio::time::{timeout_at, Instant}; use crate::broadcast_probe::{AcceptanceProbe, ProbeReport, ProbeVerdict, MINED_QUORUM}; use crate::events::{PlatformEventHandler, PlatformEventManager}; use crate::wallet::core::generation::next_acceptance_seq; +use crate::wallet::core::InBroadcastFences; use crate::wallet::platform_wallet::PlatformWalletInfo; /// Blocks from the start of a root's window (see [`ProbeSchedule`]) during @@ -955,9 +956,14 @@ pub(crate) trait WalletSource: Send + Sync { /// accepted send may fund the next payment and those of a dead one may /// not /// ([`WalletGeneration::record_acceptance`](crate::wallet::core::WalletGeneration::record_acceptance)). - async fn record_acceptance(&self, wallet_id: WalletId, verdicts: Vec<(Txid, bool)>, seq: u64); + fn record_acceptance(&self, wallet_id: WalletId, verdicts: Vec<(Txid, bool)>, seq: u64); } +/// Every registered wallet's in-broadcast fences, by id — the manager's map, +/// shared with the resolver so it records a verdict's acceptance fact without +/// the wallet manager's lock. +pub(crate) type FenceRegistry = Arc>>>; + /// Whether any account of the wallet records `txid` — a map lookup per /// account, not a walk over the records. fn holds(info: &PlatformWalletInfo, txid: &Txid) -> bool { @@ -968,17 +974,25 @@ fn holds(info: &PlatformWalletInfo, txid: &Txid) -> bool { .any(|account| account.transactions().contains_key(txid)) } -struct ManagerSource(Arc>>); +struct ManagerSource( + Arc>>, + FenceRegistry, +); #[async_trait] impl WalletSource for ManagerSource { - async fn record_acceptance(&self, wallet_id: WalletId, verdicts: Vec<(Txid, bool)>, seq: u64) { - let manager = self.0.read().await; - let Some(info) = manager.get_wallet_info(&wallet_id) else { + fn record_acceptance(&self, wallet_id: WalletId, verdicts: Vec<(Txid, bool)>, seq: u64) { + let fences = self + .1 + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .get(&wallet_id) + .cloned(); + let Some(fences) = fences else { return; }; for (txid, accepted) in verdicts { - info.generation.record_acceptance(txid, accepted, seq); + fences.record_acceptance(txid, accepted, seq); } } @@ -1327,7 +1341,7 @@ impl Actor { } } let events = self.state.forget_wallet(&wallet_id, "rewound"); - deliver(&self.sink, events); + self.publish(events); } let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; @@ -1404,7 +1418,7 @@ impl Actor { } } let events = self.state.settle(wallet_id, &txids); - deliver(&self.sink, events); + self.publish(events); if next_probe { self.probe_next(wallet_id); } @@ -1455,7 +1469,7 @@ impl Actor { } if !enabled { let events = self.state.forget_all(); - deliver(&self.sink, events); + self.publish(events); } } Command::WalletAdded(wallet_id) => { @@ -1475,7 +1489,7 @@ impl Actor { run.abort.abort(); } let events = self.state.forget_wallet(wallet_id, trigger); - deliver(&self.sink, events); + self.publish(events); } /// Start the wallet's pending pass unless one is already running. @@ -1565,7 +1579,7 @@ impl Actor { // events or an Uncertain wake it. entry.wait_until = Some(u32::MAX); let events = self.state.forget_wallet(&wallet_id, "gone"); - deliver(&self.sink, events); + self.publish(events); self.end_run(wallet_id); } Some(None) => self.end_run(wallet_id), @@ -1609,7 +1623,7 @@ impl Actor { }); // Clears and re-published dead verdicts reach the host // before the pass goes out to the network. - deliver(&self.sink, start.events); + self.publish(start.events); self.probe_next(wallet_id); } } @@ -1673,35 +1687,39 @@ impl Actor { &root, &report, ); - self.record_acceptance(wallet_id, &events); - deliver(&self.sink, events); + self.publish(events); self.probe_next(wallet_id); } } } - /// Hand the sends these events find accepted (or mined) or dead to the - /// wallet, so the coins of an accepted one are offered to payments again - /// and those of a dead one are held again. Off the actor: the record - /// takes the manager lock. The sequence number is taken here, in the - /// actor's order, so a later verdict wins however the records land. - fn record_acceptance(&self, wallet_id: WalletId, events: &[Outgoing]) { - let verdicts: Vec<(Txid, bool)> = events - .iter() - .filter_map(|item| match &item.event { + /// Hand `events` to the host — after handing the sends they find accepted + /// (or mined) or dead to their wallets, so the coins of an accepted one + /// are offered to payments again and those of a dead one are held again. + /// Recorded first and synchronously: a host re-reading spendable or max + /// on the verdict sees the figure the verdict implies. Every published + /// verdict goes through here, a re-published dead one included. The + /// sequence number is taken here, in the actor's order, so a later + /// verdict wins. + fn publish(&self, events: Vec) { + let mut verdicts: BTreeMap> = BTreeMap::new(); + for item in &events { + let verdict = match &item.event { ResolverEvent::Verdict(txid, ProbeVerdict::Accepted | ProbeVerdict::Mined) => { - Some((*txid, true)) + (*txid, true) } - ResolverEvent::Verdict(txid, ProbeVerdict::Dead { .. }) => Some((*txid, false)), - _ => None, - }) - .collect(); - if verdicts.is_empty() { - return; + ResolverEvent::Verdict(txid, ProbeVerdict::Dead { .. }) => (*txid, false), + _ => continue, + }; + verdicts.entry(item.wallet_id).or_default().push(verdict); } - let seq = next_acceptance_seq(); - let source = Arc::clone(&self.source); - tokio::spawn(async move { source.record_acceptance(wallet_id, verdicts, seq).await }); + if !verdicts.is_empty() { + let seq = next_acceptance_seq(); + for (wallet_id, verdicts) in verdicts { + self.source.record_acceptance(wallet_id, verdicts, seq); + } + } + deliver(&self.sink, events); } /// Probe the run's next due root, or end the run. @@ -1798,11 +1816,12 @@ impl BroadcastResolver { pub(crate) fn new( probe: Arc, wallet_manager: Arc>>, + fences: FenceRegistry, ) -> Self { let sink = Arc::new(EventSink::default()); let actor = Actor::new( probe, - Arc::new(ManagerSource(wallet_manager)), + Arc::new(ManagerSource(wallet_manager, fences)), Arc::clone(&sink) as Arc, ); let (commands, receiver) = mpsc::unbounded_channel(); @@ -2793,12 +2812,7 @@ mod tests { #[async_trait] impl WalletSource for FakeSource { - async fn record_acceptance( - &self, - wallet_id: WalletId, - verdicts: Vec<(Txid, bool)>, - seq: u64, - ) { + fn record_acceptance(&self, wallet_id: WalletId, verdicts: Vec<(Txid, bool)>, seq: u64) { let mut acceptance = self.acceptance.lock().expect("acceptance"); acceptance.extend( verdicts @@ -2846,10 +2860,28 @@ mod tests { } #[derive(Default)] - struct CollectSink(StdMutex>); + struct CollectSink( + StdMutex>, + /// The rig's source, to check each verdict was recorded before it + /// reached the host. + std::sync::OnceLock>, + /// Verdicts delivered before their wallet recorded them. + StdMutex>, + ); impl VerdictSink for CollectSink { fn deliver(&self, item: &Outgoing) { + if let (ResolverEvent::Verdict(txid, verdict), Some(source)) = + (&item.event, self.1.get()) + { + let records = !matches!(verdict, ProbeVerdict::Unresolved { .. }); + let recorded = source.acceptance.lock().expect("acceptance").iter().any( + |(wallet_id, recorded, _, _)| *wallet_id == item.wallet_id && recorded == txid, + ); + if records && !recorded { + self.2.lock().expect("unrecorded").push(*txid); + } + } self.0.lock().expect("sink").push(item.event.clone()); } } @@ -2867,6 +2899,7 @@ mod tests { fn with_sink(probe: Arc, sink: Arc) -> Self { let source = Arc::new(FakeSource::default()); + let _ = sink.1.set(Arc::clone(&source)); let actor = Actor::new( probe, Arc::clone(&source) as Arc, @@ -2982,10 +3015,6 @@ mod tests { for n in 1..=3 { rig.send(Command::Uncertain(txid(n))).await; } - // The record runs off the actor. - for _ in 0..20 { - tokio::task::yield_now().await; - } let recorded: Vec<(WalletId, Txid, bool)> = rig .source @@ -3009,6 +3038,83 @@ mod tests { ); } + /// A verdict is recorded on the wallet before it reaches the host, so a + /// host re-reading spendable or max on the callback sees the new figure. + #[tokio::test] + async fn should_record_a_verdict_before_delivering_it() { + let probe = Arc::new(ScriptedProbe::new(&[ + (txid(1), ProbeVerdict::Accepted), + (txid(2), dead()), + ])); + let mut rig = enabled(probe).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); + + rig.send(Command::Uncertain(txid(1))).await; + rig.send(Command::Uncertain(txid(2))).await; + + let delivered = rig.sent(); + assert!( + delivered.contains(&ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)), + "{delivered:?}" + ); + assert!( + delivered.contains(&ResolverEvent::Verdict(txid(2), dead())), + "{delivered:?}" + ); + let unrecorded = rig.sink.2.lock().expect("unrecorded").clone(); + assert!( + unrecorded.is_empty(), + "delivered before recorded: {unrecorded:?}" + ); + } + + /// A dead verdict re-published to a send built on a dead root — at the + /// start of a pass, without a probe — is recorded on the wallet too. + #[tokio::test] + async fn should_record_a_dead_verdict_republished_to_a_later_child() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), dead())])); + let mut rig = enabled(probe).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.send(Command::Uncertain(txid(1))).await; + rig.source.acceptance.lock().expect("acceptance").clear(); + + // The new send's record wakes the idle wallet, as the event adapter + // reports it; the next block starts a pass. + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])], + ); + rig.send(Command::Seen { + wallet_id: wallet(), + touched: true, + }) + .await; + rig.follow(wallet(), 100).await; + let delivered = rig.sent(); + assert!( + delivered.contains(&ResolverEvent::Verdict(txid(2), dead())), + "{delivered:?}" + ); + + let recorded: Vec<(Txid, bool)> = rig + .source + .acceptance + .lock() + .expect("acceptance") + .iter() + .map(|(_, txid, accepted, _)| (*txid, *accepted)) + .collect(); + assert!(recorded.contains(&(txid(2), false)), "{recorded:?}"); + let unrecorded = rig.sink.2.lock().expect("unrecorded").clone(); + assert!( + unrecorded.is_empty(), + "delivered before recorded: {unrecorded:?}" + ); + } + /// An `Uncertain` result carries no wallet id: every wallet hears it, but /// only the one holding the transaction walks its records. #[tokio::test] diff --git a/packages/rs-platform-wallet/src/wallet/core/generation.rs b/packages/rs-platform-wallet/src/wallet/core/generation.rs index be7dfe642f1..021f77192e4 100644 --- a/packages/rs-platform-wallet/src/wallet/core/generation.rs +++ b/packages/rs-platform-wallet/src/wallet/core/generation.rs @@ -320,6 +320,28 @@ impl InBroadcastFences { self.fences.lock().unwrap_or_else(PoisonError::into_inner) } + /// See [`WalletGeneration::record_acceptance`]. On the fences, so the + /// broadcast resolver can record a verdict without the wallet manager's + /// lock — synchronously, before the verdict reaches the host. + pub(crate) fn record_acceptance(&self, txid: Txid, accepted: bool, seq: u64) { + let mut acceptance = self.acceptance(); + if !self.hold_unresolved.load(Ordering::SeqCst) { + acceptance.clear(); + return; + } + let newer = acceptance.get(&txid).is_none_or(|known| known.seq < seq); + if newer { + acceptance.insert( + txid, + Acceptance { + seq, + accepted, + observed_at: Instant::now(), + }, + ); + } + } + fn acceptance(&self) -> MutexGuard<'_, HashMap> { self.acceptance .lock() @@ -465,22 +487,7 @@ impl WalletGeneration { /// hold is off: only the hold reads these facts, and only its reads prune /// them. pub(crate) fn record_acceptance(&self, txid: Txid, accepted: bool, seq: u64) { - let mut acceptance = self.in_broadcast.acceptance(); - if !self.holds_unresolved_sends() { - acceptance.clear(); - return; - } - let newer = acceptance.get(&txid).is_none_or(|known| known.seq < seq); - if newer { - acceptance.insert( - txid, - Acceptance { - seq, - accepted, - observed_at: Instant::now(), - }, - ); - } + self.in_broadcast.record_acceptance(txid, accepted, seq); } /// A broadcast of `txid` returned accepted: a peer echoed it back. diff --git a/packages/rs-platform-wallet/src/wallet/core/mod.rs b/packages/rs-platform-wallet/src/wallet/core/mod.rs index 9774c9dba3e..ec5da49cef3 100644 --- a/packages/rs-platform-wallet/src/wallet/core/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/core/mod.rs @@ -17,7 +17,7 @@ pub use generation::WalletGeneration; pub(crate) use generation::{InBroadcastFences, InBroadcastPin}; pub use spend_observer::SpendObservationHandler; pub(crate) use transaction::{ - awaiting_network_or_build_error, held_input_cost, requested_amount, resolve_source_accounts, + awaiting_network_or_build_error, resolve_source_accounts, shortfall_basis, }; pub use transaction::{SignedCoreTransaction, ASSET_LOCK_FUNDING_SOURCES, SEND_FUNDING_SOURCES}; pub(crate) use unresolved::UnresolvedSends; diff --git a/packages/rs-platform-wallet/src/wallet/core/transaction.rs b/packages/rs-platform-wallet/src/wallet/core/transaction.rs index 56b3bb955f7..715f163b182 100644 --- a/packages/rs-platform-wallet/src/wallet/core/transaction.rs +++ b/packages/rs-platform-wallet/src/wallet/core/transaction.rs @@ -8,11 +8,11 @@ use std::collections::{HashMap, HashSet}; use std::sync::Arc; -use dashcore::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction, TxOut}; +use dashcore::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction}; use key_wallet::account::AccountType; use key_wallet::managed_account::managed_account_collection::ManagedAccountCollection; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; -use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionError; +use key_wallet::wallet::managed_wallet_info::coin_selection::{SelectionError, SelectionStrategy}; use key_wallet::wallet::managed_wallet_info::fee::{estimate_tx_size, FeeRate}; use key_wallet::wallet::managed_wallet_info::transaction_builder::{ BuilderError, TransactionBuilder, TransactionSigner, @@ -42,8 +42,11 @@ enum FundingContext<'a> { Pooled(&'a [AccountTypePreference]), } -fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> PlatformWalletError { - let funds = match error { +/// The `(available, required)` of a build that failed for want of funds; +/// `None` for any other failure. `required` is `None` when selection had +/// nothing to choose from and so never sized the shortfall. +fn shortfall(error: &BuilderError) -> Option<(Option, Option)> { + match error { BuilderError::InsufficientFunds { available, required, @@ -51,11 +54,14 @@ fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> Platfo | BuilderError::CoinSelection(SelectionError::InsufficientFunds { available, required, - }) => Some((Some(available), Some(required))), + }) => Some((Some(*available), Some(*required))), BuilderError::CoinSelection(SelectionError::NoUtxosAvailable) => Some((Some(0), None)), _ => None, - }; - if let Some((available, required)) = funds { + } +} + +fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> PlatformWalletError { + if let Some((available, required)) = shortfall(&error) { return match context { FundingContext::Single { preference, index } => { PlatformWalletError::CoreInsufficientFunds { @@ -75,19 +81,82 @@ fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> Platfo PlatformWalletError::TransactionBuild(error.to_string()) } -/// A shortfall that the held coins of an unresolved send (see -/// [`UnresolvedSends`]) would have covered becomes -/// [`PlatformWalletError::CoreFundsAwaitingNetwork`], so the host can say the -/// money is waiting on the network rather than missing. `held` is what those -/// coins would add, net of the fee their own inputs cost -/// ([`UnresolvedSends::held_net_value`]). When selection did not report the -/// size of the shortfall — every candidate was held, say — it is measured -/// against `requested` ([`requested_amount`]). A genuine shortfall — held -/// coins or not, the funds are not there — stays insufficient funds. +/// What telling a shortfall the held coins caused needs from a build: the fee +/// one input adds at the build's own rate (what spending a held coin costs) +/// and what selection must cover before any input — the outputs plus the fee +/// of a transaction carrying them and a change output. +#[derive(Debug, Clone, Copy)] +pub(crate) struct ShortfallBasis { + pub(crate) input_cost: u64, + pub(crate) requested: u64, +} + +/// The [`ShortfallBasis`] of `builder`, or `None` when its shortfall cannot be +/// told apart — a drain (`SelectionStrategy::All`, whose output amount is a +/// placeholder), a special-payload build (whose own outputs and size +/// `outputs()` does not show), or a fee rate that overflows — and stays +/// insufficient funds. Infallible, so a caller may take it at any point. +pub(crate) fn shortfall_basis(builder: &TransactionBuilder) -> Option { + if matches!(builder.selection_strategy(), SelectionStrategy::All) + || builder.has_special_payload() + { + return None; + } + let fee_rate = builder.fee_rate(); + let outputs = builder.outputs(); + let total = outputs + .iter() + .try_fold(0u64, |total, output| total.checked_add(output.value))?; + let base = checked_fee(fee_rate, estimate_tx_size(0, outputs.len() + 1, false)).ok()?; + Some(ShortfallBasis { + input_cost: input_fee(fee_rate).ok()?, + requested: total.checked_add(base)?, + }) +} + +/// The size one more input adds, from key-wallet's estimator by difference — +/// the one model [`CoreWallet::pooled_max_sendable`] and [`shortfall_basis`] +/// both price inputs with. +fn per_input_size() -> usize { + estimate_tx_size(1, 1, false).saturating_sub(estimate_tx_size(0, 1, false)) +} + +/// The fee one more input adds at `fee_rate`. +fn input_fee(fee_rate: FeeRate) -> Result { + checked_fee(fee_rate, per_input_size()) +} + +/// [`PlatformWalletError::CoreFundsAwaitingNetwork`] when the held coins of +/// an unresolved send (see [`UnresolvedSends`]) would cover a shortfall: `held` +/// is what they would add net of their own inputs' fee +/// ([`UnresolvedSends::held_net_value`]); a shortfall selection did not size +/// is measured against the basis' `requested`. `None` — a genuine shortfall, +/// held coins or not — otherwise. +fn awaiting( + available: Option, + required: Option, + held: u64, + basis: Option, +) -> Option { + let basis = basis?; + let required = required.unwrap_or(basis.requested); + let covered = available.unwrap_or(0).saturating_add(held) >= required; + (held > 0 && covered).then_some(PlatformWalletError::CoreFundsAwaitingNetwork { + available, + held, + required: Some(required), + outpoint: None, + }) +} + +/// `error`, or [`PlatformWalletError::CoreFundsAwaitingNetwork`] when it is a +/// shortfall the held coins would have covered (see [`awaiting`]). A genuine +/// shortfall — held coins or not, the funds are not there — stays +/// insufficient funds. pub(crate) fn awaiting_network( error: PlatformWalletError, held: u64, - requested: u64, + basis: Option, ) -> PlatformWalletError { let (available, required) = match &error { PlatformWalletError::CoreInsufficientFunds { @@ -102,29 +171,7 @@ pub(crate) fn awaiting_network( } => (*available, *required), _ => return error, }; - let required = required.unwrap_or(requested); - let covered = available.unwrap_or(0).saturating_add(held) >= required; - if held == 0 || !covered { - return error; - } - PlatformWalletError::CoreFundsAwaitingNetwork { - available, - held, - required: Some(required), - outpoint: None, - } -} - -/// What a build asks of selection: its outputs plus the fee of a transaction -/// carrying them and a change output, before any input — the inputs' share is -/// already netted out of the held value ([`held_input_cost`]). -pub(crate) fn requested_amount(outputs: &[TxOut]) -> u64 { - let total: u64 = outputs.iter().map(|output| output.value).sum(); - total.saturating_add(FeeRate::normal().calculate_fee(estimate_tx_size( - 0, - outputs.len() + 1, - false, - ))) + awaiting(available, required, held, basis).unwrap_or(error) } /// key-wallet's `FeeRate::calculate_fee`, with the multiplication checked. @@ -155,27 +202,12 @@ fn checked_fee(fee_rate: FeeRate, size_bytes: usize) -> Result, ) -> PlatformWalletError { - match awaiting_network( - map_builder_error(error.clone(), FundingContext::Pooled(sources)), - held, - requested, - ) { - awaiting @ PlatformWalletError::CoreFundsAwaitingNetwork { .. } => awaiting, - _ => PlatformWalletError::TransactionBuild(error.to_string()), - } -} - -/// The fee one more input adds at the default rate: what spending a held coin -/// would cost, for [`awaiting_network`]'s coverage test. A host on a higher -/// rate sees that test lean slightly towards "waiting on the network". A fixed -/// rate and a one-input size, so it cannot overflow. -pub(crate) fn held_input_cost() -> u64 { - let per_input = estimate_tx_size(1, 1, false).saturating_sub(estimate_tx_size(0, 1, false)); - FeeRate::normal().calculate_fee(per_input) + shortfall(&error) + .and_then(|(available, required)| awaiting(available, required, held, basis)) + .unwrap_or_else(|| PlatformWalletError::TransactionBuild(error.to_string())) } /// The output [`CoreWallet::pooled_max_sendable`] prices: one P2PKH, matching @@ -588,9 +620,8 @@ impl CoreWallet { let fee_rate = fee_rate.unwrap_or_else(FeeRate::normal); // Per-input and per-output costs derived from key-wallet's estimator by // difference, so this crate never re-declares a size that could drift - // from the one the build charges. + // from the one the build charges (`input_fee` / `per_input_size`). let empty = estimate_tx_size(0, 1, false); - let per_input = estimate_tx_size(1, 1, false).saturating_sub(empty); let manager = self.wallet_manager.read().await; let (wallet, info) = manager @@ -609,7 +640,7 @@ impl CoreWallet { // A UTXO earns its place only if it brings in more than its own input // costs at this rate; the rest are dead weight and are dropped. - let input_cost = checked_fee(fee_rate, per_input)?; + let input_cost = input_fee(fee_rate)?; let mut values: Vec = Vec::new(); for at in resolved { let Some(managed) = info.core_wallet.accounts.funds_account(&at) else { @@ -730,8 +761,7 @@ impl CoreWallet { let height = info.core_wallet.last_processed_height(); let unresolved = UnresolvedSends::of(info); - let held_input_cost = held_input_cost(); - let requested = requested_amount(builder.outputs()); + let basis = shortfall_basis(&builder); let mut held_value = 0u64; // Fund from every resolved account, mirroring key-wallet's own @@ -786,8 +816,8 @@ impl CoreWallet { // never fund it and count for nothing in a shortfall. Either // way the held coins are excluded — a seeded one fails the // build. - if !reservation_only { - held_value += unresolved.held_net_value(managed, height, held_input_cost); + if let (false, Some(basis)) = (reservation_only, basis) { + held_value += unresolved.held_net_value(managed, height, basis.input_cost); } builder = if reservation_only { builder.add_funding_reservation_only(managed, account) @@ -828,7 +858,7 @@ impl CoreWallet { error => awaiting_network( map_builder_error(error, funding_context), held_value, - requested, + basis, ), })?; @@ -1127,6 +1157,7 @@ mod tests { use dashcore::secp256k1::{ecdsa, PublicKey}; use dashcore::{Address as DashAddress, BlockHash, Network, OutPoint, Transaction, Txid}; use key_wallet::account::account_type::StandardAccountType; + use key_wallet::account::AccountType; use key_wallet::signer::{Signer, SignerMethod}; use key_wallet::transaction_checking::{BlockInfo, TransactionContext}; use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionStrategy; @@ -1137,6 +1168,7 @@ mod tests { use tokio::sync::{Barrier, RwLock}; use crate::broadcaster::TransactionBroadcaster; + use crate::manager::accessors::account_utxo_snapshots; use crate::test_support::{ apply_to_wallet, funded_wallet_manager, funded_wallet_manager_dual_standard, funded_wallet_manager_with_contact, AlwaysMaybeSentBroadcaster, AlwaysOkBroadcaster, @@ -1146,8 +1178,8 @@ mod tests { use crate::wallet::core::generation::next_acceptance_seq; use crate::wallet::core::transaction::{ - awaiting_network, awaiting_network_or_build_error, held_input_cost, modeled_output_script, - MAX_STANDARD_TX_INPUTS, + awaiting_network, awaiting_network_or_build_error, input_fee, modeled_output_script, + shortfall_basis, ShortfallBasis, MAX_STANDARD_TX_INPUTS, }; use crate::wallet::core::{CoreWallet, UnresolvedSends, WalletGeneration}; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; @@ -2297,7 +2329,7 @@ mod tests { held, outpoint: None, .. - }) if held == change.value() - held_input_cost() + }) if held == change.value() - input_fee(FeeRate::normal()).expect("fee") ), "got {refused:?}" ); @@ -2496,6 +2528,42 @@ mod tests { ), "got {refused:?}" ); + // The message names it too: the FFI carries only the message. + let message = refused.expect_err("refused").to_string(); + let named = format!( + "refused input {}:{}", + held.outpoint.txid, held.outpoint.vout + ); + assert!(message.contains(&named), "{message}"); + } + + /// The account UTXO listing a host does coin control from flags the held + /// coin, and only it. + #[tokio::test] + async fn should_flag_held_coins_in_the_account_utxo_listing() { + let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; + let paid = DashAddress::dummy(Network::Testnet, 94); + let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; + let held = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; + + let listing = { + let manager = manager.read().await; + let info = manager.get_wallet_info(&wallet_id).expect("wallet"); + account_utxo_snapshots( + info, + &AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }, + ) + }; + + let flagged: Vec = listing + .iter() + .filter(|row| row.is_held) + .map(|row| row.outpoint) + .collect(); + assert_eq!(flagged, vec![held.outpoint], "{listing:?}"); } /// Acceptance is not persisted. After a restart — a fresh generation over @@ -2739,31 +2807,69 @@ mod tests { required, }; assert!(matches!( - awaiting_network(shortfall(Some(400), Some(1_000)), 600, 0), + awaiting_network(shortfall(Some(400), Some(1_000)), 600, basis(0)), PlatformWalletError::CoreFundsAwaitingNetwork { .. } )); assert!(matches!( - awaiting_network(shortfall(Some(400), Some(1_000)), 599, 0), + awaiting_network(shortfall(Some(400), Some(1_000)), 599, basis(0)), PlatformWalletError::CorePooledInsufficientFunds { .. } )); assert!(matches!( - awaiting_network(shortfall(Some(400), Some(1_000)), 0, 0), + awaiting_network(shortfall(Some(400), Some(1_000)), 0, basis(0)), PlatformWalletError::CorePooledInsufficientFunds { .. } )); // Nothing left to select (every coin held): sized by the request. assert!(matches!( - awaiting_network(shortfall(Some(0), None), 1_000_000, 900_000), + awaiting_network(shortfall(Some(0), None), 1_000_000, basis(900_000)), PlatformWalletError::CoreFundsAwaitingNetwork { required: Some(900_000), .. } )); assert!(matches!( - awaiting_network(shortfall(Some(0), None), 1_000_000, 10_000_000), + awaiting_network(shortfall(Some(0), None), 1_000_000, basis(10_000_000)), + PlatformWalletError::CorePooledInsufficientFunds { .. } + )); + // A build whose shortfall cannot be told apart stays a shortfall. + assert!(matches!( + awaiting_network(shortfall(Some(400), Some(1_000)), 600, None), PlatformWalletError::CorePooledInsufficientFunds { .. } )); } + fn basis(requested: u64) -> Option { + Some(ShortfallBasis { + input_cost: 226, + requested, + }) + } + + /// The basis is priced at the build's own fee rate; a drain or a + /// special-payload build has none. + #[test] + fn should_price_the_shortfall_basis_at_the_builds_fee_rate() { + let output = |builder: TransactionBuilder| { + builder.add_output(&DashAddress::dummy(Network::Testnet, 70), 100_000) + }; + let normal = shortfall_basis(&output(TransactionBuilder::new())).expect("basis"); + let doubled = shortfall_basis(&output( + TransactionBuilder::new() + .set_fee_rate(FeeRate::new(FeeRate::normal().as_sat_per_kb() * 2)), + )) + .expect("basis"); + + assert_eq!( + normal.input_cost, + input_fee(FeeRate::normal()).expect("fee") + ); + assert!(doubled.input_cost > normal.input_cost); + assert!(doubled.requested > normal.requested); + assert!(shortfall_basis(&output( + TransactionBuilder::new().set_selection_strategy(SelectionStrategy::All) + )) + .is_none()); + } + /// The DashPay contact payment's build failure: a shortfall the held coins /// cover is "waiting on the network"; any other failure keeps the build /// error that path has always reported. @@ -2776,28 +2882,26 @@ mod tests { }) }; assert!(matches!( - awaiting_network_or_build_error(shortfall(), &SEND_FUNDING_SOURCES, 600, 0), + awaiting_network_or_build_error(shortfall(), 600, basis(0)), PlatformWalletError::CoreFundsAwaitingNetwork { .. } )); assert!(matches!( - awaiting_network_or_build_error(shortfall(), &SEND_FUNDING_SOURCES, 10, 0), + awaiting_network_or_build_error(shortfall(), 10, basis(0)), PlatformWalletError::TransactionBuild(_) )); assert!(matches!( awaiting_network_or_build_error( BuilderError::CoinSelection(SelectionError::NoUtxosAvailable), - &SEND_FUNDING_SOURCES, 1_000_000, - 900_000, + basis(900_000), ), PlatformWalletError::CoreFundsAwaitingNetwork { .. } )); assert!(matches!( awaiting_network_or_build_error( BuilderError::CoinSelection(SelectionError::NoUtxosAvailable), - &SEND_FUNDING_SOURCES, 1_000_000, - 10_000_000, + basis(10_000_000), ), PlatformWalletError::TransactionBuild(_) )); diff --git a/packages/rs-platform-wallet/src/wallet/core/unresolved.rs b/packages/rs-platform-wallet/src/wallet/core/unresolved.rs index 4b2cb5551ec..1ef32f6c0e2 100644 --- a/packages/rs-platform-wallet/src/wallet/core/unresolved.rs +++ b/packages/rs-platform-wallet/src/wallet/core/unresolved.rs @@ -69,27 +69,11 @@ impl UnresolvedSends { // every unsettled ancestor, unless that ancestor's own record is newer // (found dead since). let facts = info.generation.acceptance_among(&unsettled_txids); - let mut accepted: HashSet = HashSet::new(); - let mut up: VecDeque<(Txid, u64)> = facts + let seeds = facts .iter() .filter(|(_, (was_accepted, _))| *was_accepted) - .map(|(txid, (_, seq))| (*txid, *seq)) - .collect(); - while let Some((txid, seq)) = up.pop_front() { - if !accepted.insert(txid) { - continue; - } - for parent in parents.get(&txid).into_iter().flatten() { - let overridden = facts - .get(parent) - .is_some_and(|(parent_accepted, parent_seq)| { - !parent_accepted && *parent_seq > seq - }); - if !overridden { - up.push_back((*parent, seq)); - } - } - } + .map(|(txid, (_, seq))| (*txid, *seq)); + let accepted = accepted_with_ancestors(&facts, &parents, seeds); // A transaction built on a held output is no better than its parent: // a node that has not seen the parent refuses it. @@ -161,3 +145,75 @@ impl UnresolvedSends { .sum() } } + +/// The accepted transactions plus every unsettled ancestor their acceptance +/// reaches. An ancestor is reached unless its own record is a newer "dead"; +/// each transaction keeps the highest acceptance seq that reached it and is +/// walked again when a higher one arrives, so the result is the same whatever +/// order `seeds` (a `HashMap` walk) come in. +fn accepted_with_ancestors( + facts: &HashMap, + parents: &HashMap>, + seeds: impl IntoIterator, +) -> HashSet { + let mut best: HashMap = HashMap::new(); + let mut up: VecDeque<(Txid, u64)> = seeds.into_iter().collect(); + while let Some((txid, seq)) = up.pop_front() { + if best.get(&txid).is_some_and(|known| *known >= seq) { + continue; + } + best.insert(txid, seq); + for parent in parents.get(&txid).into_iter().flatten() { + let overridden = facts + .get(parent) + .is_some_and(|(parent_accepted, parent_seq)| !parent_accepted && *parent_seq > seq); + if !overridden { + up.push_back((*parent, seq)); + } + } + } + best.into_keys().collect() +} + +#[cfg(test)] +mod tests { + use std::collections::{HashMap, HashSet}; + + use dashcore::hashes::Hash; + use dashcore::Txid; + + use super::accepted_with_ancestors; + + fn txid(byte: u8) -> Txid { + Txid::from_byte_array([byte; 32]) + } + + /// G <- P <- C: P accepted at seq 2, C accepted at seq 9, G found dead at + /// seq 5. C's newer acceptance reaches G past its older death, whichever + /// of P and C is walked first. + #[test] + fn should_propagate_the_newest_acceptance_whatever_the_walk_order() { + let (g, p, c) = (txid(1), txid(2), txid(3)); + let facts = HashMap::from([(p, (true, 2)), (c, (true, 9)), (g, (false, 5))]); + let parents = HashMap::from([(c, vec![p]), (p, vec![g])]); + let expected = HashSet::from([g, p, c]); + + let p_first = accepted_with_ancestors(&facts, &parents, [(p, 2), (c, 9)]); + let c_first = accepted_with_ancestors(&facts, &parents, [(c, 9), (p, 2)]); + + assert_eq!(p_first, expected); + assert_eq!(c_first, expected); + } + + /// An ancestor found dead after the newest acceptance below it stays out. + #[test] + fn should_stop_at_an_ancestor_found_dead_after_the_acceptance() { + let (g, p, c) = (txid(1), txid(2), txid(3)); + let facts = HashMap::from([(c, (true, 3)), (g, (false, 7))]); + let parents = HashMap::from([(c, vec![p]), (p, vec![g])]); + + let accepted = accepted_with_ancestors(&facts, &parents, [(c, 3)]); + + assert_eq!(accepted, HashSet::from([p, c])); + } +} diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 6fa87646faa..13ae6a4041c 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -12,11 +12,8 @@ use key_wallet_manager::WalletManager; use super::*; use crate::broadcaster::TransactionBroadcaster; use crate::error::PlatformWalletError; -use crate::wallet::core::{ - awaiting_network_or_build_error, held_input_cost, requested_amount, UnresolvedSends, -}; +use crate::wallet::core::{awaiting_network_or_build_error, shortfall_basis, UnresolvedSends}; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; -use crate::SEND_FUNDING_SOURCES; // --------------------------------------------------------------------------- // Incoming payment recording + reconcile @@ -1260,8 +1257,9 @@ impl DashPayView<'_, B> { // Coins of our own sends the network has not been seen to accept // stay out of selection (see `UnresolvedSends`). let unresolved = UnresolvedSends::of(info); - let held_input_cost = held_input_cost(); - let requested = requested_amount(builder.outputs()); + // Infallible: the payment address is already marked used here, and + // an early return now would burn it. + let basis = shortfall_basis(&builder); let mut held_value = 0u64; // Derivation paths for every offered UTXO, since the signer closure @@ -1299,8 +1297,10 @@ impl DashPayView<'_, B> { funding_paths.insert(utxo.address.clone(), path); } } - held_value += - unresolved.held_net_value(managed, current_height, held_input_cost); + if let Some(basis) = basis { + held_value += + unresolved.held_net_value(managed, current_height, basis.input_cost); + } builder = unresolved.exclude_from(builder.add_funding(managed, account), managed); offered_accounts.push(at); @@ -1351,12 +1351,7 @@ impl DashPayView<'_, B> { { return_contact_payment_address_to_pool(external_account, &payment_address); } - return Err(awaiting_network_or_build_error( - e, - &SEND_FUNDING_SOURCES, - held_value, - requested, - )); + return Err(awaiting_network_or_build_error(e, held_value, basis)); } }; diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerDiagnostics.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerDiagnostics.swift index 3a350a2a782..dde7d7109ff 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerDiagnostics.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerDiagnostics.swift @@ -337,6 +337,11 @@ extension PlatformWalletManager { public let scriptPubkey: Data public let height: UInt32 public let isLocked: Bool + /// An output of a send the network has not been seen to accept: a + /// build seeded with it fails with `.coreFundsAwaitingNetwork`, so + /// coin control and drains leave it out. Always false while broadcast + /// probing is off. + public var isHeld: Bool = false } public func accountUtxos( @@ -373,7 +378,8 @@ extension PlatformWalletManager { valueDuffs: entry.value_duffs, scriptPubkey: scriptData, height: entry.height, - isLocked: entry.is_locked + isLocked: entry.is_locked, + isHeld: entry.is_held ) } } From df35c9b2edaa9100dd59b97c54aa9d3f3e035d34 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 22:35:46 +0300 Subject: [PATCH 053/169] build(deps): pin rust-dashcore to the outpoint-exclusion branch (rust-dashcore#1100) The unresolved-send hold builds on TransactionBuilder::exclude_outpoints and its getters, which land in dashpay/rust-dashcore#1100. All eight rust-dashcore crates move from e4208c90 to that PR's head 2e0202f2 (based on e4208c90, so nothing else changes). Re-pin to the merge commit on dev once #1100 merges. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 24 ++++++++++++------------ Cargo.toml | 16 ++++++++-------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b7b6179ace..e1965de1575 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1654,7 +1654,7 @@ dependencies = [ [[package]] name = "dash-network" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" dependencies = [ "cbindgen 0.29.4", "grovedb-bincode", @@ -1665,7 +1665,7 @@ dependencies = [ [[package]] name = "dash-network-seeds" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" dependencies = [ "dash-network", ] @@ -1760,7 +1760,7 @@ dependencies = [ [[package]] name = "dash-spv" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" dependencies = [ "async-trait", "chrono", @@ -1789,7 +1789,7 @@ dependencies = [ [[package]] name = "dashcore" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" dependencies = [ "anyhow", "base64-compat", @@ -1815,12 +1815,12 @@ dependencies = [ [[package]] name = "dashcore-private" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" [[package]] name = "dashcore-rpc" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" dependencies = [ "dashcore-rpc-json", "hex", @@ -1833,7 +1833,7 @@ dependencies = [ [[package]] name = "dashcore-rpc-json" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" dependencies = [ "dashcore", "grovedb-bincode", @@ -1848,7 +1848,7 @@ dependencies = [ [[package]] name = "dashcore_hashes" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" dependencies = [ "dashcore-private", "grovedb-bincode", @@ -2922,7 +2922,7 @@ dependencies = [ [[package]] name = "git-state" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" [[package]] name = "glob" @@ -4154,7 +4154,7 @@ dependencies = [ [[package]] name = "key-wallet" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" dependencies = [ "aes", "async-trait", @@ -4183,7 +4183,7 @@ dependencies = [ [[package]] name = "key-wallet-ffi" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" dependencies = [ "cbindgen 0.29.4", "dash-network", @@ -4199,7 +4199,7 @@ dependencies = [ [[package]] name = "key-wallet-manager" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" dependencies = [ "async-trait", "dashcore", diff --git a/Cargo.toml b/Cargo.toml index 305df7df4b2..017a7432da1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,14 +65,14 @@ grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8 grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } -dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -dash-network-seeds = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -dash-network = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } +dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } +dash-network-seeds = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } +dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } +key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } +key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } +key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } +dash-network = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } +dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } tokio-metrics = "0.5" # Size-tuned profile for the iOS `rs-unified-sdk-ffi` staticlib, which From efca50fa54c26798aac2adbc94d1862b50d5ea5d Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 23:21:01 +0300 Subject: [PATCH 054/169] fix(platform-wallet-ffi): pass the probe callback in the shielded test helper FFIEventHandler::new takes the outgoing-probe callback as a third argument; the shielded-only test helper still passed two, so the crate's tests did not compile with the `shielded` feature. Co-Authored-By: Claude Opus 5.5 --- packages/rs-platform-wallet-ffi/src/shielded_sync.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/rs-platform-wallet-ffi/src/shielded_sync.rs b/packages/rs-platform-wallet-ffi/src/shielded_sync.rs index dfda7eeb4c1..c8412c1b5fc 100644 --- a/packages/rs-platform-wallet-ffi/src/shielded_sync.rs +++ b/packages/rs-platform-wallet-ffi/src/shielded_sync.rs @@ -813,6 +813,7 @@ mod local_balance_tests { release_fn: None, }, None, + None, ); let _runtime_guard = runtime().enter(); PlatformWalletManager::new( From ddf3bee42009438ccbaed7571091db8db3595646 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Sat, 3 Oct 2026 17:23:21 +0300 Subject: [PATCH 055/169] revert: drop the unresolved-send hold and the rust-dashcore#1100 pin Testing the reviewer's alternative on rust-dashcore#1100: key-wallet's require_final_inputs already leaves out every coin that is neither confirmed nor InstantSend-locked, so the per-build outpoint exclusion and the acceptance bookkeeping that fed it are not needed. Reverts, in order: df35c9b2ed, b59f0b8778, 3634e3a81c, 5fcf610b3b, cdc07ffd01, fbc1c6bd9a. The final-inputs rule follows in the next commit. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 24 +- Cargo.toml | 16 +- .../dashsdk/errors/DashSdkError.kt | 11 - .../dashsdk/errors/DashSdkErrorTest.kt | 4 - .../ERROR_CODE_REGISTRY.md | 6 +- .../src/core_wallet_types.rs | 4 - packages/rs-platform-wallet-ffi/src/error.rs | 32 - .../src/manager_diagnostics.rs | 1 - packages/rs-platform-wallet/src/error.rs | 31 - .../src/manager/accessors.rs | 66 +- .../rs-platform-wallet/src/manager/load.rs | 7 +- .../rs-platform-wallet/src/manager/mod.rs | 24 +- .../rs-platform-wallet/src/test_support.rs | 27 +- .../src/wallet/asset_lock/build.rs | 114 +- .../src/wallet/asset_lock/sync/recovery.rs | 11 +- .../src/wallet/core/broadcast.rs | 15 +- .../src/wallet/core/broadcast_resolver.rs | 334 +----- .../src/wallet/core/generation.rs | 145 +-- .../rs-platform-wallet/src/wallet/core/mod.rs | 6 +- .../src/wallet/core/transaction.rs | 985 +----------------- .../src/wallet/core/unresolved.rs | 219 ---- .../src/wallet/identity/network/payments.rs | 33 +- .../src/wallet/reservations.rs | 23 +- .../PlatformWalletManagerDiagnostics.swift | 8 +- .../PlatformWallet/PlatformWalletResult.swift | 11 - 25 files changed, 135 insertions(+), 2022 deletions(-) delete mode 100644 packages/rs-platform-wallet/src/wallet/core/unresolved.rs diff --git a/Cargo.lock b/Cargo.lock index e1965de1575..6b7b6179ace 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1654,7 +1654,7 @@ dependencies = [ [[package]] name = "dash-network" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" dependencies = [ "cbindgen 0.29.4", "grovedb-bincode", @@ -1665,7 +1665,7 @@ dependencies = [ [[package]] name = "dash-network-seeds" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" dependencies = [ "dash-network", ] @@ -1760,7 +1760,7 @@ dependencies = [ [[package]] name = "dash-spv" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" dependencies = [ "async-trait", "chrono", @@ -1789,7 +1789,7 @@ dependencies = [ [[package]] name = "dashcore" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" dependencies = [ "anyhow", "base64-compat", @@ -1815,12 +1815,12 @@ dependencies = [ [[package]] name = "dashcore-private" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" [[package]] name = "dashcore-rpc" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" dependencies = [ "dashcore-rpc-json", "hex", @@ -1833,7 +1833,7 @@ dependencies = [ [[package]] name = "dashcore-rpc-json" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" dependencies = [ "dashcore", "grovedb-bincode", @@ -1848,7 +1848,7 @@ dependencies = [ [[package]] name = "dashcore_hashes" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" dependencies = [ "dashcore-private", "grovedb-bincode", @@ -2922,7 +2922,7 @@ dependencies = [ [[package]] name = "git-state" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" [[package]] name = "glob" @@ -4154,7 +4154,7 @@ dependencies = [ [[package]] name = "key-wallet" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" dependencies = [ "aes", "async-trait", @@ -4183,7 +4183,7 @@ dependencies = [ [[package]] name = "key-wallet-ffi" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" dependencies = [ "cbindgen 0.29.4", "dash-network", @@ -4199,7 +4199,7 @@ dependencies = [ [[package]] name = "key-wallet-manager" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=2e0202f2b7856d943adb233c9f65e39602e40cbf#2e0202f2b7856d943adb233c9f65e39602e40cbf" +source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" dependencies = [ "async-trait", "dashcore", diff --git a/Cargo.toml b/Cargo.toml index 017a7432da1..305df7df4b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,14 +65,14 @@ grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8 grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } -dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } -dash-network-seeds = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } -dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } -key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } -key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } -key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } -dash-network = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } -dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "2e0202f2b7856d943adb233c9f65e39602e40cbf" } +dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } +dash-network-seeds = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } +dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } +key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } +key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } +key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } +dash-network = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } +dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } tokio-metrics = "0.5" # Size-tuned profile for the iOS `rs-unified-sdk-ffi` staticlib, which diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt index e645ade57e8..420ad1a6bbb 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt @@ -117,16 +117,6 @@ sealed class DashSdkError( class CoreInsufficientFunds(message: String, cause: Throwable? = null) : PlatformWallet(message, cause) - /** - * `ErrorCoreFundsAwaitingNetwork` (native code 59). The payment is - * covered only by coins of an earlier send of this wallet whose - * broadcast outcome is not known yet; they become spendable once the - * network is seen to accept that send. Not a shortfall — retry after - * the earlier payment settles. - */ - class CoreFundsAwaitingNetwork(message: String, cause: Throwable? = null) : - PlatformWallet(message, cause) - /** * `ErrorShieldedInsufficientBalance` (native code 41; historical FFI * spelling). A Platform Payment account's deterministic shield input @@ -743,7 +733,6 @@ sealed class DashSdkError( 20 -> PlatformWallet.TransactionBroadcastUnconfirmed(message, cause) // ErrorTransactionBroadcastUnconfirmed 42 -> PlatformWallet.MasternodeWithdrawalUnconfirmed(message, cause) // ErrorMasternodeWithdrawalUnconfirmed 22 -> PlatformWallet.CoreInsufficientFunds(message, cause) // ErrorCoreInsufficientFunds - 59 -> PlatformWallet.CoreFundsAwaitingNetwork(message, cause) // ErrorCoreFundsAwaitingNetwork 23 -> PlatformWallet.AssetLockNotTracked(message, cause) // ErrorAssetLockNotTracked 24 -> PlatformWallet.AssetLockAlreadyConsumed(message, cause) // ErrorAssetLockAlreadyConsumed 25 -> PlatformWallet.AssetLockFundingMismatch(message, cause) // ErrorAssetLockFundingMismatch diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt index 1e5c1700a0a..7129c55f633 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt @@ -129,10 +129,6 @@ class DashSdkErrorTest { DashSdkError.fromNative(DashSDKException(offset + 22, "inputs reserved")) assertTrue(coreInsufficientFunds is DashSdkError.PlatformWallet.CoreInsufficientFunds) - val awaitingNetwork = - DashSdkError.fromNative(DashSDKException(offset + 59, "funds awaiting network")) - assertTrue(awaitingNetwork is DashSdkError.PlatformWallet.CoreFundsAwaitingNetwork) - // The asset-lock coin-selection shortfall (29) must reach callers as its // own type rather than Generic, and must stay DISTINCT from the atomic // Core-send shortfall (22) — the two selectors report over different diff --git a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md index 73edb813f84..3399060f53b 100644 --- a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md +++ b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md @@ -114,14 +114,13 @@ These are shipped ABI. Do not renumber. | 98 | `NotFound` | Sentinel — `Option` returned as an error | | 99 | `ErrorUnknown` | Sentinel — unmapped/flattened errors | -**Next allocatable integer: 60** — 27–59 are all claimed (27, 29, 31, 34–42 +**Next allocatable integer: 59** — 27–58 are all claimed (27, 29, 31, 34–42 and 46 merged; 43–45 proposed by active #4313 at head `0302b188ab`; 47 and 48 proposed by active #4356 (47 renumbered from 42, 48 from 43 — see their rows below); 49–54 proposed by active #4586 (the persister operation × kind block); 55–57 proposed by #4715 for pending identity-funded shield debits and durable recovery errors; 58 proposed by #4799 for an unavailable -identity balance response; 59 proposed by the unresolved-broadcast resolver -PR for funds held until an earlier send is accepted; 28, 30, +identity balance response; 28, 30, 32 and 33 reserved). **28, 30, 32 and 33 are RESERVED, not free**: 28 and 30 were vacated when the reservation trio moved to 34–36; 32 and 33 lapsed when their in-repo owners @@ -173,7 +172,6 @@ Fork-era numbers remain in the collision history, which is immutable record. | 56 | `ErrorShieldedRecoveryCorrupted` | #4715 | Proposed — durable shielded recovery data is malformed or invalid; preserved for diagnosis. Rust, Swift and Kotlin preserve this typed error | | 57 | `ErrorShieldedRecoveryKeysRequired` | #4715 | Proposed — recovery needs the account and compatible keys; ciphertext damage can produce the same symptom. Rust, Swift and Kotlin preserve this typed error | | 58 | `ErrorIdentityBalanceUnavailable` | #4799 | Proposed — Platform returned no balance for a managed identity. Retrying the read is safe; this is distinct from missing wallet ownership and must not trigger registration or funding | -| 59 | `ErrorCoreFundsAwaitingNetwork` | `fix/resolve-unknown-broadcast` (PR not opened yet) | Proposed — claimed from the frontier (58). A Core payment that only the coins of an earlier send of this wallet would cover, while that send's broadcast outcome is unresolved; those coins become spendable once the network is seen to accept it. Emitted only while the manager probes unresolved broadcasts. Not a shortfall: retry after the earlier payment settles. Rust value + pin test, Swift raw case + typed `coreFundsAwaitingNetwork`, Kotlin typed `PlatformWallet.CoreFundsAwaitingNetwork` + `fromPlatformWalletNative` arm + `DashSdkErrorTest` pin | **Code 31 left this table on 2026-08-04.** `ErrorSigningKeyUnavailable` sat here as #4183's proposal until #4183 merged (`189a3abb1c`); it is now in the merged diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs index b89138c46e4..68e3dd8ff8a 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs @@ -957,10 +957,6 @@ pub struct AccountUtxoEntryFFI { pub script_pubkey_len: usize, pub height: u32, pub is_locked: bool, - /// An output of a send the network has not been seen to accept: a build - /// seeded with it fails with `ErrorCoreFundsAwaitingNetwork`. Coin control - /// and drains should leave it out. - pub is_held: bool, } /// One transaction row in the per-account paginated drill-down. diff --git a/packages/rs-platform-wallet-ffi/src/error.rs b/packages/rs-platform-wallet-ffi/src/error.rs index c77e348b74d..5e7bc342baf 100644 --- a/packages/rs-platform-wallet-ffi/src/error.rs +++ b/packages/rs-platform-wallet-ffi/src/error.rs @@ -599,14 +599,6 @@ pub enum PlatformWalletFFIResultCode { /// Platform returned no balance for a managed identity. Retrying this read /// is safe; this does not imply missing ownership or require registration. ErrorIdentityBalanceUnavailable = 58, - /// A Core payment needs coins held back because they came from one of the - /// wallet's own sends whose broadcast outcome is still unknown - /// ([`platform_wallet::PlatformWalletError::CoreFundsAwaitingNetwork`]). - /// Not a shortfall and nothing was sent: the coins become spendable once - /// that send is InstantSend-locked, mined, or found accepted, so the host - /// should say the money is waiting on the network and offer a retry - /// later — not "insufficient funds". - ErrorCoreFundsAwaitingNetwork = 59, /// The named thing does not exist. /// @@ -974,9 +966,6 @@ impl From for PlatformWalletFFIResult { PlatformWalletError::AddressNonceMismatch { .. } => { PlatformWalletFFIResultCode::ErrorAddressNonceMismatch } - PlatformWalletError::CoreFundsAwaitingNetwork { .. } => { - PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork - } // Both shapes are "the wallet cannot cover this payment"; hosts // classify and retry them identically, so the pooled variant rides // the same code rather than forcing every host to learn a second @@ -2589,27 +2578,6 @@ mod tests { } } -#[cfg(test)] -mod core_funds_awaiting_network_tests { - use super::*; - - #[test] - fn should_give_held_funds_their_own_code_apart_from_insufficient_funds() { - let result = PlatformWalletFFIResult::from(PlatformWalletError::CoreFundsAwaitingNetwork { - available: Some(700_000), - held: 599_000, - required: Some(1_000_000), - outpoint: None, - }); - assert_eq!( - result.code, - PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork - ); - assert_eq!(result.code as u32, 59); - assert!(!result.message.is_null()); - } -} - #[cfg(test)] mod identity_balance_error_tests { use super::*; diff --git a/packages/rs-platform-wallet-ffi/src/manager_diagnostics.rs b/packages/rs-platform-wallet-ffi/src/manager_diagnostics.rs index f6d3e20f55e..5630ad287be 100644 --- a/packages/rs-platform-wallet-ffi/src/manager_diagnostics.rs +++ b/packages/rs-platform-wallet-ffi/src/manager_diagnostics.rs @@ -580,7 +580,6 @@ pub unsafe extern "C" fn platform_wallet_account_utxos( script_pubkey_len: script_len, height: s.height, is_locked: s.is_locked, - is_held: s.is_held, } }) .collect(); diff --git a/packages/rs-platform-wallet/src/error.rs b/packages/rs-platform-wallet/src/error.rs index 1d2e5302291..d87b3f1ba58 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -393,29 +393,6 @@ pub enum PlatformWalletError { required: Option, }, - /// A Core payment the wallet's balance covers, but only with coins held - /// back because they came from one of its own sends the network has not - /// been seen to accept yet (its broadcast outcome is unknown). Spending - /// them before that is settled builds a transaction no node accepts if - /// that send never reached the network. They become spendable once the - /// send is InstantSend-locked, mined, or found accepted. Not a shortfall: - /// the host should say the money is waiting on the network. - /// - /// `held` is the held value among the offered sources. `outpoint` names - /// a held coin the caller chose as an input itself (outpoint-seeded or - /// `reservation_only` builds); `None` when selection came up short. - #[error( - "Core funds held until an earlier send is accepted by the network: \ - available {available:?}, held {held}, required {required:?}{}", - refused_input(outpoint) - )] - CoreFundsAwaitingNetwork { - available: Option, - held: u64, - required: Option, - outpoint: Option, - }, - #[error("no spendable inputs available on {account_type} account {account_index}: {context}")] NoSpendableInputs { account_type: StandardAccountType, @@ -1808,11 +1785,3 @@ mod asset_lock_already_consumed_tests { )); } } - -/// The refused input of a `CoreFundsAwaitingNetwork`, for its message: a host -/// doing coin control learns which coin to leave out. -fn refused_input(outpoint: &Option) -> String { - outpoint - .map(|outpoint| format!(", refused input {}:{}", outpoint.txid, outpoint.vout)) - .unwrap_or_default() -} diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index d4a2678ec3a..fee4b591545 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -20,8 +20,7 @@ use crate::manager::platform_address_sync::PlatformAddressSyncManager; #[cfg(feature = "shielded")] use crate::manager::shielded_sync::ShieldedSyncManager; use crate::spv::SpvRuntime; -use crate::wallet::core::UnresolvedSends; -use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; +use crate::wallet::platform_wallet::WalletId; use crate::wallet::PlatformWallet; /// Result of [`PlatformWalletManager::provider_masternode_txs_blocking`]: @@ -215,10 +214,6 @@ pub struct AccountUtxoSnapshot { pub script_pubkey: Vec, pub height: u32, pub is_locked: bool, - /// An output of a send the network has not been seen to accept (see - /// `UnresolvedSends`): no payment spends it until that send resolves, and - /// one seeded with it is refused. Always `false` while probing is off. - pub is_held: bool, } /// One row of a wallet's UTXO inventory page — see [`wallet_utxos_page`]. @@ -1140,7 +1135,30 @@ impl PlatformWalletManager

{ let Some(info) = wm.get_wallet_info(wallet_id) else { return Vec::new(); }; - account_utxo_snapshots(info, target) + let accounts = info.core_wallet.accounts.all_accounts(); + let Some(account) = accounts + .iter() + .find(|a| &a.managed_account_type().to_account_type() == target) + else { + return Vec::new(); + }; + // UTXOs only exist on the funds variant. Keys-only accounts + // (identity / asset-lock / provider) never carry UTXOs by + // construction, so an empty list is the correct snapshot. + let Some(funds) = account.as_funds() else { + return Vec::new(); + }; + funds + .utxos + .values() + .map(|utxo: &Utxo| AccountUtxoSnapshot { + outpoint: utxo.outpoint, + value_duffs: utxo.txout.value, + script_pubkey: utxo.txout.script_pubkey.as_bytes().to_vec(), + height: utxo.height, + is_locked: utxo.is_locked, + }) + .collect() } // ----------------------------------------------------------------- @@ -1491,40 +1509,6 @@ fn tx_record_snapshot(rec: &TransactionRecord) -> AccountTransactionSnapshot { } } -/// Snapshot of every UTXO row on the wallet's `target` account, each flagged -/// held when it is an output of an unresolved send. -pub(crate) fn account_utxo_snapshots( - info: &PlatformWalletInfo, - target: &AccountType, -) -> Vec { - let accounts = info.core_wallet.accounts.all_accounts(); - let Some(account) = accounts - .iter() - .find(|a| &a.managed_account_type().to_account_type() == target) - else { - return Vec::new(); - }; - // UTXOs only exist on the funds variant. Keys-only accounts - // (identity / asset-lock / provider) never carry UTXOs by - // construction, so an empty list is the correct snapshot. - let Some(funds) = account.as_funds() else { - return Vec::new(); - }; - let held = UnresolvedSends::of(info); - funds - .utxos - .values() - .map(|utxo: &Utxo| AccountUtxoSnapshot { - outpoint: utxo.outpoint, - value_duffs: utxo.txout.value, - script_pubkey: utxo.txout.script_pubkey.as_bytes().to_vec(), - height: utxo.height, - is_locked: utxo.is_locked, - is_held: held.holds(&utxo.outpoint), - }) - .collect() -} - #[cfg(test)] mod spv_rescan_tests { use std::sync::Arc; diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index dd2e579d823..7f3ba96605b 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -618,12 +618,7 @@ pub(super) async fn resend_one( let txid = tx.txid(); match broadcaster.broadcast(tx).await { - Ok(_) => { - // The one acceptance signal for a send still unconfirmed after a - // restart; its change may fund payments again (`UnresolvedSends`). - generation.mark_send_accepted(txid); - tracing::info!(%txid, "load: re-dispatched unconfirmed send, accepted") - } + Ok(_) => tracing::info!(%txid, "load: re-dispatched unconfirmed send, accepted"), // Expected for the orphaned case: sent, no acceptance signal, now // owned by the rebroadcast timer. Err(BroadcastError::MaybeSent { reason }) => tracing::info!( diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 053ddf8d95f..f3b14884fd9 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -35,8 +35,8 @@ use crate::manager::platform_address_sync::PlatformAddressSyncManager; use crate::manager::shielded_sync::ShieldedSyncManager; use crate::spv::SpvRuntime; use crate::wallet::asset_lock::LockNotifyHandler; -use crate::wallet::core::broadcast_resolver::{BroadcastResolver, FenceRegistry}; -use crate::wallet::core::{BalanceUpdateHandler, InBroadcastFences, SpendObservationHandler}; +use crate::wallet::core::broadcast_resolver::BroadcastResolver; +use crate::wallet::core::{BalanceUpdateHandler, SpendObservationHandler}; use crate::wallet::identity::network::DashPayPaymentHandler; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; use crate::wallet::PlatformWallet; @@ -472,8 +472,9 @@ pub struct PlatformWalletManager { /// /// A `std::sync::Mutex`: touched only at wallet registration and load, for /// one map lookup, and never held across an await. - /// Shared with the broadcast resolver ([`FenceRegistry`]). - pub(super) in_broadcast_fences: FenceRegistry, + pub(super) in_broadcast_fences: std::sync::Mutex< + std::collections::BTreeMap>, + >, } impl PlatformWalletManager

{ @@ -553,11 +554,9 @@ impl PlatformWalletManager

{ // was unknown (read-only; see its module docs). It publishes through // the event manager it is registered with, so it gets a weak handle // to it once that exists. - let in_broadcast_fences: FenceRegistry = Arc::default(); let broadcast_resolver = Arc::new(BroadcastResolver::new( Arc::new(DapiAcceptanceProbe::new(Arc::clone(&sdk))), Arc::clone(&wallet_manager), - Arc::clone(&in_broadcast_fences), )); let event_manager = Arc::new(PlatformEventManager::new(vec![ app_handler, @@ -630,7 +629,7 @@ impl PlatformWalletManager

{ event_adapter_join: tokio::sync::Mutex::new(Some(event_adapter_join)), registry, sync_fault, - in_broadcast_fences, + in_broadcast_fences: std::sync::Mutex::new(std::collections::BTreeMap::new()), } } @@ -641,17 +640,16 @@ impl PlatformWalletManager

{ /// replaces another under the same id inherits its pending-spend fences — /// see the [`in_broadcast_fences`](Self#structfield.in_broadcast_fences) /// field docs. - pub(super) fn in_broadcast_fences_for(&self, wallet_id: &WalletId) -> Arc { + pub(super) fn in_broadcast_fences_for( + &self, + wallet_id: &WalletId, + ) -> Arc { Arc::clone( self.in_broadcast_fences .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .entry(*wallet_id) - .or_insert_with(|| { - Arc::new(InBroadcastFences::with_hold_flag( - self.broadcast_resolver.enabled_flag(), - )) - }), + .or_default(), ) } diff --git a/packages/rs-platform-wallet/src/test_support.rs b/packages/rs-platform-wallet/src/test_support.rs index f3bd192df52..b9446432ee0 100644 --- a/packages/rs-platform-wallet/src/test_support.rs +++ b/packages/rs-platform-wallet/src/test_support.rs @@ -25,7 +25,7 @@ use key_wallet::bip32::ExtendedPubKey; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; use key_wallet::signer::{ExtendedPubKeySigner, Signer, SignerMethod}; use key_wallet::test_utils::TestWalletContext; -use key_wallet::transaction_checking::{BlockInfo, TransactionContext, WalletTransactionChecker}; +use key_wallet::transaction_checking::{BlockInfo, TransactionContext}; use key_wallet::{DerivationPath, Wallet}; use key_wallet_manager::WalletManager; use tokio::sync::RwLock; @@ -626,31 +626,6 @@ where target } -/// Apply `transaction` to the wallet in `context` — what SPV does when it sees -/// it in a peer's mempool (`Mempool`), InstantSend-locked or in a block. A send -/// this wallet signed, applied in `Mempool` and never echoed back, is what an -/// unknown broadcast outcome leaves behind. -pub async fn apply_to_wallet( - manager: &Arc>>, - wallet_id: &WalletId, - transaction: &Transaction, - context: TransactionContext, -) { - let mut wm = manager.write().await; - let (wallet, info) = wm - .get_wallet_and_info_mut(wallet_id) - .expect("wallet present in manager"); - let mut wallet = wallet.clone(); - let result = info - .core_wallet - .check_core_transaction(transaction, context, &mut wallet, true, true) - .await; - assert!( - result.is_relevant, - "the transaction must be the wallet's own" - ); -} - /// No-op persister satisfying [`PlatformWalletManager`] construction for tests /// that need a full [`PlatformWallet`] but no real persistence pipeline. pub struct NoopTestPersister; diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs index e96e1e44576..951c1e21ce4 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/build.rs @@ -25,7 +25,6 @@ use key_wallet::wallet::Wallet; use crate::changeset::{AccountRegistrationEntry, PlatformWalletChangeSet}; use crate::error::PlatformWalletError; use crate::wallet::platform_wallet::PlatformWalletInfo; -use crate::wallet::reservations::record_send_accepted; use crate::ASSET_LOCK_FUNDING_SOURCES; use super::manager::{AssetLockManager, DEFAULT_FEE_PER_KB}; @@ -274,9 +273,6 @@ impl AssetLockManager { // pools the sources, enforces that CoinJoin funding is drain-only and // unpooled, and reserves the selected inputs in each contributing // account's own set under one owner token). - // No unresolved-send hold here (see `UnresolvedSends`): the builder - // calls `require_final_inputs`, so only confirmed or InstantSend-locked - // coins are ever selected, and an unresolved send's outputs are neither. let result = info .core_wallet .build_asset_lock_with_signer( @@ -1144,9 +1140,6 @@ impl AssetLockManager { // spend. A cancellation or unwind inside `broadcast` reaches no arm // at all and settles as pending through `InBroadcastPin::drop`. let broadcast_outcome = self.broadcaster.broadcast(&tx).await; - if let Ok(txid) = &broadcast_outcome { - record_send_accepted(&self.wallet_manager, &self.wallet_id, *txid).await; - } if let Err(e) = broadcast_outcome { if matches!(e, crate::broadcaster::BroadcastError::Rejected { .. }) { // The rejection alone does NOT establish the released verdict @@ -1362,7 +1355,6 @@ fn map_builder_error(e: AssetLockError, requested: u64) -> PlatformWalletError { #[cfg(test)] mod tests { - use std::collections::HashSet; use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -1387,20 +1379,16 @@ mod tests { ClientStartState, PersistenceError, PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::test_support::{ - apply_to_wallet, funded_wallet_manager, funded_wallet_manager_dual_standard, + funded_wallet_manager, funded_wallet_manager_dual_standard, funded_wallet_manager_with_contact, AlwaysMaybeSentBroadcaster, AlwaysOkBroadcaster, AlwaysRejectedBroadcaster, WalletSigner, }; use crate::wallet::asset_lock::manager::AssetLockManager; use crate::wallet::asset_lock::tracked::AssetLockStatus; - use crate::wallet::core::CoreWallet; use crate::wallet::persister::WalletPersister; use crate::wallet::platform_wallet::PlatformWalletInfo; use crate::wallet::platform_wallet::WalletId; - use crate::SEND_FUNDING_SOURCES; use crate::{AssetLockFundingType, PlatformWalletError}; - use dashcore::{Address as DashAddress, Network}; - use key_wallet::wallet::managed_wallet_info::transaction_builder::TransactionBuilder; /// The zero-spendable-candidate selection error must surface the SAME /// typed shortfall as a partial shortfall (not the generic string form), @@ -2022,106 +2010,6 @@ mod tests { ); } - /// An asset lock never funds from the coins of an unresolved send of - /// ours: key-wallet's `require_final_inputs` keeps only confirmed or - /// InstantSend-locked coins, so with the hold on and the send's change the - /// wallet's only coin, the lock is a plain shortfall, never built on it. - #[tokio::test] - async fn should_never_fund_an_asset_lock_from_an_unresolved_sends_change() { - let (wallet_manager, wallet_id, generation, signer) = - funded_wallet_manager(StandardAccountType::BIP44Account).await; - generation.set_holds_unresolved_sends(true); - let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); - let core = CoreWallet::new( - Arc::clone(&sdk), - Arc::clone(&wallet_manager), - wallet_id, - Arc::new(AlwaysOkBroadcaster), - generation, - ); - // Spend the one confirmed coin; its change is all the wallet has left. - let finalized = core - .finalize_transaction( - TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 95), 1_000_000), - &SEND_FUNDING_SOURCES, - 0, - &signer, - ) - .await - .expect("the send builds"); - apply_to_wallet( - &wallet_manager, - &wallet_id, - finalized.transaction(), - TransactionContext::Mempool, - ) - .await; - let manager = AssetLockManager::new( - sdk, - wallet_manager, - wallet_id, - Arc::new(Notify::new()), - Arc::new(AlwaysOkBroadcaster), - WalletPersister::new( - wallet_id, - Arc::new(CapturingPersistence::default()) as Arc, - ), - ); - - let refused = manager - .create_funded_asset_lock_proof( - 100_000, - 0, - AssetLockFundingType::IdentityRegistration, - 0, - &signer, - ) - .await; - - assert!( - matches!( - refused, - Err(PlatformWalletError::AssetLockInsufficientFunds { .. }) - ), - "got {:?}", - refused.map(|_| ()) - ); - } - - /// An echoed asset-lock broadcast is recorded as accepted, so its change - /// is not held as an unresolved send's while its InstantSend lock is due. - #[tokio::test] - async fn should_mark_an_echoed_asset_lock_accepted() { - let (manager, signer, _persistence) = - funded_asset_lock_manager(Arc::new(AlwaysOkBroadcaster)).await; - { - // Facts are kept only while the hold is on, as with probing on. - let wm = manager.wallet_manager.read().await; - let (_, info) = wm.get_wallet_and_info(&manager.wallet_id).expect("wallet"); - info.generation.set_holds_unresolved_sends(true); - } - - let (_, out_point) = manager - .broadcast_funded_asset_lock( - 1_000_000, - 0, - AssetLockFundingType::IdentityRegistration, - 0, - &signer, - ) - .await - .expect("broadcast"); - - let wm = manager.wallet_manager.read().await; - let (_, info) = wm.get_wallet_and_info(&manager.wallet_id).expect("wallet"); - let txid = out_point.txid; - assert!(info - .generation - .accepted_among(&HashSet::from([txid])) - .contains(&txid)); - } - /// An *ambiguous* asset-lock broadcast failure must keep both the funding /// reservation and the resumable `Built` row: the transaction may already /// be propagating, so a retry must not double-spend and a resume must diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs index b880706b839..7e886ea7da6 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs @@ -21,7 +21,6 @@ use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoIn use crate::changeset::changeset::AssetLockChangeSet; use crate::error::PlatformWalletError; use crate::wallet::platform_wallet::PlatformWalletInfo; -use crate::wallet::reservations::record_send_accepted; use super::super::manager::AssetLockManager; use super::super::orchestration::UNCONFIRMED_BROADCAST_PROOF_TIMEOUT; @@ -1175,9 +1174,7 @@ impl AssetLockManager { .expect("Built resumes hold a cleanup-exclusion claim") .preserve_on_drop(); match self.broadcaster.broadcast(&tx).await { - Ok(txid) => { - record_send_accepted(&self.wallet_manager, &self.wallet_id, txid).await; - } + Ok(_) => {} Err(BroadcastError::MaybeSent { reason }) => { tracing::warn!( outpoint = %out_point, @@ -1491,11 +1488,7 @@ impl AssetLockManager { // the status advance), which is precisely why the `Built` arm // above reaches the same verdict from the same position. let mut local_proof = None; - let rebroadcast = self.broadcaster.broadcast(&tx).await; - if let Ok(txid) = &rebroadcast { - record_send_accepted(&self.wallet_manager, &self.wallet_id, *txid).await; - } - if let Err(e) = rebroadcast { + if let Err(e) = self.broadcaster.broadcast(&tx).await { if matches!(e, BroadcastError::Rejected { .. }) { match self.wait_for_proof(out_point, Some(Duration::ZERO)).await { Ok(proof) => { diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs index a008b498967..1879f18346c 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs @@ -156,12 +156,6 @@ impl CoreWallet { // guard, covers check-to-wire. }; let outcome = self.broadcaster.broadcast(transaction).await; - // An accepted send — a peer echoed it back — may fund the next payment - // with its change before the InstantSend lock arrives; an ambiguous one - // may not (`UnresolvedSends`). - if outcome.is_ok() { - self.generation().mark_send_accepted(transaction.txid()); - } // The pin already fences by default, so the inputs stay held on EVERY // exit from the await above — including one this code never observes: // the dispatching future being cancelled, or an unwind, mid-`broadcast`. @@ -290,11 +284,10 @@ impl CoreWallet { &self, transaction: &Transaction, ) -> Result { - let txid = self.broadcaster.broadcast(transaction).await?; - // Accepted: its change may fund the next payment before the - // InstantSend lock arrives (see `UnresolvedSends`). - self.generation().mark_send_accepted(txid); - Ok(txid) + self.broadcaster + .broadcast(transaction) + .await + .map_err(Into::into) } /// Broadcast a signed transaction, reconciling the funding account's UTXO diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 5ba92ce20a3..98d32e2acc1 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -63,7 +63,7 @@ //! are merged into the next. Every host callback comes from that task, never //! from a caller that may hold its own locks. -use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; +use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; use std::mem; use std::panic::{catch_unwind, AssertUnwindSafe}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -86,8 +86,6 @@ use tokio::time::{timeout_at, Instant}; use crate::broadcast_probe::{AcceptanceProbe, ProbeReport, ProbeVerdict, MINED_QUORUM}; use crate::events::{PlatformEventHandler, PlatformEventManager}; -use crate::wallet::core::generation::next_acceptance_seq; -use crate::wallet::core::InBroadcastFences; use crate::wallet::platform_wallet::PlatformWalletInfo; /// Blocks from the start of a root's window (see [`ProbeSchedule`]) during @@ -125,7 +123,6 @@ impl OutgoingView { } /// One account's record of a transaction, as far as the resolver cares. -#[derive(Clone, Copy)] pub(crate) struct RecordFacts<'a> { pub txid: Txid, /// In a block, InstantSend-locked, or finalized in this account. @@ -139,47 +136,25 @@ pub(crate) struct RecordFacts<'a> { /// transaction however many accounts record it; it is settled if any account /// holds it settled, and the wallet's own send if any account records it /// spending the wallet's coins (a transfer between accounts is "incoming" in -/// the receiving one). Returns the unsettled ones, borrowed: for each, whether -/// it spends the wallet's own coins, and the transaction. -/// -/// Two passes over `records()`: the unsettled ones are collected first, then -/// each settled record only drops a matching entry — no set of the whole -/// history is built, since a healthy wallet has none or a handful unsettled. -fn merge_unsettled<'a, I>(records: impl Fn() -> I) -> HashMap -where - I: IntoIterator>, -{ - let mut unsettled: HashMap = HashMap::new(); - for record in records() { - if !record.settled { - unsettled - .entry(record.txid) - .and_modify(|(own, _)| *own |= record.own) - .or_insert((record.own, record.transaction)); - } - } - if !unsettled.is_empty() { - for record in records() { - if record.settled { - unsettled.remove(&record.txid); - } - } - } - unsettled -} - -/// [`merge_unsettled`] over a list of records, as views. -#[cfg(test)] +/// the receiving one). Returns the unsettled ones. pub(crate) fn merge_records<'a>( records: impl IntoIterator>, ) -> Vec { - let records: Vec> = records.into_iter().collect(); - into_views(merge_unsettled(|| records.iter().copied())) -} - -fn into_views(unsettled: HashMap) -> Vec { + let mut settled: HashSet = HashSet::new(); + let mut unsettled: HashMap = HashMap::new(); + for record in records { + if record.settled { + settled.insert(record.txid); + continue; + } + unsettled + .entry(record.txid) + .and_modify(|(own, _)| *own |= record.own) + .or_insert((record.own, record.transaction)); + } unsettled .into_iter() + .filter(|(txid, _)| !settled.contains(txid)) .map(|(txid, (own, transaction))| OutgoingView { txid, spends_own_coins: own, @@ -188,31 +163,22 @@ fn into_views(unsettled: HashMap) -> Vec Vec { - into_views(unsettled_transactions(info)) -} - -/// This wallet's unsettled transactions, borrowed, under [`merge_unsettled`]'s -/// rule — for a reader that walks them on every balance or build call. -pub(crate) fn unsettled_transactions( - info: &PlatformWalletInfo, -) -> HashMap { let accounts = info.core_wallet.accounts.all_accounts(); - merge_unsettled(|| { - accounts.iter().flat_map(|account| { - account - .transactions() - .values() - .map(move |record| RecordFacts { - txid: record.txid, - settled: is_settled(record) || account.transaction_is_finalized(&record.txid), - own: !record.input_details.is_empty(), - transaction: &record.transaction, - }) - }) - }) + merge_records(accounts.iter().flat_map(|account| { + account + .transactions() + .values() + .map(move |record| RecordFacts { + txid: record.txid, + settled: is_settled(record) || account.transaction_is_finalized(&record.txid), + own: !record.input_details.is_empty(), + transaction: &record.transaction, + }) + })) } /// This wallet's unsettled transactions as chains. `views` hold one entry per @@ -950,20 +916,8 @@ pub(crate) trait WalletSource: Send + Sync { walk: &AtomicBool, forced: HashSet, ) -> Option>; - - /// Record what probes found about these sends of the wallet — accepted - /// (`true`) or dead (`false`) — observed at `seq`, so the coins of an - /// accepted send may fund the next payment and those of a dead one may - /// not - /// ([`WalletGeneration::record_acceptance`](crate::wallet::core::WalletGeneration::record_acceptance)). - fn record_acceptance(&self, wallet_id: WalletId, verdicts: Vec<(Txid, bool)>, seq: u64); } -/// Every registered wallet's in-broadcast fences, by id — the manager's map, -/// shared with the resolver so it records a verdict's acceptance fact without -/// the wallet manager's lock. -pub(crate) type FenceRegistry = Arc>>>; - /// Whether any account of the wallet records `txid` — a map lookup per /// account, not a walk over the records. fn holds(info: &PlatformWalletInfo, txid: &Txid) -> bool { @@ -974,28 +928,10 @@ fn holds(info: &PlatformWalletInfo, txid: &Txid) -> bool { .any(|account| account.transactions().contains_key(txid)) } -struct ManagerSource( - Arc>>, - FenceRegistry, -); +struct ManagerSource(Arc>>); #[async_trait] impl WalletSource for ManagerSource { - fn record_acceptance(&self, wallet_id: WalletId, verdicts: Vec<(Txid, bool)>, seq: u64) { - let fences = self - .1 - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .get(&wallet_id) - .cloned(); - let Some(fences) = fences else { - return; - }; - for (txid, accepted) in verdicts { - fences.record_acceptance(txid, accepted, seq); - } - } - async fn holders(&self, txid: Txid) -> Vec { let manager = self.0.read().await; manager @@ -1341,7 +1277,7 @@ impl Actor { } } let events = self.state.forget_wallet(&wallet_id, "rewound"); - self.publish(events); + deliver(&self.sink, events); } let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; @@ -1418,7 +1354,7 @@ impl Actor { } } let events = self.state.settle(wallet_id, &txids); - self.publish(events); + deliver(&self.sink, events); if next_probe { self.probe_next(wallet_id); } @@ -1469,7 +1405,7 @@ impl Actor { } if !enabled { let events = self.state.forget_all(); - self.publish(events); + deliver(&self.sink, events); } } Command::WalletAdded(wallet_id) => { @@ -1489,7 +1425,7 @@ impl Actor { run.abort.abort(); } let events = self.state.forget_wallet(wallet_id, trigger); - self.publish(events); + deliver(&self.sink, events); } /// Start the wallet's pending pass unless one is already running. @@ -1579,7 +1515,7 @@ impl Actor { // events or an Uncertain wake it. entry.wait_until = Some(u32::MAX); let events = self.state.forget_wallet(&wallet_id, "gone"); - self.publish(events); + deliver(&self.sink, events); self.end_run(wallet_id); } Some(None) => self.end_run(wallet_id), @@ -1623,7 +1559,7 @@ impl Actor { }); // Clears and re-published dead verdicts reach the host // before the pass goes out to the network. - self.publish(start.events); + deliver(&self.sink, start.events); self.probe_next(wallet_id); } } @@ -1687,41 +1623,12 @@ impl Actor { &root, &report, ); - self.publish(events); + deliver(&self.sink, events); self.probe_next(wallet_id); } } } - /// Hand `events` to the host — after handing the sends they find accepted - /// (or mined) or dead to their wallets, so the coins of an accepted one - /// are offered to payments again and those of a dead one are held again. - /// Recorded first and synchronously: a host re-reading spendable or max - /// on the verdict sees the figure the verdict implies. Every published - /// verdict goes through here, a re-published dead one included. The - /// sequence number is taken here, in the actor's order, so a later - /// verdict wins. - fn publish(&self, events: Vec) { - let mut verdicts: BTreeMap> = BTreeMap::new(); - for item in &events { - let verdict = match &item.event { - ResolverEvent::Verdict(txid, ProbeVerdict::Accepted | ProbeVerdict::Mined) => { - (*txid, true) - } - ResolverEvent::Verdict(txid, ProbeVerdict::Dead { .. }) => (*txid, false), - _ => continue, - }; - verdicts.entry(item.wallet_id).or_default().push(verdict); - } - if !verdicts.is_empty() { - let seq = next_acceptance_seq(); - for (wallet_id, verdicts) in verdicts { - self.source.record_acceptance(wallet_id, verdicts, seq); - } - } - deliver(&self.sink, events); - } - /// Probe the run's next due root, or end the run. fn probe_next(&mut self, wallet_id: WalletId) { let Some(entry) = self.wallets.get_mut(&wallet_id) else { @@ -1795,9 +1702,7 @@ enum ActorSlot { /// probe sends the signed transaction to evonodes over DAPI, which is a /// product decision, not something a wallet should start doing on upgrade. pub(crate) struct BroadcastResolver { - /// Shared with every wallet's fences: the coins of unresolved sends are - /// held exactly while probing is on (see `WalletGeneration::holds_unresolved_sends`). - enabled: Arc, + enabled: AtomicBool, /// Held while the flag is stored and the switch is queued, so the flag /// and the actor end in the same state under concurrent calls — event /// handlers gate on the flag. @@ -1816,17 +1721,16 @@ impl BroadcastResolver { pub(crate) fn new( probe: Arc, wallet_manager: Arc>>, - fences: FenceRegistry, ) -> Self { let sink = Arc::new(EventSink::default()); let actor = Actor::new( probe, - Arc::new(ManagerSource(wallet_manager, fences)), + Arc::new(ManagerSource(wallet_manager)), Arc::clone(&sink) as Arc, ); let (commands, receiver) = mpsc::unbounded_channel(); Self { - enabled: Arc::new(AtomicBool::new(false)), + enabled: AtomicBool::new(false), commands, started: AtomicBool::new(false), actor: Mutex::new(ActorSlot::NotStarted(Box::new(actor), receiver)), @@ -1882,11 +1786,6 @@ impl BroadcastResolver { self.enabled.load(Ordering::SeqCst) } - /// The switch itself, for the wallets whose held coins follow it. - pub(crate) fn enabled_flag(&self) -> Arc { - Arc::clone(&self.enabled) - } - /// Forget a removed wallet — call after it has left the wallet manager: /// its pass in flight, its entry and every verdict, each of which the host /// is told to drop (from the resolver's task, not the caller's). @@ -2787,8 +2686,6 @@ mod tests { /// Reads wait while set, so a test can hold a pass in its read. paused: StdMutex, resume: Notify, - /// Acceptance recorded per wallet: (wallet, txid, accepted, seq). - acceptance: StdMutex>, } impl FakeSource { @@ -2812,15 +2709,6 @@ mod tests { #[async_trait] impl WalletSource for FakeSource { - fn record_acceptance(&self, wallet_id: WalletId, verdicts: Vec<(Txid, bool)>, seq: u64) { - let mut acceptance = self.acceptance.lock().expect("acceptance"); - acceptance.extend( - verdicts - .into_iter() - .map(|(txid, accepted)| (wallet_id, txid, accepted, seq)), - ); - } - async fn holders(&self, txid: Txid) -> Vec { self.views .lock() @@ -2860,28 +2748,10 @@ mod tests { } #[derive(Default)] - struct CollectSink( - StdMutex>, - /// The rig's source, to check each verdict was recorded before it - /// reached the host. - std::sync::OnceLock>, - /// Verdicts delivered before their wallet recorded them. - StdMutex>, - ); + struct CollectSink(StdMutex>); impl VerdictSink for CollectSink { fn deliver(&self, item: &Outgoing) { - if let (ResolverEvent::Verdict(txid, verdict), Some(source)) = - (&item.event, self.1.get()) - { - let records = !matches!(verdict, ProbeVerdict::Unresolved { .. }); - let recorded = source.acceptance.lock().expect("acceptance").iter().any( - |(wallet_id, recorded, _, _)| *wallet_id == item.wallet_id && recorded == txid, - ); - if records && !recorded { - self.2.lock().expect("unrecorded").push(*txid); - } - } self.0.lock().expect("sink").push(item.event.clone()); } } @@ -2899,7 +2769,6 @@ mod tests { fn with_sink(probe: Arc, sink: Arc) -> Self { let source = Arc::new(FakeSource::default()); - let _ = sink.1.set(Arc::clone(&source)); let actor = Actor::new( probe, Arc::clone(&source) as Arc, @@ -2994,127 +2863,6 @@ mod tests { assert!(rig.sent().is_empty()); } - /// A probe's verdicts reach the wallet: accepted releases a send's coins, - /// dead holds them again; an unresolved verdict records nothing. - #[tokio::test] - async fn should_record_accepted_and_dead_verdicts_and_not_unresolved_ones() { - let probe = Arc::new(ScriptedProbe::new(&[ - (txid(1), ProbeVerdict::Accepted), - (txid(2), unresolved()), - (txid(3), dead()), - ])); - let mut rig = enabled(probe).await; - rig.views( - wallet(), - vec![ - send(1, &[outpoint(90, 0)]), - send(2, &[outpoint(91, 0)]), - send(3, &[outpoint(92, 0)]), - ], - ); - for n in 1..=3 { - rig.send(Command::Uncertain(txid(n))).await; - } - - let recorded: Vec<(WalletId, Txid, bool)> = rig - .source - .acceptance - .lock() - .expect("acceptance") - .iter() - .map(|(wallet_id, txid, accepted, _)| (*wallet_id, *txid, *accepted)) - .collect(); - assert!( - recorded.contains(&(wallet(), txid(1), true)), - "{recorded:?}" - ); - assert!( - recorded.contains(&(wallet(), txid(3), false)), - "{recorded:?}" - ); - assert!( - !recorded.iter().any(|(_, recorded, _)| *recorded == txid(2)), - "{recorded:?}" - ); - } - - /// A verdict is recorded on the wallet before it reaches the host, so a - /// host re-reading spendable or max on the callback sees the new figure. - #[tokio::test] - async fn should_record_a_verdict_before_delivering_it() { - let probe = Arc::new(ScriptedProbe::new(&[ - (txid(1), ProbeVerdict::Accepted), - (txid(2), dead()), - ])); - let mut rig = enabled(probe).await; - rig.views( - wallet(), - vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], - ); - - rig.send(Command::Uncertain(txid(1))).await; - rig.send(Command::Uncertain(txid(2))).await; - - let delivered = rig.sent(); - assert!( - delivered.contains(&ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)), - "{delivered:?}" - ); - assert!( - delivered.contains(&ResolverEvent::Verdict(txid(2), dead())), - "{delivered:?}" - ); - let unrecorded = rig.sink.2.lock().expect("unrecorded").clone(); - assert!( - unrecorded.is_empty(), - "delivered before recorded: {unrecorded:?}" - ); - } - - /// A dead verdict re-published to a send built on a dead root — at the - /// start of a pass, without a probe — is recorded on the wallet too. - #[tokio::test] - async fn should_record_a_dead_verdict_republished_to_a_later_child() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), dead())])); - let mut rig = enabled(probe).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.send(Command::Uncertain(txid(1))).await; - rig.source.acceptance.lock().expect("acceptance").clear(); - - // The new send's record wakes the idle wallet, as the event adapter - // reports it; the next block starts a pass. - rig.views( - wallet(), - vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])], - ); - rig.send(Command::Seen { - wallet_id: wallet(), - touched: true, - }) - .await; - rig.follow(wallet(), 100).await; - let delivered = rig.sent(); - assert!( - delivered.contains(&ResolverEvent::Verdict(txid(2), dead())), - "{delivered:?}" - ); - - let recorded: Vec<(Txid, bool)> = rig - .source - .acceptance - .lock() - .expect("acceptance") - .iter() - .map(|(_, txid, accepted, _)| (*txid, *accepted)) - .collect(); - assert!(recorded.contains(&(txid(2), false)), "{recorded:?}"); - let unrecorded = rig.sink.2.lock().expect("unrecorded").clone(); - assert!( - unrecorded.is_empty(), - "delivered before recorded: {unrecorded:?}" - ); - } - /// An `Uncertain` result carries no wallet id: every wallet hears it, but /// only the one holding the transaction walks its records. #[tokio::test] diff --git a/packages/rs-platform-wallet/src/wallet/core/generation.rs b/packages/rs-platform-wallet/src/wallet/core/generation.rs index 021f77192e4..5224985f838 100644 --- a/packages/rs-platform-wallet/src/wallet/core/generation.rs +++ b/packages/rs-platform-wallet/src/wallet/core/generation.rs @@ -3,13 +3,11 @@ //! in-broadcast outpoint pins that fence a mid-dispatch transaction's inputs //! against concurrent re-selection. -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use std::ops::Deref; -use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; -use std::time::{Duration, Instant}; -use dashcore::{OutPoint, Transaction, Txid}; +use dashcore::{OutPoint, Transaction}; use tokio::sync::{OwnedRwLockWriteGuard, RwLock, RwLockReadGuard}; use super::balance::WalletBalance; @@ -257,96 +255,14 @@ impl std::fmt::Debug for SettleBoundaryHook { #[derive(Debug, Default)] pub(crate) struct InBroadcastFences { fences: Mutex>, - /// What the network was last seen to say about our unsettled sends: a - /// peer echoed the broadcast back or a probe found it accepted or mined - /// (accepted), or a probe found it dead. Only an accepted send's outputs - /// may fund the next payment before the wallet sees it InstantSend-locked - /// or mined (see [`WalletGeneration::acceptance_among`]). - /// - /// Shared like the fences: acceptance is a fact about the network, not - /// about one in-memory instance. A fact is kept while its send is among - /// the wallet's unsettled transactions, and for [`ACCEPTANCE_GRACE`] after - /// it was observed otherwise — a broadcast can return before its record - /// reaches the wallet. Not persisted: after a restart an unsettled send is - /// held again until its lock, a block or a fresh verdict, which only errs - /// on the side of not spending. - acceptance: Mutex>, - /// Whether the coins of unresolved sends are held at all - /// ([`WalletGeneration::holds_unresolved_sends`]): the manager's - /// broadcast-probe switch, shared. Without probing nothing would ever find - /// such a send dead or mined beyond its own lock or block, so with it off - /// nothing is held and building behaves as it did before the hold. - hold_unresolved: Arc, -} - -/// How long an acceptance fact outlives its send's absence from the wallet's -/// unsettled records: covers a broadcast that returns before the wallet has -/// applied the transaction. -const ACCEPTANCE_GRACE: Duration = Duration::from_secs(10 * 60); - -/// The last word on one send, and when it was observed — see -/// [`next_acceptance_seq`]. A later observation replaces an earlier one, so a -/// send found dead after it was found accepted is held again. -#[derive(Debug, Clone, Copy)] -struct Acceptance { - seq: u64, - accepted: bool, - observed_at: Instant, -} - -static ACCEPTANCE_SEQ: AtomicU64 = AtomicU64::new(1); - -/// A sequence number for an acceptance fact, taken where the fact is observed -/// (a broadcast returning, a probe verdict published). Facts are recorded off -/// the observing task, so they can land out of order; the number keeps the -/// later one. -pub(crate) fn next_acceptance_seq() -> u64 { - ACCEPTANCE_SEQ.fetch_add(1, Ordering::Relaxed) } impl InBroadcastFences { - /// Fences for one wallet that hold the coins of its unresolved sends - /// whenever `hold_unresolved` — the manager's probe switch — is on. - pub(crate) fn with_hold_flag(hold_unresolved: Arc) -> Self { - Self { - hold_unresolved, - ..Self::default() - } - } - /// Recovers from a poisoned mutex rather than panicking — see /// [`WalletGeneration::in_broadcast_lock`]. fn lock(&self) -> MutexGuard<'_, HashMap> { self.fences.lock().unwrap_or_else(PoisonError::into_inner) } - - /// See [`WalletGeneration::record_acceptance`]. On the fences, so the - /// broadcast resolver can record a verdict without the wallet manager's - /// lock — synchronously, before the verdict reaches the host. - pub(crate) fn record_acceptance(&self, txid: Txid, accepted: bool, seq: u64) { - let mut acceptance = self.acceptance(); - if !self.hold_unresolved.load(Ordering::SeqCst) { - acceptance.clear(); - return; - } - let newer = acceptance.get(&txid).is_none_or(|known| known.seq < seq); - if newer { - acceptance.insert( - txid, - Acceptance { - seq, - accepted, - observed_at: Instant::now(), - }, - ); - } - } - - fn acceptance(&self) -> MutexGuard<'_, HashMap> { - self.acceptance - .lock() - .unwrap_or_else(PoisonError::into_inner) - } } /// One outpoint's broadcast fence — see `WalletGeneration::in_broadcast`. @@ -481,63 +397,6 @@ impl WalletGeneration { } } - /// Record what the network said about our send `txid` (accepted, or - /// found dead), observed at `seq` ([`next_acceptance_seq`]). An older - /// observation than the one recorded is dropped. Nothing is kept while the - /// hold is off: only the hold reads these facts, and only its reads prune - /// them. - pub(crate) fn record_acceptance(&self, txid: Txid, accepted: bool, seq: u64) { - self.in_broadcast.record_acceptance(txid, accepted, seq); - } - - /// A broadcast of `txid` returned accepted: a peer echoed it back. - pub(crate) fn mark_send_accepted(&self, txid: Txid) { - self.record_acceptance(txid, true, next_acceptance_seq()); - } - - /// Whether the coins of this wallet's unresolved sends are held from new - /// payments — on exactly while the manager probes unresolved broadcasts. - pub(crate) fn holds_unresolved_sends(&self) -> bool { - self.in_broadcast.hold_unresolved.load(Ordering::SeqCst) - } - - /// Turn the hold on or off for this wallet alone, as the manager's probe - /// switch would. - #[cfg(test)] - pub(crate) fn set_holds_unresolved_sends(&self, on: bool) { - self.in_broadcast - .hold_unresolved - .store(on, Ordering::SeqCst); - } - - /// The latest fact recorded about each send among `unsettled`: whether - /// the network was seen to accept it, and the sequence it was observed at. - /// Forgets a recorded send that is not among them once - /// [`ACCEPTANCE_GRACE`] has passed since it was observed — settled or gone - /// by then — so the record stays about the size of the wallet's unsettled - /// sends, yet a fact that arrived before its send's record is kept. - pub(crate) fn acceptance_among(&self, unsettled: &HashSet) -> HashMap { - let mut acceptance = self.in_broadcast.acceptance(); - acceptance.retain(|txid, known| { - unsettled.contains(txid) || known.observed_at.elapsed() < ACCEPTANCE_GRACE - }); - acceptance - .iter() - .filter(|(txid, _)| unsettled.contains(*txid)) - .map(|(txid, known)| (*txid, (known.accepted, known.seq))) - .collect() - } - - /// The sends among `unsettled` whose latest fact is accepted. - #[cfg(test)] - pub(crate) fn accepted_among(&self, unsettled: &HashSet) -> HashSet { - self.acceptance_among(unsettled) - .into_iter() - .filter(|(_, (accepted, _))| *accepted) - .map(|(txid, _)| txid) - .collect() - } - /// This generation's lock-free balance. pub fn balance(&self) -> &WalletBalance { &self.balance diff --git a/packages/rs-platform-wallet/src/wallet/core/mod.rs b/packages/rs-platform-wallet/src/wallet/core/mod.rs index ec5da49cef3..2b05316555b 100644 --- a/packages/rs-platform-wallet/src/wallet/core/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/core/mod.rs @@ -8,7 +8,6 @@ mod sign_message; pub mod spend_observer; pub(crate) use sign_message::is_signable_funding_account; mod transaction; -mod unresolved; pub mod wallet; pub use balance::WalletBalance; @@ -16,9 +15,6 @@ pub use balance_handler::BalanceUpdateHandler; pub use generation::WalletGeneration; pub(crate) use generation::{InBroadcastFences, InBroadcastPin}; pub use spend_observer::SpendObservationHandler; -pub(crate) use transaction::{ - awaiting_network_or_build_error, resolve_source_accounts, shortfall_basis, -}; +pub(crate) use transaction::resolve_source_accounts; pub use transaction::{SignedCoreTransaction, ASSET_LOCK_FUNDING_SOURCES, SEND_FUNDING_SOURCES}; -pub(crate) use unresolved::UnresolvedSends; pub use wallet::CoreWallet; diff --git a/packages/rs-platform-wallet/src/wallet/core/transaction.rs b/packages/rs-platform-wallet/src/wallet/core/transaction.rs index 715f163b182..de4bdb692c0 100644 --- a/packages/rs-platform-wallet/src/wallet/core/transaction.rs +++ b/packages/rs-platform-wallet/src/wallet/core/transaction.rs @@ -12,7 +12,7 @@ use dashcore::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction}; use key_wallet::account::AccountType; use key_wallet::managed_account::managed_account_collection::ManagedAccountCollection; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; -use key_wallet::wallet::managed_wallet_info::coin_selection::{SelectionError, SelectionStrategy}; +use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionError; use key_wallet::wallet::managed_wallet_info::fee::{estimate_tx_size, FeeRate}; use key_wallet::wallet::managed_wallet_info::transaction_builder::{ BuilderError, TransactionBuilder, TransactionSigner, @@ -22,7 +22,6 @@ use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoIn use key_wallet::wallet::Wallet; use key_wallet::{DerivationPath, ReservationToken, Utxo}; -use super::unresolved::UnresolvedSends; use super::{CoreWallet, WalletGeneration}; use crate::broadcaster::TransactionBroadcaster; use crate::wallet::reservations::reservation_expired; @@ -42,11 +41,8 @@ enum FundingContext<'a> { Pooled(&'a [AccountTypePreference]), } -/// The `(available, required)` of a build that failed for want of funds; -/// `None` for any other failure. `required` is `None` when selection had -/// nothing to choose from and so never sized the shortfall. -fn shortfall(error: &BuilderError) -> Option<(Option, Option)> { - match error { +fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> PlatformWalletError { + let funds = match error { BuilderError::InsufficientFunds { available, required, @@ -54,14 +50,11 @@ fn shortfall(error: &BuilderError) -> Option<(Option, Option)> { | BuilderError::CoinSelection(SelectionError::InsufficientFunds { available, required, - }) => Some((Some(*available), Some(*required))), + }) => Some((Some(available), Some(required))), BuilderError::CoinSelection(SelectionError::NoUtxosAvailable) => Some((Some(0), None)), _ => None, - } -} - -fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> PlatformWalletError { - if let Some((available, required)) = shortfall(&error) { + }; + if let Some((available, required)) = funds { return match context { FundingContext::Single { preference, index } => { PlatformWalletError::CoreInsufficientFunds { @@ -81,99 +74,6 @@ fn map_builder_error(error: BuilderError, context: FundingContext<'_>) -> Platfo PlatformWalletError::TransactionBuild(error.to_string()) } -/// What telling a shortfall the held coins caused needs from a build: the fee -/// one input adds at the build's own rate (what spending a held coin costs) -/// and what selection must cover before any input — the outputs plus the fee -/// of a transaction carrying them and a change output. -#[derive(Debug, Clone, Copy)] -pub(crate) struct ShortfallBasis { - pub(crate) input_cost: u64, - pub(crate) requested: u64, -} - -/// The [`ShortfallBasis`] of `builder`, or `None` when its shortfall cannot be -/// told apart — a drain (`SelectionStrategy::All`, whose output amount is a -/// placeholder), a special-payload build (whose own outputs and size -/// `outputs()` does not show), or a fee rate that overflows — and stays -/// insufficient funds. Infallible, so a caller may take it at any point. -pub(crate) fn shortfall_basis(builder: &TransactionBuilder) -> Option { - if matches!(builder.selection_strategy(), SelectionStrategy::All) - || builder.has_special_payload() - { - return None; - } - let fee_rate = builder.fee_rate(); - let outputs = builder.outputs(); - let total = outputs - .iter() - .try_fold(0u64, |total, output| total.checked_add(output.value))?; - let base = checked_fee(fee_rate, estimate_tx_size(0, outputs.len() + 1, false)).ok()?; - Some(ShortfallBasis { - input_cost: input_fee(fee_rate).ok()?, - requested: total.checked_add(base)?, - }) -} - -/// The size one more input adds, from key-wallet's estimator by difference — -/// the one model [`CoreWallet::pooled_max_sendable`] and [`shortfall_basis`] -/// both price inputs with. -fn per_input_size() -> usize { - estimate_tx_size(1, 1, false).saturating_sub(estimate_tx_size(0, 1, false)) -} - -/// The fee one more input adds at `fee_rate`. -fn input_fee(fee_rate: FeeRate) -> Result { - checked_fee(fee_rate, per_input_size()) -} - -/// [`PlatformWalletError::CoreFundsAwaitingNetwork`] when the held coins of -/// an unresolved send (see [`UnresolvedSends`]) would cover a shortfall: `held` -/// is what they would add net of their own inputs' fee -/// ([`UnresolvedSends::held_net_value`]); a shortfall selection did not size -/// is measured against the basis' `requested`. `None` — a genuine shortfall, -/// held coins or not — otherwise. -fn awaiting( - available: Option, - required: Option, - held: u64, - basis: Option, -) -> Option { - let basis = basis?; - let required = required.unwrap_or(basis.requested); - let covered = available.unwrap_or(0).saturating_add(held) >= required; - (held > 0 && covered).then_some(PlatformWalletError::CoreFundsAwaitingNetwork { - available, - held, - required: Some(required), - outpoint: None, - }) -} - -/// `error`, or [`PlatformWalletError::CoreFundsAwaitingNetwork`] when it is a -/// shortfall the held coins would have covered (see [`awaiting`]). A genuine -/// shortfall — held coins or not, the funds are not there — stays -/// insufficient funds. -pub(crate) fn awaiting_network( - error: PlatformWalletError, - held: u64, - basis: Option, -) -> PlatformWalletError { - let (available, required) = match &error { - PlatformWalletError::CoreInsufficientFunds { - available, - required, - .. - } - | PlatformWalletError::CorePooledInsufficientFunds { - available, - required, - .. - } => (*available, *required), - _ => return error, - }; - awaiting(available, required, held, basis).unwrap_or(error) -} - /// key-wallet's `FeeRate::calculate_fee`, with the multiplication checked. /// /// Upstream computes `(sat_per_kb * size_bytes).div_ceil(1000)` unchecked, and @@ -196,20 +96,6 @@ fn checked_fee(fee_rate: FeeRate, size_bytes: usize) -> Result, -) -> PlatformWalletError { - shortfall(&error) - .and_then(|(available, required)| awaiting(available, required, held, basis)) - .unwrap_or_else(|| PlatformWalletError::TransactionBuild(error.to_string())) -} - /// The output [`CoreWallet::pooled_max_sendable`] prices: one P2PKH, matching /// the `estimate_tx_size(_, 1, false)` model it sizes the transaction with. /// @@ -555,7 +441,6 @@ impl CoreWallet { .get_wallet_and_info(&self.wallet_id) .ok_or_else(|| PlatformWalletError::WalletNotFound("wallet not found".into()))?; let height = info.core_wallet.last_processed_height(); - let unresolved = UnresolvedSends::of(info); // Same resolver, same strictness rule, as the funding path. let resolved = resolved_funding_accounts( @@ -574,7 +459,6 @@ impl CoreWallet { total += managed .spendable_utxos(height) .iter() - .filter(|utxo| !unresolved.holds(&utxo.outpoint)) .map(|utxo| utxo.value()) .sum::(); } @@ -620,15 +504,15 @@ impl CoreWallet { let fee_rate = fee_rate.unwrap_or_else(FeeRate::normal); // Per-input and per-output costs derived from key-wallet's estimator by // difference, so this crate never re-declares a size that could drift - // from the one the build charges (`input_fee` / `per_input_size`). + // from the one the build charges. let empty = estimate_tx_size(0, 1, false); + let per_input = estimate_tx_size(1, 1, false).saturating_sub(empty); let manager = self.wallet_manager.read().await; let (wallet, info) = manager .get_wallet_and_info(&self.wallet_id) .ok_or_else(|| PlatformWalletError::WalletNotFound("wallet not found".into()))?; let height = info.core_wallet.last_processed_height(); - let unresolved = UnresolvedSends::of(info); let resolved = resolved_funding_accounts( &info.core_wallet.accounts, @@ -640,7 +524,7 @@ impl CoreWallet { // A UTXO earns its place only if it brings in more than its own input // costs at this rate; the rest are dead weight and are dropped. - let input_cost = input_fee(fee_rate)?; + let input_cost = checked_fee(fee_rate, per_input)?; let mut values: Vec = Vec::new(); for at in resolved { let Some(managed) = info.core_wallet.accounts.funds_account(&at) else { @@ -650,7 +534,6 @@ impl CoreWallet { managed .spendable_utxos(height) .iter() - .filter(|utxo| !unresolved.holds(&utxo.outpoint)) .map(|utxo| utxo.value()) .filter(|value| *value > input_cost), ); @@ -760,9 +643,6 @@ impl CoreWallet { .ok_or_else(|| PlatformWalletError::WalletNotFound("wallet not found".into()))?; let height = info.core_wallet.last_processed_height(); - let unresolved = UnresolvedSends::of(info); - let basis = shortfall_basis(&builder); - let mut held_value = 0u64; // Fund from every resolved account, mirroring key-wallet's own // multi-source fold (`transaction_building::fund`): dedup overlapping @@ -811,20 +691,11 @@ impl CoreWallet { paths.insert(utxo.address.clone(), path); } } - // `reservation_only` offers no candidates of its own: the - // caller seeded its inputs explicitly, so held coins could - // never fund it and count for nothing in a shortfall. Either - // way the held coins are excluded — a seeded one fails the - // build. - if let (false, Some(basis)) = (reservation_only, basis) { - held_value += unresolved.held_net_value(managed, height, basis.input_cost); - } builder = if reservation_only { builder.add_funding_reservation_only(managed, account) } else { builder.add_funding(managed, account) }; - builder = unresolved.exclude_from(builder, managed); offered_accounts.push(at); } if offered_accounts.is_empty() { @@ -841,26 +712,9 @@ impl CoreWallet { } else { FundingContext::Pooled(sources) }; - let (unsigned, fee, reservation_token) = - builder - .build_unsigned_reserved() - .map_err(|error| match error { - // A coin the caller seeded is held: refused before - // anything was reserved. - BuilderError::ExcludedInput { outpoint } => { - PlatformWalletError::CoreFundsAwaitingNetwork { - available: None, - held: held_value, - required: None, - outpoint: Some(outpoint), - } - } - error => awaiting_network( - map_builder_error(error, funding_context), - held_value, - basis, - ), - })?; + let (unsigned, fee, reservation_token) = builder + .build_unsigned_reserved() + .map_err(|error| map_builder_error(error, funding_context))?; // Release across every contributing account on the error paths // below: the pooled reservation lives per account under the one @@ -1149,43 +1003,30 @@ impl CoreWallet { #[cfg(test)] mod tests { - use std::collections::HashSet; use std::sync::Arc; use async_trait::async_trait; - use dashcore::hashes::Hash; use dashcore::secp256k1::{ecdsa, PublicKey}; - use dashcore::{Address as DashAddress, BlockHash, Network, OutPoint, Transaction, Txid}; + use dashcore::{Address as DashAddress, Network}; use key_wallet::account::account_type::StandardAccountType; - use key_wallet::account::AccountType; use key_wallet::signer::{Signer, SignerMethod}; - use key_wallet::transaction_checking::{BlockInfo, TransactionContext}; use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionStrategy; use key_wallet::wallet::managed_wallet_info::transaction_builder::TransactionBuilder; use key_wallet::wallet::managed_wallet_info::transaction_building::AccountTypePreference; - use key_wallet::{DerivationPath, Utxo}; - use key_wallet_manager::WalletManager; - use tokio::sync::{Barrier, RwLock}; + use key_wallet::DerivationPath; + use tokio::sync::Barrier; use crate::broadcaster::TransactionBroadcaster; - use crate::manager::accessors::account_utxo_snapshots; use crate::test_support::{ - apply_to_wallet, funded_wallet_manager, funded_wallet_manager_dual_standard, + funded_wallet_manager, funded_wallet_manager_dual_standard, funded_wallet_manager_with_contact, AlwaysMaybeSentBroadcaster, AlwaysOkBroadcaster, AlwaysRejectedBroadcaster, WalletSigner, }; use key_wallet::wallet::managed_wallet_info::fee::{estimate_tx_size, FeeRate}; - use crate::wallet::core::generation::next_acceptance_seq; - use crate::wallet::core::transaction::{ - awaiting_network, awaiting_network_or_build_error, input_fee, modeled_output_script, - shortfall_basis, ShortfallBasis, MAX_STANDARD_TX_INPUTS, - }; - use crate::wallet::core::{CoreWallet, UnresolvedSends, WalletGeneration}; - use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; - use crate::{PlatformWalletError, SEND_FUNDING_SOURCES}; - use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionError; - use key_wallet::wallet::managed_wallet_info::transaction_builder::BuilderError; + use crate::wallet::core::transaction::{modeled_output_script, MAX_STANDARD_TX_INPUTS}; + use crate::wallet::core::CoreWallet; + use crate::PlatformWalletError; fn preference(account_type: StandardAccountType) -> AccountTypePreference { match account_type { @@ -2166,792 +2007,4 @@ mod tests { Err(PlatformWalletError::CoreInsufficientFunds { .. }) )); } - - // ---- Coins of a send the network has not been seen to accept ----------- - - type Manager = Arc>>; - - /// BIP44 and BIP32 each funded with one confirmed 700 000-duff coin. - async fn dual_core( - broadcaster: Arc, - ) -> ( - CoreWallet, - Manager, - WalletId, - WalletSigner, - ) { - let (manager, wallet_id, generation, signer) = - funded_wallet_manager_dual_standard(&[700_000], &[700_000]).await; - // As a manager with broadcast probing on: the hold follows it. - generation.set_holds_unresolved_sends(true); - let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); - let core = CoreWallet::new( - sdk, - Arc::clone(&manager), - wallet_id, - broadcaster, - generation, - ); - (core, manager, wallet_id, signer) - } - - /// Sign a send of `amount` to `to` and put it in the wallet as SPV would - /// on seeing it in a peer's mempool — never echoed back, so its outcome is - /// unknown: the shape support ticket 32347 left behind. - async fn unresolved_send( - core: &CoreWallet, - manager: &Manager, - wallet_id: &WalletId, - signer: &WalletSigner, - to: &DashAddress, - amount: u64, - ) -> Transaction { - let finalized = core - .finalize_transaction( - TransactionBuilder::new().add_output(to, amount), - &SEND_FUNDING_SOURCES, - 0, - signer, - ) - .await - .expect("the send builds"); - let transaction = finalized.transaction().clone(); - apply_to_wallet( - manager, - wallet_id, - &transaction, - TransactionContext::Mempool, - ) - .await; - transaction - } - - async fn spendable(core: &CoreWallet) -> u64 { - core.pooled_spendable_balance(&SEND_FUNDING_SOURCES, 0) - .await - .expect("spendable balance") - } - - async fn utxo(manager: &Manager, wallet_id: &WalletId, outpoint: OutPoint) -> Utxo { - let manager = manager.read().await; - let info = manager.get_wallet_info(wallet_id).expect("wallet"); - info.core_wallet - .accounts - .all_funding_accounts() - .into_iter() - .find_map(|managed| managed.utxos.get(&outpoint).cloned()) - .expect("the wallet holds the coin") - } - - async fn record_acceptance( - manager: &Manager, - wallet_id: &WalletId, - txid: Txid, - accepted: bool, - ) { - let manager = manager.read().await; - let info = manager.get_wallet_info(wallet_id).expect("wallet"); - info.generation - .record_acceptance(txid, accepted, next_acceptance_seq()); - } - - fn change_of(transaction: &Transaction, paid: &DashAddress) -> OutPoint { - let vout = transaction - .output - .iter() - .position(|output| output.script_pubkey != paid.script_pubkey()) - .expect("a change output"); - OutPoint { - txid: transaction.txid(), - vout: vout as u32, - } - } - - /// With broadcast probing off nothing is held: building behaves exactly as - /// it did before the hold — nothing beyond a lock or a block would ever - /// release a held send, so a record stuck in mempool context would freeze - /// its coins for good. - #[tokio::test] - async fn should_hold_nothing_while_probing_is_off() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - { - let manager = manager.read().await; - let info = manager.get_wallet_info(&wallet_id).expect("wallet"); - info.generation.set_holds_unresolved_sends(false); - } - let paid = DashAddress::dummy(Network::Testnet, 80); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; - - assert_eq!( - spendable(&core).await, - 700_000 + change.value(), - "the change counts as before" - ); - core.finalize_transaction( - TransactionBuilder::new().add_output(&paid, 700_000 + change.value() / 2), - &SEND_FUNDING_SOURCES, - 0, - &signer, - ) - .await - .expect("the change funds a payment as before"); - } - - /// The coins of an unresolved send are not spendable: left out of the - /// spendable figure, of "max", and of selection. A payment only they would - /// cover is refused as waiting on the network, not as a shortfall. - #[tokio::test] - async fn should_hold_the_outputs_of_an_unresolved_send() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 80); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; - - assert_eq!(spendable(&core).await, 700_000, "only the untouched coin"); - let max = core - .pooled_max_sendable(&SEND_FUNDING_SOURCES, 0, None) - .await - .expect("max sendable"); - assert!( - max > 0 && max < 700_000, - "max draws on the untouched coin only, got {max}" - ); - - let refused = core - .finalize_transaction(payment_builder(81), &SEND_FUNDING_SOURCES, 0, &signer) - .await; - assert!( - matches!( - refused, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { - available: Some(700_000), - held, - outpoint: None, - .. - }) if held == change.value() - input_fee(FeeRate::normal()).expect("fee") - ), - "got {refused:?}" - ); - - let small = TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 82), 300_000); - let finalized = core - .finalize_transaction(small, &SEND_FUNDING_SOURCES, 0, &signer) - .await - .expect("the untouched coin covers it"); - assert!(finalized - .transaction() - .input - .iter() - .all(|input| input.previous_output.txid != send.txid())); - core.abandon_transaction(&finalized).await; - } - - /// Held coins or not, a payment the wallet cannot cover is a shortfall. - #[tokio::test] - async fn should_report_a_genuine_shortfall_as_insufficient_funds() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 83); - unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - let too_much = TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 84), 5_000_000); - - let refused = core - .finalize_transaction(too_much, &SEND_FUNDING_SOURCES, 0, &signer) - .await; - assert!( - matches!( - refused, - Err(PlatformWalletError::CorePooledInsufficientFunds { .. }) - ), - "got {refused:?}" - ); - } - - /// The hold is not a lock left behind: after a build the held coin is - /// still unlocked in the wallet. - #[tokio::test] - async fn should_leave_no_lock_on_the_wallet() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 85); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - let small = TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 86), 300_000); - let finalized = core - .finalize_transaction(small, &SEND_FUNDING_SOURCES, 0, &signer) - .await - .expect("the untouched coin covers it"); - core.abandon_transaction(&finalized).await; - - let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; - assert!(!change.is_locked); - } - - /// Accepted by the network, the send's coins are offered again. - #[tokio::test] - async fn should_offer_the_outputs_once_the_send_is_accepted() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 87); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; - - record_acceptance(&manager, &wallet_id, send.txid(), true).await; - - assert_eq!(spendable(&core).await, 700_000 + change.value()); - } - - /// Found dead after it was found accepted — evicted, or beaten by a - /// conflicting spend — the send's coins are held again. An observation - /// older than the last one recorded changes nothing. - #[tokio::test] - async fn should_hold_again_when_an_accepted_send_is_found_dead() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 88); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - let early = next_acceptance_seq(); - - record_acceptance(&manager, &wallet_id, send.txid(), true).await; - record_acceptance(&manager, &wallet_id, send.txid(), false).await; - assert_eq!(spendable(&core).await, 700_000); - - // A late-landing record of an earlier "accepted" loses to "dead". - { - let manager = manager.read().await; - let info = manager.get_wallet_info(&wallet_id).expect("wallet"); - info.generation.record_acceptance(send.txid(), true, early); - } - assert_eq!(spendable(&core).await, 700_000); - } - - /// Settled — here in a block — the send's coins are ordinary funds. - #[tokio::test] - async fn should_offer_the_outputs_once_the_send_is_mined() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 89); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; - - apply_to_wallet( - &manager, - &wallet_id, - &send, - TransactionContext::InBlock(BlockInfo::new(2, BlockHash::all_zeros(), 1_700_000_100)), - ) - .await; - - assert_eq!(spendable(&core).await, 700_000 + change.value()); - } - - /// A transfer to our own receive address is a send like any other: its - /// receiving side is as phantom as its change if it never left. - #[tokio::test] - async fn should_hold_the_receiving_side_of_an_internal_transfer() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let own_receive = utxo_addresses(&manager, &wallet_id).await; - let send = - unresolved_send(&core, &manager, &wallet_id, &signer, &own_receive, 100_000).await; - - let received = OutPoint { - txid: send.txid(), - vout: send - .output - .iter() - .position(|output| output.script_pubkey == own_receive.script_pubkey()) - .expect("the transfer output") as u32, - }; - assert_eq!(utxo(&manager, &wallet_id, received).await.value(), 100_000); - assert_eq!( - spendable(&core).await, - 700_000, - "neither side of the transfer" - ); - } - - /// A send built on an accepted send is held again, with all its coins, - /// once the one under it is found dead: a node that never saw the parent - /// refuses the child. - #[tokio::test] - async fn should_hold_a_send_built_on_one_found_dead() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 90); - let parent = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - record_acceptance(&manager, &wallet_id, parent.txid(), true).await; - // More than the untouched coin: it must spend the parent's change too. - let child = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 1_000_000).await; - assert!(child - .input - .iter() - .any(|input| input.previous_output.txid == parent.txid())); - record_acceptance(&manager, &wallet_id, child.txid(), true).await; - assert!( - spendable(&core).await > 0, - "the child's change, both accepted" - ); - - record_acceptance(&manager, &wallet_id, parent.txid(), false).await; - - assert_eq!( - spendable(&core).await, - 0, - "the child's change goes with its parent" - ); - } - - /// A held coin the caller chose itself as an input — outpoint-seeded or - /// `reservation_only` builds skip selection — is refused, naming it. - #[tokio::test] - async fn should_refuse_a_held_coin_chosen_as_an_input() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 91); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - let held = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; - - let refused = core - .finalize_transaction_with_options( - TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 92), 100_000) - .add_inputs([held.clone()]), - &[AccountTypePreference::BIP44], - 0, - &signer, - true, - ) - .await; - assert!( - matches!( - refused, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { - outpoint: Some(outpoint), - .. - }) if outpoint == held.outpoint - ), - "got {refused:?}" - ); - // The message names it too: the FFI carries only the message. - let message = refused.expect_err("refused").to_string(); - let named = format!( - "refused input {}:{}", - held.outpoint.txid, held.outpoint.vout - ); - assert!(message.contains(&named), "{message}"); - } - - /// The account UTXO listing a host does coin control from flags the held - /// coin, and only it. - #[tokio::test] - async fn should_flag_held_coins_in_the_account_utxo_listing() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 94); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - let held = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; - - let listing = { - let manager = manager.read().await; - let info = manager.get_wallet_info(&wallet_id).expect("wallet"); - account_utxo_snapshots( - info, - &AccountType::Standard { - index: 0, - standard_account_type: StandardAccountType::BIP44Account, - }, - ) - }; - - let flagged: Vec = listing - .iter() - .filter(|row| row.is_held) - .map(|row| row.outpoint) - .collect(); - assert_eq!(flagged, vec![held.outpoint], "{listing:?}"); - } - - /// Acceptance is not persisted. After a restart — a fresh generation over - /// the same records — an unsettled send is held again until the network - /// is seen to accept it anew. - #[tokio::test] - async fn should_hold_after_a_restart_until_accepted_again() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 93); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - record_acceptance(&manager, &wallet_id, send.txid(), true).await; - assert!(spendable(&core).await > 700_000); - - { - let mut manager = manager.write().await; - let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet"); - // A restarted host turns probing on again. - let fresh = WalletGeneration::new(); - fresh.set_holds_unresolved_sends(true); - info.generation = Arc::new(fresh); - } - - assert_eq!(spendable(&core).await, 700_000); - } - - /// A send the network echoed back is accepted on the spot; an ambiguous - /// outcome records nothing. - #[tokio::test] - async fn should_mark_an_echoed_send_accepted_and_not_an_ambiguous_one() { - let broadcasters: [(Arc, bool); 2] = [ - (Arc::new(AlwaysOkBroadcaster), true), - (Arc::new(AlwaysMaybeSentBroadcaster), false), - ]; - for (broadcaster, expected) in broadcasters { - let (core, manager, wallet_id, signer) = dual_core(broadcaster).await; - let small = TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 94), 300_000); - let finalized = core - .finalize_transaction(small, &SEND_FUNDING_SOURCES, 0, &signer) - .await - .expect("finalize"); - let txid = finalized.transaction().txid(); - let _ = core.broadcast_finalized_transaction(&finalized).await; - - let manager = manager.read().await; - let info = manager.get_wallet_info(&wallet_id).expect("wallet"); - let accepted = info.generation.accepted_among(&HashSet::from([txid])); - assert_eq!(accepted.contains(&txid), expected, "accepted={expected}"); - } - } - - /// An acceptance that arrives before the wallet has applied its send — a - /// broadcast returning ahead of the record — survives the reads in - /// between and releases the send once the record lands. - #[tokio::test] - async fn should_keep_an_acceptance_that_arrives_before_its_record() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 95); - let send = core - .finalize_transaction( - TransactionBuilder::new().add_output(&paid, 100_000), - &SEND_FUNDING_SOURCES, - 0, - &signer, - ) - .await - .expect("the send builds") - .transaction() - .clone(); - record_acceptance(&manager, &wallet_id, send.txid(), true).await; - // A balance read before the record lands walks the unsettled records. - let _ = spendable(&core).await; - apply_to_wallet(&manager, &wallet_id, &send, TransactionContext::Mempool).await; - - let change = utxo(&manager, &wallet_id, change_of(&send, &paid)).await; - assert_eq!( - spendable(&core).await, - 700_000 + change.value(), - "the echoed send's change is spendable" - ); - } - - /// A wallet whose only coin is the change of an unresolved send: selection - /// has nothing left to offer (no size to report), and the payment is - /// measured against what it requested — waiting on the network, not a - /// shortfall. - #[tokio::test] - async fn should_report_awaiting_network_when_the_only_coin_is_held() { - let (manager, wallet_id, generation, signer) = - funded_wallet_manager(StandardAccountType::BIP44Account).await; - generation.set_holds_unresolved_sends(true); - let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); - let core: CoreWallet = CoreWallet::new( - sdk, - Arc::clone(&manager), - wallet_id, - Arc::new(AlwaysOkBroadcaster), - generation, - ); - let paid = DashAddress::dummy(Network::Testnet, 70); - unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 1_000_000).await; - - let refused = core - .finalize_transaction( - TransactionBuilder::new().add_output(&paid, 500_000), - &SEND_FUNDING_SOURCES, - 0, - &signer, - ) - .await; - assert!( - matches!( - refused, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { - required: Some(required), - outpoint: None, - .. - }) if required > 500_000 - ), - "got {refused:?}" - ); - } - - /// A `reservation_only` build spends only the inputs the caller seeded, so - /// held coins could never fund it: its shortfall stays insufficient funds - /// however much the account holds back. - #[tokio::test] - async fn should_not_count_held_coins_in_a_reservation_only_shortfall() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 71); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - let spent: Vec = send - .input - .iter() - .map(|input| input.previous_output) - .collect(); - let untouched = { - let manager = manager.read().await; - let info = manager.get_wallet_info(&wallet_id).expect("wallet"); - info.core_wallet - .accounts - .all_funding_accounts() - .into_iter() - .flat_map(|managed| managed.utxos.values().cloned()) - .find(|utxo| utxo.value() == 700_000 && !spent.contains(&utxo.outpoint)) - .expect("the untouched coin") - }; - - let refused = core - .finalize_transaction_with_options( - TransactionBuilder::new() - .add_output(&paid, 900_000) - .add_inputs([untouched]), - &SEND_FUNDING_SOURCES, - 0, - &signer, - true, - ) - .await; - assert!( - matches!( - refused, - Err(PlatformWalletError::CorePooledInsufficientFunds { .. }) - ), - "got {refused:?}" - ); - } - - /// Acceptance is not persisted, but the startup re-dispatch echoes a child - /// whose parent stays ambiguous. A node that accepted the child has its - /// parent, so the parent counts as accepted too and the child's coins are - /// free. - #[tokio::test] - async fn should_count_a_parent_accepted_when_its_child_is() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 72); - let parent = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - record_acceptance(&manager, &wallet_id, parent.txid(), true).await; - let child = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 1_000_000).await; - assert!(child - .input - .iter() - .any(|input| input.previous_output.txid == parent.txid())); - - { - let mut manager = manager.write().await; - let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet"); - // A restart: no acceptance facts survive. - let fresh = WalletGeneration::new(); - fresh.set_holds_unresolved_sends(true); - info.generation = Arc::new(fresh); - } - record_acceptance(&manager, &wallet_id, child.txid(), true).await; - - let manager = manager.read().await; - let info = manager.get_wallet_info(&wallet_id).expect("wallet"); - let unresolved = UnresolvedSends::of(info); - assert!( - !unresolved.holds(&change_of(&child, &paid)), - "the child's change is free" - ); - assert!( - !unresolved.holds(&OutPoint { - txid: parent.txid(), - vout: 0, - }), - "the parent counts as accepted" - ); - } - - /// With probing off nothing reads acceptance facts, so none are kept. - #[tokio::test] - async fn should_keep_no_acceptance_facts_while_probing_is_off() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let paid = DashAddress::dummy(Network::Testnet, 73); - let send = unresolved_send(&core, &manager, &wallet_id, &signer, &paid, 100_000).await; - { - let manager = manager.read().await; - let info = manager.get_wallet_info(&wallet_id).expect("wallet"); - info.generation.set_holds_unresolved_sends(false); - } - record_acceptance(&manager, &wallet_id, send.txid(), true).await; - - let manager = manager.read().await; - let info = manager.get_wallet_info(&wallet_id).expect("wallet"); - assert!(info - .generation - .acceptance_among(&HashSet::from([send.txid()])) - .is_empty()); - } - - /// Only a shortfall the held coins would actually cover is "waiting on the - /// network". One they would not cover stays insufficient funds. When - /// selection did not report the shortfall's size — every candidate held — - /// it is measured against what the build requested. - #[test] - fn should_report_awaiting_network_only_when_the_held_coins_cover_the_shortfall() { - let shortfall = |available, required| PlatformWalletError::CorePooledInsufficientFunds { - sources: SEND_FUNDING_SOURCES.to_vec(), - available, - required, - }; - assert!(matches!( - awaiting_network(shortfall(Some(400), Some(1_000)), 600, basis(0)), - PlatformWalletError::CoreFundsAwaitingNetwork { .. } - )); - assert!(matches!( - awaiting_network(shortfall(Some(400), Some(1_000)), 599, basis(0)), - PlatformWalletError::CorePooledInsufficientFunds { .. } - )); - assert!(matches!( - awaiting_network(shortfall(Some(400), Some(1_000)), 0, basis(0)), - PlatformWalletError::CorePooledInsufficientFunds { .. } - )); - // Nothing left to select (every coin held): sized by the request. - assert!(matches!( - awaiting_network(shortfall(Some(0), None), 1_000_000, basis(900_000)), - PlatformWalletError::CoreFundsAwaitingNetwork { - required: Some(900_000), - .. - } - )); - assert!(matches!( - awaiting_network(shortfall(Some(0), None), 1_000_000, basis(10_000_000)), - PlatformWalletError::CorePooledInsufficientFunds { .. } - )); - // A build whose shortfall cannot be told apart stays a shortfall. - assert!(matches!( - awaiting_network(shortfall(Some(400), Some(1_000)), 600, None), - PlatformWalletError::CorePooledInsufficientFunds { .. } - )); - } - - fn basis(requested: u64) -> Option { - Some(ShortfallBasis { - input_cost: 226, - requested, - }) - } - - /// The basis is priced at the build's own fee rate; a drain or a - /// special-payload build has none. - #[test] - fn should_price_the_shortfall_basis_at_the_builds_fee_rate() { - let output = |builder: TransactionBuilder| { - builder.add_output(&DashAddress::dummy(Network::Testnet, 70), 100_000) - }; - let normal = shortfall_basis(&output(TransactionBuilder::new())).expect("basis"); - let doubled = shortfall_basis(&output( - TransactionBuilder::new() - .set_fee_rate(FeeRate::new(FeeRate::normal().as_sat_per_kb() * 2)), - )) - .expect("basis"); - - assert_eq!( - normal.input_cost, - input_fee(FeeRate::normal()).expect("fee") - ); - assert!(doubled.input_cost > normal.input_cost); - assert!(doubled.requested > normal.requested); - assert!(shortfall_basis(&output( - TransactionBuilder::new().set_selection_strategy(SelectionStrategy::All) - )) - .is_none()); - } - - /// The DashPay contact payment's build failure: a shortfall the held coins - /// cover is "waiting on the network"; any other failure keeps the build - /// error that path has always reported. - #[test] - fn should_map_a_contact_payment_shortfall_the_held_coins_cover() { - let shortfall = || { - BuilderError::CoinSelection(SelectionError::InsufficientFunds { - available: 400, - required: 1_000, - }) - }; - assert!(matches!( - awaiting_network_or_build_error(shortfall(), 600, basis(0)), - PlatformWalletError::CoreFundsAwaitingNetwork { .. } - )); - assert!(matches!( - awaiting_network_or_build_error(shortfall(), 10, basis(0)), - PlatformWalletError::TransactionBuild(_) - )); - assert!(matches!( - awaiting_network_or_build_error( - BuilderError::CoinSelection(SelectionError::NoUtxosAvailable), - 1_000_000, - basis(900_000), - ), - PlatformWalletError::CoreFundsAwaitingNetwork { .. } - )); - assert!(matches!( - awaiting_network_or_build_error( - BuilderError::CoinSelection(SelectionError::NoUtxosAvailable), - 1_000_000, - basis(10_000_000), - ), - PlatformWalletError::TransactionBuild(_) - )); - } - - /// A broadcast that releases its reservation on a rejection records an - /// echoed send as accepted too. - #[tokio::test] - async fn should_mark_a_send_accepted_when_a_releasing_broadcast_is_echoed() { - let (core, manager, wallet_id, signer) = dual_core(Arc::new(AlwaysOkBroadcaster)).await; - let finalized = core - .finalize_transaction( - TransactionBuilder::new() - .add_output(&DashAddress::dummy(Network::Testnet, 96), 300_000), - &SEND_FUNDING_SOURCES, - 0, - &signer, - ) - .await - .expect("finalize"); - let txid = finalized.transaction().txid(); - core.broadcast_transaction_releasing_reservation( - StandardAccountType::BIP44Account, - 0, - finalized.transaction(), - ) - .await - .expect("broadcast"); - - let manager = manager.read().await; - let info = manager.get_wallet_info(&wallet_id).expect("wallet"); - assert!(info - .generation - .accepted_among(&HashSet::from([txid])) - .contains(&txid)); - } - - /// The address of the wallet's BIP32 coin: one of our own receive - /// addresses. - async fn utxo_addresses(manager: &Manager, wallet_id: &WalletId) -> DashAddress { - let manager = manager.read().await; - let info = manager.get_wallet_info(wallet_id).expect("wallet"); - info.core_wallet - .first_bip32_managed_account() - .expect("bip32 account") - .utxos - .values() - .next() - .expect("bip32 coin") - .address - .clone() - } } diff --git a/packages/rs-platform-wallet/src/wallet/core/unresolved.rs b/packages/rs-platform-wallet/src/wallet/core/unresolved.rs deleted file mode 100644 index 1ef32f6c0e2..00000000000 --- a/packages/rs-platform-wallet/src/wallet/core/unresolved.rs +++ /dev/null @@ -1,219 +0,0 @@ -//! The coins of our own sends the network has not been seen to accept. -//! -//! A send whose broadcast outcome is unknown leaves its outputs in the wallet -//! as ordinary mempool funds — its change, or the receiving side of a transfer -//! between our own accounts. If that send never reached the network those -//! coins are phantom, and every payment built on them is an orphan no node -//! accepts: the shape behind "Transaction status unknown" followed by payments -//! that never land. So, while the manager probes unresolved broadcasts, they -//! are neither offered to a new payment nor counted as spendable until the -//! send is known to be accepted. - -use std::collections::{HashMap, HashSet, VecDeque}; - -use dashcore::{OutPoint, Txid}; -use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; -use key_wallet::wallet::managed_wallet_info::transaction_builder::TransactionBuilder; - -use super::broadcast_resolver::unsettled_transactions; -use crate::wallet::platform_wallet::PlatformWalletInfo; - -/// The wallet's unsettled transactions whose outputs are held: every own send -/// — one spending the wallet's coins — that is neither InstantSend-locked nor -/// mined and that the network was not last seen to accept, and every unsettled -/// transaction spending an output of one of those, transitively. -/// -/// Empty while the manager does not probe unresolved broadcasts -/// ([`WalletGeneration::holds_unresolved_sends`](super::WalletGeneration::holds_unresolved_sends)): -/// without probing nothing beyond a lock or a block would ever release a held -/// send, and a record stuck in mempool context would freeze its coins for -/// good. -/// -/// Derived from the wallet's own records on every read, so it holds the same -/// after a restart. Only the acceptance facts (a peer echoing a broadcast, a -/// probe verdict) live in memory: until one arrives again, an unsettled send -/// stays held, which only errs on the side of not spending. A send found dead -/// after it was found accepted is held again. -pub(crate) struct UnresolvedSends { - txids: HashSet, -} - -impl UnresolvedSends { - pub(crate) fn of(info: &PlatformWalletInfo) -> Self { - if !info.generation.holds_unresolved_sends() { - return Self { - txids: HashSet::new(), - }; - } - let unsettled = unsettled_transactions(info); - let unsettled_txids: HashSet = unsettled.keys().copied().collect(); - - // Each unsettled transaction's unsettled parents, and the reverse. - let mut parents: HashMap> = HashMap::new(); - let mut children: HashMap> = HashMap::new(); - for (txid, (_, transaction)) in &unsettled { - let spent: HashSet = transaction - .input - .iter() - .map(|input| input.previous_output.txid) - .filter(|parent| unsettled.contains_key(parent)) - .collect(); - for parent in spent { - children.entry(parent).or_default().push(*txid); - parents.entry(*txid).or_default().push(parent); - } - } - - // A node that accepted a transaction has its parents: acceptance of a - // child — an echo of the startup re-dispatch, say — is acceptance of - // every unsettled ancestor, unless that ancestor's own record is newer - // (found dead since). - let facts = info.generation.acceptance_among(&unsettled_txids); - let seeds = facts - .iter() - .filter(|(_, (was_accepted, _))| *was_accepted) - .map(|(txid, (_, seq))| (*txid, *seq)); - let accepted = accepted_with_ancestors(&facts, &parents, seeds); - - // A transaction built on a held output is no better than its parent: - // a node that has not seen the parent refuses it. - let mut held: HashSet = HashSet::new(); - let mut queue: VecDeque = unsettled - .iter() - .filter(|(txid, (own, _))| *own && !accepted.contains(*txid)) - .map(|(txid, _)| *txid) - .collect(); - while let Some(txid) = queue.pop_front() { - if held.insert(txid) { - if let Some(spenders) = children.get(&txid) { - queue.extend(spenders.iter().copied()); - } - } - } - Self { txids: held } - } - - /// Whether the coin at `outpoint` is an output of a held transaction. - pub(crate) fn holds(&self, outpoint: &OutPoint) -> bool { - self.txids.contains(&outpoint.txid) - } - - /// `managed`'s held coins. - pub(crate) fn held_in<'a>( - &'a self, - managed: &'a ManagedCoreFundsAccount, - ) -> impl Iterator + 'a { - managed - .utxos - .keys() - .copied() - .filter(move |outpoint| self.holds(outpoint)) - } - - /// `builder` told never to spend `managed`'s held coins: a funding - /// candidate is left out of selection, an input the caller seeded is - /// refused (key-wallet's `BuilderError::ExcludedInput`). Nothing is - /// recorded on the wallet. - pub(crate) fn exclude_from( - &self, - builder: TransactionBuilder, - managed: &ManagedCoreFundsAccount, - ) -> TransactionBuilder { - if self.txids.is_empty() { - return builder; - } - builder.exclude_outpoints(self.held_in(managed).collect::>()) - } - - /// What `managed`'s held coins could add to a payment once released: the - /// spendable ones at `height`, each net of `input_cost`, the fee its own - /// input adds. The test for whether a shortfall is the held coins' doing. - pub(crate) fn held_net_value( - &self, - managed: &ManagedCoreFundsAccount, - height: u32, - input_cost: u64, - ) -> u64 { - if self.txids.is_empty() { - return 0; - } - managed - .spendable_utxos(height) - .iter() - .filter(|utxo| self.holds(&utxo.outpoint)) - .map(|utxo| utxo.value().saturating_sub(input_cost)) - .sum() - } -} - -/// The accepted transactions plus every unsettled ancestor their acceptance -/// reaches. An ancestor is reached unless its own record is a newer "dead"; -/// each transaction keeps the highest acceptance seq that reached it and is -/// walked again when a higher one arrives, so the result is the same whatever -/// order `seeds` (a `HashMap` walk) come in. -fn accepted_with_ancestors( - facts: &HashMap, - parents: &HashMap>, - seeds: impl IntoIterator, -) -> HashSet { - let mut best: HashMap = HashMap::new(); - let mut up: VecDeque<(Txid, u64)> = seeds.into_iter().collect(); - while let Some((txid, seq)) = up.pop_front() { - if best.get(&txid).is_some_and(|known| *known >= seq) { - continue; - } - best.insert(txid, seq); - for parent in parents.get(&txid).into_iter().flatten() { - let overridden = facts - .get(parent) - .is_some_and(|(parent_accepted, parent_seq)| !parent_accepted && *parent_seq > seq); - if !overridden { - up.push_back((*parent, seq)); - } - } - } - best.into_keys().collect() -} - -#[cfg(test)] -mod tests { - use std::collections::{HashMap, HashSet}; - - use dashcore::hashes::Hash; - use dashcore::Txid; - - use super::accepted_with_ancestors; - - fn txid(byte: u8) -> Txid { - Txid::from_byte_array([byte; 32]) - } - - /// G <- P <- C: P accepted at seq 2, C accepted at seq 9, G found dead at - /// seq 5. C's newer acceptance reaches G past its older death, whichever - /// of P and C is walked first. - #[test] - fn should_propagate_the_newest_acceptance_whatever_the_walk_order() { - let (g, p, c) = (txid(1), txid(2), txid(3)); - let facts = HashMap::from([(p, (true, 2)), (c, (true, 9)), (g, (false, 5))]); - let parents = HashMap::from([(c, vec![p]), (p, vec![g])]); - let expected = HashSet::from([g, p, c]); - - let p_first = accepted_with_ancestors(&facts, &parents, [(p, 2), (c, 9)]); - let c_first = accepted_with_ancestors(&facts, &parents, [(c, 9), (p, 2)]); - - assert_eq!(p_first, expected); - assert_eq!(c_first, expected); - } - - /// An ancestor found dead after the newest acceptance below it stays out. - #[test] - fn should_stop_at_an_ancestor_found_dead_after_the_acceptance() { - let (g, p, c) = (txid(1), txid(2), txid(3)); - let facts = HashMap::from([(c, (true, 3)), (g, (false, 7))]); - let parents = HashMap::from([(c, vec![p]), (p, vec![g])]); - - let accepted = accepted_with_ancestors(&facts, &parents, [(c, 3)]); - - assert_eq!(accepted, HashSet::from([p, c])); - } -} diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 13ae6a4041c..a5b49b41bc6 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -12,7 +12,6 @@ use key_wallet_manager::WalletManager; use super::*; use crate::broadcaster::TransactionBroadcaster; use crate::error::PlatformWalletError; -use crate::wallet::core::{awaiting_network_or_build_error, shortfall_basis, UnresolvedSends}; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; // --------------------------------------------------------------------------- @@ -1128,15 +1127,7 @@ impl DashPayView<'_, B> { self.drain_pending_contact_crypto_verified(provider, None) .await?; - let ( - payment_address, - used_flip_changeset, - tx, - fee, - funding_accounts, - in_broadcast_pin, - generation, - ) = { + let (payment_address, used_flip_changeset, tx, fee, funding_accounts, in_broadcast_pin) = { let mut wm = self.wallet_manager.write().await; // Resolve the external account's xpub so we can derive addresses. @@ -1254,13 +1245,6 @@ impl DashPayView<'_, B> { .set_current_height(current_height) .set_selection_strategy(SelectionStrategy::LargestFirst) .add_output(&payment_address, amount_duffs); - // Coins of our own sends the network has not been seen to accept - // stay out of selection (see `UnresolvedSends`). - let unresolved = UnresolvedSends::of(info); - // Infallible: the payment address is already marked used here, and - // an early return now would burn it. - let basis = shortfall_basis(&builder); - let mut held_value = 0u64; // Derivation paths for every offered UTXO, since the signer closure // below cannot resolve them from one account. @@ -1297,12 +1281,7 @@ impl DashPayView<'_, B> { funding_paths.insert(utxo.address.clone(), path); } } - if let Some(basis) = basis { - held_value += - unresolved.held_net_value(managed, current_height, basis.input_cost); - } - builder = - unresolved.exclude_from(builder.add_funding(managed, account), managed); + builder = builder.add_funding(managed, account); offered_accounts.push(at); } } @@ -1351,7 +1330,7 @@ impl DashPayView<'_, B> { { return_contact_payment_address_to_pool(external_account, &payment_address); } - return Err(awaiting_network_or_build_error(e, held_value, basis)); + return Err(PlatformWalletError::TransactionBuild(e.to_string())); } }; @@ -1422,7 +1401,6 @@ impl DashPayView<'_, B> { fee, offered_accounts, in_broadcast_pin, - Arc::clone(&info.generation), ) }; @@ -1526,11 +1504,6 @@ impl DashPayView<'_, B> { // still selectable here and must stay fenced until an observed // spend says otherwise. in_broadcast_pin.settle_pending_spend(); - // Accepted: its change may fund the next payment before the - // InstantSend lock arrives (see `UnresolvedSends`). - if other.is_ok() { - generation.mark_send_accepted(tx.txid()); - } other } }; diff --git a/packages/rs-platform-wallet/src/wallet/reservations.rs b/packages/rs-platform-wallet/src/wallet/reservations.rs index a2a61370ca7..fb4ddd7676b 100644 --- a/packages/rs-platform-wallet/src/wallet/reservations.rs +++ b/packages/rs-platform-wallet/src/wallet/reservations.rs @@ -33,7 +33,6 @@ use key_wallet_manager::WalletManager; use tokio::sync::RwLock; use crate::broadcaster::{BroadcastError, TransactionBroadcaster}; -use crate::wallet::core::generation::next_acceptance_seq; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; /// Maximum age, in `last_processed_height` blocks, of a held funding @@ -127,23 +126,6 @@ pub(crate) fn reservation_expired(registered_height: u32, current_height: Option } } -/// A broadcast of ours returned accepted — a peer echoed it back. Record it, -/// so its outputs are not held as an unresolved send's -/// ([`UnresolvedSends`](crate::wallet::core::UnresolvedSends)). The sequence -/// number is taken here, where the fact is observed, before the lock is -/// awaited. -pub(crate) async fn record_send_accepted( - wallet_manager: &RwLock>, - wallet_id: &WalletId, - txid: Txid, -) { - let seq = next_acceptance_seq(); - let manager = wallet_manager.read().await; - if let Some((_, info)) = manager.get_wallet_and_info(wallet_id) { - info.generation.record_acceptance(txid, true, seq); - } -} - /// Broadcast `tx` and reconcile the funding account's UTXO reservation on /// failure. /// @@ -170,10 +152,7 @@ pub(crate) async fn broadcast_releasing_on_rejection Result { match broadcaster.broadcast(tx).await { - Ok(txid) => { - record_send_accepted(wallet_manager, wallet_id, txid).await; - Ok(txid) - } + Ok(txid) => Ok(txid), Err(e) => { if matches!(e, BroadcastError::Rejected { .. }) { release_reservation_after_rejected_broadcast( diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerDiagnostics.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerDiagnostics.swift index dde7d7109ff..3a350a2a782 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerDiagnostics.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerDiagnostics.swift @@ -337,11 +337,6 @@ extension PlatformWalletManager { public let scriptPubkey: Data public let height: UInt32 public let isLocked: Bool - /// An output of a send the network has not been seen to accept: a - /// build seeded with it fails with `.coreFundsAwaitingNetwork`, so - /// coin control and drains leave it out. Always false while broadcast - /// probing is off. - public var isHeld: Bool = false } public func accountUtxos( @@ -378,8 +373,7 @@ extension PlatformWalletManager { valueDuffs: entry.value_duffs, scriptPubkey: scriptData, height: entry.height, - isLocked: entry.is_locked, - isHeld: entry.is_held + isLocked: entry.is_locked ) } } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift index aa69117fdb3..7fcfa941f44 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift @@ -258,11 +258,6 @@ public enum PlatformWalletResultCode: Int32, Sendable { case errorShieldedRecoveryKeysRequired = 57 /// Platform returned no balance. Retrying the read is safe; ownership is unchanged. case errorIdentityBalanceUnavailable = 58 - /// The payment needs coins held back because they came from one of the - /// wallet's own sends whose broadcast outcome is still unknown. Not a - /// shortfall and nothing was sent: they become spendable once that send is - /// InstantSend-locked, mined, or found accepted. - case errorCoreFundsAwaitingNetwork = 59 /// The named thing does not exist. Besides the handle/lookup failures this /// has always covered, BOTH deferred-send paths report the /// wallet-was-REMOVED case here. @@ -390,8 +385,6 @@ public enum PlatformWalletResultCode: Int32, Sendable { self = .errorShieldedRecoveryKeysRequired case PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_IDENTITY_BALANCE_UNAVAILABLE: self = .errorIdentityBalanceUnavailable - case PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_CORE_FUNDS_AWAITING_NETWORK: - self = .errorCoreFundsAwaitingNetwork case PLATFORM_WALLET_FFI_RESULT_CODE_NOT_FOUND: self = .notFound case PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_UNKNOWN: @@ -561,8 +554,6 @@ public enum PlatformWalletError: LocalizedError { case arithmeticOverflow(String) case noSelectableInputs(String) case coreInsufficientFunds(String) - /// See `PlatformWalletResultCode.errorCoreFundsAwaitingNetwork`. - case coreFundsAwaitingNetwork(String) case assetLockNotTracked(String) /// The one-shot output cannot be reused. This may come from a retained /// local tombstone or an unauthenticated Platform report, so it does not @@ -795,7 +786,6 @@ public enum PlatformWalletError: LocalizedError { .serialization(let m), .deserialization(let m), .memoryAllocation(let m), .arithmeticOverflow(let m), .noSelectableInputs(let m), .coreInsufficientFunds(let m), - .coreFundsAwaitingNetwork(let m), .assetLockNotTracked(let m), .assetLockAlreadyConsumed(let m), .assetLockFundingMismatch(let m), .assetLockInsufficientFunds(let m), .walletAlreadyExists(let m), .shieldedBroadcastFailed(let m), @@ -908,7 +898,6 @@ public enum PlatformWalletError: LocalizedError { case .errorArithmeticOverflow: self = .arithmeticOverflow(detail) case .errorNoSelectableInputs: self = .noSelectableInputs(detail) case .errorCoreInsufficientFunds: self = .coreInsufficientFunds(detail) - case .errorCoreFundsAwaitingNetwork: self = .coreFundsAwaitingNetwork(detail) case .errorAssetLockNotTracked: self = .assetLockNotTracked(detail) case .errorAssetLockAlreadyConsumed: self = .assetLockAlreadyConsumed(detail) case .errorAssetLockFundingMismatch: self = .assetLockFundingMismatch(detail) From f4937c4c51c8390cb0eba9fea5148b8b4a4afb74 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Sat, 3 Oct 2026 17:40:18 +0300 Subject: [PATCH 056/169] fix(platform-wallet): spend only final coins, and say when funds wait for confirmation Every payment build that funds from the wallet now calls key-wallet's require_final_inputs (pooled sends incl. reservation_only, the DashPay contact payment; asset locks already did): only coins that are InstantSend-locked or mined are spent. The change of a send that never reached the network never becomes final, so nothing is built on it. - pooled_spendable_balance / pooled_max_sendable count only final coins, so Max never offers what a build refuses. - A shortfall the not-yet-final coins of the offered accounts would cover becomes CoreFundsAwaitingNetwork (FFI 59; Swift, Kotlin, registry) instead of insufficient funds; with nothing final at all (key-wallet names no requirement) it is reported as waiting too. Its message is in DASH, without Option debug text. - core_wallet_tx_builder_add_inputs_from_outpoints refuses a seeded coin that is not final with code 59 and the outpoint, instead of letting the build drop it silently. Co-Authored-By: Claude Opus 5.5 --- .../dashsdk/errors/DashSdkError.kt | 10 + .../dashsdk/errors/DashSdkErrorTest.kt | 4 + .../ERROR_CODE_REGISTRY.md | 6 +- .../src/core_wallet/transaction_builder.rs | 42 ++- packages/rs-platform-wallet-ffi/src/error.rs | 32 ++ packages/rs-platform-wallet/src/error.rs | 53 +++ .../rs-platform-wallet/src/wallet/core/mod.rs | 4 +- .../src/wallet/core/transaction.rs | 356 +++++++++++++++++- .../src/wallet/identity/network/payments.rs | 9 +- .../PlatformWallet/PlatformWalletResult.swift | 11 + 10 files changed, 504 insertions(+), 23 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt index 420ad1a6bbb..da78414a634 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt @@ -117,6 +117,15 @@ sealed class DashSdkError( class CoreInsufficientFunds(message: String, cause: Throwable? = null) : PlatformWallet(message, cause) + /** + * `ErrorCoreFundsAwaitingNetwork` (native code 59). The payment is + * covered only by coins the network has not confirmed yet (neither + * InstantSend-locked nor mined); they become spendable once their + * transaction is. Not a shortfall — retry once they are confirmed. + */ + class CoreFundsAwaitingNetwork(message: String, cause: Throwable? = null) : + PlatformWallet(message, cause) + /** * `ErrorShieldedInsufficientBalance` (native code 41; historical FFI * spelling). A Platform Payment account's deterministic shield input @@ -733,6 +742,7 @@ sealed class DashSdkError( 20 -> PlatformWallet.TransactionBroadcastUnconfirmed(message, cause) // ErrorTransactionBroadcastUnconfirmed 42 -> PlatformWallet.MasternodeWithdrawalUnconfirmed(message, cause) // ErrorMasternodeWithdrawalUnconfirmed 22 -> PlatformWallet.CoreInsufficientFunds(message, cause) // ErrorCoreInsufficientFunds + 59 -> PlatformWallet.CoreFundsAwaitingNetwork(message, cause) // ErrorCoreFundsAwaitingNetwork 23 -> PlatformWallet.AssetLockNotTracked(message, cause) // ErrorAssetLockNotTracked 24 -> PlatformWallet.AssetLockAlreadyConsumed(message, cause) // ErrorAssetLockAlreadyConsumed 25 -> PlatformWallet.AssetLockFundingMismatch(message, cause) // ErrorAssetLockFundingMismatch diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt index 7129c55f633..1e5c1700a0a 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt @@ -129,6 +129,10 @@ class DashSdkErrorTest { DashSdkError.fromNative(DashSDKException(offset + 22, "inputs reserved")) assertTrue(coreInsufficientFunds is DashSdkError.PlatformWallet.CoreInsufficientFunds) + val awaitingNetwork = + DashSdkError.fromNative(DashSDKException(offset + 59, "funds awaiting network")) + assertTrue(awaitingNetwork is DashSdkError.PlatformWallet.CoreFundsAwaitingNetwork) + // The asset-lock coin-selection shortfall (29) must reach callers as its // own type rather than Generic, and must stay DISTINCT from the atomic // Core-send shortfall (22) — the two selectors report over different diff --git a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md index 3399060f53b..e5595d41c13 100644 --- a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md +++ b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md @@ -114,13 +114,14 @@ These are shipped ABI. Do not renumber. | 98 | `NotFound` | Sentinel — `Option` returned as an error | | 99 | `ErrorUnknown` | Sentinel — unmapped/flattened errors | -**Next allocatable integer: 59** — 27–58 are all claimed (27, 29, 31, 34–42 +**Next allocatable integer: 60** — 27–59 are all claimed (27, 29, 31, 34–42 and 46 merged; 43–45 proposed by active #4313 at head `0302b188ab`; 47 and 48 proposed by active #4356 (47 renumbered from 42, 48 from 43 — see their rows below); 49–54 proposed by active #4586 (the persister operation × kind block); 55–57 proposed by #4715 for pending identity-funded shield debits and durable recovery errors; 58 proposed by #4799 for an unavailable -identity balance response; 28, 30, +identity balance response; 59 proposed by the unresolved-broadcast resolver +PR for funds held until an earlier send is accepted; 28, 30, 32 and 33 reserved). **28, 30, 32 and 33 are RESERVED, not free**: 28 and 30 were vacated when the reservation trio moved to 34–36; 32 and 33 lapsed when their in-repo owners @@ -172,6 +173,7 @@ Fork-era numbers remain in the collision history, which is immutable record. | 56 | `ErrorShieldedRecoveryCorrupted` | #4715 | Proposed — durable shielded recovery data is malformed or invalid; preserved for diagnosis. Rust, Swift and Kotlin preserve this typed error | | 57 | `ErrorShieldedRecoveryKeysRequired` | #4715 | Proposed — recovery needs the account and compatible keys; ciphertext damage can produce the same symptom. Rust, Swift and Kotlin preserve this typed error | | 58 | `ErrorIdentityBalanceUnavailable` | #4799 | Proposed — Platform returned no balance for a managed identity. Retrying the read is safe; this is distinct from missing wallet ownership and must not trigger registration or funding | +| 59 | `ErrorCoreFundsAwaitingNetwork` | `fix/resolve-unknown-broadcast` (platform#5258) | Proposed — claimed from the frontier (58). A Core payment the balance covers only with coins the network has not confirmed yet (neither InstantSend-locked nor mined); builds spend only final coins. Not a shortfall: retry once those coins are confirmed. Rust value + pin test, Swift raw case + typed `coreFundsAwaitingNetwork`, Kotlin typed `PlatformWallet.CoreFundsAwaitingNetwork` + `fromPlatformWalletNative` arm + `DashSdkErrorTest` pin | **Code 31 left this table on 2026-08-04.** `ErrorSigningKeyUnavailable` sat here as #4183's proposal until #4183 merged (`189a3abb1c`); it is now in the merged diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet/transaction_builder.rs b/packages/rs-platform-wallet-ffi/src/core_wallet/transaction_builder.rs index bb3f8f3d6dc..7abe9be67ba 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet/transaction_builder.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet/transaction_builder.rs @@ -17,6 +17,7 @@ use key_wallet::wallet::managed_wallet_info::transaction_builder::{ TransactionBuilder, MAX_STANDARD_OP_RETURN_BYTES, }; use key_wallet::wallet::managed_wallet_info::transaction_building::AccountTypePreference; +use platform_wallet::PlatformWalletError; use rs_sdk_ffi::{MnemonicResolverCoreSigner, MnemonicResolverHandle}; use std::ffi::CString; use std::os::raw::{c_char, c_void}; @@ -980,36 +981,59 @@ pub unsafe extern "C" fn core_wallet_tx_builder_add_inputs_from_outpoints( let wm = wallet.wallet_manager().read().await; let info = wm .get_wallet_info(&wallet_id) - .ok_or_else(|| "wallet not found".to_string())?; + .ok_or_else(|| SeedError::Other("wallet not found".to_string()))?; let managed = managed_account(&info.core_wallet.accounts, source, account_index) - .ok_or_else(|| format!("managed account {source:?} #{account_index} not found"))?; + .ok_or_else(|| { + SeedError::Other(format!( + "managed account {source:?} #{account_index} not found" + )) + })?; let mut selected = Vec::with_capacity(requested.len()); for op in &requested { - let utxo = managed - .utxos - .get(op) - .cloned() - .ok_or_else(|| format!("outpoint {}:{} not in account", op.txid, op.vout))?; + let utxo = managed.utxos.get(op).cloned().ok_or_else(|| { + SeedError::Other(format!("outpoint {}:{} not in account", op.txid, op.vout)) + })?; + // Builds spend only final coins (InstantSend-locked or mined), so a + // seeded coin that is not final would be dropped silently at build + // time; refuse it here, by name, instead. + if !(utxo.is_confirmed || utxo.is_instantlocked) { + return Err(SeedError::NotFinal( + PlatformWalletError::CoreFundsAwaitingNetwork { + available: None, + waiting: utxo.value(), + required: None, + outpoint: Some(*op), + }, + )); + } selected.push(utxo); } // Validation succeeded — only now consume the builder. let taken = (*builder).take_builder(); (*builder).store_builder(taken.add_inputs(selected)); - Ok::<_, String>(()) + Ok::<_, SeedError>(()) }); match result { Ok(()) => PlatformWalletFFIResult::ok(), - Err(e) => PlatformWalletFFIResult::err( + Err(SeedError::NotFinal(error)) => PlatformWalletFFIResult::from(error), + Err(SeedError::Other(e)) => PlatformWalletFFIResult::err( PlatformWalletFFIResultCode::ErrorWalletOperation, format!("add_inputs_from_outpoints failed: {e}"), ), } } +/// Why `core_wallet_tx_builder_add_inputs_from_outpoints` refused its outpoints. +enum SeedError { + /// A named coin is not final yet (code 59). + NotFinal(PlatformWalletError), + Other(String), +} + /// Destroy a transaction builder created by `core_wallet_tx_builder_new`. /// /// # Safety diff --git a/packages/rs-platform-wallet-ffi/src/error.rs b/packages/rs-platform-wallet-ffi/src/error.rs index 5e7bc342baf..96ed5a52206 100644 --- a/packages/rs-platform-wallet-ffi/src/error.rs +++ b/packages/rs-platform-wallet-ffi/src/error.rs @@ -599,6 +599,14 @@ pub enum PlatformWalletFFIResultCode { /// Platform returned no balance for a managed identity. Retrying this read /// is safe; this does not imply missing ownership or require registration. ErrorIdentityBalanceUnavailable = 58, + /// A Core payment the balance covers only with coins the network has not + /// confirmed yet — neither InstantSend-locked nor mined + /// ([`platform_wallet::PlatformWalletError::CoreFundsAwaitingNetwork`]). + /// Not a shortfall and nothing was sent: the coins become spendable once + /// their transaction is InstantSend-locked or mined, so the host should + /// say the money is waiting on the network and offer a retry later — not + /// "insufficient funds". + ErrorCoreFundsAwaitingNetwork = 59, /// The named thing does not exist. /// @@ -966,6 +974,9 @@ impl From for PlatformWalletFFIResult { PlatformWalletError::AddressNonceMismatch { .. } => { PlatformWalletFFIResultCode::ErrorAddressNonceMismatch } + PlatformWalletError::CoreFundsAwaitingNetwork { .. } => { + PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork + } // Both shapes are "the wallet cannot cover this payment"; hosts // classify and retry them identically, so the pooled variant rides // the same code rather than forcing every host to learn a second @@ -2578,6 +2589,27 @@ mod tests { } } +#[cfg(test)] +mod core_funds_awaiting_network_tests { + use super::*; + + #[test] + fn should_give_unconfirmed_funds_their_own_code_apart_from_insufficient_funds() { + let result = PlatformWalletFFIResult::from(PlatformWalletError::CoreFundsAwaitingNetwork { + available: Some(700_000), + waiting: 599_000, + required: Some(1_000_000), + outpoint: None, + }); + assert_eq!( + result.code, + PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork + ); + assert_eq!(result.code as u32, 59); + assert!(!result.message.is_null()); + } +} + #[cfg(test)] mod identity_balance_error_tests { use super::*; diff --git a/packages/rs-platform-wallet/src/error.rs b/packages/rs-platform-wallet/src/error.rs index d87b3f1ba58..f4c34b32755 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -393,6 +393,32 @@ pub enum PlatformWalletError { required: Option, }, + /// A Core payment the wallet's balance covers, but only with coins the + /// network has not confirmed yet — neither InstantSend-locked nor mined. + /// Builds spend only final coins (`require_final_inputs`): the change of + /// a send that never reached the network never becomes final, so building + /// on it would make a transaction no node accepts. Coins become spendable + /// once their transaction is InstantSend-locked (usually seconds) or mined. + /// Not a shortfall: the host should say the money is waiting on the + /// network. + /// + /// `waiting` is the value of the not-yet-final coins among the offered + /// sources. `available` / `required` are the build's figures when known. + /// `outpoint` names a not-final coin the caller chose as an input itself. + #[error( + "Core funds are waiting for network confirmation: {} DASH not yet confirmed{}{}{}", + dash_amount(*waiting), + optional_amount(", available", available), + optional_amount(", needed", required), + refused_input(outpoint) + )] + CoreFundsAwaitingNetwork { + available: Option, + waiting: u64, + required: Option, + outpoint: Option, + }, + #[error("no spendable inputs available on {account_type} account {account_index}: {context}")] NoSpendableInputs { account_type: StandardAccountType, @@ -1785,3 +1811,30 @@ mod asset_lock_already_consumed_tests { )); } } + +/// Duffs as DASH for an error message: up to eight decimals, trailing zeros +/// dropped (`85998722` → `0.85998722`, `1000000` → `0.01`). +fn dash_amount(duffs: u64) -> String { + let whole = duffs / 100_000_000; + let fraction = duffs % 100_000_000; + if fraction == 0 { + return whole.to_string(); + } + let fraction = format!("{fraction:08}"); + format!("{whole}.{}", fraction.trim_end_matches('0')) +} + +/// `",

{ // map after the rollback is one a same-id re-registration owns. let still_mapped = self.wallets.load(); if !inserted_in_manager.is_empty() { + // Under each rolled-back generation's lifecycle gate, like any + // removal: an operation that saw the registration live and is + // pushing one of its transactions to the network finishes (or + // gives up) before the wallet goes. Only generations this call + // inserted — the published ones by the identity recorded then, + // the others still ours by construction. Gates first, manager + // lock second. + let unpublished: Vec> = { + let wm = self.wallet_manager.read().await; + inserted_in_manager + .iter() + .filter(|id| !inserted_in_wallets.iter().any(|(w, _)| w == *id)) + .filter_map(|id| wm.get_wallet_info(id)) + .map(|info| Arc::clone(&info.generation)) + .collect() + }; + // Held until the rollback below is done. + let mut teardowns = Vec::new(); + for generation in inserted_in_wallets + .iter() + .map(|(_, generation)| generation) + .chain(&unpublished) + { + teardowns.push(generation.teardown_guard().await); + } let mut wm = self.wallet_manager.write().await; for id in &inserted_in_manager { // A published id whose generation is no longer ours belongs @@ -603,11 +628,7 @@ pub(super) async fn resend_one( tx: &dashcore::Transaction, ) -> ResendOutcome { let _payment = generation.payment_guard().await; - let still_live = { - let wm = wallet_manager.read().await; - wm.get_wallet_info(wallet_id) - .is_some_and(|info| Arc::ptr_eq(&info.generation, generation)) - }; + let still_live = generation.is_registered_in(&*wallet_manager.read().await, wallet_id); if !still_live { tracing::info!( wallet_id = %hex::encode(wallet_id), diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 115f5a9071d..54e82938fae 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -575,6 +575,9 @@ impl PlatformWalletManager

{ error = %e, "failed to persist wallet registration changeset" ); + // Under the generation's lifecycle gate, like any removal (gate + // first, manager lock second). + let _teardown = generation.teardown_guard().await; let mut wm = self.wallet_manager.write().await; if let Err(remove_err) = self.remove_from_inner(&mut wm, &wallet_id) { tracing::warn!( @@ -623,6 +626,8 @@ impl PlatformWalletManager

{ error = %e, "failed to restore persisted platform-address state" ); + // Under the generation's lifecycle gate, like any removal. + let _teardown = platform_wallet.generation().teardown_guard().await; let mut wm = self.wallet_manager.write().await; if let Err(remove_err) = self.remove_from_inner(&mut wm, &wallet_id) { tracing::warn!( diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index b8b019ec685..f050d77ecfa 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -6,7 +6,10 @@ //! unconfirmed spend and nothing ever tells the user whether the payment //! landed. This module re-asks the network through //! [`AcceptanceProbe`](crate::broadcast_probe::AcceptanceProbe) and publishes -//! the verdict; it **changes nothing** in the wallet. Verdicts are positive +//! the verdict; it **changes nothing** in the wallet. A probe resubmits the +//! signed transaction, so it goes out only under the lifecycle gate of the +//! registration it was read from, never for one that has left the manager, +//! and a removal stops the one in flight (see [`gated_probe`]). Verdicts are positive //! only — accepted or mined — or unresolved: no answer the probe gets proves //! that a transaction can never land (see //! [`broadcast_probe`](crate::broadcast_probe)). @@ -108,7 +111,7 @@ use tokio::time::{timeout_at, Instant}; use crate::broadcast_probe::{AcceptanceProbe, ProbeOutcome, ProbeVerdict}; use crate::events::{PlatformEventHandler, PlatformEventManager}; -use crate::wallet::core::record_spends_own_coins; +use crate::wallet::core::{record_spends_own_coins, WalletGeneration}; use crate::wallet::platform_wallet::PlatformWalletInfo; /// Blocks from the start of a root's window (see [`ProbeSchedule`]) during @@ -910,6 +913,9 @@ pub(crate) struct WalletRead { pub synced_height: u32, /// The requested `Uncertain` txids the wallet actually holds. pub forced: HashSet, + /// The registration the views were read from: its transactions are + /// probed only while it is still the wallet's (see [`gated_probe`]). + pub generation: Arc, } /// An `Uncertain` result is queued for every wallet; only one holding the @@ -951,6 +957,42 @@ pub(crate) trait WalletSource: Send + Sync { walk: &AtomicBool, forced: HashSet, ) -> Option>; + + /// Whether `generation` is still the registration under `wallet_id` — a + /// removal, or a re-registration under the same id, ends that. + async fn is_registered(&self, wallet_id: WalletId, generation: &Arc) -> bool; +} + +/// Probe `transaction` for the registration it was read from, or not at all: +/// `None` when `generation` is no longer the wallet's, with nothing sent. +/// +/// A probe resubmits the transaction, so it follows the contract of +/// [`WalletGeneration::payment_guard`]: the gate is held across both the +/// liveness check and the network step. A removal therefore either gets in +/// first, and the probe sees the registration gone, or finds the probe +/// inside — which then stops where it is, so the removal waits for no +/// node's answer (a probe is up to eight requests, each with its own +/// timeout). Stopping the job from the actor is not that barrier: the actor +/// learns of a removal only after the manager has let the wallet go. +/// +/// Lock order is the gate's: gate first, wallet-manager lock (inside +/// [`WalletSource::is_registered`]) second. +async fn gated_probe( + source: &dyn WalletSource, + probe: &dyn AcceptanceProbe, + wallet_id: WalletId, + generation: &Arc, + transaction: &Transaction, +) -> Option { + let _payment = generation.payment_guard().await; + if !source.is_registered(wallet_id, generation).await { + return None; + } + tokio::select! { + biased; + _ = generation.teardown_requested() => None, + outcome = probe.probe(transaction) => Some(outcome), + } } /// Whether any account of the wallet records `txid` — a map lookup per @@ -1024,8 +1066,13 @@ impl WalletSource for ManagerSource { views: collect_views(info, wallet.can_sign()), synced_height: info.core_wallet.synced_height(), forced, + generation: Arc::clone(&info.generation), })) } + + async fn is_registered(&self, wallet_id: WalletId, generation: &Arc) -> bool { + generation.is_registered_in(&*self.0.read().await, &wallet_id) + } } /// Where events for the host go. @@ -1149,7 +1196,10 @@ enum JobDone { /// The root it probed: a result for a root no longer in flight (its /// probe was stopped, but had already finished) is not this one's. root: Txid, - outcome: ProbeOutcome, + /// `None`: the registration the pass read is no longer the wallet's, + /// or is being removed — no verdict, and nothing more is sent for it + /// (see [`gated_probe`]). + outcome: Option, }, /// [`NO_ANSWER_RETRY`] passed since a probe at `height` that no node /// answered. @@ -1207,6 +1257,8 @@ struct Run { struct Probing { views: Vec, + /// The registration `views` were read from (see [`WalletRead::generation`]). + generation: Arc, height: u32, due: VecDeque, /// The root whose probe is out now. @@ -1971,6 +2023,7 @@ impl Actor { }; current.probing = Some(Probing { views, + generation: read.generation, height, due: start.due, in_flight: None, @@ -2034,7 +2087,7 @@ impl Actor { wallet_id, run, root: probed, - outcome: ProbeOutcome { verdict, answered }, + outcome, } => { let Some(current) = self .wallets @@ -2076,6 +2129,23 @@ impl Actor { self.probe_next(wallet_id); return; }; + let Some(ProbeOutcome { verdict, answered }) = outcome else { + // The registration this pass read is leaving the manager + // or has left it: everything kept for it is stale, as for + // a wallet registered anew. A removal through this manager + // queues `WalletRemoved` too, usually ahead of this + // result; one that does not (through + // `wallet_manager_arc`) leaves a fresh entry, which a + // same-id wallet's events find. + tracing::info!( + wallet_id = %hex::encode(wallet_id), + txid = %root.txid, + "broadcast probe: the wallet's registration is gone; probe dropped" + ); + self.drop_wallet(&wallet_id, "gone"); + self.wallets.insert(wallet_id, WalletEntry::default()); + return; + }; if !probing.views.iter().any(|view| view.txid == root.txid) { // Settled or gone while its probe was out: moot. self.probe_next(wallet_id); @@ -2199,11 +2269,20 @@ impl Actor { }; if let Some(root) = probing.due.pop_front() { let probe = Arc::clone(&self.probe); + let source = Arc::clone(&self.source); + let generation = Arc::clone(&probing.generation); let id = run.id; let transaction = Arc::clone(&root.transaction); let root_txid = root.txid; run.abort = self.jobs.spawn(async move { - let outcome = probe.probe(&transaction).await; + let outcome = gated_probe( + source.as_ref(), + probe.as_ref(), + wallet_id, + &generation, + &transaction, + ) + .await; JobDone::Probed { wallet_id, run: id, @@ -3447,6 +3526,8 @@ mod tests { walks: StdMutex>, /// The synced height each wallet's reads report. synced: StdMutex>, + /// Each wallet's current registration, made at its first use. + generations: StdMutex>>, /// Reads wait while set, so a test can hold a pass in its read. paused: StdMutex, resume: Notify, @@ -3469,6 +3550,25 @@ mod tests { self.resume.notify_waiters(); } } + + /// The wallet's current registration. + fn generation(&self, wallet_id: WalletId) -> Arc { + Arc::clone( + self.generations + .lock() + .expect("generations") + .entry(wallet_id) + .or_insert_with(|| Arc::new(WalletGeneration::new())), + ) + } + + /// Register the wallet anew under the same id, its records kept. + fn reregister(&self, wallet_id: WalletId) { + self.generations + .lock() + .expect("generations") + .insert(wallet_id, Arc::new(WalletGeneration::new())); + } } #[async_trait] @@ -3515,9 +3615,19 @@ mod tests { .copied() .unwrap_or(0), forced, + generation: self.generation(wallet_id), } })) } + + async fn is_registered( + &self, + wallet_id: WalletId, + generation: &Arc, + ) -> bool { + self.views.lock().expect("views").contains_key(&wallet_id) + && Arc::ptr_eq(&self.generation(wallet_id), generation) + } } #[derive(Default)] @@ -5032,6 +5142,7 @@ mod tests { entry.run.as_mut().expect("run").probing = Some(Probing { views, + generation: Arc::new(WalletGeneration::new()), height: 101, due: start.due, in_flight: None, @@ -5327,6 +5438,194 @@ mod tests { ); } + /// How long a test waits to see that a job stays blocked. + const STAYS_BLOCKED: Duration = Duration::from_millis(50); + + /// Start a followed pass and hand its read to the actor: the probe of + /// its first root is spawned, not yet run. + async fn spawn_first_probe(rig: &mut Rig, wallet_id: WalletId, from: u32) { + for height in [from, from + 1] { + rig.handle(Command::Height { wallet_id, height }); + } + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); + } + + /// The race a removal must win: the probe job exists, the wallet's + /// removal takes the gate, lets the wallet go and returns, and the actor + /// has not handled `WalletRemoved` yet. The probe waits at the gate and + /// then finds its registration gone: the removed wallet's transaction is + /// not sent. + #[tokio::test(start_paused = true)] + async fn should_not_send_a_probe_for_a_wallet_removed_before_it_went_out() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + let teardown = rig.source.generation(wallet()).teardown_guard().await; + spawn_first_probe(&mut rig, wallet(), 100).await; + assert!( + timeout(STAYS_BLOCKED, rig.actor.jobs.join_next_with_id()) + .await + .is_err(), + "the probe waits for the removal" + ); + + rig.source.views.lock().expect("views").remove(&wallet()); + drop(teardown); + let probed = rig.actor.jobs.join_next_with_id().await.expect("probe"); + rig.actor.joined(probed); + + assert!(probe.probed().is_empty(), "nothing sent"); + assert!(rig.sent().is_empty(), "and no verdict"); + rig.send(Command::WalletRemoved(wallet())).await; + assert!(probe.probed().is_empty()); + assert!(!rig.actor.wallets.contains_key(&wallet())); + } + + /// The same race with the id registered anew before the probe resumes: + /// the transaction the old registration's pass captured is not sent for + /// it; the new registration's own pass probes it. + #[tokio::test] + async fn should_not_send_a_removed_registrations_probe_for_its_successor() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + let teardown = rig.source.generation(wallet()).teardown_guard().await; + spawn_first_probe(&mut rig, wallet(), 100).await; + + rig.source.reregister(wallet()); + drop(teardown); + let probed = rig.actor.jobs.join_next_with_id().await.expect("probe"); + rig.actor.joined(probed); + assert!(probe.probed().is_empty(), "nothing sent for the old one"); + + rig.send(Command::WalletRemoved(wallet())).await; + rig.send(Command::WalletAdded(wallet())).await; + rig.follow(wallet(), 102).await; + assert_eq!(probe.probed(), vec![txid(1)], "the new one's own pass"); + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] + ); + } + + /// A registration replaced without the resolver being told (a removal + /// that did not go through the manager): the pass that read the old one + /// sends nothing, what was kept for the old one is dropped — the host + /// told to clear its verdict — and the wallet now there starts afresh. + #[tokio::test] + async fn should_start_afresh_after_a_registration_changed_unannounced() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] + ); + let teardown = rig.source.generation(wallet()).teardown_guard().await; + rig.handle(Command::Height { + wallet_id: wallet(), + height: 102, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("read"); + rig.actor.joined(read); + rig.source.reregister(wallet()); + drop(teardown); + + rig.settle().await; + assert_eq!(probe.probed().len(), 1, "nothing sent for the old one"); + assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); + assert!(rig.actor.state.schedules.is_empty()); + + rig.follow(wallet(), 103).await; + assert_eq!(probe.probed().len(), 2, "the registration now there"); + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] + ); + } + + /// A probe that never ends once started, and says when it was dropped. + #[derive(Default)] + struct HeldProbe { + started: Notify, + dropped: Arc, + } + + #[async_trait] + impl AcceptanceProbe for HeldProbe { + async fn probe(&self, _transaction: &Transaction) -> ProbeOutcome { + let _torn_down = DropFlag(Arc::clone(&self.dropped)); + self.started.notify_one(); + std::future::pending().await + } + } + + /// The other order, against the real manager: a probe is out — no node + /// answering — when its wallet is removed. The removal does not wait for + /// the nodes: the probe stops there, and only then does the wallet go. + #[tokio::test] + async fn should_stop_a_probe_already_out_when_its_wallet_is_removed() { + let (manager, wallet_id) = test_platform_wallet_manager().await; + let platform_wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let source = ManagerSource(Arc::clone(platform_wallet.wallet_manager())); + let generation = Arc::clone(platform_wallet.generation()); + let probe = Arc::new(HeldProbe::default()); + let job = { + let probe = Arc::clone(&probe); + let generation = Arc::clone(&generation); + tokio::spawn(async move { + let transaction = tx(1, &[outpoint(90, 0)]); + gated_probe(&source, &*probe, wallet_id, &generation, &transaction).await + }) + }; + timeout(Duration::from_secs(5), probe.started.notified()) + .await + .expect("the probe is out"); + assert!( + generation.lifecycle_is_held(), + "and inside the wallet's gate" + ); + + timeout(Duration::from_secs(5), manager.remove_wallet(&wallet_id)) + .await + .expect("the removal does not wait for the nodes") + .expect("remove"); + + assert!(probe.dropped.load(Ordering::SeqCst), "the probe stopped"); + assert!(job.await.expect("job").is_none(), "with no verdict"); + } + + /// Against the real manager: the registration a read names is the + /// wallet's until the manager lets it go; another under the same id is + /// not it; and once removed, its transactions are not probed. + #[tokio::test] + async fn should_not_probe_for_a_registration_the_manager_removed() { + let (manager, wallet_id) = test_platform_wallet_manager().await; + let platform_wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let source = ManagerSource(Arc::clone(platform_wallet.wallet_manager())); + let read = source + .read(wallet_id, &AtomicBool::new(true), HashSet::new()) + .await + .expect("registered") + .expect("walked"); + assert!(Arc::ptr_eq(&read.generation, platform_wallet.generation())); + assert!(source.is_registered(wallet_id, &read.generation).await); + let another = Arc::new(WalletGeneration::new()); + assert!(!source.is_registered(wallet_id, &another).await); + let probe = ScriptedProbe::new(&[]); + let transaction = tx(1, &[outpoint(90, 0)]); + + let sent = gated_probe(&source, &probe, wallet_id, &read.generation, &transaction).await; + assert!(sent.is_some(), "probed while registered"); + manager.remove_wallet(&wallet_id).await.expect("remove"); + let refused = gated_probe(&source, &probe, wallet_id, &read.generation, &transaction).await; + + assert!(refused.is_none()); + assert_eq!(probe.probed().len(), 1, "nothing sent after the removal"); + } + /// An `Uncertain` result forces its root even if a height pass sent it at /// this height already: one repeat probe is the accepted price of not /// tracking which report came first. @@ -6264,7 +6563,9 @@ mod tests { assert!(matches!( &probed, Ok((_, JobDone::Probed { root, outcome, .. })) - if *root == txid(1) && outcome.verdict == ProbeVerdict::Mined + if *root == txid(1) + && outcome.as_ref().map(|outcome| &outcome.verdict) + == Some(&ProbeVerdict::Mined) )); rig.handle(Command::Settled { diff --git a/packages/rs-platform-wallet/src/wallet/core/generation.rs b/packages/rs-platform-wallet/src/wallet/core/generation.rs index e4585da047d..bb77d3dff48 100644 --- a/packages/rs-platform-wallet/src/wallet/core/generation.rs +++ b/packages/rs-platform-wallet/src/wallet/core/generation.rs @@ -8,9 +8,11 @@ use std::ops::Deref; use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; use dashcore::{OutPoint, Transaction}; -use tokio::sync::{OwnedRwLockWriteGuard, RwLock, RwLockReadGuard}; +use key_wallet_manager::{WalletId, WalletManager}; +use tokio::sync::{watch, OwnedRwLockWriteGuard, RwLock, RwLockReadGuard}; use super::balance::WalletBalance; +use crate::wallet::platform_wallet::PlatformWalletInfo; /// The state one wallet *generation* shares across every handle that names it. /// @@ -63,6 +65,9 @@ pub struct WalletGeneration { /// a retry loop, and the guard must outlive the loop iteration that produced /// the `Arc` it came from. lifecycle: Arc>, + /// Set, for good, once a teardown has asked for the gate (see + /// [`teardown_requested`](Self::teardown_requested)). + leaving: watch::Sender, /// Outpoints currently fenced against re-selection because a broadcast /// dispatch owns them ([`pin_in_broadcast`](Self::pin_in_broadcast)). /// @@ -391,6 +396,7 @@ impl WalletGeneration { Self { balance: WalletBalance::new(), lifecycle: Arc::new(RwLock::new(())), + leaving: watch::Sender::new(false), in_broadcast: fences, #[cfg(test)] settle_boundary_hook: SettleBoundaryHook::default(), @@ -444,10 +450,48 @@ impl WalletGeneration { /// Returns an *owned* guard so it can outlive the `Arc` /// binding it was taken from — the remover resolves the current generation in /// a retry loop and must carry the guard out of the iteration that found it. + /// + /// Asking is final: only a caller that is removing this generation takes + /// it, and from the first poll [`teardown_requested`](Self::teardown_requested) + /// resolves for it. pub async fn teardown_guard(&self) -> OwnedRwLockWriteGuard<()> { + self.leaving.send_replace(true); Arc::clone(&self.lifecycle).write_owned().await } + /// Resolves once a teardown has asked for this generation's gate, and at + /// once from then on. + /// + /// For an operation that holds [`payment_guard`](Self::payment_guard) + /// across network work it can give up without harm: raced against this, it + /// stops and lets go of the gate instead of making the removal wait for + /// its timeouts. + pub(crate) async fn teardown_requested(&self) { + let mut leaving = self.leaving.subscribe(); + // Fails only without a sender, which `self` holds: stop then, too. + let _ = leaving.wait_for(|leaving| *leaving).await; + } + + /// Whether some operation is inside the gate right now. + #[cfg(test)] + pub(crate) fn lifecycle_is_held(&self) -> bool { + self.lifecycle.try_write().is_err() + } + + /// Whether this generation is the one registered under `wallet_id` — a + /// removal, or a re-registration under the same id, ends that. A + /// point-in-time observation: to act on it, hold + /// [`payment_guard`](Self::payment_guard) across the check and the action. + pub(crate) fn is_registered_in( + self: &Arc, + wallet_manager: &WalletManager, + wallet_id: &WalletId, + ) -> bool { + wallet_manager + .get_wallet_info(wallet_id) + .is_some_and(|info| Arc::ptr_eq(&info.generation, self)) + } + /// Recovers from a poisoned mutex rather than panicking: the guarded data /// is a plain count map with no invariant a partial write could break, and /// panicking here would strand every later build and dispatch on this diff --git a/packages/rs-platform-wallet/src/wallet/core/wallet.rs b/packages/rs-platform-wallet/src/wallet/core/wallet.rs index bdc0470445e..b0783cf6b27 100644 --- a/packages/rs-platform-wallet/src/wallet/core/wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/core/wallet.rs @@ -397,8 +397,7 @@ impl CoreWallet { /// observation. pub async fn is_current_generation(&self) -> bool { let wm = self.wallet_manager.read().await; - wm.get_wallet_info(&self.wallet_id) - .is_some_and(|info| Arc::ptr_eq(&info.generation, self.generation())) + self.generation().is_registered_in(&wm, &self.wallet_id) } } From 6676b562d92f36711a83ea75f0d6fb9e6a79763b Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:38:08 +0300 Subject: [PATCH 144/169] fix(platform-wallet): withdraw an abandoned teardown's request; unwind rollbacks by generation Review of the previous commit: - A teardown request was a one-way latch set before the gate was acquired: a removal abandoned while waiting left a still-registered wallet never probed again. The request is now counted and withdrawn when the guard, or the abandoned acquisition, is dropped. - A probe told apart "being removed" (stop, keep what is kept; the root is due again if the removal is abandoned) from "registration gone" (drop what was kept for it). It also races the request against the gate and the wallet-manager lock, not only against the nodes. - The registration and load rollbacks share one helper that removes under the generation's gate and only while that generation is still the one registered: waiting for the gate must not let a rollback delete a same-id registration that arrived meanwhile. The load records each generation at its insert instead of reading it back. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/load.rs | 42 +--- .../src/manager/wallet_lifecycle.rs | 109 +++++++++-- .../src/wallet/core/broadcast_resolver.rs | 179 +++++++++++------- .../src/wallet/core/generation.rs | 121 ++++++++++-- 4 files changed, 311 insertions(+), 140 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index e0ec2f3cac3..1ad7621a5a7 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -106,7 +106,7 @@ impl PlatformWalletManager

{ // restarts. The orphan state is observable across the FFI // boundary with no Swift-side reset path, so transactional // semantics matter for this hydration API. - let mut inserted_in_manager: Vec = Vec::new(); + let mut inserted_in_manager: Vec<(WalletId, Arc)> = Vec::new(); // Re-dispatches owed by this load, held until the rollback point has // passed. See the push site for why they cannot be spawned inline. #[allow(clippy::type_complexity)] @@ -278,7 +278,7 @@ impl PlatformWalletManager

{ break 'load; } } - inserted_in_manager.push(wallet_id); + inserted_in_manager.push((wallet_id, Arc::clone(&generation))); let broadcaster = Arc::new(crate::broadcaster::SpvBroadcaster::new(Arc::clone( &self.spv_manager, @@ -431,34 +431,8 @@ impl PlatformWalletManager

{ // Wait-free, purely for the diagnostics below: an id still in the // map after the rollback is one a same-id re-registration owns. let still_mapped = self.wallets.load(); - if !inserted_in_manager.is_empty() { - // Under each rolled-back generation's lifecycle gate, like any - // removal: an operation that saw the registration live and is - // pushing one of its transactions to the network finishes (or - // gives up) before the wallet goes. Only generations this call - // inserted — the published ones by the identity recorded then, - // the others still ours by construction. Gates first, manager - // lock second. - let unpublished: Vec> = { - let wm = self.wallet_manager.read().await; - inserted_in_manager - .iter() - .filter(|id| !inserted_in_wallets.iter().any(|(w, _)| w == *id)) - .filter_map(|id| wm.get_wallet_info(id)) - .map(|info| Arc::clone(&info.generation)) - .collect() - }; - // Held until the rollback below is done. - let mut teardowns = Vec::new(); - for generation in inserted_in_wallets - .iter() - .map(|(_, generation)| generation) - .chain(&unpublished) - { - teardowns.push(generation.teardown_guard().await); - } - let mut wm = self.wallet_manager.write().await; - for id in &inserted_in_manager { + { + for (id, generation) in &inserted_in_manager { // A published id whose generation is no longer ours belongs // to a newer registration; taking it out of the inner // manager would strip a live wallet of its backing. An id @@ -490,13 +464,7 @@ impl PlatformWalletManager

{ } continue; } - if let Err(e) = self.remove_from_inner(&mut wm, id) { - tracing::warn!( - wallet_id = %hex::encode(id), - error = %e, - "rollback after load failure: remove_wallet failed" - ); - } + self.roll_back_inner(id, generation).await; } } return Err(err); diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 54e82938fae..0cf7296bc71 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -104,6 +104,39 @@ impl PlatformWalletManager

{ removed } + /// Take `generation` — a registration this manager inserted and is now + /// unwinding — back out of the inner wallet manager. + /// + /// Under the generation's lifecycle gate, like any removal (gate first, + /// manager lock second): an operation that saw the registration live and + /// is pushing one of its transactions to the network finishes, or gives + /// up, before the wallet goes. And by identity, not by id: whatever else + /// has come to be registered under `wallet_id` while this waited for the + /// gate is not this call's to remove. + pub(super) async fn roll_back_inner( + &self, + wallet_id: &WalletId, + generation: &Arc, + ) { + let _teardown = generation.teardown_guard().await; + let mut wm = self.wallet_manager.write().await; + if !generation.is_registered_in(&wm, wallet_id) { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + "rollback: the registration to unwind is no longer the one under this id; \ + leaving what is there in place" + ); + return; + } + if let Err(error) = self.remove_from_inner(&mut wm, wallet_id) { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + %error, + "rollback: remove_wallet failed while unwinding a wallet registration" + ); + } + } + /// Create a PlatformWallet from a BIP39 mnemonic phrase. /// /// The mnemonic's language is auto-detected by trying each @@ -575,17 +608,7 @@ impl PlatformWalletManager

{ error = %e, "failed to persist wallet registration changeset" ); - // Under the generation's lifecycle gate, like any removal (gate - // first, manager lock second). - let _teardown = generation.teardown_guard().await; - let mut wm = self.wallet_manager.write().await; - if let Err(remove_err) = self.remove_from_inner(&mut wm, &wallet_id) { - tracing::warn!( - wallet_id = %hex::encode(wallet_id), - error = %remove_err, - "rollback: remove_wallet failed while unwinding a failed wallet registration" - ); - } + self.roll_back_inner(&wallet_id, &generation).await; return Err(PlatformWalletError::from_store_failure(&*self.persister, e)); } @@ -626,16 +649,8 @@ impl PlatformWalletManager

{ error = %e, "failed to restore persisted platform-address state" ); - // Under the generation's lifecycle gate, like any removal. - let _teardown = platform_wallet.generation().teardown_guard().await; - let mut wm = self.wallet_manager.write().await; - if let Err(remove_err) = self.remove_from_inner(&mut wm, &wallet_id) { - tracing::warn!( - wallet_id = %hex::encode(wallet_id), - error = %remove_err, - "rollback: remove_wallet failed while unwinding a failed wallet setup" - ); - } + self.roll_back_inner(&wallet_id, platform_wallet.generation()) + .await; // Wrap the already-typed error rather than stringify it, so // its concrete variant and source chain survive. return Err(PlatformWalletError::from_restore_failure(e)); @@ -2356,3 +2371,55 @@ mod remove_versus_recreate_tests { ); } } + +#[cfg(test)] +mod rollback_tests { + use std::sync::Arc; + use std::time::Duration; + + use tokio::time::timeout; + + use crate::test_support::test_platform_wallet_manager; + use crate::wallet::core::WalletGeneration; + + /// A rollback is a removal: it takes the registration out under its + /// lifecycle gate, so it waits for an operation that saw the wallet live + /// and is still inside — gate first, manager lock second. + #[tokio::test(start_paused = true)] + async fn a_rollback_waits_for_an_operation_inside_the_generations_gate() { + let (manager, wallet_id) = test_platform_wallet_manager().await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let generation = Arc::clone(wallet.generation()); + let payment = generation.payment_guard().await; + let mut rollback = Box::pin(manager.roll_back_inner(&wallet_id, &generation)); + + assert!( + timeout(Duration::from_millis(50), &mut rollback) + .await + .is_err(), + "the rollback waits for the gate" + ); + // Not holding the manager lock while it waits: the operation inside + // the gate still gets it. + assert!(generation.is_registered_in(&*manager.wallet_manager.read().await, &wallet_id)); + + drop(payment); + rollback.await; + assert!(!generation.is_registered_in(&*manager.wallet_manager.read().await, &wallet_id)); + } + + /// By identity, not by id: a registration that is not the one being + /// unwound stays. + #[tokio::test] + async fn a_rollback_leaves_another_registration_under_the_id_in_place() { + let (manager, wallet_id) = test_platform_wallet_manager().await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let unwound = Arc::new(WalletGeneration::new()); + + manager.roll_back_inner(&wallet_id, &unwound).await; + + assert!(wallet + .generation() + .is_registered_in(&*manager.wallet_manager.read().await, &wallet_id)); + } +} diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index f050d77ecfa..dc7eec8dc71 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -833,8 +833,8 @@ fn withdraw_unanswered( roots } -/// A root's probe never produced a verdict (its job panicked): nothing was -/// learned, so it is due again at once. +/// A root's probe never produced a verdict (its job panicked, or stopped for +/// a removal): nothing was learned, so it is due again at once. pub(crate) fn withdraw_send(state: &mut ResolverState, wallet_id: WalletId, root: Txid) { if let Some(schedule) = state.schedules.get_mut(&(wallet_id, root)) { schedule.withdraw(); @@ -963,17 +963,28 @@ pub(crate) trait WalletSource: Send + Sync { async fn is_registered(&self, wallet_id: WalletId, generation: &Arc) -> bool; } -/// Probe `transaction` for the registration it was read from, or not at all: -/// `None` when `generation` is no longer the wallet's, with nothing sent. +/// Why a probe produced no verdict (see [`gated_probe`]). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum NotProbed { + /// A teardown is asking for the registration's gate, or holds it: the + /// probe stopped where it was. The wallet may yet stay — a removal can + /// be abandoned. + Leaving, + /// The registration is no longer the wallet's: nothing was sent. + Unregistered, +} + +/// Probe `transaction` for the registration it was read from, or not at all. /// /// A probe resubmits the transaction, so it follows the contract of /// [`WalletGeneration::payment_guard`]: the gate is held across both the /// liveness check and the network step. A removal therefore either gets in -/// first, and the probe sees the registration gone, or finds the probe -/// inside — which then stops where it is, so the removal waits for no -/// node's answer (a probe is up to eight requests, each with its own -/// timeout). Stopping the job from the actor is not that barrier: the actor -/// learns of a removal only after the manager has let the wallet go. +/// first, and the probe sees it asking or the registration gone, or finds +/// the probe inside — which then stops where it is, so the removal waits +/// neither for the wallet-manager lock nor for any node's answer (a probe is +/// up to eight requests, each with its own timeout). Stopping the job from the actor is +/// not that barrier: the actor learns of a removal only after the manager +/// has let the wallet go. /// /// Lock order is the gate's: gate first, wallet-manager lock (inside /// [`WalletSource::is_registered`]) second. @@ -983,15 +994,17 @@ async fn gated_probe( wallet_id: WalletId, generation: &Arc, transaction: &Transaction, -) -> Option { - let _payment = generation.payment_guard().await; - if !source.is_registered(wallet_id, generation).await { - return None; - } +) -> Result { tokio::select! { biased; - _ = generation.teardown_requested() => None, - outcome = probe.probe(transaction) => Some(outcome), + _ = generation.teardown_requested() => Err(NotProbed::Leaving), + probed = async { + let _payment = generation.payment_guard().await; + if !source.is_registered(wallet_id, generation).await { + return Err(NotProbed::Unregistered); + } + Ok(probe.probe(transaction).await) + } => probed, } } @@ -1196,10 +1209,7 @@ enum JobDone { /// The root it probed: a result for a root no longer in flight (its /// probe was stopped, but had already finished) is not this one's. root: Txid, - /// `None`: the registration the pass read is no longer the wallet's, - /// or is being removed — no verdict, and nothing more is sent for it - /// (see [`gated_probe`]). - outcome: Option, + outcome: Result, }, /// [`NO_ANSWER_RETRY`] passed since a probe at `height` that no node /// answered. @@ -2129,22 +2139,39 @@ impl Actor { self.probe_next(wallet_id); return; }; - let Some(ProbeOutcome { verdict, answered }) = outcome else { - // The registration this pass read is leaving the manager - // or has left it: everything kept for it is stale, as for - // a wallet registered anew. A removal through this manager - // queues `WalletRemoved` too, usually ahead of this - // result; one that does not (through - // `wallet_manager_arc`) leaves a fresh entry, which a - // same-id wallet's events find. - tracing::info!( - wallet_id = %hex::encode(wallet_id), - txid = %root.txid, - "broadcast probe: the wallet's registration is gone; probe dropped" - ); - self.drop_wallet(&wallet_id, "gone"); - self.wallets.insert(wallet_id, WalletEntry::default()); - return; + let ProbeOutcome { verdict, answered } = match outcome { + Ok(outcome) => outcome, + Err(NotProbed::Leaving) => { + // The wallet is being removed: `WalletRemoved` + // follows and drops what is kept for it. Until then + // the pass sends nothing more; should the removal be + // abandoned, the root is due again and the next + // height reads the wallet. + withdraw_send(&mut self.state, wallet_id, root.txid); + if let Some(entry) = self.wallets.get_mut(&wallet_id) { + entry.wait_until = None; + } + self.end_run(wallet_id); + return; + } + Err(NotProbed::Unregistered) => { + // A removal through this manager queues + // `WalletRemoved` before it lets the probe see this, + // and that drops the run: only a registration + // replaced or removed behind the manager + // (`wallet_manager_arc`) gets here. Everything kept + // for the old one is stale, as when a wallet is + // registered anew; the fresh entry is what a same-id + // wallet's events find. + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + txid = %root.txid, + "broadcast probe: the wallet's registration is gone; nothing sent" + ); + self.drop_wallet(&wallet_id, "gone"); + self.wallets.insert(wallet_id, WalletEntry::default()); + return; + } }; if !probing.views.iter().any(|view| view.txid == root.txid) { // Settled or gone while its probe was out: moot. @@ -5438,9 +5465,6 @@ mod tests { ); } - /// How long a test waits to see that a job stays blocked. - const STAYS_BLOCKED: Duration = Duration::from_millis(50); - /// Start a followed pass and hand its read to the actor: the probe of /// its first root is spawned, not yet run. async fn spawn_first_probe(rig: &mut Rig, wallet_id: WalletId, from: u32) { @@ -5451,50 +5475,42 @@ mod tests { rig.actor.joined(read); } - /// The race a removal must win: the probe job exists, the wallet's - /// removal takes the gate, lets the wallet go and returns, and the actor - /// has not handled `WalletRemoved` yet. The probe waits at the gate and - /// then finds its registration gone: the removed wallet's transaction is - /// not sent. - #[tokio::test(start_paused = true)] - async fn should_not_send_a_probe_for_a_wallet_removed_before_it_went_out() { + /// The race a removal must win: the probe job exists when the wallet's + /// removal takes the gate, and the actor has not handled `WalletRemoved` + /// yet. The probe sends nothing — not while the removal runs, not after + /// it returns. + #[tokio::test] + async fn should_not_send_a_probe_for_a_wallet_being_removed() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); let mut rig = enabled(probe.clone()).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); let teardown = rig.source.generation(wallet()).teardown_guard().await; spawn_first_probe(&mut rig, wallet(), 100).await; - assert!( - timeout(STAYS_BLOCKED, rig.actor.jobs.join_next_with_id()) - .await - .is_err(), - "the probe waits for the removal" - ); - - rig.source.views.lock().expect("views").remove(&wallet()); - drop(teardown); - let probed = rig.actor.jobs.join_next_with_id().await.expect("probe"); - rig.actor.joined(probed); + rig.settle().await; assert!(probe.probed().is_empty(), "nothing sent"); assert!(rig.sent().is_empty(), "and no verdict"); + + rig.source.views.lock().expect("views").remove(&wallet()); + drop(teardown); rig.send(Command::WalletRemoved(wallet())).await; assert!(probe.probed().is_empty()); assert!(!rig.actor.wallets.contains_key(&wallet())); } - /// The same race with the id registered anew before the probe resumes: - /// the transaction the old registration's pass captured is not sent for - /// it; the new registration's own pass probes it. + /// A probe job that gets to the gate only after the removal is over — + /// and the id registered anew — finds the registration it was read from + /// replaced: the transaction the old pass captured is not sent for it; + /// the new registration's own pass probes it. #[tokio::test] async fn should_not_send_a_removed_registrations_probe_for_its_successor() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); let mut rig = enabled(probe.clone()).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - let teardown = rig.source.generation(wallet()).teardown_guard().await; spawn_first_probe(&mut rig, wallet(), 100).await; + // The whole removal and the new registration, before the job runs. rig.source.reregister(wallet()); - drop(teardown); let probed = rig.actor.jobs.join_next_with_id().await.expect("probe"); rig.actor.joined(probed); assert!(probe.probed().is_empty(), "nothing sent for the old one"); @@ -5523,7 +5539,6 @@ mod tests { rig.sent(), vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] ); - let teardown = rig.source.generation(wallet()).teardown_guard().await; rig.handle(Command::Height { wallet_id: wallet(), height: 102, @@ -5531,7 +5546,6 @@ mod tests { let read = rig.actor.jobs.join_next_with_id().await.expect("read"); rig.actor.joined(read); rig.source.reregister(wallet()); - drop(teardown); rig.settle().await; assert_eq!(probe.probed().len(), 1, "nothing sent for the old one"); @@ -5546,6 +5560,27 @@ mod tests { ); } + /// A removal that was asked for and then abandoned leaves the wallet + /// registered: the pass it stopped sends nothing and keeps what the + /// host was shown, and the next block probes the root again. + #[tokio::test] + async fn should_probe_again_after_an_abandoned_removal() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.sent(); + let teardown = rig.source.generation(wallet()).teardown_guard().await; + + rig.height(wallet(), 102).await; + assert_eq!(probe.probed().len(), 1, "stopped for the removal"); + assert!(rig.sent().is_empty(), "the verdict shown stands"); + + drop(teardown); + rig.height(wallet(), 103).await; + assert_eq!(probe.probed().len(), 2); + } + /// A probe that never ends once started, and says when it was dropped. #[derive(Default)] struct HeldProbe { @@ -5594,7 +5629,15 @@ mod tests { .expect("remove"); assert!(probe.dropped.load(Ordering::SeqCst), "the probe stopped"); - assert!(job.await.expect("job").is_none(), "with no verdict"); + assert_eq!(job.await.expect("job"), Err(NotProbed::Leaving)); + // The request is over with the removal: only the registration + // check answers now. + let transaction = tx(1, &[outpoint(90, 0)]); + let source = ManagerSource(Arc::clone(platform_wallet.wallet_manager())); + assert_eq!( + gated_probe(&source, &*probe, wallet_id, &generation, &transaction).await, + Err(NotProbed::Unregistered) + ); } /// Against the real manager: the registration a read names is the @@ -5618,11 +5661,11 @@ mod tests { let transaction = tx(1, &[outpoint(90, 0)]); let sent = gated_probe(&source, &probe, wallet_id, &read.generation, &transaction).await; - assert!(sent.is_some(), "probed while registered"); + assert!(sent.is_ok(), "probed while registered"); manager.remove_wallet(&wallet_id).await.expect("remove"); let refused = gated_probe(&source, &probe, wallet_id, &read.generation, &transaction).await; - assert!(refused.is_none()); + assert_eq!(refused, Err(NotProbed::Unregistered)); assert_eq!(probe.probed().len(), 1, "nothing sent after the removal"); } @@ -6565,7 +6608,7 @@ mod tests { Ok((_, JobDone::Probed { root, outcome, .. })) if *root == txid(1) && outcome.as_ref().map(|outcome| &outcome.verdict) - == Some(&ProbeVerdict::Mined) + == Ok(&ProbeVerdict::Mined) )); rig.handle(Command::Settled { diff --git a/packages/rs-platform-wallet/src/wallet/core/generation.rs b/packages/rs-platform-wallet/src/wallet/core/generation.rs index bb77d3dff48..30d7821e936 100644 --- a/packages/rs-platform-wallet/src/wallet/core/generation.rs +++ b/packages/rs-platform-wallet/src/wallet/core/generation.rs @@ -14,6 +14,35 @@ use tokio::sync::{watch, OwnedRwLockWriteGuard, RwLock, RwLockReadGuard}; use super::balance::WalletBalance; use crate::wallet::platform_wallet::PlatformWalletInfo; +/// One teardown's place in [`WalletGeneration::teardown_requested`]'s count, +/// given up when dropped — with the guard, or with a +/// [`teardown_guard`](WalletGeneration::teardown_guard) call abandoned while +/// it waits. +#[derive(Debug)] +struct TeardownRequest(Arc>); + +impl TeardownRequest { + fn new(leaving: &Arc>) -> Self { + leaving.send_modify(|asking| *asking += 1); + Self(Arc::clone(leaving)) + } +} + +impl Drop for TeardownRequest { + fn drop(&mut self) { + self.0.send_modify(|asking| *asking -= 1); + } +} + +/// The exclusive side of a generation's lifecycle gate (see +/// [`WalletGeneration::teardown_guard`]). Owned: it may outlive the +/// `Arc` binding it was taken from. +#[derive(Debug)] +pub struct TeardownGuard { + _gate: OwnedRwLockWriteGuard<()>, + _request: TeardownRequest, +} + /// The state one wallet *generation* shares across every handle that names it. /// /// A "generation" is one live in-memory instance of a logical wallet. Removing a @@ -65,9 +94,10 @@ pub struct WalletGeneration { /// a retry loop, and the guard must outlive the loop iteration that produced /// the `Arc` it came from. lifecycle: Arc>, - /// Set, for good, once a teardown has asked for the gate (see - /// [`teardown_requested`](Self::teardown_requested)). - leaving: watch::Sender, + /// How many teardowns are asking for the gate or hold it (see + /// [`teardown_requested`](Self::teardown_requested)). Shared with their + /// guards, which take themselves off it when dropped. + leaving: Arc>, /// Outpoints currently fenced against re-selection because a broadcast /// dispatch owns them ([`pin_in_broadcast`](Self::pin_in_broadcast)). /// @@ -396,7 +426,7 @@ impl WalletGeneration { Self { balance: WalletBalance::new(), lifecycle: Arc::new(RwLock::new(())), - leaving: watch::Sender::new(false), + leaving: Arc::new(watch::Sender::new(0)), in_broadcast: fences, #[cfg(test)] settle_boundary_hook: SettleBoundaryHook::default(), @@ -451,25 +481,30 @@ impl WalletGeneration { /// binding it was taken from — the remover resolves the current generation in /// a retry loop and must carry the guard out of the iteration that found it. /// - /// Asking is final: only a caller that is removing this generation takes - /// it, and from the first poll [`teardown_requested`](Self::teardown_requested) - /// resolves for it. - pub async fn teardown_guard(&self) -> OwnedRwLockWriteGuard<()> { - self.leaving.send_replace(true); - Arc::clone(&self.lifecycle).write_owned().await + /// From this future's first poll until the guard is dropped — or until + /// the future is, if it is abandoned while still waiting — + /// [`teardown_requested`](Self::teardown_requested) resolves. + pub async fn teardown_guard(&self) -> TeardownGuard { + let request = TeardownRequest::new(&self.leaving); + let gate = Arc::clone(&self.lifecycle).write_owned().await; + TeardownGuard { + _gate: gate, + _request: request, + } } - /// Resolves once a teardown has asked for this generation's gate, and at - /// once from then on. + /// Resolves while a teardown is asking for this generation's gate or + /// holds it; pending otherwise. /// /// For an operation that holds [`payment_guard`](Self::payment_guard) /// across network work it can give up without harm: raced against this, it /// stops and lets go of the gate instead of making the removal wait for - /// its timeouts. + /// its timeouts. Not a liveness check — a teardown that was abandoned + /// leaves the generation registered and this pending again. pub(crate) async fn teardown_requested(&self) { let mut leaving = self.leaving.subscribe(); // Fails only without a sender, which `self` holds: stop then, too. - let _ = leaving.wait_for(|leaving| *leaving).await; + let _ = leaving.wait_for(|asking| *asking > 0).await; } /// Whether some operation is inside the gate right now. @@ -1544,3 +1579,61 @@ mod tests { assert_eq!(fired.load(Ordering::SeqCst), 1); } } + +#[cfg(test)] +mod teardown_request_tests { + use std::time::Duration; + + use tokio::time::timeout; + + use super::*; + + /// Long enough for anything that was going to resolve to have done so. + const SETTLED: Duration = Duration::from_millis(50); + + async fn is_requested(generation: &WalletGeneration) -> bool { + timeout(SETTLED, generation.teardown_requested()) + .await + .is_ok() + } + + /// A teardown is "requested" from the moment it asks for the gate — while + /// it still waits behind a payment operation — until its guard is dropped. + #[tokio::test(start_paused = true)] + async fn should_report_a_teardown_from_its_request_until_its_guard_is_dropped() { + let generation = WalletGeneration::new(); + assert!(!is_requested(&generation).await); + let payment = generation.payment_guard().await; + let mut teardown = Box::pin(generation.teardown_guard()); + assert!( + timeout(SETTLED, &mut teardown).await.is_err(), + "waits behind the payment operation" + ); + + assert!(is_requested(&generation).await, "asked, not yet inside"); + drop(payment); + let guard = teardown.await; + assert!(is_requested(&generation).await, "inside"); + drop(guard); + assert!(!is_requested(&generation).await); + } + + /// A teardown given up while it waits for the gate leaves no trace: the + /// generation is still registered, and its operations must go on. + #[tokio::test(start_paused = true)] + async fn should_withdraw_the_request_of_an_abandoned_teardown() { + let generation = WalletGeneration::new(); + let _payment = generation.payment_guard().await; + let mut outlasting = Box::pin(generation.teardown_guard()); + assert!(timeout(SETTLED, &mut outlasting).await.is_err()); + { + let mut abandoned = Box::pin(generation.teardown_guard()); + assert!(timeout(SETTLED, &mut abandoned).await.is_err()); + } + assert!(is_requested(&generation).await, "one still asks"); + + drop(outlasting); + + assert!(!is_requested(&generation).await); + } +} From 26517a7225179e15f446c32f2d19e1ea042f11cb Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Mon, 12 Oct 2026 14:51:38 +0300 Subject: [PATCH 145/169] fix(platform-wallet): keep a forced probe a removal stopped; roll a load back in one step Review of the two previous commits: - A probe stopped for a removal no longer drops what the pass was forced to probe, starts no every-block window for a probe that never went out, and is not started over while the removal lasts. - A registration found gone at the gate takes the same fail-safe as one read as gone, instead of a second one that made a wallet entry itself. - The load rollback takes every gate first and then undoes everything under one manager write lock, as before, instead of a wallet at a time. - `TeardownGuard` is exported beside `WalletGeneration`. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/load.rs | 15 +- .../src/manager/wallet_lifecycle.rs | 35 ++-- .../src/wallet/core/broadcast_resolver.rs | 164 +++++++++++++----- .../rs-platform-wallet/src/wallet/core/mod.rs | 2 +- 4 files changed, 160 insertions(+), 56 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 1ad7621a5a7..900c806563c 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -404,6 +404,16 @@ impl PlatformWalletManager

{ } if let Some(err) = load_error { + // The whole rollback runs under the lifecycle gate of every + // generation this call inserted, taken before anything is undone + // (gates first, manager lock second): an operation that saw one + // of them live and is pushing its transaction to the network + // finishes, or gives up, before the wallet goes — and the + // rollback itself stays one step, not one per gate it waits for. + let mut _teardowns = Vec::with_capacity(inserted_in_manager.len()); + for (_, generation) in &inserted_in_manager { + _teardowns.push(generation.teardown_guard().await); + } // Walk back every wallet committed in this call so the // manager state matches what it was before. Order: // remove from `self.wallets` first (UI surface), then @@ -431,7 +441,8 @@ impl PlatformWalletManager

{ // Wait-free, purely for the diagnostics below: an id still in the // map after the rollback is one a same-id re-registration owns. let still_mapped = self.wallets.load(); - { + if !inserted_in_manager.is_empty() { + let mut wm = self.wallet_manager.write().await; for (id, generation) in &inserted_in_manager { // A published id whose generation is no longer ours belongs // to a newer registration; taking it out of the inner @@ -464,7 +475,7 @@ impl PlatformWalletManager

{ } continue; } - self.roll_back_inner(id, generation).await; + self.roll_back_from_inner(&mut wm, id, generation); } } return Err(err); diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 0cf7296bc71..da6f17846ab 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -107,12 +107,13 @@ impl PlatformWalletManager

{ /// Take `generation` — a registration this manager inserted and is now /// unwinding — back out of the inner wallet manager. /// - /// Under the generation's lifecycle gate, like any removal (gate first, - /// manager lock second): an operation that saw the registration live and - /// is pushing one of its transactions to the network finishes, or gives - /// up, before the wallet goes. And by identity, not by id: whatever else - /// has come to be registered under `wallet_id` while this waited for the - /// gate is not this call's to remove. + /// Under the generation's lifecycle gate (gate first, manager lock + /// second): an operation that saw the registration live and is pushing + /// one of its transactions to the network finishes, or gives up, before + /// the wallet goes. Only the inner removal: a rollback has no deferred + /// state to sweep the way + /// [`remove_wallet_with_teardown`](Self::remove_wallet_with_teardown) + /// does. pub(super) async fn roll_back_inner( &self, wallet_id: &WalletId, @@ -120,7 +121,21 @@ impl PlatformWalletManager

{ ) { let _teardown = generation.teardown_guard().await; let mut wm = self.wallet_manager.write().await; - if !generation.is_registered_in(&wm, wallet_id) { + self.roll_back_from_inner(&mut wm, wallet_id, generation); + } + + /// The removal of [`roll_back_inner`](Self::roll_back_inner), for a + /// caller that holds `generation`'s teardown guard and the manager's + /// write guard. By identity, not by id: whatever else has come to be + /// registered under `wallet_id` while the caller waited for the gate is + /// not its to remove. + pub(super) fn roll_back_from_inner( + &self, + wm: &mut WalletManager, + wallet_id: &WalletId, + generation: &Arc, + ) { + if !generation.is_registered_in(wm, wallet_id) { tracing::warn!( wallet_id = %hex::encode(wallet_id), "rollback: the registration to unwind is no longer the one under this id; \ @@ -128,7 +143,7 @@ impl PlatformWalletManager

{ ); return; } - if let Err(error) = self.remove_from_inner(&mut wm, wallet_id) { + if let Err(error) = self.remove_from_inner(wm, wallet_id) { tracing::warn!( wallet_id = %hex::encode(wallet_id), %error, @@ -2386,7 +2401,7 @@ mod rollback_tests { /// lifecycle gate, so it waits for an operation that saw the wallet live /// and is still inside — gate first, manager lock second. #[tokio::test(start_paused = true)] - async fn a_rollback_waits_for_an_operation_inside_the_generations_gate() { + async fn should_make_a_rollback_wait_for_an_operation_inside_the_generations_gate() { let (manager, wallet_id) = test_platform_wallet_manager().await; let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); let generation = Arc::clone(wallet.generation()); @@ -2411,7 +2426,7 @@ mod rollback_tests { /// By identity, not by id: a registration that is not the one being /// unwound stays. #[tokio::test] - async fn a_rollback_leaves_another_registration_under_the_id_in_place() { + async fn should_leave_another_registration_under_the_id_in_place_on_rollback() { let (manager, wallet_id) = test_platform_wallet_manager().await; let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); let unwound = Arc::new(WalletGeneration::new()); diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index dc7eec8dc71..b2d58b0cf37 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -794,16 +794,19 @@ pub(crate) fn mark_sent( root: Txid, height: u32, anchor: Option, -) { +) -> bool { + let mut anchored = false; if let Some(schedule) = state.schedules.get_mut(&(wallet_id, root)) { // A root's every-block window starts at its first probe by a pass // that follows the tip — not while it waits in a backlog, nor when a // pass that planned it gave way before sending it. if let Some(anchor) = anchor { + anchored = schedule.window_start.is_none(); schedule.anchor(anchor); } schedule.probed_at(height); } + anchored } /// Whether a root's last probe reached a node (see @@ -1273,6 +1276,9 @@ struct Probing { due: VecDeque, /// The root whose probe is out now. in_flight: Option, + /// That probe started its root's every-block window: taken back if the + /// probe is stopped before anything goes out. + anchored: bool, } impl Probing { @@ -1756,6 +1762,28 @@ impl Actor { } } + /// The registration a pass read, or was to read, is not in the wallet + /// manager. This manager's own removals queue `WalletRemoved` under the + /// same guard, and commands are handled before job results, so only a + /// removal or replacement through `wallet_manager_arc` gets here. Fail + /// safe: forget its state, the host told to clear what it showed, and + /// end the run. + fn registration_gone(&mut self, wallet_id: WalletId) { + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; + tracing::error!( + wallet_id = %hex::encode(wallet_id), + "broadcast probe: a registered wallet read as gone" + ); + // Idle, not re-read every block: a same-id wallet's events or an + // Uncertain wake it. + entry.wait_until = Some(u32::MAX); + let events = self.forget_wallet(&wallet_id, "gone"); + deliver(&self.sink, events); + self.end_run(wallet_id); + } + /// Drop the wallet's entry, stop its pass and forget its state, the host /// told to clear what it showed (logged with `trigger`). Report lookups /// still out are made again, and the wallet leaves `reported`. @@ -1938,23 +1966,7 @@ impl Actor { return; } match read { - None => { - // Gone from the wallet manager. This manager's own - // removals queue WalletRemoved under the same guard, - // and commands are handled before job results, so only - // a removal through `wallet_manager_arc` gets here. - // Fail safe: forget its state and end the run. - tracing::error!( - wallet_id = %hex::encode(wallet_id), - "broadcast probe: a registered wallet read as gone" - ); - // Idle, not re-read every block: a same-id wallet's - // events or an Uncertain wake it. - entry.wait_until = Some(u32::MAX); - let events = self.forget_wallet(&wallet_id, "gone"); - deliver(&self.sink, events); - self.end_run(wallet_id); - } + None => self.registration_gone(wallet_id), Some(None) => self.end_run(wallet_id), Some(Some(read)) => { // A request queued before any height was reported @@ -2037,6 +2049,7 @@ impl Actor { height, due: start.due, in_flight: None, + anchored: false, }); // A forced request that came while this pass read. yield_to_forced(entry); @@ -2128,6 +2141,7 @@ impl Actor { // could be stopped: not the root in flight now. return; } + let height_of_pass = probing.height; let Some(root) = probing.in_flight.take() else { // Cannot happen: a settle that clears the root in flight // re-arms the run or ends it at once. Log it and carry on @@ -2143,33 +2157,38 @@ impl Actor { Ok(outcome) => outcome, Err(NotProbed::Leaving) => { // The wallet is being removed: `WalletRemoved` - // follows and drops what is kept for it. Until then - // the pass sends nothing more; should the removal be - // abandoned, the root is due again and the next - // height reads the wallet. + // follows and drops what is kept for it. Should the + // removal be abandoned instead, nothing is lost: the + // root is as if never sent, and what the pass was + // forced to probe waits for the next height or + // report — not started now, when it would only be + // stopped again. + let anchored = probing.anchored; withdraw_send(&mut self.state, wallet_id, root.txid); - if let Some(entry) = self.wallets.get_mut(&wallet_id) { - entry.wait_until = None; + if anchored { + if let Some(schedule) = + self.state.schedules.get_mut(&(wallet_id, root.txid)) + { + schedule.window_start = None; + } + } + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; + entry.wait_until = None; + if let Some(run) = entry.run.take() { + if !run.forced.is_empty() || !run.retried.is_empty() { + let pending = + entry.pending.get_or_insert_with(PendingPass::default); + pending.height = pending.height.max(height_of_pass); + pending.forced.extend(run.forced); + pending.retried.extend(run.retried); + } } - self.end_run(wallet_id); return; } Err(NotProbed::Unregistered) => { - // A removal through this manager queues - // `WalletRemoved` before it lets the probe see this, - // and that drops the run: only a registration - // replaced or removed behind the manager - // (`wallet_manager_arc`) gets here. Everything kept - // for the old one is stale, as when a wallet is - // registered anew; the fresh entry is what a same-id - // wallet's events find. - tracing::warn!( - wallet_id = %hex::encode(wallet_id), - txid = %root.txid, - "broadcast probe: the wallet's registration is gone; nothing sent" - ); - self.drop_wallet(&wallet_id, "gone"); - self.wallets.insert(wallet_id, WalletEntry::default()); + self.registration_gone(wallet_id); return; } }; @@ -2319,7 +2338,7 @@ impl Actor { }); self.job_runs .insert(run.abort.id(), (wallet_id, id, Some(root_txid))); - mark_sent( + probing.anchored = mark_sent( &mut self.state, wallet_id, root.txid, @@ -5173,6 +5192,7 @@ mod tests { height: 101, due: start.due, in_flight: None, + anchored: false, }); yield_to_forced(&mut entry); @@ -5528,7 +5548,8 @@ mod tests { /// A registration replaced without the resolver being told (a removal /// that did not go through the manager): the pass that read the old one /// sends nothing, what was kept for the old one is dropped — the host - /// told to clear its verdict — and the wallet now there starts afresh. + /// told to clear its verdict — and the wallet now there starts afresh at + /// its first event. #[tokio::test] async fn should_start_afresh_after_a_registration_changed_unannounced() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); @@ -5552,7 +5573,13 @@ mod tests { assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); assert!(rig.actor.state.schedules.is_empty()); - rig.follow(wallet(), 103).await; + // Idle until the wallet now there shows up in an event. + rig.send(Command::Seen { + wallet_id: wallet(), + touched: true, + }) + .await; + rig.height(wallet(), 103).await; assert_eq!(probe.probed().len(), 2, "the registration now there"); assert_eq!( rig.sent(), @@ -5581,6 +5608,57 @@ mod tests { assert_eq!(probe.probed().len(), 2); } + /// A report's forced probe stopped by a removal that is then abandoned + /// is not lost: it goes out with the wallet's next pass, though the + /// wallet is catching up and probes nothing by schedule. And a root's + /// every-block window does not start at a probe that never went out. + #[tokio::test] + async fn should_keep_a_forced_probe_stopped_by_an_abandoned_removal() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + let teardown = rig.source.generation(wallet()).teardown_guard().await; + + rig.send(Command::Uncertain(txid(1))).await; + assert!(probe.probed().is_empty(), "stopped for the removal"); + let entry = &rig.actor.wallets[&wallet()]; + assert!(entry.run.is_none(), "and not started over while it lasts"); + assert!(entry + .pending + .as_ref() + .is_some_and(|pending| pending.forced.contains(&txid(1)))); + + drop(teardown); + // The first step after launch: catch-up, forced roots only. + rig.height(wallet(), 100).await; + assert_eq!(probe.probed(), vec![txid(1)]); + } + + /// A followed probe stopped by a removal does not start its root's + /// every-block window: nothing went out. + #[tokio::test] + async fn should_not_start_a_window_at_a_probe_stopped_by_a_removal() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + let teardown = rig.source.generation(wallet()).teardown_guard().await; + + rig.follow(wallet(), 100).await; + assert!(probe.probed().is_empty()); + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].window_start, + None + ); + + drop(teardown); + rig.height(wallet(), 102).await; + assert_eq!(probe.probed(), vec![txid(1)]); + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].window_start, + Some(102) + ); + } + /// A probe that never ends once started, and says when it was dropped. #[derive(Default)] struct HeldProbe { diff --git a/packages/rs-platform-wallet/src/wallet/core/mod.rs b/packages/rs-platform-wallet/src/wallet/core/mod.rs index 06fba069222..d1ff1c9c8a9 100644 --- a/packages/rs-platform-wallet/src/wallet/core/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/core/mod.rs @@ -12,8 +12,8 @@ pub mod wallet; pub use balance::WalletBalance; pub use balance_handler::BalanceUpdateHandler; -pub use generation::WalletGeneration; pub(crate) use generation::{InBroadcastFences, InBroadcastPin}; +pub use generation::{TeardownGuard, WalletGeneration}; pub use spend_observer::SpendObservationHandler; pub(crate) use transaction::{ build_error_awaiting_network, resolve_source_accounts, trial_with_waiting_coins, From ae5f49a4a315c6db209b8c43e4963b71301f7f60 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Mon, 12 Oct 2026 15:06:26 +0300 Subject: [PATCH 146/169] fix(platform-wallet): leave the rollbacks ungated; undo a stopped probe exactly Review of the three previous commits: - Gating the registration and load rollbacks kept producing edge cases of its own (one wallet's send stalling the rollback of all, a resolver entry left behind) for no user-visible gain: a rollback is not a removal anyone asked for, and the probe resubmits the same signed bytes. They are back as on the base branch; the probe's doc says where the gate ends. - A probe stopped for a removal puts its root's schedule back as it was before the probe was marked sent, instead of approximating that, and lets a sync completion at the same height run the pass again. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/load.rs | 24 ++-- .../src/manager/wallet_lifecycle.rs | 119 +++--------------- .../src/wallet/core/broadcast_resolver.rs | 55 ++++---- 3 files changed, 55 insertions(+), 143 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 900c806563c..5c8947fbb09 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -106,7 +106,7 @@ impl PlatformWalletManager

{ // restarts. The orphan state is observable across the FFI // boundary with no Swift-side reset path, so transactional // semantics matter for this hydration API. - let mut inserted_in_manager: Vec<(WalletId, Arc)> = Vec::new(); + let mut inserted_in_manager: Vec = Vec::new(); // Re-dispatches owed by this load, held until the rollback point has // passed. See the push site for why they cannot be spawned inline. #[allow(clippy::type_complexity)] @@ -278,7 +278,7 @@ impl PlatformWalletManager

{ break 'load; } } - inserted_in_manager.push((wallet_id, Arc::clone(&generation))); + inserted_in_manager.push(wallet_id); let broadcaster = Arc::new(crate::broadcaster::SpvBroadcaster::new(Arc::clone( &self.spv_manager, @@ -404,16 +404,6 @@ impl PlatformWalletManager

{ } if let Some(err) = load_error { - // The whole rollback runs under the lifecycle gate of every - // generation this call inserted, taken before anything is undone - // (gates first, manager lock second): an operation that saw one - // of them live and is pushing its transaction to the network - // finishes, or gives up, before the wallet goes — and the - // rollback itself stays one step, not one per gate it waits for. - let mut _teardowns = Vec::with_capacity(inserted_in_manager.len()); - for (_, generation) in &inserted_in_manager { - _teardowns.push(generation.teardown_guard().await); - } // Walk back every wallet committed in this call so the // manager state matches what it was before. Order: // remove from `self.wallets` first (UI surface), then @@ -443,7 +433,7 @@ impl PlatformWalletManager

{ let still_mapped = self.wallets.load(); if !inserted_in_manager.is_empty() { let mut wm = self.wallet_manager.write().await; - for (id, generation) in &inserted_in_manager { + for id in &inserted_in_manager { // A published id whose generation is no longer ours belongs // to a newer registration; taking it out of the inner // manager would strip a live wallet of its backing. An id @@ -475,7 +465,13 @@ impl PlatformWalletManager

{ } continue; } - self.roll_back_from_inner(&mut wm, id, generation); + if let Err(e) = self.remove_from_inner(&mut wm, id) { + tracing::warn!( + wallet_id = %hex::encode(id), + error = %e, + "rollback after load failure: remove_wallet failed" + ); + } } } return Err(err); diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index da6f17846ab..115f5a9071d 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -104,54 +104,6 @@ impl PlatformWalletManager

{ removed } - /// Take `generation` — a registration this manager inserted and is now - /// unwinding — back out of the inner wallet manager. - /// - /// Under the generation's lifecycle gate (gate first, manager lock - /// second): an operation that saw the registration live and is pushing - /// one of its transactions to the network finishes, or gives up, before - /// the wallet goes. Only the inner removal: a rollback has no deferred - /// state to sweep the way - /// [`remove_wallet_with_teardown`](Self::remove_wallet_with_teardown) - /// does. - pub(super) async fn roll_back_inner( - &self, - wallet_id: &WalletId, - generation: &Arc, - ) { - let _teardown = generation.teardown_guard().await; - let mut wm = self.wallet_manager.write().await; - self.roll_back_from_inner(&mut wm, wallet_id, generation); - } - - /// The removal of [`roll_back_inner`](Self::roll_back_inner), for a - /// caller that holds `generation`'s teardown guard and the manager's - /// write guard. By identity, not by id: whatever else has come to be - /// registered under `wallet_id` while the caller waited for the gate is - /// not its to remove. - pub(super) fn roll_back_from_inner( - &self, - wm: &mut WalletManager, - wallet_id: &WalletId, - generation: &Arc, - ) { - if !generation.is_registered_in(wm, wallet_id) { - tracing::warn!( - wallet_id = %hex::encode(wallet_id), - "rollback: the registration to unwind is no longer the one under this id; \ - leaving what is there in place" - ); - return; - } - if let Err(error) = self.remove_from_inner(wm, wallet_id) { - tracing::warn!( - wallet_id = %hex::encode(wallet_id), - %error, - "rollback: remove_wallet failed while unwinding a wallet registration" - ); - } - } - /// Create a PlatformWallet from a BIP39 mnemonic phrase. /// /// The mnemonic's language is auto-detected by trying each @@ -623,7 +575,14 @@ impl PlatformWalletManager

{ error = %e, "failed to persist wallet registration changeset" ); - self.roll_back_inner(&wallet_id, &generation).await; + let mut wm = self.wallet_manager.write().await; + if let Err(remove_err) = self.remove_from_inner(&mut wm, &wallet_id) { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = %remove_err, + "rollback: remove_wallet failed while unwinding a failed wallet registration" + ); + } return Err(PlatformWalletError::from_store_failure(&*self.persister, e)); } @@ -664,8 +623,14 @@ impl PlatformWalletManager

{ error = %e, "failed to restore persisted platform-address state" ); - self.roll_back_inner(&wallet_id, platform_wallet.generation()) - .await; + let mut wm = self.wallet_manager.write().await; + if let Err(remove_err) = self.remove_from_inner(&mut wm, &wallet_id) { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = %remove_err, + "rollback: remove_wallet failed while unwinding a failed wallet setup" + ); + } // Wrap the already-typed error rather than stringify it, so // its concrete variant and source chain survive. return Err(PlatformWalletError::from_restore_failure(e)); @@ -2386,55 +2351,3 @@ mod remove_versus_recreate_tests { ); } } - -#[cfg(test)] -mod rollback_tests { - use std::sync::Arc; - use std::time::Duration; - - use tokio::time::timeout; - - use crate::test_support::test_platform_wallet_manager; - use crate::wallet::core::WalletGeneration; - - /// A rollback is a removal: it takes the registration out under its - /// lifecycle gate, so it waits for an operation that saw the wallet live - /// and is still inside — gate first, manager lock second. - #[tokio::test(start_paused = true)] - async fn should_make_a_rollback_wait_for_an_operation_inside_the_generations_gate() { - let (manager, wallet_id) = test_platform_wallet_manager().await; - let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); - let generation = Arc::clone(wallet.generation()); - let payment = generation.payment_guard().await; - let mut rollback = Box::pin(manager.roll_back_inner(&wallet_id, &generation)); - - assert!( - timeout(Duration::from_millis(50), &mut rollback) - .await - .is_err(), - "the rollback waits for the gate" - ); - // Not holding the manager lock while it waits: the operation inside - // the gate still gets it. - assert!(generation.is_registered_in(&*manager.wallet_manager.read().await, &wallet_id)); - - drop(payment); - rollback.await; - assert!(!generation.is_registered_in(&*manager.wallet_manager.read().await, &wallet_id)); - } - - /// By identity, not by id: a registration that is not the one being - /// unwound stays. - #[tokio::test] - async fn should_leave_another_registration_under_the_id_in_place_on_rollback() { - let (manager, wallet_id) = test_platform_wallet_manager().await; - let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); - let unwound = Arc::new(WalletGeneration::new()); - - manager.roll_back_inner(&wallet_id, &unwound).await; - - assert!(wallet - .generation() - .is_registered_in(&*manager.wallet_manager.read().await, &wallet_id)); - } -} diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index b2d58b0cf37..b32774f3ba7 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -8,8 +8,8 @@ //! [`AcceptanceProbe`](crate::broadcast_probe::AcceptanceProbe) and publishes //! the verdict; it **changes nothing** in the wallet. A probe resubmits the //! signed transaction, so it goes out only under the lifecycle gate of the -//! registration it was read from, never for one that has left the manager, -//! and a removal stops the one in flight (see [`gated_probe`]). Verdicts are positive +//! registration it was read from, and a wallet's removal stops the one in +//! flight and lets none start after it (see [`gated_probe`]). Verdicts are positive //! only — accepted or mined — or unresolved: no answer the probe gets proves //! that a transaction can never land (see //! [`broadcast_probe`](crate::broadcast_probe)). @@ -794,19 +794,16 @@ pub(crate) fn mark_sent( root: Txid, height: u32, anchor: Option, -) -> bool { - let mut anchored = false; +) { if let Some(schedule) = state.schedules.get_mut(&(wallet_id, root)) { // A root's every-block window starts at its first probe by a pass // that follows the tip — not while it waits in a backlog, nor when a // pass that planned it gave way before sending it. if let Some(anchor) = anchor { - anchored = schedule.window_start.is_none(); schedule.anchor(anchor); } schedule.probed_at(height); } - anchored } /// Whether a root's last probe reached a node (see @@ -836,8 +833,8 @@ fn withdraw_unanswered( roots } -/// A root's probe never produced a verdict (its job panicked, or stopped for -/// a removal): nothing was learned, so it is due again at once. +/// A root's probe never produced a verdict (its job panicked): nothing was +/// learned, so it is due again at once. pub(crate) fn withdraw_send(state: &mut ResolverState, wallet_id: WalletId, root: Txid) { if let Some(schedule) = state.schedules.get_mut(&(wallet_id, root)) { schedule.withdraw(); @@ -989,6 +986,11 @@ enum NotProbed { /// not that barrier: the actor learns of a removal only after the manager /// has let the wallet go. /// +/// "Removal" is the manager's `remove_wallet`. The rollback of a +/// registration or load that failed half-way takes no gate: a probe already +/// past the check may finish for a wallet rolled back meanwhile — the same +/// signed bytes, for a wallet nobody asked to remove. +/// /// Lock order is the gate's: gate first, wallet-manager lock (inside /// [`WalletSource::is_registered`]) second. async fn gated_probe( @@ -1276,9 +1278,9 @@ struct Probing { due: VecDeque, /// The root whose probe is out now. in_flight: Option, - /// That probe started its root's every-block window: taken back if the - /// probe is stopped before anything goes out. - anchored: bool, + /// That root's schedule before its probe was marked sent: put back if + /// the probe is stopped before anything goes out. + before: Option, } impl Probing { @@ -2049,7 +2051,7 @@ impl Actor { height, due: start.due, in_flight: None, - anchored: false, + before: None, }); // A forced request that came while this pass read. yield_to_forced(entry); @@ -2159,23 +2161,23 @@ impl Actor { // The wallet is being removed: `WalletRemoved` // follows and drops what is kept for it. Should the // removal be abandoned instead, nothing is lost: the - // root is as if never sent, and what the pass was - // forced to probe waits for the next height or - // report — not started now, when it would only be - // stopped again. - let anchored = probing.anchored; - withdraw_send(&mut self.state, wallet_id, root.txid); - if anchored { - if let Some(schedule) = - self.state.schedules.get_mut(&(wallet_id, root.txid)) - { - schedule.window_start = None; - } + // root's schedule is as before this probe, and what + // the pass was to probe waits for the next height, + // sync completion or report — not started now, when + // it would only be stopped again. + if let (Some(before), Some(schedule)) = ( + probing.before, + self.state.schedules.get_mut(&(wallet_id, root.txid)), + ) { + *schedule = before; } let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; entry.wait_until = None; + // This height's pass sent nothing for the rest of its + // roots: a sync completion at it may run another. + entry.followed_pass_at = None; if let Some(run) = entry.run.take() { if !run.forced.is_empty() || !run.retried.is_empty() { let pending = @@ -2338,7 +2340,8 @@ impl Actor { }); self.job_runs .insert(run.abort.id(), (wallet_id, id, Some(root_txid))); - probing.anchored = mark_sent( + probing.before = self.state.schedules.get(&(wallet_id, root.txid)).copied(); + mark_sent( &mut self.state, wallet_id, root.txid, @@ -5192,7 +5195,7 @@ mod tests { height: 101, due: start.due, in_flight: None, - anchored: false, + before: None, }); yield_to_forced(&mut entry); From 9f85ccb8b2ced39fcc3f068fd502531c23f29110 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Mon, 12 Oct 2026 15:21:54 +0300 Subject: [PATCH 147/169] refactor(platform-wallet): wait a removal out inside the gated probe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review of the previous commits: everything left to fix sat in the actor's handling of a removal that is asked for and then given up — the schedule undo, the carried forced roots, the pass that must not start over. The probe job now handles it itself: on a teardown request it drops the probe and the gate, waits until no teardown asks any more, and starts over — to find the registration gone, or, after an abandoned removal, to probe after all. The actor only ever sees a verdict or "registration gone", and no pass starts over while a removal lasts. `TeardownGuard` is `#[must_use]`, as the tokio guard it replaced. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/core/broadcast_resolver.rs | 265 +++++++----------- .../src/wallet/core/generation.rs | 12 + 2 files changed, 110 insertions(+), 167 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index b32774f3ba7..473a7420d80 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -8,8 +8,8 @@ //! [`AcceptanceProbe`](crate::broadcast_probe::AcceptanceProbe) and publishes //! the verdict; it **changes nothing** in the wallet. A probe resubmits the //! signed transaction, so it goes out only under the lifecycle gate of the -//! registration it was read from, and a wallet's removal stops the one in -//! flight and lets none start after it (see [`gated_probe`]). Verdicts are positive +//! registration it was read from: a wallet's removal stops the one in +//! flight and none goes out after it (see [`gated_probe`]). Verdicts are positive //! only — accepted or mined — or unresolved: no answer the probe gets proves //! that a transaction can never land (see //! [`broadcast_probe`](crate::broadcast_probe)). @@ -963,28 +963,21 @@ pub(crate) trait WalletSource: Send + Sync { async fn is_registered(&self, wallet_id: WalletId, generation: &Arc) -> bool; } -/// Why a probe produced no verdict (see [`gated_probe`]). -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum NotProbed { - /// A teardown is asking for the registration's gate, or holds it: the - /// probe stopped where it was. The wallet may yet stay — a removal can - /// be abandoned. - Leaving, - /// The registration is no longer the wallet's: nothing was sent. - Unregistered, -} - -/// Probe `transaction` for the registration it was read from, or not at all. +/// Probe `transaction` for the registration it was read from, or not at all: +/// `None` when `generation` is no longer the wallet's, with nothing sent. /// /// A probe resubmits the transaction, so it follows the contract of /// [`WalletGeneration::payment_guard`]: the gate is held across both the /// liveness check and the network step. A removal therefore either gets in -/// first, and the probe sees it asking or the registration gone, or finds -/// the probe inside — which then stops where it is, so the removal waits -/// neither for the wallet-manager lock nor for any node's answer (a probe is -/// up to eight requests, each with its own timeout). Stopping the job from the actor is -/// not that barrier: the actor learns of a removal only after the manager -/// has let the wallet go. +/// first, and the probe waits it out and then sees the registration gone, or +/// finds the probe inside — which then stops where it is and lets go of the +/// gate, so the removal waits neither for the wallet-manager lock nor for +/// any node's answer (a probe is up to eight requests, each with its own +/// timeout). Stopping the job from the actor is not that barrier: the actor +/// learns of a removal only after the manager has let the wallet go. +/// +/// A removal that is given up leaves the wallet registered: the probe then +/// starts over — the same signed bytes again. /// /// "Removal" is the manager's `remove_wallet`. The rollback of a /// registration or load that failed half-way takes no gate: a probe already @@ -999,17 +992,20 @@ async fn gated_probe( wallet_id: WalletId, generation: &Arc, transaction: &Transaction, -) -> Result { - tokio::select! { - biased; - _ = generation.teardown_requested() => Err(NotProbed::Leaving), - probed = async { - let _payment = generation.payment_guard().await; - if !source.is_registered(wallet_id, generation).await { - return Err(NotProbed::Unregistered); - } - Ok(probe.probe(transaction).await) - } => probed, +) -> Option { + loop { + tokio::select! { + biased; + _ = generation.teardown_requested() => {} + probed = async { + let _payment = generation.payment_guard().await; + if !source.is_registered(wallet_id, generation).await { + return None; + } + Some(probe.probe(transaction).await) + } => return probed, + } + generation.teardown_over().await; } } @@ -1214,7 +1210,9 @@ enum JobDone { /// The root it probed: a result for a root no longer in flight (its /// probe was stopped, but had already finished) is not this one's. root: Txid, - outcome: Result, + /// `None`: nothing was sent — the registration the pass read is no + /// longer the wallet's (see [`gated_probe`]). + outcome: Option, }, /// [`NO_ANSWER_RETRY`] passed since a probe at `height` that no node /// answered. @@ -1278,9 +1276,6 @@ struct Probing { due: VecDeque, /// The root whose probe is out now. in_flight: Option, - /// That root's schedule before its probe was marked sent: put back if - /// the probe is stopped before anything goes out. - before: Option, } impl Probing { @@ -1766,18 +1761,14 @@ impl Actor { /// The registration a pass read, or was to read, is not in the wallet /// manager. This manager's own removals queue `WalletRemoved` under the - /// same guard, and commands are handled before job results, so only a - /// removal or replacement through `wallet_manager_arc` gets here. Fail - /// safe: forget its state, the host told to clear what it showed, and + /// same guard, and commands are handled before job results, so in + /// practice only a removal or replacement through `wallet_manager_arc` + /// gets here. Fail safe: forget its state, the host told to clear what it showed, and /// end the run. fn registration_gone(&mut self, wallet_id: WalletId) { let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; - tracing::error!( - wallet_id = %hex::encode(wallet_id), - "broadcast probe: a registered wallet read as gone" - ); // Idle, not re-read every block: a same-id wallet's events or an // Uncertain wake it. entry.wait_until = Some(u32::MAX); @@ -1968,7 +1959,13 @@ impl Actor { return; } match read { - None => self.registration_gone(wallet_id), + None => { + tracing::error!( + wallet_id = %hex::encode(wallet_id), + "broadcast probe: a registered wallet read as gone" + ); + self.registration_gone(wallet_id); + } Some(None) => self.end_run(wallet_id), Some(Some(read)) => { // A request queued before any height was reported @@ -2051,7 +2048,6 @@ impl Actor { height, due: start.due, in_flight: None, - before: None, }); // A forced request that came while this pass read. yield_to_forced(entry); @@ -2143,7 +2139,6 @@ impl Actor { // could be stopped: not the root in flight now. return; } - let height_of_pass = probing.height; let Some(root) = probing.in_flight.take() else { // Cannot happen: a settle that clears the root in flight // re-arms the run or ends it at once. Log it and carry on @@ -2155,44 +2150,18 @@ impl Actor { self.probe_next(wallet_id); return; }; - let ProbeOutcome { verdict, answered } = match outcome { - Ok(outcome) => outcome, - Err(NotProbed::Leaving) => { - // The wallet is being removed: `WalletRemoved` - // follows and drops what is kept for it. Should the - // removal be abandoned instead, nothing is lost: the - // root's schedule is as before this probe, and what - // the pass was to probe waits for the next height, - // sync completion or report — not started now, when - // it would only be stopped again. - if let (Some(before), Some(schedule)) = ( - probing.before, - self.state.schedules.get_mut(&(wallet_id, root.txid)), - ) { - *schedule = before; - } - let Some(entry) = self.wallets.get_mut(&wallet_id) else { - return; - }; - entry.wait_until = None; - // This height's pass sent nothing for the rest of its - // roots: a sync completion at it may run another. - entry.followed_pass_at = None; - if let Some(run) = entry.run.take() { - if !run.forced.is_empty() || !run.retried.is_empty() { - let pending = - entry.pending.get_or_insert_with(PendingPass::default); - pending.height = pending.height.max(height_of_pass); - pending.forced.extend(run.forced); - pending.retried.extend(run.retried); - } - } - return; - } - Err(NotProbed::Unregistered) => { - self.registration_gone(wallet_id); - return; - } + let Some(ProbeOutcome { verdict, answered }) = outcome else { + // A removal through this manager queues `WalletRemoved` + // before it lets the probe see this, and that drops the + // run: all but a registration removed or replaced behind + // the manager (`wallet_manager_arc`) end there. + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + txid = %root.txid, + "broadcast probe: the wallet's registration changed; nothing sent" + ); + self.registration_gone(wallet_id); + return; }; if !probing.views.iter().any(|view| view.txid == root.txid) { // Settled or gone while its probe was out: moot. @@ -2340,7 +2309,6 @@ impl Actor { }); self.job_runs .insert(run.abort.id(), (wallet_id, id, Some(root_txid))); - probing.before = self.state.schedules.get(&(wallet_id, root.txid)).copied(); mark_sent( &mut self.state, wallet_id, @@ -5195,7 +5163,6 @@ mod tests { height: 101, due: start.due, in_flight: None, - before: None, }); yield_to_forced(&mut entry); @@ -5488,6 +5455,9 @@ mod tests { ); } + /// How long a test waits to see that a job stays blocked. + const STAYS_BLOCKED: Duration = Duration::from_millis(50); + /// Start a followed pass and hand its read to the actor: the probe of /// its first root is spawned, not yet run. async fn spawn_first_probe(rig: &mut Rig, wallet_id: WalletId, from: u32) { @@ -5500,22 +5470,29 @@ mod tests { /// The race a removal must win: the probe job exists when the wallet's /// removal takes the gate, and the actor has not handled `WalletRemoved` - /// yet. The probe sends nothing — not while the removal runs, not after - /// it returns. - #[tokio::test] + /// yet. The probe waits the removal out and sends nothing — not while + /// it runs, not after it returns. + #[tokio::test(start_paused = true)] async fn should_not_send_a_probe_for_a_wallet_being_removed() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); let mut rig = enabled(probe.clone()).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); let teardown = rig.source.generation(wallet()).teardown_guard().await; spawn_first_probe(&mut rig, wallet(), 100).await; + assert!( + timeout(STAYS_BLOCKED, rig.actor.jobs.join_next_with_id()) + .await + .is_err(), + "the probe waits for the removal" + ); + assert!(probe.probed().is_empty(), "nothing sent meanwhile"); + rig.source.views.lock().expect("views").remove(&wallet()); + drop(teardown); rig.settle().await; + assert!(probe.probed().is_empty(), "nothing sent"); assert!(rig.sent().is_empty(), "and no verdict"); - - rig.source.views.lock().expect("views").remove(&wallet()); - drop(teardown); rig.send(Command::WalletRemoved(wallet())).await; assert!(probe.probed().is_empty()); assert!(!rig.actor.wallets.contains_key(&wallet())); @@ -5591,75 +5568,33 @@ mod tests { } /// A removal that was asked for and then abandoned leaves the wallet - /// registered: the pass it stopped sends nothing and keeps what the - /// host was shown, and the next block probes the root again. - #[tokio::test] - async fn should_probe_again_after_an_abandoned_removal() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.follow(wallet(), 100).await; - rig.sent(); - let teardown = rig.source.generation(wallet()).teardown_guard().await; - - rig.height(wallet(), 102).await; - assert_eq!(probe.probed().len(), 1, "stopped for the removal"); - assert!(rig.sent().is_empty(), "the verdict shown stands"); - - drop(teardown); - rig.height(wallet(), 103).await; - assert_eq!(probe.probed().len(), 2); - } - - /// A report's forced probe stopped by a removal that is then abandoned - /// is not lost: it goes out with the wallet's next pass, though the - /// wallet is catching up and probes nothing by schedule. And a root's - /// every-block window does not start at a probe that never went out. - #[tokio::test] - async fn should_keep_a_forced_probe_stopped_by_an_abandoned_removal() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - let teardown = rig.source.generation(wallet()).teardown_guard().await; - - rig.send(Command::Uncertain(txid(1))).await; - assert!(probe.probed().is_empty(), "stopped for the removal"); - let entry = &rig.actor.wallets[&wallet()]; - assert!(entry.run.is_none(), "and not started over while it lasts"); - assert!(entry - .pending - .as_ref() - .is_some_and(|pending| pending.forced.contains(&txid(1)))); - - drop(teardown); - // The first step after launch: catch-up, forced roots only. - rig.height(wallet(), 100).await; - assert_eq!(probe.probed(), vec![txid(1)]); - } - - /// A followed probe stopped by a removal does not start its root's - /// every-block window: nothing went out. - #[tokio::test] - async fn should_not_start_a_window_at_a_probe_stopped_by_a_removal() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + /// registered: the probe it held back goes out after all, and the pass + /// carries on as if nothing had happened. + #[tokio::test(start_paused = true)] + async fn should_probe_after_an_abandoned_removal() { + let probe = Arc::new(ScriptedProbe::new(&[ + (txid(1), ProbeVerdict::Accepted), + (txid(2), ProbeVerdict::Accepted), + ])); let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); let teardown = rig.source.generation(wallet()).teardown_guard().await; - - rig.follow(wallet(), 100).await; - assert!(probe.probed().is_empty()); - assert_eq!( - rig.actor.state.schedules[&(wallet(), txid(1))].window_start, - None + spawn_first_probe(&mut rig, wallet(), 100).await; + assert!( + timeout(STAYS_BLOCKED, rig.actor.jobs.join_next_with_id()) + .await + .is_err(), + "held back while the removal is asked for" ); drop(teardown); - rig.height(wallet(), 102).await; - assert_eq!(probe.probed(), vec![txid(1)]); - assert_eq!( - rig.actor.state.schedules[&(wallet(), txid(1))].window_start, - Some(102) - ); + rig.settle().await; + + assert_eq!(probe.probed().len(), 2, "both roots of the pass"); + assert_eq!(rig.sent().len(), 2); } /// A probe that never ends once started, and says when it was dropped. @@ -5710,15 +5645,11 @@ mod tests { .expect("remove"); assert!(probe.dropped.load(Ordering::SeqCst), "the probe stopped"); - assert_eq!(job.await.expect("job"), Err(NotProbed::Leaving)); - // The request is over with the removal: only the registration - // check answers now. - let transaction = tx(1, &[outpoint(90, 0)]); - let source = ManagerSource(Arc::clone(platform_wallet.wallet_manager())); - assert_eq!( - gated_probe(&source, &*probe, wallet_id, &generation, &transaction).await, - Err(NotProbed::Unregistered) - ); + let outcome = timeout(Duration::from_secs(5), job) + .await + .expect("the job ends once the removal is over") + .expect("job"); + assert!(outcome.is_none(), "with no verdict, and not started over"); } /// Against the real manager: the registration a read names is the @@ -5742,11 +5673,11 @@ mod tests { let transaction = tx(1, &[outpoint(90, 0)]); let sent = gated_probe(&source, &probe, wallet_id, &read.generation, &transaction).await; - assert!(sent.is_ok(), "probed while registered"); + assert!(sent.is_some(), "probed while registered"); manager.remove_wallet(&wallet_id).await.expect("remove"); let refused = gated_probe(&source, &probe, wallet_id, &read.generation, &transaction).await; - assert_eq!(refused, Err(NotProbed::Unregistered)); + assert!(refused.is_none()); assert_eq!(probe.probed().len(), 1, "nothing sent after the removal"); } @@ -6689,7 +6620,7 @@ mod tests { Ok((_, JobDone::Probed { root, outcome, .. })) if *root == txid(1) && outcome.as_ref().map(|outcome| &outcome.verdict) - == Ok(&ProbeVerdict::Mined) + == Some(&ProbeVerdict::Mined) )); rig.handle(Command::Settled { diff --git a/packages/rs-platform-wallet/src/wallet/core/generation.rs b/packages/rs-platform-wallet/src/wallet/core/generation.rs index 30d7821e936..ef685c9521b 100644 --- a/packages/rs-platform-wallet/src/wallet/core/generation.rs +++ b/packages/rs-platform-wallet/src/wallet/core/generation.rs @@ -38,6 +38,7 @@ impl Drop for TeardownRequest { /// [`WalletGeneration::teardown_guard`]). Owned: it may outlive the /// `Arc` binding it was taken from. #[derive(Debug)] +#[must_use = "the gate is released as soon as the guard is dropped"] pub struct TeardownGuard { _gate: OwnedRwLockWriteGuard<()>, _request: TeardownRequest, @@ -507,6 +508,14 @@ impl WalletGeneration { let _ = leaving.wait_for(|asking| *asking > 0).await; } + /// Resolves once no teardown is asking for this generation's gate or + /// holding it: the removal is done — the generation is then no longer + /// registered — or was given up. + pub(crate) async fn teardown_over(&self) { + let mut leaving = self.leaving.subscribe(); + let _ = leaving.wait_for(|asking| *asking == 0).await; + } + /// Whether some operation is inside the gate right now. #[cfg(test)] pub(crate) fn lifecycle_is_held(&self) -> bool { @@ -1614,8 +1623,11 @@ mod teardown_request_tests { drop(payment); let guard = teardown.await; assert!(is_requested(&generation).await, "inside"); + let mut over = Box::pin(generation.teardown_over()); + assert!(timeout(SETTLED, &mut over).await.is_err(), "not over yet"); drop(guard); assert!(!is_requested(&generation).await); + over.await; } /// A teardown given up while it waits for the gate leaves no trace: the From a48cb85bc6d3d6598375a08d838f4ec708bacc7a Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Mon, 12 Oct 2026 15:33:53 +0300 Subject: [PATCH 148/169] fix(platform-wallet): keep a finished probe's verdict when a removal asks at the same moment Review of the previous commits: a probe that has completed returns its verdict even if a teardown request arrived in the same poll; the doc says what stopping a probe does and does not recall; the test reuses the existing hanging probe; the teardown count needs no `Arc` of its own. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/core/broadcast_resolver.rs | 29 +++++-------------- .../src/wallet/core/generation.rs | 10 +++---- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 473a7420d80..2b13e0d8eb7 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -9,7 +9,7 @@ //! the verdict; it **changes nothing** in the wallet. A probe resubmits the //! signed transaction, so it goes out only under the lifecycle gate of the //! registration it was read from: a wallet's removal stops the one in -//! flight and none goes out after it (see [`gated_probe`]). Verdicts are positive +//! flight and none is started after it (see [`gated_probe`]). Verdicts are positive //! only — accepted or mined — or unresolved: no answer the probe gets proves //! that a transaction can never land (see //! [`broadcast_probe`](crate::broadcast_probe)). @@ -973,8 +973,10 @@ pub(crate) trait WalletSource: Send + Sync { /// finds the probe inside — which then stops where it is and lets go of the /// gate, so the removal waits neither for the wallet-manager lock nor for /// any node's answer (a probe is up to eight requests, each with its own -/// timeout). Stopping the job from the actor is not that barrier: the actor -/// learns of a removal only after the manager has let the wallet go. +/// timeout). A request already handed to the transport is not recalled, but +/// no further one is made. Stopping the job from the actor is not that +/// barrier: the actor learns of a removal only after the manager has let the +/// wallet go. /// /// A removal that is given up leaves the wallet registered: the probe then /// starts over — the same signed bytes again. @@ -996,7 +998,7 @@ async fn gated_probe( loop { tokio::select! { biased; - _ = generation.teardown_requested() => {} + // First: a probe that has finished keeps its verdict. probed = async { let _payment = generation.payment_guard().await; if !source.is_registered(wallet_id, generation).await { @@ -1004,6 +1006,7 @@ async fn gated_probe( } Some(probe.probe(transaction).await) } => return probed, + _ = generation.teardown_requested() => {} } generation.teardown_over().await; } @@ -5597,22 +5600,6 @@ mod tests { assert_eq!(rig.sent().len(), 2); } - /// A probe that never ends once started, and says when it was dropped. - #[derive(Default)] - struct HeldProbe { - started: Notify, - dropped: Arc, - } - - #[async_trait] - impl AcceptanceProbe for HeldProbe { - async fn probe(&self, _transaction: &Transaction) -> ProbeOutcome { - let _torn_down = DropFlag(Arc::clone(&self.dropped)); - self.started.notify_one(); - std::future::pending().await - } - } - /// The other order, against the real manager: a probe is out — no node /// answering — when its wallet is removed. The removal does not wait for /// the nodes: the probe stops there, and only then does the wallet go. @@ -5622,7 +5609,7 @@ mod tests { let platform_wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); let source = ManagerSource(Arc::clone(platform_wallet.wallet_manager())); let generation = Arc::clone(platform_wallet.generation()); - let probe = Arc::new(HeldProbe::default()); + let probe = Arc::new(HangingProbe::default()); let job = { let probe = Arc::clone(&probe); let generation = Arc::clone(&generation); diff --git a/packages/rs-platform-wallet/src/wallet/core/generation.rs b/packages/rs-platform-wallet/src/wallet/core/generation.rs index ef685c9521b..c4114eca17d 100644 --- a/packages/rs-platform-wallet/src/wallet/core/generation.rs +++ b/packages/rs-platform-wallet/src/wallet/core/generation.rs @@ -19,12 +19,12 @@ use crate::wallet::platform_wallet::PlatformWalletInfo; /// [`teardown_guard`](WalletGeneration::teardown_guard) call abandoned while /// it waits. #[derive(Debug)] -struct TeardownRequest(Arc>); +struct TeardownRequest(watch::Sender); impl TeardownRequest { - fn new(leaving: &Arc>) -> Self { + fn new(leaving: &watch::Sender) -> Self { leaving.send_modify(|asking| *asking += 1); - Self(Arc::clone(leaving)) + Self(leaving.clone()) } } @@ -98,7 +98,7 @@ pub struct WalletGeneration { /// How many teardowns are asking for the gate or hold it (see /// [`teardown_requested`](Self::teardown_requested)). Shared with their /// guards, which take themselves off it when dropped. - leaving: Arc>, + leaving: watch::Sender, /// Outpoints currently fenced against re-selection because a broadcast /// dispatch owns them ([`pin_in_broadcast`](Self::pin_in_broadcast)). /// @@ -427,7 +427,7 @@ impl WalletGeneration { Self { balance: WalletBalance::new(), lifecycle: Arc::new(RwLock::new(())), - leaving: Arc::new(watch::Sender::new(0)), + leaving: watch::Sender::new(0), in_broadcast: fences, #[cfg(test)] settle_boundary_hook: SettleBoundaryHook::default(), From a9babfbc7862a9cccc4fbc462a0ac2e621215380 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Mon, 12 Oct 2026 20:24:11 +0300 Subject: [PATCH 149/169] fix(swift-sdk): drop probe verdicts queued for a wallet deleted meanwhile A broadcast-probe callback hops to the main queue. One that was queued while `deleteWallet` ran on the main actor was delivered after the deletion had cleared the wallet's verdicts, and published the deleted wallet's verdict again. The trampoline now reads a per-wallet generation when the callback arrives, `deleteWallet` bumps it once the native wallet is gone, and a queued verdict or clear whose generation no longer matches is dropped. A wallet restored under the same id keeps the bumped generation. Co-Authored-By: Claude Opus 5.5 --- .../PlatformWalletManager.swift | 14 +- .../PlatformWalletManagerBroadcastProbe.swift | 52 ++++++- .../BroadcastProbeWalletGenerationTests.swift | 138 ++++++++++++++++++ 3 files changed, 191 insertions(+), 13 deletions(-) create mode 100644 packages/swift-sdk/SwiftTests/SwiftDashSDKTests/BroadcastProbeWalletGenerationTests.swift diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift index c656f2213e7..32cb4006fb3 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift @@ -467,6 +467,11 @@ public class PlatformWalletManager: ObservableObject { /// each one as it arrives. @Published public internal(set) var lastOutgoingTransactionProbe: OutgoingTransactionProbeEvent? + /// Per-wallet generation that fences broadcast-probe callbacks queued for + /// the main actor against `deleteWallet`. `nonisolated` so the C + /// trampoline can read it on the Rust thread. + nonisolated let outgoingProbeWalletGeneration = WalletGenerationCounter() + /// Cumulative number of encrypted notes scanned in the **current** /// in-flight shielded sync pass, published once per chunk (~every /// 2048 notes) via the Rust-side progress callback. Nil between @@ -2663,12 +2668,9 @@ public class PlatformWalletManager: ObservableObject { // with the same deterministic id doesn't inherit a stale banner (the // poller would also prune it, but not until the next tick). dashPayUnlockStatus.removeValue(forKey: walletId) - // Same for broadcast-probe verdicts: Rust forgets the wallet's sends - // on removal, and a restored wallet (same id) must start clean. - outgoingTransactionVerdicts = outgoingTransactionVerdicts.filter { $0.key.walletId != walletId } - if lastOutgoingTransactionProbe?.walletId == walletId { - lastOutgoingTransactionProbe = nil - } + // Same for broadcast-probe verdicts, including any still queued for + // the main actor. + forgetOutgoingTransactionProbes(walletId: walletId) // A store reconcile in flight for any wallet stops between pages: // its next step would read rows `deleteWalletData` is about to // remove. Coarse on purpose — the cadence re-runs the others. diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index ec0a8c237cd..8ff97769991 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -47,12 +47,30 @@ public struct OutgoingTransactionProbeEvent: Sendable, Equatable { } } +/// Lock-guarded generation counter per wallet id, safe to read and bump from +/// any thread: `SyncGenerationCounter` for state that is one wallet's. A +/// broadcast-probe callback reads its wallet's generation on the Rust thread +/// that delivers it; `deleteWallet` bumps it once the native wallet is gone, +/// so a verdict or clear still queued for the main actor is dropped there. +/// A wallet restored under the same deterministic id keeps the bumped +/// generation: the deleted wallet's queued callbacks stay stale, its own +/// are current. For that reason an entry is never removed: one counter stays +/// per wallet id ever deleted. +final class WalletGenerationCounter: @unchecked Sendable { + private let lock = NSLock() + private var values: [Data: UInt64] = [:] + func current(_ walletId: Data) -> UInt64 { lock.withLock { values[walletId] ?? 0 } } + func bump(_ walletId: Data) { lock.withLock { values[walletId, default: 0] &+= 1 } } +} + /// C trampoline for /// `EventHandlerCallbacksExtension.on_outgoing_transaction_probed_fn`. Rust /// owns every pointer only for this call, so everything is copied before the /// hop to the main queue. `DispatchQueue.main` is FIFO, so a verdict and a /// later clear for the same send are applied in the order Rust sent them — -/// separate `Task`s give no such guarantee. +/// separate `Task`s give no such guarantee. The FIFO does not order them +/// against a synchronous `deleteWallet` on the main actor, so the wallet's +/// generation is read here, before the hop, and checked on delivery. func outgoingTransactionProbedCallback( context: UnsafeMutableRawPointer?, walletIdPtr: UnsafePointer?, @@ -67,11 +85,12 @@ func outgoingTransactionProbedCallback( let walletId = Data(bytes: walletIdPtr, count: 32) let txidWire = Data(bytes: txidPtr, count: 32) + let generation = handler.manager?.outgoingProbeWalletGeneration.current(walletId) ?? 0 if verdictCode == UInt8(OUTGOING_PROBE_VERDICT_CLEARED) { let key = OutgoingTransactionKey(walletId: walletId, txidWire: txidWire) DispatchQueue.main.async { [weak manager = handler.manager] in MainActor.assumeIsolated { - manager?.handleOutgoingTransactionCleared(key) + manager?.handleOutgoingTransactionCleared(key, generation: generation) } } return @@ -94,7 +113,7 @@ func outgoingTransactionProbedCallback( DispatchQueue.main.async { [weak manager = handler.manager] in MainActor.assumeIsolated { - manager?.handleOutgoingTransactionProbed(event) + manager?.handleOutgoingTransactionProbed(event, generation: generation) } } } @@ -136,18 +155,37 @@ extension PlatformWalletManager { try platform_wallet_manager_set_broadcast_probe_enabled(handle, enabled).check() } + /// The native wallet is gone: drop its verdicts and make every callback + /// still queued for it stale. Rust forgets the wallet's sends on removal, + /// and a restored wallet (same id) must start clean. + @MainActor + func forgetOutgoingTransactionProbes(walletId: Data) { + outgoingProbeWalletGeneration.bump(walletId) + outgoingTransactionVerdicts = outgoingTransactionVerdicts.filter { $0.key.walletId != walletId } + if lastOutgoingTransactionProbe?.walletId == walletId { + lastOutgoingTransactionProbe = nil + } + } + + /// `generation` is the wallet's `outgoingProbeWalletGeneration` as read + /// when the callback arrived; a verdict queued before its wallet was + /// deleted no longer matches and is dropped. @MainActor - func handleOutgoingTransactionProbed(_ event: OutgoingTransactionProbeEvent) { - guard !shutdownRequested, isConfigured else { return } + func handleOutgoingTransactionProbed(_ event: OutgoingTransactionProbeEvent, generation: UInt64) { + guard !shutdownRequested, isConfigured, + generation == outgoingProbeWalletGeneration.current(event.walletId) else { return } outgoingTransactionVerdicts[event.key] = event lastOutgoingTransactionProbe = event } /// Drop the send's verdict: the send settled or left the wallet, its wallet /// was removed, or probing was turned off. Not a statement that it settled. + /// A clear queued before its wallet was deleted is dropped like a verdict; + /// the deletion already removed the entry. @MainActor - func handleOutgoingTransactionCleared(_ key: OutgoingTransactionKey) { - guard !shutdownRequested, isConfigured else { return } + func handleOutgoingTransactionCleared(_ key: OutgoingTransactionKey, generation: UInt64) { + guard !shutdownRequested, isConfigured, + generation == outgoingProbeWalletGeneration.current(key.walletId) else { return } outgoingTransactionVerdicts.removeValue(forKey: key) if lastOutgoingTransactionProbe?.key == key { lastOutgoingTransactionProbe = nil diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/BroadcastProbeWalletGenerationTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/BroadcastProbeWalletGenerationTests.swift new file mode 100644 index 00000000000..b31f07e804c --- /dev/null +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/BroadcastProbeWalletGenerationTests.swift @@ -0,0 +1,138 @@ +import XCTest +import DashSDKFFI +@testable import SwiftDashSDK + +/// Regression coverage for broadcast-probe callbacks that are queued for the +/// main actor while `deleteWallet` runs on it. +@MainActor +final class BroadcastProbeWalletGenerationTests: XCTestCase { + + private nonisolated static func noOpCalls() -> PlatformWalletNativeTeardownCalls { + let success: PlatformWalletNativeTeardownCalls.Call = { _ in + PlatformWalletFFIResult( + code: PLATFORM_WALLET_FFI_RESULT_CODE_SUCCESS, + message: nil + ) + } + return PlatformWalletNativeTeardownCalls( + spvStop: success, + platformAddressSyncStop: success, + shieldedSyncStop: success, + dashPaySyncStop: success, + dpnsSyncStop: success, + destroy: success + ) + } + + private func makeManager() -> PlatformWalletManager { + PlatformWalletManager.makeForTesting(handle: 72, calls: Self.noOpCalls()) + } + + private func id(_ byte: UInt8) -> Data { Data(repeating: byte, count: 32) } + + /// Deliver a callback the way Rust does, through the C trampoline. It + /// reads the wallet's generation now and queues the rest for the main + /// queue, which cannot run before this actor turn yields. + private func deliver( + _ handler: PlatformWalletEventHandler, + walletId: Data, + txid: Data, + verdictCode: Int + ) { + walletId.withUnsafeBytes { wallet in + txid.withUnsafeBytes { tx in + outgoingTransactionProbedCallback( + context: Unmanaged.passUnretained(handler).toOpaque(), + walletIdPtr: wallet.bindMemory(to: UInt8.self).baseAddress, + txidPtr: tx.bindMemory(to: UInt8.self).baseAddress, + verdictCode: UInt8(verdictCode), + reasonPtr: nil + ) + } + } + } + + /// Returns once everything queued on the main queue so far has run. + private func drainMainQueue() async { + await withCheckedContinuation { continuation in + DispatchQueue.main.async { continuation.resume() } + } + } + + func testShouldPublishAQueuedVerdictAndApplyItsLaterClear() async { + let manager = makeManager() + let handler = PlatformWalletEventHandler(manager: manager) + let key = OutgoingTransactionKey(walletId: id(1), txidWire: id(9)) + + deliver(handler, walletId: id(1), txid: id(9), verdictCode: Int(OUTGOING_PROBE_VERDICT_ACCEPTED)) + await drainMainQueue() + XCTAssertEqual(manager.outgoingTransactionVerdicts[key]?.verdict, .accepted) + XCTAssertEqual(manager.lastOutgoingTransactionProbe?.key, key) + + deliver(handler, walletId: id(1), txid: id(9), verdictCode: Int(OUTGOING_PROBE_VERDICT_CLEARED)) + await drainMainQueue() + XCTAssertNil(manager.outgoingTransactionVerdicts[key]) + XCTAssertNil(manager.lastOutgoingTransactionProbe) + await manager.shutdown() + } + + func testShouldDropAQueuedVerdictWhenItsWalletIsDeletedBeforeDelivery() async { + let manager = makeManager() + let handler = PlatformWalletEventHandler(manager: manager) + + deliver(handler, walletId: id(1), txid: id(9), verdictCode: Int(OUTGOING_PROBE_VERDICT_ACCEPTED)) + manager.forgetOutgoingTransactionProbes(walletId: id(1)) + await drainMainQueue() + + XCTAssertTrue(manager.outgoingTransactionVerdicts.isEmpty) + XCTAssertNil(manager.lastOutgoingTransactionProbe) + await manager.shutdown() + } + + /// A wallet restored from the same mnemonic has the same id, so wallet + /// membership cannot tell its verdicts from the deleted wallet's. + func testShouldNotPublishADeletedWalletsQueuedVerdictForItsRestoredSuccessor() async { + let manager = makeManager() + let handler = PlatformWalletEventHandler(manager: manager) + + deliver(handler, walletId: id(1), txid: id(9), verdictCode: Int(OUTGOING_PROBE_VERDICT_ACCEPTED)) + manager.forgetOutgoingTransactionProbes(walletId: id(1)) + // The successor's own verdict, for another send. + deliver(handler, walletId: id(1), txid: id(8), verdictCode: Int(OUTGOING_PROBE_VERDICT_UNRESOLVED)) + await drainMainQueue() + + let successorKey = OutgoingTransactionKey(walletId: id(1), txidWire: id(8)) + XCTAssertEqual(Array(manager.outgoingTransactionVerdicts.keys), [successorKey]) + XCTAssertEqual(manager.lastOutgoingTransactionProbe?.key, successorKey) + await manager.shutdown() + } + + func testShouldNotLetADeletedWalletsClearRemoveItsSuccessorsVerdict() async { + let manager = makeManager() + let key = OutgoingTransactionKey(walletId: id(1), txidWire: id(9)) + let deletedWalletsGeneration = manager.outgoingProbeWalletGeneration.current(id(1)) + manager.forgetOutgoingTransactionProbes(walletId: id(1)) + let event = OutgoingTransactionProbeEvent(walletId: id(1), txidWire: id(9), verdict: .accepted) + manager.handleOutgoingTransactionProbed( + event, generation: manager.outgoingProbeWalletGeneration.current(id(1))) + + manager.handleOutgoingTransactionCleared(key, generation: deletedWalletsGeneration) + + XCTAssertEqual(manager.outgoingTransactionVerdicts[key], event) + XCTAssertEqual(manager.lastOutgoingTransactionProbe, event) + await manager.shutdown() + } + + func testShouldKeepAnotherWalletsQueuedVerdictWhenAWalletIsDeleted() async { + let manager = makeManager() + let handler = PlatformWalletEventHandler(manager: manager) + let otherKey = OutgoingTransactionKey(walletId: id(2), txidWire: id(9)) + + deliver(handler, walletId: id(2), txid: id(9), verdictCode: Int(OUTGOING_PROBE_VERDICT_ACCEPTED)) + manager.forgetOutgoingTransactionProbes(walletId: id(1)) + await drainMainQueue() + + XCTAssertEqual(manager.outgoingTransactionVerdicts[otherKey]?.verdict, .accepted) + await manager.shutdown() + } +} From 44d5d3918ea16b2b1475777a19b2478c5205871f Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Mon, 12 Oct 2026 23:10:21 +0300 Subject: [PATCH 150/169] refactor(platform-wallet): narrow this branch to the broadcast probe and resolver The pull request is split in three at review. Two parts move out: - the InstantSend-lock record re-emission, now #5393 - spending only final coins and FFI code 59, now #5395 What stays is the probe of sends with an unknown broadcast outcome: the probe, the resolver, the DapiBroadcaster and rs-sdk changes it needs, the FFI callback and the Swift surface. This commit restores the files of the two other parts to the base branch and keeps the resolver's hunks of the files they shared. Co-Authored-By: Claude Opus 5.5 --- .../dashsdk/errors/DashSdkError.kt | 10 - .../dashsdk/ffi/WalletManagerNative.kt | 5 +- .../dashsdk/wallet/CoreTransactionBuilder.kt | 5 +- .../dashsdk/errors/DashSdkErrorTest.kt | 4 - .../ERROR_CODE_REGISTRY.md | 6 +- .../src/core_wallet/transaction_builder.rs | 1180 +-------- .../src/core_wallet_types.rs | 4 +- packages/rs-platform-wallet-ffi/src/error.rs | 38 - packages/rs-platform-wallet/README.md | 61 +- .../src/changeset/core_bridge.rs | 133 - packages/rs-platform-wallet/src/error.rs | 91 - packages/rs-platform-wallet/src/lib.rs | 5 +- .../src/masternode/update_service.rs | 66 +- .../rs-platform-wallet/src/test_support.rs | 156 +- .../src/wallet/core/generation.rs | 35 +- .../rs-platform-wallet/src/wallet/core/mod.rs | 10 +- .../src/wallet/core/transaction.rs | 2195 +---------------- .../src/wallet/identity/network/payments.rs | 108 +- .../CoreWallet/CoreTransactionBuilder.swift | 17 +- .../PlatformWallet/PlatformWalletResult.swift | 11 - 20 files changed, 274 insertions(+), 3866 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt index da78414a634..420ad1a6bbb 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt @@ -117,15 +117,6 @@ sealed class DashSdkError( class CoreInsufficientFunds(message: String, cause: Throwable? = null) : PlatformWallet(message, cause) - /** - * `ErrorCoreFundsAwaitingNetwork` (native code 59). The payment is - * covered only by coins the network has not confirmed yet (neither - * InstantSend-locked nor mined); they become spendable once their - * transaction is. Not a shortfall — retry once they are confirmed. - */ - class CoreFundsAwaitingNetwork(message: String, cause: Throwable? = null) : - PlatformWallet(message, cause) - /** * `ErrorShieldedInsufficientBalance` (native code 41; historical FFI * spelling). A Platform Payment account's deterministic shield input @@ -742,7 +733,6 @@ sealed class DashSdkError( 20 -> PlatformWallet.TransactionBroadcastUnconfirmed(message, cause) // ErrorTransactionBroadcastUnconfirmed 42 -> PlatformWallet.MasternodeWithdrawalUnconfirmed(message, cause) // ErrorMasternodeWithdrawalUnconfirmed 22 -> PlatformWallet.CoreInsufficientFunds(message, cause) // ErrorCoreInsufficientFunds - 59 -> PlatformWallet.CoreFundsAwaitingNetwork(message, cause) // ErrorCoreFundsAwaitingNetwork 23 -> PlatformWallet.AssetLockNotTracked(message, cause) // ErrorAssetLockNotTracked 24 -> PlatformWallet.AssetLockAlreadyConsumed(message, cause) // ErrorAssetLockAlreadyConsumed 25 -> PlatformWallet.AssetLockFundingMismatch(message, cause) // ErrorAssetLockFundingMismatch diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/WalletManagerNative.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/WalletManagerNative.kt index 794625ee6f4..58351c59fbb 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/WalletManagerNative.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/WalletManagerNative.kt @@ -199,10 +199,7 @@ internal object WalletManagerNative { */ external fun coreTxBuilderChangeToFirstInput(builder: Long) - /** - * `core_wallet_tx_builder_set_fee_rate` — fee rate in duffs/kB (> 0: the JNI glue refuses 0); - * refused above about 42.9 DASH/kB (the fee arithmetic would overflow). - */ + /** `core_wallet_tx_builder_set_fee_rate` — fee rate in duffs/kB (> 0). */ external fun coreTxBuilderSetFeeRate(builder: Long, satPerKb: Long) /** diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/wallet/CoreTransactionBuilder.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/wallet/CoreTransactionBuilder.kt index f11fb413194..a740d779857 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/wallet/CoreTransactionBuilder.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/wallet/CoreTransactionBuilder.kt @@ -133,10 +133,7 @@ class CoreTransactionBuilder internal constructor(network: Network) : AutoClosea WalletManagerNative.coreTxBuilderChangeToFirstInput(handle) } - /** - * Set the fee rate in duffs/kB (> 0: the JNI glue refuses 0). Refused (invalid parameter) above - * about 42.9 DASH/kB, where the fee arithmetic would overflow. - */ + /** Set the fee rate in duffs/kB (> 0). */ internal fun setFeeRate(satPerKb: Long): CoreTransactionBuilder = apply { WalletManagerNative.coreTxBuilderSetFeeRate(handle, satPerKb) } diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt index 1e5c1700a0a..7129c55f633 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt @@ -129,10 +129,6 @@ class DashSdkErrorTest { DashSdkError.fromNative(DashSDKException(offset + 22, "inputs reserved")) assertTrue(coreInsufficientFunds is DashSdkError.PlatformWallet.CoreInsufficientFunds) - val awaitingNetwork = - DashSdkError.fromNative(DashSDKException(offset + 59, "funds awaiting network")) - assertTrue(awaitingNetwork is DashSdkError.PlatformWallet.CoreFundsAwaitingNetwork) - // The asset-lock coin-selection shortfall (29) must reach callers as its // own type rather than Generic, and must stay DISTINCT from the atomic // Core-send shortfall (22) — the two selectors report over different diff --git a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md index 84c2763fcb9..3399060f53b 100644 --- a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md +++ b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md @@ -114,14 +114,13 @@ These are shipped ABI. Do not renumber. | 98 | `NotFound` | Sentinel — `Option` returned as an error | | 99 | `ErrorUnknown` | Sentinel — unmapped/flattened errors | -**Next allocatable integer: 60** — 27–59 are all claimed (27, 29, 31, 34–42 +**Next allocatable integer: 59** — 27–58 are all claimed (27, 29, 31, 34–42 and 46 merged; 43–45 proposed by active #4313 at head `0302b188ab`; 47 and 48 proposed by active #4356 (47 renumbered from 42, 48 from 43 — see their rows below); 49–54 proposed by active #4586 (the persister operation × kind block); 55–57 proposed by #4715 for pending identity-funded shield debits and durable recovery errors; 58 proposed by #4799 for an unavailable -identity balance response; 59 proposed by #5258 for funds waiting for -network confirmation; 28, 30, +identity balance response; 28, 30, 32 and 33 reserved). **28, 30, 32 and 33 are RESERVED, not free**: 28 and 30 were vacated when the reservation trio moved to 34–36; 32 and 33 lapsed when their in-repo owners @@ -173,7 +172,6 @@ Fork-era numbers remain in the collision history, which is immutable record. | 56 | `ErrorShieldedRecoveryCorrupted` | #4715 | Proposed — durable shielded recovery data is malformed or invalid; preserved for diagnosis. Rust, Swift and Kotlin preserve this typed error | | 57 | `ErrorShieldedRecoveryKeysRequired` | #4715 | Proposed — recovery needs the account and compatible keys; ciphertext damage can produce the same symptom. Rust, Swift and Kotlin preserve this typed error | | 58 | `ErrorIdentityBalanceUnavailable` | #4799 | Proposed — Platform returned no balance for a managed identity. Retrying the read is safe; this is distinct from missing wallet ownership and must not trigger registration or funding | -| 59 | `ErrorCoreFundsAwaitingNetwork` | #5258 | Proposed — claimed from the frontier (58). A Core payment the balance covers only with coins the network has not confirmed yet (neither InstantSend-locked nor mined); builds spend only final coins. Not a shortfall: retry once those coins are confirmed. Rust value + pin test, Swift raw case + typed `coreFundsAwaitingNetwork`, Kotlin typed `PlatformWallet.CoreFundsAwaitingNetwork` + `fromPlatformWalletNative` arm + `DashSdkErrorTest` pin | **Code 31 left this table on 2026-08-04.** `ErrorSigningKeyUnavailable` sat here as #4183's proposal until #4183 merged (`189a3abb1c`); it is now in the merged diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet/transaction_builder.rs b/packages/rs-platform-wallet-ffi/src/core_wallet/transaction_builder.rs index c348bba4fde..bb3f8f3d6dc 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet/transaction_builder.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet/transaction_builder.rs @@ -17,23 +17,18 @@ use key_wallet::wallet::managed_wallet_info::transaction_builder::{ TransactionBuilder, MAX_STANDARD_OP_RETURN_BYTES, }; use key_wallet::wallet::managed_wallet_info::transaction_building::AccountTypePreference; -use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; -use platform_wallet::{ - check_chosen_inputs, check_fee_rate, in_broadcast_outpoints, FinalizeOptions, - PlatformWalletError, -}; use rs_sdk_ffi::{MnemonicResolverCoreSigner, MnemonicResolverHandle}; use std::ffi::CString; use std::os::raw::{c_char, c_void}; use std::str::FromStr; /// Opaque, C-compatible transaction builder. `inner` is a heap-boxed -/// [`BuilderState`] (the recorded configuration and the inputs chosen by -/// outpoint); `network` is the wallet network output and change addresses -/// are validated against. +/// key-wallet `TransactionBuilder`; `network` is the wallet network output +/// and change addresses are validated against. /// /// NOT thread-safe: a single builder must be used from one thread at a time. -/// The setters mutate `*inner` in place without synchronization. +/// The setters mutate `*inner` in place (`take_builder`/`store_builder`) +/// without synchronization. #[repr(C)] pub struct FFITransactionBuilder { inner: *mut c_void, @@ -44,101 +39,6 @@ pub struct FFITransactionBuilder { reservation_only: bool, } -/// What `FFITransactionBuilder::inner` points at — Rust-only, so the C -/// layout stays as it was. -#[derive(Default)] -struct BuilderState { - /// The setters' calls, in order. The finalizers make the key-wallet - /// builder from them, and make it again for the waiting-coins trial on a - /// shortfall (`CoreWallet::finalize_transaction_from`). - recipe: Vec, - /// Set by `core_wallet_tx_builder_add_inputs_from_outpoints`: the - /// finalizers seed the wallet's current copy of each, so a coin's finality - /// is judged when the build is finalized, not when it was chosen. - inputs: Vec, -} - -/// One validated setter call, replayed onto a fresh `TransactionBuilder`. -enum Step { - AddOutput(DashAddress, u64), - AddOpReturn(Vec), - SetChangeAddress(DashAddress), - PreserveOutputOrder, - ChangeToFirstInput, - SetFeeRate(FeeRate), - SetSelectionStrategy(SelectionStrategy), - SetSpecialPayload(TransactionPayload), -} - -impl BuilderState { - /// A fresh key-wallet builder with every recorded call applied. The - /// setters validated each call before recording it (an OP_RETURN within - /// the standard size, a fee rate that passes `check_fee_rate`), so the one - /// fallible step cannot fail here; it is still an error, not a panic. - fn make(&self) -> Result { - self.recipe - .iter() - .try_fold(TransactionBuilder::new(), |builder, step| { - Ok(match step { - Step::AddOutput(address, amount) => builder.add_output(address, *amount), - Step::AddOpReturn(data) => builder - .add_op_return(data) - .map_err(|e| PlatformWalletError::TransactionBuild(e.to_string()))?, - Step::SetChangeAddress(address) => builder.set_change_address(address.clone()), - Step::PreserveOutputOrder => builder.preserve_output_order(), - Step::ChangeToFirstInput => builder.change_to_first_input(), - Step::SetFeeRate(rate) => builder.set_fee_rate(*rate), - Step::SetSelectionStrategy(strategy) => { - builder.set_selection_strategy(*strategy) - } - Step::SetSpecialPayload(payload) => { - builder.set_special_payload(payload.clone()) - } - }) - }) - } -} - -impl FFITransactionBuilder { - /// Reclaim both heap boxes of a builder a finalizer consumes: the network - /// it was made for, its recorded configuration and inputs, and how to fund - /// it. - /// - /// # Safety - /// `builder` must be a live pointer from `core_wallet_tx_builder_new`; it - /// is consumed. - unsafe fn into_build(builder: *mut FFITransactionBuilder) -> (FFINetwork, BuildPlan) { - let ffi = Box::from_raw(builder); - let mut state = *Box::from_raw(ffi.inner as *mut BuilderState); - let options = FinalizeOptions::default() - .with_inputs(std::mem::take(&mut state.inputs)) - .with_reservation_only(ffi.reservation_only); - (ffi.network, BuildPlan { state, options }) - } - - /// The inner state. - /// - /// # Safety - /// `self.inner` must point at a live `BuilderState`. - unsafe fn state(&mut self) -> &mut BuilderState { - &mut *(self.inner as *mut BuilderState) - } - - /// Record one validated setter call. - /// - /// # Safety - /// `self.inner` must point at a live `BuilderState`. - unsafe fn record(&mut self, step: Step) { - self.state().recipe.push(step); - } -} - -/// A reclaimed builder, ready for `CoreWallet::finalize_transaction_from`. -struct BuildPlan { - state: BuilderState, - options: FinalizeOptions, -} - /// Owned signed-transaction bytes handed across the C ABI as the `out_tx` /// of `core_wallet_signed_payment_finalize`; release it with /// `core_wallet_transaction_free`. @@ -190,7 +90,7 @@ impl CoreAccountTypeFFI { } /// The funding sources this selector pools, in funding order — handed to - /// [`CoreWallet::finalize_transaction_from`]'s multi-source API, whose first + /// [`CoreWallet::finalize_transaction`]'s multi-source API, whose first /// source supplies the change address. A single-family selector yields a /// one-element list, which keeps that API's strict one-account semantics. pub(crate) fn funding_sources(self) -> &'static [AccountTypePreference] { @@ -234,10 +134,11 @@ pub unsafe extern "C" fn core_wallet_tx_builder_finalize( check_ptr!(out_transaction_handle); *out_transaction_handle = 0; - let (network, BuildPlan { state, options }) = FFITransactionBuilder::into_build(builder); + let ffi = Box::from_raw(builder); + let inner = *Box::from_raw(ffi.inner as *mut TransactionBuilder); let wallet = unwrap_option_or_return!(PLATFORM_WALLET_STORAGE.with_item(wallet, |w| w.clone())); - let builder_network: Network = network.into(); + let builder_network: Network = ffi.network.into(); if builder_network != wallet.network() { return PlatformWalletFFIResult::err( PlatformWalletFFIResultCode::ErrorInvalidParameter, @@ -247,18 +148,18 @@ pub unsafe extern "C" fn core_wallet_tx_builder_finalize( let signer = MnemonicResolverCoreSigner::new(core_signer_handle, wallet.wallet_id(), wallet.network()); - let make = move || state.make(); - let finalized = runtime().block_on(wallet.core().finalize_transaction_from( - make, - options, + let reservation_only = ffi.reservation_only; + let finalized = runtime().block_on(wallet.core().finalize_transaction_with_options( + inner, account_type.funding_sources(), account_index, &signer, + reservation_only, )); let finalized = unwrap_result_or_return!(finalized); // Publishing the finalized handle is gated exactly like the deferred-token sibling - // below (`core_wallet_signed_payment_finalize`). `finalize_transaction_from` drops + // below (`core_wallet_signed_payment_finalize`). `finalize_transaction` drops // the wallet-manager write lock before awaiting the (external, possibly slow) // signer, so the host can have removed this wallet while we were signing — // and that removal's finalized-handle sweep has then ALREADY run. Inserting now @@ -325,9 +226,7 @@ fn sole_deliverable_value(outputs: &[TxOut]) -> u64 { /// UTXO reservation — in one native operation. /// /// This is the deferred counterpart to `core_wallet_tx_builder_finalize`: it -/// runs the same atomic `finalize_transaction_from` (the recorded configuration -/// is replayed for the build, and again for the waiting-coins trial on a -/// shortfall), where selection and insertion +/// runs the same atomic `finalize_transaction`, where selection and insertion /// into the account `ReservationSet` commit as a single unit under the /// wallet-manager lock (signing happens after the lock is dropped). Routing the /// deferred build through it closes the double-selection window the former @@ -402,14 +301,14 @@ pub unsafe extern "C" fn core_wallet_signed_payment_finalize_with_deliverable( *out_bytes_len = 0; *out_deliverable_duffs = 0; - // The finalizer consumes the builder: reclaim both heap boxes up front so - // they are freed on every return path below. The recorded configuration - // is replayed by `finalize_transaction_from`, possibly twice. - let (network, BuildPlan { state, options }) = FFITransactionBuilder::into_build(builder); + // `finalize_transaction` consumes the builder: reclaim both heap boxes up + // front so they are freed on every return path below. + let ffi = Box::from_raw(builder); + let inner = *Box::from_raw(ffi.inner as *mut TransactionBuilder); let wallet = unwrap_option_or_return!(PLATFORM_WALLET_STORAGE.with_item(wallet, |w| w.clone())); - let builder_network: Network = network.into(); + let builder_network: Network = ffi.network.into(); if builder_network != wallet.network() { return PlatformWalletFFIResult::err( PlatformWalletFFIResultCode::ErrorInvalidParameter, @@ -421,25 +320,23 @@ pub unsafe extern "C" fn core_wallet_signed_payment_finalize_with_deliverable( MnemonicResolverCoreSigner::new(core_signer_handle, wallet.wallet_id(), wallet.network()); // Atomic select + reserve + sign in one wallet-manager critical section. - // `options` (the `reservation_only` flag, the inputs chosen by outpoint and - // the recorded configuration) is read off the reclaimed boxes by `into_build` - // (never through `builder`, whose provenance ends at `Box::from_raw`) and - // threaded through here for the same reason as in the immediate sibling: - // a host that called + // `reservation_only` is read off the reclaimed box (never through `builder`, + // whose provenance ends at `Box::from_raw`) and threaded through here for the + // same reason as in the immediate sibling: a host that called // `core_wallet_tx_builder_use_only_added_inputs` and then finalized a // DEFERRED payment would otherwise have the restriction silently discarded, // and the account's UTXOs would be offered to selection after all. - let make = move || state.make(); - let finalized = runtime().block_on(wallet.core().finalize_transaction_from( - make, - options, + let reservation_only = ffi.reservation_only; + let finalized = runtime().block_on(wallet.core().finalize_transaction_with_options( + inner, account_type.funding_sources(), account_index, &signer, + reservation_only, )); let finalized = unwrap_result_or_return!(finalized); - // `finalize_transaction_from` drops the wallet-manager write lock before awaiting + // `finalize_transaction` drops the wallet-manager write lock before awaiting // the (external, possibly slow) signer, so the host can have removed this // wallet while we were signing — and that removal's registry sweep has then // ALREADY run. Registering now would insert a live token for a removed @@ -640,6 +537,26 @@ fn managed_account( .and_then(|at| accounts.funds_account(&at)) } +impl FFITransactionBuilder { + /// The inner builder taken out by value, leaving an empty one in its + /// place. Pair with [`FFITransactionBuilder::store_builder`] to apply a + /// fluent (`self -> Self`) method. + /// + /// # Safety + /// `self.inner` must point at a live `TransactionBuilder` + unsafe fn take_builder(&self) -> TransactionBuilder { + std::mem::take(&mut *(self.inner as *mut TransactionBuilder)) + } + + /// Store `builder` back into the inner slot. + /// + /// # Safety + /// `self.inner` must point at a live `TransactionBuilder` + unsafe fn store_builder(&self, builder: TransactionBuilder) { + *(self.inner as *mut TransactionBuilder) = builder; + } +} + /// Create a new transaction builder for `network`. Free with /// `core_wallet_tx_builder_destroy` (or the consuming finalizers /// `core_wallet_tx_builder_finalize` / `core_wallet_signed_payment_finalize`). @@ -650,7 +567,7 @@ fn managed_account( pub unsafe extern "C" fn core_wallet_tx_builder_new( network: FFINetwork, ) -> *mut FFITransactionBuilder { - let inner = Box::into_raw(Box::::default()) as *mut c_void; + let inner = Box::into_raw(Box::new(TransactionBuilder::new())) as *mut c_void; Box::into_raw(Box::new(FFITransactionBuilder { inner, network, @@ -682,7 +599,9 @@ pub unsafe extern "C" fn core_wallet_tx_builder_add_output( } }; - (*builder).record(Step::AddOutput(address, amount)); + let b = (*builder).take_builder(); + let b = b.add_output(&address, amount); + (*builder).store_builder(b); PlatformWalletFFIResult::ok() } @@ -709,8 +628,11 @@ pub unsafe extern "C" fn core_wallet_tx_builder_add_op_return( std::slice::from_raw_parts(data, data_len) }; - // The one thing `add_op_return` refuses, by the same policy constant: - // checked here, so a recorded step always replays. + // `add_op_return` takes the builder by value, so a rejected payload drops it and leaves + // `take_builder`'s `mem::take` default behind — silently discarding outputs and options + // the caller already configured. Reject an over-long payload *before* taking the builder + // so the slot keeps its real state. `add_op_return` re-checks; this is the same policy + // constant, not a second opinion. if data_len > MAX_STANDARD_OP_RETURN_BYTES { return PlatformWalletFFIResult::err( PlatformWalletFFIResultCode::ErrorInvalidParameter, @@ -720,7 +642,17 @@ pub unsafe extern "C" fn core_wallet_tx_builder_add_op_return( ); } - (*builder).record(Step::AddOpReturn(bytes.to_vec())); + let b = (*builder).take_builder(); + let b = match b.add_op_return(bytes) { + Ok(b) => b, + Err(err) => { + return PlatformWalletFFIResult::err( + PlatformWalletFFIResultCode::ErrorWalletOperation, + err.to_string(), + ); + } + }; + (*builder).store_builder(b); PlatformWalletFFIResult::ok() } @@ -748,7 +680,9 @@ pub unsafe extern "C" fn core_wallet_tx_builder_set_change_address( } }; - (*builder).record(Step::SetChangeAddress(address)); + let b = (*builder).take_builder(); + let b = b.set_change_address(address); + (*builder).store_builder(b); PlatformWalletFFIResult::ok() } @@ -763,7 +697,9 @@ pub unsafe extern "C" fn core_wallet_tx_builder_preserve_output_order( ) -> PlatformWalletFFIResult { check_ptr!(builder); - (*builder).record(Step::PreserveOutputOrder); + let b = (*builder).take_builder(); + let b = b.preserve_output_order(); + (*builder).store_builder(b); PlatformWalletFFIResult::ok() } @@ -778,15 +714,13 @@ pub unsafe extern "C" fn core_wallet_tx_builder_change_to_first_input( ) -> PlatformWalletFFIResult { check_ptr!(builder); - (*builder).record(Step::ChangeToFirstInput); + let b = (*builder).take_builder(); + let b = b.change_to_first_input(); + (*builder).store_builder(b); PlatformWalletFFIResult::ok() } -/// Set the fee rate in duffs per kB. Refused with `ErrorInvalidParameter` when -/// the rate's fee arithmetic would overflow (above about 42.9 DASH per kB — -/// key-wallet multiplies rate by size unchecked); the builder is unchanged. -/// /// # Safety /// `builder` must be a valid, non-destroyed pointer. #[no_mangle] @@ -796,16 +730,9 @@ pub unsafe extern "C" fn core_wallet_tx_builder_set_fee_rate( ) -> PlatformWalletFFIResult { check_ptr!(builder); - // key-wallet prices with the rate unchecked: refuse one whose fee - // arithmetic overflows here, where the host set it. - let rate = FeeRate::new(sat_per_kb); - if let Err(error) = check_fee_rate(rate) { - return PlatformWalletFFIResult::err( - PlatformWalletFFIResultCode::ErrorInvalidParameter, - error.to_string(), - ); - } - (*builder).record(Step::SetFeeRate(rate)); + let b = (*builder).take_builder(); + let b = b.set_fee_rate(FeeRate::new(sat_per_kb)); + (*builder).store_builder(b); PlatformWalletFFIResult::ok() } @@ -918,7 +845,9 @@ pub unsafe extern "C" fn core_wallet_tx_builder_set_selection_strategy( ) -> PlatformWalletFFIResult { check_ptr!(builder); - (*builder).record(Step::SetSelectionStrategy(strategy.into())); + let b = (*builder).take_builder(); + let b = b.set_selection_strategy(strategy.into()); + (*builder).store_builder(b); PlatformWalletFFIResult::ok() } @@ -926,9 +855,10 @@ pub unsafe extern "C" fn core_wallet_tx_builder_set_selection_strategy( /// Set the block height coin selection treats as the chain tip (used for /// coinbase maturity and locktime). /// -/// Accepted and ignored: the finalizers (the only way this builder is -/// built) always build at the wallet's last processed height, so a height -/// set here never reached a build. +/// This value is advisory: the wallet-aware finalizers override it with the +/// wallet's last processed height when they run, so the wallet height always +/// wins for the funded/signed build. Use this only when building without a +/// wallet. /// /// # Safety /// `builder` must be a valid, non-destroyed pointer. @@ -938,7 +868,10 @@ pub unsafe extern "C" fn core_wallet_tx_builder_set_current_height( height: u32, ) -> PlatformWalletFFIResult { check_ptr!(builder); - let _ = height; + + let b = (*builder).take_builder(); + let b = b.set_current_height(height); + (*builder).store_builder(b); PlatformWalletFFIResult::ok() } @@ -983,7 +916,9 @@ pub unsafe extern "C" fn core_wallet_tx_builder_set_special_payload( } }; - (*builder).record(Step::SetSpecialPayload(payload)); + let b = (*builder).take_builder(); + let b = b.set_special_payload(payload); + (*builder).store_builder(b); PlatformWalletFFIResult::ok() } @@ -991,25 +926,7 @@ pub unsafe extern "C" fn core_wallet_tx_builder_set_special_payload( /// Add a caller-chosen subset of the account's UTXOs as inputs. `outpoints` /// are selected from the account's own UTXO set (the same ones /// `platform_wallet_account_utxos` returns). An outpoint not owned by the -/// account is an error (`ErrorWalletOperation`); one owned is refused by name, -/// in the finalizer's order: unspendable — a locked coin, or an immature -/// coinbase output (spendable once it matures, 100 blocks after its block; -/// neither is cured by the InstantSend lock or confirmation code 59 waits for) -/// — (`ErrorInvalidParameter`, "not spendable"), pinned by an in-flight -/// broadcast (input mid-broadcast; `ErrorUnknown` until it has a code of its -/// own, the outpoint in the message), then not final yet (code 59). Nothing is -/// recorded unless every outpoint passes. -/// -/// The finalizers look each recorded outpoint up again, in the accounts THEY -/// fund from (their own account type and index), and use the wallet's copy as -/// it is then. A chosen coin is never dropped silently: one those accounts -/// don't hold (chosen from another account, or spent since) fails the build -/// by name, with or without `core_wallet_tx_builder_use_only_added_inputs`; -/// one that lost its final status fails with code 59, one an in-flight -/// broadcast pins with an input-mid-broadcast error. The one exception: a coin -/// another in-flight build holds reserved (a pending deferred payment, say) -/// is left out by the reservation filter, as reservations are not readable -/// here. +/// account is an error /// /// # Safety /// `builder` must be a valid, non-destroyed pointer; `wallet` a valid platform-wallet handle; @@ -1063,53 +980,36 @@ pub unsafe extern "C" fn core_wallet_tx_builder_add_inputs_from_outpoints( let wm = wallet.wallet_manager().read().await; let info = wm .get_wallet_info(&wallet_id) - .ok_or_else(|| SeedError::Other("wallet not found".to_string()))?; + .ok_or_else(|| "wallet not found".to_string())?; let managed = managed_account(&info.core_wallet.accounts, source, account_index) - .ok_or_else(|| { - SeedError::Other(format!( - "managed account {source:?} #{account_index} not found" - )) - })?; - - let height = info.core_wallet.last_processed_height(); - let pinned = in_broadcast_outpoints(info); - let mut named = Vec::with_capacity(requested.len()); + .ok_or_else(|| format!("managed account {source:?} #{account_index} not found"))?; + + let mut selected = Vec::with_capacity(requested.len()); for op in &requested { - named.push(managed.utxos.get(op).ok_or_else(|| { - SeedError::Other(format!("outpoint {}:{} not in account", op.txid, op.vout)) - })?); + let utxo = managed + .utxos + .get(op) + .cloned() + .ok_or_else(|| format!("outpoint {}:{} not in account", op.txid, op.vout))?; + selected.push(utxo); } - // Refused by name as soon as they are named, by the finalizer's own - // check and in its order (unspendable, mid-broadcast, not final). The - // finalizers check again against the coins as they are then. - check_chosen_inputs(named.iter().copied(), height, &pinned).map_err(SeedError::Refused)?; - let selected = requested.clone(); - - // Validation succeeded — only now record them. - (*builder).state().inputs.extend(selected); - Ok::<_, SeedError>(()) + + // Validation succeeded — only now consume the builder. + let taken = (*builder).take_builder(); + (*builder).store_builder(taken.add_inputs(selected)); + Ok::<_, String>(()) }); match result { Ok(()) => PlatformWalletFFIResult::ok(), - Err(SeedError::Refused(error)) => PlatformWalletFFIResult::from(error), - Err(SeedError::Other(e)) => PlatformWalletFFIResult::err( + Err(e) => PlatformWalletFFIResult::err( PlatformWalletFFIResultCode::ErrorWalletOperation, format!("add_inputs_from_outpoints failed: {e}"), ), } } -/// Why `core_wallet_tx_builder_add_inputs_from_outpoints` refused its outpoints. -enum SeedError { - /// A named coin is refused with the finalizer's typed error: unspendable - /// (`ChosenInputUnavailable`, `ErrorInvalidParameter`), mid-broadcast - /// (`InputMidBroadcast`) or not final yet (code 59). - Refused(PlatformWalletError), - Other(String), -} - /// Destroy a transaction builder created by `core_wallet_tx_builder_new`. /// /// # Safety @@ -1121,7 +1021,7 @@ pub unsafe extern "C" fn core_wallet_tx_builder_destroy(builder: *mut FFITransac } let b = Box::from_raw(builder); - drop(Box::from_raw(b.inner as *mut BuilderState)); + let _ = Box::from_raw(b.inner as *mut TransactionBuilder); } /// Free a transaction written by `core_wallet_signed_payment_finalize`. @@ -1200,11 +1100,8 @@ mod pooled_balance_handle_tests { //! published a permanent 0, which zeroed Max and blocked every send on the //! 2026-09-03 QA build (dashwallet-ios#1107 / platform#4582). - use dashcore::blockdata::transaction::special_transaction::asset_lock::AssetLockPayload; use key_wallet::account::account_type::StandardAccountType; - use platform_wallet::test_support::{ - funded_spv_core_wallet, funded_spv_core_wallet_with_outputs, standard_account_coins, - }; + use platform_wallet::test_support::funded_spv_core_wallet; use platform_wallet::SEND_FUNDING_SOURCES; use super::*; @@ -1277,468 +1174,6 @@ mod pooled_balance_handle_tests { assert_ne!(result.code, PlatformWalletFFIResultCode::Success); assert_eq!(out, 0); } - - /// Finalize what the FFI setters built, the way the finalizers do: from - /// the recorded configuration, so a shortfall gets the waiting-coins trial. - fn finalize_built( - builder: *mut FFITransactionBuilder, - core: &platform_wallet::CoreWallet, - signer: &platform_wallet::test_support::WalletSigner, - ) -> Result { - let (_, BuildPlan { state, options }) = - unsafe { FFITransactionBuilder::into_build(builder) }; - let make = move || state.make(); - runtime().block_on(core.finalize_transaction_from( - make, - options, - CoreAccountTypeFFI::AllSpendable.funding_sources(), - 0, - signer, - )) - } - - fn payment(fee_rate: Option, amount: u64) -> *mut FFITransactionBuilder { - let builder = unsafe { core_wallet_tx_builder_new(FFINetwork::Testnet) }; - if let Some(rate) = fee_rate { - let set = unsafe { core_wallet_tx_builder_set_fee_rate(builder, rate) }; - assert_eq!(set.code, PlatformWalletFFIResultCode::Success); - } - let address = - CString::new(DashAddress::dummy(Network::Testnet, 90).to_string()).expect("address"); - let added = unsafe { core_wallet_tx_builder_add_output(builder, address.as_ptr(), amount) }; - assert_eq!(added.code, PlatformWalletFFIResultCode::Success); - builder - } - - /// The app's Send right after a send whose broadcast outcome is unknown: - /// the wallet's only coin is that send's change, not final yet. A second - /// payment built the way the app builds it — one `add_output`, default - /// rate, the pooled `AllSpendable` sources — is refused with code 59 and - /// builds nothing. - #[test] - fn should_refuse_a_second_payment_on_an_unknown_sends_change_with_code_59() { - let change = 8_999_774; - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[change], - &[change], - )); - let result = finalize_built(payment(None, 1_000_000), &core, &signer); - assert!( - matches!( - &result, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { - available: Some(0), - waiting, - outpoint: None, - .. - }) if *waiting == change - ), - "got {result:?}" - ); - let error = result.expect_err("refused"); - assert_eq!( - PlatformWalletFFIResult::from(error).code, - PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork - ); - } - - /// 900,000 final + 101,000 waiting against a 1,000,000 payment. At the - /// rate the builder set, 10,000 duffs/kB, the waiting coin nets too little - /// once its input and the fee are paid: insufficient funds, not code 59. - /// The same build at the default rate would be covered by waiting. - #[test] - fn should_price_a_waiting_shortfall_at_the_fee_rate_the_builder_set() { - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[900_000, 101_000], - &[101_000], - )); - - let high = finalize_built(payment(Some(10_000), 1_000_000), &core, &signer); - assert!( - matches!( - high, - Err(PlatformWalletError::CoreInsufficientFunds { .. } - | PlatformWalletError::CorePooledInsufficientFunds { .. }) - ), - "got {high:?}" - ); - - let default = finalize_built(payment(None, 1_000_000), &core, &signer); - assert!( - matches!( - default, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { .. }) - ), - "got {default:?}" - ); - } - - /// The shortfall is priced at the transaction's serialized shape, every - /// explicit output counted. 900,000 final + 100,400 waiting against four - /// 250,000 outputs: once confirmed, the two inputs and four outputs need a - /// 442-duff fee with no change and only 400 are left, so this is - /// insufficient funds. The same 1,000,000 in one output is covered. - #[test] - fn should_price_a_waiting_shortfall_at_every_output_the_builder_added() { - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[900_000, 100_400], - &[100_400], - )); - - let four = payment(None, 250_000); - for tag in 91..94 { - let address = CString::new(DashAddress::dummy(Network::Testnet, tag).to_string()) - .expect("address"); - let added = - unsafe { core_wallet_tx_builder_add_output(four, address.as_ptr(), 250_000) }; - assert_eq!(added.code, PlatformWalletFFIResultCode::Success); - } - let four = finalize_built(four, &core, &signer); - assert!( - matches!( - four, - Err(PlatformWalletError::CoreInsufficientFunds { .. } - | PlatformWalletError::CorePooledInsufficientFunds { .. }) - ), - "got {four:?}" - ); - - let one = finalize_built(payment(None, 1_000_000), &core, &signer); - assert!( - matches!( - one, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { .. }) - ), - "got {one:?}" - ); - } - - fn is_insufficient( - result: &Result, - ) -> bool { - matches!( - result, - Err(PlatformWalletError::CoreInsufficientFunds { .. } - | PlatformWalletError::CorePooledInsufficientFunds { .. }) - ) - } - - /// A drain of a lone coin that is not final, at 10,000 duffs/kB (a - /// 1,920-duff fee): 1,600 duffs do not even pay the fee, and 2,400 pay it - /// but leave 480 — under the 546-duff dust floor key-wallet's drain - /// requires — so both are insufficient funds even once confirmed. 100,000 - /// duffs drain fine once confirmed: waiting. - #[test] - fn should_judge_a_drain_shortfall_by_its_fee_and_dust() { - for (coin, waiting) in [(1_600, false), (2_400, false), (100_000, true)] { - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[coin], - &[coin], - )); - let drain = payment(Some(10_000), 0); - let set = unsafe { - core_wallet_tx_builder_set_selection_strategy(drain, CoreSelectionStrategyFFI::All) - }; - assert_eq!(set.code, PlatformWalletFFIResultCode::Success); - let result = finalize_built(drain, &core, &signer); - if waiting { - assert!( - matches!( - result, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { waiting, .. }) - if waiting == coin - ), - "coin {coin}: got {result:?}" - ); - } else { - assert!( - !matches!( - result, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { .. }) - ) && result.is_err(), - "coin {coin}: got {result:?}" - ); - } - } - } - - /// A drain cannot leave an uneconomic coin out, so its feasibility is the - /// gross value of every coin against the fee of spending all of them. - /// Two non-final outputs of one transaction, 2,467 and 600 duffs, drained - /// at 10,000 duffs/kB: once they confirm (together) the two-input - /// transaction's 3,400-duff fee alone exceeds their 3,067 duffs — - /// insufficient funds, not code 59. Coin by coin, net of each input's fee - /// (987 + 0 against a 986-duff one-output reserve), it looked covered. - #[test] - fn should_judge_a_drain_by_the_gross_value_of_every_coin() { - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[2_467, 600], - &[2_467, 600], - )); - let drain = payment(Some(10_000), 0); - let set = unsafe { - core_wallet_tx_builder_set_selection_strategy(drain, CoreSelectionStrategyFFI::All) - }; - assert_eq!(set.code, PlatformWalletFFIResultCode::Success); - - let result = finalize_built(drain, &core, &signer); - assert!(is_insufficient(&result), "got {result:?}"); - } - - /// key-wallet checks a drain's inputs against its placeholder amount - /// before draining: a 1,000,000-duff placeholder over one non-final - /// 100,000-duff coin is insufficient funds, not code 59. - #[test] - fn should_hold_a_drain_to_its_placeholder_amount() { - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[100_000], - &[100_000], - )); - let drain = payment(None, 1_000_000); - let set = unsafe { - core_wallet_tx_builder_set_selection_strategy(drain, CoreSelectionStrategyFFI::All) - }; - assert_eq!(set.code, PlatformWalletFFIResultCode::Success); - - let result = finalize_built(drain, &core, &signer); - assert!(is_insufficient(&result), "got {result:?}"); - } - - /// A drain key-wallet would refuse on its shape (two value outputs) is not - /// called waiting, however much is unconfirmed. - #[test] - fn should_not_call_a_misshapen_drain_waiting() { - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[100_000], - &[100_000], - )); - let drain = payment(None, 0); - let address = - CString::new(DashAddress::dummy(Network::Testnet, 96).to_string()).expect("address"); - let added = unsafe { core_wallet_tx_builder_add_output(drain, address.as_ptr(), 0) }; - assert_eq!(added.code, PlatformWalletFFIResultCode::Success); - let set = unsafe { - core_wallet_tx_builder_set_selection_strategy(drain, CoreSelectionStrategyFFI::All) - }; - assert_eq!(set.code, PlatformWalletFFIResultCode::Success); - - let result = finalize_built(drain, &core, &signer); - assert!( - !matches!( - result, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { .. }) - ) && result.is_err(), - "got {result:?}" - ); - } - - /// An asset lock spends what its credit outputs burn, set through the - /// payload setter with no explicit output. With every coin not final - /// (500,000 duffs), a burn beyond them is insufficient funds, not code 59; - /// one they cover is code 59. - #[test] - fn should_size_an_asset_lock_shortfall_by_its_burn() { - for (burn, waiting) in [(5_000_000, false), (100_000, true)] { - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[500_000], - &[500_000], - )); - let payload = - TransactionPayload::AssetLockPayloadType(AssetLockPayload::new(vec![TxOut { - value: burn, - script_pubkey: DashAddress::dummy(Network::Testnet, 95).script_pubkey(), - }])); - let bytes = bincode::encode_to_vec(payload, bincode::config::standard()) - .expect("encode an asset lock payload"); - let lock = unsafe { core_wallet_tx_builder_new(FFINetwork::Testnet) }; - let set = unsafe { - core_wallet_tx_builder_set_special_payload(lock, bytes.as_ptr(), bytes.len()) - }; - assert_eq!(set.code, PlatformWalletFFIResultCode::Success); - - let result = finalize_built(lock, &core, &signer); - if waiting { - assert!( - matches!( - result, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { - waiting: 500_000, - .. - }) - ), - "burn {burn}: got {result:?}" - ); - } else { - assert!(is_insufficient(&result), "burn {burn}: got {result:?}"); - } - } - } - - /// An input recorded on the builder is finalized from the wallet's copy as - /// it is then: one not final by that time is refused by name (code 59), - /// with and without `use_only_added_inputs`. - #[test] - fn should_refuse_a_recorded_input_that_is_not_final_when_finalized() { - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[900_000, 1_500_000], - &[1_500_000], - )); - let coin = runtime() - .block_on(standard_account_coins( - &core, - StandardAccountType::BIP44Account, - )) - .into_iter() - .find(|utxo| utxo.value() == 1_500_000) - .expect("the non-final coin") - .outpoint; - for only_added in [false, true] { - let builder = payment(None, 100_000); - unsafe { (*builder).state().inputs.push(coin) }; - if only_added { - let set = unsafe { core_wallet_tx_builder_use_only_added_inputs(builder) }; - assert_eq!(set.code, PlatformWalletFFIResultCode::Success); - } - let result = finalize_built(builder, &core, &signer); - assert!( - matches!( - result, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { - outpoint: Some(outpoint), - .. - }) if outpoint == coin - ), - "only_added={only_added}: got {result:?}" - ); - } - } - - /// An explicit zero rate is the rate the build pays, not "unset": 900,000 - /// final + 100,100 waiting fund a 1,000,000 payment for free once the - /// waiting coin confirms — code 59. Left unset, the default rate makes the - /// same build short. - #[test] - fn should_price_an_explicit_zero_fee_rate_as_zero() { - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[900_000, 100_100], - &[100_100], - )); - - let zero = finalize_built(payment(Some(0), 1_000_000), &core, &signer); - assert!( - matches!( - zero, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { .. }) - ), - "got {zero:?}" - ); - - let unset = finalize_built(payment(None, 1_000_000), &core, &signer); - assert!(is_insufficient(&unset), "got {unset:?}"); - } - - /// A special payload is bytes the fee pays for too. 900,000 final + 100,400 - /// waiting against a 1,000,000 payment is covered without one, but a - /// ProUpRevTx payload (132 bytes) leaves too little: insufficient funds. - #[test] - fn should_price_a_waiting_shortfall_with_the_special_payload() { - use dashcore::blockdata::transaction::special_transaction::provider_update_revocation::ProviderUpdateRevocationPayload; - use dashcore::bls_sig_utils::BLSSignature; - use dashcore::hash_types::InputsHash; - use dashcore::hashes::Hash; - - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[900_000, 100_400], - &[100_400], - )); - let payload = TransactionPayload::ProviderUpdateRevocationPayloadType( - ProviderUpdateRevocationPayload::new( - dashcore::Txid::all_zeros(), - 0, - InputsHash::all_zeros(), - BLSSignature::from([0u8; 96]), - ), - ); - let bytes = bincode::encode_to_vec(payload, bincode::config::standard()) - .expect("encode a revocation payload"); - - let with_payload = payment(None, 1_000_000); - let set = unsafe { - core_wallet_tx_builder_set_special_payload(with_payload, bytes.as_ptr(), bytes.len()) - }; - assert_eq!(set.code, PlatformWalletFFIResultCode::Success); - let with_payload = finalize_built(with_payload, &core, &signer); - assert!(is_insufficient(&with_payload), "got {with_payload:?}"); - - let without = finalize_built(payment(None, 1_000_000), &core, &signer); - assert!( - matches!( - without, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { .. }) - ), - "got {without:?}" - ); - } - - /// A fee rate whose fee arithmetic overflows is refused where the host - /// sets it — a typed error, not a panic or a wrapped fee at finalize — - /// and the builder stays usable: a sane rate set after it builds. - #[test] - fn should_refuse_an_overflowing_fee_rate_when_it_is_set() { - let (core, signer) = - runtime().block_on(funded_spv_core_wallet(StandardAccountType::BIP44Account)); - let builder = payment(None, 100_000); - let refused = unsafe { core_wallet_tx_builder_set_fee_rate(builder, u64::MAX) }; - assert_eq!( - refused.code, - PlatformWalletFFIResultCode::ErrorInvalidParameter - ); - let set = unsafe { core_wallet_tx_builder_set_fee_rate(builder, 1_000) }; - assert_eq!(set.code, PlatformWalletFFIResultCode::Success); - let built = finalize_built(builder, &core, &signer).expect("a sane rate builds"); - runtime().block_on(core.abandon_transaction(&built)); - } - - /// With no coin final, the trial decides: a payment the waiting coins - /// could never cover is insufficient funds, one they cover is code 59. - #[test] - fn should_call_a_payment_beyond_every_coin_insufficient_with_no_final_coin() { - let (core, signer) = runtime().block_on(funded_spv_core_wallet_with_outputs( - StandardAccountType::BIP44Account, - &[500_000], - &[500_000], - )); - - let beyond = finalize_built(payment(None, 5_000_000), &core, &signer); - assert!( - !matches!( - beyond, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { .. }) - ), - "got {beyond:?}" - ); - assert!(beyond.is_err()); - - let within = finalize_built(payment(None, 100_000), &core, &signer); - assert!( - matches!( - within, - Err(PlatformWalletError::CoreFundsAwaitingNetwork { .. }) - ), - "got {within:?}" - ); - } } #[cfg(test)] @@ -1859,394 +1294,3 @@ mod tests { assert_eq!(sole_deliverable_value(&[burn]), 0); } } - -#[cfg(test)] -mod real_finalizer_tests { - //! The extern finalizers end to end: a wallet handle, a mnemonic - //! resolver handle, the recorded builder — no shortcut through the - //! Rust API — so their wiring (recipe replay, the waiting-coins trial, - //! options) is what is tested. - - use super::*; - use crate::core_wallet::broadcast::core_wallet_signed_transaction_free; - use crate::core_wallet::signed_payment::registry_test_guard; - use platform_wallet::test_support::{ - add_bip44_coin, pin_in_broadcast, test_platform_wallet_manager, - }; - use rs_sdk_ffi::{ - dash_sdk_mnemonic_resolver_create, dash_sdk_mnemonic_resolver_destroy, - mnemonic_resolver_result, - }; - use std::ffi::CStr; - - /// The mnemonic `test_platform_wallet_manager` creates its wallet from. - const PHRASE: &str = "abandon abandon abandon abandon abandon abandon \ - abandon abandon abandon abandon abandon about"; - - unsafe extern "C" fn resolve( - _ctx: *const c_void, - _wallet_id_bytes: *const u8, - out_buf: *mut c_char, - out_capacity: usize, - out_len: *mut usize, - ) -> i32 { - let phrase = PHRASE.as_bytes(); - if phrase.len() + 1 > out_capacity { - return mnemonic_resolver_result::BUFFER_TOO_SMALL; - } - std::ptr::copy_nonoverlapping(phrase.as_ptr() as *const c_char, out_buf, phrase.len()); - *out_buf.add(phrase.len()) = 0; - *out_len = phrase.len(); - mnemonic_resolver_result::SUCCESS - } - - unsafe extern "C" fn noop_destroy(_ctx: *mut c_void) {} - - /// A registered wallet whose only coin is `value` duffs, final or not. - fn wallet_with_coin(value: u64, final_: bool) -> (Handle, impl Drop) { - let (manager, wallet_id) = runtime().block_on(test_platform_wallet_manager()); - let wallet = runtime() - .block_on(manager.get_wallet(&wallet_id)) - .expect("wallet present"); - runtime().block_on(add_bip44_coin(&wallet, value, final_, COIN_TAG)); - let handle = PLATFORM_WALLET_STORAGE.insert(wallet); - /// Unregisters the handle; keeps the manager (which owns the wallet's - /// event task) alive until then. - struct Release { - handle: Handle, - _manager: std::sync::Arc, - } - impl Drop for Release { - fn drop(&mut self) { - PLATFORM_WALLET_STORAGE.remove(self.handle); - } - } - ( - handle, - Release { - handle, - _manager: manager, - }, - ) - } - - /// The txid byte of the coin `wallet_with_coin` adds (vout 0). - const COIN_TAG: u8 = 0x5a; - - fn coin_outpoint() -> OutPointFFI { - OutPointFFI { - txid: [COIN_TAG; 32], - vout: 0, - } - } - - fn finalize_real( - builder: *mut FFITransactionBuilder, - wallet: Handle, - ) -> (PlatformWalletFFIResult, Handle) { - let resolver = unsafe { - dash_sdk_mnemonic_resolver_create(std::ptr::null_mut(), resolve, noop_destroy) - }; - let mut out: Handle = 0; - let result = unsafe { - core_wallet_tx_builder_finalize( - builder, - wallet, - CoreAccountTypeFFI::AllSpendable, - 0, - resolver, - &mut out, - ) - }; - unsafe { dash_sdk_mnemonic_resolver_destroy(resolver) }; - (result, out) - } - - /// Coin control through the real finalizer: the coin recorded with - /// `add_inputs_from_outpoints` under `use_only_added_inputs` is the input. - #[test] - fn should_spend_the_recorded_input_through_the_real_finalizer() { - let (wallet, _release) = wallet_with_coin(8_999_774, true); - let builder = app_payment(1_000_000); - let chosen = [coin_outpoint()]; - let added = unsafe { - core_wallet_tx_builder_add_inputs_from_outpoints( - builder, - wallet, - CoreAccountTypeFFI::BIP44, - 0, - chosen.as_ptr(), - chosen.len(), - ) - }; - assert_eq!(added.code, PlatformWalletFFIResultCode::Success); - let only = unsafe { core_wallet_tx_builder_use_only_added_inputs(builder) }; - assert_eq!(only.code, PlatformWalletFFIResultCode::Success); - - let (result, out) = finalize_real(builder, wallet); - assert_eq!(result.code, PlatformWalletFFIResultCode::Success); - let spent = CORE_SIGNED_TRANSACTION_STORAGE - .with_item(out, |tx| { - tx.transaction - .transaction() - .input - .iter() - .map(|input| input.previous_output) - .collect::>() - }) - .expect("published handle"); - assert_eq!( - spent, - vec![OutPoint { - txid: Txid::from_byte_array([COIN_TAG; 32]), - vout: 0, - }] - ); - core_wallet_signed_transaction_free(out); - } - - /// A recorded input spent before the build is refused by name, as - /// `ErrorInvalidParameter` (`ChosenInputUnavailable`), nothing published. - #[test] - fn should_refuse_a_recorded_input_spent_since_through_the_real_finalizer() { - let (manager, wallet_id) = runtime().block_on(test_platform_wallet_manager()); - let platform_wallet = runtime() - .block_on(manager.get_wallet(&wallet_id)) - .expect("wallet present"); - runtime().block_on(add_bip44_coin(&platform_wallet, 8_999_774, true, COIN_TAG)); - let wallet = PLATFORM_WALLET_STORAGE.insert(platform_wallet.clone()); - let builder = app_payment(1_000_000); - let chosen = [coin_outpoint()]; - let added = unsafe { - core_wallet_tx_builder_add_inputs_from_outpoints( - builder, - wallet, - CoreAccountTypeFFI::BIP44, - 0, - chosen.as_ptr(), - chosen.len(), - ) - }; - assert_eq!(added.code, PlatformWalletFFIResultCode::Success); - runtime().block_on(async { - // Spent since it was chosen. - let mut wm = platform_wallet.wallet_manager().write().await; - let (_, info) = wm.get_wallet_and_info_mut(&wallet_id).expect("wallet"); - info.core_wallet - .first_bip44_managed_account_mut() - .expect("bip44 account") - .utxos - .clear(); - }); - - let (result, out) = finalize_real(builder, wallet); - PLATFORM_WALLET_STORAGE.remove(wallet); - assert_eq!( - result.code, - PlatformWalletFFIResultCode::ErrorInvalidParameter - ); - assert_eq!(out, 0); - } - - /// A locked coin that is not final either: confirmation would not make - /// it usable, so naming it is the native finalizer's refusal - /// (`ChosenInputUnavailable::NotSpendable`, `ErrorInvalidParameter`), not - /// code 59. - #[test] - fn should_refuse_a_locked_coin_as_unspendable_before_finality() { - let (manager, wallet_id) = runtime().block_on(test_platform_wallet_manager()); - let platform_wallet = runtime() - .block_on(manager.get_wallet(&wallet_id)) - .expect("wallet present"); - runtime().block_on(add_bip44_coin(&platform_wallet, 8_999_774, false, COIN_TAG)); - runtime().block_on(async { - let mut wm = platform_wallet.wallet_manager().write().await; - let (_, info) = wm.get_wallet_and_info_mut(&wallet_id).expect("wallet"); - for utxo in info - .core_wallet - .first_bip44_managed_account_mut() - .expect("bip44 account") - .utxos - .values_mut() - { - utxo.is_locked = true; - } - }); - let wallet = PLATFORM_WALLET_STORAGE.insert(platform_wallet.clone()); - let builder = app_payment(1_000_000); - let chosen = [coin_outpoint()]; - let added = unsafe { - core_wallet_tx_builder_add_inputs_from_outpoints( - builder, - wallet, - CoreAccountTypeFFI::BIP44, - 0, - chosen.as_ptr(), - chosen.len(), - ) - }; - PLATFORM_WALLET_STORAGE.remove(wallet); - assert_eq!( - added.code, - PlatformWalletFFIResultCode::ErrorInvalidParameter - ); - // The refusal of this coin, not another invalid-parameter path. - let message = unsafe { CStr::from_ptr(added.message) }.to_string_lossy(); - assert!(message.contains("not spendable"), "{message}"); - assert!( - message.contains(&Txid::from_byte_array([COIN_TAG; 32]).to_string()), - "{message}" - ); - assert!(unsafe { (*builder).state() }.inputs.is_empty()); - unsafe { core_wallet_tx_builder_destroy(builder) }; - } - - /// A coin an in-flight broadcast pins is refused when it is named, as the - /// finalizer would refuse it (input mid-broadcast, which crosses as - /// `ErrorUnknown`) — final or not: the fence comes before code 59, since - /// waiting would not clear it. Not recorded. - #[test] - fn should_refuse_a_coin_pinned_by_an_in_flight_broadcast() { - for final_ in [true, false] { - let (manager, wallet_id) = runtime().block_on(test_platform_wallet_manager()); - let platform_wallet = runtime() - .block_on(manager.get_wallet(&wallet_id)) - .expect("wallet present"); - let coin = runtime().block_on(add_bip44_coin( - &platform_wallet, - 8_999_774, - final_, - COIN_TAG, - )); - let _pin = runtime().block_on(pin_in_broadcast(&platform_wallet, coin)); - let wallet = PLATFORM_WALLET_STORAGE.insert(platform_wallet.clone()); - let builder = app_payment(1_000_000); - let chosen = [coin_outpoint()]; - let added = unsafe { - core_wallet_tx_builder_add_inputs_from_outpoints( - builder, - wallet, - CoreAccountTypeFFI::BIP44, - 0, - chosen.as_ptr(), - chosen.len(), - ) - }; - PLATFORM_WALLET_STORAGE.remove(wallet); - assert_eq!(added.code, PlatformWalletFFIResultCode::ErrorUnknown); - let message = unsafe { CStr::from_ptr(added.message) }.to_string_lossy(); - assert!(message.contains(&coin.to_string()), "{message}"); - assert!(unsafe { (*builder).state() }.inputs.is_empty()); - unsafe { core_wallet_tx_builder_destroy(builder) }; - } - } - - /// The app's Send: one output, default rate, the pooled sources. - fn app_payment(amount: u64) -> *mut FFITransactionBuilder { - let builder = unsafe { core_wallet_tx_builder_new(FFINetwork::Testnet) }; - let address = - CString::new(DashAddress::dummy(Network::Testnet, 97).to_string()).expect("address"); - let added = unsafe { core_wallet_tx_builder_add_output(builder, address.as_ptr(), amount) }; - assert_eq!(added.code, PlatformWalletFFIResultCode::Success); - builder - } - - /// The app's shape through `core_wallet_tx_builder_finalize`: the only - /// coin is the change of a send whose broadcast outcome is unknown (not - /// final), and a second payment is code 59, with no handle published. - #[test] - fn should_return_code_59_from_the_real_finalizer_for_the_apps_second_payment() { - let (wallet, _release) = wallet_with_coin(8_999_774, false); - let resolver = unsafe { - dash_sdk_mnemonic_resolver_create(std::ptr::null_mut(), resolve, noop_destroy) - }; - let mut out: Handle = 7; - let result = unsafe { - core_wallet_tx_builder_finalize( - app_payment(1_000_000), - wallet, - CoreAccountTypeFFI::AllSpendable, - 0, - resolver, - &mut out, - ) - }; - unsafe { dash_sdk_mnemonic_resolver_destroy(resolver) }; - assert_eq!( - result.code, - PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork - ); - assert_eq!(out, 0, "nothing is published"); - } - - /// The same through the deferred finalizer, which shares the plumbing. - #[test] - fn should_return_code_59_from_the_deferred_finalizer_too() { - let _registry = registry_test_guard(); - let (wallet, _release) = wallet_with_coin(8_999_774, false); - let resolver = unsafe { - dash_sdk_mnemonic_resolver_create(std::ptr::null_mut(), resolve, noop_destroy) - }; - let (mut token, mut fee) = (0u64, 0u64); - let mut txid: *mut c_char = std::ptr::null_mut(); - let mut tx = FFICoreTransaction { - tx_bytes: std::ptr::null_mut(), - tx_len: 0, - fee: 0, - }; - let mut bytes: *const u8 = std::ptr::null(); - let mut len: usize = 0; - let result = unsafe { - core_wallet_signed_payment_finalize( - app_payment(1_000_000), - wallet, - CoreAccountTypeFFI::AllSpendable, - 0, - resolver, - &mut token, - &mut fee, - &mut txid, - &mut tx, - &mut bytes, - &mut len, - ) - }; - unsafe { dash_sdk_mnemonic_resolver_destroy(resolver) }; - assert_eq!( - result.code, - PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork - ); - assert_eq!(token, 0); - } - - /// A final coin funds the same payment: signed through the resolver, a - /// handle published. - #[test] - fn should_finalize_and_sign_through_the_real_finalizer() { - let (wallet, _release) = wallet_with_coin(8_999_774, true); - let resolver = unsafe { - dash_sdk_mnemonic_resolver_create(std::ptr::null_mut(), resolve, noop_destroy) - }; - let mut out: Handle = 0; - let result = unsafe { - core_wallet_tx_builder_finalize( - app_payment(1_000_000), - wallet, - CoreAccountTypeFFI::AllSpendable, - 0, - resolver, - &mut out, - ) - }; - unsafe { dash_sdk_mnemonic_resolver_destroy(resolver) }; - let message = (!result.message.is_null()) - .then(|| unsafe { std::ffi::CStr::from_ptr(result.message) }.to_string_lossy()); - assert_eq!( - result.code, - PlatformWalletFFIResultCode::Success, - "{message:?}" - ); - assert_ne!(out, 0); - core_wallet_signed_transaction_free(out); - } -} diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs index c111c98abd5..68e3dd8ff8a 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs @@ -449,8 +449,8 @@ impl WalletChangeSetFFI { /// only when a re-emitted `TransactionRecord` flows through `records` /// with `context = InstantSend(..)`. The standalone IS-lock map is /// dropped here. Acceptable as long as the event adapter re-emits - /// affected records (it does for `TransactionDetected`, - /// `BlockProcessed` and `TransactionInstantLocked`). + /// affected records (it currently does for `TransactionDetected` + /// and `BlockProcessed` but NOT for `TransactionInstantLocked`). /// When the standalone IS-lock event needs to flow to Swift, add /// a `BTreeMap`-shaped FFI field here and populate it. pub fn from_changeset(cs: &platform_wallet::changeset::CoreChangeSet) -> Self { diff --git a/packages/rs-platform-wallet-ffi/src/error.rs b/packages/rs-platform-wallet-ffi/src/error.rs index 331b63a0ca7..3c138a56904 100644 --- a/packages/rs-platform-wallet-ffi/src/error.rs +++ b/packages/rs-platform-wallet-ffi/src/error.rs @@ -601,14 +601,6 @@ pub enum PlatformWalletFFIResultCode { /// Platform returned no balance for a managed identity. Retrying this read /// is safe; this does not imply missing ownership or require registration. ErrorIdentityBalanceUnavailable = 58, - /// A Core payment the balance covers only with coins the network has not - /// confirmed yet — neither InstantSend-locked nor mined - /// ([`platform_wallet::PlatformWalletError::CoreFundsAwaitingNetwork`]). - /// Not a shortfall and nothing was sent: the coins become spendable once - /// their transaction is InstantSend-locked or mined, so the host should - /// say the money is waiting on the network and offer a retry later — not - /// "insufficient funds". - ErrorCoreFundsAwaitingNetwork = 59, /// The named thing does not exist. /// @@ -967,12 +959,6 @@ impl From for PlatformWalletFFIResult { PlatformWalletError::InputMidBroadcast { .. } => { PlatformWalletFFIResultCode::ErrorUnknown } - // A chosen input the build cannot spend (not held by the funding - // accounts, or not spendable yet): the caller's parameter. The - // outpoint and the reason travel in the message. - PlatformWalletError::ChosenInputUnavailable { .. } => { - PlatformWalletFFIResultCode::ErrorInvalidParameter - } // A definitively-failed address-nonce race (reaches the blanket impl // via identity `top_up_from_addresses` → `?`/`.into()`). Exposing // provided/expected nonce as structured out-fields is INTENTIONALLY @@ -982,9 +968,6 @@ impl From for PlatformWalletFFIResult { PlatformWalletError::AddressNonceMismatch { .. } => { PlatformWalletFFIResultCode::ErrorAddressNonceMismatch } - PlatformWalletError::CoreFundsAwaitingNetwork { .. } => { - PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork - } // Both shapes are "the wallet cannot cover this payment"; hosts // classify and retry them identically, so the pooled variant rides // the same code rather than forcing every host to learn a second @@ -2597,27 +2580,6 @@ mod tests { } } -#[cfg(test)] -mod core_funds_awaiting_network_tests { - use super::*; - - #[test] - fn should_give_unconfirmed_funds_their_own_code_apart_from_insufficient_funds() { - let result = PlatformWalletFFIResult::from(PlatformWalletError::CoreFundsAwaitingNetwork { - available: Some(700_000), - waiting: 599_000, - required: Some(1_000_000), - outpoint: None, - }); - assert_eq!( - result.code, - PlatformWalletFFIResultCode::ErrorCoreFundsAwaitingNetwork - ); - assert_eq!(result.code as u32, 59); - assert!(!result.message.is_null()); - } -} - #[cfg(test)] mod identity_balance_error_tests { use super::*; diff --git a/packages/rs-platform-wallet/README.md b/packages/rs-platform-wallet/README.md index efcd3806244..ba2d499b542 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -146,62 +146,15 @@ The local shielded balance API introduces three Rust source compatibility change index is zero. An absent row is different from a recorded empty scan; do not infer presence merely from `last_synced_index > 0`. -## Final-inputs and broadcast-probe API migration - -Spending only final coins and probing unresolved broadcasts bring these Rust -source and behaviour changes: - -- `CoreWallet::finalize_transaction_with_options(builder, options, sources, - source_index, signer)` takes a `FinalizeOptions` after the builder in place - of the trailing `reservation_only: bool`: `reservation_only` and `inputs` - (coins the build may spend, by outpoint — the wallet's current copy of each - is seeded under the write guard). `finalize_transaction` keeps its - signature. -- New `CoreWallet::finalize_transaction_from(make, options, sources, - source_index, signer)` takes a factory (`Fn() -> - Result + Send + Sync`, by value, - dropped before the signer runs) instead of a builder. It - builds `make()`, and on a shortfall builds `make()` once more as a trial, - without signing or keeping a reservation, with the coins that are not final - yet treated as final: if key-wallet builds that, the shortfall is - `CoreFundsAwaitingNetwork` (not for a `reservation_only` build, which spends - only its final chosen inputs). A finalizer handed a plain builder cannot make - it again, so its shortfall stays insufficient funds. -- `FinalizeOptions` is `#[non_exhaustive]`: build it with - `FinalizeOptions::default()`, `with_inputs` and `with_reservation_only`. -- FFI: `core_wallet_tx_builder_set_fee_rate` refuses a rate whose fee - arithmetic would overflow (`ErrorInvalidParameter`, above about 42.9 DASH - per kB); `core_wallet_tx_builder_set_current_height` is accepted and ignored - — the finalizers always built at the wallet's own height. -- `PlatformWalletError` gains `ChosenInputUnavailable { outpoint, problem: - ChosenInputProblem }` (new public enum: `NotInFundingAccounts`, - `NotSpendable`; FFI `ErrorInvalidParameter`): a coin chosen by outpoint the - build cannot spend — and a coin seeded on the builder with `add_inputs` - that selection picked but no funding account holds, which used to be a - `TransactionBuild` string error. A short build that would only succeed by - selecting such a seeded coin once waiting coins confirm now returns this - refusal too, where it used to return `CoreInsufficientFunds` / - `CorePooledInsufficientFunds`. An exhaustive `match` needs an arm for it. -- `PlatformWalletError` gains `CoreFundsAwaitingNetwork { available, waiting, - required, outpoint }` (FFI code 59): the build's final coins fall short, but - key-wallet would build it if the coins that are not yet confirmed or - InstantSend-locked were final. An exhaustive `match` needs an arm for it. +## Broadcast-probe API migration + +Probing sends whose broadcast outcome is unknown brings these Rust source +changes: + - `WalletWorker` gains `BroadcastProbes`, reported in the shutdown report. An exhaustive `match` needs an arm for it. -- Behaviour: every payment build that funds from the wallet, and - `pooled_spendable_balance` / `pooled_max_sendable`, use only confirmed or - InstantSend-locked coins. A coin in `FinalizeOptions::inputs` that a - funding account holds is judged as the wallet holds it when the build is - finalized: a candidate if final by then (the only kind with - `reservation_only`), refused with `CoreFundsAwaitingNetwork` naming its - outpoint if not, with `InputMidBroadcast` if an in-flight broadcast pins it. - A chosen coin the funding accounts don't hold (another account's, or spent - since it was chosen) is refused by name in both funding modes: the caller - picked it, so it is not dropped silently. The one exception: a coin another - in-flight build holds reserved is left out by key-wallet's reservation - filter (reservations are not readable from platform-wallet). A coin - seeded on the builder itself (`add_inputs`) is the caller's snapshot; a - selected one that has lost its final status is refused the same way. +- `WalletGeneration::teardown_guard` returns a `TeardownGuard` in place of the + bare `OwnedRwLockWriteGuard<()>`. ## Dependencies diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index 7fe2fae841c..73acd93a831 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -1223,26 +1223,6 @@ async fn build_core_changeset( let mut cs = CoreChangeSet::default(); cs.instant_locks_for_non_final_records .insert(*txid, instant_lock.clone()); - // The persister learns a record's context only from a re-emitted - // record; the lock map above does not reach it. Upstream rewrote - // the record's context to `InstantSend` before emitting this - // event, so re-emit the manager's slices for the txid — without - // this the stored row stays at mempool until the block, and a - // send the network has locked keeps reading "Sending". Same - // shape as a `BlockProcessed` update: records and the owned - // outputs' verdicts, no UTXO topology change. `None` / no slices - // (wallet gone, record pruned) leave just the lock. - if let Some((slices, verdicts)) = - wallet_slices_and_verdicts_for_txid(wallet_manager, wallet_id, txid).await - { - cs.account_records = slices - .into_iter() - .filter(|r| !is_contact_watch_only(r)) - .collect(); - cs.records = cs.account_records.clone(); - crate::changeset::changeset::fold_same_txid_records(&mut cs.records); - cs.utxo_credit_verdicts = verdicts; - } cs } WalletEvent::BlockProcessed { @@ -3819,119 +3799,6 @@ mod contact_watch_only_projection_tests { ); } - /// An InstantSend lock on a mempool send re-emits the send's row with - /// its `InstantSend` context. The lock event carries only a txid, so - /// without the re-emission the stored row stayed at mempool until the - /// block — a send the network had locked kept reading "Sending". - #[tokio::test] - async fn an_instant_lock_re_emits_the_locked_record() { - use crate::wallet::core::WalletGeneration; - use crate::wallet::identity::IdentityManager; - use dashcore::ephemerealdata::instant_lock::InstantLock; - use key_wallet::test_utils::TestWalletContext; - use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; - - let mut ctx = TestWalletContext::new_random(); - let funding = Transaction::dummy(&ctx.receive_address.clone(), 0..1, &[100_000_000]); - let funded = ctx - .check_transaction( - &funding, - TransactionContext::InChainLockedBlock(BlockInfo::new( - 1, - BlockHash::from_slice(&[4u8; 32]).expect("valid block hash"), - 1_700_000_000, - )), - ) - .await; - assert!(funded.is_relevant, "funding tx should be relevant"); - - // A send of the whole coin to a foreign address: no change, so - // no output of ours for the lock to mark — only the context moves. - let spend = Transaction { - version: 2, - lock_time: 0, - input: vec![TxIn { - previous_output: OutPoint { - txid: funding.txid(), - vout: 0, - }, - script_sig: ScriptBuf::new(), - sequence: 0xffffffff, - witness: Witness::new(), - }], - output: vec![TxOut { - value: 99_999_000, - script_pubkey: DashAddress::dummy(Network::Testnet, 7).script_pubkey(), - }], - special_transaction_payload: None, - }; - let sent = ctx - .check_transaction(&spend, TransactionContext::Mempool) - .await; - assert!(sent.is_relevant, "the spend should match"); - let lock = InstantLock::default(); - assert!( - ctx.managed_wallet - .mark_instant_send_utxos(&spend.txid(), &lock), - "the lock rewrites the send's context" - ); - - let info = PlatformWalletInfo { - core_wallet: ctx.managed_wallet, - generation: Arc::new(WalletGeneration::new()), - identity_manager: IdentityManager::new(), - tracked_asset_locks: BTreeMap::new(), - dpns_name_states: BTreeMap::new(), - observed_input_conflicts: Default::default(), - }; - let mut wm = WalletManager::::new(dashcore::Network::Testnet); - let wallet_id = wm.insert_wallet(ctx.wallet, info).expect("insert wallet"); - let manager = Arc::new(RwLock::new(wm)); - - let event = WalletEvent::TransactionInstantLocked { - wallet_id, - txid: spend.txid(), - instant_lock: lock, - balance: WalletCoreBalance::default(), - account_balances: BTreeMap::new(), - }; - let cs = build_core_changeset(&manager, &event).await; - - assert!(cs - .instant_locks_for_non_final_records - .contains_key(&spend.txid())); - assert_eq!(cs.records.len(), 1, "the send's row is re-emitted"); - assert_eq!(cs.records[0].txid, spend.txid()); - assert!( - matches!(cs.records[0].context, TransactionContext::InstantSend(_)), - "the row carries the lock, got {:?}", - cs.records[0].context - ); - assert!(cs.new_utxos.is_empty(), "a lock adds no coins"); - assert!(cs.spent_utxos.is_empty(), "a lock spends no coins"); - } - - /// An InstantSend lock for a txid the manager holds no record for - /// (pruned, or a wallet it does not know) keeps the lock alone. - #[tokio::test] - async fn an_instant_lock_without_a_record_carries_only_the_lock() { - use dashcore::ephemerealdata::instant_lock::InstantLock; - let txid = |byte: u8| Txid::from_byte_array([byte; 32]); - let event = WalletEvent::TransactionInstantLocked { - wallet_id: [0x21u8; 32], - txid: txid(9), - instant_lock: InstantLock::default(), - balance: WalletCoreBalance::default(), - account_balances: BTreeMap::new(), - }; - let cs = build_core_changeset(&test_manager(), &event).await; - assert!(cs.records.is_empty()); - assert!(cs.account_records.is_empty()); - assert!(cs - .instant_locks_for_non_final_records - .contains_key(&txid(9))); - } - /// A contact spending an output that a *pre-fix* build already /// persisted must still clear that stale row, so `derive_spent_utxos` /// stays deliberately unfiltered. Only the transaction row and the diff --git a/packages/rs-platform-wallet/src/error.rs b/packages/rs-platform-wallet/src/error.rs index 12efc720ada..73955b6604f 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -271,20 +271,6 @@ pub enum PlatformWalletError { )] InputMidBroadcast { outpoint: dashcore::OutPoint }, - /// A coin the caller chose as an input — by outpoint - /// (`FinalizeOptions::inputs`, FFI `add_inputs_from_outpoints`), or seeded - /// on the builder itself (`TransactionBuilder::add_inputs`) and selected — - /// cannot be spent by this build: the finalizer's funding accounts don't hold it - /// (another account's coin, or spent since it was chosen — refresh the - /// list), or it is not spendable yet (an immature coinbase output, a - /// locked coin). Refused by name rather than left out: the caller picked - /// it. - #[error("chosen input {outpoint} cannot be spent by this build: {problem}")] - ChosenInputUnavailable { - outpoint: dashcore::OutPoint, - problem: ChosenInputProblem, - }, - /// The address handed to [`CoreWallet::sign_message`] cannot be a signing /// target at all: unparseable, encoded for a different network than the /// wallet's, or not P2PKH. A caller-input error — the classic Dash @@ -407,34 +393,6 @@ pub enum PlatformWalletError { required: Option, }, - /// A Core payment the wallet's balance covers, but only with coins the - /// network has not confirmed yet — neither InstantSend-locked nor mined. - /// Builds spend only final coins (`require_final_inputs`): the change of - /// a send that never reached the network never becomes final, so building - /// on it would make a transaction no node accepts. Coins become spendable - /// once their transaction is InstantSend-locked (usually seconds) or mined. - /// Not a shortfall: the host should say the money is waiting on the - /// network. - /// - /// `waiting` is the value of the not-yet-final coins the build would - /// spend once they are final; with `outpoint` set, of that one coin, which - /// the caller chose as an input itself and which is not final. - /// `available` / `required` are key-wallet's figures for the build when it - /// names them. - #[error( - "Core funds are waiting for network confirmation: {} DASH not yet confirmed{}{}{}", - dash_amount(*waiting), - optional_amount(", available", available), - optional_amount(", needed", required), - refused_input(outpoint) - )] - CoreFundsAwaitingNetwork { - available: Option, - waiting: u64, - required: Option, - outpoint: Option, - }, - #[error("no spendable inputs available on {account_type} account {account_index}: {context}")] NoSpendableInputs { account_type: StandardAccountType, @@ -1018,28 +976,6 @@ pub enum PlatformWalletError { ShieldedNotBound, } -/// Why a chosen input cannot be spent -/// ([`PlatformWalletError::ChosenInputUnavailable`]). -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum ChosenInputProblem { - /// The finalizer's funding accounts don't hold it: another account's - /// coin, or spent since it was chosen. - NotInFundingAccounts, - /// Held, but not spendable yet: an immature coinbase output, or locked. - NotSpendable, -} - -impl std::fmt::Display for ChosenInputProblem { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(match self { - Self::NotInFundingAccounts => { - "not a coin of any funding account (another account's, or spent since it was chosen)" - } - Self::NotSpendable => "not spendable yet (immature or locked)", - }) - } -} - impl PlatformWalletError { /// A persister `load` failed. /// @@ -1884,33 +1820,6 @@ mod asset_lock_already_consumed_tests { } } -/// Duffs as DASH for an error message: up to eight decimals, trailing zeros -/// dropped (`85998722` → `0.85998722`, `1000000` → `0.01`). -fn dash_amount(duffs: u64) -> String { - let whole = duffs / 100_000_000; - let fraction = duffs % 100_000_000; - if fraction == 0 { - return whole.to_string(); - } - let fraction = format!("{fraction:08}"); - format!("{whole}.{}", fraction.trim_end_matches('0')) -} - -/// `",

( sync_fault: Arc, cancel: CancellationToken, ) -> JoinHandle<()> +where + P: PlatformWalletPersistence + 'static, +{ + spawn_observed_wallet_event_adapter( + wallet_manager, + persister, + receiver, + sync_fault, + cancel, + None, + ) +} + +/// Sees every wallet event the adapter takes off the lossless channel, in +/// order, before it is projected. It runs on the adapter task: it must not +/// block, and a panic in it would end persistence for the session. +pub(crate) type WalletEventObserver = Arc; + +/// [`spawn_wallet_event_adapter`] with an observer of the events it consumes. +/// +/// The persistence channel is the one stream of wallet events that cannot +/// lose any (the broadcast fan-out ends on a lagged receiver), and it has a +/// single receiver, so a second consumer that must not miss an event listens +/// here rather than on a channel of its own. +pub(crate) fn spawn_observed_wallet_event_adapter

( + wallet_manager: Arc>>, + persister: Weak

, + receiver: mpsc::UnboundedReceiver, + sync_fault: Arc, + cancel: CancellationToken, + observer: Option, +) -> JoinHandle<()> where P: PlatformWalletPersistence + 'static, { @@ -276,6 +308,7 @@ where receiver, sync_fault, cancel, + observer, )) } @@ -336,6 +369,7 @@ async fn run_wallet_event_adapter

( mut receiver: mpsc::UnboundedReceiver, sync_fault: Arc, cancel: CancellationToken, + observer: Option, ) where P: PlatformWalletPersistence + 'static, { @@ -390,6 +424,11 @@ async fn run_wallet_event_adapter

( } break; }; + // Before the persister is claimed: the observer sees an event even + // when its drain exits uncommitted. + if let Some(observer) = &observer { + observer(&event); + } // Claim the persister before folding anything else off the channel, so // everything this drain consumes is guaranteed a commit: an owner @@ -444,6 +483,9 @@ async fn run_wallet_event_adapter

( while folded < ADAPTER_STORE_BATCH_LIMIT { match receiver.try_recv() { Ok(event) => { + if let Some(observer) = &observer { + observer(&event); + } let wallet_id = event.wallet_id(); let core = build_core_changeset(&wallet_manager, &event).await; let asset_locks = @@ -4447,6 +4489,43 @@ mod tests { } } + /// The observer sees every event of a burst once and in order, whether + /// the adapter took it as the first of a batch or folded it in. + #[tokio::test] + async fn should_show_the_observer_every_event_in_order() { + use super::{spawn_observed_wallet_event_adapter, WalletEventObserver}; + + const BURST: u32 = 3000; + let wallet_id = [7u8; 32]; + let (tx, rx) = unbounded_channel::(); + for h in 1..=BURST { + tx.send(sync_height_event(wallet_id, h)).unwrap(); + } + let seen = Arc::new(Mutex::new(Vec::new())); + let observer: WalletEventObserver = { + let seen = Arc::clone(&seen); + Arc::new(move |event: &WalletEvent| { + if let WalletEvent::SyncHeightAdvanced { height, .. } = event { + seen.lock().unwrap().push(*height); + } + }) + }; + let (obs_tx, _obs_rx) = unbounded_channel(); + let persister = Arc::new(ProbePersister::new(obs_tx)); + let handle = spawn_observed_wallet_event_adapter( + test_manager(), + Arc::downgrade(&persister), + rx, + Arc::new(AtomicBool::new(false)), + CancellationToken::new(), + Some(observer), + ); + drop(tx); + handle.await.unwrap(); + + assert_eq!(*seen.lock().unwrap(), (1..=BURST).collect::>()); + } + /// (b) THE ROOT-CAUSE FIX: a burst far larger than the old bounded ring /// (`DEFAULT_WALLET_EVENT_CAPACITY` == 1000) is delivered losslessly over /// the unbounded persistence channel, so the fault latch never trips and @@ -4473,6 +4552,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); // Close the channel so the adapter drains the entire burst (across as @@ -4533,6 +4613,7 @@ mod tests { rx, Arc::new(AtomicBool::new(false)), cancel, + None, ) .await; @@ -4587,6 +4668,7 @@ mod tests { rx, Arc::new(AtomicBool::new(false)), cancel, + None, )); // Park inside the first chunk's `store()`, then release the only @@ -4648,6 +4730,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); // 1) Record-bearing event whose store() is rejected → faults wallet. @@ -4702,6 +4785,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); // Trip the guard. @@ -4750,6 +4834,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); let observed = obs_rx @@ -4797,6 +4882,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); let observed = obs_rx.recv().await.expect("merged store must arrive"); @@ -4849,6 +4935,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); let mut heights: BTreeMap> = BTreeMap::new(); @@ -4896,6 +4983,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); // The store for this batch panics: no observation is emitted, and the @@ -4983,6 +5071,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); // Park the commit inside a synchronous `store()`. @@ -5044,6 +5133,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); assert_eq!( @@ -5127,6 +5217,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); // All three in one drain: `healthy` stores, `doomed` panics, and @@ -5228,6 +5319,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); // Trip the fault and wait until it has latched. @@ -5316,6 +5408,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); tx.send(swept_event(wallet_id, 0x51, 0x52)).unwrap(); @@ -5390,6 +5483,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); // Bounded like the neighbouring capability tests below: both the @@ -5439,6 +5533,7 @@ mod tests { rx, Arc::clone(&sync_fault), cancel.clone(), + None, )); tx.send(swept_event(wallet_id, 0x61, 0x62)).unwrap(); @@ -6760,6 +6855,7 @@ mod tests { rx, Arc::new(AtomicBool::new(false)), CancellationToken::new(), + None, )); handle.await.expect("the adapter exits on a closed channel"); diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index bb44735c05e..da0972c0de2 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -528,7 +528,10 @@ impl PlatformWalletManager

{ /// resubmitted to evonodes over DAPI — right after dash-spv reports a send /// `Uncertain` (each report), then, while the wallet follows the chain tip, /// every block for 24 blocks from when it first did so for that root, and - /// every 10 blocks after, a return from the background included — and + /// every 10 blocks after, a return from the background included, 55 + /// times at most (about a day of following the tip, counted within one + /// session), after which only a + /// new `Uncertain` report probes the root — and /// each change of a send's verdict is published as /// `on_outgoing_transaction_probed`, each send that no longer needs one /// as `on_outgoing_transaction_cleared`. Nothing in the wallet changes. diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index f3b14884fd9..ff67fb38449 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -22,10 +22,11 @@ use tokio::sync::{Notify, RwLock}; use tokio::task::JoinHandle; use tokio_util::sync::CancellationToken; -use key_wallet_manager::WalletManager; +use key_wallet_manager::{WalletEvent, WalletManager}; use crate::broadcast_probe::DapiAcceptanceProbe; -use crate::changeset::{spawn_wallet_event_adapter, PlatformWalletPersistence}; +use crate::changeset::core_bridge::spawn_observed_wallet_event_adapter; +use crate::changeset::PlatformWalletPersistence; use crate::events::{PlatformEventHandler, PlatformEventManager}; use crate::manager::dashpay_sync::DashPaySyncManager; use crate::manager::dpns_sync::DpnsSyncManager; @@ -512,16 +513,33 @@ impl PlatformWalletManager

{ // adapter raises it the first time it freezes a durable watermark. let sync_fault = Arc::new(std::sync::atomic::AtomicBool::new(false)); + // BroadcastResolver probes unconfirmed sends whose broadcast outcome + // was unknown (read-only; see its module docs). It publishes through + // the event manager it is registered with, so it gets a weak handle + // to it once that exists. Built before the adapter, which also feeds + // it wallet events from the lossless channel. + let broadcast_resolver = Arc::new(BroadcastResolver::new( + Arc::new(DapiAcceptanceProbe::new(Arc::clone(&sdk))), + Arc::clone(&wallet_manager), + )); // Spawn the wallet-event adapter that translates upstream // `WalletEvent`s into `CoreChangeSet`s and forwards them to // the persister. let event_adapter_cancel = CancellationToken::new(); - let event_adapter_join = spawn_wallet_event_adapter( + // Weak: the resolver's lifetime stays with the manager, not with + // the adapter task, which may outlive it draining its backlog. + let resolver_observer = Arc::downgrade(&broadcast_resolver); + let event_adapter_join = spawn_observed_wallet_event_adapter( Arc::clone(&wallet_manager), Arc::downgrade(&persister), event_receiver, Arc::clone(&sync_fault), event_adapter_cancel.clone(), + Some(Arc::new(move |event: &WalletEvent| { + if let Some(resolver) = resolver_observer.upgrade() { + resolver.on_recorded_wallet_event(event); + } + })), ); // Build handler list: app handler + internal handlers. @@ -550,14 +568,6 @@ impl PlatformWalletManager

{ Arc::clone(&wallet_manager), Arc::clone(&persister) as Arc, )); - // BroadcastResolver probes unconfirmed sends whose broadcast outcome - // was unknown (read-only; see its module docs). It publishes through - // the event manager it is registered with, so it gets a weak handle - // to it once that exists. - let broadcast_resolver = Arc::new(BroadcastResolver::new( - Arc::new(DapiAcceptanceProbe::new(Arc::clone(&sdk))), - Arc::clone(&wallet_manager), - )); let event_manager = Arc::new(PlatformEventManager::new(vec![ app_handler, lock_handler, diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 2b13e0d8eb7..a7d2c2248d9 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -54,7 +54,9 @@ //! root is probed every block for [`EVERY_BLOCK_WINDOW`] blocks, counted //! from its first probe by a height-driven pass while the wallet follows the //! tip (a forced pass does not start it), and every [`SLOW_INTERVAL`] blocks -//! after. A probe no node answered (DAPI unreachable — its addresses +//! after, [`MAX_SLOW_PROBES`] times at most: from then on only a new +//! `Uncertain` report probes it, and its last verdict stays published. A +//! probe no node answered (DAPI unreachable — its addresses //! still banned just after an outage) does not count: its root is due again //! at the next block, whatever its phase, and at the same height a retry //! probes it again (as does any `Uncertain` report, which forces a probe) — after [`NO_ANSWER_RETRY`] if no block comes @@ -74,7 +76,11 @@ //! one whose last pass had nothing left to probe is idle and not scanned //! again until a wallet event that touched its records (a new or //! InstantSend-locked transaction, a block that changed records, a sweep) or -//! an `Uncertain` result for one of its transactions wakes it. No verdict +//! an `Uncertain` result for one of its transactions wakes it. Those wallet +//! events wake it twice: from dash-spv's fan-out, which is timely but ends +//! when a receiver lags, and from the lossless persistence channel +//! ([`BroadcastResolver::on_recorded_wallet_event`]), so a lost one cannot +//! leave a wallet idle over an unsettled send. No verdict //! pauses the probing of a root: neither a mempool nor the nodes' word on a //! block is settlement. //! Probes run only while the SPV client delivers blocks, i.e. while the app is @@ -121,6 +127,21 @@ pub(crate) const EVERY_BLOCK_WINDOW: u32 = 24; /// Blocks between probes once [`EVERY_BLOCK_WINDOW`] has passed. pub(crate) const SLOW_INTERVAL: u32 = 10; +/// Probes of a root after its every-block window, beyond which the schedule +/// stops probing it: about a day of following the tip, as long as dash-spv +/// itself keeps rebroadcasting a send. A root that never settles would +/// otherwise be resubmitted for as long as the wallet holds it — and a +/// wallet's records can be fed by a peer (see [`MAX_ROOTS_PER_PASS`]), so +/// such roots would hold probe slots for good. Counted in probes a node +/// answered, not in blocks, so the blocks an app slept through do not use it +/// up; a forced probe (an `Uncertain` report, a retry) after the window +/// counts like a scheduled one. A new `Uncertain` report still probes a +/// retired root, and so does a probe of it that no node answered. Counted +/// within one session: the schedule lives in memory, so a relaunch, a +/// rewind, switching probing off and on, or registering the wallet again +/// starts a root's count anew. +pub(crate) const MAX_SLOW_PROBES: u32 = 55; + /// Scheduled roots a pass probes at most; forced ones (an `Uncertain` /// report's, a no-answer retry's) come on top. A wallet's records are not /// all its own doing — a peer can feed it transactions that make many roots — @@ -383,6 +404,9 @@ pub(crate) struct ProbeSchedule { /// Its last probe job panicked: it waits its turn behind the other roots, /// even a reported send's, until a probe of it completes. panicked: bool, + /// Probes a node answered after the every-block window, up to + /// [`MAX_SLOW_PROBES`]. + slow_probes: u32, } impl ProbeSchedule { @@ -396,11 +420,20 @@ impl ProbeSchedule { .is_none_or(|first| height.saturating_sub(first) < EVERY_BLOCK_WINDOW) } + /// Out of slow probes ([`MAX_SLOW_PROBES`]): the schedule probes the + /// root no more. + fn retired(&self) -> bool { + self.slow_probes >= MAX_SLOW_PROBES + } + pub(crate) fn is_due(&self, height: u32) -> bool { match self.last_probe { None => true, Some(last) if height <= last => false, - Some(_) if self.unanswered || self.in_window(height) => true, + // A probe no node answered is owed whatever the root's phase. + Some(_) if self.unanswered => true, + Some(_) if self.retired() => false, + Some(_) if self.in_window(height) => true, Some(last) => height - last >= SLOW_INTERVAL, } } @@ -413,18 +446,33 @@ impl ProbeSchedule { self.unanswered = false; } + /// Whether the probe sent at `height` reached a node. One that reached + /// none is due again; one that did counts towards [`MAX_SLOW_PROBES`] + /// once the every-block window is over. + fn answered(&mut self, answered: bool, height: u32) { + self.unanswered = !answered; + if answered + && self.window_start.is_some() + && !self.in_window(height) + && self.slow_probes < MAX_SLOW_PROBES + { + self.slow_probes += 1; + } + } + /// The last probe produced nothing: due again at once. fn withdraw(&mut self) { self.last_probe = None; } /// The lowest height at which [`is_due`](Self::is_due) holds. + /// `u32::MAX` once the root is retired: no height makes it due again. pub(crate) fn next_due(&self) -> u32 { match self.last_probe { None => 0, - Some(last) if self.unanswered || self.in_window(last.saturating_add(1)) => { - last.saturating_add(1) - } + Some(last) if self.unanswered => last.saturating_add(1), + Some(_) if self.retired() => u32::MAX, + Some(last) if self.in_window(last.saturating_add(1)) => last.saturating_add(1), Some(last) => last.saturating_add(SLOW_INTERVAL), } } @@ -808,9 +856,15 @@ pub(crate) fn mark_sent( /// Whether a root's last probe reached a node (see /// [`ProbeSchedule::unanswered`]). -fn mark_answered(state: &mut ResolverState, wallet_id: WalletId, root: Txid, answered: bool) { +fn mark_answered( + state: &mut ResolverState, + wallet_id: WalletId, + root: Txid, + answered: bool, + height: u32, +) { if let Some(schedule) = state.schedules.get_mut(&(wallet_id, root)) { - schedule.unanswered = !answered; + schedule.answered(answered, height); schedule.panicked = false; } } @@ -2174,7 +2228,7 @@ impl Actor { let height = probing.height; let events = record_probe(&mut self.state, wallet_id, height, &root, &verdict); deliver(&self.sink, events); - mark_answered(&mut self.state, wallet_id, root.txid, answered); + mark_answered(&mut self.state, wallet_id, root.txid, answered, height); if !answered { self.arm_no_answer_retry(wallet_id, root.txid, height); } @@ -2586,22 +2640,61 @@ fn is_settled(record: &TransactionRecord) -> bool { record.is_confirmed() || matches!(record.context, TransactionContext::InstantSend(_)) } -impl EventHandler for BroadcastResolver { - fn on_wallet_event(&self, event: &WalletEvent) { - let wallet_id = event.wallet_id(); +impl BroadcastResolver { + /// Tell the actor which sends `event` settled or removed. + fn send_settled(&self, event: &WalletEvent) { // While probing is off nothing is published, so there is nothing to // clear (turning it off cleared everything). - let settled = if self.is_enabled() { - settled_or_gone(event) - } else { - HashSet::new() - }; + if !self.is_enabled() { + return; + } + let settled = settled_or_gone(event); if !settled.is_empty() { self.send(Command::Settled { - wallet_id, + wallet_id: event.wallet_id(), txids: settled, }); } + } + + /// A wallet event as the lossless persistence channel delivered it (see + /// `spawn_observed_wallet_event_adapter`), next to the same event from + /// dash-spv's fan-out ([`EventHandler::on_wallet_event`]). + /// + /// The fan-out is timely but can lose events: it ends when a receiver + /// lags, and the client stops until the host restarts it. An idle wallet + /// learns of a new unsettled send only from a wallet event, so one lost + /// there would leave the send unread until it settles. This channel loses + /// none, at the price of running behind the persister's commits, so it + /// repeats only what is harmless to hear late or twice: that the wallet's + /// records changed, which makes its next pass read it — and a read also + /// clears what settled. Heard late, it costs one more read of the wallet + /// at the next block, and no probe. What settled is not repeated by name: named late, + /// after a reorg or a reinstatement brought the send back, it would clear + /// a send the wallet still holds. Heights stay with the fan-out: a + /// missed one is made up by the next. + pub(crate) fn on_recorded_wallet_event(&self, event: &WalletEvent) { + if let Some(command) = recorded_work(event, self.is_enabled()) { + self.send(command); + } + } +} + +/// What a wallet event from the lossless channel tells the actor: that the +/// wallet's records changed, for the events that change them, so its next +/// pass reads it. Nothing for the rest, heights included, and nothing while +/// probing is off (turning it on reads every wallet). +fn recorded_work(event: &WalletEvent, enabled: bool) -> Option { + (enabled && may_leave_work(event)).then(|| Command::Seen { + wallet_id: event.wallet_id(), + touched: true, + }) +} + +impl EventHandler for BroadcastResolver { + fn on_wallet_event(&self, event: &WalletEvent) { + let wallet_id = event.wallet_id(); + self.send_settled(event); let command = match event { WalletEvent::SyncHeightAdvanced { height, .. } => Command::Height { wallet_id, @@ -6401,6 +6494,65 @@ mod tests { assert!(settled_or_gone(&detected(TransactionContext::Mempool)).is_empty()); } + /// The lossless channel repeats only that a wallet's records changed: a + /// new transaction wakes it, a height or a chain lock says nothing, and + /// nothing is said while probing is off. + #[test] + fn should_repeat_only_record_changes_from_the_recorded_events() { + let detected = WalletEvent::TransactionDetected { + wallet_id: wallet(), + record: Box::new(record(4, TransactionContext::Mempool)), + balance: WalletCoreBalance::default(), + account_balances: BTreeMap::new(), + addresses_derived: Vec::new(), + }; + assert!(matches!( + recorded_work(&detected, true), + Some(Command::Seen { wallet_id, touched: true }) if wallet_id == wallet() + )); + assert!(recorded_work(&detected, false).is_none()); + let height = WalletEvent::SyncHeightAdvanced { + wallet_id: wallet(), + height: 101, + }; + assert!(recorded_work(&height, true).is_none()); + let chain_lock = WalletEvent::ChainLockProcessed { + wallet_id: wallet(), + chain_lock: ChainLock::dummy(100), + locked_transactions: BTreeMap::new(), + }; + assert!(recorded_work(&chain_lock, true).is_none()); + } + + /// An idle wallet whose new send's event was lost on dash-spv's fan-out + /// is woken by what the lossless channel repeats, and reads the send at + /// the next block. + #[tokio::test] + async fn should_wake_an_idle_wallet_by_what_a_recorded_event_repeats() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![]); + rig.follow(wallet(), 100).await; + assert_eq!(rig.actor.wallets[&wallet()].wait_until, Some(u32::MAX)); + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + + rig.height(wallet(), 102).await; + assert!(probe.probed().is_empty(), "idle: the wallet is not read"); + + let detected = WalletEvent::TransactionDetected { + wallet_id: wallet(), + record: Box::new(record(1, TransactionContext::Mempool)), + balance: WalletCoreBalance::default(), + account_balances: BTreeMap::new(), + addresses_derived: Vec::new(), + }; + rig.send(recorded_work(&detected, true).expect("a new transaction is work")) + .await; + rig.height(wallet(), 103).await; + + assert_eq!(probe.probed(), vec![txid(1)]); + } + /// A reorg brings a settled send back unsettled: a run that starts after /// the settle event reads it as current, and the send is probed again. #[tokio::test] @@ -6939,6 +7091,101 @@ mod tests { assert!(rig.actor.wallets[&wallet()].run.is_none()); } + /// A root that never settles is probed `MAX_SLOW_PROBES` times after its + /// every-block window and no further: the wallet goes idle, its last + /// verdict stays published, and a new report of the send still probes it. + #[tokio::test] + async fn should_stop_probing_a_root_out_of_slow_probes() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + let first = rig.actor.state.schedules[&(wallet(), txid(1))] + .window_start + .expect("the first followed probe starts the window"); + rig.sent(); + + // Through the every-block window, then well past the last slow probe. + let window_end = first + EVERY_BLOCK_WINDOW - 1; + for height in 102..=window_end { + rig.height(wallet(), height).await; + } + let in_window = probe.probed().len(); + for height in window_end + 1..=window_end + (MAX_SLOW_PROBES + 3) * SLOW_INTERVAL { + rig.height(wallet(), height).await; + } + let retired = probe.probed().len(); + assert_eq!(retired, in_window + MAX_SLOW_PROBES as usize); + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].slow_probes, + MAX_SLOW_PROBES + ); + assert_eq!( + rig.actor.wallets[&wallet()].wait_until, + Some(u32::MAX), + "nothing left to wait for" + ); + assert!(rig.sent().is_empty(), "the verdict stays as published"); + + rig.send(Command::Uncertain(txid(1))).await; + assert_eq!( + probe.probed().len(), + retired + 1, + "a report probes it again" + ); + } + + /// The count is of probes a node answered, after the window: the blocks + /// an app slept through do not use it up, and neither does a probe no + /// node answered or one that was withdrawn — which stay due even for a + /// retired root. It stops at the cap. + #[test] + fn should_retire_a_schedule_by_its_answered_slow_probes() { + let mut schedule = ProbeSchedule::default(); + schedule.anchor(100); + schedule.probed_at(100); + schedule.answered(true, 100); + assert_eq!(schedule.slow_probes, 0, "inside the window"); + // A day of blocks later, never followed: still due, not retired. + assert!(schedule.is_due(100 + 600)); + + let mut height = 100 + 600; + for _ in 0..MAX_SLOW_PROBES { + schedule.probed_at(height); + schedule.answered(true, height); + height += SLOW_INTERVAL; + } + assert_eq!(schedule.next_due(), u32::MAX); + assert!(!schedule.is_due(height)); + assert!(!schedule.is_due(u32::MAX)); + + // A forced probe of the retired root that reached no node is owed. + schedule.probed_at(height); + schedule.answered(false, height); + assert!(schedule.is_due(height + 1)); + assert_eq!(schedule.next_due(), height + 1); + + // Withdrawn (its probe produced nothing): due once more. + schedule.probed_at(height + 1); + schedule.withdraw(); + assert!(schedule.is_due(height + 1)); + + // Answered at last: retired again, the count still at the cap. + schedule.probed_at(height + 1); + schedule.answered(true, height + 1); + assert_eq!(schedule.slow_probes, MAX_SLOW_PROBES); + assert_eq!(schedule.next_due(), u32::MAX); + } + + /// A root probed only by forced passes has no window, so nothing counts. + #[test] + fn should_not_count_probes_of_a_root_without_a_window() { + let mut schedule = ProbeSchedule::default(); + schedule.probed_at(700); + schedule.answered(true, 700); + assert_eq!(schedule.slow_probes, 0); + } + /// After the every-block window a root is due only every SLOW_INTERVAL /// blocks: the heights in between do not walk the wallet's records. #[tokio::test] diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index 8ff97769991..1456e1c1570 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -6,8 +6,10 @@ import DashSDKFFI public enum OutgoingTransactionVerdict: Sendable, Equatable { /// A node holds the transaction: it is in a mempool or a block. case accepted - /// No verdict yet; the SDK asks again on a later block. A refusal by the - /// nodes asked lands here too: no answer proves a send can never land. + /// No verdict yet; the SDK asks again on later blocks, up to a limit per + /// launch (see `setBroadcastProbeEnabled`). Nothing is published when it + /// stops asking: the verdict stays `unresolved`. A refusal by the nodes asked lands + /// here too: no answer proves a send can never land. /// `reason` is diagnostic, never user-facing copy. case unresolved(reason: String) } @@ -127,7 +129,9 @@ extension PlatformWalletManager { /// after SPV reports a broadcast uncertain (each report), then, while the /// wallet follows the chain tip, every block for 24 blocks from when it /// first did so for that root, and every 10 blocks after — a return from - /// the background does not restart the 24 — plus at the end of a sync + /// the background does not restart the 24 — 55 times at most (about a + /// day of following the tip, counted within one launch), after which only a new uncertain report + /// probes the root and its last verdict stays published; plus at the end of a sync /// that was catching up (a launch, a reconnect). A root no evonode /// answered (DAPI unreachable) is due again at every block, and retried a /// minute later if no block has come (then after 2 and 4 more minutes; From 990ee4c9e502f9c71c5e7958e6c24c09f5cbd3ce Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 13:31:29 +0300 Subject: [PATCH 152/169] fix(platform-wallet): bound the roots one report or retry forces in a pass A forced root was probed whatever their number. One report forces the roots of its send's whole chain, and a peer can feed the wallet the incoming parents a send spends, so a single report could queue hundreds of probes ahead of the next report. A pass now forces at most MAX_FORCED_ROOTS_PER_PASS roots, the wallet's own sends first, then those probed longest ago. The others take their turn among the scheduled roots, where a reported send's roots go first. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/core/broadcast_resolver.rs | 181 +++++++++++++++--- 1 file changed, 155 insertions(+), 26 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index a7d2c2248d9..79819d9c5a1 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -63,7 +63,9 @@ //! first, then after twice and four times that again (at most //! [`MAX_NO_ANSWER_RETRIES`] per height). Probes never ban a DAPI address //! themselves. Each Uncertain report forces a probe of its chain's current -//! root in the next pass (reports queued together share it); a pass under +//! roots in the next pass (reports queued together share it), at most +//! [`MAX_FORCED_ROOTS_PER_PASS`] of them, the wallet's own sends first; the +//! others take their turn among the scheduled roots. A pass under //! way in a wallet whose own send it is gives way to it after its probe in //! flight — once per height; a later request waits behind it. A pass probes at most //! [`MAX_ROOTS_PER_PASS`] scheduled roots, those probed longest ago first — @@ -143,7 +145,8 @@ pub(crate) const SLOW_INTERVAL: u32 = 10; pub(crate) const MAX_SLOW_PROBES: u32 = 55; /// Scheduled roots a pass probes at most; forced ones (an `Uncertain` -/// report's, a no-answer retry's) come on top. A wallet's records are not +/// report's, a no-answer retry's) come on top, at most +/// [`MAX_FORCED_ROOTS_PER_PASS`] of them. A wallet's records are not /// all its own doing — a peer can feed it transactions that make many roots — /// so the work of one pass is bounded, and within it the roots probed /// longest ago (or never) go first: the rest stay due and come round at the @@ -151,6 +154,17 @@ pub(crate) const MAX_SLOW_PROBES: u32 = 55; /// go first, but take at most half of the slots while other roots wait. pub(crate) const MAX_ROOTS_PER_PASS: usize = 8; +/// Forced roots a pass probes at most. One report can force many: a send's +/// chain reaches every unsettled ancestor, and a peer can feed the wallet +/// incoming parents for it to spend, so the roots of a single reported send +/// are not bounded by what the wallet did. The wallet's own sends go first, +/// then those probed longest ago (or never). A forced root beyond the +/// allowance is not forced any more: it takes its turn among the scheduled +/// roots, where a reported send's roots go first, in this pass if it follows +/// the tip and at the next heights. As many as a scheduled pass probes, so a +/// retry can repeat every root of the pass no node answered. +pub(crate) const MAX_FORCED_ROOTS_PER_PASS: usize = MAX_ROOTS_PER_PASS; + /// How long after a probe that no node answered (all of DAPI unreachable — /// the network just coming back, its addresses still banned for at least a /// minute) the wallet's roots are probed again instead of waiting for the @@ -731,7 +745,7 @@ pub(crate) struct PassStart { /// roots of every `forced` transaction's chain (dash-spv has just reported /// it `Uncertain`) are due regardless of their schedule — even if one went /// out at this height already: a repeat probe is cheaper than bookkeeping -/// that tells the two apart. +/// that tells the two apart — up to [`MAX_FORCED_ROOTS_PER_PASS`] of them. /// `anchor`: the tip height /// a height-driven pass knows the wallet follows — a root it takes starts /// its every-block window when its probe goes out (see [`mark_sent`]). @@ -771,7 +785,10 @@ pub(crate) fn begin_pass( let roots = graph.roots(); let forced_roots = graph.roots_of(forced.iter().copied()); - // Forced roots first, then at most `MAX_ROOTS_PER_PASS` scheduled ones + // Forced roots first, at most `MAX_FORCED_ROOTS_PER_PASS`: own sends + // before the rest (a reported send that is its own root is what the + // report is about; its unsettled ancestors can be a peer's doing), then + // in turn. Then at most `MAX_ROOTS_PER_PASS` scheduled ones // in turn: those probed longest ago first, a never-probed one by the // height it was first seen at — a queue, so fresh roots (a peer can feed // them at will) wait behind older ones and cannot starve them. Reported @@ -786,31 +803,46 @@ pub(crate) fn begin_pass( forced, retry: false, }; - let mut due = VecDeque::new(); type Turn = (u32, bool, Txid); - let mut first: Vec<(Turn, &OutgoingView)> = Vec::new(); - let mut rest: Vec<(Turn, &OutgoingView)> = Vec::new(); + let mut forced_queue: Vec<((bool, Turn), &OutgoingView)> = Vec::new(); + let mut scheduled_queue: Vec<(Turn, bool, &OutgoingView)> = Vec::new(); for view in &roots { let schedule = state.schedules.entry((wallet_id, view.txid)).or_default(); if let Some(anchor) = anchor { schedule.seen_at.get_or_insert(anchor); } + // At the same height, one still waiting for its first probe goes + // before one probed there. + let turn = ( + schedule.last_probe.or(schedule.seen_at).unwrap_or(0), + schedule.last_probe.is_some(), + view.txid, + ); if forced_roots.contains(&view.txid) { - due.push_back(due_root(view, true)); - } else if anchor.is_some() && schedule.is_due(height) { - // Without an anchor the pass is confined to forced roots. - // At the same height, one still waiting for its first probe goes - // before one probed there. - let turn = ( - schedule.last_probe.or(schedule.seen_at).unwrap_or(0), - schedule.last_probe.is_some(), - view.txid, - ); - if priority.contains(&view.txid) && !schedule.panicked { - first.push((turn, view)); - } else { - rest.push((turn, view)); - } + forced_queue.push(((!graph.is_own(&view.txid), turn), view)); + } + // Without an anchor the pass is confined to forced roots. + if anchor.is_some() && schedule.is_due(height) { + let first = priority.contains(&view.txid) && !schedule.panicked; + scheduled_queue.push((turn, first, view)); + } + } + let forced_due = take_in_turn(forced_queue, MAX_FORCED_ROOTS_PER_PASS); + let taken: HashSet = forced_due.iter().map(|(_, view)| view.txid).collect(); + let mut due: VecDeque = forced_due + .into_iter() + .map(|(_, view)| due_root(view, true)) + .collect(); + let mut first: Vec<(Turn, &OutgoingView)> = Vec::new(); + let mut rest: Vec<(Turn, &OutgoingView)> = Vec::new(); + for (turn, goes_first, view) in scheduled_queue { + if taken.contains(&view.txid) { + continue; + } + if goes_first { + first.push((turn, view)); + } else { + rest.push((turn, view)); } } let first_slots = first @@ -2153,8 +2185,10 @@ impl Actor { return; } // A pass of exactly those roots, ahead of the per-pass cap and - // of nothing else: it takes no scheduled roots (they would - // fail too while no node answers) and starts no window. + // of nothing else, within the forced allowance + // (`MAX_FORCED_ROOTS_PER_PASS`): it takes no scheduled roots + // (they would fail too while no node answers) and starts no + // window. let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(height); pending.retried.extend(roots); @@ -2264,8 +2298,9 @@ impl Actor { /// then twice and four times that again (about 1, 3 and 7 minutes after /// the first probe), at most [`MAX_NO_ANSWER_RETRIES`] per height, while /// no block has come. A retry is a pass of exactly those roots (still - /// roots), ahead of the per-pass cap: it takes no scheduled roots and - /// starts no window. While DAPI + /// roots), ahead of the per-pass cap and within + /// [`MAX_FORCED_ROOTS_PER_PASS`] (the rest wait for the next block): it + /// takes no scheduled roots and starts no window. While DAPI /// stays down, an unanswered root is due once at every block (in its turn /// under [`MAX_ROOTS_PER_PASS`] when a backlog is larger) plus the /// retries, which go ahead of the cap, until the next block comes. One info line per such root and @@ -4871,6 +4906,100 @@ mod tests { assert_eq!(since.first(), Some(&txid(200)), "{since:?}"); } + /// A send that spends twenty unsettled incoming parents, with those + /// parents: one report of it forces twenty roots. + fn send_over_many_parents(send_n: u8) -> Vec { + let parents = 10..30u8; + let spent: Vec = parents.clone().map(|parent| outpoint(parent, 0)).collect(); + let mut views: Vec = parents + .map(|parent| incoming(parent, &[outpoint(parent.wrapping_add(100), 0)])) + .collect(); + views.push(send(send_n, &spent)); + views + } + + /// One reported send can force more roots than a pass may probe (a peer + /// can feed the parents it spends): the pass takes + /// `MAX_FORCED_ROOTS_PER_PASS` of them and no more, anchored or not, and + /// an anchored pass adds its scheduled slots on top. + #[test] + fn should_bound_the_roots_one_report_forces() { + let views = send_over_many_parents(200); + let forced: HashSet = [txid(200)].into(); + + let mut state = ResolverState::default(); + let unanchored = begin_pass(&mut state, wallet(), 100, &views, &forced, &forced, None); + assert_eq!(unanchored.due.len(), MAX_FORCED_ROOTS_PER_PASS); + assert!(unanchored.due.iter().all(|root| root.forced)); + + let mut state = ResolverState::default(); + let anchored = begin_pass( + &mut state, + wallet(), + 100, + &views, + &forced, + &forced, + Some(100), + ); + let forced_roots = anchored.due.iter().filter(|root| root.forced).count(); + assert_eq!(forced_roots, MAX_FORCED_ROOTS_PER_PASS); + assert_eq!( + anchored.due.len(), + MAX_FORCED_ROOTS_PER_PASS + MAX_ROOTS_PER_PASS + ); + let distinct: HashSet = anchored.due.iter().map(|root| root.txid).collect(); + assert_eq!(distinct.len(), anchored.due.len(), "no root twice"); + } + + /// Among forced roots the wallet's own sends go first: a reported send + /// that is its own root is not left behind the peer-fed parents of + /// another reported send. + #[test] + fn should_put_own_sends_first_among_forced_roots() { + let mut views = send_over_many_parents(200); + views.push(send(201, &[outpoint(251, 0)])); + let forced: HashSet = [txid(200), txid(201)].into(); + let mut state = ResolverState::default(); + + let start = begin_pass(&mut state, wallet(), 100, &views, &forced, &forced, None); + + assert_eq!(start.due.len(), MAX_FORCED_ROOTS_PER_PASS); + assert_eq!(start.due.front().map(|root| root.txid), Some(txid(201))); + } + + /// A report whose send has more ancestor roots than a pass may force + /// costs a bounded pass, and a send reported after it is probed at once, + /// not behind the rest of those roots. Following the tip then brings + /// every one of them round within a few blocks. + #[tokio::test] + async fn should_probe_a_later_report_without_waiting_for_an_oversized_one() { + let probe = Arc::new(ScriptedProbe::new(&[])); + let mut rig = enabled(probe.clone()).await; + let mut views = send_over_many_parents(200); + views.push(send(201, &[outpoint(251, 0)])); + rig.views(wallet(), views); + + rig.send(Command::Uncertain(txid(200))).await; + let oversized = probe.probed(); + assert_eq!(oversized.len(), MAX_FORCED_ROOTS_PER_PASS, "{oversized:?}"); + + rig.send(Command::Uncertain(txid(201))).await; + assert_eq!(probe.probed()[oversized.len()..], [txid(201)]); + + // The roots the forced pass reached were probed longer ago than the + // others were first seen, so they take the first followed pass; the + // twelve left over fill the next two. + rig.follow(wallet(), 100).await; + rig.height(wallet(), 102).await; + rig.height(wallet(), 103).await; + let probed: HashSet = probe.probed().into_iter().collect(); + let missed: Vec = (10..30) + .filter(|parent| !probed.contains(&txid(*parent))) + .collect(); + assert!(missed.is_empty(), "left over and never probed: {missed:?}"); + } + /// A forced root no node answered stays ahead of the cap at its retry, /// whatever the backlog. #[tokio::test(start_paused = true)] From 97fb634cd5b877aa32e2370cc16433e5ebd3472e Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 13:32:34 +0300 Subject: [PATCH 153/169] fix(platform-wallet): look an echo up again when a wallet changes under its lookup A wallet added or removed while an echo's holder lookup was out dropped every such lookup, another wallet's included. The echo had already left the uncertain set, so its acceptance was lost. The lookups are made again instead, against the wallets as they are now. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/core/broadcast_resolver.rs | 100 +++++++++++++++--- 1 file changed, 84 insertions(+), 16 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 79819d9c5a1..74ad779ba33 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -1740,17 +1740,7 @@ impl Actor { if !self.enabled || self.uncertain.remove(&txid).is_none() { return; } - let token = self.next_echo_token; - self.next_echo_token += 1; - self.echo_lookups.insert(txid, token); - let source = Arc::clone(&self.source); - self.jobs.spawn(async move { - JobDone::EchoListed { - txid, - wallets: source.holders(txid).await.own, - token, - } - }); + self.look_up_echo(txid); } Command::SetEnabled(enabled) => { if enabled == self.enabled { @@ -1848,6 +1838,23 @@ impl Actor { } } + /// Find the wallets an echoed send is an unsettled own send of: its + /// acceptance is theirs. A lookup already out for `txid` is superseded — + /// its result no longer carries the current token. + fn look_up_echo(&mut self, txid: Txid) { + let token = self.next_echo_token; + self.next_echo_token += 1; + self.echo_lookups.insert(txid, token); + let source = Arc::clone(&self.source); + self.jobs.spawn(async move { + JobDone::EchoListed { + txid, + wallets: source.holders(txid).await.own, + token, + } + }); + } + /// The registration a pass read, or was to read, is not in the wallet /// manager. This manager's own removals queue `WalletRemoved` under the /// same guard, and commands are handled before job results, so in @@ -1867,15 +1874,20 @@ impl Actor { } /// Drop the wallet's entry, stop its pass and forget its state, the host - /// told to clear what it showed (logged with `trigger`). Report lookups - /// still out are made again, and the wallet leaves `reported`. + /// told to clear what it showed (logged with `trigger`). Report and echo + /// lookups still out are made again, and the wallet leaves `reported`. fn drop_wallet(&mut self, wallet_id: &WalletId, trigger: &'static str) { if let Some(run) = self.wallets.remove(wallet_id).and_then(|entry| entry.run) { run.abort.abort(); } - // Which wallet an echo's lookup will name is not known yet: withdraw - // them all — at worst an acceptance waits for the next probe. - self.echo_lookups.clear(); + // An echo's lookup may name the wallet gone, or a registration this + // one replaces: look again, so the acceptance reaches the wallets + // that hold the send now. The echo was already taken out of + // `uncertain`, so dropping its lookup would lose it for the others. + let echoes: Vec = self.echo_lookups.keys().copied().collect(); + for txid in echoes { + self.look_up_echo(txid); + } // A report's lookup may name the wallet gone: look again, so the // other holders still get their forced probe and priority. let reports: Vec = self.report_lookups.keys().copied().collect(); @@ -5501,6 +5513,62 @@ mod tests { assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); } + /// An echo's acceptance survives another wallet's registration or removal + /// while its lookup is out: the lookup is made again, not dropped. + #[tokio::test] + async fn should_keep_an_echo_whose_lookup_another_wallets_change_overtook() { + let other: WalletId = [8u8; 32]; + for change in [Command::WalletAdded(other), Command::WalletRemoved(other)] { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.views(other, Vec::new()); + rig.send(Command::Seen { + wallet_id: wallet(), + touched: false, + }) + .await; + rig.send(Command::Uncertain(txid(1))).await; + rig.sent(); + + // The echo's lookup is out when the other wallet changes. + rig.handle(Command::Echoed(txid(1))); + rig.handle(change); + rig.settle().await; + + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] + ); + } + } + + /// The wallet an echo was for is removed while the lookup is out: the + /// lookup made again names no holder, and nothing is published for it. + #[tokio::test] + async fn should_drop_an_echo_whose_wallet_was_removed_before_its_lookup_landed() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.send(Command::Seen { + wallet_id: wallet(), + touched: false, + }) + .await; + rig.send(Command::Uncertain(txid(1))).await; + rig.sent(); + + rig.handle(Command::Echoed(txid(1))); + rig.source.views.lock().expect("views").remove(&wallet()); + rig.handle(Command::WalletRemoved(wallet())); + rig.settle().await; + + assert!(!rig + .sent() + .iter() + .any(|event| matches!(event, ResolverEvent::Verdict(..)))); + } + /// A late acceptance is news only to a wallet where the send is still an /// unsettled own send: one that only receives it hears nothing. #[tokio::test] From e7690eed30cf065804a5e8a7cab3cd9c628970d3 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 13:35:08 +0300 Subject: [PATCH 154/169] fix(platform-wallet): run the pass at the tip when a sync's completion overtook the height step Sync events and wallet events come from separate tasks. A completion handled before the height step it follows skipped the wallet as still below the tip, and the step that brought it there was then a catch-up step, so the pass waited for the next block. The actor now remembers the tip for such a wallet, and the step that reaches it runs the pass. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/core/broadcast_resolver.rs | 94 ++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 74ad779ba33..bd32d96f8cf 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -1403,6 +1403,15 @@ struct WalletEntry { /// The height at which a run last gave way to a forced request (see /// [`yield_to_forced`]). yielded_at: Option, + /// The tip of a sync that completed while the wallet's last reported + /// height was still well below it. Sync events and wallet events come + /// from separate tasks, so a completion can be handled before the height + /// step it follows: the catch-up step that then brings the wallet to that + /// tip is its arrival there, and runs the pass the completion could not. + /// Replaced by every later completion, so a wallet that really is + /// rescanning gets its pass when it reaches the latest tip, not a stale + /// one. + completed_ahead: Option, } #[derive(Debug, Clone, Copy)] @@ -1625,7 +1634,21 @@ impl Actor { let events = self.state.forget_wallet(&wallet_id, "rewound"); deliver(&self.sink, events); } - let catch_up = is_catch_up_step(previous, height); + // A step up to the tip of a sync that already completed is + // the wallet arriving at the tip, however large the step: its + // completion was handled first. A rewind is a new scan, which + // the completion did not cover. + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; + let arrived = !rewound + && entry + .completed_ahead + .is_some_and(|tip| near_tip(height, tip)); + if rewound || entry.completed_ahead.is_some_and(|tip| height >= tip) || arrived { + entry.completed_ahead = None; + } + let catch_up = is_catch_up_step(previous, height) && !arrived; if catch_up { // The tip moved on, unfollowed: no lower wallet's steps // may run the echo windows now. @@ -1780,6 +1803,16 @@ impl Actor { // without a step since launch reads its own height. While // probing is off this does nothing (queue_followed_pass): the // next completion, after the next block, covers it. + // + // A wallet below the tip may also be one whose height step + // has not been handled yet: remember the tip for it (see + // `WalletEntry::completed_ahead`). + for entry in self.wallets.values_mut() { + let below = entry + .height + .is_some_and(|height| height < tip && !near_tip(height, tip)); + entry.completed_ahead = below.then_some(tip); + } let at_tip: Vec<(WalletId, u32)> = self .wallets .iter() @@ -4536,6 +4569,65 @@ mod tests { } } + /// Sync events and wallet events come from separate tasks: a completion + /// can be handled before the height step it follows. The step that then + /// brings the wallet to that tip runs the pass the completion could not, + /// instead of waiting for the next block as a catch-up step does. + #[tokio::test] + async fn should_probe_at_the_tip_when_the_completion_overtook_the_height_step() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; // the first step after launch + assert_eq!(probe.probed().len(), 0); + + // The wallet is at 150 already; the actor has not heard yet. + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 150); + rig.send(sync_complete(150)).await; + assert_eq!( + probe.probed().len(), + 0, + "the actor still has the wallet at 100" + ); + + rig.height(wallet(), 150).await; + + assert_eq!(probe.probed().len(), 1); + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].window_start, + Some(150) + ); + } + + /// A completion a wallet is well below is remembered only until the + /// wallet reaches that tip: steps on the way stay catch-up, a later + /// completion replaces the tip, and a rewind forgets it. + #[tokio::test] + async fn should_keep_a_scan_below_a_completed_tip_as_catch_up() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + rig.send(sync_complete(150)).await; + + rig.height(wallet(), 120).await; // on the way + assert_eq!(probe.probed().len(), 0); + rig.send(sync_complete(200)).await; // the tip moved on + rig.height(wallet(), 150).await; // the old tip: still below + assert_eq!(probe.probed().len(), 0); + rig.height(wallet(), 110).await; // a rewind: a new scan + rig.height(wallet(), 200).await; + assert_eq!( + probe.probed().len(), + 0, + "the completion did not cover the new scan" + ); + } + /// The completion after a block whose pass already read the wallet adds /// no second walk over its records. #[tokio::test] From 3c27f1d4f136c3a2ab7a7aece26fc95928b0b75f Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 13:39:28 +0300 Subject: [PATCH 155/169] test(platform-wallet): drive the lossless wake through the manager's own wiring The wake of an idle wallet from the lossless event channel was tested in two halves, neither of which would notice the manager no longer connecting them. The new test builds a manager with a counting probe, lets a wallet go idle, delivers a new send through the lossless channel alone, and waits for the wake and the next block's probe. It fails with the constructor's observer disconnected. The constructor takes its probe through a private variant for that. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/mod.rs | 192 +++++++++++++++++- .../src/wallet/core/broadcast_resolver.rs | 29 +++ 2 files changed, 219 insertions(+), 2 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index ff67fb38449..15d5256952b 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -24,7 +24,7 @@ use tokio_util::sync::CancellationToken; use key_wallet_manager::{WalletEvent, WalletManager}; -use crate::broadcast_probe::DapiAcceptanceProbe; +use crate::broadcast_probe::{AcceptanceProbe, DapiAcceptanceProbe}; use crate::changeset::core_bridge::spawn_observed_wallet_event_adapter; use crate::changeset::PlatformWalletPersistence; use crate::events::{PlatformEventHandler, PlatformEventManager}; @@ -488,6 +488,18 @@ impl PlatformWalletManager

{ sdk: Arc, persister: Arc

, app_handler: Arc, + ) -> Self { + let probe = Arc::new(DapiAcceptanceProbe::new(Arc::clone(&sdk))); + Self::with_acceptance_probe(sdk, persister, app_handler, probe) + } + + /// [`Self::new`] with the probe the broadcast resolver asks the network + /// through. + fn with_acceptance_probe( + sdk: Arc, + persister: Arc

, + app_handler: Arc, + acceptance_probe: Arc, ) -> Self { // Take the manager's lossless, unbounded persistence receiver BEFORE // the manager is wrapped in the shared `Arc` and handed to any @@ -519,7 +531,7 @@ impl PlatformWalletManager

{ // to it once that exists. Built before the adapter, which also feeds // it wallet events from the lossless channel. let broadcast_resolver = Arc::new(BroadcastResolver::new( - Arc::new(DapiAcceptanceProbe::new(Arc::clone(&sdk))), + acceptance_probe, Arc::clone(&wallet_manager), )); // Spawn the wallet-event adapter that translates upstream @@ -1134,6 +1146,9 @@ impl Drop for PlatformWalletManager

{ mod tests { use super::*; + use crate::broadcast_probe::{ProbeOutcome, ProbeVerdict}; + use dashcore::hashes::Hash as _; + use dash_async::WorkerStatus; use crate::changeset::{ @@ -1394,6 +1409,179 @@ mod tests { ); } + /// Counts the probes it is asked for and decides nothing. + #[derive(Default)] + struct CountingProbe(std::sync::atomic::AtomicUsize); + + impl CountingProbe { + fn probes(&self) -> usize { + self.0.load(std::sync::atomic::Ordering::SeqCst) + } + } + + #[async_trait::async_trait] + impl AcceptanceProbe for CountingProbe { + async fn probe(&self, _transaction: &dashcore::Transaction) -> ProbeOutcome { + self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + ProbeOutcome { + verdict: ProbeVerdict::Unresolved { + reason: "counted".to_string(), + }, + answered: true, + } + } + } + + /// Wait for `holds`, asked every few milliseconds, or fail naming `what`. + async fn eventually(what: &str, mut holds: F) + where + F: FnMut() -> Fut, + Fut: std::future::Future, + { + let waited = tokio::time::timeout(std::time::Duration::from_secs(10), async { + while !holds().await { + tokio::time::sleep(std::time::Duration::from_millis(5)).await; + } + }) + .await; + assert!(waited.is_ok(), "never happened: {what}"); + } + + /// The constructor must connect the lossless wallet-event channel to the + /// broadcast resolver: a record change that reaches the resolver only + /// through that channel wakes an idle wallet, and the next block's pass + /// reads it and probes the new send. No SPV client runs here, so nothing + /// forwards the fan-out's copy of the events; without the constructor's + /// observer the wallet stays idle and the first wait below fails. + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn constructor_wires_the_lossless_event_channel_into_the_broadcast_resolver() { + use dashcore::{OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Witness}; + use key_wallet::mnemonic::Mnemonic; + use key_wallet::wallet::initialization::WalletAccountCreationOptions; + use key_wallet_manager::{WalletEvent, WalletInterface}; + + const TEST_MNEMONIC: &str = "abandon abandon abandon abandon abandon abandon \ + abandon abandon abandon abandon abandon about"; + + let probe = Arc::new(CountingProbe::default()); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let manager = PlatformWalletManager::with_acceptance_probe( + sdk, + Arc::new(NoopPersister), + Arc::new(NoopEventHandler) as Arc, + Arc::clone(&probe) as Arc, + ); + let seed = Mnemonic::from_phrase(TEST_MNEMONIC) + .expect("valid test mnemonic") + .to_seed(""); + let wallet_id = manager + .create_wallet_from_seed_bytes( + key_wallet::Network::Testnet, + &seed, + WalletAccountCreationOptions::Default, + Some(0), + ) + .await + .expect("register the test wallet") + .wallet_id(); + let resolver = Arc::clone(&manager.broadcast_resolver); + resolver.set_enabled(true); + // A height step, told to the resolver as the SPV fan-out tells it. + let step_to = |height: u32| { + let manager = &manager; + let resolver = &resolver; + async move { + manager + .wallet_manager + .write() + .await + .update_wallet_synced_height(&wallet_id, height); + resolver.on_wallet_event(&WalletEvent::SyncHeightAdvanced { wallet_id, height }); + } + }; + + // Following the tip with nothing unsettled: the wallet goes idle. + step_to(100).await; + step_to(101).await; + eventually("the wallet goes idle", || resolver.is_idle(&wallet_id)).await; + assert_eq!(probe.probes(), 0); + + // An unconfirmed payment to the wallet, and a send of the wallet's + // that spends it. Their events reach the resolver through the + // lossless channel alone. + let address = manager + .wallet_manager + .read() + .await + .monitored_addresses() + .into_iter() + .next() + .expect("a monitored address"); + let funding = Transaction { + version: 2, + lock_time: 0, + input: vec![TxIn { + previous_output: OutPoint { + txid: dashcore::Txid::from_byte_array([7; 32]), + vout: 0, + }, + script_sig: ScriptBuf::new(), + sequence: 0xffffffff, + witness: Witness::new(), + }], + output: vec![TxOut { + value: 100_000_000, + script_pubkey: address.script_pubkey(), + }], + special_transaction_payload: None, + }; + let send = Transaction { + version: 2, + lock_time: 0, + input: vec![TxIn { + previous_output: OutPoint { + txid: funding.txid(), + vout: 0, + }, + script_sig: ScriptBuf::new(), + sequence: 0xffffffff, + witness: Witness::new(), + }], + output: vec![TxOut { + value: 99_999_000, + script_pubkey: ScriptBuf::new_op_return(&[0u8; 4]), + }], + special_transaction_payload: None, + }; + { + let mut wallet_manager = manager.wallet_manager.write().await; + wallet_manager + .process_mempool_transaction(&funding, None) + .await; + wallet_manager + .process_mempool_transaction(&send, None) + .await; + } + eventually("the lossless channel wakes the idle wallet", || async { + !resolver.is_idle(&wallet_id).await + }) + .await; + assert_eq!(probe.probes(), 0, "a wake reads nothing by itself"); + + step_to(102).await; + eventually("the next block's pass probes the send's chain", || async { + probe.probes() > 0 + }) + .await; + + let report = manager.shutdown().await; + assert_eq!( + report.per_worker.get(&WalletWorker::EventAdapter), + Some(&WorkerStatus::Ok), + "{report:?}" + ); + } + /// `reset_platform_address_sync_state` must fail closed when the /// in-flight pass does not drain: resetting watermarks and balances /// under a live pass would let that pass's tail re-persist the state diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index bd32d96f8cf..c9cff8dce6b 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -1264,6 +1264,14 @@ enum Command { SyncComplete { tip: u32, }, + /// Whether the wallet is idle: no pass running or queued, and none due + /// at any height (see [`WalletEntry::wait_until`]). Answered in order + /// with the other commands. + #[cfg(test)] + IsIdle { + wallet_id: WalletId, + reply: oneshot::Sender, + }, } /// What a finished job reports back to the actor. @@ -1841,6 +1849,15 @@ impl Actor { self.wallets.insert(wallet_id, WalletEntry::default()); } Command::WalletRemoved(wallet_id) => self.drop_wallet(&wallet_id, "removed"), + #[cfg(test)] + Command::IsIdle { wallet_id, reply } => { + let idle = self.wallets.get(&wallet_id).is_some_and(|entry| { + entry.run.is_none() + && entry.pending.is_none() + && entry.wait_until == Some(u32::MAX) + }); + let _ = reply.send(idle); + } } } @@ -2582,6 +2599,18 @@ impl BroadcastResolver { self.enabled.load(Ordering::SeqCst) } + /// Whether the actor holds the wallet idle (see [`Command::IsIdle`]). + /// `false` once the actor is gone. + #[cfg(test)] + pub(crate) async fn is_idle(&self, wallet_id: &WalletId) -> bool { + let (reply, answer) = oneshot::channel(); + self.send(Command::IsIdle { + wallet_id: *wallet_id, + reply, + }); + answer.await.unwrap_or(false) + } + /// Forget a removed wallet — call after it has left the wallet manager: /// its pass in flight, its entry and every verdict, each of which the host /// is told to drop (from the resolver's task, not the caller's). From 81bed29dda8d0bf8b450f09b8859acb49aa2faad Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 14:34:20 +0300 Subject: [PATCH 156/169] fix(platform-wallet): finish a removal only once the resolver is past it A removal only queued the broadcast resolver's part. The resolver's task could be publishing a verdict of that wallet at that moment, and the host received it after `remove_wallet` had returned, where it cannot tell it from a verdict of a wallet registered under the same id since. The removal now waits, outside the wallet-manager lock and for at most five seconds, until the task has handled everything sent before it. The Swift manager bumps its per-wallet generation after that return, so no verdict of the deleted wallet is stamped with the new generation. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/mod.rs | 273 ++++++++++++------ .../src/manager/wallet_lifecycle.rs | 25 ++ .../src/wallet/core/broadcast_resolver.rs | 27 ++ .../PlatformWalletManagerBroadcastProbe.swift | 3 + 4 files changed, 242 insertions(+), 86 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 15d5256952b..58113c1d4fc 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -1432,13 +1432,16 @@ mod tests { } } + /// How long a test waits for something another task does. + const TEST_WAIT: std::time::Duration = std::time::Duration::from_secs(10); + /// Wait for `holds`, asked every few milliseconds, or fail naming `what`. async fn eventually(what: &str, mut holds: F) where F: FnMut() -> Fut, Fut: std::future::Future, { - let waited = tokio::time::timeout(std::time::Duration::from_secs(10), async { + let waited = tokio::time::timeout(TEST_WAIT, async { while !holds().await { tokio::time::sleep(std::time::Duration::from_millis(5)).await; } @@ -1447,29 +1450,80 @@ mod tests { assert!(waited.is_ok(), "never happened: {what}"); } - /// The constructor must connect the lossless wallet-event channel to the - /// broadcast resolver: a record change that reaches the resolver only - /// through that channel wakes an idle wallet, and the next block's pass - /// reads it and probes the new send. No SPV client runs here, so nothing - /// forwards the fan-out's copy of the events; without the constructor's - /// observer the wallet stays idle and the first wait below fails. - #[tokio::test(flavor = "multi_thread", worker_threads = 2)] - async fn constructor_wires_the_lossless_event_channel_into_the_broadcast_resolver() { + /// An InstantSend-locked payment to the manager's wallet and an + /// unsettled send of the wallet's that spends it, as the mempool hands + /// them to the wallet: the send is a root of its own, so its probe's + /// verdict is published. No SPV client runs in these tests, so their + /// events reach the resolver through the lossless channel alone. + async fn receive_and_spend( + manager: &PlatformWalletManager

, + ) { + use dashcore::ephemerealdata::instant_lock::InstantLock; use dashcore::{OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Witness}; + use key_wallet_manager::WalletInterface; + + let spending = |txid, output| Transaction { + version: 2, + lock_time: 0, + input: vec![TxIn { + previous_output: OutPoint { txid, vout: 0 }, + script_sig: ScriptBuf::new(), + sequence: 0xffffffff, + witness: Witness::new(), + }], + output: vec![output], + special_transaction_payload: None, + }; + let mut wallet_manager = manager.wallet_manager.write().await; + let address = wallet_manager + .monitored_addresses() + .into_iter() + .next() + .expect("a monitored address"); + let funding = spending( + dashcore::Txid::from_byte_array([7; 32]), + TxOut { + value: 100_000_000, + script_pubkey: address.script_pubkey(), + }, + ); + let send = spending( + funding.txid(), + TxOut { + value: 99_999_000, + script_pubkey: ScriptBuf::new_op_return(&[0u8; 4]), + }, + ); + let lock = InstantLock { + txid: funding.txid(), + ..Default::default() + }; + wallet_manager + .process_mempool_transaction(&funding, Some(lock)) + .await; + wallet_manager + .process_mempool_transaction(&send, None) + .await; + } + + /// A manager over `probe` and `handler` with one wallet registered and + /// probing on. + async fn probing_manager( + probe: Arc, + handler: Arc, + ) -> (PlatformWalletManager, WalletId) { use key_wallet::mnemonic::Mnemonic; use key_wallet::wallet::initialization::WalletAccountCreationOptions; - use key_wallet_manager::{WalletEvent, WalletInterface}; const TEST_MNEMONIC: &str = "abandon abandon abandon abandon abandon abandon \ abandon abandon abandon abandon abandon about"; - let probe = Arc::new(CountingProbe::default()); let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); let manager = PlatformWalletManager::with_acceptance_probe( sdk, Arc::new(NoopPersister), - Arc::new(NoopEventHandler) as Arc, - Arc::clone(&probe) as Arc, + handler, + probe, ); let seed = Mnemonic::from_phrase(TEST_MNEMONIC) .expect("valid test mnemonic") @@ -1484,91 +1538,59 @@ mod tests { .await .expect("register the test wallet") .wallet_id(); + manager.broadcast_resolver.set_enabled(true); + (manager, wallet_id) + } + + /// Move the wallet's synced height to `height` and tell the resolver, as + /// the SPV fan-out tells it. + async fn step_to( + manager: &PlatformWalletManager

, + wallet_id: WalletId, + height: u32, + ) { + use key_wallet_manager::{WalletEvent, WalletInterface}; + + manager + .wallet_manager + .write() + .await + .update_wallet_synced_height(&wallet_id, height); + manager + .broadcast_resolver + .on_wallet_event(&WalletEvent::SyncHeightAdvanced { wallet_id, height }); + } + + /// The constructor must connect the lossless wallet-event channel to the + /// broadcast resolver: a record change that reaches the resolver only + /// through that channel wakes an idle wallet, and the next block's pass + /// reads it and probes the new send. No SPV client runs here, so nothing + /// forwards the fan-out's copy of the events; without the constructor's + /// observer the wallet stays idle and the first wait below fails. + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn constructor_wires_the_lossless_event_channel_into_the_broadcast_resolver() { + let probe = Arc::new(CountingProbe::default()); + let (manager, wallet_id) = probing_manager( + Arc::clone(&probe) as Arc, + Arc::new(NoopEventHandler), + ) + .await; let resolver = Arc::clone(&manager.broadcast_resolver); - resolver.set_enabled(true); - // A height step, told to the resolver as the SPV fan-out tells it. - let step_to = |height: u32| { - let manager = &manager; - let resolver = &resolver; - async move { - manager - .wallet_manager - .write() - .await - .update_wallet_synced_height(&wallet_id, height); - resolver.on_wallet_event(&WalletEvent::SyncHeightAdvanced { wallet_id, height }); - } - }; // Following the tip with nothing unsettled: the wallet goes idle. - step_to(100).await; - step_to(101).await; + step_to(&manager, wallet_id, 100).await; + step_to(&manager, wallet_id, 101).await; eventually("the wallet goes idle", || resolver.is_idle(&wallet_id)).await; assert_eq!(probe.probes(), 0); - // An unconfirmed payment to the wallet, and a send of the wallet's - // that spends it. Their events reach the resolver through the - // lossless channel alone. - let address = manager - .wallet_manager - .read() - .await - .monitored_addresses() - .into_iter() - .next() - .expect("a monitored address"); - let funding = Transaction { - version: 2, - lock_time: 0, - input: vec![TxIn { - previous_output: OutPoint { - txid: dashcore::Txid::from_byte_array([7; 32]), - vout: 0, - }, - script_sig: ScriptBuf::new(), - sequence: 0xffffffff, - witness: Witness::new(), - }], - output: vec![TxOut { - value: 100_000_000, - script_pubkey: address.script_pubkey(), - }], - special_transaction_payload: None, - }; - let send = Transaction { - version: 2, - lock_time: 0, - input: vec![TxIn { - previous_output: OutPoint { - txid: funding.txid(), - vout: 0, - }, - script_sig: ScriptBuf::new(), - sequence: 0xffffffff, - witness: Witness::new(), - }], - output: vec![TxOut { - value: 99_999_000, - script_pubkey: ScriptBuf::new_op_return(&[0u8; 4]), - }], - special_transaction_payload: None, - }; - { - let mut wallet_manager = manager.wallet_manager.write().await; - wallet_manager - .process_mempool_transaction(&funding, None) - .await; - wallet_manager - .process_mempool_transaction(&send, None) - .await; - } + receive_and_spend(&manager).await; eventually("the lossless channel wakes the idle wallet", || async { !resolver.is_idle(&wallet_id).await }) .await; assert_eq!(probe.probes(), 0, "a wake reads nothing by itself"); - step_to(102).await; + step_to(&manager, wallet_id, 102).await; eventually("the next block's pass probes the send's chain", || async { probe.probes() > 0 }) @@ -1582,6 +1604,85 @@ mod tests { ); } + /// Holds the resolver's task inside a verdict callback until released, + /// and counts the verdicts it was handed. + struct HeldVerdictHandler { + entered: tokio::sync::Notify, + release: std::sync::Mutex>, + verdicts: std::sync::atomic::AtomicUsize, + } + + impl EventHandler for HeldVerdictHandler {} + impl PlatformEventHandler for HeldVerdictHandler { + fn on_outgoing_transaction_probed( + &self, + _wallet_id: &WalletId, + _txid: &dashcore::Txid, + _verdict: &ProbeVerdict, + ) { + self.verdicts + .fetch_add(1, std::sync::atomic::Ordering::SeqCst); + self.entered.notify_one(); + // Released once; later verdicts pass straight through. + let _ = self.release.lock().expect("release").recv(); + } + } + + /// A removal returns only once the resolver's task is past it: a verdict + /// the task was publishing for the wallet when it was removed is + /// delivered before `remove_wallet` returns, and none follows. A host + /// that fences its own queue at that return (the Swift manager bumps a + /// per-wallet generation there) then sees no verdict of the removed + /// wallet stamped as a successor's. + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + async fn remove_wallet_waits_for_a_verdict_the_resolver_is_publishing() { + let (release, held) = std::sync::mpsc::channel(); + let handler = Arc::new(HeldVerdictHandler { + entered: tokio::sync::Notify::new(), + release: std::sync::Mutex::new(held), + verdicts: std::sync::atomic::AtomicUsize::new(0), + }); + let (manager, wallet_id) = probing_manager( + Arc::new(CountingProbe::default()), + Arc::clone(&handler) as Arc, + ) + .await; + let manager = Arc::new(manager); + + // A send to probe, then two steps that follow the tip: the pass + // probes it and publishes its verdict, which the handler holds. + receive_and_spend(&manager).await; + step_to(&manager, wallet_id, 100).await; + step_to(&manager, wallet_id, 101).await; + tokio::time::timeout(TEST_WAIT, handler.entered.notified()) + .await + .expect("the send's verdict is published"); + + let removal = tokio::spawn({ + let manager = Arc::clone(&manager); + async move { manager.remove_wallet(&wallet_id).await } + }); + tokio::time::sleep(std::time::Duration::from_millis(200)).await; + assert!( + !removal.is_finished(), + "the removal returned while a verdict of the wallet was still being delivered" + ); + + drop(release); // the held callback returns; later ones do not wait + removal + .await + .expect("removal task") + .expect("the wallet is removed"); + let delivered = handler.verdicts.load(std::sync::atomic::Ordering::SeqCst); + + // Nothing of the removed wallet's is published from here on. + assert!(manager.broadcast_resolver.caught_up(TEST_WAIT).await); + assert_eq!( + handler.verdicts.load(std::sync::atomic::Ordering::SeqCst), + delivered + ); + } + /// `reset_platform_address_sync_state` must fail closed when the /// in-flight pass does not drain: resetting watermarks and balances /// under a live pass would let that pass's tail re-persist the state diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 115f5a9071d..ecc51b9078b 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -1,6 +1,7 @@ //! Wallet creation, registration, and removal on [`PlatformWalletManager`]. use std::sync::Arc; +use std::time::Duration; use dash_spv::chain::CheckpointManager; use key_wallet::mnemonic::Mnemonic; @@ -45,6 +46,11 @@ fn parse_mnemonic_any_language(phrase: &str) -> Result { Mnemonic::from_phrase(phrase).map_err(|_| "phrase does not match any supported BIP-39 wordlist") } +/// How long a removal waits for the broadcast resolver's task to get past +/// it (see `remove_wallet_with_teardown`). The task only has to finish the +/// one command or result it is handling, a host callback at most. +const RESOLVER_REMOVAL_BUDGET: Duration = Duration::from_secs(5); + /// Test-only rendezvous fired inside [`PlatformWalletManager::remove_wallet_with_teardown`], /// between the inner-manager removal and the public-map removal. /// @@ -921,6 +927,25 @@ impl PlatformWalletManager

{ } } + // The removal above only queued the broadcast resolver's part. Its + // task could be publishing a verdict of this wallet's right now, and + // a host cannot tell one that arrives after this call returned from a + // verdict of a wallet registered under the same id since. Wait for + // the task to get past the removal, outside the wallet-manager lock + // (its reads take that lock). Bounded: a host handler that never + // returns must not hold a removal for ever. + if !self + .broadcast_resolver + .caught_up(RESOLVER_REMOVAL_BUDGET) + .await + { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + "remove_wallet: the broadcast resolver did not get to the removal in time; \ + a verdict for this wallet may still reach the host" + ); + } + // Test-only rendezvous: the window a concurrent same-id registration can // publish a new generation into. Sits AFTER every id-keyed unregister, // so state a midpoint recreation installs is never torn down by this diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index c9cff8dce6b..a6c6d93ade7 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -1264,6 +1264,10 @@ enum Command { SyncComplete { tip: u32, }, + /// Answered once every command sent before it has been handled, the + /// host callbacks those made included (see + /// [`BroadcastResolver::caught_up`]). + Barrier(oneshot::Sender<()>), /// Whether the wallet is idle: no pass running or queued, and none due /// at any height (see [`WalletEntry::wait_until`]). Answered in order /// with the other commands. @@ -1849,6 +1853,10 @@ impl Actor { self.wallets.insert(wallet_id, WalletEntry::default()); } Command::WalletRemoved(wallet_id) => self.drop_wallet(&wallet_id, "removed"), + Command::Barrier(reply) => { + // The waiter may have given up: nothing to do then. + let _ = reply.send(()); + } #[cfg(test)] Command::IsIdle { wallet_id, reply } => { let idle = self.wallets.get(&wallet_id).is_some_and(|entry| { @@ -2599,6 +2607,25 @@ impl BroadcastResolver { self.enabled.load(Ordering::SeqCst) } + /// Wait until the resolver's task has handled everything sent to it + /// before this call, the host callbacks that made included, for at most + /// `budget`. Whether it did; `true` also when the task is gone, since + /// then nothing is published any more. + /// + /// After a removal ([`wallet_removed`](Self::wallet_removed)) this is + /// what makes "no verdict for the removed wallet from now on" true: the + /// removal is only queued, and the task could be publishing a verdict of + /// that wallet's at that moment. The task handles one thing at a time and + /// calls the host from itself, so once it has reached this barrier the + /// removal is behind it and so is every callback from before. Not to be + /// awaited under the wallet-manager lock: the task's reads take it. + pub(crate) async fn caught_up(&self, budget: Duration) -> bool { + let (reply, answer) = oneshot::channel(); + self.send(Command::Barrier(reply)); + // Not timed out: answered, or the task is gone. + tokio::time::timeout(budget, answer).await.is_ok() + } + /// Whether the actor holds the wallet idle (see [`Command::IsIdle`]). /// `false` once the actor is gone. #[cfg(test)] diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index 1456e1c1570..38b5ccce5b9 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -54,6 +54,9 @@ public struct OutgoingTransactionProbeEvent: Sendable, Equatable { /// broadcast-probe callback reads its wallet's generation on the Rust thread /// that delivers it; `deleteWallet` bumps it once the native wallet is gone, /// so a verdict or clear still queued for the main actor is dropped there. +/// The native removal returns only after the resolver's task is past it, so +/// no verdict of the deleted wallet is handed to the callback after the +/// bump and stamped with the new generation. /// A wallet restored under the same deterministic id keeps the bumped /// generation: the deleted wallet's queued callbacks stay stale, its own /// are current. For that reason an entry is never removed: one counter stays From 343c7fcfb2f417631f5643284e9ff0393c0872ae Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 14:55:50 +0300 Subject: [PATCH 157/169] fix(platform-wallet): keep named forced roots and tighten the overtaken completion Local review of the last fixes: - The forced allowance applied to every forced root, so a burst of the wallet's own reported sends lost the ones past it, and a no-answer retry repeated only eight of the roots it had withdrawn. A root forced by name always goes now; the allowance bounds the roots a forced send's chain reaches. - A completion that overtook a wallet's first step since launch was not remembered, and that step stopped the pass the completion had started. - An arrival at a completed tip left a pass anchored before the gap as it was, so its roots' windows started at the height the wallet left. - A remembered completion stood until reached. It now stands for thirty seconds, so a later scan's step at that old tip is not an arrival. - A superseded echo lookup is aborted, as a report lookup is. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/core/broadcast_resolver.rs | 382 +++++++++++++----- .../PlatformWalletManagerBroadcastProbe.swift | 4 +- 2 files changed, 291 insertions(+), 95 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index a6c6d93ade7..31fa36b1854 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -51,6 +51,10 @@ //! unless one already read the wallet at that height — so the pass a //! catch-up skipped runs at the tip without waiting for the next block (a //! completion while probing is off is ignored: the next one covers it). A +//! completion can be handled before the wallet's own step to that tip (the +//! two come from separate tasks): that step, when it follows within +//! [`COMPLETED_AHEAD_TTL`], is the wallet's arrival at the tip and runs the +//! pass, however large it is. A //! root is probed every block for [`EVERY_BLOCK_WINDOW`] blocks, counted //! from its first probe by a height-driven pass while the wallet follows the //! tip (a forced pass does not start it), and every [`SLOW_INTERVAL`] blocks @@ -63,9 +67,10 @@ //! first, then after twice and four times that again (at most //! [`MAX_NO_ANSWER_RETRIES`] per height). Probes never ban a DAPI address //! themselves. Each Uncertain report forces a probe of its chain's current -//! roots in the next pass (reports queued together share it), at most -//! [`MAX_FORCED_ROOTS_PER_PASS`] of them, the wallet's own sends first; the -//! others take their turn among the scheduled roots. A pass under +//! roots in the next pass (reports queued together share it): the send +//! itself when it is a root, and at most [`MAX_FORCED_ROOTS_PER_PASS`] of +//! the roots its chain reaches, the wallet's own sends first; the others +//! take their turn among the scheduled roots. A pass under //! way in a wallet whose own send it is gives way to it after its probe in //! flight — once per height; a later request waits behind it. A pass probes at most //! [`MAX_ROOTS_PER_PASS`] scheduled roots, those probed longest ago first — @@ -145,8 +150,8 @@ pub(crate) const SLOW_INTERVAL: u32 = 10; pub(crate) const MAX_SLOW_PROBES: u32 = 55; /// Scheduled roots a pass probes at most; forced ones (an `Uncertain` -/// report's, a no-answer retry's) come on top, at most -/// [`MAX_FORCED_ROOTS_PER_PASS`] of them. A wallet's records are not +/// report's, a no-answer retry's) come on top: those forced by name, and at +/// most [`MAX_FORCED_ROOTS_PER_PASS`] their chains reach. A wallet's records are not /// all its own doing — a peer can feed it transactions that make many roots — /// so the work of one pass is bounded, and within it the roots probed /// longest ago (or never) go first: the rest stay due and come round at the @@ -154,15 +159,21 @@ pub(crate) const MAX_SLOW_PROBES: u32 = 55; /// go first, but take at most half of the slots while other roots wait. pub(crate) const MAX_ROOTS_PER_PASS: usize = 8; -/// Forced roots a pass probes at most. One report can force many: a send's -/// chain reaches every unsettled ancestor, and a peer can feed the wallet -/// incoming parents for it to spend, so the roots of a single reported send -/// are not bounded by what the wallet did. The wallet's own sends go first, -/// then those probed longest ago (or never). A forced root beyond the -/// allowance is not forced any more: it takes its turn among the scheduled -/// roots, where a reported send's roots go first, in this pass if it follows -/// the tip and at the next heights. As many as a scheduled pass probes, so a -/// retry can repeat every root of the pass no node answered. +/// Roots a pass probes at most because a forced send's chain reaches them. +/// One report can reach many: a send's chain goes back to every unsettled +/// ancestor, and a peer can feed the wallet incoming parents for it to +/// spend, so the roots of a single reported send are not bounded by what the +/// wallet did. The wallet's own sends go first, then those probed longest +/// ago (or never). A root beyond the allowance is not forced: it takes its +/// turn among the scheduled roots, where a reported send's roots go first, +/// in the passes that follow the tip. A pass confined to forced roots (a +/// report's own, or any pass of a wallet behind the tip) leaves it for the +/// first of those. +/// +/// A root that is itself the forced transaction is outside the allowance: a +/// reported send that is its own root (one per report the wallet's own +/// broadcast produced), and each root a no-answer retry repeats (no more +/// than the passes at that height probed). pub(crate) const MAX_FORCED_ROOTS_PER_PASS: usize = MAX_ROOTS_PER_PASS; /// How long after a probe that no node answered (all of DAPI unreachable — @@ -649,6 +660,14 @@ pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { previous.is_none_or(|previous| height < previous || height - previous > CATCH_UP_STEP) } +/// How long a completion that overtook a wallet's height step stands (see +/// `WalletEntry::completed_ahead`). The step it waits for was already on its +/// way, behind another task's queue, so it comes within moments. Without a +/// limit, a completion whose step was lost would turn a step of some later +/// scan that happens to land at that old tip into an arrival, and start +/// windows there. +const COMPLETED_AHEAD_TTL: Duration = Duration::from_secs(30); + /// A wallet at `height` is at a sync's completed `tip`, as far as following /// goes. fn near_tip(height: u32, tip: u32) -> bool { @@ -745,7 +764,9 @@ pub(crate) struct PassStart { /// roots of every `forced` transaction's chain (dash-spv has just reported /// it `Uncertain`) are due regardless of their schedule — even if one went /// out at this height already: a repeat probe is cheaper than bookkeeping -/// that tells the two apart — up to [`MAX_FORCED_ROOTS_PER_PASS`] of them. +/// that tells the two apart. A root named in `forced` always goes; of the +/// roots a forced send's chain only reaches, up to +/// [`MAX_FORCED_ROOTS_PER_PASS`]. /// `anchor`: the tip height /// a height-driven pass knows the wallet follows — a root it takes starts /// its every-block window when its probe goes out (see [`mark_sent`]). @@ -785,10 +806,12 @@ pub(crate) fn begin_pass( let roots = graph.roots(); let forced_roots = graph.roots_of(forced.iter().copied()); - // Forced roots first, at most `MAX_FORCED_ROOTS_PER_PASS`: own sends - // before the rest (a reported send that is its own root is what the - // report is about; its unsettled ancestors can be a peer's doing), then - // in turn. Then at most `MAX_ROOTS_PER_PASS` scheduled ones + // Forced roots first. A root named in `forced` itself always goes: a + // reported send that is its own root is what the report is about, and a + // retry names the roots it repeats. The roots a forced send's chain + // only reaches (its unsettled ancestors, which can be a peer's doing) + // are at most `MAX_FORCED_ROOTS_PER_PASS`: own sends before the rest, + // then in turn. Then at most `MAX_ROOTS_PER_PASS` scheduled ones // in turn: those probed longest ago first, a never-probed one by the // height it was first seen at — a queue, so fresh roots (a peer can feed // them at will) wait behind older ones and cannot starve them. Reported @@ -804,6 +827,7 @@ pub(crate) fn begin_pass( retry: false, }; type Turn = (u32, bool, Txid); + let mut named: Vec<(Turn, &OutgoingView)> = Vec::new(); let mut forced_queue: Vec<((bool, Turn), &OutgoingView)> = Vec::new(); let mut scheduled_queue: Vec<(Turn, bool, &OutgoingView)> = Vec::new(); for view in &roots { @@ -818,7 +842,9 @@ pub(crate) fn begin_pass( schedule.last_probe.is_some(), view.txid, ); - if forced_roots.contains(&view.txid) { + if forced.contains(&view.txid) { + named.push((turn, view)); + } else if forced_roots.contains(&view.txid) { forced_queue.push(((!graph.is_own(&view.txid), turn), view)); } // Without an anchor the pass is confined to forced roots. @@ -827,11 +853,17 @@ pub(crate) fn begin_pass( scheduled_queue.push((turn, first, view)); } } - let forced_due = take_in_turn(forced_queue, MAX_FORCED_ROOTS_PER_PASS); - let taken: HashSet = forced_due.iter().map(|(_, view)| view.txid).collect(); + let named = take_in_turn(named, usize::MAX); + let reached = take_in_turn(forced_queue, MAX_FORCED_ROOTS_PER_PASS); + let forced_due: Vec<&OutgoingView> = named + .into_iter() + .map(|(_, view)| view) + .chain(reached.into_iter().map(|(_, view)| view)) + .collect(); + let taken: HashSet = forced_due.iter().map(|view| view.txid).collect(); let mut due: VecDeque = forced_due .into_iter() - .map(|(_, view)| due_root(view, true)) + .map(|view| due_root(view, true)) .collect(); let mut first: Vec<(Turn, &OutgoingView)> = Vec::new(); let mut rest: Vec<(Turn, &OutgoingView)> = Vec::new(); @@ -1292,8 +1324,9 @@ enum JobDone { own: Vec, }, /// The wallets holding a send that was echoed after its `Uncertain`. - /// `token` names the echo this lookup answers: a send that settled, or a - /// wallet that left or re-registered, since then has withdrawn it. + /// `token` names the echo this lookup answers: a send that settled since + /// then has withdrawn it, and a wallet that left or registered had it + /// made again under a new token. EchoListed { txid: Txid, wallets: Vec, @@ -1422,8 +1455,23 @@ struct WalletEntry { /// tip is its arrival there, and runs the pass the completion could not. /// Replaced by every later completion, so a wallet that really is /// rescanning gets its pass when it reaches the latest tip, not a stale - /// one. - completed_ahead: Option, + /// one. Kept with the time it was handled: it stands for + /// [`COMPLETED_AHEAD_TTL`] only (see there). + completed_ahead: Option<(u32, Instant)>, +} + +impl WalletEntry { + /// Whether a pass that follows the tip ran, runs or is queued for the + /// wallet at `height` or above. + fn anchored_at_or_above(&self, height: u32) -> bool { + [ + self.followed_pass_at, + self.run.as_ref().and_then(|run| run.anchor_at), + self.pending.as_ref().and_then(|pending| pending.anchor_at), + ] + .iter() + .any(|at| at.is_some_and(|at| at >= height)) + } } #[derive(Debug, Clone, Copy)] @@ -1484,10 +1532,13 @@ struct Actor { /// Names each retry series (see [`NoAnswerRetries`]), so a stale timer /// matches no later series. next_retry_series: u64, - /// Echoes whose holder lookup is out, by the token their result carries. - /// A send that settles, or a wallet that leaves or re-registers, withdraws - /// them: an acceptance landing after that must not reach the host. - echo_lookups: HashMap, + /// Echoes whose holder lookup is out, by the token their result carries + /// and the handle that stops a superseded one. A send that settles + /// withdraws its lookup: an acceptance landing after that must not reach + /// the host. A wallet that leaves or registers has every lookup made + /// again (see `drop_wallet`), so the acceptance goes to the wallets that + /// hold the send then. + echo_lookups: HashMap, next_echo_token: u64, } @@ -1653,14 +1704,16 @@ impl Actor { let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; - let arrived = !rewound - && entry - .completed_ahead - .is_some_and(|tip| near_tip(height, tip)); - if rewound || entry.completed_ahead.is_some_and(|tip| height >= tip) || arrived { + let completed = entry + .completed_ahead + .filter(|(_, at)| at.elapsed() <= COMPLETED_AHEAD_TTL) + .map(|(tip, _)| tip); + let arrived = !rewound && completed.is_some_and(|tip| near_tip(height, tip)); + if rewound || arrived || completed.is_none_or(|tip| height >= tip) { entry.completed_ahead = None; } - let catch_up = is_catch_up_step(previous, height) && !arrived; + let gap = is_catch_up_step(previous, height); + let catch_up = gap && !arrived; if catch_up { // The tip moved on, unfollowed: no lower wallet's steps // may run the echo windows now. @@ -1668,39 +1721,21 @@ impl Actor { } else { self.expire_uncertain(height); } - let Some(entry) = self.wallets.get_mut(&wallet_id) else { - return; - }; + if gap { + self.confine_to_forced(wallet_id, height, arrived); + } if catch_up { - // Behind the tip: passes queued or running from before are - // confined to what an `Uncertain` result forced — the scan - // settles most roots, and its events clear their verdicts - // (see `Command::Settled`) — and start no windows. The - // sync's completion runs the pass at the tip. - if let Some(pending) = entry.pending.as_mut() { - pending.anchor_at = None; - // A rewind replays lower heights: never run above them. - pending.height = height; - if pending.forced.is_empty() { - entry.pending = None; - } - } - let Some(run) = entry.run.as_mut() else { - // A rewind stopped the run: start what it carried. - self.start(wallet_id); - return; - }; - run.anchor_at = None; - run.walk.store(false, Ordering::SeqCst); - if let Some(probing) = run.probing.as_mut() { - probing.drop_scheduled(); - } - // Still reading, with nothing forced: nothing it could - // probe — stop its walk over the records now. - if run.probing.is_none() && run.forced.is_empty() { - run.abort.abort(); - self.end_run(wallet_id); - } + return; + } + // The completion that came first may have run this height's + // pass already (a wallet with no step since launch reads its + // own height): no second walk over the records then. + if arrived + && self + .wallets + .get(&wallet_id) + .is_some_and(|entry| entry.anchored_at_or_above(height)) + { return; } self.queue_followed_pass(wallet_id, height); @@ -1737,7 +1772,9 @@ impl Actor { // lookup for one already out, must not publish after this. for txid in &txids { self.uncertain.remove(txid); - self.echo_lookups.remove(txid); + if let Some((_, lookup)) = self.echo_lookups.remove(txid) { + lookup.abort(); + } } forget_reported(&mut self.reported, &wallet_id, |txid| txids.contains(txid)); let events = self.state.settle(wallet_id, &txids); @@ -1802,7 +1839,9 @@ impl Actor { lookup.abort(); } self.reported.clear(); - self.echo_lookups.clear(); + for (_, (_, lookup)) in self.echo_lookups.drain() { + lookup.abort(); + } let events = self.state.forget_all(); deliver(&self.sink, events); } @@ -1818,27 +1857,22 @@ impl Actor { // // A wallet below the tip may also be one whose height step // has not been handled yet: remember the tip for it (see - // `WalletEntry::completed_ahead`). + // `WalletEntry::completed_ahead`). So may one without a step + // since launch: its first step, when it lands at this tip, is + // then its arrival and keeps the pass started here. + let now = Instant::now(); for entry in self.wallets.values_mut() { let below = entry .height - .is_some_and(|height| height < tip && !near_tip(height, tip)); - entry.completed_ahead = below.then_some(tip); + .is_none_or(|height| height < tip && !near_tip(height, tip)); + entry.completed_ahead = below.then_some((tip, now)); } let at_tip: Vec<(WalletId, u32)> = self .wallets .iter() .map(|(wallet_id, entry)| (wallet_id, entry, entry.height.unwrap_or(tip))) .filter(|(_, entry, height)| { - let anchored_at = [ - entry.followed_pass_at, - entry.run.as_ref().and_then(|run| run.anchor_at), - entry.pending.as_ref().and_then(|pending| pending.anchor_at), - ]; - near_tip(*height, tip) - && !anchored_at - .iter() - .any(|at| at.is_some_and(|at| at >= *height)) + near_tip(*height, tip) && !entry.anchored_at_or_above(*height) }) .map(|(wallet_id, _, height)| (*wallet_id, height)) .collect(); @@ -1902,15 +1936,17 @@ impl Actor { fn look_up_echo(&mut self, txid: Txid) { let token = self.next_echo_token; self.next_echo_token += 1; - self.echo_lookups.insert(txid, token); let source = Arc::clone(&self.source); - self.jobs.spawn(async move { + let job = self.jobs.spawn(async move { JobDone::EchoListed { txid, wallets: source.holders(txid).await.own, token, } }); + if let Some((_, superseded)) = self.echo_lookups.insert(txid, (token, job)) { + superseded.abort(); + } } /// The registration a pass read, or was to read, is not in the wallet @@ -2073,7 +2109,9 @@ impl Actor { wallets, token, } => { - if !self.enabled || self.echo_lookups.get(&txid) != Some(&token) { + if !self.enabled + || self.echo_lookups.get(&txid).map(|(current, _)| *current) != Some(token) + { return; } self.echo_lookups.remove(&txid); @@ -2255,10 +2293,8 @@ impl Actor { return; } // A pass of exactly those roots, ahead of the per-pass cap and - // of nothing else, within the forced allowance - // (`MAX_FORCED_ROOTS_PER_PASS`): it takes no scheduled roots - // (they would fail too while no node answers) and starts no - // window. + // of nothing else: it takes no scheduled roots (they would + // fail too while no node answers) and starts no window. let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(height); pending.retried.extend(roots); @@ -2341,6 +2377,53 @@ impl Actor { } } + /// The wallet stepped to `height` over a gap: passes queued or running + /// from before are confined to what an `Uncertain` result forced, and + /// start no windows. The scan settles most roots, and its events clear + /// their verdicts (see `Command::Settled`); a pass at the tip follows — + /// the sync's completion runs it, or this step does when the wallet + /// `arrived` at a tip whose completion came first. On such an arrival a + /// pass already anchored at this tip stays as it is: only one anchored + /// before the gap would start its roots' windows at a stale height. + fn confine_to_forced(&mut self, wallet_id: WalletId, height: u32, arrived: bool) { + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + return; + }; + let stays = |anchor: Option| arrived && anchor.is_some_and(|at| near_tip(at, height)); + if let Some(pending) = entry.pending.as_mut() { + if !stays(pending.anchor_at) { + pending.anchor_at = None; + // A rewind replays lower heights: never run above them. + pending.height = height; + if pending.forced.is_empty() { + entry.pending = None; + } + } + } + let Some(run) = entry.run.as_mut() else { + // A rewind stopped the run: start what it carried. An arrival + // queues its own pass next. + if !arrived { + self.start(wallet_id); + } + return; + }; + if stays(run.anchor_at) { + return; + } + run.anchor_at = None; + run.walk.store(false, Ordering::SeqCst); + if let Some(probing) = run.probing.as_mut() { + probing.drop_scheduled(); + } + // Still reading, with nothing forced: nothing it could probe — stop + // its walk over the records now. + if run.probing.is_none() && run.forced.is_empty() { + run.abort.abort(); + self.end_run(wallet_id); + } + } + /// A height-driven pass of a wallet that follows the tip at `height`: /// queued unless a pass already runs (its end decides) or no root can be /// due yet. @@ -2368,9 +2451,8 @@ impl Actor { /// then twice and four times that again (about 1, 3 and 7 minutes after /// the first probe), at most [`MAX_NO_ANSWER_RETRIES`] per height, while /// no block has come. A retry is a pass of exactly those roots (still - /// roots), ahead of the per-pass cap and within - /// [`MAX_FORCED_ROOTS_PER_PASS`] (the rest wait for the next block): it - /// takes no scheduled roots and starts no window. While DAPI + /// roots), ahead of the per-pass cap: it takes no scheduled roots and + /// starts no window. While DAPI /// stays down, an unanswered root is due once at every block (in its turn /// under [`MAX_ROOTS_PER_PASS`] when a backlog is larger) plus the /// retries, which go ahead of the cap, until the next block comes. One info line per such root and @@ -5124,10 +5206,122 @@ mod tests { let start = begin_pass(&mut state, wallet(), 100, &views, &forced, &forced, None); - assert_eq!(start.due.len(), MAX_FORCED_ROOTS_PER_PASS); + // The reported root itself, then the allowance of the other's parents. + assert_eq!(start.due.len(), 1 + MAX_FORCED_ROOTS_PER_PASS); assert_eq!(start.due.front().map(|root| root.txid), Some(txid(201))); } + /// The allowance is for the roots a forced send's chain reaches, not for + /// a root forced by name: every reported send that is its own root is + /// probed, and a retry repeats every root it names, however many. + #[test] + fn should_probe_every_root_forced_by_name() { + let named = 1..=(MAX_FORCED_ROOTS_PER_PASS as u8 + 4); + let views: Vec = named + .clone() + .map(|n| send(n, &[outpoint(n.wrapping_add(100), 0)])) + .collect(); + let forced: HashSet = named.clone().map(txid).collect(); + let mut state = ResolverState::default(); + + let start = begin_pass(&mut state, wallet(), 100, &views, &forced, &forced, None); + + let due: HashSet = start.due.iter().map(|root| root.txid).collect(); + assert_eq!(due, forced); + assert!(start.due.iter().all(|root| root.forced)); + } + + /// A completion can also overtake a wallet's first step since launch. + /// The completion reads the wallet at the tip and starts its pass; the + /// step that follows is the wallet's arrival there, not a catch-up step + /// that would stop that pass. + #[tokio::test] + async fn should_keep_the_pass_of_a_completion_that_overtook_the_first_step() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 150); + + // Both are queued before the pass's read comes back. + rig.handle(sync_complete(150)); + rig.handle(Command::Height { + wallet_id: wallet(), + height: 150, + }); + rig.settle().await; + + assert_eq!(probe.probed(), [txid(1)], "one pass, not stopped"); + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].window_start, + Some(150) + ); + } + + /// An arrival at a completed tip still confines a pass anchored before + /// the gap: a root that pass had not sent yet is left to the arrival's + /// own pass, so its window starts at the tip, not at the height the + /// wallet left. + #[tokio::test] + async fn should_not_start_windows_before_the_gap_on_an_arrival() { + let probe = Arc::new(ScriptedProbe::new(&[ + (txid(1), unresolved()), + (txid(2), unresolved()), + ])); + let mut rig = enabled(probe.clone()).await; + rig.views( + wallet(), + vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], + ); + rig.height(wallet(), 100).await; + + // A followed step starts a pass. Its read comes back and its first + // probe goes out; then the wallet is suspended, catches up, and the + // completion comes before the step. + rig.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("the read"); + rig.actor.joined(read); + rig.handle(sync_complete(150)); + rig.handle(Command::Height { + wallet_id: wallet(), + height: 150, + }); + rig.settle().await; + + let mut windows: Vec> = [1, 2] + .into_iter() + .map(|n| rig.actor.state.schedules[&(wallet(), txid(n))].window_start) + .collect(); + windows.sort(); + assert_eq!( + windows, + [Some(101), Some(150)], + "the probe already out counts from 101, the other from the tip" + ); + } + + /// A completion whose height step never came is forgotten after a + /// while: a step of a later scan that lands at that old tip is catch-up. + #[tokio::test(start_paused = true)] + async fn should_forget_a_completion_its_step_did_not_follow() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + rig.send(sync_complete(150)).await; + + tokio::time::advance(COMPLETED_AHEAD_TTL + Duration::from_secs(1)).await; + rig.height(wallet(), 150).await; + + assert_eq!(probe.probed().len(), 0, "a scan's step, not an arrival"); + } + /// A report whose send has more ancestor roots than a pass may force /// costs a bounded pass, and a send reported after it is probed at once, /// not behind the rest of those roots. Following the tip then brings diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index 38b5ccce5b9..549670e64dd 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -129,7 +129,9 @@ extension PlatformWalletManager { /// While on, the SDK resubmits to evonodes over DAPI the root of every /// chain of unconfirmed sends — the first unsettled transaction the chain /// depends on, which may be an incoming payment the wallet spent — right - /// after SPV reports a broadcast uncertain (each report), then, while the + /// after SPV reports a broadcast uncertain (each report; when the + /// reported send is built on other unsettled transactions, at most 8 of + /// those chains' roots at once, the rest in their turn), then, while the /// wallet follows the chain tip, every block for 24 blocks from when it /// first did so for that root, and every 10 blocks after — a return from /// the background does not restart the 24 — 55 times at most (about a From 69ab25815303cdb6d5a1065dee5c5d171f51519e Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 14:55:50 +0300 Subject: [PATCH 158/169] fix(platform-wallet): wait for the resolver after the removal is complete The wait sat between the inner removal and the public-map removal, under the generation's gate, so a slow host handler kept a half-removed wallet visible and its teardown waiting. It is now the last step, with the wallet out of both maps and the gate released. The manager tests build their manager through the shared test helper, which takes the event handler and the probe, and follow the "should" naming. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/mod.rs | 40 ++++-------------- .../src/manager/wallet_lifecycle.rs | 41 ++++++++++--------- .../rs-platform-wallet/src/test_support.rs | 29 +++++++++---- 3 files changed, 53 insertions(+), 57 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 58113c1d4fc..4ae854f6e57 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -495,7 +495,7 @@ impl PlatformWalletManager

{ /// [`Self::new`] with the probe the broadcast resolver asks the network /// through. - fn with_acceptance_probe( + pub(crate) fn with_acceptance_probe( sdk: Arc, persister: Arc

, app_handler: Arc, @@ -1511,33 +1511,12 @@ mod tests { async fn probing_manager( probe: Arc, handler: Arc, - ) -> (PlatformWalletManager, WalletId) { - use key_wallet::mnemonic::Mnemonic; - use key_wallet::wallet::initialization::WalletAccountCreationOptions; - - const TEST_MNEMONIC: &str = "abandon abandon abandon abandon abandon abandon \ - abandon abandon abandon abandon abandon about"; - - let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); - let manager = PlatformWalletManager::with_acceptance_probe( - sdk, - Arc::new(NoopPersister), - handler, - probe, - ); - let seed = Mnemonic::from_phrase(TEST_MNEMONIC) - .expect("valid test mnemonic") - .to_seed(""); - let wallet_id = manager - .create_wallet_from_seed_bytes( - key_wallet::Network::Testnet, - &seed, - WalletAccountCreationOptions::Default, - Some(0), - ) - .await - .expect("register the test wallet") - .wallet_id(); + ) -> ( + Arc>, + WalletId, + ) { + let (manager, wallet_id) = + crate::test_support::test_platform_wallet_manager_with(handler, Some(probe)).await; manager.broadcast_resolver.set_enabled(true); (manager, wallet_id) } @@ -1568,7 +1547,7 @@ mod tests { /// forwards the fan-out's copy of the events; without the constructor's /// observer the wallet stays idle and the first wait below fails. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] - async fn constructor_wires_the_lossless_event_channel_into_the_broadcast_resolver() { + async fn should_wire_the_lossless_event_channel_into_the_broadcast_resolver() { let probe = Arc::new(CountingProbe::default()); let (manager, wallet_id) = probing_manager( Arc::clone(&probe) as Arc, @@ -1635,7 +1614,7 @@ mod tests { /// per-wallet generation there) then sees no verdict of the removed /// wallet stamped as a successor's. #[tokio::test(flavor = "multi_thread", worker_threads = 4)] - async fn remove_wallet_waits_for_a_verdict_the_resolver_is_publishing() { + async fn should_finish_a_removal_only_after_a_verdict_the_resolver_is_publishing() { let (release, held) = std::sync::mpsc::channel(); let handler = Arc::new(HeldVerdictHandler { entered: tokio::sync::Notify::new(), @@ -1647,7 +1626,6 @@ mod tests { Arc::clone(&handler) as Arc, ) .await; - let manager = Arc::new(manager); // A send to probe, then two steps that follow the tip: the pass // probes it and publishes its verdict, which the handler holds. diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index ecc51b9078b..0621e82a389 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -927,25 +927,6 @@ impl PlatformWalletManager

{ } } - // The removal above only queued the broadcast resolver's part. Its - // task could be publishing a verdict of this wallet's right now, and - // a host cannot tell one that arrives after this call returned from a - // verdict of a wallet registered under the same id since. Wait for - // the task to get past the removal, outside the wallet-manager lock - // (its reads take that lock). Bounded: a host handler that never - // returns must not hold a removal for ever. - if !self - .broadcast_resolver - .caught_up(RESOLVER_REMOVAL_BUDGET) - .await - { - tracing::warn!( - wallet_id = %hex::encode(wallet_id), - "remove_wallet: the broadcast resolver did not get to the removal in time; \ - a verdict for this wallet may still reach the host" - ); - } - // Test-only rendezvous: the window a concurrent same-id registration can // publish a new generation into. Sits AFTER every id-keyed unregister, // so state a midpoint recreation installs is never torn down by this @@ -1003,6 +984,28 @@ impl PlatformWalletManager

{ // itself, so no payment operation can observe the wallet as live and then // act on it after this returns. tear_down(&removed); + drop(_teardown); + + // The removal only queued the broadcast resolver's part. Its task + // could be publishing a verdict of this wallet's right now, and a + // host cannot tell one that arrives after this call returned from a + // verdict of a wallet registered under the same id since. Wait for + // the task to get past the removal. Last, with the wallet out of both + // maps and the gate released, so nothing else waits with it; and + // outside the wallet-manager lock, which the task's reads take. + // Bounded: a host handler that never returns must not hold a removal + // for ever. + if !self + .broadcast_resolver + .caught_up(RESOLVER_REMOVAL_BUDGET) + .await + { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + "remove_wallet: the broadcast resolver did not get to the removal in time; \ + a verdict for this wallet may still reach the host" + ); + } Ok(removed) } diff --git a/packages/rs-platform-wallet/src/test_support.rs b/packages/rs-platform-wallet/src/test_support.rs index b9446432ee0..d2e38a3d725 100644 --- a/packages/rs-platform-wallet/src/test_support.rs +++ b/packages/rs-platform-wallet/src/test_support.rs @@ -667,6 +667,19 @@ impl crate::events::PlatformEventHandler for NoopTestEventHandler {} pub async fn test_platform_wallet_manager() -> ( Arc>, WalletId, +) { + test_platform_wallet_manager_with(Arc::new(NoopTestEventHandler), None).await +} + +/// [`test_platform_wallet_manager`] with the host's `event_handler` and, +/// when given, the probe the broadcast resolver asks the network through +/// (the DAPI one otherwise). +pub(crate) async fn test_platform_wallet_manager_with( + event_handler: Arc, + acceptance_probe: Option>, +) -> ( + Arc>, + WalletId, ) { use key_wallet::mnemonic::Mnemonic; use key_wallet::wallet::initialization::WalletAccountCreationOptions; @@ -677,13 +690,15 @@ pub async fn test_platform_wallet_manager() -> ( let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); let persister = Arc::new(NoopTestPersister); - let event_handler: Arc = - Arc::new(NoopTestEventHandler); - let manager = Arc::new(crate::PlatformWalletManager::new( - sdk, - persister, - event_handler, - )); + let manager = Arc::new(match acceptance_probe { + Some(probe) => crate::PlatformWalletManager::with_acceptance_probe( + sdk, + persister, + event_handler, + probe, + ), + None => crate::PlatformWalletManager::new(sdk, persister, event_handler), + }); let mnemonic = Mnemonic::from_phrase(TEST_MNEMONIC).expect("valid test mnemonic"); let seed_bytes = mnemonic.to_seed(""); From 1612897fe0a55d707840bb5674a79dea7637a57c Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 15:15:07 +0300 Subject: [PATCH 159/169] fix(platform-wallet): second local review of the resolver fixes - A panicked echo lookup is given up, as a panicked report lookup is; it stayed in the table and every later wallet change made it again. - A wallet's first step landing just below a completion's tip queues the pass at its own height: the completion's pass loses its anchor when its read finds the wallet below it, and the step used to count it as done. - The removal's wait says what it does not cover, in Rust and in Swift. - The Rust and FFI docs of the switch name the forced-root allowance. - Test helpers import what they use; the gate binding is named for the explicit drop. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/manager.rs | 4 +- .../src/manager/accessors.rs | 4 +- .../rs-platform-wallet/src/manager/mod.rs | 15 ++- .../src/manager/wallet_lifecycle.rs | 8 +- .../rs-platform-wallet/src/test_support.rs | 23 ++--- .../src/wallet/core/broadcast_resolver.rs | 95 ++++++++++++++++++- .../PlatformWalletManagerBroadcastProbe.swift | 13 +-- 7 files changed, 126 insertions(+), 36 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 9683258a107..5d4e72827d9 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -2147,7 +2147,9 @@ mod remove_wallet_lifecycle_tests { /// Turn automatic probing of unconfirmed sends on or off (off by default). /// /// While on, the roots of every unconfirmed chain are resubmitted to evonodes -/// over DAPI — right after SPV reports a send uncertain (each report), then, +/// over DAPI — right after SPV reports a send uncertain (each report: the +/// send itself when it is a root, and at most 8 of the roots its chain +/// reaches, the rest in their turn), then, /// while the wallet follows the chain tip, every block for 24 blocks from when /// it first did so for that root, and every 10 blocks after (a return from the /// background does not restart the 24), 55 times at most (about a day of diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index da0972c0de2..b97b5e44dbb 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -526,7 +526,9 @@ impl PlatformWalletManager

{ /// /// When on, the roots of a registered wallet's unconfirmed chains are /// resubmitted to evonodes over DAPI — right after dash-spv reports a send - /// `Uncertain` (each report), then, while the wallet follows the chain tip, + /// `Uncertain` (each report: the send itself when it is a root, and at + /// most 8 of the roots its chain reaches, the rest in their turn), then, + /// while the wallet follows the chain tip, /// every block for 24 blocks from when it first did so for that root, and /// every 10 blocks after, a return from the background included, 55 /// times at most (about a day of following the tip, counted within one diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 4ae854f6e57..ba56ab61e83 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -1155,6 +1155,7 @@ mod tests { ClientStartState, PersistenceError, PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::events::{EventHandler, PlatformEventHandler}; + use crate::test_support::{test_platform_wallet_manager_with, NoopTestPersister}; struct NoopPersister; impl PlatformWalletPersistence for NoopPersister { @@ -1433,7 +1434,7 @@ mod tests { } /// How long a test waits for something another task does. - const TEST_WAIT: std::time::Duration = std::time::Duration::from_secs(10); + const TEST_WAIT: Duration = Duration::from_secs(10); /// Wait for `holds`, asked every few milliseconds, or fail naming `what`. async fn eventually(what: &str, mut holds: F) @@ -1443,7 +1444,7 @@ mod tests { { let waited = tokio::time::timeout(TEST_WAIT, async { while !holds().await { - tokio::time::sleep(std::time::Duration::from_millis(5)).await; + tokio::time::sleep(Duration::from_millis(5)).await; } }) .await; @@ -1511,12 +1512,8 @@ mod tests { async fn probing_manager( probe: Arc, handler: Arc, - ) -> ( - Arc>, - WalletId, - ) { - let (manager, wallet_id) = - crate::test_support::test_platform_wallet_manager_with(handler, Some(probe)).await; + ) -> (Arc>, WalletId) { + let (manager, wallet_id) = test_platform_wallet_manager_with(handler, Some(probe)).await; manager.broadcast_resolver.set_enabled(true); (manager, wallet_id) } @@ -1640,7 +1637,7 @@ mod tests { let manager = Arc::clone(&manager); async move { manager.remove_wallet(&wallet_id).await } }); - tokio::time::sleep(std::time::Duration::from_millis(200)).await; + tokio::time::sleep(Duration::from_millis(200)).await; assert!( !removal.is_finished(), "the removal returned while a verdict of the wallet was still being delivered" diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 0621e82a389..e28f314d0df 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -805,7 +805,7 @@ impl PlatformWalletManager

{ // The validated handle is carried out of the loop: it is both what this // call returns and tears down, and the identity every mutation below is // matched against. - let (removed, _teardown) = loop { + let (removed, teardown) = loop { let candidate = { let wallets = self.wallets.load(); match wallets.get(wallet_id) { @@ -984,7 +984,7 @@ impl PlatformWalletManager

{ // itself, so no payment operation can observe the wallet as live and then // act on it after this returns. tear_down(&removed); - drop(_teardown); + drop(teardown); // The removal only queued the broadcast resolver's part. Its task // could be publishing a verdict of this wallet's right now, and a @@ -994,7 +994,9 @@ impl PlatformWalletManager

{ // maps and the gate released, so nothing else waits with it; and // outside the wallet-manager lock, which the task's reads take. // Bounded: a host handler that never returns must not hold a removal - // for ever. + // for ever. Past the budget the removal still succeeds and a verdict + // of this wallet's may follow it; so may the first verdicts of a + // wallet registered under the same id while this waits. if !self .broadcast_resolver .caught_up(RESOLVER_REMOVAL_BUDGET) diff --git a/packages/rs-platform-wallet/src/test_support.rs b/packages/rs-platform-wallet/src/test_support.rs index d2e38a3d725..de028f22a83 100644 --- a/packages/rs-platform-wallet/src/test_support.rs +++ b/packages/rs-platform-wallet/src/test_support.rs @@ -30,8 +30,11 @@ use key_wallet::{DerivationPath, Wallet}; use key_wallet_manager::WalletManager; use tokio::sync::RwLock; +use crate::broadcast_probe::AcceptanceProbe; #[cfg(test)] use crate::broadcaster::{BroadcastError, TransactionBroadcaster}; +use crate::events::PlatformEventHandler; +use crate::manager::PlatformWalletManager; use crate::wallet::core::WalletGeneration; use crate::wallet::identity::IdentityManager; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; @@ -675,12 +678,9 @@ pub async fn test_platform_wallet_manager() -> ( /// when given, the probe the broadcast resolver asks the network through /// (the DAPI one otherwise). pub(crate) async fn test_platform_wallet_manager_with( - event_handler: Arc, - acceptance_probe: Option>, -) -> ( - Arc>, - WalletId, -) { + event_handler: Arc, + acceptance_probe: Option>, +) -> (Arc>, WalletId) { use key_wallet::mnemonic::Mnemonic; use key_wallet::wallet::initialization::WalletAccountCreationOptions; @@ -691,13 +691,10 @@ pub(crate) async fn test_platform_wallet_manager_with( let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); let persister = Arc::new(NoopTestPersister); let manager = Arc::new(match acceptance_probe { - Some(probe) => crate::PlatformWalletManager::with_acceptance_probe( - sdk, - persister, - event_handler, - probe, - ), - None => crate::PlatformWalletManager::new(sdk, persister, event_handler), + Some(probe) => { + PlatformWalletManager::with_acceptance_probe(sdk, persister, event_handler, probe) + } + None => PlatformWalletManager::new(sdk, persister, event_handler), }); let mnemonic = Mnemonic::from_phrase(TEST_MNEMONIC).expect("valid test mnemonic"); diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 31fa36b1854..3ce99320968 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -665,7 +665,9 @@ pub(crate) fn is_catch_up_step(previous: Option, height: u32) -> bool { /// way, behind another task's queue, so it comes within moments. Without a /// limit, a completion whose step was lost would turn a step of some later /// scan that happens to land at that old tip into an arrival, and start -/// windows there. +/// windows there. A step that takes longer than this is a catch-up step +/// again, and its pass waits for the next completion. Measured on the +/// monotonic clock, which stands still while the device sleeps. const COMPLETED_AHEAD_TTL: Duration = Duration::from_secs(30); /// A wallet at `height` is at a sync's completed `tip`, as far as following @@ -853,7 +855,7 @@ pub(crate) fn begin_pass( scheduled_queue.push((turn, first, view)); } } - let named = take_in_turn(named, usize::MAX); + named.sort_unstable_by_key(|(turn, _)| *turn); let reached = take_in_turn(forced_queue, MAX_FORCED_ROOTS_PER_PASS); let forced_due: Vec<&OutgoingView> = named .into_iter() @@ -1461,6 +1463,17 @@ struct WalletEntry { } impl WalletEntry { + /// Whether a pass that follows the tip ran at `height` or above, or one + /// anchored at exactly `height` runs or is queued. + fn followed_at(&self, height: u32) -> bool { + self.followed_pass_at.is_some_and(|at| at >= height) + || [ + self.run.as_ref().and_then(|run| run.anchor_at), + self.pending.as_ref().and_then(|pending| pending.anchor_at), + ] + .contains(&Some(height)) + } + /// Whether a pass that follows the tip ran, runs or is queued for the /// wallet at `height` or above. fn anchored_at_or_above(&self, height: u32) -> bool { @@ -1655,6 +1668,10 @@ impl Actor { // later wallet change brings it back. self.report_lookups .retain(|_, (_, lookup)| lookup.id() != error.id()); + // So does a panicked echo lookup its echo: a wallet + // change must not make it again. + self.echo_lookups + .retain(|_, (_, lookup)| lookup.id() != error.id()); for entry in self.wallets.values_mut() { entry.wait_until = None; } @@ -1730,11 +1747,15 @@ impl Actor { // The completion that came first may have run this height's // pass already (a wallet with no step since launch reads its // own height): no second walk over the records then. + // Only one anchored at this very height: a completion + // anchors a wallet it has no height for at its own tip, and + // that pass's read drops the anchor when the wallet turns out + // to be a block or two below. if arrived && self .wallets .get(&wallet_id) - .is_some_and(|entry| entry.anchored_at_or_above(height)) + .is_some_and(|entry| entry.followed_at(height)) { return; } @@ -2701,6 +2722,10 @@ impl BroadcastResolver { /// calls the host from itself, so once it has reached this barrier the /// removal is behind it and so is every callback from before. Not to be /// awaited under the wallet-manager lock: the task's reads take it. + /// + /// What it does not cover: a handler that holds the task longer than + /// `budget` (`false`), and a wallet registered under the same id while + /// this waits, whose first verdicts can be delivered before the answer. pub(crate) async fn caught_up(&self, budget: Duration) -> bool { let (reply, answer) = oneshot::channel(); self.send(Command::Barrier(reply)); @@ -3862,6 +3887,8 @@ mod tests { /// Reads wait while set, so a test can hold a pass in its read. paused: StdMutex, resume: Notify, + /// Holder lookups panic while set. + holders_panic: StdMutex, } impl FakeSource { @@ -3905,6 +3932,9 @@ mod tests { #[async_trait] impl WalletSource for FakeSource { async fn holders(&self, txid: Txid) -> Holders { + if *self.holders_panic.lock().expect("holders_panic") { + panic!("a holder lookup panics"); + } let views = self.views.lock().expect("views"); let held = |own: bool| { views @@ -5261,6 +5291,35 @@ mod tests { ); } + /// A completion anchors a wallet it has no height for at its own tip. + /// When that wallet's first step lands a block or two below, the step + /// queues the pass at its own height: the completion's pass loses its + /// anchor once its read finds the wallet below it. + #[tokio::test] + async fn should_run_the_pass_when_the_first_step_lands_just_below_the_completion() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 148); + + rig.handle(sync_complete(150)); + rig.handle(Command::Height { + wallet_id: wallet(), + height: 148, + }); + rig.settle().await; + + assert_eq!(probe.probed(), [txid(1)]); + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].window_start, + Some(148) + ); + } + /// An arrival at a completed tip still confines a pass anchored before /// the gap: a root that pass had not sent yet is left to the arrival's /// own pass, so its window starts at the tip, not at the height the @@ -5805,6 +5864,36 @@ mod tests { ); } + /// An echo whose holder lookup panicked is given up, as a report whose + /// lookup panicked is: a wallet registered later does not have it looked + /// up again and accepted at that unrelated moment. + #[tokio::test] + async fn should_give_up_an_echo_whose_lookup_panicked() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.send(Command::Seen { + wallet_id: wallet(), + touched: false, + }) + .await; + rig.send(Command::Uncertain(txid(1))).await; + rig.sent(); + + *rig.source.holders_panic.lock().expect("holders_panic") = true; + rig.send(Command::Echoed(txid(1))).await; + *rig.source.holders_panic.lock().expect("holders_panic") = false; + assert!(rig.actor.echo_lookups.is_empty()); + + rig.send(Command::WalletAdded([0xEE; 32])).await; + + assert!( + !rig.sent() + .contains(&ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)), + "the given-up echo came back at a wallet change" + ); + } + /// An echo adds nothing to a decided verdict: a send probed Mined stays /// Mined, not turned back into Accepted. #[tokio::test] diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index 549670e64dd..0c16947621b 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -54,9 +54,10 @@ public struct OutgoingTransactionProbeEvent: Sendable, Equatable { /// broadcast-probe callback reads its wallet's generation on the Rust thread /// that delivers it; `deleteWallet` bumps it once the native wallet is gone, /// so a verdict or clear still queued for the main actor is dropped there. -/// The native removal returns only after the resolver's task is past it, so -/// no verdict of the deleted wallet is handed to the callback after the -/// bump and stamped with the new generation. +/// The native removal returns only after the resolver's task is past it +/// (it waits up to five seconds for a handler the task is held in), so no +/// verdict of the deleted wallet is handed to the callback after the bump +/// and stamped with the new generation. /// A wallet restored under the same deterministic id keeps the bumped /// generation: the deleted wallet's queued callbacks stay stale, its own /// are current. For that reason an entry is never removed: one counter stays @@ -129,9 +130,9 @@ extension PlatformWalletManager { /// While on, the SDK resubmits to evonodes over DAPI the root of every /// chain of unconfirmed sends — the first unsettled transaction the chain /// depends on, which may be an incoming payment the wallet spent — right - /// after SPV reports a broadcast uncertain (each report; when the - /// reported send is built on other unsettled transactions, at most 8 of - /// those chains' roots at once, the rest in their turn), then, while the + /// after SPV reports a broadcast uncertain (each report: the send itself + /// when it is a root, and at most 8 of the roots its chain reaches, the + /// rest in their turn), then, while the /// wallet follows the chain tip, every block for 24 blocks from when it /// first did so for that root, and every 10 blocks after — a return from /// the background does not restart the 24 — 55 times at most (about a From a8ffadc953f37802d65281656f6f667a0369925f Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 15:32:29 +0300 Subject: [PATCH 160/169] fix(platform-wallet): third local review of the resolver fixes - An arrival queues its height behind a pass still running instead of taking that pass for the height's own: its read may have found the wallet lower and dropped its anchor. - The roots a forced pass leaves beyond its allowance are stamped as seen by it, so they go ahead of the ones it probed. - Docs: the allowance is per pass and shared by reports handled together; a report reaches a retired root only within it; the removal is complete before its wait. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/manager.rs | 5 ++- .../src/manager/accessors.rs | 5 ++- .../src/manager/wallet_lifecycle.rs | 4 +- .../src/wallet/core/broadcast_resolver.rs | 45 ++++++++----------- .../PlatformWalletManagerBroadcastProbe.swift | 5 ++- 5 files changed, 30 insertions(+), 34 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 5d4e72827d9..413a1998f8d 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -2148,8 +2148,9 @@ mod remove_wallet_lifecycle_tests { /// /// While on, the roots of every unconfirmed chain are resubmitted to evonodes /// over DAPI — right after SPV reports a send uncertain (each report: the -/// send itself when it is a root, and at most 8 of the roots its chain -/// reaches, the rest in their turn), then, +/// send itself when it is a root, and of the roots its chain reaches at +/// most 8 per pass, shared by the reports handled together, the rest in +/// their turn), then, /// while the wallet follows the chain tip, every block for 24 blocks from when /// it first did so for that root, and every 10 blocks after (a return from the /// background does not restart the 24), 55 times at most (about a day of diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index b97b5e44dbb..f12bbca3cf4 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -526,8 +526,9 @@ impl PlatformWalletManager

{ /// /// When on, the roots of a registered wallet's unconfirmed chains are /// resubmitted to evonodes over DAPI — right after dash-spv reports a send - /// `Uncertain` (each report: the send itself when it is a root, and at - /// most 8 of the roots its chain reaches, the rest in their turn), then, + /// `Uncertain` (each report: the send itself when it is a root, and of + /// the roots its chain reaches at most 8 per pass, shared by the reports + /// handled together, the rest in their turn), then, /// while the wallet follows the chain tip, /// every block for 24 blocks from when it first did so for that root, and /// every 10 blocks after, a return from the background included, 55 diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index e28f314d0df..5a2c1516312 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -996,7 +996,9 @@ impl PlatformWalletManager

{ // Bounded: a host handler that never returns must not hold a removal // for ever. Past the budget the removal still succeeds and a verdict // of this wallet's may follow it; so may the first verdicts of a - // wallet registered under the same id while this waits. + // wallet registered under the same id while this waits. The removal + // is complete before this point: a caller that drops this future + // here has removed the wallet without getting it back. if !self .broadcast_resolver .caught_up(RESOLVER_REMOVAL_BUDGET) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 3ce99320968..3d49473ac0b 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -143,7 +143,8 @@ pub(crate) const SLOW_INTERVAL: u32 = 10; /// answered, not in blocks, so the blocks an app slept through do not use it /// up; a forced probe (an `Uncertain` report, a retry) after the window /// counts like a scheduled one. A new `Uncertain` report still probes a -/// retired root, and so does a probe of it that no node answered. Counted +/// retired root (within [`MAX_FORCED_ROOTS_PER_PASS`] when the report only +/// reaches it), and so does a probe of it that no node answered. Counted /// within one session: the schedule lives in memory, so a relaunch, a /// rewind, switching probing off and on, or registering the wallet again /// starts a root's count anew. @@ -834,8 +835,11 @@ pub(crate) fn begin_pass( let mut scheduled_queue: Vec<(Turn, bool, &OutgoingView)> = Vec::new(); for view in &roots { let schedule = state.schedules.entry((wallet_id, view.txid)).or_default(); - if let Some(anchor) = anchor { - schedule.seen_at.get_or_insert(anchor); + // A forced pass sees the roots its chains reach too, so the ones it + // leaves beyond the allowance go ahead of the ones it probed. + let reached = forced_roots.contains(&view.txid); + if let Some(seen) = anchor.or(reached.then_some(height)) { + schedule.seen_at.get_or_insert(seen); } // At the same height, one still waiting for its first probe goes // before one probed there. @@ -846,7 +850,7 @@ pub(crate) fn begin_pass( ); if forced.contains(&view.txid) { named.push((turn, view)); - } else if forced_roots.contains(&view.txid) { + } else if reached { forced_queue.push(((!graph.is_own(&view.txid), turn), view)); } // Without an anchor the pass is confined to forced roots. @@ -856,11 +860,11 @@ pub(crate) fn begin_pass( } } named.sort_unstable_by_key(|(turn, _)| *turn); - let reached = take_in_turn(forced_queue, MAX_FORCED_ROOTS_PER_PASS); + let reached_due = take_in_turn(forced_queue, MAX_FORCED_ROOTS_PER_PASS); let forced_due: Vec<&OutgoingView> = named .into_iter() .map(|(_, view)| view) - .chain(reached.into_iter().map(|(_, view)| view)) + .chain(reached_due.into_iter().map(|(_, view)| view)) .collect(); let taken: HashSet = forced_due.iter().map(|view| view.txid).collect(); let mut due: VecDeque = forced_due @@ -1463,17 +1467,6 @@ struct WalletEntry { } impl WalletEntry { - /// Whether a pass that follows the tip ran at `height` or above, or one - /// anchored at exactly `height` runs or is queued. - fn followed_at(&self, height: u32) -> bool { - self.followed_pass_at.is_some_and(|at| at >= height) - || [ - self.run.as_ref().and_then(|run| run.anchor_at), - self.pending.as_ref().and_then(|pending| pending.anchor_at), - ] - .contains(&Some(height)) - } - /// Whether a pass that follows the tip ran, runs or is queued for the /// wallet at `height` or above. fn anchored_at_or_above(&self, height: u32) -> bool { @@ -1746,16 +1739,16 @@ impl Actor { } // The completion that came first may have run this height's // pass already (a wallet with no step since launch reads its - // own height): no second walk over the records then. - // Only one anchored at this very height: a completion - // anchors a wallet it has no height for at its own tip, and - // that pass's read drops the anchor when the wallet turns out - // to be a block or two below. + // own height): no second walk over the records then. A pass + // still running is not taken for that: its read may have + // found the wallet lower and dropped its anchor, so the + // height is queued behind it, and dropped at its end if no + // root is due. if arrived && self .wallets .get(&wallet_id) - .is_some_and(|entry| entry.followed_at(height)) + .is_some_and(|entry| entry.followed_pass_at.is_some_and(|at| at >= height)) { return; } @@ -5400,12 +5393,10 @@ mod tests { rig.send(Command::Uncertain(txid(201))).await; assert_eq!(probe.probed()[oversized.len()..], [txid(201)]); - // The roots the forced pass reached were probed longer ago than the - // others were first seen, so they take the first followed pass; the - // twelve left over fill the next two. + // The twelve the forced pass left go first: eight in the first + // followed pass, the rest in the next. rig.follow(wallet(), 100).await; rig.height(wallet(), 102).await; - rig.height(wallet(), 103).await; let probed: HashSet = probe.probed().into_iter().collect(); let missed: Vec = (10..30) .filter(|parent| !probed.contains(&txid(*parent))) diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index 0c16947621b..3a2e04be592 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -131,8 +131,9 @@ extension PlatformWalletManager { /// chain of unconfirmed sends — the first unsettled transaction the chain /// depends on, which may be an incoming payment the wallet spent — right /// after SPV reports a broadcast uncertain (each report: the send itself - /// when it is a root, and at most 8 of the roots its chain reaches, the - /// rest in their turn), then, while the + /// when it is a root, and of the roots its chain reaches at most 8 per + /// pass, shared by the reports handled together, the rest in their + /// turn), then, while the /// wallet follows the chain tip, every block for 24 blocks from when it /// first did so for that root, and every 10 blocks after — a return from /// the background does not restart the 24 — 55 times at most (about a From c23604d796a221e5d0726af3af4a5a8a88a05ff9 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 16:50:48 +0300 Subject: [PATCH 161/169] fix(platform-wallet): recover a lost height step and a late echo after a quiet settle - A completion reads a wallet it has a lower height for, when that wallet has no pass of its own: the read runs the pass if the wallet is at the tip, so the pass no longer depends on a height step that the fan-out may have dropped. - A pass's read that found a send settled, with no event naming it, left an echo lookup from before valid; its acceptance then followed the clear. Echo lookups are made again after every read. - A removal that finds the wallet already gone waits for the resolver too: another removal of that id may still be in that wait. - Tests: the stale-acceptance test now holds a completed lookup result across the settlement, and the removal test waits for the removal's last mutation instead of sleeping. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/mod.rs | 14 ++- .../src/manager/wallet_lifecycle.rs | 25 +++-- .../src/wallet/core/broadcast_resolver.rs | 91 +++++++++++++++++-- 3 files changed, 109 insertions(+), 21 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index ba56ab61e83..7490a9b7fd6 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -1633,13 +1633,21 @@ mod tests { .await .expect("the send's verdict is published"); - let removal = tokio::spawn({ + let mut removal = tokio::spawn({ let manager = Arc::clone(&manager); async move { manager.remove_wallet(&wallet_id).await } }); - tokio::time::sleep(Duration::from_millis(200)).await; + // The removal has done all it changes (the wallet is out of the + // public map, its last mutation) and still does not return: what is + // left of it is the wait for the resolver's task. + eventually("the wallet leaves the manager", || async { + manager.get_wallet(&wallet_id).await.is_none() + }) + .await; assert!( - !removal.is_finished(), + tokio::time::timeout(Duration::from_millis(200), &mut removal) + .await + .is_err(), "the removal returned while a verdict of the wallet was still being delivered" ); diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 5a2c1516312..3c3cf71231c 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -806,14 +806,13 @@ impl PlatformWalletManager

{ // call returns and tears down, and the identity every mutation below is // matched against. let (removed, teardown) = loop { - let candidate = { - let wallets = self.wallets.load(); - match wallets.get(wallet_id) { - None => { - return Err(PlatformWalletError::WalletNotFound(hex::encode(wallet_id))) - } - Some(wallet) => Arc::clone(wallet), - } + let registered = self.wallets.load().get(wallet_id).cloned(); + let Some(candidate) = registered else { + // Another removal of this id may be in its last step, + // waiting for the resolver: a caller told "not found" takes + // the wallet for gone, so this return keeps the same promise. + self.wait_for_resolver(wallet_id).await; + return Err(PlatformWalletError::WalletNotFound(hex::encode(wallet_id))); }; let guard = candidate.generation().teardown_guard().await; let still_current = { @@ -999,6 +998,14 @@ impl PlatformWalletManager

{ // wallet registered under the same id while this waits. The removal // is complete before this point: a caller that drops this future // here has removed the wallet without getting it back. + self.wait_for_resolver(wallet_id).await; + + Ok(removed) + } + + /// Wait, for at most [`RESOLVER_REMOVAL_BUDGET`], until the broadcast + /// resolver's task is past everything sent to it so far. + async fn wait_for_resolver(&self, wallet_id: &WalletId) { if !self .broadcast_resolver .caught_up(RESOLVER_REMOVAL_BUDGET) @@ -1010,8 +1017,6 @@ impl PlatformWalletManager

{ a verdict for this wallet may still reach the host" ); } - - Ok(removed) } } diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 3d49473ac0b..8ed9160e150 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -54,7 +54,10 @@ //! completion can be handled before the wallet's own step to that tip (the //! two come from separate tasks): that step, when it follows within //! [`COMPLETED_AHEAD_TTL`], is the wallet's arrival at the tip and runs the -//! pass, however large it is. A +//! pass, however large it is. The step may also never come (the fan-out +//! can drop one), so a completion reads a wallet it has a lower height for, +//! when that wallet has no pass of its own: the read runs the pass if the +//! wallet is at the tip and confines it to forced roots if not. A //! root is probed every block for [`EVERY_BLOCK_WINDOW`] blocks, counted //! from its first probe by a height-driven pass while the wallet follows the //! tip (a forced pass does not start it), and every [`SLOW_INTERVAL`] blocks @@ -1893,6 +1896,36 @@ impl Actor { for (wallet_id, height) in at_tip { self.queue_followed_pass(wallet_id, height); } + // The height the actor has for a wallet below the tip may be + // old because its step was lost, not late (the fan-out can + // drop one). Ask the wallet itself: a pass anchored at the + // tip, which its read confines to forced roots when the + // wallet really is below (see `JobDone::Read`). Only for a + // wallet with no pass of its own, and not an idle one. + let enabled = self.enabled; + let below: Vec = self + .wallets + .iter() + .filter(|(_, entry)| { + enabled + && entry.completed_ahead.is_some() + && entry.height.is_some() + && entry.run.is_none() + && entry.pending.is_none() + && !entry.wait_until.is_some_and(|until| tip < until) + }) + .map(|(wallet_id, _)| *wallet_id) + .collect(); + for wallet_id in below { + if let Some(entry) = self.wallets.get_mut(&wallet_id) { + entry.pending = Some(PendingPass { + height: entry.height.unwrap_or(0), + anchor_at: Some(tip), + ..PendingPass::default() + }); + } + self.start(wallet_id); + } } Command::WalletAdded(wallet_id) => { // A fresh start: whatever was kept under this id (a same-id @@ -2266,6 +2299,15 @@ impl Actor { // the network. deliver(&self.sink, start.events); self.probe_next(wallet_id); + // This read may have found a send settled or gone + // that no `Settled` named (the lossless channel only + // says a wallet's records changed). An echo lookup + // from before would still name this wallet: look + // again, as after a wallet change. + let echoes: Vec = self.echo_lookups.keys().copied().collect(); + for txid in echoes { + self.look_up_echo(txid); + } } } } @@ -4748,16 +4790,14 @@ mod tests { .lock() .expect("synced") .insert(wallet(), 150); + // The completion asks the wallet itself, whatever height the actor + // has for it: the pass runs even if that step never comes. rig.send(sync_complete(150)).await; - assert_eq!( - probe.probed().len(), - 0, - "the actor still has the wallet at 100" - ); + assert_eq!(probe.probed().len(), 1); rig.height(wallet(), 150).await; - assert_eq!(probe.probed().len(), 1); + assert_eq!(probe.probed().len(), 1, "the step adds no second probe"); assert_eq!( rig.actor.state.schedules[&(wallet(), txid(1))].window_start, Some(150) @@ -5924,17 +5964,52 @@ mod tests { rig.send(Command::Uncertain(txid(1))).await; rig.sent(); - // The echo's lookup is out, then the send settles before it lands. + // The echo's lookup finds the wallet holding the send; the send + // settles before that result is handled. rig.handle(Command::Echoed(txid(1))); + let landed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + assert!(matches!( + &landed, + Ok((_, JobDone::EchoListed { wallets, .. })) if wallets == &[wallet()] + )); rig.handle(Command::Settled { wallet_id: wallet(), txids: HashSet::from([txid(1)]), }); + rig.actor.joined(landed); rig.settle().await; assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); } + /// The same when no event named the settlement and a pass's read found + /// it: the lookup from before that read must not accept the send. + #[tokio::test] + async fn should_drop_an_acceptance_that_lands_after_a_read_found_the_send_settled() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + rig.send(Command::Uncertain(txid(1))).await; + rig.sent(); + + rig.handle(Command::Echoed(txid(1))); + let landed = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + assert!(matches!( + &landed, + Ok((_, JobDone::EchoListed { wallets, .. })) if wallets == &[wallet()] + )); + // The send settles; only the next pass's read learns it. + rig.views(wallet(), Vec::new()); + rig.height(wallet(), 102).await; + assert_eq!(rig.sent(), vec![ResolverEvent::Cleared(txid(1))]); + + rig.actor.joined(landed); + rig.settle().await; + + assert!(rig.sent().is_empty(), "accepted after it was cleared"); + } + /// An echo's acceptance survives another wallet's registration or removal /// while its lookup is out: the lookup is made again, not dropped. #[tokio::test] From 0c96e225eb5e7fd9fa43dbfe9ef0622963f98a0b Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 17:11:38 +0300 Subject: [PATCH 162/169] fix(platform-wallet): take the wallet's height from the pass a completion ran Local review of the lost-step recovery: - The pass a completion runs for a wallet whose step was lost left the actor's height for it stale, so its no-answer retries were dropped as belonging to another height and the next real step counted as a catch-up. A pass that follows the tip and finds the wallet there now sets the height. - The wallet is asked once per completed tip, and behind a pass under way instead of not at all. - Echo lookups are made again only for the sends a read no longer finds as the wallet's own unsettled ones, and also when a registration turns out gone. Co-Authored-By: Claude Opus 5.5 --- .../src/manager/wallet_lifecycle.rs | 6 +- .../src/wallet/core/broadcast_resolver.rs | 159 +++++++++++++----- 2 files changed, 121 insertions(+), 44 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 3c3cf71231c..d45ea77819c 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -811,6 +811,8 @@ impl PlatformWalletManager

{ // Another removal of this id may be in its last step, // waiting for the resolver: a caller told "not found" takes // the wallet for gone, so this return keeps the same promise. + // An id that was never registered waits too, only while a + // host handler holds the resolver's task. self.wait_for_resolver(wallet_id).await; return Err(PlatformWalletError::WalletNotFound(hex::encode(wallet_id))); }; @@ -1013,8 +1015,8 @@ impl PlatformWalletManager

{ { tracing::warn!( wallet_id = %hex::encode(wallet_id), - "remove_wallet: the broadcast resolver did not get to the removal in time; \ - a verdict for this wallet may still reach the host" + "remove_wallet: the broadcast resolver's task was still busy after the wait; \ + if this wallet was registered, a verdict for it may still reach the host" ); } } diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 8ed9160e150..373ef67e836 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -1467,6 +1467,10 @@ struct WalletEntry { /// one. Kept with the time it was handled: it stands for /// [`COMPLETED_AHEAD_TTL`] only (see there). completed_ahead: Option<(u32, Instant)>, + /// The completed tip the wallet was last asked its own height for (see + /// `Command::SyncComplete`): one read per tip, however often it + /// completes. + asked_at: Option, } impl WalletEntry { @@ -1866,8 +1870,9 @@ impl Actor { Command::SyncComplete { tip } => { // Every wallet at the tip follows it now: a launch's or a // reconnect's catch-up skipped its pass, and the next block - // can be minutes away. A wallet still far below (rescanning) - // waits: its windows must not start at a stale height. One + // can be minutes away. A wallet the actor has far below is + // asked where it is (below): its windows must not start at a + // stale height. One // without a step since launch reads its own height. While // probing is off this does nothing (queue_followed_pass): the // next completion, after the next block, covers it. @@ -1878,11 +1883,23 @@ impl Actor { // since launch: its first step, when it lands at this tip, is // then its arrival and keeps the pass started here. let now = Instant::now(); - for entry in self.wallets.values_mut() { + let enabled = self.enabled; + let mut below_tip: Vec = Vec::new(); + for (wallet_id, entry) in self.wallets.iter_mut() { let below = entry .height .is_none_or(|height| height < tip && !near_tip(height, tip)); entry.completed_ahead = below.then_some((tip, now)); + // Asked once per tip, and not while idle. + if below + && enabled + && entry.height.is_some() + && entry.asked_at != Some(tip) + && !entry.wait_until.is_some_and(|until| tip < until) + { + entry.asked_at = Some(tip); + below_tip.push(*wallet_id); + } } let at_tip: Vec<(WalletId, u32)> = self .wallets @@ -1900,30 +1917,17 @@ impl Actor { // old because its step was lost, not late (the fan-out can // drop one). Ask the wallet itself: a pass anchored at the // tip, which its read confines to forced roots when the - // wallet really is below (see `JobDone::Read`). Only for a - // wallet with no pass of its own, and not an idle one. - let enabled = self.enabled; - let below: Vec = self - .wallets - .iter() - .filter(|(_, entry)| { - enabled - && entry.completed_ahead.is_some() - && entry.height.is_some() - && entry.run.is_none() - && entry.pending.is_none() - && !entry.wait_until.is_some_and(|until| tip < until) - }) - .map(|(wallet_id, _)| *wallet_id) - .collect(); - for wallet_id in below { - if let Some(entry) = self.wallets.get_mut(&wallet_id) { - entry.pending = Some(PendingPass { - height: entry.height.unwrap_or(0), - anchor_at: Some(tip), - ..PendingPass::default() - }); - } + // wallet really is below, and whose height the actor takes + // when it is not (see `JobDone::Read`). Behind a pass under + // way; a pass already queued as following keeps its anchor. + for wallet_id in below_tip { + let Some(entry) = self.wallets.get_mut(&wallet_id) else { + continue; + }; + let known = entry.height.unwrap_or(tip); + let pending = entry.pending.get_or_insert_with(PendingPass::default); + pending.height = pending.height.max(known); + pending.anchor_at.get_or_insert(tip); self.start(wallet_id); } } @@ -2011,9 +2015,25 @@ impl Actor { entry.wait_until = Some(u32::MAX); let events = self.forget_wallet(&wallet_id, "gone"); deliver(&self.sink, events); + // An echo lookup from before may still name this registration. + self.relook_echoes(|_| true); self.end_run(wallet_id); } + /// Make again the echo lookups still out whose txid `affected` names: + /// what they found may no longer hold. + fn relook_echoes(&mut self, affected: impl Fn(&Txid) -> bool) { + let echoes: Vec = self + .echo_lookups + .keys() + .filter(|txid| affected(txid)) + .copied() + .collect(); + for txid in echoes { + self.look_up_echo(txid); + } + } + /// Drop the wallet's entry, stop its pass and forget its state, the host /// told to clear what it showed (logged with `trigger`). Report and echo /// lookups still out are made again, and the wallet leaves `reported`. @@ -2025,10 +2045,7 @@ impl Actor { // one replaces: look again, so the acceptance reaches the wallets // that hold the send now. The echo was already taken out of // `uncertain`, so dropping its lookup would lose it for the others. - let echoes: Vec = self.echo_lookups.keys().copied().collect(); - for txid in echoes { - self.look_up_echo(txid); - } + self.relook_echoes(|_| true); // A report's lookup may name the wallet gone: look again, so the // other holders still get their forced probe and priority. let reports: Vec = self.report_lookups.keys().copied().collect(); @@ -2215,6 +2232,21 @@ impl Actor { // A request queued before any height was reported // carries 0; the wallet's own synced height is better. let height = height.max(read.synced_height); + // A pass that follows the tip found the wallet there: + // that is the wallet's height, also when the step to + // it never reached the actor. Retries and the next + // step are judged against it. + let synced = read.synced_height; + let follows = entry + .run + .as_ref() + .and_then(|run| run.anchor_at) + .is_some_and(|at| { + synced >= at && synced <= at.saturating_add(CATCH_UP_STEP) + }); + if follows && entry.height.is_none_or(|known| known < synced) { + entry.height = Some(synced); + } let Some(current) = entry.run.as_mut() else { return; // matched by id above }; @@ -2261,6 +2293,11 @@ impl Actor { forget_reported(&mut self.reported, &wallet_id, |txid| { !present.contains(txid) }); + let own_present: HashSet = views + .iter() + .filter(|view| view.spends_own_coins) + .map(|view| view.txid) + .collect(); let priority: HashSet = self .reported .iter() @@ -2302,12 +2339,9 @@ impl Actor { // This read may have found a send settled or gone // that no `Settled` named (the lossless channel only // says a wallet's records changed). An echo lookup - // from before would still name this wallet: look - // again, as after a wallet change. - let echoes: Vec = self.echo_lookups.keys().copied().collect(); - for txid in echoes { - self.look_up_echo(txid); - } + // from before could still name this wallet for such + // a send: look those up again. + self.relook_echoes(|txid| !own_present.contains(txid)); } } } @@ -4365,8 +4399,9 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1)]); } - /// A wallet still rescanning far below the tip gets no pass from a - /// completion: its windows must not start at a stale height. + /// A wallet still rescanning far below the tip gets no probe from a + /// completion: it is read once for that tip, found below, and its + /// windows do not start at a stale height. #[tokio::test] async fn should_not_probe_a_wallet_far_below_a_completed_tip() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); @@ -4773,9 +4808,9 @@ mod tests { } /// Sync events and wallet events come from separate tasks: a completion - /// can be handled before the height step it follows. The step that then - /// brings the wallet to that tip runs the pass the completion could not, - /// instead of waiting for the next block as a catch-up step does. + /// can be handled before the height step it follows, and that step can + /// be lost. The completion asks the wallet where it is and runs the pass + /// at the tip; the step, when it comes, adds nothing. #[tokio::test] async fn should_probe_at_the_tip_when_the_completion_overtook_the_height_step() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); @@ -5294,6 +5329,46 @@ mod tests { assert!(start.due.iter().all(|root| root.forced)); } + /// The pass a completion ran for a wallet whose step was lost leaves the + /// actor with the wallet's real height: a probe no node answered there + /// is retried, not dropped as belonging to another height. + #[tokio::test(start_paused = true)] + async fn should_retry_at_the_height_a_completion_found_the_wallet_at() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unreachable_verdict())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 150); + + rig.send(sync_complete(150)).await; + + assert_eq!(rig.actor.wallets[&wallet()].height, Some(150)); + assert_eq!(probe.probed().len(), 1 + MAX_NO_ANSWER_RETRIES as usize); + } + + /// A wallet the actor has below a completed tip is read once for that + /// tip, however often the sync completes there. + #[tokio::test] + async fn should_ask_a_wallet_below_the_tip_once_per_completed_tip() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + + rig.send(sync_complete(150)).await; + let asked = rig.walks(); + rig.send(sync_complete(150)).await; + assert_eq!(rig.walks(), asked, "a second read for the same tip"); + + rig.send(sync_complete(151)).await; + assert_eq!(rig.walks(), asked + 1); + assert_eq!(probe.probed().len(), 0, "the wallet is still below"); + } + /// A completion can also overtake a wallet's first step since launch. /// The completion reads the wallet at the tip and starts its pass; the /// step that follows is the wallet's arrival there, not a catch-up step From bba5408f8610c896558ac10cd747ee7d7b8b180f Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 18:10:10 +0300 Subject: [PATCH 163/169] refactor(platform-wallet)!: drop the Mined probe verdict No host could see it: the FFI delivered Accepted and Mined as one code, Swift has only accepted and unresolved, and the schedule does not read the verdict. Telling them apart cost up to four getTransaction lookups per probe while nodes had the block and the wallet did not, and a repeated ACCEPTED callback on each move between the two. Core's -27 ("already in block chain") is Accepted now. MINED_QUORUM, the second-opinion lookups and the in-block flag of a lookup answer are gone; lookups remain for the refusal case only, where they find a mined transaction whose outputs are all spent. Co-Authored-By: Claude Opus 5.5 --- .../src/event_handler.rs | 6 +- .../rs-platform-wallet/src/broadcast_probe.rs | 235 +++++------------- packages/rs-platform-wallet/src/events.rs | 9 +- .../src/wallet/core/broadcast_resolver.rs | 142 ++++++----- 4 files changed, 148 insertions(+), 244 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/event_handler.rs b/packages/rs-platform-wallet-ffi/src/event_handler.rs index bf65bdbb1de..672a0a6d08d 100644 --- a/packages/rs-platform-wallet-ffi/src/event_handler.rs +++ b/packages/rs-platform-wallet-ffi/src/event_handler.rs @@ -53,9 +53,7 @@ pub type DpnsMarketplaceSyncCompletedFn = unsafe extern "C" fn( ); /// Verdict codes for [`OutgoingTransactionProbedFn`]. -/// A node holds the send in its mempool, or nodes have it in a block: the -/// resolver's Accepted and Mined both arrive as this code, so a move between -/// them (a later probe may disagree with an earlier one) delivers it again. +/// A node holds the send, in its mempool or in a block. pub const OUTGOING_PROBE_VERDICT_ACCEPTED: u8 = 0; /// Reserved; not emitted by this version. pub const OUTGOING_PROBE_VERDICT_DEAD: u8 = 1; @@ -324,7 +322,7 @@ impl PlatformEventHandler for FFIEventHandler { let (code, reason) = match verdict { // The host does not need to tell a mempool from a block: both mean // the payment is going through. - ProbeVerdict::Accepted | ProbeVerdict::Mined => (OUTGOING_PROBE_VERDICT_ACCEPTED, None), + ProbeVerdict::Accepted => (OUTGOING_PROBE_VERDICT_ACCEPTED, None), ProbeVerdict::Unresolved { reason } => { (OUTGOING_PROBE_VERDICT_UNRESOLVED, Some(reason)) } diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index 66d7de49cb8..204463d2f3c 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -17,8 +17,8 @@ //! | spends an input that does not exist | `-25 bad-txns-inputs-missingorspent` | //! | spends an input an IS-locked tx already spent | `-26 tx-txlock-conflict` | //! -//! The probe uses the positive answers: a node holding the transaction makes -//! it accepted, and nodes having it in a block make it mined. A refusal never +//! The probe uses the positive answers: a node holding the transaction, in its +//! mempool or in a block, makes it accepted. A refusal never //! settles anything. Proving that a transaction can never land would need the //! finality of whatever spent its input, which these answers do not show — a //! node lagging behind a parent's block, an ancestor still propagating, or a @@ -30,16 +30,11 @@ //! `-25`), and the lookup is what finds it. Otherwise the transaction stays //! unresolved, with the refusal in the reason. //! -//! Mined needs two distinct nodes reporting the transaction in a block -//! ([`MINED_QUORUM`]); one such report makes it accepted — it exists — and the -//! second opinion is asked by lookup, not by resubmitting. On a network with -//! a single reachable evonode (a devnet pinned to one address) nothing is -//! ever mined by probe, only accepted; the wallet's own block processing -//! settles it. Each probe judges on its own: there is no adding up of nodes -//! across probes. Mined is the nodes' word, not settlement — the resolver -//! keeps probing a mined send like any other until the wallet settles it, and -//! a later probe may say only accepted (a block a reorg dropped, or one the -//! chain the wallet follows never had). +//! The probe does not tell "in a mempool" from "in a block": no host and no +//! schedule reads the difference, and a node's word on a block is not +//! settlement — the block may be on a stale fork or about to be reorged out. +//! The wallet's own block processing settles a send; until then the resolver +//! keeps probing an accepted one like any other. //! //! The two DAPI implementations surface those codes differently — the JS //! server maps `-25` to `InvalidArgument`, rs-dapi to `FailedPrecondition` — @@ -101,11 +96,9 @@ const TRANSPORT_MARKERS: &[&str] = &[ /// What a single Core node said when handed a signed transaction. #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum NodeVerdict { - /// The node holds the transaction in its mempool — it accepted it now or - /// already had it. + /// The node holds the transaction: it accepted it now, already had it in + /// its mempool, or has it in a block (`-27`). Accepted, - /// The node has the transaction in a block (`-27`). - Mined, /// The node refused it for its inputs ([`REFUSAL_REASONS`]). Refused { reason: String }, /// The node answered, but with no verdict: another rejection (such as @@ -139,8 +132,9 @@ pub(crate) fn classify_failed_submission(code: Code, message: &str) -> NodeVerdi reason: format!("{code:?}: {message}"), }; } + // `-27`: already in a block, by this node's word. if code == Code::AlreadyExists { - return NodeVerdict::Mined; + return NodeVerdict::Accepted; } if REFUSAL_REASONS .iter() @@ -158,14 +152,11 @@ pub(crate) fn classify_failed_submission(code: Code, message: &str) -> NodeVerdi /// The answer a probe gives about one transaction. #[derive(Debug, Clone, PartialEq, Eq)] pub enum ProbeVerdict { - /// A node holds the transaction in its mempool, or one node — short of - /// [`MINED_QUORUM`] — has it in a block. Not settlement: it can still - /// expire or lose to a conflict, so the resolver keeps asking. + /// A node holds the transaction, in its mempool or in a block. Not + /// settlement: it can still expire, lose to a conflict or sit in a block + /// a reorg drops, so the resolver keeps asking until the wallet settles + /// the send. Accepted, - /// Two different nodes have the transaction in a block, within one probe. - /// Advisory, like Accepted: the resolver keeps asking until the wallet - /// settles the send, and a later probe may turn it back into Accepted. - Mined, /// Not enough evidence that it landed; the transaction stays ambiguous. /// The reason says what the nodes answered, refusals included. Unresolved { reason: String }, @@ -174,8 +165,8 @@ pub enum ProbeVerdict { /// Whether one node knows a txid (DAPI `getTransaction`). #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum LookupAnswer { - /// The node has the transaction; `mined` when it is in a block. - Known { mined: bool }, + /// The node has the transaction, in its mempool or in a block. + Known, /// The node answered and does not have it. NotFound, /// No answer: transport failure, timeout, anything else. @@ -194,8 +185,7 @@ pub(crate) trait NodeSubmitter: Send + Sync { /// How many submissions one probe may spend looking for a verdict. const MAX_SUBMISSIONS_PER_PROBE: usize = 4; -/// How many txid lookups one probe may spend: after refusals, or to get a -/// second opinion on one node's "in a block". +/// How many txid lookups one probe may spend after refusals. const MAX_LOOKUPS_PER_PROBE: usize = 4; /// How many *distinct* nodes must refuse a transaction for its inputs before @@ -204,13 +194,6 @@ const MAX_LOOKUPS_PER_PROBE: usize = 4; /// outputs are all spent. const REFUSAL_QUORUM: usize = 2; -/// How many *distinct* nodes must report a transaction in a block before it -/// counts as mined, within one probe. One node on a stale fork, or one whose -/// block is about to be reorged out, would otherwise report it mined on false -/// evidence. A single such answer still shows the transaction exists: it -/// counts as accepted. -pub(crate) const MINED_QUORUM: usize = 2; - /// Resubmits a transaction whose broadcast outcome is unknown and turns the /// nodes' answers into a verdict. /// @@ -246,12 +229,11 @@ impl ProbeOutcome { } } -/// Evidence rules shared by every probe: a node holding the transaction in its -/// mempool makes it accepted; one reporting it in a block sends the probe to -/// lookups for a second opinion ([`MINED_QUORUM`] distinct nodes make it -/// mined); [`REFUSAL_QUORUM`] distinct nodes refusing it for its inputs send -/// the probe to lookups too, which find a mined transaction whose outputs are -/// all spent and otherwise leave it unresolved; give up after +/// Evidence rules shared by every probe: a node holding the transaction, in +/// its mempool or in a block, makes it accepted; [`REFUSAL_QUORUM`] distinct +/// nodes refusing it for its inputs send the probe to lookups, which find a +/// mined transaction whose outputs are all spent and otherwise leave it +/// unresolved; give up after /// [`MAX_SUBMISSIONS_PER_PROBE`] submissions and [`MAX_LOOKUPS_PER_PROBE`] /// lookups. pub(crate) async fn probe_with( @@ -274,18 +256,6 @@ pub(crate) async fn probe_with( ); match verdict { NodeVerdict::Accepted => return ProbeOutcome::answered(ProbeVerdict::Accepted), - NodeVerdict::Mined => { - // In a block by this node's word. A second opinion comes from - // a lookup, not from sending it again. - return ProbeOutcome::answered( - confirm_by_lookup( - submitter, - transaction, - LookupFor::SecondOpinion { seen_by: node }, - ) - .await, - ); - } NodeVerdict::Refused { reason } => { // An unidentified node cannot count towards the quorum — it // might be the node we already heard. @@ -296,8 +266,7 @@ pub(crate) async fn probe_with( if refusing_nodes.len() >= REFUSAL_QUORUM { let reason = first.clone(); return ProbeOutcome::answered( - confirm_by_lookup(submitter, transaction, LookupFor::Refused { reason }) - .await, + look_up_refused(submitter, transaction, reason).await, ); } } @@ -324,54 +293,17 @@ pub(crate) async fn probe_with( ProbeOutcome { verdict, answered } } -/// Why a probe turns to lookups. -enum LookupFor { - /// A submission answered "already in a block" (from `seen_by`): a second - /// node is asked. - SecondOpinion { seen_by: Option }, - /// [`REFUSAL_QUORUM`] nodes refused it as `reason`: is it a mined - /// transaction whose outputs are all spent? - Refused { reason: String }, -} - -/// Nodes that reported the transaction in a block, by submission or lookup. -#[derive(Default)] -struct MinedEvidence { - nodes: HashSet, - /// Any node said so, identified or not: the transaction exists. - seen: bool, -} - -impl MinedEvidence { - /// Count one report; whether the quorum is reached. An unidentified node - /// cannot count — it might be one already heard. - fn add(&mut self, node: Option) -> bool { - self.seen = true; - if let Some(node) = node { - self.nodes.insert(node); - } - self.nodes.len() >= MINED_QUORUM - } -} - -/// Settle by `getTransaction` lookups. A node that knows the txid in its -/// mempool makes it accepted; in a block, counts towards [`MINED_QUORUM`]. -/// After refusals, nothing better than unresolved comes of nodes not knowing -/// it. -async fn confirm_by_lookup( +/// [`REFUSAL_QUORUM`] nodes refused the transaction as `reason`: ask by +/// `getTransaction` whether the txid is known at all. A node that has it — a +/// mined transaction whose outputs are all spent is refused the same way — +/// makes it accepted; nothing better than unresolved comes of nodes not +/// knowing it. +async fn look_up_refused( submitter: &dyn NodeSubmitter, transaction: &Transaction, - purpose: LookupFor, + reason: String, ) -> ProbeVerdict { let txid = &transaction.txid(); - let mut mined = MinedEvidence::default(); - let refused = match purpose { - LookupFor::SecondOpinion { seen_by } => { - mined.add(seen_by); - None - } - LookupFor::Refused { reason } => Some(reason), - }; let mut not_found: HashSet = HashSet::new(); let mut last_unknown = String::from("no lookup was answered"); for _ in 0..MAX_LOOKUPS_PER_PROBE { @@ -383,29 +315,20 @@ async fn confirm_by_lookup( "broadcast probe: txid lookup answered" ); match answer { - LookupAnswer::Known { mined: true } => { - if mined.add(node) { - return ProbeVerdict::Mined; - } - } - LookupAnswer::Known { mined: false } => return ProbeVerdict::Accepted, + LookupAnswer::Known => return ProbeVerdict::Accepted, LookupAnswer::NotFound => { if let Some(node) = node { not_found.insert(node); } - // Nobody has it in a block and enough nodes say so: asking on - // learns nothing more. - if refused.is_some() && !mined.seen && not_found.len() >= REFUSAL_QUORUM { + // Enough nodes say they do not have it: asking on learns + // nothing more. + if not_found.len() >= REFUSAL_QUORUM { break; } } LookupAnswer::Unknown { reason } => last_unknown = reason, } } - // A node's word that it is in a block: it exists, not settled as final. - let Some(reason) = refused.filter(|_| !mined.seen) else { - return ProbeVerdict::Accepted; - }; ProbeVerdict::Unresolved { reason: format!( "refused as {reason}; {} node(s) do not know the txid; last other lookup answer: {last_unknown}", @@ -491,13 +414,13 @@ fn lookup_reply( /// no confirmations is a block off the node's active chain (a stale fork; both /// DAPI servers send height 0 for Core's -1): neither mined nor in a mempool. fn lookup_answer(tx: &GetTransactionResponse) -> LookupAnswer { - let mined = tx.height > 0 || tx.confirmations > 0; - if !mined && !tx.block_hash.is_empty() { + let in_block = tx.height > 0 || tx.confirmations > 0; + if !in_block && !tx.block_hash.is_empty() { return LookupAnswer::Unknown { reason: "known only from a block off the active chain".to_string(), }; } - LookupAnswer::Known { mined } + LookupAnswer::Known } /// One node per request: the probe decides whether to ask another, and it has @@ -656,12 +579,8 @@ mod tests { } } - fn in_block() -> LookupAnswer { - LookupAnswer::Known { mined: true } - } - - fn in_mempool() -> LookupAnswer { - LookupAnswer::Known { mined: false } + fn known() -> LookupAnswer { + LookupAnswer::Known } fn conflict() -> NodeVerdict { @@ -693,7 +612,7 @@ mod tests { ( Code::AlreadyExists, "Transaction already in chain: Transaction already in block chain", - NodeVerdict::Mined, + NodeVerdict::Accepted, ), // -25, JS dapi. ( @@ -879,66 +798,40 @@ mod tests { } /// Core answers `missingorspent` for a mined transaction whose outputs - /// are all spent (testnet `e758f06a…`, 2026-09-29). Nodes that still know - /// the txid in a block must turn the refusals into Mined. + /// are all spent (testnet `e758f06a…`, 2026-09-29). A node that still + /// knows the txid turns the refusals into Accepted, at the first lookup. #[tokio::test] - async fn should_report_mined_for_a_refused_transaction_nodes_have_in_a_block() { + async fn should_accept_a_refused_transaction_a_node_still_knows() { let nodes = ScriptedNodes::with_lookups( vec![(Some("a"), refused()), (Some("b"), refused())], - vec![(Some("c"), in_block()), (Some("d"), in_block())], + vec![(Some("c"), known()), (Some("d"), known())], ); assert_eq!( probe_with(&nodes, &transaction()).await.verdict, - ProbeVerdict::Mined - ); - assert_eq!(nodes.lookups_made(), 2); - } - - /// One node on a stale fork (or with a block about to be reorged out) - /// must not settle a transaction as mined: two distinct nodes must. - #[tokio::test] - async fn should_report_mined_only_when_two_distinct_nodes_have_it_in_a_block() { - let two = ScriptedNodes::with_lookups( - vec![(Some("a"), NodeVerdict::Mined)], - vec![(Some("b"), in_block())], - ); - let same_twice = ScriptedNodes::with_lookups( - vec![(Some("a"), NodeVerdict::Mined)], - vec![(Some("a"), in_block())], - ); - - assert_eq!( - probe_with(&two, &transaction()).await.verdict, - ProbeVerdict::Mined - ); - assert_eq!(two.submissions(), 1, "the second opinion is a lookup"); - assert_eq!( - probe_with(&same_twice, &transaction()).await.verdict, - ProbeVerdict::Accepted, - "one node's word: it exists, not that it is final" + ProbeVerdict::Accepted ); + assert_eq!(nodes.lookups_made(), 1); } - /// A node that has the transaction in a block makes it exist, whatever - /// the refusals and lookups that follow say. + /// A node's "already in a block" (`-27`) is acceptance, with no lookup + /// for a second opinion: the probe does not tell a block from a mempool. #[tokio::test] - async fn should_accept_a_refused_transaction_one_node_has_in_a_block() { - let refused_then_seen_mined = ScriptedNodes::with_lookups( - vec![(Some("a"), refused()), (Some("b"), refused())], - vec![ - (Some("c"), in_block()), - (Some("d"), LookupAnswer::NotFound), - (Some("e"), LookupAnswer::NotFound), - ], + async fn should_accept_on_one_nodes_word_that_it_is_in_a_block() { + let nodes = ScriptedNodes::with_lookups( + vec![( + Some("a"), + classify_failed_submission(Code::AlreadyExists, "Transaction already in chain"), + )], + vec![(Some("b"), known())], ); assert_eq!( - probe_with(&refused_then_seen_mined, &transaction()) - .await - .verdict, + probe_with(&nodes, &transaction()).await.verdict, ProbeVerdict::Accepted ); + assert_eq!(nodes.submissions(), 1); + assert_eq!(nodes.lookups_made(), 0); } #[test] @@ -952,8 +845,8 @@ mod tests { }) }; - assert_eq!(answer(Vec::new(), 0, 0), in_mempool()); - assert_eq!(answer(vec![1; 32], 100, 3), in_block()); + assert_eq!(answer(Vec::new(), 0, 0), known()); + assert_eq!(answer(vec![1; 32], 100, 3), known()); assert!(matches!( answer(vec![1; 32], 0, 0), LookupAnswer::Unknown { .. } @@ -997,8 +890,8 @@ mod tests { let (_, mismatched) = lookup_reply(&txid, reply(other)); assert_eq!(mismatched, LookupAnswer::NotFound); - let (_, known) = lookup_reply(&txid, reply(consensus::serialize(&held))); - assert_eq!(known, in_mempool()); + let (_, answer) = lookup_reply(&txid, reply(consensus::serialize(&held))); + assert_eq!(answer, known()); } /// A node's answer stays the reason when later submissions reach no @@ -1227,7 +1120,7 @@ mod tests { async fn should_accept_a_refused_transaction_a_node_holds_in_its_mempool() { let nodes = ScriptedNodes::with_lookups( vec![(Some("a"), refused()), (Some("b"), refused())], - vec![(Some("c"), in_mempool())], + vec![(Some("c"), known())], ); assert_eq!( diff --git a/packages/rs-platform-wallet/src/events.rs b/packages/rs-platform-wallet/src/events.rs index 139e5e08183..7a2ead39d6d 100644 --- a/packages/rs-platform-wallet/src/events.rs +++ b/packages/rs-platform-wallet/src/events.rs @@ -50,12 +50,9 @@ pub trait PlatformEventHandler: EventHandler { /// Fired when the verdict on an unconfirmed send of this wallet changes — /// the network was asked about the root of its chain. `Accepted` means a - /// node holds the transaction in its mempool (or one has it in a block); - /// `Mined` that two distinct nodes have it in a block within one probe — - /// advisory like `Accepted`: the resolver keeps asking until the wallet - /// settles the send, a later probe may turn it back into `Accepted`, and - /// one dropped by a reorg stays published until a probe disagrees or the - /// send settles; `Unresolved` that there is no verdict + /// node holds the transaction, in its mempool or in a block — advisory: + /// the resolver keeps asking until the wallet settles the send, and it + /// stays published until then; `Unresolved` that there is no verdict /// yet — refusals included, since no node's answer proves a transaction /// can never land; the resolver asks again on a later block. Nothing in /// the wallet changes either way. diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 373ef67e836..f73a96c5ab8 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -10,7 +10,7 @@ //! signed transaction, so it goes out only under the lifecycle gate of the //! registration it was read from: a wallet's removal stops the one in //! flight and none is started after it (see [`gated_probe`]). Verdicts are positive -//! only — accepted or mined — or unresolved: no answer the probe gets proves +//! only — accepted — or unresolved: no answer the probe gets proves //! that a transaction can never land (see //! [`broadcast_probe`](crate::broadcast_probe)). //! @@ -25,13 +25,12 @@ //! //! What is published: verdicts per **own send**, which is what the host //! shows. A root's verdict goes to the root when it is the wallet's send. -//! Every verdict is advisory, Mined included: only the wallet itself (a -//! block, an InstantSend lock) settles a send, so a root found mined stays a -//! root — probed on its schedule like any other — until the wallet settles -//! it, and a later probe may turn it back into Accepted. Only changes are -//! published: Unresolved never replaces a decided verdict (Accepted, Mined), -//! so a Mined a reorg dropped stays published until a probe disagrees or the -//! wallet settles the send. The host is told to drop a send's verdict +//! Every verdict is advisory: only the wallet itself (a block, an +//! InstantSend lock) settles a send, so a root the nodes accepted, in a +//! mempool or in a block, stays a root — probed on its schedule like any +//! other — until the wallet settles it. Only changes are published: +//! Unresolved never replaces Accepted, so an Accepted stays published until +//! the wallet settles the send. The host is told to drop a send's verdict //! (cleared) when the send settles or leaves the wallet, when its wallet is //! removed and, for every send, when probing is turned off; cleared says //! only "forget the verdict", not that the send settled. A @@ -326,8 +325,8 @@ pub(crate) struct ChainGraph<'a> { } impl<'a> ChainGraph<'a> { - /// Only the wallet settles a transaction: a probe's verdict, Mined - /// included, changes nothing here. + /// Only the wallet settles a transaction: a probe's verdict changes + /// nothing here. pub(crate) fn new(views: &'a [OutgoingView]) -> Self { Self { by_txid: views.iter().map(|view| (view.txid, view)).collect(), @@ -508,9 +507,7 @@ impl ProbeSchedule { } /// Whether two verdicts say the same thing. Every Unresolved counts as one, -/// since its reason varies from probe to probe. Accepted and Mined differ — -/// though the FFI host receives both as the same code (see -/// `OUTGOING_PROBE_VERDICT_ACCEPTED`). +/// since its reason varies from probe to probe. fn same_verdict(a: &ProbeVerdict, b: &ProbeVerdict) -> bool { match (a, b) { (ProbeVerdict::Unresolved { .. }, ProbeVerdict::Unresolved { .. }) => true, @@ -619,8 +616,8 @@ impl ResolverState { } /// Record `verdict` for `send`; returns whether the host must hear it. - /// Unresolved never replaces a decided verdict (Accepted, Mined) — one - /// probe that only met transport errors says nothing new. + /// Unresolved never replaces Accepted — one probe that only met + /// transport errors says nothing new. fn publish(&mut self, key: (WalletId, Txid), verdict: &ProbeVerdict) -> bool { let changed = match self.published.get(&key) { None => true, @@ -2185,8 +2182,7 @@ impl Actor { continue; }; // An echo says only that the send is out there: it adds - // nothing to a decided verdict (and must not turn a Mined - // back into Accepted). + // nothing to a send already accepted. if self .state .published @@ -3558,12 +3554,6 @@ mod tests { [7u8; 32] } - /// The nodes' advisory word that a send is in a block: the root is still - /// probed until the wallet settles it. - fn mined() -> ProbeVerdict { - ProbeVerdict::Mined - } - fn unresolved() -> ProbeVerdict { ProbeVerdict::Unresolved { reason: "no quorum".to_string(), @@ -3827,25 +3817,23 @@ mod tests { } } - /// Nodes' word that a send is mined is not settlement: the root keeps - /// being probed on its schedule, a later probe that finds it only in a - /// mempool turns the verdict back into Accepted, a probe that learns - /// nothing keeps the last decided verdict, and only the wallet settling - /// the send ends it. + /// Nodes' word that a send is accepted, in a mempool or a block, is not + /// settlement: the root keeps being probed on its schedule, a repeat or + /// a probe that learns nothing publishes nothing, and only the wallet + /// settling the send ends it. #[tokio::test] - async fn should_keep_probing_a_mined_root_until_the_wallet_settles_it() { + async fn should_keep_probing_an_accepted_root_until_the_wallet_settles_it() { let state = Mutex::new(Harness::default()); - let probe = SequenceProbe::new(&[ProbeVerdict::Mined, ProbeVerdict::Accepted]); + let probe = SequenceProbe::new(&[ProbeVerdict::Accepted, ProbeVerdict::Accepted]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 0)])]; assert_eq!( pass(&state, &probe, 100, &views).await, - vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Mined)] + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] ); - assert_eq!( - pass(&state, &probe, 101, &views).await, - vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)], - "a later probe disagrees" + assert!( + pass(&state, &probe, 101, &views).await.is_empty(), + "a repeat is no news" ); assert!(pass(&state, &probe, 102, &views).await.is_empty()); assert_eq!(probe.calls(), 3, "only the root, on every block"); @@ -3925,9 +3913,8 @@ mod tests { } /// One probe that met only transport errors says nothing new about a send - /// already seen accepted or mined; the UI must not flicker back to - /// "unknown". Accepted and Mined replace each other — a later probe may - /// disagree with an earlier one either way — and a repeat is no news. + /// already seen accepted; the UI must not flicker back to "unknown". A + /// repeat is no news. #[test] fn should_publish_only_a_change_of_verdict() { let mut state = ResolverState::default(); @@ -3936,10 +3923,6 @@ mod tests { assert!(state.publish(key, &ProbeVerdict::Accepted)); assert!(!state.publish(key, &unresolved())); assert!(!state.publish(key, &ProbeVerdict::Accepted)); - assert!(state.publish(key, &ProbeVerdict::Mined)); - assert!(!state.publish(key, &unresolved())); - assert!(!state.publish(key, &ProbeVerdict::Mined)); - assert!(state.publish(key, &ProbeVerdict::Accepted)); } // ---- the actor ---------------------------------------------------------- @@ -6000,11 +5983,11 @@ mod tests { ); } - /// An echo adds nothing to a decided verdict: a send probed Mined stays - /// Mined, not turned back into Accepted. + /// An echo adds nothing to a send a probe already found accepted: no + /// second Accepted is published. #[tokio::test] - async fn should_not_turn_a_mined_send_back_into_accepted_on_its_echo() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), mined())])); + async fn should_not_publish_an_accepted_send_again_on_its_echo() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); let mut rig = enabled(probe).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.send(Command::Seen { @@ -6013,14 +5996,17 @@ mod tests { }) .await; rig.send(Command::Uncertain(txid(1))).await; - assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(1), mined())]); + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] + ); rig.send(Command::Echoed(txid(1))).await; assert!(rig.sent().is_empty()); assert_eq!( rig.actor.state.published.get(&(wallet(), txid(1))), - Some(&mined()) + Some(&ProbeVerdict::Accepted) ); } @@ -6215,7 +6201,11 @@ mod tests { /// it before, so it must not leave the wallet idle. #[tokio::test] async fn should_not_leave_idle_a_wallet_touched_during_its_pass() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), mined())]))).await; + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[( + txid(1), + ProbeVerdict::Accepted, + )]))) + .await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.height(wallet(), 100).await; @@ -6237,7 +6227,7 @@ mod tests { /// height re-publishes what was cleared. #[tokio::test] async fn should_republish_after_probing_goes_off_and_on() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), mined())])); + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); let mut rig = enabled(probe.clone()).await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.follow(wallet(), 100).await; @@ -6248,7 +6238,10 @@ mod tests { rig.send(Command::SetEnabled(true)).await; rig.height(wallet(), 102).await; - assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(1), mined())]); + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] + ); assert_eq!(probe.probed(), vec![txid(1), txid(1)]); } @@ -6256,7 +6249,11 @@ mod tests { /// dropped, nothing is published after the clears. #[tokio::test] async fn should_drop_a_pass_in_flight_when_probing_goes_off() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), mined())]))).await; + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[( + txid(1), + ProbeVerdict::Accepted, + )]))) + .await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.height(wallet(), 100).await; @@ -7067,7 +7064,7 @@ mod tests { #[tokio::test] async fn should_clear_a_verdict_as_soon_as_an_event_settles_its_send() { let mut rig = enabled(Arc::new(ScriptedProbe::new(&[ - (txid(1), mined()), + (txid(1), ProbeVerdict::Accepted), (txid(2), unresolved()), ]))) .await; @@ -7193,7 +7190,11 @@ mod tests { /// verdict cleared, every schedule gone, so every root is due at once. #[tokio::test] async fn should_start_over_on_a_rewind() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), mined())]))).await; + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[( + txid(1), + ProbeVerdict::Accepted, + )]))) + .await; rig.views( wallet(), vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])], @@ -7503,12 +7504,16 @@ mod tests { assert_eq!(probe.probed(), vec![txid(2)]); } - /// The first root's probe finishes Mined, then the root settles before + /// The first root's probe finishes Accepted, then the root settles before /// the result is handled: the leftover answer is not recorded against /// the next root in flight. #[tokio::test] async fn should_not_record_a_stopped_probe_answer_against_the_next_root() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), mined())]))).await; + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[( + txid(1), + ProbeVerdict::Accepted, + )]))) + .await; rig.views( wallet(), vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(91, 0)])], @@ -7528,7 +7533,7 @@ mod tests { Ok((_, JobDone::Probed { root, outcome, .. })) if *root == txid(1) && outcome.as_ref().map(|outcome| &outcome.verdict) - == Some(&ProbeVerdict::Mined) + == Some(&ProbeVerdict::Accepted) )); rig.handle(Command::Settled { @@ -7540,10 +7545,10 @@ mod tests { assert!(!rig .sent() - .contains(&ResolverEvent::Verdict(txid(2), mined()))); + .contains(&ResolverEvent::Verdict(txid(2), ProbeVerdict::Accepted))); assert_ne!( rig.actor.state.published.get(&(wallet(), txid(2))), - Some(&mined()) + Some(&ProbeVerdict::Accepted) ); } @@ -7651,10 +7656,17 @@ mod tests { /// stale verdicts from before do not carry over. #[tokio::test] async fn should_start_a_re_registered_wallet_fresh() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), mined())]))).await; + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[( + txid(1), + ProbeVerdict::Accepted, + )]))) + .await; rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); rig.follow(wallet(), 100).await; - assert_eq!(rig.sent(), vec![ResolverEvent::Verdict(txid(1), mined())]); + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] + ); assert!(rig.actor.wallets[&wallet()].wait_until.is_some()); rig.send(Command::WalletAdded(wallet())).await; @@ -7693,12 +7705,16 @@ mod tests { assert!(rig.actor.state.published.is_empty()); } - /// A probe sent before a rewind would answer Mined: the answer belongs to + /// A probe sent before a rewind would answer Accepted: the answer belongs to /// the state the rewind dropped — the rewind stops it, and nothing is told /// or recorded. #[tokio::test] async fn should_neither_tell_nor_record_a_pre_rewind_answer() { - let mut rig = enabled(Arc::new(ScriptedProbe::new(&[(txid(1), mined())]))).await; + let mut rig = enabled(Arc::new(ScriptedProbe::new(&[( + txid(1), + ProbeVerdict::Accepted, + )]))) + .await; rig.views( wallet(), vec![send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 1)])], From 17ec1caa9cb73c6fbdfef55699be92edb370241e Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 18:24:24 +0300 Subject: [PATCH 164/169] refactor(platform-wallet): simplify what is left after the Mined verdict With two verdicts, publishing is one rule: a first verdict, or Accepted after Unresolved. The helpers that compared three are gone, and so is the echo guard that kept an echo from turning Mined into Accepted. Tests that only separated a mempool from a block are merged, and the publish test asserts the one transition left. Docs say that an accepted send stays accepted until it is cleared. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/broadcast_probe.rs | 28 +++----- .../src/wallet/core/broadcast_resolver.rs | 66 ++++++++----------- .../PlatformWalletManagerBroadcastProbe.swift | 4 +- 3 files changed, 38 insertions(+), 60 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index 204463d2f3c..03fda5d4640 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -154,8 +154,9 @@ pub(crate) fn classify_failed_submission(code: Code, message: &str) -> NodeVerdi pub enum ProbeVerdict { /// A node holds the transaction, in its mempool or in a block. Not /// settlement: it can still expire, lose to a conflict or sit in a block - /// a reorg drops, so the resolver keeps asking until the wallet settles - /// the send. + /// a reorg drops. The resolver keeps probing the send until the wallet + /// settles it, which hands the nodes its bytes again; the verdict itself + /// does not go back to Unresolved. Accepted, /// Not enough evidence that it landed; the transaction stays ambiguous. /// The reason says what the nodes answered, refusals included. @@ -798,13 +799,15 @@ mod tests { } /// Core answers `missingorspent` for a mined transaction whose outputs - /// are all spent (testnet `e758f06a…`, 2026-09-29). A node that still - /// knows the txid turns the refusals into Accepted, at the first lookup. + /// are all spent (testnet `e758f06a…`, 2026-09-29), and for one still in + /// a mempool that lost a double-spend race on the refusing nodes. A node + /// that knows the txid turns the refusals into Accepted, at the first + /// lookup. #[tokio::test] async fn should_accept_a_refused_transaction_a_node_still_knows() { let nodes = ScriptedNodes::with_lookups( vec![(Some("a"), refused()), (Some("b"), refused())], - vec![(Some("c"), known()), (Some("d"), known())], + vec![(Some("c"), known())], ); assert_eq!( @@ -1113,19 +1116,4 @@ mod tests { assert_eq!(nodes.lookups_made(), 0); } - - /// Refused for its inputs yet still in a node's mempool (a double-spend - /// race the node lost locally): not mined — keep asking. - #[tokio::test] - async fn should_accept_a_refused_transaction_a_node_holds_in_its_mempool() { - let nodes = ScriptedNodes::with_lookups( - vec![(Some("a"), refused()), (Some("b"), refused())], - vec![(Some("c"), known())], - ); - - assert_eq!( - probe_with(&nodes, &transaction()).await.verdict, - ProbeVerdict::Accepted - ); - } } diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index f73a96c5ab8..2f7adfd699d 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -28,9 +28,11 @@ //! Every verdict is advisory: only the wallet itself (a block, an //! InstantSend lock) settles a send, so a root the nodes accepted, in a //! mempool or in a block, stays a root — probed on its schedule like any -//! other — until the wallet settles it. Only changes are published: -//! Unresolved never replaces Accepted, so an Accepted stays published until -//! the wallet settles the send. The host is told to drop a send's verdict +//! other — until the wallet settles it; those probes hand the signed bytes +//! to the nodes again and tell the host nothing new. Only changes are +//! published: Unresolved never replaces Accepted, so an Accepted stays +//! published until the wallet settles the send, also for a send that was +//! evicted or reorged out since. The host is told to drop a send's verdict //! (cleared) when the send settles or leaves the wallet, when its wallet is //! removed and, for every send, when probing is turned off; cleared says //! only "forget the verdict", not that the send settled. A @@ -506,19 +508,6 @@ impl ProbeSchedule { } } -/// Whether two verdicts say the same thing. Every Unresolved counts as one, -/// since its reason varies from probe to probe. -fn same_verdict(a: &ProbeVerdict, b: &ProbeVerdict) -> bool { - match (a, b) { - (ProbeVerdict::Unresolved { .. }, ProbeVerdict::Unresolved { .. }) => true, - _ => a == b, - } -} - -fn is_decided(verdict: &ProbeVerdict) -> bool { - !matches!(verdict, ProbeVerdict::Unresolved { .. }) -} - /// What the host must hear, per own send. #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum ResolverEvent { @@ -615,15 +604,20 @@ impl ResolverState { .collect() } - /// Record `verdict` for `send`; returns whether the host must hear it. - /// Unresolved never replaces Accepted — one probe that only met - /// transport errors says nothing new. + /// Record `verdict` for `send`; returns whether the host must hear it: + /// a first verdict, or Accepted after Unresolved. Accepted stays until + /// the send is cleared — Unresolved never replaces it, since one probe + /// that only met transport errors says nothing new — and every + /// Unresolved counts as one, its reason varying from probe to probe. fn publish(&mut self, key: (WalletId, Txid), verdict: &ProbeVerdict) -> bool { - let changed = match self.published.get(&key) { - None => true, - Some(previous) if is_decided(previous) && !is_decided(verdict) => false, - Some(previous) => !same_verdict(previous, verdict), - }; + let changed = matches!( + (self.published.get(&key), verdict), + (None, _) + | ( + Some(ProbeVerdict::Unresolved { .. }), + ProbeVerdict::Accepted + ) + ); if changed { self.published.insert(key, verdict.clone()); } @@ -2181,16 +2175,8 @@ impl Actor { let Some(entry) = self.wallets.get(&wallet_id) else { continue; }; - // An echo says only that the send is out there: it adds - // nothing to a send already accepted. - if self - .state - .published - .get(&(wallet_id, txid)) - .is_some_and(is_decided) - { - continue; - } + // An echo says only that the send is out there: for one + // already accepted this publishes nothing. let height = entry.height.unwrap_or(0); self.state.publish_all( wallet_id, @@ -3818,11 +3804,10 @@ mod tests { } /// Nodes' word that a send is accepted, in a mempool or a block, is not - /// settlement: the root keeps being probed on its schedule, a repeat or - /// a probe that learns nothing publishes nothing, and only the wallet - /// settling the send ends it. + /// settlement: the root is still probed, and only the wallet settling + /// the send clears it and makes its child the root. #[tokio::test] - async fn should_keep_probing_an_accepted_root_until_the_wallet_settles_it() { + async fn should_clear_an_accepted_root_only_when_the_wallet_settles_it() { let state = Mutex::new(Harness::default()); let probe = SequenceProbe::new(&[ProbeVerdict::Accepted, ProbeVerdict::Accepted]); let views = [send(1, &[outpoint(90, 0)]), send(2, &[outpoint(1, 0)])]; @@ -3920,6 +3905,11 @@ mod tests { let mut state = ResolverState::default(); let key = (wallet(), txid(1)); + assert!(state.publish(key, &unresolved())); + let other_reason = ProbeVerdict::Unresolved { + reason: "another reason".to_string(), + }; + assert!(!state.publish(key, &other_reason), "still unresolved"); assert!(state.publish(key, &ProbeVerdict::Accepted)); assert!(!state.publish(key, &unresolved())); assert!(!state.publish(key, &ProbeVerdict::Accepted)); diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift index 3a2e04be592..efaf888bac9 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerBroadcastProbe.swift @@ -151,8 +151,8 @@ extension PlatformWalletManager { /// probe in flight once per block; a later one waits for that pass. /// It publishes each *change* of /// verdict for the wallet's own sends in `outgoingTransactionVerdicts` / - /// `lastOutgoingTransactionProbe` (an accepted send whose nodes' block - /// evidence comes or goes may be published accepted again). Every verdict + /// `lastOutgoingTransactionProbe` (an accepted send stays accepted until + /// its entry is removed). Every verdict /// is advisory: only the wallet's own sync settles a send. An entry is removed when its send /// settles or leaves the wallet, when its wallet is deleted, and every /// entry when probing is turned off; a removal does not mean the send From ef801dd3c0353df69e0d85e0998368ca67e252e2 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 18:33:43 +0300 Subject: [PATCH 165/169] fix(platform-wallet): keep the found height apart from the step cursor, bound a probe request - The height a completion's pass finds the wallet at went into the cursor of delivered height steps, so a step still on its way below it read as a rewind and cleared what the pass had published. It is kept apart now: a late step below it is ignored. - The completed tip a wallet was asked about is carried beside a pass's own anchor, so a question queued behind a pass under way survives that pass's end and does not replace a following pass's anchor. - Switching probing off and on, and a rewind, start that question over. - A probe waits at most 15 seconds for a node's answer to a submission or a lookup, whatever timeout the SDK is configured with. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/broadcast_probe.rs | 81 ++++++++- .../src/wallet/core/broadcast_resolver.rs | 159 +++++++++++++++--- 2 files changed, 216 insertions(+), 24 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index 03fda5d4640..b1942c2cc42 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -46,6 +46,7 @@ use std::collections::HashSet; use std::sync::Arc; +use std::time::Duration; use async_trait::async_trait; use dash_sdk::core::transaction_from_reply; @@ -60,6 +61,7 @@ use dash_sdk::dapi_grpc::core::v0::{ use dash_sdk::dapi_grpc::tonic::Code; use dashcore::consensus; use dashcore::{Transaction, Txid}; +use tokio::time::timeout; /// Core reject reasons about the transaction's inputs. /// @@ -189,6 +191,13 @@ const MAX_SUBMISSIONS_PER_PROBE: usize = 4; /// How many txid lookups one probe may spend after refusals. const MAX_LOOKUPS_PER_PROBE: usize = 4; +/// How long a probe waits for one node's answer to a submission or a lookup, +/// whatever timeout the SDK it goes through is configured with (an SDK may +/// run with none). A probe is at most [`MAX_SUBMISSIONS_PER_PROBE`] plus +/// [`MAX_LOOKUPS_PER_PROBE`] requests, so this bounds how long one holds its +/// wallet's pass. A request that runs out is no answer. +const REQUEST_DEADLINE: Duration = Duration::from_secs(15); + /// How many *distinct* nodes must refuse a transaction for its inputs before /// the probe looks the txid up. One refusal may be a node lagging behind the /// tip; two make it worth asking whether it is a mined transaction whose @@ -247,7 +256,12 @@ pub(crate) async fn probe_with( let mut answered = false; for _ in 0..MAX_SUBMISSIONS_PER_PROBE { - let (node, verdict) = submitter.submit(transaction).await; + let (node, verdict) = timeout(REQUEST_DEADLINE, submitter.submit(transaction)) + .await + .unwrap_or_else(|_| { + let reason = format!("no answer within {}s", REQUEST_DEADLINE.as_secs()); + (None, NodeVerdict::Unreachable { reason }) + }); answered |= !matches!(verdict, NodeVerdict::Unreachable { .. }); tracing::debug!( txid = %transaction.txid(), @@ -308,7 +322,12 @@ async fn look_up_refused( let mut not_found: HashSet = HashSet::new(); let mut last_unknown = String::from("no lookup was answered"); for _ in 0..MAX_LOOKUPS_PER_PROBE { - let (node, answer) = submitter.lookup(txid).await; + let (node, answer) = timeout(REQUEST_DEADLINE, submitter.lookup(txid)) + .await + .unwrap_or_else(|_| { + let reason = format!("no answer within {}s", REQUEST_DEADLINE.as_secs()); + (None, LookupAnswer::Unknown { reason }) + }); tracing::debug!( txid = %txid, node = node.as_deref().unwrap_or("unidentified"), @@ -1116,4 +1135,62 @@ mod tests { assert_eq!(nodes.lookups_made(), 0); } + + /// Nodes that never answer, as behind an SDK configured with no request + /// timeout. + struct SilentNodes; + + #[async_trait] + impl NodeSubmitter for SilentNodes { + async fn submit(&self, _transaction: &Transaction) -> (Option, NodeVerdict) { + std::future::pending().await + } + + async fn lookup(&self, _txid: &Txid) -> (Option, LookupAnswer) { + std::future::pending().await + } + } + + /// A probe ends by its own deadline when no node ever answers: it does + /// not hold its wallet's pass for as long as the transport waits. + #[tokio::test(start_paused = true)] + async fn should_end_a_probe_whose_requests_are_never_answered() { + let started = tokio::time::Instant::now(); + + let outcome = probe_with(&SilentNodes, &transaction()).await; + + assert!(!outcome.answered); + assert!(is_unresolved(&outcome.verdict)); + assert_eq!( + started.elapsed(), + REQUEST_DEADLINE * MAX_SUBMISSIONS_PER_PROBE as u32 + ); + } + + /// A lookup that is never answered does not take back the refusals the + /// probe already heard: it ends unresolved, as an answered probe. + #[tokio::test(start_paused = true)] + async fn should_end_a_lookup_that_is_never_answered() { + struct RefusingThenSilent(ScriptedNodes); + + #[async_trait] + impl NodeSubmitter for RefusingThenSilent { + async fn submit(&self, transaction: &Transaction) -> (Option, NodeVerdict) { + self.0.submit(transaction).await + } + + async fn lookup(&self, _txid: &Txid) -> (Option, LookupAnswer) { + std::future::pending().await + } + } + let nodes = RefusingThenSilent(ScriptedNodes::new(vec![ + (Some("a"), refused()), + (Some("b"), refused()), + ])); + + let outcome = probe_with(&nodes, &transaction()).await; + + assert!(outcome.answered); + assert!(is_unresolved(&outcome.verdict)); + } } diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 2f7adfd699d..338e3f074b9 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -1366,6 +1366,11 @@ struct PendingPass { /// results asked for it, or a catch-up overtook it — probes only the /// roots those results forced and starts no windows. anchor_at: Option, + /// A completed tip the wallet is asked about (see + /// `Command::SyncComplete`): the read anchors the pass there when it + /// finds the wallet at that tip and `anchor_at` does not hold. Such a + /// pass is not dropped for being below the wallet's wait. + asked_tip: Option, /// Roots a retry forces (see [`NO_ANSWER_RETRY`]): probed only if they /// are still roots — one whose parent has since shown up unsettled is /// not resolved to that parent. @@ -1389,6 +1394,8 @@ struct Run { /// Where this pass starts windows; `None` confines it to forced roots /// (see [`PendingPass::anchor_at`]). anchor_at: Option, + /// See [`PendingPass::asked_tip`]. + asked_tip: Option, /// What `Uncertain` results forced it to probe: carried over to the next /// pass when a rewind stops it. forced: HashSet, @@ -1462,6 +1469,12 @@ struct WalletEntry { /// `Command::SyncComplete`): one read per tip, however often it /// completes. asked_at: Option, + /// The height a pass that follows the tip found the wallet at when that + /// was above the last height step handled: the wallet's height for + /// retries and for judging the next step, until the steps catch up. Kept + /// apart from `height`, which is the cursor of the steps delivered: one + /// still on its way below this is late, not a rewind. + found_at: Option, } impl WalletEntry { @@ -1692,6 +1705,8 @@ impl Actor { entry.retry_series = None; entry.followed_pass_at = None; entry.yielded_at = None; + entry.asked_at = None; + entry.found_at = None; if let Some(run) = entry.run.take() { run.abort.abort(); if !run.forced.is_empty() { @@ -1712,6 +1727,12 @@ impl Actor { let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; + // A pass has already found the wallet above this step: the + // step is late, and what follows from it is done. + if entry.found_at.is_some_and(|found| height < found) { + return; + } + let previous = previous.max(entry.found_at.take()); let completed = entry .completed_ahead .filter(|(_, at)| at.elapsed() <= COMPLETED_AHEAD_TTL) @@ -1838,6 +1859,8 @@ impl Actor { entry.retry_series = None; entry.followed_pass_at = None; entry.yielded_at = None; + entry.asked_at = None; + entry.found_at = None; if !enabled { entry.pending = None; if let Some(run) = entry.run.take() { @@ -1910,7 +1933,8 @@ impl Actor { // tip, which its read confines to forced roots when the // wallet really is below, and whose height the actor takes // when it is not (see `JobDone::Read`). Behind a pass under - // way; a pass already queued as following keeps its anchor. + // way; a pass already queued as following keeps its anchor, + // and the tip is asked beside it (`PendingPass::asked_tip`). for wallet_id in below_tip { let Some(entry) = self.wallets.get_mut(&wallet_id) else { continue; @@ -1918,7 +1942,7 @@ impl Actor { let known = entry.height.unwrap_or(tip); let pending = entry.pending.get_or_insert_with(PendingPass::default); pending.height = pending.height.max(known); - pending.anchor_at.get_or_insert(tip); + pending.asked_tip = Some(tip); self.start(wallet_id); } } @@ -2073,12 +2097,13 @@ impl Actor { return; }; let anchor_at = pending.anchor_at; + let asked_tip = pending.asked_tip; let forced = pending.forced.clone(); let retried = pending.retried.clone(); let to_read: HashSet = pending.forced.union(&pending.retried).copied().collect(); // Walk the records if the pass may probe by schedule; one confined to // forced roots with none of them held has nothing to read for. - let walk = Arc::new(AtomicBool::new(anchor_at.is_some())); + let walk = Arc::new(AtomicBool::new(anchor_at.is_some() || asked_tip.is_some())); self.next_run += 1; let run = self.next_run; let source = Arc::clone(&self.source); @@ -2098,6 +2123,7 @@ impl Actor { settled: HashSet::new(), woken: false, anchor_at, + asked_tip, forced, retried, walk, @@ -2219,29 +2245,27 @@ impl Actor { // it never reached the actor. Retries and the next // step are judged against it. let synced = read.synced_height; - let follows = entry - .run - .as_ref() - .and_then(|run| run.anchor_at) - .is_some_and(|at| { - synced >= at && synced <= at.saturating_add(CATCH_UP_STEP) - }); - if follows && entry.height.is_none_or(|known| known < synced) { - entry.height = Some(synced); - } + let fits = + |at: u32| synced >= at && synced <= at.saturating_add(CATCH_UP_STEP); let Some(current) = entry.run.as_mut() else { return; // matched by id above }; // The wallet is not where this pass was queued: far // ahead (a catch-up whose height step has not arrived // yet) or behind (a rewind for a rescan). Either way a - // catch-up — confine the pass to forced roots. - if current.anchor_at.is_some_and(|at| { - read.synced_height < at - || read.synced_height > at.saturating_add(CATCH_UP_STEP) - }) { - current.anchor_at = None; + // catch-up — confine the pass to forced roots. Unless + // a completion asked about a tip and the wallet is + // there: then that tip is the pass's anchor. + if !current.anchor_at.is_some_and(fits) { + current.anchor_at = current.asked_tip.filter(|at| fits(*at)); } + let follows = current.anchor_at.is_some(); + if follows && entry.height.is_none_or(|known| known < synced) { + entry.found_at = Some(synced); + } + let Some(current) = entry.run.as_mut() else { + return; + }; // What an event settled since the read began is not // the pass's any more. let views: Vec = read @@ -2342,7 +2366,11 @@ impl Actor { } // Still at that height: a new block's pass probes the // unanswered roots anyway (due at the next height). - if entry.height.is_some_and(|now| now != height) { + if entry + .height + .max(entry.found_at) + .is_some_and(|now| now != height) + { return; } tracing::info!( @@ -2465,6 +2493,7 @@ impl Actor { if let Some(pending) = entry.pending.as_mut() { if !stays(pending.anchor_at) { pending.anchor_at = None; + pending.asked_tip = None; // A rewind replays lower heights: never run above them. pending.height = height; if pending.forced.is_empty() { @@ -2484,6 +2513,7 @@ impl Actor { return; } run.anchor_at = None; + run.asked_tip = None; run.walk.store(false, Ordering::SeqCst); if let Some(probing) = run.probing.as_mut() { probing.drop_scheduled(); @@ -2649,7 +2679,7 @@ impl Actor { }; entry.run = None; if let (Some(pending), Some(until)) = (&entry.pending, entry.wait_until) { - if pending.forced.is_empty() && pending.height < until { + if pending.forced.is_empty() && pending.asked_tip.is_none() && pending.height < until { entry.pending = None; } } @@ -5319,10 +5349,94 @@ mod tests { rig.send(sync_complete(150)).await; - assert_eq!(rig.actor.wallets[&wallet()].height, Some(150)); + assert_eq!(rig.actor.wallets[&wallet()].found_at, Some(150)); assert_eq!(probe.probed().len(), 1 + MAX_NO_ANSWER_RETRIES as usize); } + /// The height a completion's pass found the wallet at is not the cursor + /// of its height steps: a step still on its way below it is late, not a + /// rewind, and must not clear what that pass published. + #[tokio::test] + async fn should_not_take_a_late_step_below_a_found_height_for_a_rewind() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 150); + rig.send(sync_complete(150)).await; + assert_eq!( + rig.sent(), + vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] + ); + + rig.height(wallet(), 120).await; // late + rig.height(wallet(), 150).await; // the step the completion overtook + + assert!(rig.sent().is_empty(), "nothing cleared, nothing repeated"); + assert_eq!(probe.probed().len(), 1); + assert_eq!( + rig.actor.state.schedules[&(wallet(), txid(1))].window_start, + Some(150) + ); + assert_eq!(rig.actor.wallets[&wallet()].found_at, None); + } + + /// Turning probing off and on starts the completion's question over: a + /// tip asked in the earlier period is asked again. + #[tokio::test] + async fn should_ask_a_completed_tip_again_after_probing_was_switched_off_and_on() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + rig.send(sync_complete(150)).await; // asked: the wallet is at 100 + assert_eq!(probe.probed().len(), 0); + + rig.send(Command::SetEnabled(false)).await; + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 150); + rig.send(Command::SetEnabled(true)).await; + rig.send(sync_complete(150)).await; + + assert_eq!(probe.probed().len(), 1); + } + + /// A completion's question queued behind a pass under way survives that + /// pass's end: the wallet is read again and the pass at the tip runs. + #[tokio::test] + async fn should_ask_the_wallet_after_the_pass_a_completion_found_under_way() { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe.clone()).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.height(wallet(), 100).await; + + // A followed pass at 101: its read is back, its probe is out. + rig.handle(Command::Height { + wallet_id: wallet(), + height: 101, + }); + let read = rig.actor.jobs.join_next_with_id().await.expect("the read"); + rig.actor.joined(read); + // The wallet reaches 150 with no step delivered; the sync completes. + rig.source + .synced + .lock() + .expect("synced") + .insert(wallet(), 150); + rig.handle(sync_complete(150)); + rig.settle().await; + + assert_eq!(rig.actor.wallets[&wallet()].found_at, Some(150)); + assert_eq!(rig.actor.wallets[&wallet()].followed_pass_at, Some(150)); + } + /// A wallet the actor has below a completed tip is read once for that /// tip, however often the sync completes there. #[tokio::test] @@ -5857,6 +5971,7 @@ mod tests { settled: HashSet::new(), woken: false, anchor_at: Some(101), + asked_tip: None, forced: HashSet::new(), retried: HashSet::new(), walk: Arc::new(AtomicBool::new(false)), From efb536df60823722495dddf3be154c43cadebc3c Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 18:49:57 +0300 Subject: [PATCH 166/169] fix(platform-wallet): take the lost-step recovery back out, keep the request deadline The recovery of a height step that never arrives (a completion asking the wallet where it is) could not tell a late step from a rewind by heights alone: taking the found height as the wallet's made a late step read as a rewind, and keeping it apart made a real rewind read as late. Three review rounds each found another case, so it is removed. A lost step costs what it cost before: the pass runs at the next block. The completion that overtakes its step is still handled (completed_ahead). Kept from those rounds: echo lookups made again after a read, the removal's wait on a wallet already gone, the tests made to bite. A probe's request to a node now carries its own 15 second deadline, with a 20 second limit in the probe itself, whatever the SDK's timeout is. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/broadcast_probe.rs | 27 +- .../src/wallet/core/broadcast_resolver.rs | 255 ++---------------- 2 files changed, 44 insertions(+), 238 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index b1942c2cc42..33dfeb03e62 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -191,13 +191,23 @@ const MAX_SUBMISSIONS_PER_PROBE: usize = 4; /// How many txid lookups one probe may spend after refusals. const MAX_LOOKUPS_PER_PROBE: usize = 4; -/// How long a probe waits for one node's answer to a submission or a lookup, -/// whatever timeout the SDK it goes through is configured with (an SDK may -/// run with none). A probe is at most [`MAX_SUBMISSIONS_PER_PROBE`] plus +/// How long a probe's request to one node may take, whatever timeout the SDK +/// it goes through is configured with (an SDK may run with none): set on the +/// request itself (see [`single_node`]), so a node that ran out is still +/// named. A probe is at most [`MAX_SUBMISSIONS_PER_PROBE`] plus /// [`MAX_LOOKUPS_PER_PROBE`] requests, so this bounds how long one holds its /// wallet's pass. A request that runs out is no answer. const REQUEST_DEADLINE: Duration = Duration::from_secs(15); +/// The probe's own limit on one request, a little past [`REQUEST_DEADLINE`]: +/// it holds whatever the transport underneath does with its deadline. +const REQUEST_BACKSTOP: Duration = Duration::from_secs(20); + +/// The reason of a request the backstop ended. +fn out_of_time() -> String { + format!("no answer within {}s", REQUEST_BACKSTOP.as_secs()) +} + /// How many *distinct* nodes must refuse a transaction for its inputs before /// the probe looks the txid up. One refusal may be a node lagging behind the /// tip; two make it worth asking whether it is a mined transaction whose @@ -256,10 +266,10 @@ pub(crate) async fn probe_with( let mut answered = false; for _ in 0..MAX_SUBMISSIONS_PER_PROBE { - let (node, verdict) = timeout(REQUEST_DEADLINE, submitter.submit(transaction)) + let (node, verdict) = timeout(REQUEST_BACKSTOP, submitter.submit(transaction)) .await .unwrap_or_else(|_| { - let reason = format!("no answer within {}s", REQUEST_DEADLINE.as_secs()); + let reason = out_of_time(); (None, NodeVerdict::Unreachable { reason }) }); answered |= !matches!(verdict, NodeVerdict::Unreachable { .. }); @@ -322,10 +332,10 @@ async fn look_up_refused( let mut not_found: HashSet = HashSet::new(); let mut last_unknown = String::from("no lookup was answered"); for _ in 0..MAX_LOOKUPS_PER_PROBE { - let (node, answer) = timeout(REQUEST_DEADLINE, submitter.lookup(txid)) + let (node, answer) = timeout(REQUEST_BACKSTOP, submitter.lookup(txid)) .await .unwrap_or_else(|_| { - let reason = format!("no answer within {}s", REQUEST_DEADLINE.as_secs()); + let reason = out_of_time(); (None, LookupAnswer::Unknown { reason }) }); tracing::debug!( @@ -452,6 +462,7 @@ fn single_node() -> RequestSettings { RequestSettings { retries: Some(0), ban_failed_address: Some(false), + timeout: Some(REQUEST_DEADLINE), ..RequestSettings::default() } } @@ -1163,7 +1174,7 @@ mod tests { assert!(is_unresolved(&outcome.verdict)); assert_eq!( started.elapsed(), - REQUEST_DEADLINE * MAX_SUBMISSIONS_PER_PROBE as u32 + REQUEST_BACKSTOP * MAX_SUBMISSIONS_PER_PROBE as u32 ); } diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 338e3f074b9..28df0ce16f3 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -55,10 +55,8 @@ //! completion can be handled before the wallet's own step to that tip (the //! two come from separate tasks): that step, when it follows within //! [`COMPLETED_AHEAD_TTL`], is the wallet's arrival at the tip and runs the -//! pass, however large it is. The step may also never come (the fan-out -//! can drop one), so a completion reads a wallet it has a lower height for, -//! when that wallet has no pass of its own: the read runs the pass if the -//! wallet is at the tip and confines it to forced roots if not. A +//! pass, however large it is. A step that never comes (the fan-out can +//! drop one) is not made up for: the pass then runs at the next block. A //! root is probed every block for [`EVERY_BLOCK_WINDOW`] blocks, counted //! from its first probe by a height-driven pass while the wallet follows the //! tip (a forced pass does not start it), and every [`SLOW_INTERVAL`] blocks @@ -1366,11 +1364,6 @@ struct PendingPass { /// results asked for it, or a catch-up overtook it — probes only the /// roots those results forced and starts no windows. anchor_at: Option, - /// A completed tip the wallet is asked about (see - /// `Command::SyncComplete`): the read anchors the pass there when it - /// finds the wallet at that tip and `anchor_at` does not hold. Such a - /// pass is not dropped for being below the wallet's wait. - asked_tip: Option, /// Roots a retry forces (see [`NO_ANSWER_RETRY`]): probed only if they /// are still roots — one whose parent has since shown up unsettled is /// not resolved to that parent. @@ -1394,8 +1387,6 @@ struct Run { /// Where this pass starts windows; `None` confines it to forced roots /// (see [`PendingPass::anchor_at`]). anchor_at: Option, - /// See [`PendingPass::asked_tip`]. - asked_tip: Option, /// What `Uncertain` results forced it to probe: carried over to the next /// pass when a rewind stops it. forced: HashSet, @@ -1465,16 +1456,6 @@ struct WalletEntry { /// one. Kept with the time it was handled: it stands for /// [`COMPLETED_AHEAD_TTL`] only (see there). completed_ahead: Option<(u32, Instant)>, - /// The completed tip the wallet was last asked its own height for (see - /// `Command::SyncComplete`): one read per tip, however often it - /// completes. - asked_at: Option, - /// The height a pass that follows the tip found the wallet at when that - /// was above the last height step handled: the wallet's height for - /// retries and for judging the next step, until the steps catch up. Kept - /// apart from `height`, which is the cursor of the steps delivered: one - /// still on its way below this is late, not a rewind. - found_at: Option, } impl WalletEntry { @@ -1705,8 +1686,6 @@ impl Actor { entry.retry_series = None; entry.followed_pass_at = None; entry.yielded_at = None; - entry.asked_at = None; - entry.found_at = None; if let Some(run) = entry.run.take() { run.abort.abort(); if !run.forced.is_empty() { @@ -1727,12 +1706,6 @@ impl Actor { let Some(entry) = self.wallets.get_mut(&wallet_id) else { return; }; - // A pass has already found the wallet above this step: the - // step is late, and what follows from it is done. - if entry.found_at.is_some_and(|found| height < found) { - return; - } - let previous = previous.max(entry.found_at.take()); let completed = entry .completed_ahead .filter(|(_, at)| at.elapsed() <= COMPLETED_AHEAD_TTL) @@ -1859,8 +1832,6 @@ impl Actor { entry.retry_series = None; entry.followed_pass_at = None; entry.yielded_at = None; - entry.asked_at = None; - entry.found_at = None; if !enabled { entry.pending = None; if let Some(run) = entry.run.take() { @@ -1884,9 +1855,8 @@ impl Actor { Command::SyncComplete { tip } => { // Every wallet at the tip follows it now: a launch's or a // reconnect's catch-up skipped its pass, and the next block - // can be minutes away. A wallet the actor has far below is - // asked where it is (below): its windows must not start at a - // stale height. One + // can be minutes away. A wallet still far below (rescanning) + // waits: its windows must not start at a stale height. One // without a step since launch reads its own height. While // probing is off this does nothing (queue_followed_pass): the // next completion, after the next block, covers it. @@ -1897,23 +1867,11 @@ impl Actor { // since launch: its first step, when it lands at this tip, is // then its arrival and keeps the pass started here. let now = Instant::now(); - let enabled = self.enabled; - let mut below_tip: Vec = Vec::new(); - for (wallet_id, entry) in self.wallets.iter_mut() { + for entry in self.wallets.values_mut() { let below = entry .height .is_none_or(|height| height < tip && !near_tip(height, tip)); entry.completed_ahead = below.then_some((tip, now)); - // Asked once per tip, and not while idle. - if below - && enabled - && entry.height.is_some() - && entry.asked_at != Some(tip) - && !entry.wait_until.is_some_and(|until| tip < until) - { - entry.asked_at = Some(tip); - below_tip.push(*wallet_id); - } } let at_tip: Vec<(WalletId, u32)> = self .wallets @@ -1927,24 +1885,6 @@ impl Actor { for (wallet_id, height) in at_tip { self.queue_followed_pass(wallet_id, height); } - // The height the actor has for a wallet below the tip may be - // old because its step was lost, not late (the fan-out can - // drop one). Ask the wallet itself: a pass anchored at the - // tip, which its read confines to forced roots when the - // wallet really is below, and whose height the actor takes - // when it is not (see `JobDone::Read`). Behind a pass under - // way; a pass already queued as following keeps its anchor, - // and the tip is asked beside it (`PendingPass::asked_tip`). - for wallet_id in below_tip { - let Some(entry) = self.wallets.get_mut(&wallet_id) else { - continue; - }; - let known = entry.height.unwrap_or(tip); - let pending = entry.pending.get_or_insert_with(PendingPass::default); - pending.height = pending.height.max(known); - pending.asked_tip = Some(tip); - self.start(wallet_id); - } } Command::WalletAdded(wallet_id) => { // A fresh start: whatever was kept under this id (a same-id @@ -2097,13 +2037,12 @@ impl Actor { return; }; let anchor_at = pending.anchor_at; - let asked_tip = pending.asked_tip; let forced = pending.forced.clone(); let retried = pending.retried.clone(); let to_read: HashSet = pending.forced.union(&pending.retried).copied().collect(); // Walk the records if the pass may probe by schedule; one confined to // forced roots with none of them held has nothing to read for. - let walk = Arc::new(AtomicBool::new(anchor_at.is_some() || asked_tip.is_some())); + let walk = Arc::new(AtomicBool::new(anchor_at.is_some())); self.next_run += 1; let run = self.next_run; let source = Arc::clone(&self.source); @@ -2123,7 +2062,6 @@ impl Actor { settled: HashSet::new(), woken: false, anchor_at, - asked_tip, forced, retried, walk, @@ -2240,32 +2178,19 @@ impl Actor { // A request queued before any height was reported // carries 0; the wallet's own synced height is better. let height = height.max(read.synced_height); - // A pass that follows the tip found the wallet there: - // that is the wallet's height, also when the step to - // it never reached the actor. Retries and the next - // step are judged against it. - let synced = read.synced_height; - let fits = - |at: u32| synced >= at && synced <= at.saturating_add(CATCH_UP_STEP); let Some(current) = entry.run.as_mut() else { return; // matched by id above }; // The wallet is not where this pass was queued: far // ahead (a catch-up whose height step has not arrived // yet) or behind (a rewind for a rescan). Either way a - // catch-up — confine the pass to forced roots. Unless - // a completion asked about a tip and the wallet is - // there: then that tip is the pass's anchor. - if !current.anchor_at.is_some_and(fits) { - current.anchor_at = current.asked_tip.filter(|at| fits(*at)); - } - let follows = current.anchor_at.is_some(); - if follows && entry.height.is_none_or(|known| known < synced) { - entry.found_at = Some(synced); + // catch-up — confine the pass to forced roots. + if current.anchor_at.is_some_and(|at| { + read.synced_height < at + || read.synced_height > at.saturating_add(CATCH_UP_STEP) + }) { + current.anchor_at = None; } - let Some(current) = entry.run.as_mut() else { - return; - }; // What an event settled since the read began is not // the pass's any more. let views: Vec = read @@ -2366,11 +2291,7 @@ impl Actor { } // Still at that height: a new block's pass probes the // unanswered roots anyway (due at the next height). - if entry - .height - .max(entry.found_at) - .is_some_and(|now| now != height) - { + if entry.height.is_some_and(|now| now != height) { return; } tracing::info!( @@ -2493,7 +2414,6 @@ impl Actor { if let Some(pending) = entry.pending.as_mut() { if !stays(pending.anchor_at) { pending.anchor_at = None; - pending.asked_tip = None; // A rewind replays lower heights: never run above them. pending.height = height; if pending.forced.is_empty() { @@ -2513,7 +2433,6 @@ impl Actor { return; } run.anchor_at = None; - run.asked_tip = None; run.walk.store(false, Ordering::SeqCst); if let Some(probing) = run.probing.as_mut() { probing.drop_scheduled(); @@ -2679,7 +2598,7 @@ impl Actor { }; entry.run = None; if let (Some(pending), Some(until)) = (&entry.pending, entry.wait_until) { - if pending.forced.is_empty() && pending.asked_tip.is_none() && pending.height < until { + if pending.forced.is_empty() && pending.height < until { entry.pending = None; } } @@ -4402,9 +4321,8 @@ mod tests { assert_eq!(probe.probed(), vec![txid(1)]); } - /// A wallet still rescanning far below the tip gets no probe from a - /// completion: it is read once for that tip, found below, and its - /// windows do not start at a stale height. + /// A wallet still rescanning far below the tip gets no pass from a + /// completion: its windows must not start at a stale height. #[tokio::test] async fn should_not_probe_a_wallet_far_below_a_completed_tip() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); @@ -4811,9 +4729,9 @@ mod tests { } /// Sync events and wallet events come from separate tasks: a completion - /// can be handled before the height step it follows, and that step can - /// be lost. The completion asks the wallet where it is and runs the pass - /// at the tip; the step, when it comes, adds nothing. + /// can be handled before the height step it follows. The step that then + /// brings the wallet to that tip runs the pass the completion could not, + /// instead of waiting for the next block as a catch-up step does. #[tokio::test] async fn should_probe_at_the_tip_when_the_completion_overtook_the_height_step() { let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); @@ -4828,14 +4746,16 @@ mod tests { .lock() .expect("synced") .insert(wallet(), 150); - // The completion asks the wallet itself, whatever height the actor - // has for it: the pass runs even if that step never comes. rig.send(sync_complete(150)).await; - assert_eq!(probe.probed().len(), 1); + assert_eq!( + probe.probed().len(), + 0, + "the actor still has the wallet at 100" + ); rig.height(wallet(), 150).await; - assert_eq!(probe.probed().len(), 1, "the step adds no second probe"); + assert_eq!(probe.probed().len(), 1); assert_eq!( rig.actor.state.schedules[&(wallet(), txid(1))].window_start, Some(150) @@ -5332,130 +5252,6 @@ mod tests { assert!(start.due.iter().all(|root| root.forced)); } - /// The pass a completion ran for a wallet whose step was lost leaves the - /// actor with the wallet's real height: a probe no node answered there - /// is retried, not dropped as belonging to another height. - #[tokio::test(start_paused = true)] - async fn should_retry_at_the_height_a_completion_found_the_wallet_at() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unreachable_verdict())])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.height(wallet(), 100).await; - rig.source - .synced - .lock() - .expect("synced") - .insert(wallet(), 150); - - rig.send(sync_complete(150)).await; - - assert_eq!(rig.actor.wallets[&wallet()].found_at, Some(150)); - assert_eq!(probe.probed().len(), 1 + MAX_NO_ANSWER_RETRIES as usize); - } - - /// The height a completion's pass found the wallet at is not the cursor - /// of its height steps: a step still on its way below it is late, not a - /// rewind, and must not clear what that pass published. - #[tokio::test] - async fn should_not_take_a_late_step_below_a_found_height_for_a_rewind() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), ProbeVerdict::Accepted)])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.height(wallet(), 100).await; - rig.source - .synced - .lock() - .expect("synced") - .insert(wallet(), 150); - rig.send(sync_complete(150)).await; - assert_eq!( - rig.sent(), - vec![ResolverEvent::Verdict(txid(1), ProbeVerdict::Accepted)] - ); - - rig.height(wallet(), 120).await; // late - rig.height(wallet(), 150).await; // the step the completion overtook - - assert!(rig.sent().is_empty(), "nothing cleared, nothing repeated"); - assert_eq!(probe.probed().len(), 1); - assert_eq!( - rig.actor.state.schedules[&(wallet(), txid(1))].window_start, - Some(150) - ); - assert_eq!(rig.actor.wallets[&wallet()].found_at, None); - } - - /// Turning probing off and on starts the completion's question over: a - /// tip asked in the earlier period is asked again. - #[tokio::test] - async fn should_ask_a_completed_tip_again_after_probing_was_switched_off_and_on() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.height(wallet(), 100).await; - rig.send(sync_complete(150)).await; // asked: the wallet is at 100 - assert_eq!(probe.probed().len(), 0); - - rig.send(Command::SetEnabled(false)).await; - rig.source - .synced - .lock() - .expect("synced") - .insert(wallet(), 150); - rig.send(Command::SetEnabled(true)).await; - rig.send(sync_complete(150)).await; - - assert_eq!(probe.probed().len(), 1); - } - - /// A completion's question queued behind a pass under way survives that - /// pass's end: the wallet is read again and the pass at the tip runs. - #[tokio::test] - async fn should_ask_the_wallet_after_the_pass_a_completion_found_under_way() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.height(wallet(), 100).await; - - // A followed pass at 101: its read is back, its probe is out. - rig.handle(Command::Height { - wallet_id: wallet(), - height: 101, - }); - let read = rig.actor.jobs.join_next_with_id().await.expect("the read"); - rig.actor.joined(read); - // The wallet reaches 150 with no step delivered; the sync completes. - rig.source - .synced - .lock() - .expect("synced") - .insert(wallet(), 150); - rig.handle(sync_complete(150)); - rig.settle().await; - - assert_eq!(rig.actor.wallets[&wallet()].found_at, Some(150)); - assert_eq!(rig.actor.wallets[&wallet()].followed_pass_at, Some(150)); - } - - /// A wallet the actor has below a completed tip is read once for that - /// tip, however often the sync completes there. - #[tokio::test] - async fn should_ask_a_wallet_below_the_tip_once_per_completed_tip() { - let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); - let mut rig = enabled(probe.clone()).await; - rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); - rig.height(wallet(), 100).await; - - rig.send(sync_complete(150)).await; - let asked = rig.walks(); - rig.send(sync_complete(150)).await; - assert_eq!(rig.walks(), asked, "a second read for the same tip"); - - rig.send(sync_complete(151)).await; - assert_eq!(rig.walks(), asked + 1); - assert_eq!(probe.probed().len(), 0, "the wallet is still below"); - } - /// A completion can also overtake a wallet's first step since launch. /// The completion reads the wallet at the tip and starts its pass; the /// step that follows is the wallet's arrival there, not a catch-up step @@ -5971,7 +5767,6 @@ mod tests { settled: HashSet::new(), woken: false, anchor_at: Some(101), - asked_tip: None, forced: HashSet::new(), retried: HashSet::new(), walk: Arc::new(AtomicBool::new(false)), From 7c1775067d80a60f823ad2e99119560e49908e88 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Tue, 13 Oct 2026 19:56:40 +0300 Subject: [PATCH 167/169] test(platform-wallet): assert the probe request's own deadline in its settings Co-Authored-By: Claude Opus 5.5 --- packages/rs-platform-wallet/src/broadcast_probe.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcast_probe.rs b/packages/rs-platform-wallet/src/broadcast_probe.rs index 33dfeb03e62..0c604fe13ee 100644 --- a/packages/rs-platform-wallet/src/broadcast_probe.rs +++ b/packages/rs-platform-wallet/src/broadcast_probe.rs @@ -1031,13 +1031,16 @@ mod tests { ); } - /// A probe never bans the address it reached, and asks one node per - /// request. + /// A probe never bans the address it reached, asks one node per request, + /// and gives each request its own deadline, inside the probe's backstop, + /// so the SDK's timeout (or its absence) does not decide it. #[test] - fn should_probe_without_banning_and_without_retries() { + fn should_probe_without_banning_without_retries_and_with_its_own_deadline() { let settings = single_node(); assert_eq!(settings.ban_failed_address, Some(false)); assert_eq!(settings.retries, Some(0)); + assert_eq!(settings.timeout, Some(REQUEST_DEADLINE)); + assert!(REQUEST_DEADLINE < REQUEST_BACKSTOP); } /// A failed lookup: gRPC `NOT_FOUND` is the node not knowing the txid, From 641675f34a71a5bad829dab702ac46cf161a3c92 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 14 Oct 2026 11:51:10 +0300 Subject: [PATCH 168/169] test(platform-wallet): pin how the DAPI broadcaster reads each answer The mapping from a DAPI broadcast answer to a send outcome moves into `dapi_broadcast_outcome`, unchanged, and gets a table test: `AlreadyExists` (-27) is a delivered send; a Core refusal in either DAPI's codes, another Core error, a transport failure, a deadline and running out of nodes after an attempt all stay `MaybeSent`. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/broadcaster.rs | 136 +++++++++++++++--- 1 file changed, 115 insertions(+), 21 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcaster.rs b/packages/rs-platform-wallet/src/broadcaster.rs index bd55cb4808f..c88d4b0bf1a 100644 --- a/packages/rs-platform-wallet/src/broadcaster.rs +++ b/packages/rs-platform-wallet/src/broadcaster.rs @@ -20,7 +20,7 @@ use std::time::Duration; use async_trait::async_trait; use dash_sdk::dapi_client::transport::TransportError; use dash_sdk::dapi_client::{DapiClientError, DapiRequestExecutor, IntoInner, RequestSettings}; -use dash_sdk::dapi_grpc::core::v0::BroadcastTransactionRequest; +use dash_sdk::dapi_grpc::core::v0::{BroadcastTransactionRequest, BroadcastTransactionResponse}; use dash_sdk::dapi_grpc::tonic::Code; use dash_spv::BroadcastResult; use dashcore::{consensus, Transaction, Txid}; @@ -131,31 +131,42 @@ impl TransactionBroadcaster for DapiBroadcaster { bypass_limits: false, }; - // A node that answers `AlreadyExists` (-27) has the transaction in a block, - // so that is success. Every other failure stays `MaybeSent`, including - // Core's own rejections: one node's `missingorspent` is also what a - // lagging node, a node that has not seen our unconfirmed parent, and - // a mined transaction whose outputs are all spent produce (see - // `broadcast_probe`), and the `Rejected` contract demands proof that - // the transaction never entered the network — which one answer cannot - // give. `sdk.execute` also retries transport failures across nodes, so - // an earlier attempt may have delivered it before this error. - match self + let result = self .sdk .execute(request, RequestSettings::default()) .await - .into_inner() + .into_inner(); + dapi_broadcast_outcome(transaction.txid(), result) + } +} + +/// What a DAPI broadcast answer means for the send. +/// +/// A node that answers `AlreadyExists` (Core's -27: the transaction is +/// mined and at least one of its outputs is still unspent) has it, so that +/// is success, whatever the message says. Every other +/// failure stays `MaybeSent`, including Core's own rejections: one node's +/// `missingorspent` is also what a lagging node, a node that has not seen +/// our unconfirmed parent, and a mined transaction whose outputs are all +/// spent produce (see `broadcast_probe`), and the `Rejected` contract +/// demands proof that the transaction never entered the network, which +/// one answer cannot give. `sdk.execute` also retries transport failures +/// across nodes, so an earlier attempt may have delivered it before this +/// error. +fn dapi_broadcast_outcome( + txid: Txid, + result: Result, +) -> Result { + match result { + Ok(_response) => Ok(txid), + Err(DapiClientError::Transport(TransportError::Grpc(status))) + if status.code() == Code::AlreadyExists => { - Ok(_response) => Ok(transaction.txid()), - Err(DapiClientError::Transport(TransportError::Grpc(status))) - if status.code() == Code::AlreadyExists => - { - Ok(transaction.txid()) - } - Err(error) => Err(BroadcastError::MaybeSent { - reason: format!("DAPI broadcast failed: {error}"), - }), + Ok(txid) } + Err(error) => Err(BroadcastError::MaybeSent { + reason: format!("DAPI broadcast failed: {error}"), + }), } } @@ -289,6 +300,8 @@ mod tests { use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Mutex; + use dash_sdk::dapi_grpc::tonic::Status; + use super::*; struct AcceptanceSpy { @@ -385,6 +398,87 @@ mod tests { } } + /// A DAPI node that already has the transaction (`AlreadyExists`) is a + /// delivered send. A Core refusal stays `MaybeSent`, never `Rejected`, + /// whichever code DAPI gives it (-25: `InvalidArgument` from JS-DAPI, + /// `FailedPrecondition` from rs-dapi; -26: `FailedPrecondition` from + /// both; any other Core error: `Internal` from JS-DAPI, `Unavailable` + /// from rs-dapi): a mined + /// transaction whose outputs are all spent is refused the same way. So + /// does a transport failure, and running out of nodes after an attempt + /// that may have delivered it. + #[test] + fn should_take_already_exists_as_sent_and_every_other_dapi_failure_as_maybe_sent() { + let txid = transaction().txid(); + let grpc = |code, message: &str| { + Err(DapiClientError::Transport(TransportError::Grpc( + Status::new(code, message), + ))) + }; + + let sent = [ + ("accepted", Ok(BroadcastTransactionResponse::default())), + ( + "already in chain", + grpc(Code::AlreadyExists, "Transaction already in chain: "), + ), + ]; + for (label, answer) in sent { + let outcome = dapi_broadcast_outcome(txid, answer); + assert!( + matches!(outcome, Ok(sent) if sent == txid), + "{label}: got {outcome:?}" + ); + } + + let maybe_sent = [ + ( + "JS-DAPI refusal", + grpc( + Code::InvalidArgument, + "invalid transaction: bad-txns-inputs-missingorspent", + ), + ), + ( + "rs-dapi refusal", + grpc( + Code::FailedPrecondition, + "Transaction is rejected: bad-txns-inputs-missingorspent", + ), + ), + ( + "lock conflict", + grpc( + Code::FailedPrecondition, + "Transaction is rejected: tx-txlock-conflict", + ), + ), + ( + "JS-DAPI rethrown Core error", + grpc(Code::Internal, "Internal error"), + ), + ( + "rs-dapi other Core error", + grpc(Code::Unavailable, "still warming up"), + ), + ("transport", grpc(Code::Unavailable, "connection refused")), + ("deadline", grpc(Code::DeadlineExceeded, "timeout")), + ( + "out of nodes after an attempt", + Err(DapiClientError::NoAvailableAddressesToRetry(Box::new( + TransportError::Grpc(Status::new(Code::Unavailable, "connection refused")), + ))), + ), + ]; + for (label, answer) in maybe_sent { + let outcome = dapi_broadcast_outcome(txid, answer); + assert!( + matches!(outcome, Err(BroadcastError::MaybeSent { .. })), + "{label}: got {outcome:?}" + ); + } + } + /// The readiness gate the resume paths depend on has to reach the SPV /// channel, budget intact. Callers can only observe readiness through /// `TransactionBroadcaster`, so a `SpvBroadcaster` that silently kept From 3468910a4a7ef4743c047e72b61bf12ae99e3917 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Wed, 14 Oct 2026 12:54:41 +0300 Subject: [PATCH 169/169] fix(platform-wallet): forget a send a forced read finds gone, and look its echo up again A forced pass whose read finds none of the txids it asked for in the wallet (`Some(None)`) only ended. The sends had left the wallet with no event naming them, yet their verdicts stayed shown, the wallet kept its report priority, and an echo lookup from before could still accept a send the wallet no longer holds. Now that read clears them at the pass height, drops their schedules and the wallet's report entry, and looks their echoes up again, so another wallet still holding the send keeps its acceptance. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/core/broadcast_resolver.rs | 89 ++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs index 28df0ce16f3..914dade0a56 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast_resolver.rs @@ -2173,7 +2173,32 @@ impl Actor { ); self.registration_gone(wallet_id); } - Some(None) => self.end_run(wallet_id), + Some(None) => { + // The wallet has no record of any txid this pass + // asked for: they left it with no event naming them. + // Forget them here as `Settled` would, but look their + // echoes up again rather than drop them: another + // wallet may still hold the send. + let Some(run) = entry.run.take() else { + return; // matched by id above + }; + let asked: HashSet = + run.forced.union(&run.retried).copied().collect(); + forget_reported(&mut self.reported, &wallet_id, |txid| { + asked.contains(txid) + }); + for txid in &asked { + self.state.schedules.remove(&(wallet_id, *txid)); + } + let events = self.state.clear_where( + |wallet, txid| *wallet == wallet_id && asked.contains(txid), + Some(height), + "settled-or-left", + ); + deliver(&self.sink, events); + self.relook_echoes(|txid| asked.contains(txid)); + self.end_run(wallet_id); + } Some(Some(read)) => { // A request queued before any height was reported // carries 0; the wallet's own synced height is better. @@ -5971,6 +5996,68 @@ mod tests { assert!(rig.sent().is_empty(), "accepted after it was cleared"); } + /// The same when the read that finds the send gone is a forced one that + /// reads nothing (`Some(None)`): its verdict is cleared, and the lookup + /// from before accepts it only for a wallet that still holds it. + #[tokio::test] + async fn should_clear_a_send_a_forced_read_found_gone_and_keep_its_echo_for_the_others() { + let other: WalletId = [8u8; 32]; + for other_holds in [false, true] { + let probe = Arc::new(ScriptedProbe::new(&[(txid(1), unresolved())])); + let mut rig = enabled(probe).await; + rig.views(wallet(), vec![send(1, &[outpoint(90, 0)])]); + rig.follow(wallet(), 100).await; + if other_holds { + rig.views(other, vec![send(1, &[outpoint(90, 0)])]); + rig.follow(other, 100).await; + } + rig.send(Command::Uncertain(txid(1))).await; + rig.sent(); + + rig.handle(Command::Echoed(txid(1))); + let echo = rig.actor.jobs.join_next_with_id().await.expect("lookup"); + assert!(matches!(&echo, Ok((_, JobDone::EchoListed { .. })))); + // A new report forces a pass for the wallet still holding the + // send; the send leaves it before that pass reads. + rig.handle(Command::Uncertain(txid(1))); + let report = rig.actor.jobs.join_next_with_id().await.expect("report"); + rig.views(wallet(), Vec::new()); + rig.actor.joined(report); + rig.settle().await; + assert!( + rig.actor.wallets[&wallet()].run.is_none(), + "{other_holds}: the forced pass ended" + ); + + rig.actor.joined(echo); + rig.settle().await; + + let published = &rig.actor.state.published; + assert_eq!( + published.get(&(wallet(), txid(1))), + None, + "{other_holds}: cleared for the wallet it left" + ); + assert!( + !rig.actor + .reported + .get(&txid(1)) + .is_some_and(|holders| holders.contains(&wallet())), + "{other_holds}: no priority for a send the wallet lost" + ); + assert_eq!( + published.get(&(other, txid(1))), + other_holds.then_some(&ProbeVerdict::Accepted), + "{other_holds}: the echo still reaches the wallet holding it" + ); + let events = rig.sent(); + assert!( + events.contains(&ResolverEvent::Cleared(txid(1))), + "{other_holds}: got {events:?}" + ); + } + } + /// An echo's acceptance survives another wallet's registration or removal /// while its lookup is out: the lookup is made again, not dropped. #[tokio::test]