diff --git a/packages/rs-dpp/src/shielded/builder/mod.rs b/packages/rs-dpp/src/shielded/builder/mod.rs index 3651ecbf12b..eb1d9d91de2 100644 --- a/packages/rs-dpp/src/shielded/builder/mod.rs +++ b/packages/rs-dpp/src/shielded/builder/mod.rs @@ -47,7 +47,10 @@ mod token_shielded_transfer; mod token_unshield; mod unshield; -pub use self::shield::build_shield_transition; +pub use self::shield::{ + build_shield_transition, build_shield_transition_from_proved_bundle, prove_shield_bundle, + ProvedShieldBundle, +}; pub use document_token_payment::build_document_shielded_token_payment; pub use identity_create_from_shielded_pool::{ build_identity_create_from_shielded_pool_transition, IdentityCreateFromShieldedPoolBuildResult, @@ -56,7 +59,11 @@ pub use identity_top_up_from_shielded_pool::build_identity_top_up_from_shielded_ pub use shield_from_asset_lock::build_shield_from_asset_lock_transition; #[cfg(feature = "core_key_wallet")] pub use shield_from_asset_lock::build_shield_from_asset_lock_transition_with_signer; -pub use shield_from_identity::build_shield_from_identity_transition; +pub use shield_from_identity::{ + build_shield_from_identity_transition, + build_shield_from_identity_transition_from_proved_bundle, prove_shield_from_identity_bundle, + ProvedShieldFromIdentityBundle, +}; pub use shielded_transfer::build_shielded_transfer_transition; pub use shielded_withdrawal::build_shielded_withdrawal_transition; pub use token_burn_from_pool::build_token_burn_from_pool_transition; diff --git a/packages/rs-dpp/src/shielded/builder/shield.rs b/packages/rs-dpp/src/shielded/builder/shield.rs index b78ac3dd77a..0eb3db1861c 100644 --- a/packages/rs-dpp/src/shielded/builder/shield.rs +++ b/packages/rs-dpp/src/shielded/builder/shield.rs @@ -1,4 +1,4 @@ -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use crate::address_funds::AddressFundsFeeStrategy; use crate::address_funds::{OrchardAddress, PlatformAddress}; @@ -12,12 +12,132 @@ use crate::state_transition::StateTransition; use crate::ProtocolError; use platform_version::version::PlatformVersion; -use super::{build_output_only_bundle, serialize_authorized_bundle, OrchardProver}; +use super::{ + build_output_only_bundle, serialize_authorized_bundle, OrchardProver, SerializedBundle, +}; + +/// A Shield bundle that has been proved and bound to its funding addresses, but not yet wrapped +/// in a signed transition. Produced by [`prove_shield_bundle`], consumed by +/// [`build_shield_transition_from_proved_bundle`]. +pub struct ProvedShieldBundle { + bundle: SerializedBundle, + /// The funding binding the bundle's sighash committed to (see + /// [`shield_extra_sighash_data`]); empty at protocol versions that bind nothing. + extra_sighash_data: Vec, +} + +impl ProvedShieldBundle { + /// Whether this bundle binds exactly what consensus will re-derive for `inputs` at + /// `platform_version` — the same funding addresses under the same binding rules. A bundle + /// proved at a protocol version with a different binding (e.g. across the activation of + /// `credit_pool_bundle_binding`) must be proved again. + pub fn is_bound_for( + &self, + inputs: &BTreeMap, + platform_version: &PlatformVersion, + ) -> Result { + Ok(shield_extra_sighash_data(inputs, platform_version)? == self.extra_sighash_data) + } +} + +/// Proves the output-only Orchard bundle of a Shield transition. +/// +/// This is the CPU-heavy half of [`build_shield_transition`] (Halo 2 proof generation, seconds of +/// work) and does no I/O and no signing, so it can run on a blocking thread while the caller +/// fetches the input nonces. +/// +/// The bundle's binding signature commits to the funding addresses — the SET of input addresses, +/// via [`shield_extra_sighash_data`] — and never to their nonces or contributed amounts. The +/// nonces and amounts are committed later by the input witnesses, which sign the whole +/// transition in [`build_shield_transition_from_proved_bundle`]. +pub fn prove_shield_bundle( + recipient: &OrchardAddress, + shield_amount: u64, + input_addresses: BTreeSet, + prover: &P, + memo: [u8; 36], + sender_ovk: Option, + platform_version: &PlatformVersion, +) -> Result { + // `shield_extra_sighash_data` digests only the map's keys (in key order); the nonce and + // amount values are deliberately not part of the binding, so placeholders bind exactly the + // bytes the real inputs map binds (pinned by + // `test_proved_bundle_binding_ignores_nonces_and_amounts`). + let binding_inputs: BTreeMap = input_addresses + .iter() + .map(|address| (*address, (0, 0))) + .collect(); + // Bound to the funding addresses, so nobody else can re-wrap the proved bundle; empty at + // protocol versions whose verifier predates the binding. + let extra_sighash_data = shield_extra_sighash_data(&binding_inputs, platform_version)?; + + // Shield (Type 15) never pads with anonymity-set fillers — only the + // Type 18 ShieldFromAssetLock pool-seeding path does (`dummy_outputs`). + let bundle = build_output_only_bundle( + recipient, + shield_amount, + memo, + sender_ovk, + 0, + &extra_sighash_data, + prover, + )?; + Ok(ProvedShieldBundle { + bundle: serialize_authorized_bundle(&bundle), + extra_sighash_data, + }) +} + +/// Wraps a [`ProvedShieldBundle`] into a signed Shield state transition: attaches the inputs with +/// their nonces and amounts and signs every input witness over the transition's signable bytes. +/// +/// The binding consensus will re-derive from `inputs` at `platform_version` must equal the one +/// the bundle was proved with (see [`ProvedShieldBundle::is_bound_for`]); otherwise this errors +/// instead of producing a transition consensus would reject. +pub async fn build_shield_transition_from_proved_bundle>( + proved: ProvedShieldBundle, + inputs: BTreeMap, + fee_strategy: AddressFundsFeeStrategy, + signer: &S, + user_fee_increase: UserFeeIncrease, + platform_version: &PlatformVersion, +) -> Result { + if fee_strategy.is_empty() { + return Err(ProtocolError::ShieldedBuildError( + "fee_strategy must have at least one step".to_string(), + )); + } + if !proved.is_bound_for(&inputs, platform_version)? { + return Err(ProtocolError::ShieldedBuildError( + "shield inputs do not match the binding the bundle was proved with (different \ + funding addresses or protocol version)" + .to_string(), + )); + } + let sb = proved.bundle; + + ShieldTransition::try_from_bundle_with_signer( + inputs, + sb.actions, + sb.value_balance.unsigned_abs(), + sb.anchor, + sb.proof, + sb.binding_signature, + fee_strategy, + signer, + user_fee_increase, + platform_version, + ) + .await +} /// Builds a Shield state transition (transparent platform addresses -> shielded pool). /// /// Constructs an output-only Orchard bundle (no spends), proves it, signs the /// transparent input witnesses, and returns a ready-to-broadcast `StateTransition`. +/// Equivalent to [`prove_shield_bundle`] followed by +/// [`build_shield_transition_from_proved_bundle`]; use those two directly to prove while the +/// input nonces are still being fetched. /// /// # Parameters /// - `recipient` - Orchard address to receive the shielded note @@ -47,36 +167,26 @@ pub async fn build_shield_transition, P: OrchardProve sender_ovk: Option, platform_version: &PlatformVersion, ) -> Result { + // Checked before proving so a malformed request fails without paying for the proof. if fee_strategy.is_empty() { return Err(ProtocolError::ShieldedBuildError( "fee_strategy must have at least one step".to_string(), )); } - // Bound to the funding addresses, so nobody else can re-wrap the proved bundle; empty at - // protocol versions whose verifier predates the binding. - let extra_sighash_data = shield_extra_sighash_data(&inputs, platform_version)?; - - // Shield (Type 15) never pads with anonymity-set fillers — only the - // Type 18 ShieldFromAssetLock pool-seeding path does (`dummy_outputs`). - let bundle = build_output_only_bundle( + let proved = prove_shield_bundle( recipient, shield_amount, + inputs.keys().copied().collect(), + prover, memo, sender_ovk, - 0, - &extra_sighash_data, - prover, + platform_version, )?; - let sb = serialize_authorized_bundle(&bundle); - ShieldTransition::try_from_bundle_with_signer( + build_shield_transition_from_proved_bundle( + proved, inputs, - sb.actions, - sb.value_balance.unsigned_abs(), - sb.anchor, - sb.proof, - sb.binding_signature, fee_strategy, signer, user_fee_increase, @@ -287,4 +397,93 @@ mod tests { result.err() ); } + + #[test] + fn test_proved_bundle_binding_ignores_nonces_and_amounts() { + // `prove_shield_bundle` binds placeholder nonces/amounts; that is only sound because the + // Shield binding digests the input address set alone. Pin it at every version that binds. + let mut real = BTreeMap::new(); + real.insert(PlatformAddress::P2pkh([1u8; 20]), (17u32, 100_000u64)); + real.insert(PlatformAddress::P2sh([2u8; 20]), (u32::MAX, 1u64)); + let placeholders: BTreeMap = + real.keys().map(|address| (*address, (0, 0))).collect(); + for platform_version in [PlatformVersion::first(), PlatformVersion::latest()] { + assert_eq!( + shield_extra_sighash_data(&real, platform_version).unwrap(), + shield_extra_sighash_data(&placeholders, platform_version).unwrap(), + ); + } + // And at the latest version the binding is not vacuous: it depends on the address set. + let latest = PlatformVersion::latest(); + assert!(!shield_extra_sighash_data(&real, latest).unwrap().is_empty()); + let mut other = real.clone(); + other.insert(PlatformAddress::P2pkh([3u8; 20]), (0, 0)); + assert_ne!( + shield_extra_sighash_data(&real, latest).unwrap(), + shield_extra_sighash_data(&other, latest).unwrap(), + ); + } + + #[tokio::test] + async fn test_proved_bundle_split_path_and_binding_mismatch() { + let recipient = test_orchard_address(); + let latest = PlatformVersion::latest(); + let a = PlatformAddress::P2pkh([1u8; 20]); + let b = PlatformAddress::P2pkh([2u8; 20]); + let fee_strategy = vec![AddressFundsFeeStrategyStep::DeductFromInput(0)]; + let inputs_for = |addrs: &[PlatformAddress]| -> BTreeMap<_, _> { + addrs + .iter() + .map(|addr| (*addr, (7u32, 100_000u64))) + .collect() + }; + let prove = |platform_version| { + prove_shield_bundle( + &recipient, + 50_000, + [a].into_iter().collect(), + &TestProver, + [0u8; 36], + None, + platform_version, + ) + .expect("prove") + }; + + // Proved for {a} at the latest version: bound for exactly those inputs at that version. + let proved = prove(latest); + assert!(proved.is_bound_for(&inputs_for(&[a]), latest).unwrap()); + assert!(!proved.is_bound_for(&inputs_for(&[a, b]), latest).unwrap()); + assert!(!proved.is_bound_for(&inputs_for(&[b]), latest).unwrap()); + // A version whose binding rules differ (the first version binds nothing) must re-prove. + assert!(!proved + .is_bound_for(&inputs_for(&[a]), PlatformVersion::first()) + .unwrap()); + + // Wrapping it around other inputs is refused rather than built. + let err = build_shield_transition_from_proved_bundle( + proved, + inputs_for(&[a, b]), + fee_strategy.clone(), + &DummySigner, + 0, + latest, + ) + .await + .expect_err("mismatched inputs must be rejected"); + assert!(err.to_string().contains("do not match"), "{err}"); + + // Prove first, attach nonces afterwards (the wallet's overlap path). + let st = build_shield_transition_from_proved_bundle( + prove(latest), + inputs_for(&[a]), + fee_strategy, + &DummySigner, + 0, + latest, + ) + .await + .expect("assemble"); + assert!(matches!(st, StateTransition::Shield(_))); + } } diff --git a/packages/rs-dpp/src/shielded/builder/shield_from_identity.rs b/packages/rs-dpp/src/shielded/builder/shield_from_identity.rs index 37e2345bd3e..36d7686a4fd 100644 --- a/packages/rs-dpp/src/shielded/builder/shield_from_identity.rs +++ b/packages/rs-dpp/src/shielded/builder/shield_from_identity.rs @@ -2,7 +2,7 @@ use crate::address_funds::OrchardAddress; use crate::identity::accessors::IdentityGettersV0; use crate::identity::signer::Signer; use crate::identity::{Identity, IdentityPublicKey}; -use crate::prelude::{IdentityNonce, UserFeeIncrease}; +use crate::prelude::{Identifier, IdentityNonce, UserFeeIncrease}; use crate::shielded::shield_from_identity_extra_sighash_data; use crate::state_transition::shield_from_identity_transition::methods::ShieldFromIdentityTransitionMethodsV0; use crate::state_transition::shield_from_identity_transition::ShieldFromIdentityTransition; @@ -10,30 +10,54 @@ use crate::state_transition::StateTransition; use crate::ProtocolError; use platform_version::version::PlatformVersion; -use super::{build_output_only_bundle, serialize_authorized_bundle, OrchardProver}; +use super::{ + build_output_only_bundle, serialize_authorized_bundle, OrchardProver, SerializedBundle, +}; -/// Build a `ShieldFromIdentity` transition: prove an outputs-only Orchard bundle -/// paying `shield_amount` to `recipient`, then identity-sign it with a TRANSFER key. +/// A `ShieldFromIdentity` bundle that has been proved and bound to its funding identity, but not +/// yet wrapped in an identity-signed transition. Produced by +/// [`prove_shield_from_identity_bundle`], consumed by +/// [`build_shield_from_identity_transition_from_proved_bundle`]. +pub struct ProvedShieldFromIdentityBundle { + bundle: SerializedBundle, + /// The funding binding the bundle's sighash committed to (see + /// [`shield_from_identity_extra_sighash_data`]); empty at protocol versions that bind nothing. + extra_sighash_data: Vec, +} + +impl ProvedShieldFromIdentityBundle { + /// Whether this bundle binds exactly what consensus will re-derive for `identity_id` at + /// `platform_version`. A bundle proved at a protocol version with a different binding (e.g. + /// across the activation of `credit_pool_bundle_binding`) must be proved again. + pub fn is_bound_for( + &self, + identity_id: Identifier, + platform_version: &PlatformVersion, + ) -> Result { + Ok( + shield_from_identity_extra_sighash_data(&identity_id.to_buffer(), platform_version)? + == self.extra_sighash_data, + ) + } +} + +/// Proves the outputs-only Orchard bundle of a `ShieldFromIdentity` transition. /// -/// The identity nonce must already be the next nonce for `identity` (the SDK -/// fetches it); `amount + fee` is debited from the identity balance at execution. -#[allow(clippy::too_many_arguments)] -pub async fn build_shield_from_identity_transition< - S: Signer, - P: OrchardProver, ->( - identity: &Identity, +/// This is the CPU-heavy half of [`build_shield_from_identity_transition`] and does no I/O and no +/// signing, so it can run on a blocking thread while the caller fetches the identity nonce. The +/// bundle's binding signature commits to the funding identity id (see +/// [`shield_from_identity_extra_sighash_data`]), never to the nonce; the nonce is committed by +/// the identity signature over the whole transition in +/// [`build_shield_from_identity_transition_from_proved_bundle`]. +pub fn prove_shield_from_identity_bundle( + identity_id: Identifier, recipient: &OrchardAddress, shield_amount: u64, - nonce: IdentityNonce, - signer: &S, - signing_transfer_key_to_use: Option<&IdentityPublicKey>, - user_fee_increase: UserFeeIncrease, prover: &P, memo: [u8; 36], sender_ovk: Option, platform_version: &PlatformVersion, -) -> Result { +) -> Result { if shield_amount == 0 { return Err(ProtocolError::ShieldedBuildError( "shield amount must be greater than zero".to_string(), @@ -43,7 +67,7 @@ pub async fn build_shield_from_identity_transition< // Bound to the funding identity, so no other identity can sign the proved bundle into a // transition of its own. let extra_sighash_data = - shield_from_identity_extra_sighash_data(&identity.id().to_buffer(), platform_version)?; + shield_from_identity_extra_sighash_data(&identity_id.to_buffer(), platform_version)?; let bundle = build_output_only_bundle( recipient, shield_amount, @@ -53,7 +77,37 @@ pub async fn build_shield_from_identity_transition< &extra_sighash_data, prover, )?; - let sb = serialize_authorized_bundle(&bundle); + Ok(ProvedShieldFromIdentityBundle { + bundle: serialize_authorized_bundle(&bundle), + extra_sighash_data, + }) +} + +/// Wraps a [`ProvedShieldFromIdentityBundle`] into a `ShieldFromIdentity` transition signed by +/// `identity`'s TRANSFER key with `nonce`. +/// +/// The binding consensus will re-derive for `identity` at `platform_version` must equal the one +/// the bundle was proved with (see [`ProvedShieldFromIdentityBundle::is_bound_for`]); otherwise +/// this errors instead of producing a transition consensus would reject. +pub async fn build_shield_from_identity_transition_from_proved_bundle< + S: Signer, +>( + proved: ProvedShieldFromIdentityBundle, + identity: &Identity, + nonce: IdentityNonce, + signer: &S, + signing_transfer_key_to_use: Option<&IdentityPublicKey>, + user_fee_increase: UserFeeIncrease, + platform_version: &PlatformVersion, +) -> Result { + if !proved.is_bound_for(identity.id(), platform_version)? { + return Err(ProtocolError::ShieldedBuildError( + "identity does not match the binding the shield bundle was proved with (different \ + identity or protocol version)" + .to_string(), + )); + } + let sb = proved.bundle; ShieldFromIdentityTransition::try_from_bundle_with_identity_signer( identity, @@ -71,11 +125,58 @@ pub async fn build_shield_from_identity_transition< .await } +/// Build a `ShieldFromIdentity` transition: prove an outputs-only Orchard bundle +/// paying `shield_amount` to `recipient`, then identity-sign it with a TRANSFER key. +/// +/// The identity nonce must already be the next nonce for `identity` (the SDK +/// fetches it); `amount + fee` is debited from the identity balance at execution. +/// Equivalent to [`prove_shield_from_identity_bundle`] followed by +/// [`build_shield_from_identity_transition_from_proved_bundle`]; use those two directly to prove +/// while the nonce is still being fetched. +#[allow(clippy::too_many_arguments)] +pub async fn build_shield_from_identity_transition< + S: Signer, + P: OrchardProver, +>( + identity: &Identity, + recipient: &OrchardAddress, + shield_amount: u64, + nonce: IdentityNonce, + signer: &S, + signing_transfer_key_to_use: Option<&IdentityPublicKey>, + user_fee_increase: UserFeeIncrease, + prover: &P, + memo: [u8; 36], + sender_ovk: Option, + platform_version: &PlatformVersion, +) -> Result { + let proved = prove_shield_from_identity_bundle( + identity.id(), + recipient, + shield_amount, + prover, + memo, + sender_ovk, + platform_version, + )?; + + build_shield_from_identity_transition_from_proved_bundle( + proved, + identity, + nonce, + signer, + signing_transfer_key_to_use, + user_fee_increase, + platform_version, + ) + .await +} + #[cfg(test)] mod tests { use super::*; use crate::address_funds::AddressWitness; - use crate::identity::accessors::IdentityGettersV0; + use crate::identity::accessors::{IdentityGettersV0, IdentitySettersV0}; use crate::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; use crate::identity::{KeyType, Purpose, SecurityLevel}; use crate::shielded::builder::test_helpers::{test_orchard_address, TestProver}; @@ -190,4 +291,44 @@ mod tests { .expect("transfer key"); assert_eq!(t.signature_public_key_id(), key.id()); } + + #[tokio::test] + async fn test_proved_bundle_binding_rejects_other_identity_or_version() { + let identity = identity_with_transfer_key(); + let mut other = identity_with_transfer_key(); + other.set_id(Identifier::from([9u8; 32])); + assert_ne!(identity.id(), other.id()); + + let proved = prove_shield_from_identity_bundle( + identity.id(), + &test_orchard_address(), + 50_000, + &TestProver, + [0u8; 36], + None, + PlatformVersion::latest(), + ) + .expect("prove"); + assert!(proved + .is_bound_for(identity.id(), PlatformVersion::latest()) + .unwrap()); + assert!(!proved + .is_bound_for(other.id(), PlatformVersion::latest()) + .unwrap()); + assert!(!proved + .is_bound_for(identity.id(), PlatformVersion::first()) + .unwrap()); + let err = build_shield_from_identity_transition_from_proved_bundle( + proved, + &other, + 7, + &DummySigner, + None, + 0, + PlatformVersion::latest(), + ) + .await + .expect_err("a bundle proved for one identity must not be signed by another"); + assert!(err.to_string().contains("does not match"), "{err}"); + } } diff --git a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs index 40f1ba0e5e1..07396773129 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs @@ -27,7 +27,9 @@ use super::keys::{AccountViewingKeys, OrchardKeySet}; use super::note_selection::{ select_notes_for_denomination, select_notes_with_fee, ShieldedFeeKind, }; -use super::prover::{prove_on_blocking_thread, ShieldedProver}; +use super::prover::{ + fetch_while_proving, prove_on_blocking_thread, reprove_if_unbound, ShieldedProver, +}; use super::store::{PendingRedrive, ShieldedNote, ShieldedStore, SubwalletId}; use crate::broadcast_outcome::{broadcast_definitely_failed, carries_consensus_rejection}; use crate::changeset::{PlatformWalletChangeSet, ShieldedChangeSet}; @@ -35,7 +37,7 @@ use crate::error::{preserve_signer_key_unavailable_or, PlatformWalletError}; use crate::wallet::persister::WalletPersister; use crate::wallet::platform_wallet::WalletId; -use std::collections::{BTreeMap, HashSet}; +use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::sync::Arc; use dash_sdk::dapi_grpc::platform::v0::ResponseMetadata; @@ -60,9 +62,11 @@ use dpp::identity::{Identity, IdentityPublicKey}; use dpp::prelude::{Identifier, IdentityNonce}; use dpp::shielded::builder::{ build_identity_create_from_shielded_pool_transition, - build_identity_top_up_from_shielded_pool_transition, build_shield_from_identity_transition, - build_shield_transition, build_shielded_transfer_transition, - build_shielded_withdrawal_transition, build_unshield_transition, SpendableNote, + build_identity_top_up_from_shielded_pool_transition, + build_shield_from_identity_transition_from_proved_bundle, + build_shield_transition_from_proved_bundle, build_shielded_transfer_transition, + build_shielded_withdrawal_transition, build_unshield_transition, prove_shield_bundle, + prove_shield_from_identity_bundle, SpendableNote, }; use dpp::shielded::compute_minimum_shielded_fee; use dpp::state_transition::proof_result::StateTransitionProofResult; @@ -604,31 +608,6 @@ pub async fn shield_to, P: Shield .map_err(|e| PlatformWalletError::ShieldedBuildError(e.to_string()))?; let inputs = reserve_shield_fee_on_input_0(inputs, fee)?; - // Reuse rs-sdk's canonical fetch + hard balance check rather than - // re-implementing the fetch-and-validate dance. Unlike the old - // warn-and-proceed path, `fetch_inputs_with_nonce` errors with - // `AddressNotEnoughFundsError` when any input is short, so we fail - // before paying the ~30 s Halo 2 proof for a transition drive-abci - // would reject. It returns the *current* on-chain nonces; we apply - // a checked increment (the canonical `nonce_inc` uses an unchecked - // `+ 1`, which would wrap an address at u32::MAX into a replay - // nonce — bail loudly here instead). - use dash_sdk::platform::transition::fetch_inputs_with_nonce; - - let fetched = fetch_inputs_with_nonce(sdk, &inputs) - .await - .map_err(|error| map_shield_input_fetch_error(&error))?; - - let mut inputs_with_nonce: BTreeMap = BTreeMap::new(); - for (addr, (nonce, credits)) in fetched { - let next_nonce = nonce.checked_add(1).ok_or_else(|| { - PlatformWalletError::ShieldedBuildError(format!( - "input address nonce exhausted on platform: {addr:?}" - )) - })?; - inputs_with_nonce.insert(addr, (next_nonce, credits)); - } - let fee_strategy: AddressFundsFeeStrategy = vec![AddressFundsFeeStrategyStep::DeductFromInput(0)]; @@ -639,25 +618,89 @@ pub async fn shield_to, P: Shield "Shield: building proof" ); + // Prove the bundle while the input nonces are fetched. The bundle's + // binding signature commits only to the SET of input addresses (known + // here), never to their nonces or claimed amounts — those are committed + // by the input witnesses, which sign the assembled transition below, after + // both halves are done. See `dpp::shielded::shield_extra_sighash_data`. + let prove_at = |platform_version: &'static PlatformVersion| { + let input_addresses: BTreeSet = inputs.keys().copied().collect(); + let sender_ovk = keys.outgoing_viewing_key.clone(); + async move { + prove_on_blocking_thread(prover, move |prover| { + prove_shield_bundle( + &recipient_addr, + amount, + input_addresses, + prover, + memo, + // Encrypt the output under the account's own OVK so the + // wallet's shielded sync can recover this send (recipient, + // value, memo) from chain data alone. + Some(sender_ovk), + platform_version, + ) + }) + .await? + .map_err(|e| PlatformWalletError::ShieldedBuildError(e.to_string())) + } + }; + + // Reuse rs-sdk's canonical fetch + hard balance check rather than + // re-implementing the fetch-and-validate dance. Unlike the old + // warn-and-proceed path, `fetch_inputs_with_nonce` errors with + // `AddressNotEnoughFundsError` when any input is short; that error is + // returned as soon as the fetch fails (the concurrent proof is discarded), + // so a transition drive-abci would reject is never signed or broadcast. It + // returns the *current* on-chain nonces; we apply a checked increment (the + // canonical `nonce_inc` uses an unchecked `+ 1`, which would wrap an + // address at u32::MAX into a replay nonce — bail loudly here instead). + let fetch = async { + use dash_sdk::platform::transition::fetch_inputs_with_nonce; + + let fetched = fetch_inputs_with_nonce(sdk, &inputs) + .await + .map_err(|error| map_shield_input_fetch_error(&error))?; + + let mut inputs_with_nonce: BTreeMap = BTreeMap::new(); + for (addr, (nonce, credits)) in fetched { + let next_nonce = nonce.checked_add(1).ok_or_else(|| { + PlatformWalletError::ShieldedBuildError(format!( + "input address nonce exhausted on platform: {addr:?}" + )) + })?; + inputs_with_nonce.insert(addr, (next_nonce, credits)); + } + Ok(inputs_with_nonce) + }; + + let (inputs_with_nonce, proved) = fetch_while_proving(fetch, prove_at(sdk.version())).await?; + + // The fetch is a proved query and can advance the SDK's protocol + // version. Assemble at the version current now (as the sequential code + // did) and, in the rare case the binding rules changed under the proof + // (e.g. across `credit_pool_bundle_binding` activation), prove again. + let platform_version: &'static PlatformVersion = sdk.version(); + let proved = reprove_if_unbound( + proved, + |proved| { + proved + .is_bound_for(&inputs_with_nonce, platform_version) + .map_err(|e| PlatformWalletError::ShieldedBuildError(e.to_string())) + }, + || prove_at(platform_version), + ) + .await?; + let claimed_inputs = inputs_with_nonce.clone(); - // Await the shared, off-runtime proving-key preparation rather than - // blocking this worker on the key build. - let prover = prover.ready().await?; - let state_transition = build_shield_transition( - &recipient_addr, - amount, + let state_transition = build_shield_transition_from_proved_bundle( + proved, inputs_with_nonce, fee_strategy, signer, 0, // user_fee_increase - &prover, - memo, - // Encrypt the output under the account's own OVK so the wallet's - // shielded sync can recover this send (recipient, value, memo) - // from chain data alone. - Some(keys.outgoing_viewing_key.clone()), - sdk.version(), + platform_version, ) .await .map_err(|e| PlatformWalletError::ShieldedBuildError(e.to_string()))?; @@ -977,13 +1020,6 @@ pub(in crate::wallet) async fn shield_from_identity_to< other => other, }; - let nonce = sdk - .get_identity_nonce(identity_id, true, None) - .await - .map_err(|e| { - PlatformWalletError::ShieldedBuildError(format!("fetch identity nonce: {e}")) - })?; - info!( account, credits = amount, @@ -992,21 +1028,62 @@ pub(in crate::wallet) async fn shield_from_identity_to< "ShieldFromIdentity: building proof" ); - // Await the shared, off-runtime proving-key preparation rather than - // blocking this worker on the key build. - let prover = prover.ready().await?; - let state_transition = build_shield_from_identity_transition( + // Prove the bundle while the identity nonce is fetched. The bundle's + // binding signature commits to the funding identity id, never to the + // nonce; the nonce is committed by the identity signature over the + // assembled transition below, after both halves are done. See + // `dpp::shielded::shield_from_identity_extra_sighash_data`. + let prove_at = |platform_version: &'static PlatformVersion| { + let sender_ovk = keys.outgoing_viewing_key.clone(); + async move { + prove_on_blocking_thread(prover, move |prover| { + prove_shield_from_identity_bundle( + identity_id, + &recipient_addr, + amount, + prover, + memo, + Some(sender_ovk), + platform_version, + ) + }) + .await? + .map_err(map_shield_from_identity_build_error) + } + }; + let fetch = async { + sdk.get_identity_nonce(identity_id, true, None) + .await + .map_err(|e| { + PlatformWalletError::ShieldedBuildError(format!("fetch identity nonce: {e}")) + }) + }; + let (nonce, proved) = fetch_while_proving(fetch, prove_at(sdk.version())).await?; + + // The nonce fetch can advance the SDK's protocol version; assemble at + // the current version and re-prove if the binding rules changed under + // the proof (see `shield_to`). The fetched nonce is reused, never + // fetched again. + let platform_version: &'static PlatformVersion = sdk.version(); + let proved = reprove_if_unbound( + proved, + |proved| { + proved + .is_bound_for(identity_id, platform_version) + .map_err(map_shield_from_identity_build_error) + }, + || prove_at(platform_version), + ) + .await?; + + let state_transition = build_shield_from_identity_transition_from_proved_bundle( + proved, identity, - &recipient_addr, - amount, nonce, signer, None, 0, // user_fee_increase - &prover, - memo, - Some(keys.outgoing_viewing_key.clone()), - sdk.version(), + platform_version, ) .await .map_err(map_shield_from_identity_build_error)?; diff --git a/packages/rs-platform-wallet/src/wallet/shielded/prover.rs b/packages/rs-platform-wallet/src/wallet/shielded/prover.rs index 9db4dc53f39..fc2996f6a05 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/prover.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/prover.rs @@ -28,6 +28,7 @@ //! dedicated user-initiated-QoS rayon pool (see `on_proving_threads`); //! elsewhere it runs on the blocking thread. +use std::future::Future; use std::sync::OnceLock; use async_trait::async_trait; @@ -390,6 +391,58 @@ where } } +/// Run a network `fetch` concurrently with `prove` (proof generation) and +/// return both results. +/// +/// Error precedence matches the sequential fetch-then-prove order this +/// replaces: a `fetch` error is returned as soon as it is known, without +/// waiting for the proof (an in-flight blocking proof then finishes in the +/// background and is discarded); a `prove` error is returned only once +/// `fetch` has succeeded, so a fetch failure is never masked by a proof +/// failure. +pub(crate) async fn fetch_while_proving(fetch: FF, prove: PF) -> Result<(N, B), E> +where + FF: Future>, + PF: Future>, +{ + tokio::pin!(fetch); + tokio::pin!(prove); + tokio::select! { + biased; + fetched = &mut fetch => { + let fetched = fetched?; + Ok((fetched, prove.await?)) + } + proved = &mut prove => { + let fetched = fetch.await?; + Ok((fetched, proved?)) + } + } +} + +/// Keep `proved` if `is_bound` says it still matches the transition about +/// to be assembled; otherwise replace it with exactly one fresh `reprove`. +/// +/// Used after [`fetch_while_proving`]: the fetch can advance the SDK's +/// protocol version and with it the bundle-binding rules. Only the proof is +/// redone — the fetched values are the caller's and are never fetched again +/// (an identity-nonce fetch bumps the SDK's nonce cache). An `is_bound` or +/// `reprove` error is returned as is, before anything is signed. +pub(crate) async fn reprove_if_unbound( + proved: B, + is_bound: impl FnOnce(&B) -> Result, + reprove: impl FnOnce() -> PF, +) -> Result +where + PF: Future>, +{ + if is_bound(&proved)? { + Ok(proved) + } else { + reprove().await + } +} + #[cfg(test)] mod tests { use super::*; @@ -706,6 +759,227 @@ mod tests { eprintln!("real proving key prepared in {built:?} (8 concurrent callers)"); } + async fn timed(ms: u64, out: Result) -> Result { + tokio::time::sleep(Duration::from_millis(ms)).await; + out + } + + // The clock is paused, so `elapsed()` is exact: it shows both that the + // two futures overlap and whether the proof was awaited. + + #[tokio::test(start_paused = true)] + async fn fetch_while_proving_overlaps_and_returns_both() { + let start = tokio::time::Instant::now(); + let both = fetch_while_proving(timed(200, Ok(1u32)), timed(1000, Ok("proof"))).await; + assert_eq!(both.unwrap(), (1, "proof")); + // Concurrent: total = max(200, 1000), not the sum. + assert_eq!(start.elapsed(), Duration::from_millis(1000)); + } + + #[tokio::test(start_paused = true)] + async fn fetch_while_proving_fetch_error_returns_without_waiting_for_proof() { + let start = tokio::time::Instant::now(); + let err = fetch_while_proving( + timed::(200, Err("not enough funds")), + timed(1000, Ok("proof")), + ) + .await + .unwrap_err(); + assert_eq!(err, "not enough funds"); + assert_eq!(start.elapsed(), Duration::from_millis(200)); + } + + #[tokio::test(start_paused = true)] + async fn fetch_while_proving_fetch_error_wins_over_earlier_proof_error() { + let err = fetch_while_proving( + timed::(500, Err("nonce fetch failed")), + timed::(100, Err("proof failed")), + ) + .await + .unwrap_err(); + assert_eq!(err, "nonce fetch failed"); + } + + #[tokio::test(start_paused = true)] + async fn fetch_while_proving_proof_error_after_fetch_succeeds() { + let start = tokio::time::Instant::now(); + let err = fetch_while_proving(timed(500, Ok(1u32)), timed::(100, Err("proof failed"))) + .await + .unwrap_err(); + assert_eq!(err, "proof failed"); + assert_eq!(start.elapsed(), Duration::from_millis(500)); + } + + /// With a real off-runtime proof: the fetch only completes once the + /// proof has started, so this finishes only if the two genuinely + /// overlap (sequential execution would hit the timeout). Deterministic: + /// no wall-clock thresholds, so it can't flake when the proving pool is + /// busy with another test's real key build. + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn fetch_while_proving_with_blocking_proof_is_concurrent() { + test_cell!(CELL, BUILDS, 0); + let prover = CountingProver(&CELL); + let (started_tx, started_rx) = tokio::sync::oneshot::channel::<()>(); + let (finish_tx, finish_rx) = std::sync::mpsc::channel::<()>(); + let overlapped = tokio::time::timeout( + HANG_GUARD, + fetch_while_proving( + async move { + // The proof must already be running for the fetch to finish... + started_rx.await.expect("proof started"); + // ...and is still running: let it finish only now. + finish_tx.send(()).unwrap(); + Ok::<_, PlatformWalletError>("nonces") + }, + prove_on_blocking_thread(&prover, move |_| { + started_tx.send(()).unwrap(); + finish_rx.recv().expect("fetch finished first"); + "proof" + }), + ), + ) + .await + .expect("fetch and proof did not overlap"); + assert_eq!(overlapped.unwrap(), ("nonces", "proof")); + } + + #[tokio::test] + async fn reprove_if_unbound_keeps_a_still_bound_proof() { + let kept = reprove_if_unbound( + "first proof", + |p| { + assert_eq!(*p, "first proof"); + Ok::<_, &str>(true) + }, + || async { panic!("must not re-prove a still-bound proof") }, + ) + .await; + assert_eq!(kept.unwrap(), "first proof"); + } + + #[tokio::test] + async fn reprove_if_unbound_reproves_once_when_binding_changed() { + let reproved = AtomicUsize::new(0); + let out = reprove_if_unbound( + "stale proof", + |_| Ok::<_, &str>(false), + || { + reproved.fetch_add(1, Ordering::SeqCst); + async { Ok("fresh proof") } + }, + ) + .await; + assert_eq!(out.unwrap(), "fresh proof"); + assert_eq!(reproved.load(Ordering::SeqCst), 1); + } + + #[tokio::test] + async fn reprove_if_unbound_returns_reprove_error() { + let err = reprove_if_unbound( + "stale proof", + |_| Ok(false), + || async { Err::<&str, _>("re-prove failed") }, + ) + .await + .unwrap_err(); + assert_eq!(err, "re-prove failed"); + } + + #[tokio::test] + async fn reprove_if_unbound_returns_binding_check_error_without_reproving() { + let err = reprove_if_unbound( + "proof", + |_| Err("unknown binding version"), + || async { panic!("must not re-prove when the binding check fails") }, + ) + .await + .unwrap_err(); + assert_eq!(err, "unknown binding version"); + } + + /// Run `fut` while a 10 ms heartbeat ticks on the same runtime, and + /// return the largest observed gap between heartbeats (measurement + /// helper for the ignored real-proof test). + async fn heartbeat_max_gap_during(fut: F) -> Duration { + let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let stop_hb = stop.clone(); + let heartbeat = tokio::spawn(async move { + let mut last = Instant::now(); + let mut max_gap = Duration::ZERO; + while !stop_hb.load(Ordering::SeqCst) { + tokio::time::sleep(Duration::from_millis(10)).await; + let now = Instant::now(); + max_gap = max_gap.max(now - last); + last = now; + } + max_gap + }); + // Let the heartbeat start before the work does. + tokio::time::sleep(Duration::from_millis(20)).await; + fut.await; + // Let one more tick observe the end of any blocking stretch. + tokio::time::sleep(Duration::from_millis(20)).await; + stop.store(true, Ordering::SeqCst); + heartbeat.await.unwrap() + } + + /// Real proof, real key: heartbeat starvation of a current-thread + /// runtime while a Shield bundle is proved inline (the pre-change + /// behavior) versus through `prove_on_blocking_thread`. Ignored by + /// default (builds the real key); run in release for meaningful numbers: + /// `cargo test -p platform-wallet --features shielded --release --lib -- --ignored real_proof --nocapture` + #[tokio::test(flavor = "current_thread")] + #[ignore = "builds the real Halo 2 proving key and proves a bundle"] + async fn real_proof_heartbeat_inline_vs_blocking_pool() { + use dpp::address_funds::{OrchardAddress, PlatformAddress}; + use dpp::shielded::builder::prove_shield_bundle; + use dpp::version::PlatformVersion; + use grovedb_commitment_tree::{FullViewingKey, Scope, SpendingKey}; + + let start = Instant::now(); + CachedOrchardProver::prepare().await.unwrap(); + eprintln!("real proving key prepared in {:?}", start.elapsed()); + + let sk = SpendingKey::from_bytes([42u8; 32]).unwrap(); + let recipient = OrchardAddress::from_raw_bytes( + &FullViewingKey::from(&sk) + .address_at(0u32, Scope::External) + .to_raw_address_bytes(), + ) + .unwrap(); + let prove = move |prover: &ReadyOrchardProver| { + prove_shield_bundle( + &recipient, + 50_000, + [PlatformAddress::P2pkh([1u8; 20])].into_iter().collect(), + prover, + [0u8; 36], + None, + PlatformVersion::latest(), + ) + .map(|_| ()) + }; + let ready = CachedOrchardProver.ready().await.unwrap(); + + let t = Instant::now(); + let inline_gap = heartbeat_max_gap_during(async { prove(&ready).unwrap() }).await; + let inline_total = t.elapsed(); + let t = Instant::now(); + let offloaded_gap = heartbeat_max_gap_during(async { + prove_on_blocking_thread(&CachedOrchardProver, prove) + .await + .unwrap() + .unwrap() + }) + .await; + let offloaded_total = t.elapsed(); + eprintln!( + "real Shield proof: inline heartbeat max gap {inline_gap:?} (window {inline_total:?}); \ + blocking pool heartbeat max gap {offloaded_gap:?} (window {offloaded_total:?})" + ); + assert!(offloaded_gap < inline_gap); + } + /// Darwin QoS: even when the build is requested from a /// background-QoS thread, the build and its nested rayon work run at /// user-initiated QoS on the dedicated proving pool.