Skip to content
Open
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
18 changes: 9 additions & 9 deletions dash-spv-ffi/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,19 +362,19 @@ impl FFISyncEventCallbacks {
SyncEvent::BlockProcessed {
block_hash,
height,
new_addresses,
new_scripts,
confirmed_txids,
..
} => {
if let Some(cb) = self.on_block_processed {
let hash_bytes = block_hash.as_byte_array();
let txid_bytes: Vec<[u8; 32]> =
confirmed_txids.iter().map(|txid| *txid.as_byte_array()).collect();
let total_new_addresses: usize = new_addresses.values().map(|v| v.len()).sum();
let total_new_scripts: usize = new_scripts.values().map(|v| v.len()).sum();
cb(
*height,
hash_bytes as *const [u8; 32],
total_new_addresses as u32,
total_new_scripts as u32,
txid_bytes.as_ptr(),
txid_bytes.len() as u32,
self.user_data,
Expand Down Expand Up @@ -1185,7 +1185,7 @@ impl FFIWalletEventCallbacks {
mod tests {
use super::*;
use dashcore::hashes::Hash;
use dashcore::{Address, BlockHash, ChainLock, Network, Txid};
use dashcore::{Address, BlockHash, ChainLock, Network, ScriptBuf, Txid};
use key_wallet_manager::{FilterMatchKey, WalletId};
use std::collections::{BTreeMap, BTreeSet};
use std::sync::atomic::{AtomicU32, Ordering};
Expand Down Expand Up @@ -1247,16 +1247,16 @@ mod tests {
let addr_a = Address::dummy(Network::Regtest, 1);
let addr_b = Address::dummy(Network::Regtest, 2);
let addr_c = Address::dummy(Network::Regtest, 3);
let mut new_addresses: BTreeMap<WalletId, Vec<Address>> = BTreeMap::new();
// Wallet 1 contributes 2 new addresses, wallet 2 contributes 1. Total = 3.
new_addresses.insert([1u8; 32], vec![addr_a, addr_b]);
new_addresses.insert([2u8; 32], vec![addr_c]);
let mut new_scripts: BTreeMap<WalletId, Vec<ScriptBuf>> = BTreeMap::new();
// Wallet 1 contributes 2 new scripts, wallet 2 contributes 1. Total = 3.
new_scripts.insert([1u8; 32], vec![addr_a.script_pubkey(), addr_b.script_pubkey()]);
new_scripts.insert([2u8; 32], vec![addr_c.script_pubkey()]);

callbacks.dispatch(&SyncEvent::BlockProcessed {
block_hash: BlockHash::from_byte_array([7u8; 32]),
height: 100,
wallets: BTreeSet::new(),
new_addresses,
new_scripts,
confirmed_txids: vec![Txid::from_byte_array([9u8; 32])],
});
assert_eq!(NEW_ADDR_COUNT.load(Ordering::SeqCst), 3);
Expand Down
22 changes: 11 additions & 11 deletions dash-spv/src/sync/blocks/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,30 @@ impl<H: BlockHeaderStorage, B: BlockStorage, W: WalletInterface> BlocksManager<H
drop(wallet);

let total_relevant = result.relevant_tx_count();
let new_addresses_total: usize = result.new_addresses.values().map(|v| v.len()).sum();
let new_scripts_total: usize = result.new_scripts.values().map(|v| v.len()).sum();
if total_relevant > 0 {
tracing::info!(
"Found {} relevant transactions ({} new, {} existing) {} at height {}, new addresses: {}",
"Found {} relevant transactions ({} new, {} existing) {} at height {}, new scripts: {}",
total_relevant,
result.new_txids.len(),
result.existing_txids.len(),
hash,
height,
new_addresses_total
new_scripts_total
);
}

// Collect confirmed txids before moving new_addresses out of result
// Collect confirmed txids before moving new_scripts out of result
let confirmed_txids: Vec<_> = result.relevant_txids().cloned().collect();

// Collect new addresses for gap limit rescanning
let new_addresses = result.new_addresses;
if new_addresses_total > 0 {
// Collect new scripts for gap limit rescanning
let new_scripts = result.new_scripts;
if new_scripts_total > 0 {
tracing::debug!(
"Block {} generated {} new addresses for gap limit maintenance across {} wallets",
"Block {} generated {} new scripts for gap limit maintenance across {} wallets",
height,
new_addresses_total,
new_addresses.len()
new_scripts_total,
new_scripts.len()
);
}

Expand All @@ -128,7 +128,7 @@ impl<H: BlockHeaderStorage, B: BlockStorage, W: WalletInterface> BlocksManager<H
block_hash: hash,
height,
wallets: interested,
new_addresses,
new_scripts,
confirmed_txids,
});
}
Expand Down
14 changes: 7 additions & 7 deletions dash-spv/src/sync/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::sync::ManagerIdentifier;
use dashcore::ephemerealdata::chain_lock::ChainLock;
use dashcore::ephemerealdata::instant_lock::InstantLock;
use dashcore::sml::masternode_list_engine::QRInfoFeedResult;
use dashcore::{Address, BlockHash, Txid};
use dashcore::{BlockHash, ScriptBuf, Txid};
use key_wallet_manager::{FilterMatchKey, WalletId};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
Expand Down Expand Up @@ -105,9 +105,9 @@ pub enum SyncEvent {
height: u32,
/// Wallets the block was actually processed for.
wallets: BTreeSet<WalletId>,
/// New addresses discovered from wallet gap limit maintenance, attributed
/// to the wallet that produced them.
new_addresses: BTreeMap<WalletId, Vec<Address>>,
/// Cached scriptPubKeys for addresses freshly derived via wallet
/// gap-limit maintenance, attributed to the wallet that produced them.
new_scripts: BTreeMap<WalletId, Vec<ScriptBuf>>,
/// Transaction IDs confirmed in this block that are relevant to the wallet
confirmed_txids: Vec<Txid>,
},
Expand Down Expand Up @@ -214,11 +214,11 @@ impl fmt::Display for SyncEvent {
} => write!(f, "BlocksNeeded(count={})", blocks.len()),
SyncEvent::BlockProcessed {
height,
new_addresses,
new_scripts,
..
} => {
let total: usize = new_addresses.values().map(|v| v.len()).sum();
write!(f, "BlockProcessed(height={}, new_addrs={})", height, total)
let total: usize = new_scripts.values().map(|v| v.len()).sum();
write!(f, "BlockProcessed(height={}, new_scripts={})", height, total)
}
SyncEvent::MasternodeStateUpdated {
height,
Expand Down
26 changes: 13 additions & 13 deletions dash-spv/src/sync/filters/batch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dashcore::bip158::BlockFilter;
use dashcore::Address;
use dashcore::ScriptBuf;
use key_wallet_manager::{FilterMatchKey, WalletId};
use std::collections::{BTreeSet, HashMap, HashSet};

Expand Down Expand Up @@ -28,10 +28,10 @@ pub(super) struct FiltersBatch {
/// therefore need their `synced_height` advanced when the batch commits.
/// Already-synced wallets must not be touched.
scanned_wallets: BTreeSet<WalletId>,
/// Addresses discovered during block processing that still need rescan,
/// attributed per wallet so we can rerun matching only against the wallet
/// that produced each new address.
collected_addresses: HashMap<WalletId, HashSet<Address>>,
/// Cached scriptPubKeys discovered during block processing that still
/// need rescan, attributed per wallet so we can rerun matching only
/// against the wallet that produced each new script.
collected_scripts: HashMap<WalletId, HashSet<ScriptBuf>>,
}

impl FiltersBatch {
Expand All @@ -50,7 +50,7 @@ impl FiltersBatch {
pending_blocks: 0,
rescan_complete: false,
scanned_wallets: BTreeSet::new(),
collected_addresses: HashMap::new(),
collected_scripts: HashMap::new(),
}
}
/// Start height of this batch (inclusive).
Expand Down Expand Up @@ -106,17 +106,17 @@ impl FiltersBatch {
pub(super) fn mark_rescan_complete(&mut self) {
self.rescan_complete = true;
}
/// Add addresses discovered during block processing for later rescan.
pub(super) fn add_addresses_for_wallet(
/// Add scriptPubKeys discovered during block processing for later rescan.
pub(super) fn add_scripts_for_wallet(
&mut self,
wallet_id: WalletId,
addresses: impl IntoIterator<Item = Address>,
scripts: impl IntoIterator<Item = ScriptBuf>,
) {
self.collected_addresses.entry(wallet_id).or_default().extend(addresses);
self.collected_scripts.entry(wallet_id).or_default().extend(scripts);
}
/// Take collected per-wallet addresses for rescan, leaving the map empty.
pub(super) fn take_collected_addresses(&mut self) -> HashMap<WalletId, HashSet<Address>> {
std::mem::take(&mut self.collected_addresses)
/// Take collected per-wallet scripts for rescan, leaving the map empty.
pub(super) fn take_collected_scripts(&mut self) -> HashMap<WalletId, HashSet<ScriptBuf>> {
std::mem::take(&mut self.collected_scripts)
}
/// Record the set of wallets that were behind for this batch at scan time.
pub(super) fn set_scanned_wallets(&mut self, wallets: BTreeSet<WalletId>) {
Expand Down
Loading
Loading