Skip to content

platform-wallet: dropping or cancelling a native PreparedDashPayPayment leaves its inputs and contact address reserved #5640

Open

Description

@romchornyi

Expected Behavior

When a native Rust caller drops a PreparedDashPayPayment without sending or abandoning it, its funding inputs are released and the contact's DIP-15 address is left untouched. When it cancels a send_prepared_payment future, the contact address is settled conservatively: Used if the transaction may have been dispatched, available if it never was.

Current Behavior

  1. PreparedDashPayPayment owns a signed transaction whose funding inputs are reserved, but it has no Drop behaviour. #[must_use] only warns. A value dropped on an early return or a cancelled confirmation future keeps its inputs reserved until key-wallet's reservation TTL expires.
    #[must_use = "a prepared payment holds its funding inputs until it is sent or abandoned"]
    pub struct PreparedDashPayPayment<B: TransactionBroadcaster + ?Sized> {
  2. send_prepared_payment reserves the contact address and stores the reservation before it broadcasts. It settles the address only after broadcast_finalized_transaction returns. If the future is dropped while the broadcast is awaited, the address stays AddressState::Reserved. The reservation sweep covers only Standard accounts, not the DashPay external account, so each such cancellation skips one contact index permanently.
    // The age guard, the in-broadcast pin and the owner-guarded release
    // on a definitive rejection all live in the finalized-broadcast path.
    let broadcast = core.broadcast_finalized_transaction(&signed).await;
    // Rejected, or refused as stale at dispatch: provably not on the
    // wire. Anything else went out or may have. On a rejection the
    // finalized-broadcast path has already released the inputs, so a
    // build that takes them before the release below sees this address
    // still reserved and pays the next index; this one becomes available
    // again, still inside the gap window.
    let outcome = match &broadcast {
    Err(
    PlatformWalletError::TransactionBroadcast(_)
    | PlatformWalletError::StaleReservation,
    ) => AddressOutcome::NotSent,
    _ => AddressOutcome::Sent,
    };
    self.settle_payment_address(
    generation,
    &from_identity_id,
    &to_contact_id,
    &payment_address,
    outcome,
    )
    .await;

    //! Every address change is bound to the wallet generation the payment was
    //! funded from: a payment that outlives its wallet (removed, or re-created
    //! under the same id) never touches the new generation's pool. A send
    //! dropped between reserve and settle leaves the address reserved, one
    //! skipped index: the reservation sweep covers only Standard accounts.

Hosts that use the FFI are not affected. platform_wallet_prepared_dashpay_payment_free abandons the payment, and the send runs to completion through block_on_worker, so a host cannot cancel it mid-broadcast. The gap applies only to native Rust consumers and future bindings.

Possible Solution

Give the native type an owner guard that releases on drop. A Drop impl cannot await the owner-guarded release, and spawning the release needs Send + 'static bounds that PreparedDashPayPayment<B> does not have. Options:

  • add those bounds to the struct and spawn the abandon from Drop; or
  • hand callers a guard bound to the wallet's runtime, with explicit send and abandon methods, whose Drop queues the release.

The cancellation case needs a guard around the broadcast that settles the address as Used, the ambiguous outcome, when it is dropped after dispatch.

Steps to Reproduce

Not reachable from the iOS or Android apps, which go through the FFI. In Rust: call prepare_payment and drop the result, then try to fund another payment from the same inputs before the TTL expires. Or wrap send_prepared_payment in a tokio::time::timeout shorter than the broadcast and read the contact account's address state afterwards.

Context

Follow-up to #5465, raised in its review by a maintainer and by the review bot. The PR's two-step contact payment (prepare → send / abandon) introduced the type.

Your Environment

platform feat/dashpay-payment-prepare @ dfbeb7d (PR #5465), base v5.1-dev.

🤖 Generated with Claude Code

Activity

  1. romchornyi commented on Oct 29, 2026

    @romchornyi
    ContributorAuthor

    Also in scope, from review of #5465: move the prepare lifecycle step out of the FFI wrapper and into rs-platform-wallet. Today platform_wallet_prepare_dashpay_payment takes the generation gate itself, checks that the generation is still current, and abandons the payment when it is not:

    };
    // The signer ran with no gate held, so the wallet may have been removed
    // (and its teardown sweep already run) meanwhile. Check liveness and
    // publish under this generation's gate, as `core_wallet_tx_builder_finalize`
    // does, so no handle to a removed generation is ever published.
    let (lifecycle, wallet_is_live) = runtime().block_on(async {
    let gate = wallet.core().generation_payment_guard().await;
    let live = wallet.core().is_current_generation().await;
    (gate, live)
    });
    if !wallet_is_live {
    drop(lifecycle);
    abandon_prepared(FFIPreparedDashPayPayment {
    wallet: std::sync::Arc::clone(&wallet),
    payment,
    });
    return PlatformWalletFFIResult::err(
    PlatformWalletFFIResultCode::NotFound,
    "wallet is no longer registered in the manager (removed or re-created while the \
    payment was being signed); no payment handle was published"
    .to_string(),
    );
    }
    let fee = payment.fee_duffs();
    let total = payment.total_duffs();
    let txid = txid_bytes(&payment.txid());
    let handle = PREPARED_DASHPAY_PAYMENT_STORAGE.insert(FFIPreparedDashPayPayment {
    wallet: std::sync::Arc::clone(&wallet),
    payment,
    });
    drop(lifecycle);
    unsafe {
    *out_payment = handle;

    Add a library operation (in wallet::core and prepared_payment) that does that and returns the payment together with a guard for the live generation. The handle registry and the FFI error conversion stay in the wrapper. The guard has to stay held through the registry insertion, so moving only the liveness check into prepare_payment is not enough: it would reopen the teardown race. This fits with the release guard above, since both shape how a PreparedDashPayPayment is owned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions