Skip to content

Commit

Permalink
wollet: full scan up to derivation index
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Jan 3, 2025
1 parent d643ae8 commit adef9c8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
13 changes: 12 additions & 1 deletion lwk_bindings/src/electrum_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,19 @@ impl ElectrumClient {
}

pub fn full_scan(&self, wollet: &Wollet) -> Result<Option<Arc<Update>>, LwkError> {
self.full_scan_to_index(wollet, 0)
}

pub fn full_scan_to_index(
&self,
wollet: &Wollet,
index: u32,
) -> Result<Option<Arc<Update>>, LwkError> {
let wollet = wollet.inner_wollet()?;
let update: Option<lwk_wollet::Update> = self.inner.lock()?.full_scan(&wollet.state())?;
let update: Option<lwk_wollet::Update> = self
.inner
.lock()?
.full_scan_to_index(&wollet.state(), index)?;
Ok(update.map(Into::into).map(Arc::new))
}
}
13 changes: 12 additions & 1 deletion lwk_bindings/src/esplora_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,19 @@ impl EsploraClient {
}

pub fn full_scan(&self, wollet: &Wollet) -> Result<Option<Arc<Update>>, LwkError> {
self.full_scan_to_index(wollet, 0)
}

pub fn full_scan_to_index(
&self,
wollet: &Wollet,
index: u32,
) -> Result<Option<Arc<Update>>, LwkError> {
let wollet = wollet.inner_wollet()?;
let update: Option<lwk_wollet::Update> = self.inner.lock()?.full_scan(&wollet.state())?;
let update: Option<lwk_wollet::Update> = self
.inner
.lock()?
.full_scan_to_index(&wollet.state(), index)?;
Ok(update.map(Into::into).map(Arc::new))
}
}
21 changes: 18 additions & 3 deletions lwk_wollet/src/clients/blocking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub trait BlockchainBackend {
&mut self,
descriptor: &WolletDescriptor,
state: &S,
index: u32,
) -> Result<Data, Error> {
let mut data = Data::default();

Expand Down Expand Up @@ -91,7 +92,7 @@ pub trait BlockchainBackend {

let flattened: Vec<History> = result.into_iter().flatten().collect();

if flattened.is_empty() {
if flattened.is_empty() && index <= 1 + batch_count * BATCH_SIZE {
break;
}

Expand Down Expand Up @@ -127,6 +128,18 @@ pub trait BlockchainBackend {

/// Scan the blockchain for the scripts generated by a watch-only wallet
fn full_scan<S: WolletState>(&mut self, state: &S) -> Result<Option<Update>, Error> {
self.full_scan_to_index(state, 0)
}

/// Scan the blockchain for the scripts generated by a watch-only wallet
///
/// Will scan up to a specified derivation index before stopping to prevent gap limit issues
/// if the number of addresses derived is already known
fn full_scan_to_index<S: WolletState>(
&mut self,
state: &S,
index: u32,
) -> Result<Option<Update>, Error> {
let descriptor = state.descriptor();

let Data {
Expand All @@ -139,11 +152,13 @@ pub trait BlockchainBackend {
} = if self.capabilities().contains(&Capability::Waterfalls) {
match self.get_history_waterfalls(&descriptor, state) {
Ok(d) => d,
Err(Error::UsingWaterfallsWithElip151) => self.get_history(&descriptor, state)?,
Err(Error::UsingWaterfallsWithElip151) => {
self.get_history(&descriptor, state, index)?
}
Err(e) => return Err(e),
}
} else {
self.get_history(&descriptor, state)?
self.get_history(&descriptor, state, index)?
};

let tip = self.tip()?;
Expand Down
2 changes: 2 additions & 0 deletions lwk_wollet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ pub use crate::wollet::{Tip, Wollet};
#[cfg(feature = "electrum")]
pub use crate::wollet::full_scan_with_electrum_client;
#[cfg(feature = "electrum")]
pub use crate::wollet::full_scan_to_index_with_electrum_client;
#[cfg(feature = "electrum")]
pub use clients::blocking::electrum_client::{ElectrumClient, ElectrumOptions, ElectrumUrl};

#[cfg(feature = "esplora")]
Expand Down
11 changes: 10 additions & 1 deletion lwk_wollet/src/wollet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,19 @@ fn tx_balance(
pub fn full_scan_with_electrum_client(
wollet: &mut Wollet,
electrum_client: &mut crate::ElectrumClient,
) -> Result<(), Error> {
full_scan_to_index_with_electrum_client(wollet, 0, electrum_client)
}

#[cfg(feature = "electrum")]
pub fn full_scan_to_index_with_electrum_client(
wollet: &mut Wollet,
index: u32,
electrum_client: &mut crate::ElectrumClient,
) -> Result<(), Error> {
use crate::clients::blocking::BlockchainBackend;

let update = electrum_client.full_scan(wollet)?;
let update = electrum_client.full_scan_to_index(wollet, index)?;
if let Some(update) = update {
wollet.apply_update(update)?
}
Expand Down

0 comments on commit adef9c8

Please sign in to comment.