Add wallet::scan_blocks()
This commit is contained in:
parent
ff3b00e651
commit
b6e28b8750
@ -7,3 +7,5 @@ mod peers;
|
||||
mod user;
|
||||
mod wallet;
|
||||
mod scanner;
|
||||
|
||||
const WITH_CUTTHROUGH: bool = true;
|
||||
|
@ -1,17 +1,18 @@
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
sync::{Mutex, MutexGuard, OnceLock},
|
||||
sync::{atomic::AtomicBool, Arc, Mutex, MutexGuard, OnceLock},
|
||||
};
|
||||
use web_time::Instant;
|
||||
|
||||
use anyhow::Error;
|
||||
use rand::Rng;
|
||||
use sdk_common::sp_client::{
|
||||
bitcoin::{secp256k1::SecretKey, Network, OutPoint},
|
||||
use sdk_common::{backend_blindbit_wasm::{BlindbitBackend, SpScanner}, log, silentpayments::SpWallet, sp_client::{
|
||||
bitcoin::{absolute::Height, secp256k1::SecretKey, Amount, Network, OutPoint},
|
||||
silentpayments::SilentPaymentAddress,
|
||||
SpClient, SpendKey,
|
||||
};
|
||||
}, updates::StateUpdater};
|
||||
|
||||
use crate::MutexExt;
|
||||
use crate::{scanner::WasmSpScanner, MutexExt, WITH_CUTTHROUGH};
|
||||
|
||||
pub static FREEZED_UTXOS: OnceLock<Mutex<HashSet<OutPoint>>> = OnceLock::new();
|
||||
|
||||
@ -29,3 +30,59 @@ pub fn generate_sp_wallet(network: Network) -> anyhow::Result<SpClient> {
|
||||
network,
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn scan_blocks(mut n_blocks_to_scan: u32, blindbit_url: &str, sp_wallet: &SpWallet, tip_height: u32, scan_height: u32) -> anyhow::Result<()> {
|
||||
log::info!("Starting a rescan");
|
||||
|
||||
// 0 means scan to tip
|
||||
if n_blocks_to_scan == 0 {
|
||||
n_blocks_to_scan = tip_height - scan_height;
|
||||
}
|
||||
|
||||
let start = scan_height + 1;
|
||||
let end = if scan_height + n_blocks_to_scan <= tip_height {
|
||||
scan_height + n_blocks_to_scan
|
||||
} else {
|
||||
tip_height
|
||||
};
|
||||
|
||||
if start > end {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let updater = StateUpdater::new();
|
||||
let backend = BlindbitBackend::new(blindbit_url.to_string())?;
|
||||
|
||||
let owned_outpoints = sp_wallet.get_unspent_outputs().keys().map(|o| *o).collect();
|
||||
|
||||
let keep_scanning = Arc::new(AtomicBool::new(true));
|
||||
|
||||
log::info!("start: {} end: {}", start, end);
|
||||
let start_time = Instant::now();
|
||||
log::info!("{:?}", start_time);
|
||||
let mut scanner = WasmSpScanner::new(
|
||||
sp_wallet.get_sp_client().clone(),
|
||||
Box::new(updater),
|
||||
Box::new(backend),
|
||||
owned_outpoints,
|
||||
&keep_scanning,
|
||||
);
|
||||
|
||||
let dust_limit = Amount::from_sat(0); // We don't really have a dust limit for this use case
|
||||
scanner
|
||||
.scan_blocks(
|
||||
Height::from_consensus(start)?,
|
||||
Height::from_consensus(end)?,
|
||||
dust_limit,
|
||||
WITH_CUTTHROUGH,
|
||||
)
|
||||
.await?;
|
||||
|
||||
// time elapsed for the scan
|
||||
log::info!(
|
||||
"Scan complete in {} seconds",
|
||||
start_time.elapsed().as_secs()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user