-
Notifications
You must be signed in to change notification settings - Fork 12
refactor: gate manager module behind feature flag #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1419b84
refactor: gate `manager` module behind feature flag in key-wallet
QuantumExplorer 3a4cf54
refactor: make rayon optional with parallel-filters feature
QuantumExplorer fd8e4e5
feat: enable parallel-filters for dash-spv
QuantumExplorer 0362b14
fix: enable manager feature for key-wallet-ffi
QuantumExplorer ac7ee25
refactor: extract mock wallets into dedicated test_utils/mock_wallet …
QuantumExplorer bd840db
fix: use std::sync::Mutex for synchronously-accessed status_changes
QuantumExplorer 938d841
fix: update dash-spv tests for std::sync::Mutex on status_changes
QuantumExplorer ee5ade0
revert: keep status_changes as tokio::sync::Mutex
QuantumExplorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| use crate::manager::MempoolTransactionResult; | ||
| use crate::manager::{BlockProcessingResult, WalletEvent, WalletInterface}; | ||
| use crate::transaction_checking::TransactionContext; | ||
| use dashcore::address::NetworkUnchecked; | ||
| use dashcore::prelude::CoreBlockHeight; | ||
| use dashcore::{Address, Block, OutPoint, Transaction, Txid}; | ||
| use std::collections::BTreeMap; | ||
| use std::str::FromStr; | ||
| use std::sync::Arc; | ||
| use tokio::sync::{broadcast, Mutex}; | ||
|
|
||
| // Type alias for transaction effects map | ||
| type TransactionEffectsMap = Arc<Mutex<BTreeMap<Txid, (i64, Vec<String>)>>>; | ||
|
|
||
| pub struct MockWallet { | ||
| processed_blocks: Arc<Mutex<Vec<(dashcore::BlockHash, u32)>>>, | ||
| processed_transactions: Arc<Mutex<Vec<dashcore::Txid>>>, | ||
| // Map txid -> (net_amount, addresses) | ||
| effects: TransactionEffectsMap, | ||
| synced_height: CoreBlockHeight, | ||
| event_sender: broadcast::Sender<WalletEvent>, | ||
| /// When true, process_mempool_transaction returns is_relevant=true. | ||
| mempool_relevant: bool, | ||
| /// Addresses returned by monitored_addresses. | ||
| addresses: Vec<Address>, | ||
| /// Outpoints returned by watched_outpoints. | ||
| outpoints: Vec<OutPoint>, | ||
| /// New addresses returned by process_mempool_transaction. | ||
| mempool_new_addresses: Vec<Address>, | ||
| /// Recorded status change notifications for test assertions. | ||
| status_changes: Arc<Mutex<Vec<(Txid, TransactionContext)>>>, | ||
| /// Monitor revision counter for staleness detection. | ||
| monitor_revision: u64, | ||
| } | ||
|
|
||
| impl Default for MockWallet { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl MockWallet { | ||
| pub fn new() -> Self { | ||
| let (event_sender, _) = broadcast::channel(16); | ||
| Self { | ||
| processed_blocks: Arc::new(Mutex::new(Vec::new())), | ||
| processed_transactions: Arc::new(Mutex::new(Vec::new())), | ||
| effects: Arc::new(Mutex::new(BTreeMap::new())), | ||
| synced_height: 0, | ||
| event_sender, | ||
| mempool_relevant: false, | ||
| addresses: Vec::new(), | ||
| outpoints: Vec::new(), | ||
| mempool_new_addresses: Vec::new(), | ||
| status_changes: Arc::new(Mutex::new(Vec::new())), | ||
| monitor_revision: 0, | ||
| } | ||
| } | ||
|
|
||
| /// Configure whether mempool transactions are reported as relevant. | ||
| pub fn set_mempool_relevant(&mut self, relevant: bool) { | ||
| self.mempool_relevant = relevant; | ||
| } | ||
|
|
||
| /// Set the addresses returned by monitored_addresses. | ||
| pub fn set_addresses(&mut self, addresses: Vec<Address>) { | ||
| self.addresses = addresses; | ||
| self.monitor_revision += 1; | ||
| } | ||
|
|
||
| /// Set the outpoints returned by watched_outpoints. | ||
| pub fn set_outpoints(&mut self, outpoints: Vec<OutPoint>) { | ||
| self.outpoints = outpoints; | ||
| self.monitor_revision += 1; | ||
| } | ||
|
|
||
| /// Set new addresses returned by process_mempool_transaction. | ||
| pub fn set_mempool_new_addresses(&mut self, addresses: Vec<Address>) { | ||
| self.mempool_new_addresses = addresses; | ||
| } | ||
|
|
||
| pub fn status_changes(&self) -> Arc<Mutex<Vec<(Txid, TransactionContext)>>> { | ||
| self.status_changes.clone() | ||
| } | ||
|
|
||
| pub async fn set_effect(&self, txid: dashcore::Txid, net: i64, addresses: Vec<String>) { | ||
| let mut map = self.effects.lock().await; | ||
| map.insert(txid, (net, addresses)); | ||
| } | ||
|
|
||
| pub fn processed_blocks(&self) -> Arc<Mutex<Vec<(dashcore::BlockHash, u32)>>> { | ||
| self.processed_blocks.clone() | ||
| } | ||
|
|
||
| pub fn processed_transactions(&self) -> Arc<Mutex<Vec<dashcore::Txid>>> { | ||
| self.processed_transactions.clone() | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl WalletInterface for MockWallet { | ||
| async fn process_block(&mut self, block: &Block, height: u32) -> BlockProcessingResult { | ||
| let mut processed = self.processed_blocks.lock().await; | ||
| processed.push((block.block_hash(), height)); | ||
|
|
||
| BlockProcessingResult { | ||
| new_txids: block.txdata.iter().map(|tx| tx.txid()).collect(), | ||
| existing_txids: Vec::new(), | ||
| new_addresses: Vec::new(), | ||
| } | ||
| } | ||
|
|
||
| async fn process_mempool_transaction( | ||
| &mut self, | ||
| tx: &Transaction, | ||
| _is_instant_send: bool, | ||
| ) -> MempoolTransactionResult { | ||
| let mut processed = self.processed_transactions.lock().await; | ||
| processed.push(tx.txid()); | ||
|
|
||
| if !self.mempool_relevant { | ||
| return MempoolTransactionResult::default(); | ||
| } | ||
|
|
||
| let effects = self.effects.lock().await; | ||
| let (net_amount, addresses) = if let Some((net, addr_strs)) = effects.get(&tx.txid()) { | ||
| let addrs = addr_strs | ||
| .iter() | ||
| .filter_map(|s| { | ||
| Address::<NetworkUnchecked>::from_str(s).ok().map(|a| a.assume_checked()) | ||
| }) | ||
| .collect(); | ||
| (*net, addrs) | ||
| } else { | ||
| (0, Vec::new()) | ||
| }; | ||
|
|
||
| MempoolTransactionResult { | ||
| is_relevant: true, | ||
| net_amount, | ||
| is_outgoing: net_amount < 0, | ||
| addresses, | ||
| new_addresses: self.mempool_new_addresses.clone(), | ||
| } | ||
| } | ||
|
|
||
| async fn describe(&self) -> String { | ||
| "MockWallet (test implementation)".to_string() | ||
| } | ||
|
|
||
| async fn transaction_effect(&self, tx: &Transaction) -> Option<(i64, Vec<String>)> { | ||
| let map = self.effects.lock().await; | ||
| map.get(&tx.txid()).cloned() | ||
| } | ||
|
|
||
| fn monitored_addresses(&self) -> Vec<Address> { | ||
| self.addresses.clone() | ||
| } | ||
|
|
||
| fn watched_outpoints(&self) -> Vec<OutPoint> { | ||
| self.outpoints.clone() | ||
| } | ||
|
|
||
| fn synced_height(&self) -> CoreBlockHeight { | ||
| self.synced_height | ||
| } | ||
|
|
||
| fn update_synced_height(&mut self, height: CoreBlockHeight) { | ||
| self.synced_height = height; | ||
| } | ||
|
|
||
| fn monitor_revision(&self) -> u64 { | ||
| self.monitor_revision | ||
| } | ||
|
|
||
| fn subscribe_events(&self) -> broadcast::Receiver<WalletEvent> { | ||
| self.event_sender.subscribe() | ||
| } | ||
|
|
||
| fn process_instant_send_lock(&mut self, txid: Txid) { | ||
| let mut changes = | ||
| self.status_changes.try_lock().expect("status_changes lock contention in test helper"); | ||
| changes.push((txid, TransactionContext::InstantSend)); | ||
| } | ||
| } | ||
|
|
||
| /// Mock wallet that returns false for filter checks | ||
| pub struct NonMatchingMockWallet { | ||
| synced_height: CoreBlockHeight, | ||
| event_sender: broadcast::Sender<WalletEvent>, | ||
| } | ||
|
|
||
| impl Default for NonMatchingMockWallet { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl NonMatchingMockWallet { | ||
| pub fn new() -> Self { | ||
| let (event_sender, _) = broadcast::channel(16); | ||
| Self { | ||
| synced_height: 0, | ||
| event_sender, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl WalletInterface for NonMatchingMockWallet { | ||
| async fn process_block(&mut self, _block: &Block, _height: u32) -> BlockProcessingResult { | ||
| BlockProcessingResult::default() | ||
| } | ||
|
|
||
| async fn process_mempool_transaction( | ||
| &mut self, | ||
| _tx: &Transaction, | ||
| _is_instant_send: bool, | ||
| ) -> MempoolTransactionResult { | ||
| MempoolTransactionResult::default() | ||
| } | ||
|
|
||
| fn monitored_addresses(&self) -> Vec<Address> { | ||
| Vec::new() | ||
| } | ||
|
|
||
| fn watched_outpoints(&self) -> Vec<OutPoint> { | ||
| Vec::new() | ||
| } | ||
|
|
||
| fn synced_height(&self) -> CoreBlockHeight { | ||
| self.synced_height | ||
| } | ||
|
|
||
| fn update_synced_height(&mut self, height: CoreBlockHeight) { | ||
| self.synced_height = height; | ||
| } | ||
|
|
||
| fn subscribe_events(&self) -> broadcast::Receiver<WalletEvent> { | ||
| self.event_sender.subscribe() | ||
| } | ||
|
|
||
| async fn describe(&self) -> String { | ||
| "NonMatchingWallet (test implementation)".to_string() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| mod account; | ||
| #[cfg(feature = "manager")] | ||
| mod mock_wallet; | ||
| mod utxo; | ||
| mod wallet; | ||
|
|
||
| pub use wallet::MockWallet; | ||
| pub use wallet::NonMatchingMockWallet; | ||
| #[cfg(feature = "manager")] | ||
| pub use mock_wallet::MockWallet; | ||
| #[cfg(feature = "manager")] | ||
| pub use mock_wallet::NonMatchingMockWallet; | ||
| pub use wallet::TestWalletContext; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.