Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dash-spv/src/client/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ impl<W: WalletInterface, N: NetworkManager> DashSpvClient<W, N> {

// Build mempool manager if tracking is enabled
if config.enable_mempool_tracking {
let initial_revision = wallet.read().await.monitor_revision();
let initial_hash = wallet.read().await.monitor_hash();
managers.mempool = Some(MempoolManager::new(
wallet.clone(),
config.mempool_strategy,
config.max_mempool_transactions,
initial_revision,
initial_hash,
config.broadcast_config(),
));
}
Expand Down
11 changes: 6 additions & 5 deletions dash-spv/src/sync/mempool/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) struct MempoolManager<W: WalletInterface> {
/// Self-broadcast transactions and their network-acceptance state.
pub(super) broadcasts: HashMap<Txid, TxBroadcastState>,
broadcast_config: BroadcastConfig,
strategy: MempoolStrategy,
pub(super) strategy: MempoolStrategy,
max_transactions: usize,
/// Txids we have requested via getdata but not yet received, with request time.
pending_requests: HashMap<Txid, Instant>,
Expand All @@ -71,9 +71,9 @@ pub(crate) struct MempoolManager<W: WalletInterface> {
/// Prevents duplicate downloads when multiple peers announce the same transactions.
/// Entries expire after `SEEN_TXID_EXPIRY`.
seen_txids: HashMap<Txid, Instant>,
/// Wallet monitor revision at the time of the last filter build.
/// Wallet monitor hash at the time of the last filter build.
/// Compared on each tick to detect when the wallet's monitored set has changed.
pub(super) last_monitor_revision: u64,
pub(super) last_monitor_hash: u64,
}

impl<W: WalletInterface> MempoolManager<W> {
Expand All @@ -83,7 +83,7 @@ impl<W: WalletInterface> MempoolManager<W> {
wallet: Arc<RwLock<W>>,
strategy: MempoolStrategy,
max_transactions: usize,
initial_monitor_revision: u64,
initial_monitor_hash: u64,
broadcast_config: BroadcastConfig,
) -> Self {
Self {
Expand All @@ -98,7 +98,7 @@ impl<W: WalletInterface> MempoolManager<W> {
peers: HashMap::new(),
pending_is_locks: HashMap::new(),
seen_txids: HashMap::new(),
last_monitor_revision: initial_monitor_revision,
last_monitor_hash: initial_monitor_hash,
}
}

Expand Down Expand Up @@ -136,6 +136,7 @@ impl<W: WalletInterface> MempoolManager<W> {
&mut self,
network: &Arc<dyn NetworkManager>,
) -> SyncResult<()> {
self.last_monitor_hash = self.wallet.read().await.monitor_hash();
let inactive: Vec<SocketAddr> =
self.peers.iter().filter(|(_, v)| v.is_none()).map(|(k, _)| *k).collect();
for peer in inactive {
Expand Down
80 changes: 44 additions & 36 deletions dash-spv/src/sync/mempool/sync_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::manager::MEMPOOL_TX_EXPIRY;
use crate::client::config::MempoolStrategy;
use crate::error::SyncResult;
use crate::network::{InboundMessage, MessageType, NetworkEvent, NetworkManager};
use crate::sync::{
Expand Down Expand Up @@ -123,7 +124,7 @@ impl<W: WalletInterface + 'static> SyncManager for MempoolManager<W> {
..
} => {
// Remove confirmed transactions from mempool.
// Bloom filter rebuild is handled by the tick's revision check.
// Bloom filter rebuild is handled by the tick's monitor hash check.
if !confirmed_txids.is_empty() {
return Ok(self.remove_confirmed(confirmed_txids));
}
Expand Down Expand Up @@ -158,19 +159,15 @@ impl<W: WalletInterface + 'static> SyncManager for MempoolManager<W> {

// Rebuild bloom filter if the wallet's monitored set has changed.
//
// We poll the revision counter rather than using push-based wallet events
// for simplicity: the revision lives on `ManagedCoreFundsAccount` and auto-bumps
// on address generation and UTXO mutations, giving us a single source of
// truth without needing event emission after every wallet operation.
// Adding a push-based approach would require a new `select!` branch in the
// shared `SyncManager::run` loop or a `WalletEvent` bridge — complexity
// that isn't justified given the 100ms tick latency is negligible for bloom
// filter rebuilds and the read lock is non-contending.
let current_revision = self.wallet.read().await.monitor_revision();
if current_revision != self.last_monitor_revision {
tracing::info!("Wallet monitor revision changed, rebuilding bloom filter");
self.rebuild_filter(network).await?;
self.last_monitor_revision = current_revision;
// The hash covers exactly what the filter is built from, so polling it
// catches every change without the wallet having to signal one.
if self.strategy == MempoolStrategy::BloomFilter {
Comment thread
ZocoLini marked this conversation as resolved.
Resolved
let current_hash = self.wallet.read().await.monitor_hash();
if current_hash != self.last_monitor_hash {
tracing::info!("Wallet monitored set changed, rebuilding bloom filter");
self.rebuild_filter(network).await?;
self.last_monitor_hash = current_hash;
}
}

Ok(events)
Expand All @@ -184,7 +181,6 @@ impl<W: WalletInterface + 'static> SyncManager for MempoolManager<W> {
#[cfg(test)]
mod tests {
use super::*;
use crate::client::config::MempoolStrategy;
use crate::sync::BroadcastConfig;
use crate::test_utils::{test_socket_address, MockNetworkManager};
use key_wallet_manager::test_utils::MockWallet;
Expand Down Expand Up @@ -762,7 +758,7 @@ mod tests {
network_mock.clear_sent().await;

// BlockProcessed does not eagerly rebuild — the tick handles it via
// the revision check. Verify no FilterLoad is sent from the event handler.
// the monitor hash check. Verify no FilterLoad is sent from the event handler.
let event = SyncEvent::BlockProcessed {
block_hash: dashcore::BlockHash::all_zeros(),
height: 1001,
Expand Down Expand Up @@ -803,7 +799,7 @@ mod tests {
}

#[tokio::test]
async fn test_tick_rebuilds_filter_when_monitor_revision_changes() {
async fn test_tick_rebuilds_filter_when_monitor_hash_changes() {
let addr = {
let script = dashcore::ScriptBuf::from_bytes(vec![
0x76, 0xa9, 0x14, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
Expand All @@ -814,7 +810,7 @@ mod tests {

let mut mock = MockWallet::new();
mock.set_addresses(vec![addr.clone()]);
let initial_revision = mock.monitor_revision();
let initial_hash = mock.monitor_hash();
let wallet = Arc::new(RwLock::new(mock));
let network_mock = Arc::new(MockNetworkManager::new());
let network: Arc<dyn NetworkManager> = network_mock.clone();
Expand All @@ -823,14 +819,27 @@ mod tests {
wallet.clone(),
MempoolStrategy::BloomFilter,
1000,
initial_revision,
initial_hash,
BroadcastConfig::default(),
);

let peer = test_socket_address(1);
manager.handle_peer_connected(peer);

// Activate — this snapshots the monitor revision
// Sync discovers addresses before the filters complete
{
let sync_addr = dashcore::Address::from_script(
&dashcore::ScriptBuf::from_bytes(vec![
0x76, 0xa9, 0x14, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef,
0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0x88, 0xac,
]),
dashcore::Network::Testnet,
)
.unwrap();
wallet.write().await.set_addresses(vec![addr.clone(), sync_addr]);
}

// Activate — this snapshots the monitor hash
let sync = SyncEvent::FiltersSyncComplete {
tip_height: 1000,
};
Expand All @@ -840,14 +849,14 @@ mod tests {
// Drain activation messages
network_mock.clear_sent().await;

// tick with unchanged revision should not rebuild
// tick with unchanged hash should not rebuild
manager.tick(&network).await.unwrap();
assert!(
network_mock.sent_to_messages().await.is_empty(),
"no messages expected when revision unchanged"
"no messages expected when hash unchanged"
);

// Simulate wallet adding new addresses (bumps revision)
// Simulate wallet adding new addresses (changes the monitor hash)
{
let mut w = wallet.write().await;
let addr2 = dashcore::Address::from_script(
Expand All @@ -870,14 +879,14 @@ mod tests {
found_filter_load = true;
}
}
assert!(found_filter_load, "expected FilterLoad after monitor revision change");
assert!(found_filter_load, "expected FilterLoad after monitor hash change");

// Subsequent tick should not rebuild again (revision was snapshotted)
// Subsequent tick should not rebuild again (hash was snapshotted)
network_mock.clear_sent().await;
manager.tick(&network).await.unwrap();
assert!(
network_mock.sent_to_messages().await.is_empty(),
"no messages expected after revision re-snapshot"
"no messages expected after hash re-snapshot"
);
}

Expand All @@ -904,7 +913,7 @@ mod tests {
manager.handle_sync_event(&sync, &network).await.unwrap();
network_mock.clear_sent().await;

// Bump revision
// Change the monitored set
{
let mut w = wallet.write().await;
w.set_addresses(vec![dashcore::Address::dummy(dashcore::Network::Testnet, 0)]);
Expand All @@ -918,7 +927,7 @@ mod tests {
found_filter = true;
}
}
assert!(!found_filter, "FetchAll should not send filter messages on revision change");
assert!(!found_filter, "FetchAll should not send filter messages on monitor hash change");
}

#[tokio::test]
Expand All @@ -933,7 +942,7 @@ mod tests {

let mut mock = MockWallet::new();
mock.set_addresses(vec![addr]);
let initial_revision = mock.monitor_revision();
let initial_hash = mock.monitor_hash();
let wallet = Arc::new(RwLock::new(mock));
let network_mock = Arc::new(MockNetworkManager::new());
let network: Arc<dyn NetworkManager> = network_mock.clone();
Expand All @@ -942,7 +951,7 @@ mod tests {
wallet.clone(),
MempoolStrategy::BloomFilter,
1000,
initial_revision,
initial_hash,
BroadcastConfig::default(),
);

Expand All @@ -964,7 +973,7 @@ mod tests {
}]);
}

// tick should detect the revision change and rebuild
// tick should detect the monitor hash change and rebuild
manager.tick(&network).await.unwrap();

let found_filter_load = network_mock
Expand All @@ -985,7 +994,7 @@ mod tests {
]);
let addr = dashcore::Address::from_script(&script, dashcore::Network::Testnet).unwrap();
mock.set_addresses(vec![addr]);
let initial_revision = mock.monitor_revision();
let initial_hash = mock.monitor_hash();
let wallet = Arc::new(RwLock::new(mock));
let network_mock = Arc::new(MockNetworkManager::new());
let network: Arc<dyn NetworkManager> = network_mock.clone();
Expand All @@ -994,7 +1003,7 @@ mod tests {
wallet.clone(),
MempoolStrategy::BloomFilter,
1000,
initial_revision,
initial_hash,
BroadcastConfig::default(),
);

Expand Down Expand Up @@ -1024,8 +1033,7 @@ mod tests {
.any(|(_, msg)| matches!(msg, NetworkMessage::FilterLoad(_)));
assert!(!has_filter_load, "handle_tx should not eagerly rebuild filter");

// But the next tick should catch it if the wallet revision changed
// (MockWallet bumps revision when set_mempool_relevant triggers processing)
// But the next tick should catch it if the monitored set changed
{
let mut w = wallet.write().await;
w.set_addresses(vec![dashcore::Address::dummy(dashcore::Network::Testnet, 0)]);
Expand All @@ -1037,6 +1045,6 @@ mod tests {
.await
.into_iter()
.any(|(_, msg)| matches!(msg, NetworkMessage::FilterLoad(_)));
assert!(found_filter_load, "tick should rebuild after revision change");
assert!(found_filter_load, "tick should rebuild after the monitored set changed");
}
}
12 changes: 1 addition & 11 deletions key-wallet-manager/src/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletManager<T> {
/// Insert a pre-built wallet and info pair.
///
/// Errors with [`WalletError::WalletExists`] if a wallet with the same ID is
/// already registered. On success bumps the structural revision.
/// already registered.
pub fn insert_wallet(&mut self, wallet: Wallet, info: T) -> Result<WalletId, WalletError> {
let wallet_id = wallet.compute_wallet_id();
if self.wallets.contains_key(&wallet_id) {
return Err(WalletError::WalletExists(wallet_id));
}
self.wallets.insert(wallet_id, wallet);
self.wallet_infos.insert(wallet_id, info);
self.bump_structural_revision();
Ok(wallet_id)
}

Expand All @@ -77,9 +76,6 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletManager<T> {
self.wallets.remove(wallet_id).ok_or(WalletError::WalletNotFound(*wallet_id))?;
let info =
self.wallet_infos.remove(wallet_id).ok_or(WalletError::WalletNotFound(*wallet_id))?;
// Absorb the removed wallet's account-level revision so the total
// stays monotonically increasing even though we lost a contributor.
self.structural_revision += info.monitor_revision() + 1;
Ok((wallet, info))
}

Expand Down Expand Up @@ -246,12 +242,6 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletManager<T> {
Some(receiver)
}

/// Return the total monitor revision (structural + per-wallet account revisions).
pub fn monitor_revision(&self) -> u64 {
self.structural_revision
+ self.wallet_infos.values().map(|w| w.monitor_revision()).sum::<u64>()
}

/// Snapshot the current balance of every managed wallet.
pub(crate) fn snapshot_balances(&self) -> BTreeMap<WalletId, WalletCoreBalance> {
self.wallet_infos.iter().map(|(id, info)| (*id, info.balance())).collect()
Expand Down
Loading
Loading