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
49 changes: 42 additions & 7 deletions dash-spv/src/sync/blocks/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl<H: BlockHeaderStorage, B: BlockStorage, W: WalletInterface> BlocksManager<H
confirmed_txids,
});

self.reapply_blocks(result.reapply_heights).await;
events.extend(self.reapply_blocks(result.reapply_heights).await);
}

// Check if pipeline is empty
Expand Down Expand Up @@ -163,7 +163,14 @@ impl<H: BlockHeaderStorage, B: BlockStorage, W: WalletInterface> BlocksManager<H
result
}

async fn reapply_blocks(&mut self, reapply: BTreeMap<WalletId, BTreeSet<u32>>) {
/// Re-applies stored blocks to the wallets in `reapply`, returning a
/// `BlockProcessed` per pass so the filters manager learns the scripts each
/// pass derived, in the order the passes ran.
async fn reapply_blocks(
&mut self,
reapply: BTreeMap<WalletId, BTreeSet<u32>>,
) -> Vec<SyncEvent> {
let mut events = Vec::new();
let mut queue: BTreeSet<(u32, WalletId)> = reapply
.into_iter()
.flat_map(|(wallet_id, heights)| heights.into_iter().map(move |h| (h, wallet_id)))
Expand All @@ -180,13 +187,23 @@ impl<H: BlockHeaderStorage, B: BlockStorage, W: WalletInterface> BlocksManager<H
continue;
}
};
let result = self.apply_block(&block, height, &BTreeSet::from([wallet_id])).await;
let wallets = BTreeSet::from([wallet_id]);
let result = self.apply_block(&block, height, &wallets).await;
let confirmed_txids = result.relevant_txids().cloned().collect();
queue.extend(
result.reapply_heights.into_iter().flat_map(|(wallet_id, heights)| {
heights.into_iter().map(move |h| (h, wallet_id))
}),
);
events.push(SyncEvent::BlockProcessed {
block_hash: *block.hash(),
height,
wallets,
new_scripts: result.new_scripts,
confirmed_txids,
});
}
events
}
}

