diff --git a/crypto/src/bls.rs b/crypto/src/bls.rs index c4fbd6980..55d7a397b 100644 --- a/crypto/src/bls.rs +++ b/crypto/src/bls.rs @@ -311,6 +311,20 @@ impl BlsSecretKey<'_> { } } + /// Signs a 32-byte message digest, written under the scheme. + /// + /// # Errors + /// + /// Returns `InvalidSecretKey` when the bytes are not a valid scalar. + pub fn sign_digest(self, digest: &[u8; 32]) -> Result { + let signature = match self.scheme { + BlsScheme::Legacy => self.scalar::()?.sign_digest(digest).to_bytes(), + BlsScheme::Modern => self.scalar::()?.sign_digest(digest).to_bytes(), + }; + + Ok(BlsSigBytes::from_bytes(signature)) + } + fn scalar(self) -> Result, BlsError> { PkcSecretKey::::from_bytes(self.bytes.as_bytes()).map_err(|_| BlsError::InvalidSecretKey) } @@ -571,6 +585,23 @@ mod tests { assert_eq!(signature.is_valid(), valid); } + #[test] + fn a_digest_signature_verifies_against_its_key_and_digest() { + let secret = BlsSkBytes::from_bytes([1; BLS_SK_LEN]); + for scheme in [BlsScheme::Modern, BlsScheme::Legacy] { + let key = secret.as_scheme(scheme); + let signature = key.sign_digest(&CHAINLOCK_BLOCK_HASH).unwrap(); + let public_key = key.public_key().unwrap().as_scheme(scheme); + assert!(public_key.verify(&CHAINLOCK_BLOCK_HASH, &signature).is_ok()); + assert!(public_key.verify(&[0; 32], &signature).is_err()); + } + let invalid = BlsSkBytes::from_bytes([0xff; BLS_SK_LEN]); + assert_eq!( + invalid.as_scheme(BlsScheme::Modern).sign_digest(&[0; 32]), + Err(BlsError::InvalidSecretKey) + ); + } + #[test] fn test_legacy_format_detection() { // Test the ability to detect and handle legacy format keys diff --git a/dash-spv/ARCHITECTURE.md b/dash-spv/ARCHITECTURE.md index 05ee3c2d1..6f8ff0ec5 100644 --- a/dash-spv/ARCHITECTURE.md +++ b/dash-spv/ARCHITECTURE.md @@ -1112,7 +1112,7 @@ sync// - Listens for InstantLock messages from network - Validates signatures -- Emits `InstantLockReceived` events +- Emits `InstantLockReceived` events carrying a `VerifiedInstantLock`; locks awaiting quorum data are queued without an event ##### `src/sync/mempool/` - Mempool Transaction Tracking @@ -1174,7 +1174,7 @@ The `seen_txids` map provides a 180-second deduplication window to handle the ca |-------|--------| | `SyncComplete` | Activate mempool relay on all connected peers (transitions to `Synced`) | | `BlockProcessed` | Remove confirmed txids from mempool state; immediately rebuild bloom filter if new addresses | -| `InstantLockReceived` | Mark transaction as IS-locked, or store in pending_is_locks if TX not yet received | +| `InstantLockReceived` | Mark transaction as IS-locked, or store in pending_is_locks if TX not yet received. The event carries only verified locks | | `PeerConnected` | Activate on new peer immediately if already synced | | `PeerDisconnected` | Remove peer; redistribute its queued txids to a random activated peer | | `PeersUpdated` with no addresses | All peers lost: call `stop_sync()`, transition to `WaitingForConnections` | diff --git a/dash-spv/src/client/queries.rs b/dash-spv/src/client/queries.rs index 9b53baf24..660271e3c 100644 --- a/dash-spv/src/client/queries.rs +++ b/dash-spv/src/client/queries.rs @@ -10,12 +10,14 @@ use crate::error::{Result, SpvError}; use crate::network::NetworkManager; use crate::sml_engine::MasternodeListEngine; use crate::storage::PersistentBlockHeaderStorage; +use crate::validation::{InstantLockVerificationError, VerifiedInstantLock}; use dashcore::sml::llmq_type::LLMQType; use dashcore::sml::masternode_list::MasternodeList; use dashcore::sml::quorum_entry::qualified_quorum_entry::QualifiedQuorumEntry; use dashcore::transaction::special_transaction::coinbase::CoinbasePayload; -use dashcore::QuorumHash; +use dashcore::{InstantLock, QuorumHash}; use key_wallet_manager::WalletInterface; +use std::result; use std::sync::Arc; use tokio::sync::RwLock; @@ -43,6 +45,24 @@ impl DashSpvClient { } } + /// Authenticates a lock using the client's synchronized quorum state. + /// + /// Returns `None` while the masternode engine or the lock's quorum is unavailable. + /// Malformed locks, and signatures a complete cycle of quorums refutes, return an + /// error. No network request or wallet mutation is made; callers may retry after + /// masternode sync advances. + /// Fetch this evidence before acquiring wallet or persistence write locks. + pub async fn verify_instant_lock( + &self, + instant_lock: &InstantLock, + ) -> result::Result, InstantLockVerificationError> { + let Some(engine) = &self.masternode_engine else { + return VerifiedInstantLock::verify::(instant_lock, None); + }; + let engine = engine.read().await; + VerifiedInstantLock::verify(instant_lock, Some(&*engine)) + } + /// The newest masternode list, `None` while masternode sync is off or has /// not built a list yet. pub async fn latest_masternode_list(&self) -> Option { diff --git a/dash-spv/src/lib.rs b/dash-spv/src/lib.rs index c6a9e8399..8dd47ab61 100644 --- a/dash-spv/src/lib.rs +++ b/dash-spv/src/lib.rs @@ -93,6 +93,7 @@ pub use sync::{ }; pub use tracing::level_filters::LevelFilter; pub use types::{FilterMatch, ValidationMode}; +pub use validation::{InstantLockVerificationError, VerifiedInstantLock}; // Re-export commonly used dashcore types pub use dashcore::{Address, BlockHash, Network, OutPoint, QuorumHash, ScriptBuf}; diff --git a/dash-spv/src/network/costs.rs b/dash-spv/src/network/costs.rs index 19b26ecae..94f04920e 100644 --- a/dash-spv/src/network/costs.rs +++ b/dash-spv/src/network/costs.rs @@ -90,9 +90,7 @@ impl Reply { NetworkMessage::CFHeaders(m) => Reply::CfHeaders(m.stop_hash), NetworkMessage::Block(block) => Reply::Block(block.block_hash()), NetworkMessage::Tx(tx) => Reply::Tx(tx.txid()), - NetworkMessage::ISLock(lock) => Reply::InstantLock(InstantSendLockHash::from_raw_hash( - sha256d::Hash::hash(&encode::serialize(lock)), - )), + NetworkMessage::ISLock(lock) => Reply::InstantLock(lock.hash()), NetworkMessage::CLSig(lock) => Reply::ChainLock(ChainLockHash::from_raw_hash( sha256d::Hash::hash(&encode::serialize(lock)), )), diff --git a/dash-spv/src/sml_engine.rs b/dash-spv/src/sml_engine.rs index de42485d7..808ccff95 100644 --- a/dash-spv/src/sml_engine.rs +++ b/dash-spv/src/sml_engine.rs @@ -56,6 +56,48 @@ impl QRInfoFeedResult { } } +/// A stored cycle whose quorums may sign this network's InstantSend locks. +#[derive(Clone, Copy, Debug)] +pub(crate) struct InstantSendCycle<'a> { + /// The cycle's quorums, by quorum index. + pub(crate) quorums: &'a BTreeMap, + /// Whether no later QRInfo can replace the cycle's quorums, so it can refute a lock. + pub(crate) complete: bool, +} + +/// `true` when the cycle holds a `Verified` quorum of `isd_type` for every active slot. +fn fills_every_slot(quorums: &BTreeMap, isd_type: LLMQType) -> bool { + quorums.len() == isd_type.active_quorum_count() as usize + && quorums.values().all(|quorum| { + quorum.verified == LLMQEntryVerificationStatus::Verified + && quorum.quorum_entry.llmq_type == isd_type + }) +} + +/// Whether a stored cycle is complete, or `None` when one of its quorums is unverified or +/// cannot sign IS locks: of type `isd_type`, or any rotating type when `reads_served_type`. +/// Storage stops merging into a cycle only once it fills every slot of `isd_type`. +fn instant_send_cycle_completeness( + quorums: &BTreeMap, + isd_type: LLMQType, + reads_served_type: bool, +) -> Option { + let signs = |llmq_type: LLMQType| { + if reads_served_type { + llmq_type.is_rotating_quorum_type() + } else { + llmq_type == isd_type + } + }; + if quorums.values().any(|quorum| { + quorum.verified != LLMQEntryVerificationStatus::Verified + || !signs(quorum.quorum_entry.llmq_type) + }) { + return None; + } + Some(fills_every_slot(quorums, isd_type)) +} + /// Why the engine did not take a diff or a QRInfo. #[derive(Debug, thiserror::Error)] pub enum FeedError { @@ -391,12 +433,31 @@ impl MasternodeListEngine { self.masternode_lists.last_key_value().map(|(_, list)| list) } - /// The rotated quorums of the cycle `cycle_hash`, by quorum index. - pub fn rotated_quorums_of_cycle( + /// The cycle `cycle_hash` when every quorum of it is verified and of a type that + /// signs this network's InstantSend locks. + pub(crate) fn instant_send_cycle( &self, cycle_hash: &BlockHash, - ) -> Option<&BTreeMap> { - self.rotated_quorums_per_cycle.get(cycle_hash) + ) -> Option> { + let quorums = self.rotated_quorums_per_cycle.get(cycle_hash)?; + let complete = instant_send_cycle_completeness( + quorums, + self.network.isd_llmq_type(), + self.reads_served_rotation_type(), + )?; + Some(InstantSendCycle { + quorums, + complete, + }) + } + + #[cfg(feature = "test-utils")] + pub(crate) fn install_test_instant_lock_cycle( + &mut self, + cycle: BlockHash, + quorums: BTreeMap, + ) { + self.rotated_quorums_per_cycle.insert(cycle, quorums); } #[cfg(any(test, feature = "test-utils"))] @@ -702,14 +763,14 @@ impl MasternodeListEngine { Ok(()) } - /// `true` when the stored cycle has a `Verified` quorum for every active - /// slot. A partial cycle stays open so a later QRInfo can complete it. + /// `true` when the stored cycle has a `Verified` quorum of the configured type for + /// every active slot. A partial or foreign-type cycle stays open so a later QRInfo + /// can complete it. fn is_cycle_fully_verified(&self, cycle_hash: &BlockHash) -> bool { - let expected = self.network.isd_llmq_type().active_quorum_count() as usize; - self.rotated_quorums_per_cycle.get(cycle_hash).is_some_and(|existing| { - existing.len() == expected - && existing.values().all(|q| q.verified == LLMQEntryVerificationStatus::Verified) - }) + let isd_type = self.network.isd_llmq_type(); + self.rotated_quorums_per_cycle + .get(cycle_hash) + .is_some_and(|existing| fills_every_slot(existing, isd_type)) } /// Keeps the restored cycles whose quorums are all `Verified` and held by @@ -799,7 +860,7 @@ impl MasternodeListEngine { /// rotating type the peer serves. fn rotation_quorum_type(&self, served: &[QuorumEntry]) -> LLMQType { let configured = self.network.isd_llmq_type(); - if self.network != Network::Devnet || devnet_isd_type_override().is_some() { + if !self.reads_served_rotation_type() { return configured; } served @@ -809,6 +870,10 @@ impl MasternodeListEngine { .unwrap_or(configured) } + fn reads_served_rotation_type(&self) -> bool { + self.network == Network::Devnet && devnet_isd_type_override().is_none() + } + /// The hash of the newest cycle base in an active set. A failed DKG leaves /// an older cycle's quorum at its index, so a set can span cycles, and the /// newest one is the cycle the set is active for. @@ -1135,7 +1200,7 @@ pub(crate) mod test_support { use dashcore::consensus::deserialize; use dashcore::hash_types::QuorumVVecHash; use dashcore::sml::masternode_list_entry::qualified_masternode_list_entry::QualifiedMasternodeListEntry; - use dashcore::ProTxHash; + use dashcore::{InstantLock, ProTxHash}; pub(crate) type TestEngine = MasternodeListEngine; @@ -1171,6 +1236,11 @@ pub(crate) mod test_support { block_hashes.into_iter().map(|(height, block_hash)| (block_hash, height)).collect() } + /// An IS lock of the mainnet fixture's cycle, signed by its quorum at index 23. + pub(crate) fn mainnet_fixture_is_lock() -> InstantLock { + deserialize(&hex_conservative::decode_to_vec("01018d53e7997ead57409750942af0d5e0aafc06f852a9a52308f4781b6a8220298f00000000c6f9d8c63dd15937ea70aaddb7890daad42c91bf6818e2bf76d183d6f2d9215b4b5f84978fad9dde7ab52bdcc0674be891e9029cc1ef0cb01200000000000000a27c98836c4c04653ab81eb4e07ddfc2c8c2c1036b75247969c05a4f25451cd78913a971f1899d9f2bddec9cf8e0104004f72f20c2856453e5aa3bcd2a8200670ec28feda38f67cc400fc72ef1966956656ec0765478c9d16e9a9e470c07f9ed").unwrap()).unwrap() + } + /// A commitment with one signer and zeroed keys and signatures. pub(crate) fn quorum_entry( llmq_type: LLMQType, @@ -1396,9 +1466,11 @@ pub(crate) mod test_support { #[cfg(test)] mod tests { - use super::build_cycle_quorum_map; - use super::test_support::{fixture_heights, quorum_entry, TestEngine}; - use super::valid_coinbase_chain_lock; + use super::test_support::{fixture_heights, mainnet_fixture_is_lock, quorum_entry, TestEngine}; + use super::{ + build_cycle_quorum_map, fills_every_slot, instant_send_cycle_completeness, + valid_coinbase_chain_lock, + }; use super::{FeedError, MasternodeListEngine, QRInfoFeedResult}; use crate::error::{StorageError, StorageResult}; use crate::storage::{BlockHeaderStorage, BlockHeaderTip}; @@ -3312,4 +3384,25 @@ mod tests { restored.coinbase_chain_locks.get(&tip_height) ); } + + /// On a devnet the signing cycle can be of another rotating type than the configured + /// one. Holding as many quorums as the configured type has slots does not complete + /// it, so a lock it does not verify is deferred and a later QRInfo still merges in. + #[test] + fn should_not_complete_a_devnet_cycle_of_another_rotating_type() { + let lock = mainnet_fixture_is_lock(); + let cycle = + TestEngine::mainnet_fixture().rotated_quorums_per_cycle[&lock.cyclehash].clone(); + let served = LLMQType::Llmqtype60_75; + let configured = LLMQType::LlmqtypeDevnetDIP0024; + let slots = configured.active_quorum_count() as usize; + assert!(slots < cycle.len(), "the mainnet cycle has more slots than a devnet one"); + let partial: BTreeMap<_, _> = cycle.clone().into_iter().take(slots).collect(); + + assert!(!fills_every_slot(&partial, configured)); + assert_eq!(instant_send_cycle_completeness(&partial, configured, true), Some(false)); + assert_eq!(instant_send_cycle_completeness(&partial, configured, false), None); + assert_eq!(instant_send_cycle_completeness(&cycle, served, false), Some(true)); + assert_eq!(instant_send_cycle_completeness(&partial, served, false), Some(false)); + } } diff --git a/dash-spv/src/sync/events.rs b/dash-spv/src/sync/events.rs index 695042093..25726bc08 100644 --- a/dash-spv/src/sync/events.rs +++ b/dash-spv/src/sync/events.rs @@ -1,8 +1,8 @@ use crate::sml_engine::QRInfoFeedResult; use crate::sync::mempool::BroadcastResult; use crate::sync::ManagerIdentifier; +use crate::validation::VerifiedInstantLock; use dashcore::ephemerealdata::chain_lock::ChainLock; -use dashcore::ephemerealdata::instant_lock::InstantLock; use dashcore::{BlockHash, ScriptBuf, Txid}; use key_wallet_manager::{FilterMatchKey, WalletId}; use std::collections::{BTreeMap, BTreeSet}; @@ -153,15 +153,16 @@ pub enum SyncEvent { validated: bool, }, - /// InstantSend lock received and processed. + /// InstantSend lock verified against its signing quorum. + /// + /// A lock awaiting quorum data emits this event once it verifies. Malformed + /// locks and locks a complete, verified cycle refutes emit no event. /// /// Emitted by: `InstantSendManager` /// Consumed by: External listeners, mempool state updates InstantLockReceived { - /// The complete InstantLock data - instant_lock: InstantLock, - /// Whether the BLS signature was validated - validated: bool, + /// The verified lock + instant_lock: VerifiedInstantLock, }, /// The network-level outcome of a self-broadcast transaction was @@ -284,12 +285,7 @@ impl fmt::Display for SyncEvent { ), SyncEvent::InstantLockReceived { instant_lock, - validated, - } => write!( - f, - "InstantLockReceived(txid={}, validated={})", - instant_lock.txid, validated - ), + } => write!(f, "InstantLockReceived(txid={})", instant_lock.txid()), SyncEvent::TransactionBroadcastResult { txid, result, diff --git a/dash-spv/src/sync/instantsend/manager.rs b/dash-spv/src/sync/instantsend/manager.rs index 2b19acb24..0db7ec870 100644 --- a/dash-spv/src/sync/instantsend/manager.rs +++ b/dash-spv/src/sync/instantsend/manager.rs @@ -3,27 +3,28 @@ //! Handles InstantSendLock messages (islock) from the network. Validates locks //! when masternode data is available, queues them when not. -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::sync::Arc; use std::time::{Duration, Instant, SystemTime}; use crate::sml_engine::MasternodeListEngine; use crate::storage::BlockHeaderStorage; use dashcore::ephemerealdata::instant_lock::InstantLock; +use dashcore::hash_types::InstantSendLockHash; use dashcore::Txid; use tokio::sync::RwLock; use crate::error::SyncResult; use crate::sync::{InstantSendProgress, SyncEvent, SyncState}; -use crate::validation::{InstantLockStructureValidator, InstantLockValidator, Validator}; +use crate::validation::{InstantLockStructureValidator, Validator, VerifiedInstantLock}; /// Maximum number of pending InstantLocks awaiting validation. const MAX_PENDING_INSTANTLOCKS: usize = 500; -/// Maximum number of InstantLocks to cache. +/// Maximum number of verified txids to cache. const MAX_CACHE_SIZE: usize = 5000; -/// TTL for cached InstantLocks (1 hour). +/// TTL for cached verified txids (1 hour). const CACHE_TTL: Duration = Duration::from_secs(3600); /// How long a pending InstantLock is retained while awaiting the quorum data @@ -33,20 +34,13 @@ const CACHE_TTL: Duration = Duration::from_secs(3600); /// would overrun long before an hour elapsed during a fresh sync. const PENDING_TTL: Duration = Duration::from_secs(3600); -/// Entry in the InstantLock cache. -#[derive(Debug, Clone)] -pub struct InstantLockEntry { - /// When the InstantLock was received. - pub received_at: SystemTime, - /// Whether the BLS signature was validated. - pub validated: bool, -} - /// Pending InstantLock awaiting the quorum data required to verify it. #[derive(Debug, Clone)] pub(super) struct PendingInstantLock { /// The InstantLock data. instant_lock: InstantLock, + /// Hash of the serialized lock, which tells distinct locks of one txid apart. + hash: InstantSendLockHash, /// When the lock was first received, used to expire locks that never /// become verifiable. Uses a monotonic `Instant` so the elapsed-time TTL /// check is unaffected by wall-clock adjustments. @@ -54,6 +48,14 @@ pub(super) struct PendingInstantLock { } impl PendingInstantLock { + fn new(instant_lock: InstantLock, hash: InstantSendLockHash) -> Self { + Self { + instant_lock, + hash, + first_seen: Instant::now(), + } + } + /// Whether this pending lock has outlived [`PENDING_TTL`] as of `now`. /// /// Uses [`Instant::saturating_duration_since`], which clamps to zero @@ -97,14 +99,15 @@ impl PendingInstantLock { /// - Subscribes to ISLock messages from the network /// - Validates InstantLocks when masternode engine is available /// - Queues InstantLocks for later validation when engine not ready -/// - Emits InstantLockReceived events +/// - Emits InstantLockReceived events for verified locks pub struct InstantSendManager { /// Current progress of the manager. pub(super) progress: InstantSendProgress, /// Shared Masternode list engine. engine: Arc>>, - /// InstantLocks indexed by txid. - instantlocks: HashMap, + /// When each txid's lock verified. Only verified locks are cached, so locks that + /// wait for quorum data cannot evict them. + verified_txids: HashMap, /// Pending InstantLocks awaiting the quorum data required to verify them. pub(super) pending_instantlocks: Vec, /// Highest masternode-list height at which every pending InstantLock has @@ -124,7 +127,7 @@ impl InstantSendManager { Self { progress: InstantSendProgress::default(), engine, - instantlocks: HashMap::new(), + verified_txids: HashMap::new(), pending_instantlocks: Vec::new(), last_validated_engine_height: None, } @@ -139,60 +142,63 @@ impl InstantSendManager { tracing::info!("Processing InstantLock for txid {}", txid); - // Check for duplicates - if self.instantlocks.contains_key(&txid) { - tracing::debug!("Already have InstantLock for txid {}", txid); + // Only a verified lock settles its txid: an unverified one, possibly forged, + // must not shadow the genuine lock for the same transaction. + if self.verified_txids.contains_key(&txid) { + tracing::debug!("Already have a verified InstantLock for txid {}", txid); return Ok(vec![]); } - - // A malformed lock is dropped before it is queued or cached, so it can - // neither stay pending nor shadow the genuine lock for its txid. + // Checked before hashing, so an oversized lock is dropped without serializing it. if let Err(e) = InstantLockStructureValidator.validate(instantlock) { - tracing::warn!("Invalid InstantLock for txid {}: {}", txid, e); + tracing::warn!("Malformed InstantLock for txid {}: {}", txid, e); self.progress.add_invalid(1); return Ok(vec![]); } + let hash = instantlock.hash(); + if self.pending_instantlocks.iter().any(|pending| pending.hash == hash) { + tracing::debug!("The same InstantLock for txid {} is already pending", txid); + return Ok(vec![]); + } - // Until the engine knows the lock's cycle, or when its signature does not - // verify, the engine may only lack its quorum yet: the lock is kept pending. + // A rejected lock is dropped before it is queued or cached, so it can + // neither stay pending nor shadow the genuine lock for its txid. let engine = self.engine.read().await; - let result = engine - .rotated_quorums_of_cycle(&instantlock.cyclehash) - .map(|quorums| InstantLockValidator::new(quorums).validate(instantlock)); + let result = VerifiedInstantLock::verify(instantlock, Some(&*engine)); drop(engine); - let validated = match result { - Some(Ok(())) => true, - Some(Err(e)) => { - tracing::warn!("InstantLock for txid {} not verified yet: {}", txid, e); - false + let verified = match result { + Ok(Some(verified)) => Some(verified), + Ok(None) => { + tracing::debug!("InstantLock for txid {} awaits quorum data", txid); + None } - None => { - tracing::debug!("InstantLock for txid {} is of a cycle not known yet", txid); - false + Err(e) => { + tracing::warn!("Rejected InstantLock for txid {}: {}", txid, e); + self.progress.add_invalid(1); + return Ok(vec![]); } }; - if validated { + if verified.is_some() { self.progress.add_valid(1); + let queued = self.pending_instantlocks.len(); + self.pending_instantlocks.retain(|pending| pending.instant_lock.txid != txid); + if self.pending_instantlocks.len() < queued { + self.transition_synced_if_idle(); + } } else { - self.queue_pending(PendingInstantLock { - instant_lock: instantlock.clone(), - first_seen: Instant::now(), - }); - self.progress.update_pending(self.pending_instantlocks.len()); + self.queue_pending(PendingInstantLock::new(instantlock.clone(), hash)); + } + self.progress.update_pending(self.pending_instantlocks.len()); + if verified.is_some() { + self.store_verified(txid); } - // Store in cache - let entry = InstantLockEntry { - received_at: SystemTime::now(), - validated, - }; - self.store_instantlock(txid, entry); - - Ok(vec![SyncEvent::InstantLockReceived { - instant_lock: instantlock.clone(), - validated, - }]) + Ok(verified + .map(|instant_lock| SyncEvent::InstantLockReceived { + instant_lock, + }) + .into_iter() + .collect()) } /// Queue an InstantLock for later validation. @@ -210,16 +216,18 @@ impl InstantSendManager { self.pending_instantlocks.push(pending); } - /// Store an InstantLock in the cache. - fn store_instantlock(&mut self, txid: Txid, entry: InstantLockEntry) { - self.instantlocks.insert(txid, entry); + /// Records that `txid`'s lock verified, evicting the oldest record past [`MAX_CACHE_SIZE`]. + fn store_verified(&mut self, txid: Txid) { + self.verified_txids.insert(txid, SystemTime::now()); - // Enforce cache limit by removing oldest - if self.instantlocks.len() > MAX_CACHE_SIZE { - let oldest = - self.instantlocks.iter().min_by_key(|(_, e)| e.received_at).map(|(k, _)| *k); - if let Some(key) = oldest { - self.instantlocks.remove(&key); + if self.verified_txids.len() > MAX_CACHE_SIZE { + let oldest = self + .verified_txids + .iter() + .min_by_key(|(_, verified_at)| **verified_at) + .map(|(txid, _)| *txid); + if let Some(txid) = oldest { + self.verified_txids.remove(&txid); } } } @@ -232,7 +240,7 @@ impl InstantSendManager { /// Re-validate every pending InstantLock against the current engine state. /// /// Called both when a `MasternodeStateUpdated` event arrives and from `tick` - /// once the engine has advanced. Locks that verify are emitted as validated + /// once the engine has advanced. Locks that verify are emitted as /// `InstantLockReceived` events; locks that still can't be verified are /// re-queued until they either become verifiable or exceed [`PENDING_TTL`]. /// @@ -247,6 +255,8 @@ impl InstantSendManager { let pending = std::mem::take(&mut self.pending_instantlocks); let mut events = Vec::new(); + let mut validated_txids = HashSet::new(); + let mut rejected = 0; for pending_lock in pending { let txid = pending_lock.instant_lock.txid; @@ -256,29 +266,38 @@ impl InstantSendManager { self.progress.add_invalid(1); continue; } + if validated_txids.contains(&txid) { + continue; + } - let lock = &pending_lock.instant_lock; - let validated = - self.engine.read().await.rotated_quorums_of_cycle(&lock.cyclehash).is_some_and( - |quorums| InstantLockValidator::new(quorums).validate(lock).is_ok(), - ); - - if validated { - self.progress.add_valid(1); - // Update the cached entry - if let Some(entry) = self.instantlocks.get_mut(&txid) { - entry.validated = true; + let result = VerifiedInstantLock::verify( + &pending_lock.instant_lock, + Some(&*self.engine.read().await), + ); + match result { + Ok(Some(instant_lock)) => { + self.progress.add_valid(1); + validated_txids.insert(txid); + self.store_verified(txid); + events.push(SyncEvent::InstantLockReceived { + instant_lock, + }); } - events.push(SyncEvent::InstantLockReceived { - instant_lock: pending_lock.instant_lock.clone(), - validated: true, - }); - } else { - // Still can't validate, re-queue - self.queue_pending(pending_lock); + Ok(None) => self.queue_pending(pending_lock), + Err(_) => rejected += 1, } } + // Other locks for a txid that just verified are superseded, not pending. + self.pending_instantlocks + .retain(|pending| !validated_txids.contains(&pending.instant_lock.txid)); + if rejected > 0 { + tracing::warn!( + "Dropped {} pending InstantLocks refuted by their complete cycle", + rejected + ); + self.progress.add_invalid(rejected); + } self.last_validated_engine_height = engine_height; self.progress.update_pending(self.pending_instantlocks.len()); Ok(events) @@ -287,22 +306,17 @@ impl InstantSendManager { /// Prune old entries from the cache. pub(super) fn prune_old_entries(&mut self) { let now = SystemTime::now(); - self.instantlocks.retain(|_, entry| { - now.duration_since(entry.received_at).map(|d| d < CACHE_TTL).unwrap_or(true) + self.verified_txids.retain(|_, verified_at| { + now.duration_since(*verified_at).map(|d| d < CACHE_TTL).unwrap_or(true) }); } - /// Drop cached InstantLock entries that were never BLS-validated, and clear - /// the queued locks that fed them. + /// Drop the locks still waiting for quorum data. /// - /// Called on disconnect. The `instantlocks` cache is what - /// [`process_instantlock`](Self::process_instantlock) dedupes against, so if - /// an unvalidated entry survived a disconnect the same lock re-announced - /// after reconnect would be treated as a duplicate and never re-queued or - /// re-validated. Validated entries are kept — their locks are already final. - pub(super) fn clear_unvalidated_on_disconnect(&mut self) { + /// Called on disconnect, so a lock re-announced after reconnect is processed + /// fresh. Verified txids are kept, since their locks are already final. + pub(super) fn clear_pending_on_disconnect(&mut self) { self.pending_instantlocks.clear(); - self.instantlocks.retain(|_, entry| entry.validated); self.progress.update_pending(0); } @@ -356,7 +370,7 @@ impl std::fmt::Debug for InstantSendManager { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("InstantSendManager") .field("progress", &self.progress) - .field("cached", &self.instantlocks.len()) + .field("cached", &self.verified_txids.len()) .field("pending", &self.pending_instantlocks.len()) .finish() } @@ -365,13 +379,14 @@ impl std::fmt::Debug for InstantSendManager { mod tests { use super::*; use crate::network::{MessageType, NetworkManager}; - use crate::sml_engine::test_support::TestEngine; + use crate::sml_engine::test_support::{mainnet_fixture_is_lock, TestEngine}; use crate::sync::{ManagerIdentifier, SyncManager, SyncManagerProgress, SyncState}; use crate::test_utils::MockHeaderStorage; use dashcore::bls_sig_utils::BLSSignature; + use dashcore::constants::MAX_BLOCK_SIZE; use dashcore::hash_types::CycleHash; use dashcore::sml::masternode_list::MasternodeList; - use dashcore::{BlockHash, OutPoint}; + use dashcore::{BlockHash, Network, OutPoint}; use test_case::test_case; /// Insert an empty masternode list at `height` so the shared engine reports a @@ -394,10 +409,9 @@ mod tests { /// driven by the `now` passed to `tick_at`/`expire_pending`, not by /// back-dating this field — see [`past_ttl`]. fn fresh_pending(txid: Txid) -> PendingInstantLock { - PendingInstantLock { - instant_lock: create_test_instantlock(txid), - first_seen: Instant::now(), - } + let instant_lock = create_test_instantlock(txid); + let hash = instant_lock.hash(); + PendingInstantLock::new(instant_lock, hash) } /// An injected "now" far enough past a [`fresh_pending`] lock's `first_seen` @@ -471,19 +485,20 @@ mod tests { let islock1 = create_test_instantlock(txid); let islock2 = create_test_instantlock(txid); - // First should process - let events1 = manager.process_instantlock(&islock1).await.unwrap(); - assert_eq!(events1.len(), 1); + // First is queued until quorum data arrives + assert!(manager.process_instantlock(&islock1).await.unwrap().is_empty()); + assert_eq!(manager.pending_count(), 1); // Second should be ignored as duplicate - let events2 = manager.process_instantlock(&islock2).await.unwrap(); - assert_eq!(events2.len(), 0); + assert!(manager.process_instantlock(&islock2).await.unwrap().is_empty()); + assert_eq!(manager.pending_count(), 1); } #[test_case(|lock| lock.txid = Txid::all_zeros(); "null txid")] #[test_case(|lock| lock.signature = BLSSignature::from([0; 96]); "zero signature")] #[test_case(|lock| lock.inputs.clear(); "no inputs")] #[test_case(|lock| lock.inputs[0].txid = Txid::all_zeros(); "null input txid")] + #[test_case(|lock| lock.inputs = vec![lock.inputs[0]; MAX_BLOCK_SIZE as usize / 41 + 1]; "too many inputs")] #[tokio::test] async fn a_malformed_lock_of_an_unknown_cycle_is_dropped(malform: fn(&mut InstantLock)) { let mut manager = create_test_manager(); @@ -495,15 +510,185 @@ mod tests { assert!(events.is_empty(), "no InstantLockReceived for a malformed lock"); assert_eq!(manager.pending_count(), 0, "a malformed lock is not kept pending"); - assert!(manager.instantlocks.is_empty(), "a malformed lock is not cached"); + assert!(manager.verified_txids.is_empty(), "a malformed lock is not cached"); assert_eq!(manager.progress.invalid(), 1); // Not cached, so the well-formed lock for the transaction still gets in. - let events = manager.process_instantlock(&create_test_instantlock(txid)).await.unwrap(); - assert_eq!(events.len(), 1); + manager.process_instantlock(&create_test_instantlock(txid)).await.unwrap(); assert_eq!(manager.pending_count(), 1); } + /// A peer-forged, well-formed lock must not shadow the genuine lock for the + /// same txid: the mempool only acts on validated locks. + #[tokio::test] + async fn should_accept_genuine_lock_after_forged_lock_for_same_txid() { + let engine = TestEngine::mainnet_fixture(); + let mut manager = InstantSendManager::new(Arc::new(RwLock::new(engine))); + let genuine = mainnet_fixture_is_lock(); + let mut forged = genuine.clone(); + forged.cyclehash = CycleHash::from_byte_array([7; 32]); + + let events = manager.process_instantlock(&forged).await.unwrap(); + assert!(events.is_empty(), "a lock awaiting quorum data emits no event"); + + let events = manager.process_instantlock(&genuine).await.unwrap(); + assert!( + matches!( + events.as_slice(), + [SyncEvent::InstantLockReceived { instant_lock }] + if instant_lock.instant_lock() == &genuine + ), + "genuine lock was shadowed by the forged one: {events:?}" + ); + assert_eq!(manager.pending_count(), 0, "the forged lock no longer awaits quorum data"); + assert!(manager.process_instantlock(&genuine).await.unwrap().is_empty()); + assert!(manager.process_instantlock(&forged).await.unwrap().is_empty()); + } + + /// A lock its complete, verified cycle refutes is rejected, not kept pending. + #[tokio::test] + async fn should_reject_lock_refuted_by_its_complete_cycle() { + let engine = TestEngine::mainnet_fixture(); + let mut manager = InstantSendManager::new(Arc::new(RwLock::new(engine))); + let genuine = mainnet_fixture_is_lock(); + let mut forged = genuine.clone(); + forged.signature = BLSSignature::from([1; 96]); + + assert!(manager.process_instantlock(&forged).await.unwrap().is_empty()); + assert_eq!(manager.pending_count(), 0); + assert!(manager.verified_txids.is_empty(), "a rejected lock is not cached"); + assert_eq!(manager.progress.invalid(), 1); + + let events = manager.process_instantlock(&genuine).await.unwrap(); + assert!(matches!(events.as_slice(), [SyncEvent::InstantLockReceived { .. }])); + } + + /// Once one lock for a txid verifies, its other pending locks never emit. + #[tokio::test] + async fn should_drop_other_pending_locks_of_a_txid_once_one_verifies() { + let mut manager = + InstantSendManager::new(Arc::new(RwLock::new(TestEngine::empty(Network::Mainnet)))); + let genuine = mainnet_fixture_is_lock(); + let mut other_cycle = genuine.clone(); + other_cycle.cyclehash = CycleHash::from_byte_array([7; 32]); + for lock in [&other_cycle, &genuine] { + let events = manager.process_instantlock(lock).await.unwrap(); + assert!(events.is_empty(), "a lock awaiting quorum data emits no event"); + } + assert_eq!(manager.pending_count(), 2); + + *manager.engine.write().await = TestEngine::mainnet_fixture(); + let events = manager.validate_pending(Instant::now()).await.unwrap(); + assert!(matches!( + events.as_slice(), + [SyncEvent::InstantLockReceived { instant_lock }] + if instant_lock.instant_lock() == &genuine + )); + assert_eq!(manager.pending_count(), 0); + assert!(manager.validate_pending(Instant::now()).await.unwrap().is_empty()); + assert!(manager.process_instantlock(&other_cycle).await.unwrap().is_empty()); + } + + /// A pending lock refuted once its cycle completes is dropped on replay without + /// an event, and the drained queue lets the manager leave `Syncing`. + #[tokio::test] + async fn should_drop_pending_lock_refuted_once_its_cycle_completes() { + let mut manager = + InstantSendManager::new(Arc::new(RwLock::new(TestEngine::empty(Network::Mainnet)))); + manager.set_state(SyncState::Syncing); + let genuine = mainnet_fixture_is_lock(); + let mut forged = genuine.clone(); + forged.signature = BLSSignature::from([1; 96]); + let events = manager.process_instantlock(&forged).await.unwrap(); + assert!(events.is_empty(), "a lock awaiting quorum data emits no event"); + assert_eq!(manager.pending_count(), 1); + + *manager.engine.write().await = TestEngine::mainnet_fixture(); + let event = SyncEvent::MasternodeStateUpdated { + height: 1, + qr_info_result: None, + }; + let events = manager.handle_sync_event(&event, &no_op_network()).await.unwrap(); + + assert!(events.is_empty(), "a refuted lock emits no event: {events:?}"); + assert_eq!(manager.pending_count(), 0); + assert_eq!(manager.progress.invalid(), 1); + assert_eq!(manager.progress.valid(), 0); + assert_eq!(manager.state(), SyncState::Synced); + let events = manager.process_instantlock(&genuine).await.unwrap(); + assert!(matches!( + events.as_slice(), + [SyncEvent::InstantLockReceived { instant_lock }] + if instant_lock.instant_lock() == &genuine + )); + } + + /// A lock queued behind the one that verifies for its txid is superseded: + /// it emits nothing and leaves no pending entry. + #[tokio::test] + async fn should_supersede_locks_queued_after_the_verified_lock_of_a_txid() { + let mut manager = + InstantSendManager::new(Arc::new(RwLock::new(TestEngine::empty(Network::Mainnet)))); + let genuine = mainnet_fixture_is_lock(); + let mut forged = genuine.clone(); + forged.signature = BLSSignature::from([1; 96]); + for lock in [&genuine, &forged] { + manager.process_instantlock(lock).await.unwrap(); + } + assert_eq!(manager.pending_count(), 2); + + *manager.engine.write().await = TestEngine::mainnet_fixture(); + let events = manager.validate_pending(Instant::now()).await.unwrap(); + + assert!(matches!( + events.as_slice(), + [SyncEvent::InstantLockReceived { instant_lock }] + if instant_lock.instant_lock() == &genuine + )); + assert_eq!(manager.pending_count(), 0); + assert!(manager.process_instantlock(&forged).await.unwrap().is_empty()); + } + + #[tokio::test] + async fn should_leave_syncing_when_a_direct_verification_drains_the_pending_queue() { + let mut manager = + InstantSendManager::new(Arc::new(RwLock::new(TestEngine::mainnet_fixture()))); + manager.set_state(SyncState::Syncing); + let genuine = mainnet_fixture_is_lock(); + let mut unknown_cycle = genuine.clone(); + unknown_cycle.cyclehash = CycleHash::from_byte_array([7; 32]); + manager.process_instantlock(&unknown_cycle).await.unwrap(); + assert_eq!(manager.pending_count(), 1); + + manager.process_instantlock(&genuine).await.unwrap(); + + assert_eq!(manager.pending_count(), 0); + assert_eq!(manager.state(), SyncState::Synced); + } + + /// Locks awaiting quorum data settle nothing, so they must not push a verified + /// lock out of the cache: its next announcement would be verified and emitted again. + #[tokio::test] + async fn should_keep_a_verified_lock_settled_while_unverified_locks_fill_the_cache() { + let mut manager = + InstantSendManager::new(Arc::new(RwLock::new(TestEngine::mainnet_fixture()))); + let genuine = mainnet_fixture_is_lock(); + let events = manager.process_instantlock(&genuine).await.unwrap(); + assert!(matches!(events.as_slice(), [SyncEvent::InstantLockReceived { .. }])); + + for i in 0..MAX_CACHE_SIZE { + let mut txid = [0xee; 32]; + txid[0..4].copy_from_slice(&(i as u32).to_le_bytes()); + let mut unverifiable = create_test_instantlock(Txid::from_byte_array(txid)); + unverifiable.cyclehash = CycleHash::from_byte_array([7; 32]); + assert!(manager.process_instantlock(&unverifiable).await.unwrap().is_empty()); + } + + let events = manager.process_instantlock(&genuine).await.unwrap(); + assert!(events.is_empty(), "the verified lock was emitted a second time: {events:?}"); + assert_eq!(manager.progress.valid(), 1); + } + #[tokio::test] async fn test_instantsend_pending_queue() { let mut manager = create_test_manager(); @@ -544,29 +729,22 @@ mod tests { let islock = create_test_instantlock(txid); let _ = manager.process_instantlock(&islock).await.unwrap(); - // Should be retrievable by txid - assert!(manager.instantlocks.contains_key(&txid)); - - // Unknown txid - let unknown = Txid::from_byte_array([2u8; 32]); - assert!(!manager.instantlocks.contains_key(&unknown)); + // A lock awaiting quorum data is queued, not recorded as verified. + assert_eq!(manager.pending_count(), 1); + assert!(!manager.verified_txids.contains_key(&txid)); } #[tokio::test] async fn test_instantsend_cache_limit() { let mut manager = create_test_manager(); - // Add more than MAX_CACHE_SIZE instantlocks for i in 0..MAX_CACHE_SIZE + 10 { let mut bytes = [0u8; 32]; bytes[0..4].copy_from_slice(&(i as u32).to_le_bytes()); - let txid = Txid::from_byte_array(bytes); - let islock = create_test_instantlock(txid); - let _ = manager.process_instantlock(&islock).await.unwrap(); + manager.store_verified(Txid::from_byte_array(bytes)); } - // Should be capped at MAX_CACHE_SIZE - assert!(manager.instantlocks.len() <= MAX_CACHE_SIZE); + assert_eq!(manager.verified_txids.len(), MAX_CACHE_SIZE); } /// A queued InstantLock must be re-validated by `tick` as soon as the @@ -696,30 +874,23 @@ mod tests { } /// A lock re-announced with the same txid after a disconnect must be - /// processed fresh, not silently dropped as a duplicate. `on_disconnect` has - /// to evict the unvalidated cache entry that `process_instantlock` dedupes - /// against, otherwise the re-announced lock is never re-queued/re-validated. + /// processed fresh, not dropped as a duplicate of the cleared pending queue. #[tokio::test] async fn test_reannounce_after_disconnect_is_not_duplicate() { let mut manager = create_test_manager(); let txid = Txid::from_byte_array([5u8; 32]); - // First announcement: the empty engine can't verify it, so it is queued - // and cached as an unvalidated entry. - let events = manager.process_instantlock(&create_test_instantlock(txid)).await.unwrap(); - assert_eq!(events.len(), 1); + // First announcement: the empty engine can't verify it, so it is queued. + manager.process_instantlock(&create_test_instantlock(txid)).await.unwrap(); assert_eq!(manager.pending_count(), 1); - assert!(manager.instantlocks.contains_key(&txid)); - // Disconnect must clear the queue AND the unvalidated cache entry. + // Disconnect must clear the queue. manager.on_disconnect(); assert_eq!(manager.pending_count(), 0); - assert!(!manager.instantlocks.contains_key(&txid)); // Re-announcement of the same txid is processed fresh, not deduped away. - let events = manager.process_instantlock(&create_test_instantlock(txid)).await.unwrap(); - assert_eq!(events.len(), 1); + manager.process_instantlock(&create_test_instantlock(txid)).await.unwrap(); assert_eq!(manager.pending_count(), 1); } diff --git a/dash-spv/src/sync/instantsend/sync_manager.rs b/dash-spv/src/sync/instantsend/sync_manager.rs index 527cbb9da..3239a5ae3 100644 --- a/dash-spv/src/sync/instantsend/sync_manager.rs +++ b/dash-spv/src/sync/instantsend/sync_manager.rs @@ -30,11 +30,8 @@ impl SyncManager for InstantSendManager { } fn on_disconnect(&mut self) { - // Clear the queue *and* drop unvalidated cache entries. Leaving the - // latter behind would make `process_instantlock` treat a lock - // re-announced after reconnect as a duplicate, so it would never be - // re-queued or re-validated. - self.clear_unvalidated_on_disconnect(); + // Clear the queue so a lock re-announced after reconnect is processed fresh. + self.clear_pending_on_disconnect(); // Nothing is queued anymore, so the height at which we last validated is // meaningless; reset it so re-validation runs again after reconnect. self.last_validated_engine_height = None; diff --git a/dash-spv/src/sync/mempool/manager.rs b/dash-spv/src/sync/mempool/manager.rs index 69d667cec..d7f17a043 100644 --- a/dash-spv/src/sync/mempool/manager.rs +++ b/dash-spv/src/sync/mempool/manager.rs @@ -10,7 +10,6 @@ use std::net::SocketAddr; use std::sync::Arc; use std::time::{Duration, Instant}; -use dashcore::ephemerealdata::instant_lock::InstantLock; use dashcore::network::message::NetworkMessage; use dashcore::network::message_blockdata::Inventory; use dashcore::{Amount, Transaction, Txid}; @@ -26,6 +25,7 @@ use crate::network::NetworkManager; use crate::sync::mempool::MempoolProgress; use crate::sync::SyncEvent; use crate::types::UnconfirmedTransaction; +use crate::validation::VerifiedInstantLock; use key_wallet_manager::WalletInterface; /// Timeout for pruning expired mempool transactions (24 hours). @@ -66,7 +66,7 @@ pub(crate) struct MempoolManager { /// Connected peers and their activation state. pub(super) peers: HashMap>>, /// IS locks that arrived before their corresponding transaction, with insertion time. - pending_is_locks: HashMap, + pub(super) pending_is_locks: HashMap, /// Txids already downloaded, with download timestamp. /// Prevents duplicate downloads when multiple peers announce the same transactions. /// Entries expire after `SEEN_TXID_EXPIRY`. @@ -401,7 +401,8 @@ impl MempoolManager { self.progress.add_received(1); // Check for a pre-arrived IS lock before wallet processing consumes it - let pending_lock = self.pending_is_locks.remove(&txid).map(|(lock, _)| lock); + let pending_lock = + self.pending_is_locks.remove(&txid).map(|(lock, _)| lock.instant_lock().clone()); let is_locked = pending_lock.is_some(); let result = { @@ -606,9 +607,9 @@ impl MempoolManager { /// broadcast for the txid is promoted to accepted and dropped from tracking. pub(super) async fn process_instant_send( &mut self, - instant_lock: InstantLock, + instant_lock: VerifiedInstantLock, ) -> Vec { - let txid = instant_lock.txid; + let txid = instant_lock.txid(); let mut events = Vec::new(); if let Some(state) = self.broadcasts.remove(&txid) { if matches!(state.status, BroadcastStatus::Pending | BroadcastStatus::Uncertain) { @@ -640,7 +641,9 @@ impl MempoolManager { if let Some(lock) = instant_lock_opt { let mut wallet = self.wallet.write().await; if let Some(tx) = self.transactions.get(&txid) { - wallet.process_mempool_instant_send_lock(&tx.transaction, lock).await; + wallet + .process_mempool_instant_send_lock(&tx.transaction, lock.instant_lock().clone()) + .await; } } events @@ -853,12 +856,13 @@ impl fmt::Debug for MempoolManager { mod tests { use super::*; use dashcore::network::message::NetworkMessage; - use dashcore::{Address, BlockHash, Network, ScriptBuf, Transaction}; + use dashcore::{Address, BlockHash, InstantLock, Network, ScriptBuf, Transaction}; use key_wallet::transaction_checking::TransactionContext; use key_wallet_manager::test_utils::MockWallet; use crate::sync::SyncState; use crate::test_utils::{test_socket_address, MockNetworkManager}; + use test_case::test_case; fn dummy_instant_lock(txid: Txid) -> InstantLock { InstantLock { @@ -867,6 +871,10 @@ mod tests { } } + fn verified_lock(txid: Txid) -> VerifiedInstantLock { + VerifiedInstantLock::assume_verified(dummy_instant_lock(txid)) + } + fn rich_instant_lock(txid: Txid) -> InstantLock { InstantLock { txid, @@ -1334,7 +1342,7 @@ mod tests { UnconfirmedTransaction::new(tx, Amount::from_sat(0), false, false, Vec::new(), 0), ); - let events = manager.process_instant_send(dummy_instant_lock(txid)).await; + let events = manager.process_instant_send(verified_lock(txid)).await; // Verify IS flag, broadcast promotion, and tracking cleanup assert!(manager.transactions.get(&txid).unwrap().is_instant_send); @@ -1366,7 +1374,7 @@ mod tests { let (mut manager, _requests, _rx) = create_test_manager(); let unknown_txid = Txid::from_byte_array([0xbb; 32]); - manager.process_instant_send(dummy_instant_lock(unknown_txid)).await; + manager.process_instant_send(verified_lock(unknown_txid)).await; // No immediate wallet notification let wallet = manager.wallet.read().await; @@ -1478,7 +1486,7 @@ mod tests { .peers .insert(test_socket_address(1), Some(VecDeque::from([Txid::from_byte_array([2; 32])]))); let txid3 = Txid::from_byte_array([3; 32]); - manager.pending_is_locks.insert(txid3, (dummy_instant_lock(txid3), Instant::now())); + manager.pending_is_locks.insert(txid3, (verified_lock(txid3), Instant::now())); manager.clear_pending(); @@ -1527,7 +1535,9 @@ mod tests { let txid = tx.txid(); // IS lock arrives before the transaction (with a distinct cyclehash) - manager.process_instant_send(rich_instant_lock(txid)).await; + manager + .process_instant_send(VerifiedInstantLock::assume_verified(rich_instant_lock(txid))) + .await; assert!(manager.pending_is_locks.contains_key(&txid)); // Transaction arrives @@ -1565,7 +1575,7 @@ mod tests { let txid = tx.txid(); // IS lock arrives before the transaction - manager.process_instant_send(dummy_instant_lock(txid)).await; + manager.process_instant_send(verified_lock(txid)).await; assert!(manager.pending_is_locks.contains_key(&txid)); // Transaction arrives but wallet says it's not relevant @@ -1587,13 +1597,13 @@ mod tests { let mut bytes = [0u8; 32]; bytes[0..8].copy_from_slice(&(i as u64).to_le_bytes()); let txid = Txid::from_byte_array(bytes); - manager.pending_is_locks.insert(txid, (dummy_instant_lock(txid), Instant::now())); + manager.pending_is_locks.insert(txid, (verified_lock(txid), Instant::now())); } assert_eq!(manager.pending_is_locks.len(), MAX_PENDING_IS_LOCKS); // Next IS lock should be dropped let overflow_txid = Txid::from_byte_array([0xff; 32]); - manager.process_instant_send(dummy_instant_lock(overflow_txid)).await; + manager.process_instant_send(verified_lock(overflow_txid)).await; assert!(!manager.pending_is_locks.contains_key(&overflow_txid)); assert_eq!(manager.pending_is_locks.len(), MAX_PENDING_IS_LOCKS); } @@ -1621,10 +1631,10 @@ mod tests { // Also store a pending IS lock for this txid and an unrelated one let unrelated_txid = Txid::from_byte_array([0xdd; 32]); - manager.pending_is_locks.insert(txid, (dummy_instant_lock(txid), Instant::now())); + manager.pending_is_locks.insert(txid, (verified_lock(txid), Instant::now())); manager .pending_is_locks - .insert(unrelated_txid, (dummy_instant_lock(unrelated_txid), Instant::now())); + .insert(unrelated_txid, (verified_lock(unrelated_txid), Instant::now())); manager.prune_expired(test_timeout); @@ -1650,17 +1660,12 @@ mod tests { let stale_txid = Txid::from_byte_array([0xaa; 32]); manager.pending_is_locks.insert( stale_txid, - ( - dummy_instant_lock(stale_txid), - Instant::now() - test_timeout - Duration::from_secs(1), - ), + (verified_lock(stale_txid), Instant::now() - test_timeout - Duration::from_secs(1)), ); // Insert a fresh pending IS lock let fresh_txid = Txid::from_byte_array([0xbb; 32]); - manager - .pending_is_locks - .insert(fresh_txid, (dummy_instant_lock(fresh_txid), Instant::now())); + manager.pending_is_locks.insert(fresh_txid, (verified_lock(fresh_txid), Instant::now())); manager.prune_expired(test_timeout); @@ -1962,7 +1967,7 @@ mod tests { // The same signed transaction reached the network by another route first (a BIP70 // merchant broadcasting the bytes it was just paid with), so its InstantSend lock // arrives before this node dispatches its own copy. - manager.process_instant_send(dummy_instant_lock(txid)).await; + manager.process_instant_send(verified_lock(txid)).await; assert!(manager.pending_is_locks.contains_key(&txid)); let events = manager.start_broadcast(&tx, &requests).await; @@ -1984,6 +1989,47 @@ mod tests { ); } + #[test_case(true, true; "known uncertain")] + #[test_case(false, false; "early pending")] + #[test_case(false, true; "early uncertain")] + #[tokio::test] + async fn should_accept_broadcast_on_instant_lock(known: bool, uncertain: bool) { + let (mut manager, requests, _rx) = create_test_manager(); + let tx = test_transaction(42); + let txid = tx.txid(); + assert!(manager.start_broadcast(&tx, &requests).await.is_empty()); + if known { + manager.transactions.insert( + txid, + UnconfirmedTransaction::new(tx, Amount::from_sat(0), false, false, Vec::new(), 0), + ); + } + if uncertain { + let deadline = Instant::now() + BroadcastConfig::default().acceptance_timeout; + assert_eq!(manager.expire_broadcasts_at(deadline).len(), 1); + } + let lock = dummy_instant_lock(txid); + + let events = manager.process_instant_send(verified_lock(txid)).await; + + assert!(matches!( + events.as_slice(), + [SyncEvent::TransactionBroadcastResult { + txid: accepted, + result: BroadcastResult::Accepted { .. }, + }] if *accepted == txid + )); + assert!(!manager.broadcasts.contains_key(&txid)); + let processed = manager.wallet.read().await.processed_instant_locks.lock().await.clone(); + if known { + assert!(manager.transactions[&txid].is_instant_send); + assert_eq!(processed, vec![(txid, Some(lock))]); + } else { + assert_eq!(manager.pending_is_locks[&txid].0.instant_lock(), &lock); + assert!(processed.is_empty()); + } + } + #[tokio::test] async fn test_broadcast_seeds_acceptance_from_peer_relay() { let (mut manager, requests, _rx) = create_test_manager(); @@ -2064,7 +2110,7 @@ mod tests { let tx = test_transaction(43); let txid = tx.txid(); - manager.process_instant_send(dummy_instant_lock(txid)).await; + manager.process_instant_send(verified_lock(txid)).await; let local_addr = SocketAddr::from(([0, 0, 0, 0], 0)); let events = manager.handle_tx(tx, local_addr, &requests).await.unwrap(); diff --git a/dash-spv/src/sync/mempool/sync_manager.rs b/dash-spv/src/sync/mempool/sync_manager.rs index 2ad7ce33e..1e66c0a63 100644 --- a/dash-spv/src/sync/mempool/sync_manager.rs +++ b/dash-spv/src/sync/mempool/sync_manager.rs @@ -132,7 +132,6 @@ impl SyncManager for MempoolManager { } SyncEvent::InstantLockReceived { instant_lock, - .. } => Ok(self.process_instant_send(instant_lock.clone()).await), _ => Ok(vec![]), } @@ -183,6 +182,8 @@ mod tests { use super::*; use crate::sync::BroadcastConfig; use crate::test_utils::{test_socket_address, MockNetworkManager}; + use crate::validation::VerifiedInstantLock; + use dashcore::Transaction; use key_wallet_manager::test_utils::MockWallet; use std::collections::{BTreeMap, BTreeSet}; use std::sync::Arc; @@ -350,7 +351,7 @@ mod tests { // Add transactions to mempool let mut txids = Vec::new(); for i in 0..2u32 { - let tx = dashcore::Transaction { + let tx = Transaction { version: 1, lock_time: i, input: vec![], @@ -398,7 +399,7 @@ mod tests { manager.handle_sync_event(&sync, &network).await.unwrap(); // Add a transaction to mempool - let tx = dashcore::Transaction { + let tx = Transaction { version: 1, lock_time: 0, input: vec![], @@ -423,8 +424,7 @@ mod tests { is_lock.txid = txid; let event = SyncEvent::InstantLockReceived { - instant_lock: is_lock, - validated: true, + instant_lock: VerifiedInstantLock::assume_verified(is_lock), }; let events = manager.handle_sync_event(&event, &network).await.unwrap(); assert!(events.is_empty()); @@ -493,8 +493,7 @@ mod tests { ..InstantLock::default() }; let event = SyncEvent::InstantLockReceived { - instant_lock: lock, - validated: true, + instant_lock: VerifiedInstantLock::assume_verified(lock), }; manager.handle_sync_event(&event, &network).await.unwrap(); let mut corrections = Vec::new(); @@ -573,11 +572,10 @@ mod tests { } while rx.try_recv().is_ok() {} let event = SyncEvent::InstantLockReceived { - instant_lock: InstantLock { + instant_lock: VerifiedInstantLock::assume_verified(InstantLock { txid: tx.txid(), ..InstantLock::default() - }, - validated: true, + }), }; manager.handle_sync_event(&event, &network).await.unwrap(); let mut locks = 0; @@ -618,11 +616,10 @@ mod tests { .unwrap(); while rx.try_recv().is_ok() {} let event = SyncEvent::InstantLockReceived { - instant_lock: InstantLock { + instant_lock: VerifiedInstantLock::assume_verified(InstantLock { txid: outgoing.txid(), ..InstantLock::default() - }, - validated: true, + }), }; manager.handle_sync_event(&event, &network).await.unwrap(); assert!( @@ -1017,7 +1014,7 @@ mod tests { network_mock.clear_sent().await; // handle_tx with a relevant transaction should NOT eagerly rebuild - let tx = dashcore::Transaction { + let tx = Transaction { version: 1, lock_time: 0, input: vec![], diff --git a/dash-spv/src/test_utils/instant_lock.rs b/dash-spv/src/test_utils/instant_lock.rs new file mode 100644 index 000000000..b8f5fda31 --- /dev/null +++ b/dash-spv/src/test_utils/instant_lock.rs @@ -0,0 +1,110 @@ +//! Signed fixtures for consumers testing authenticated InstantSend replay. + +use std::collections::BTreeMap; +use std::io; + +use dashcore::bls_sig_utils::{BLSPublicKey, BLSSignature, BlsError, BlsScheme, BlsSkBytes}; +use dashcore::hash_types::QuorumVVecHash; +use dashcore::sml::llmq_entry_verification::LLMQEntryVerificationStatus; +use dashcore::sml::llmq_type::network::NetworkLLMQExt; +use dashcore::sml::quorum_entry::qualified_quorum_entry::QualifiedQuorumEntry; +use dashcore::transaction::special_transaction::quorum_commitment::QuorumEntry; +use dashcore::{BlockHash, InstantLock, Network, QuorumHash, Transaction}; +use key_wallet_manager::WalletInterface; + +use crate::client::DashSpvClient; +use crate::error::SpvError; +use crate::network::NetworkManager; + +/// Failure to build a signed test fixture. +#[derive(Debug, thiserror::Error)] +pub enum InstantLockFixtureError { + /// The fixture key could not sign. + #[error("invalid fixture BLS key: {0}")] + Key(#[from] BlsError), + /// The lock signing digest could not be encoded. + #[error("cannot encode fixture signing digest: {0}")] + Encoding(#[from] io::Error), +} + +/// A lock signed by a random key of its own, and a synthetic quorum cycle for it. +/// +/// `install` makes every lock signed by that key verify on the client it is installed +/// into, so use it only on throwaway test clients. It is never a network proof. +#[derive(Debug, Clone)] +pub struct InstantLockFixture { + instant_lock: InstantLock, + quorums: BTreeMap, +} + +impl InstantLockFixture { + /// Signs the transaction's ID and the inputs its lock carries with a quorum of `network`'s + /// InstantSend quorum type. + pub fn new( + network: Network, + transaction: &Transaction, + ) -> Result { + let secret = + BlsSkBytes::from_bytes(rand::random()).as_scheme(BlsScheme::Modern).canonicalize()?; + let key = secret.as_scheme(BlsScheme::Modern); + let llmq_type = network.isd_llmq_type(); + let quorum_hash = QuorumHash::from_byte_array([9; 32]); + let mut instant_lock = InstantLock { + version: 1, + inputs: InstantLock::lock_inputs(transaction), + txid: transaction.txid(), + cyclehash: BlockHash::from_byte_array([8; 32]), + signature: BLSSignature::from([0; 96]), + }; + let digest = instant_lock.sign_id(llmq_type, quorum_hash, None)?; + instant_lock.signature = key.sign_digest(digest.as_byte_array())?; + let quorum_public_key = BLSPublicKey::from(key.public_key()?); + let quorums = (0..llmq_type.active_quorum_count() as u16) + .map(|index| { + let entry = QuorumEntry { + version: 2, + llmq_type, + quorum_hash, + quorum_index: Some(index as i16), + signers: vec![true; 4], + valid_members: vec![true; 4], + quorum_public_key, + quorum_vvec_hash: QuorumVVecHash::all_zeros(), + threshold_sig: instant_lock.signature, + all_commitment_aggregated_signature: instant_lock.signature, + }; + let mut quorum = QualifiedQuorumEntry::from(entry); + quorum.verified = LLMQEntryVerificationStatus::Verified; + (index, quorum) + }) + .collect(); + Ok(Self { + instant_lock, + quorums, + }) + } + + /// Omits one quorum slot to model a cycle whose synchronization is incomplete. + pub fn without_quorum(mut self, index: u16) -> Self { + self.quorums.remove(&index); + self + } + + /// The signed payload to pass through the public verification query. + pub fn instant_lock(&self) -> &InstantLock { + &self.instant_lock + } + + /// Installs this synthetic quorum cycle into the client's engine. + pub async fn install( + &self, + client: &DashSpvClient, + ) -> Result<(), SpvError> { + let engine = client.masternode_list_engine()?; + engine + .write() + .await + .install_test_instant_lock_cycle(self.instant_lock.cyclehash, self.quorums.clone()); + Ok(()) + } +} diff --git a/dash-spv/src/test_utils/mod.rs b/dash-spv/src/test_utils/mod.rs index 18fc8ec35..8d2c0bdb7 100644 --- a/dash-spv/src/test_utils/mod.rs +++ b/dash-spv/src/test_utils/mod.rs @@ -6,6 +6,7 @@ mod event_handler; mod filter; mod fs_helpers; mod header_storage; +mod instant_lock; pub(crate) mod masternode_network; mod network; mod node; @@ -21,6 +22,7 @@ pub use context::DashdTestContext; pub use event_handler::TestEventHandler; pub use fs_helpers::retain_test_dir; pub use header_storage::MockHeaderStorage; +pub use instant_lock::{InstantLockFixture, InstantLockFixtureError}; pub use masternode_network::{ ControllerInfo, MasternodeInfo, MasternodeTestContext, NetworkMetadata, }; diff --git a/dash-spv/src/validation/instantlock.rs b/dash-spv/src/validation/instantlock.rs index 88982e204..b0ce18f27 100644 --- a/dash-spv/src/validation/instantlock.rs +++ b/dash-spv/src/validation/instantlock.rs @@ -1,13 +1,112 @@ -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashSet}; use dashcore::bls_sig_utils::BlsScheme; +use dashcore::constants::MAX_BLOCK_SIZE; use dashcore::sml::message_verification_error::MessageVerificationError; use dashcore::sml::quorum_entry::qualified_quorum_entry::QualifiedQuorumEntry; -use dashcore::{InstantLock, QuorumSigningRequestId}; +use dashcore::{InstantLock, Network, QuorumSigningRequestId, Transaction, Txid}; use crate::error::{ValidationError, ValidationResult}; +use crate::sml_engine::MasternodeListEngine; +use crate::storage::BlockHeaderStorage; use crate::validation::Validator; +/// An InstantLock authenticated against the client's synchronized quorum state. +/// +/// This evidence is process-local: persisted locks must be verified again on load. +/// Obtain it with `DashSpvClient::verify_instant_lock` or from `SyncEvent::InstantLockReceived`; +/// a stored context is not proof. +#[derive(Debug, Clone)] +pub struct VerifiedInstantLock { + instant_lock: InstantLock, + network: Network, +} + +#[cfg(test)] +impl VerifiedInstantLock { + /// Evidence for `instant_lock` without authenticating it, for unit tests of its consumers. + pub(crate) fn assume_verified(instant_lock: InstantLock) -> Self { + Self { + instant_lock, + network: Network::Testnet, + } + } +} + +/// A stored or received InstantLock failed authentication. +#[derive(Debug, thiserror::Error)] +pub enum InstantLockVerificationError { + /// The lock's fields are malformed. + #[error("malformed InstantLock: {0}")] + Malformed(#[source] ValidationError), + /// The lock is not signed by the selected quorum. + #[error("InstantLock authentication failed: {0}")] + InvalidSignature(#[source] ValidationError), +} + +impl VerifiedInstantLock { + /// The authenticated lock, including its signed inputs. + pub fn instant_lock(&self) -> &InstantLock { + &self.instant_lock + } + + /// The network of the client whose quorum state authenticated this lock. + pub fn network(&self) -> Network { + self.network + } + + /// The transaction ID covered by the quorum signature. + pub fn txid(&self) -> Txid { + self.instant_lock.txid + } + + /// Binds the evidence to the wallet's network, the complete transaction and the ordered + /// inputs its lock carries, see [`InstantLock::lock_inputs`]. + /// + /// No wallet arithmetic or transaction consensus validation is performed. + pub fn matches_transaction(&self, network: Network, transaction: &Transaction) -> bool { + self.network == network + && self.txid() == transaction.txid() + && self.instant_lock.inputs == InstantLock::lock_inputs(transaction) + } + + /// Authenticates a lock, or returns `None` while the engine lacks the quorum data to decide. + /// + /// Fails for a malformed lock, or one whose signature a complete, verified cycle of + /// the network's InstantSend quorum type refutes. + pub(crate) fn verify( + instant_lock: &InstantLock, + engine: Option<&MasternodeListEngine>, + ) -> Result, InstantLockVerificationError> { + InstantLockStructureValidator + .validate(instant_lock) + .map_err(InstantLockVerificationError::Malformed)?; + let Some(engine) = engine else { + return Ok(None); + }; + let Some(cycle) = engine.instant_send_cycle(&instant_lock.cyclehash) else { + return Ok(None); + }; + match InstantLockValidator::new(cycle.quorums).validate_signature(instant_lock) { + Ok(()) => Ok(Some(Self { + instant_lock: instant_lock.clone(), + network: engine.network(), + })), + Err( + MessageVerificationError::CycleHashNotPresent(_) + | MessageVerificationError::QuorumIndexNotFound(..), + ) => Ok(None), + // A later QRInfo can still replace the quorums of a partial cycle. + Err(_) if !cycle.complete => Ok(None), + Err(e) => Err(InstantLockVerificationError::InvalidSignature(signature_error(e))), + } + } +} + +/// The most inputs a valid InstantLock can carry, as Dash Core caps them. +/// Its transaction fits in a block, and every input takes at least 41 bytes. +const MAX_INSTANT_LOCK_INPUTS: usize = MAX_BLOCK_SIZE as usize / 41; + /// Rejects malformed InstantLocks. Needs no quorum data, so it runs before a /// lock is looked up, queued or cached. /// @@ -34,6 +133,19 @@ impl Validator<&InstantLock> for InstantLockStructureValidator { )); } + if instant_lock.inputs.len() > MAX_INSTANT_LOCK_INPUTS { + return Err(ValidationError::InvalidInstantLock(format!( + "InstantLock has more than {MAX_INSTANT_LOCK_INPUTS} inputs" + ))); + } + + let mut seen = HashSet::with_capacity(instant_lock.inputs.len()); + if let Some(idx) = instant_lock.inputs.iter().position(|input| !seen.insert(input)) { + return Err(ValidationError::InvalidInstantLock(format!( + "InstantLock input {idx} repeats an earlier input" + ))); + } + if let Some(idx) = instant_lock.inputs.iter().position(|input| input.txid == dashcore::Txid::all_zeros()) { @@ -46,30 +158,22 @@ impl Validator<&InstantLock> for InstantLockStructureValidator { } } -/// Verifies an InstantLock's signature against the rotated quorums of its -/// cycle. It does not check the lock's structure: run -/// [`InstantLockStructureValidator`] first. Never accept InstantLocks from the -/// network without both. -/// -/// A lock whose signature does not verify, or whose quorum is not in the -/// cycle, fails with [`ValidationError::InvalidSignature`]. -pub struct InstantLockValidator<'a> { +/// Verifies an InstantLock's signature against the rotated quorums of its cycle. +/// It does not check the lock's structure or the cycle's eligibility: use +/// [`VerifiedInstantLock::verify`], which does both. +struct InstantLockValidator<'a> { /// The rotated quorums of the lock's cycle, by quorum index. cycle_quorums: &'a BTreeMap, } -impl Validator<&InstantLock> for InstantLockValidator<'_> { - fn validate(&self, instant_lock: &InstantLock) -> ValidationResult<()> { - self.validate_signature(instant_lock).map_err(|e| { - ValidationError::InvalidSignature(format!( - "InstantLock BLS signature verification failed: {e}" - )) - }) - } +fn signature_error(error: MessageVerificationError) -> ValidationError { + ValidationError::InvalidSignature(format!( + "InstantLock BLS signature verification failed: {error}" + )) } impl<'a> InstantLockValidator<'a> { - pub fn new(cycle_quorums: &'a BTreeMap) -> Self { + fn new(cycle_quorums: &'a BTreeMap) -> Self { Self { cycle_quorums, } @@ -121,11 +225,11 @@ impl<'a> InstantLockValidator<'a> { BlsScheme::Modern, ); match &result { - Ok(()) => tracing::info!( + Ok(()) => tracing::trace!( "IS lock {} verified by quorum {quorum_hash} (index {quorum_index})", instant_lock.txid ), - Err(e) => tracing::warn!( + Err(e) => tracing::debug!( "IS lock {} failed against quorum {quorum_hash} (index {quorum_index}): {e}", instant_lock.txid ), @@ -137,27 +241,116 @@ impl<'a> InstantLockValidator<'a> { #[cfg(test)] mod tests { use super::*; - use crate::sml_engine::test_support::TestEngine; - use dashcore::consensus::deserialize; + use crate::sml_engine::test_support::{mainnet_fixture_is_lock, TestEngine}; + use crate::test_utils::MockHeaderStorage; + use dashcore::bls_sig_utils::BLSSignature; use dashcore::sml::llmq_type::LLMQType; - use dashcore::QuorumHash; + use dashcore::{Address, BlockHash, OutPoint, QuorumHash, ScriptBuf, TxIn, TxOut}; use hex_conservative::DisplayHex; use test_case::test_case; - /// An IS lock of the mainnet fixture's cycle, signed by its quorum at index 23. - fn fixture_is_lock() -> InstantLock { - deserialize(&hex_conservative::decode_to_vec("01018d53e7997ead57409750942af0d5e0aafc06f852a9a52308f4781b6a8220298f00000000c6f9d8c63dd15937ea70aaddb7890daad42c91bf6818e2bf76d183d6f2d9215b4b5f84978fad9dde7ab52bdcc0674be891e9029cc1ef0cb01200000000000000a27c98836c4c04653ab81eb4e07ddfc2c8c2c1036b75247969c05a4f25451cd78913a971f1899d9f2bddec9cf8e0104004f72f20c2856453e5aa3bcd2a8200670ec28feda38f67cc400fc72ef1966956656ec0765478c9d16e9a9e470c07f9ed").unwrap()).unwrap() + #[test] + fn should_authenticate_stored_lock_only_with_available_cycle() { + let engine = TestEngine::mainnet_fixture(); + let lock = mainnet_fixture_is_lock(); + assert!(VerifiedInstantLock::verify::(&lock, None,).unwrap().is_none()); + let empty = TestEngine::empty(Network::Mainnet); + assert!(VerifiedInstantLock::verify(&lock, Some(&empty)).unwrap().is_none()); + let verified = VerifiedInstantLock::verify(&lock, Some(&engine)).unwrap().unwrap(); + assert_eq!(verified.txid(), lock.txid); + assert_eq!(verified.instant_lock(), &lock); + let mut unknown_cycle = lock.clone(); + unknown_cycle.cyclehash = BlockHash::all_zeros(); + assert!(VerifiedInstantLock::verify(&unknown_cycle, Some(&engine)).unwrap().is_none()); + let mut invalid = lock.clone(); + invalid.txid = Txid::from_byte_array([7; 32]); + assert!(matches!( + VerifiedInstantLock::verify(&invalid, Some(&engine)), + Err(InstantLockVerificationError::InvalidSignature(_)) + )); + let mut changed_input = lock.clone(); + changed_input.inputs[0].vout += 1; + assert!(matches!( + VerifiedInstantLock::verify(&changed_input, Some(&engine)), + Err(InstantLockVerificationError::InvalidSignature(_)) + )); + let mut changed_signature = lock.clone(); + changed_signature.signature = BLSSignature::from([1; 96]); + assert!(matches!( + VerifiedInstantLock::verify(&changed_signature, Some(&engine)), + Err(InstantLockVerificationError::InvalidSignature(_)) + )); + let mut malformed = lock; + malformed.inputs.clear(); + assert!(matches!( + VerifiedInstantLock::verify(&malformed, Some(&empty)), + Err(InstantLockVerificationError::Malformed(_)) + )); + } + + #[test] + fn should_bind_verified_evidence_without_wallet_arithmetic() { + let transaction = Transaction { + version: 1, + lock_time: 0, + input: vec![TxIn { + previous_output: OutPoint::new(Txid::from_byte_array([3; 32]), 0), + ..Default::default() + }], + output: vec![ + TxOut { + value: u64::MAX, + script_pubkey: ScriptBuf::new(), + }; + 2 + ], + special_transaction_payload: None, + }; + // This unit tests binding only; authentication is exercised with the signed fixture. + let mut lock = mainnet_fixture_is_lock(); + lock.txid = transaction.txid(); + lock.inputs = InstantLock::lock_inputs(&transaction); + let verified = VerifiedInstantLock { + instant_lock: lock, + network: Network::Mainnet, + }; + assert!(verified.matches_transaction(Network::Mainnet, &transaction)); + assert!(!verified.matches_transaction(Network::Testnet, &transaction)); + let mut changed = transaction.clone(); + changed.output[0].value -= 1; + assert!(!verified.matches_transaction(Network::Mainnet, &changed)); + let mut wrong_inputs = verified.clone(); + wrong_inputs.instant_lock.inputs[0].vout += 1; + assert!(!wrong_inputs.matches_transaction(Network::Mainnet, &transaction)); + + let address = Address::dummy(Network::Mainnet, 0); + let withdrawal = Transaction::dummy_asset_unlock(&address, 7, 1); + let mut lock = mainnet_fixture_is_lock(); + lock.txid = withdrawal.txid(); + lock.inputs = InstantLock::lock_inputs(&withdrawal); + let verified = VerifiedInstantLock { + instant_lock: lock, + network: Network::Mainnet, + }; + assert!(verified.matches_transaction(Network::Mainnet, &withdrawal)); } #[test] fn a_well_formed_lock_passes_the_structure_check() { assert!(InstantLockStructureValidator.validate(&InstantLock::dummy(0..3)).is_ok()); + let mut largest = InstantLock::dummy(0..1); + let txid = largest.inputs[0].txid; + largest.inputs = + (0..MAX_INSTANT_LOCK_INPUTS as u32).map(|vout| OutPoint::new(txid, vout)).collect(); + assert!(InstantLockStructureValidator.validate(&largest).is_ok()); } #[test_case(|lock| lock.inputs.clear(), "at least one input"; "no inputs")] #[test_case(|lock| lock.signature = dashcore::bls_sig_utils::BLSSignature::from([0; 96]), "signature cannot be zero"; "zero signature")] #[test_case(|lock| lock.txid = dashcore::Txid::all_zeros(), "transaction ID cannot be zero"; "null txid")] #[test_case(|lock| lock.inputs[1].txid = dashcore::Txid::all_zeros(), "input 1 has null transaction ID"; "null input txid")] + #[test_case(|lock| lock.inputs = vec![lock.inputs[0]; MAX_INSTANT_LOCK_INPUTS + 1], "more than 48780 inputs"; "too many inputs")] + #[test_case(|lock| lock.inputs[2] = lock.inputs[0], "input 2 repeats an earlier input"; "repeated input")] fn a_malformed_lock_fails_the_structure_check(malform: fn(&mut InstantLock), reason: &str) { let mut is_lock = InstantLock::dummy(0..3); malform(&mut is_lock); @@ -171,9 +364,9 @@ mod tests { #[test] fn a_lock_verifies_against_the_quorum_its_request_id_selects() { let engine = TestEngine::mainnet_fixture(); - let lock = fixture_is_lock(); + let lock = mainnet_fixture_is_lock(); let validator = - InstantLockValidator::new(engine.rotated_quorums_of_cycle(&lock.cyclehash).unwrap()); + InstantLockValidator::new(&engine.rotated_quorums_per_cycle()[&lock.cyclehash]); let request_id = lock.request_id().expect("expected to make request id"); assert_eq!( @@ -198,10 +391,10 @@ mod tests { sign_id.as_byte_array().to_lower_hex_string(), "6fcbf58004b118d865a448bf89d9299c64d4ecedd754dabec655090224de91cd" ); - validator.validate(&lock).expect("expected to verify is lock"); + validator.validate_signature(&lock).expect("expected to verify is lock"); // Without its quorum index in the cycle, the lock cannot be resolved. - let mut quorums = engine.rotated_quorums_of_cycle(&lock.cyclehash).unwrap().clone(); + let mut quorums = engine.rotated_quorums_per_cycle()[&lock.cyclehash].clone(); quorums.remove(&23); match InstantLockValidator::new(&quorums).signing_quorum(&lock) { Err(MessageVerificationError::QuorumIndexNotFound(23, hash)) => { diff --git a/dash-spv/src/validation/mod.rs b/dash-spv/src/validation/mod.rs index 52e6275a1..9a1b245fc 100644 --- a/dash-spv/src/validation/mod.rs +++ b/dash-spv/src/validation/mod.rs @@ -11,7 +11,8 @@ pub use block::BlockValidator; pub use chainlock::ChainLockValidator; pub use filter::{FilterValidationInput, FilterValidator}; pub use header::BlockHeaderValidator; -pub use instantlock::{InstantLockStructureValidator, InstantLockValidator}; +pub(crate) use instantlock::InstantLockStructureValidator; +pub use instantlock::{InstantLockVerificationError, VerifiedInstantLock}; pub use masternode_list::MasternodeListValidator; use crate::error::ValidationResult; diff --git a/dash-spv/tests/dashd_masternode/helpers.rs b/dash-spv/tests/dashd_masternode/helpers.rs index 6bb76061c..c28020cd8 100644 --- a/dash-spv/tests/dashd_masternode/helpers.rs +++ b/dash-spv/tests/dashd_masternode/helpers.rs @@ -280,14 +280,12 @@ pub(super) async fn wait_for_mn_state_with_stored_cycle_above( } } -/// Wait for an `InstantLockReceived` sync event for `txid` with the desired validation state. +/// Wait for the verified `InstantLockReceived` sync event for `txid`. /// -/// Returns the received `InstantLock`. Ignores events for unrelated txids and events -/// whose `validated` flag does not match `want_validated`. +/// Returns the verified `InstantLock`. Ignores events for unrelated txids. pub(super) async fn wait_for_instant_lock_received( event_receiver: &mut broadcast::Receiver, txid: Txid, - want_validated: bool, timeout_secs: u64, ) -> InstantLock { let timeout = time::sleep(Duration::from_secs(timeout_secs)); @@ -296,26 +294,18 @@ pub(super) async fn wait_for_instant_lock_received( loop { tokio::select! { _ = &mut timeout => { - panic!( - "Timeout waiting for InstantLockReceived(txid={}, validated={})", - txid, want_validated - ); + panic!("Timeout waiting for InstantLockReceived(txid={})", txid); } result = event_receiver.recv() => { match result { - Ok(SyncEvent::InstantLockReceived { instant_lock, validated }) - if instant_lock.txid == txid && validated == want_validated => - { - tracing::info!( - "InstantLockReceived(txid={}, validated={})", - txid, validated - ); - return instant_lock; + Ok(SyncEvent::InstantLockReceived { instant_lock }) if instant_lock.txid() == txid => { + tracing::info!("InstantLockReceived(txid={})", txid); + return instant_lock.instant_lock().clone(); } - Ok(SyncEvent::InstantLockReceived { instant_lock, validated }) => { + Ok(SyncEvent::InstantLockReceived { instant_lock }) => { tracing::debug!( - "Ignoring InstantLockReceived(txid={}, validated={}) — waiting for txid={} validated={}", - instant_lock.txid, validated, txid, want_validated + "Ignoring InstantLockReceived(txid={}) — waiting for txid={}", + instant_lock.txid(), txid ); continue; } diff --git a/dash-spv/tests/dashd_masternode/tests_instantsend.rs b/dash-spv/tests/dashd_masternode/tests_instantsend.rs index 74559c003..912bdde17 100644 --- a/dash-spv/tests/dashd_masternode/tests_instantsend.rs +++ b/dash-spv/tests/dashd_masternode/tests_instantsend.rs @@ -31,7 +31,7 @@ use super::setup::{ /// up). The sends are sequential rather than concurrent because regtest's /// 4-MN quorum has trouble keeping multiple concurrent IS signing sessions /// alive past the session timeout. For each transaction the test asserts: -/// 1. `SyncEvent::InstantLockReceived { validated: true }` fires. +/// 1. `SyncEvent::InstantLockReceived` fires for the verified lock. /// 2. A wallet event reports `TransactionContext::InstantSend(_)` for the txid. /// Then it mines blocks until a ChainLock is produced and asserts each tx /// transitions to `TransactionContext::InChainLockedBlock(_)`. @@ -80,7 +80,6 @@ async fn test_instantsend_full_lifecycle() { let lock = wait_for_instant_lock_received( &mut client_handle.sync_event_receiver, txid, - true, SYNC_TIMEOUT, ) .await; @@ -181,7 +180,6 @@ async fn test_instantsend_across_quorum_rotation() { let _ = wait_for_instant_lock_received( &mut client_handle.sync_event_receiver, pre_txid, - true, SYNC_TIMEOUT, ) .await; @@ -215,7 +213,6 @@ async fn test_instantsend_across_quorum_rotation() { let _ = wait_for_instant_lock_received( &mut client_handle.sync_event_receiver, post_txid, - true, SYNC_TIMEOUT, ) .await; @@ -239,7 +236,7 @@ async fn test_instantsend_across_quorum_rotation() { /// path where an islock is held until the matching mempool entry arrives. /// /// Regardless of exact ordering, the test asserts the end-to-end outcome: a -/// validated `InstantLockReceived` event plus a wallet event reporting the +/// verified `InstantLockReceived` event plus a wallet event reporting the /// transaction in an `InstantSend` context. #[tokio::test] async fn test_instantsend_islock_arrives_before_tx() { @@ -288,18 +285,14 @@ async fn test_instantsend_islock_arrives_before_tx() { // rotated_quorums_per_cycle so the pending islock can be validated. wait_for_masternode_sync(&mut client_handle.progress_receiver, SYNC_TIMEOUT).await; - // The isdlock may have arrived before MN sync (validated=false, queued as - // pending). MasternodeStateUpdated triggers validate_pending which re-emits - // with validated=true. Bump mocktime again to ensure the scheduler fires. + // The isdlock may have arrived before MN sync and been queued as pending. + // MasternodeStateUpdated triggers validate_pending, which emits it once it + // verifies. Bump mocktime again to ensure the scheduler fires. ctx.mn_ctx.bump_mocktime(30); - let lock = wait_for_instant_lock_received( - &mut client_handle.sync_event_receiver, - txid, - true, - SYNC_TIMEOUT, - ) - .await; + let lock = + wait_for_instant_lock_received(&mut client_handle.sync_event_receiver, txid, SYNC_TIMEOUT) + .await; assert_eq!(lock.txid, txid); // And the wallet must observe it with an InstantSend context regardless of diff --git a/dash-spv/tests/dashd_masternode/tests_sync.rs b/dash-spv/tests/dashd_masternode/tests_sync.rs index a05367c19..27b88394a 100644 --- a/dash-spv/tests/dashd_masternode/tests_sync.rs +++ b/dash-spv/tests/dashd_masternode/tests_sync.rs @@ -204,17 +204,13 @@ async fn test_masternode_list_sync_with_restart() { wait_for_masternode_sync(&mut client_handle.progress_receiver, SYNC_TIMEOUT).await; let second_height = second_mn_progress.current_height(); - let lock = wait_for_instant_lock_received( - &mut client_handle.sync_event_receiver, - txid, - true, - SYNC_TIMEOUT, - ) - .await; + let lock = + wait_for_instant_lock_received(&mut client_handle.sync_event_receiver, txid, SYNC_TIMEOUT) + .await; { let engine = client_handle.engine.read().await; assert!( - engine.rotated_quorums_of_cycle(&lock.cyclehash).is_some(), + engine.rotated_quorums_per_cycle().contains_key(&lock.cyclehash), "the lock is accepted against a stored cycle" ); assert_all_rotated_quorums_verified(&engine); diff --git a/dash-spv/tests/instant_lock_evidence.rs b/dash-spv/tests/instant_lock_evidence.rs new file mode 100644 index 000000000..26ba0fe56 --- /dev/null +++ b/dash-spv/tests/instant_lock_evidence.rs @@ -0,0 +1,131 @@ +#![cfg(feature = "test-utils")] + +use dash_spv::test_utils::{InstantLockFixture, MockNetworkManager}; +use dash_spv::{ClientConfig, DashSpvClient, InstantLockVerificationError, VerifiedInstantLock}; +use dashcore::bls_sig_utils::BLSSignature; +use dashcore::{InstantLock, Network, OutPoint, Transaction, TxIn, Txid}; +use key_wallet_manager::test_utils::MockWallet; +use std::sync::Arc; +use tempfile::TempDir; +use tokio::sync::RwLock; + +type TestClient = DashSpvClient; + +/// A client built from `config`, storing into a directory that lives as long as it. +async fn client(config: ClientConfig) -> (TestClient, Arc>, TempDir) { + let directory = TempDir::new().unwrap(); + let wallet = Arc::new(RwLock::new(MockWallet::new())); + let config = config.without_filters().with_storage_path(directory.path()); + let client = DashSpvClient::new(config, MockNetworkManager::new(), wallet.clone(), vec![]) + .await + .unwrap(); + (client, wallet, directory) +} + +fn spend() -> Transaction { + let mut transaction = Transaction::dummy_empty(); + transaction.input.push(TxIn { + previous_output: OutPoint::new(Txid::from_byte_array([3; 32]), 0), + ..Default::default() + }); + transaction +} + +fn with_wrong_signature(lock: &InstantLock) -> InstantLock { + InstantLock { + signature: BLSSignature::from([1; 96]), + ..lock.clone() + } +} + +#[tokio::test] +async fn should_verify_instant_lock_after_cycle_arrives_without_wallet_arithmetic() { + let (client, wallet, _directory) = client(ClientConfig::mainnet()).await; + let transaction = spend(); + let fixture = InstantLockFixture::new(Network::Mainnet, &transaction).unwrap(); + let lock = fixture.instant_lock(); + assert!(client.verify_instant_lock(lock).await.unwrap().is_none()); + fixture.install(&client).await.unwrap(); + let proof: VerifiedInstantLock = client.verify_instant_lock(lock).await.unwrap().unwrap(); + assert!(proof.matches_transaction(Network::Mainnet, &transaction)); + assert_eq!(proof.instant_lock(), lock); + assert!(wallet.read().await.processed_instant_locks.lock().await.is_empty()); + assert!(matches!( + client.verify_instant_lock(&with_wrong_signature(lock)).await, + Err(InstantLockVerificationError::InvalidSignature(_)) + )); +} + +#[tokio::test] +async fn should_defer_instant_lock_when_masternodes_are_disabled() { + let (client, _wallet, _directory) = client(ClientConfig::mainnet().without_masternodes()).await; + assert!(client.verify_instant_lock(&InstantLock::dummy(0..1)).await.unwrap().is_none()); + assert!(matches!( + client.verify_instant_lock(&InstantLock::default()).await, + Err(InstantLockVerificationError::Malformed(_)) + )); +} + +#[tokio::test] +async fn should_defer_instant_lock_until_selected_quorum_arrives() { + // Regtest's InstantSend quorum type has two active slots; exactly one signs this lock. + let (client, _wallet, _directory) = client(ClientConfig::regtest()).await; + let transaction = spend(); + let fixture = InstantLockFixture::new(Network::Regtest, &transaction).unwrap(); + let lock = fixture.instant_lock(); + let mut missing = 0; + for index in 0..2 { + fixture.clone().without_quorum(index).install(&client).await.unwrap(); + match client.verify_instant_lock(lock).await { + Ok(None) => missing += 1, + Ok(Some(proof)) => assert!(proof.matches_transaction(Network::Regtest, &transaction)), + Err(error) => panic!("incomplete quorum data is unavailable, not invalid: {error}"), + } + assert!( + client.verify_instant_lock(&with_wrong_signature(lock)).await.unwrap().is_none(), + "a partial cycle cannot refute a lock" + ); + } + assert_eq!(missing, 1); + fixture.clone().without_quorum(0).without_quorum(1).install(&client).await.unwrap(); + assert!(client.verify_instant_lock(lock).await.unwrap().is_none()); + fixture.install(&client).await.unwrap(); + assert!(client.verify_instant_lock(lock).await.unwrap().is_some()); +} + +#[tokio::test] +async fn should_bind_instant_lock_evidence_to_the_verifying_network() { + let transaction = spend(); + for network in [Network::Mainnet, Network::Testnet] { + let (client, _wallet, _directory) = client(ClientConfig::new(network)).await; + let fixture = InstantLockFixture::new(network, &transaction).unwrap(); + fixture.install(&client).await.unwrap(); + let proof = client.verify_instant_lock(fixture.instant_lock()).await.unwrap().unwrap(); + assert_eq!(proof.network(), network); + assert!(proof.matches_transaction(network, &transaction)); + for other in [Network::Mainnet, Network::Testnet].into_iter().filter(|n| *n != network) { + assert!(!proof.matches_transaction(other, &transaction)); + } + } +} + +#[tokio::test] +async fn should_not_verify_lock_of_another_instant_send_quorum_type() { + let (client, _wallet, _directory) = client(ClientConfig::mainnet()).await; + let fixture = InstantLockFixture::new(Network::Regtest, &spend()).unwrap(); + fixture.install(&client).await.unwrap(); + assert!(client.verify_instant_lock(fixture.instant_lock()).await.unwrap().is_none()); +} + +#[tokio::test] +async fn should_refute_a_lock_signed_by_another_fixture() { + let (client, _wallet, _directory) = client(ClientConfig::mainnet()).await; + let transaction = spend(); + let installed = InstantLockFixture::new(Network::Mainnet, &transaction).unwrap(); + let other = InstantLockFixture::new(Network::Mainnet, &transaction).unwrap(); + installed.install(&client).await.unwrap(); + assert!(matches!( + client.verify_instant_lock(other.instant_lock()).await, + Err(InstantLockVerificationError::InvalidSignature(_)) + )); +} diff --git a/dash/src/ephemerealdata/instant_lock.rs b/dash/src/ephemerealdata/instant_lock.rs index 6ea96abf8..1f2189c59 100644 --- a/dash/src/ephemerealdata/instant_lock.rs +++ b/dash/src/ephemerealdata/instant_lock.rs @@ -11,12 +11,17 @@ pub use std::vec::Vec; use crate::bls_sig_utils::BLSSignature; use crate::consensus::Encodable; -use crate::hash_types::{CycleHash, QuorumSigningRequestId, QuorumSigningSignId}; +use crate::consensus::encode::serialize; +use crate::hash_types::{ + CycleHash, InstantSendLockHash, QuorumSigningRequestId, QuorumSigningSignId, +}; use crate::internal_macros::impl_consensus_encoding; use crate::sml::llmq_type::LLMQType; -use crate::{OutPoint, QuorumHash, Txid, VarInt, io}; +use crate::transaction::special_transaction::TransactionPayload; +use crate::{OutPoint, QuorumHash, Transaction, Txid, VarInt, io}; const IS_LOCK_REQUEST_ID_PREFIX: &str = "islock"; +const ASSET_UNLOCK_REQUEST_ID_PREFIX: &str = "plwdtx"; #[derive(Clone, Eq, PartialEq)] #[cfg_attr(feature = "bincode", derive(Encode, Decode))] @@ -52,6 +57,25 @@ impl Default for InstantLock { } impl InstantLock { + /// Returns the outpoints a lock of `transaction` lists, as Dash Core's `GetLockInputs` + /// derives them. An asset unlock spends no inputs, so its lock lists one outpoint + /// derived from the withdrawal index. + pub fn lock_inputs(transaction: &Transaction) -> Vec { + let Some(TransactionPayload::AssetUnlockPayloadType(payload)) = + &transaction.special_transaction_payload + else { + return transaction.input.iter().map(|input| input.previous_output).collect(); + }; + let request = (ASSET_UNLOCK_REQUEST_ID_PREFIX.to_string(), payload.base.index); + let txid = Txid::from_raw_hash(sha256d::Hash::hash(&serialize(&request))); + vec![OutPoint::new(txid, 0)] + } + + /// Returns the hash peers use to announce and request this lock. + pub fn hash(&self) -> InstantSendLockHash { + InstantSendLockHash::from_raw_hash(sha256d::Hash::hash(&serialize(self))) + } + /// Returns quorum signing request ID pub fn request_id(&self) -> Result { let mut engine = sha256d::Hash::engine(); @@ -110,6 +134,26 @@ mod tests { use super::*; use crate::consensus::{deserialize, serialize}; use crate::internal_macros::hex; + use crate::{Address, Network}; + + #[test] + fn should_list_the_inputs_dash_core_locks() { + let address = Address::dummy(Network::Testnet, 0); + let transaction = Transaction::dummy(&address, 0..2, &[1]); + let prevouts: Vec = + transaction.input.iter().map(|input| input.previous_output).collect(); + assert_eq!(InstantLock::lock_inputs(&transaction), prevouts); + + // sha256d of the compact-size string "plwdtx" followed by index 7 as a u64. + let withdrawal_txid = Txid::from_slice(&hex!( + "938752a81b996801646ce07e4647fa3f536fabbc7d308b0a7647b75732a0ae58" + )) + .unwrap(); + assert_eq!( + InstantLock::lock_inputs(&Transaction::dummy_asset_unlock(&address, 7, 1)), + vec![OutPoint::new(withdrawal_txid, 0)] + ); + } #[test] pub fn should_decode_vec() { diff --git a/dash/src/test_utils/transaction.rs b/dash/src/test_utils/transaction.rs index dc370e22c..e60df3b98 100644 --- a/dash/src/test_utils/transaction.rs +++ b/dash/src/test_utils/transaction.rs @@ -6,12 +6,17 @@ use crate::hash_types::{ InputsHash, MerkleRootAssetUnlocks, MerkleRootMasternodeList, MerkleRootQuorums, }; use crate::sml::masternode_list_entry::MasternodeNetInfo; +use crate::transaction::special_transaction::asset_unlock::qualified_asset_unlock::AssetUnlockPayload; +use crate::transaction::special_transaction::asset_unlock::request_info::AssetUnlockRequestInfo; +use crate::transaction::special_transaction::asset_unlock::unqualified_asset_unlock::AssetUnlockBasePayload; use crate::transaction::special_transaction::coinbase::CoinbasePayload; use crate::transaction::special_transaction::provider_registration::{ ProviderMasternodeType, ProviderRegistrationPayload, }; use crate::transaction::special_transaction::{TransactionPayload, TransactionType}; -use crate::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction, TxIn, TxOut, Txid, Witness}; +use crate::{ + Address, OutPoint, PubkeyHash, QuorumHash, ScriptBuf, Transaction, TxIn, TxOut, Txid, Witness, +}; /// A special transaction type no Dash Core version assigns pub const UNASSIGNED_SPECIAL_TX_TYPE: u16 = 0xffff; @@ -28,6 +33,33 @@ impl Transaction { } } + /// Creates an asset unlock (Platform withdrawal) paying `value` to `address`. + pub fn dummy_asset_unlock(address: &Address, index: u64, value: u64) -> Transaction { + Transaction { + version: 3, + lock_time: 0, + input: Vec::new(), + output: vec![TxOut { + value, + script_pubkey: address.script_pubkey(), + }], + special_transaction_payload: Some(TransactionPayload::AssetUnlockPayloadType( + AssetUnlockPayload { + base: AssetUnlockBasePayload { + version: 1, + index, + fee: 0, + }, + request_info: AssetUnlockRequestInfo { + request_height: 0, + quorum_hash: QuorumHash::all_zeros(), + }, + quorum_sig: BLSSignature::from([0; 96]), + }, + )), + } + } + pub fn dummy( address: &Address, inputs_ids_range: Range,