Expand Down Expand Up @@ -214,10 +231,10 @@ mod tests {

use dashcore::network::message::NetworkMessage;
use dashcore::network::message_blockdata::Inventory;
use dashcore::{Block, BlockHash};
use dashcore::{Address, Block, BlockHash, Network};
use key_wallet_manager::test_utils::{MockWallet, MOCK_WALLET_ID};
use key_wallet_manager::FilterMatchKey;
use std::collections::BTreeSet;
use std::collections::{BTreeMap, BTreeSet};

type TestBlocksManager =
BlocksManager<PersistentBlockHeaderStorage, PersistentBlockStorage, MockWallet>;
Expand Down Expand Up @@ -349,11 +366,15 @@ mod tests {
assert_eq!(processed[0].1, 100);
}

/// A re-application reports its own `BlockProcessed` with the scripts it
/// derived, but does not count as another processed block.
#[tokio::test]
async fn test_process_buffered_blocks_reapplies_requested_stored_block() {
let storage = DiskStorageManager::with_temp_dir().await.unwrap();
let mut wallet = MockWallet::new();
wallet.set_reapply_heights(100, BTreeSet::from([200]));
let derived = Address::dummy(Network::Regtest, 7);
wallet.set_derived_addresses(200, vec![derived.clone()]);
let wallet = Arc::new(RwLock::new(wallet));
let mut manager: TestBlocksManager =
BlocksManager::new(wallet.clone(), storage.block_headers(), storage.blocks()).await;
Expand All @@ -373,9 +394,23 @@ mod tests {
);

let events = manager.process_buffered_blocks().await.unwrap();
let reported: Vec<_> = events
.iter()
.filter_map(|event| match event {
SyncEvent::BlockProcessed {
height,
wallets,
new_scripts,
..
} => Some((*height, wallets, new_scripts)),
_ => None,
})
.collect();
let wallets = BTreeSet::from([MOCK_WALLET_ID]);
let derived_scripts = BTreeMap::from([(MOCK_WALLET_ID, vec![derived.script_pubkey()])]);
assert_eq!(
events.iter().filter(|e| matches!(e, SyncEvent::BlockProcessed { .. })).count(),
1
reported,
vec![(100, &wallets, &BTreeMap::new()), (200, &wallets, &derived_scripts)]
);

let processed = wallet.read().await.processed_blocks();
Expand Down
3 changes: 2 additions & 1 deletion dash-spv/src/sync/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ pub enum SyncEvent {
blocks: BTreeMap<FilterMatchKey, BTreeSet<WalletId>>,
},

/// Block downloaded and processed through wallet.
/// Block downloaded and processed through wallet. Also emitted for each
/// re-application of a stored block, with the one wallet it was re-applied for.
///
/// Emitted by: `BlocksManager`
/// Consumed by: `FiltersManager` (for gap limit rescanning),
Expand Down
31 changes: 17 additions & 14 deletions dash-spv/src/sync/filters/batch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use dashcore::bip158::BlockFilter;
use dashcore::ScriptBuf;
use key_wallet_manager::{FilterMatchKey, WalletId};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;
Expand Down Expand Up @@ -32,8 +31,9 @@ pub(super) struct FiltersBatch {
/// current account set (dashpay/rust-dashcore#649). Already-synced wallets
/// must not be touched.
scanned_wallets: BTreeMap<WalletId, u64>,
/// Every script already matched against this batch's filters, per wallet.
tested_scripts: HashMap<WalletId, HashSet<ScriptBuf>>,
/// Every script and filter element already matched against this batch's
/// filters, per wallet, as the bytes the filters were queried with.
tested: HashMap<WalletId, HashSet<Vec<u8>>>,
}

impl FiltersBatch {
Expand All @@ -51,7 +51,7 @@ impl FiltersBatch {
scanned: false,
pending_blocks: 0,
scanned_wallets: BTreeMap::new(),
tested_scripts: HashMap::new(),
tested: HashMap::new(),
}
}
/// Start height of this batch (inclusive).
Expand Down Expand Up @@ -103,23 +103,26 @@ impl FiltersBatch {
self.pending_blocks = self.pending_blocks.saturating_sub(1);
self.pending_blocks
}
/// Record that `scripts` have been matched against this batch's filters.
pub(super) fn mark_tested<I: IntoIterator<Item = ScriptBuf>>(
/// Record that `items`, scripts or filter elements, have been matched
/// against this batch's filters.
pub(super) fn mark_tested<T: AsRef<[u8]>>(
&mut self,
wallet_id: WalletId,
scripts: I,
items: impl IntoIterator<Item = T>,
) {
self.tested_scripts.entry(wallet_id).or_default().extend(scripts);
let tested = self.tested.entry(wallet_id).or_default();
tested.extend(items.into_iter().map(|item| item.as_ref().to_vec()));
}

/// The wallet's scripts that this batch has never been matched against.
pub(super) fn untested<'a>(
/// The wallet's scripts or filter elements that this batch has never been
/// matched against.
pub(super) fn untested<'a, T: AsRef<[u8]>>(
&'a self,
wallet_id: &WalletId,
monitored: &'a [ScriptBuf],
) -> impl Iterator<Item = &'a ScriptBuf> {
let tested = self.tested_scripts.get(wallet_id);
monitored.iter().filter(move |script| tested.is_none_or(|t| !t.contains(*script)))
monitored: &'a [T],
) -> impl Iterator<Item = &'a T> {
let tested = self.tested.get(wallet_id);
monitored.iter().filter(move |item| tested.is_none_or(|t| !t.contains(item.as_ref())))
}

/// Record the wallets that were behind for this batch at scan time, each
Expand Down
Loading
Loading