Skip to content

Commit

Permalink
Merge pull request #60 from rustaceanrob/lookahead-09-30
Browse files Browse the repository at this point in the history
Always use lookahead in builder
  • Loading branch information
rustaceanrob authored Oct 1, 2024
2 parents e9ac942 + e08fa53 commit 4f7d9e5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
1 change: 0 additions & 1 deletion examples/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ async fn main() -> anyhow::Result<()> {
let (node, mut client) = LightClientBuilder::new(&wallet)
.scan_after(170_000)
.peers(peers)
.use_lookahead_scripts()
.build()
.unwrap();

Expand Down
18 changes: 1 addition & 17 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ use kyoto::{

use crate::Client;

const PEEK_INDEX: u32 = 20;
const RECOMMENDED_PEERS: u8 = 2;

#[derive(Debug)]
Expand All @@ -70,7 +69,6 @@ pub struct LightClientBuilder<'a> {
birthday_height: Option<u32>,
data_dir: Option<PathBuf>,
timeout: Option<Duration>,
lookahead: bool,
}

impl<'a> LightClientBuilder<'a> {
Expand All @@ -83,7 +81,6 @@ impl<'a> LightClientBuilder<'a> {
birthday_height: None,
data_dir: None,
timeout: None,
lookahead: false,
}
}
/// Add peers to connect to over the P2P network.
Expand Down Expand Up @@ -112,15 +109,6 @@ impl<'a> LightClientBuilder<'a> {
self
}

/// Use all the scripts up to the wallet's lookahead parameter. Only to be used during wallet
/// recovery, when we do not have any knowledge of the scripts used in the wallet yet. For
/// more information on the wallet lookahead,
/// see [`KeychainTxOutIndex`](bdk_wallet::chain::indexer::keychain_txout::KeychainTxOutIndex).
pub fn use_lookahead_scripts(mut self) -> Self {
self.lookahead = true;
self
}

/// Configure the duration of time a remote node has to respond to a message.
pub fn timeout_duration(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
Expand Down Expand Up @@ -215,11 +203,7 @@ impl<'a> LightClientBuilder<'a> {
.spk_index()
.last_revealed_index(keychain)
.unwrap_or(0);
let lookahead_index = if self.lookahead {
last_revealed + self.wallet.spk_index().lookahead()
} else {
last_revealed + PEEK_INDEX
};
let lookahead_index = last_revealed + self.wallet.spk_index().lookahead();
for index in 0..=lookahead_index {
spks.insert(self.wallet.peek_address(keychain, index).script_pubkey());
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ where

/// Add more scripts to the node. For example, a user may reveal a Bitcoin address to receive a
/// payment, so this script should be added to the [`Node`].
///
/// ## Note
///
/// When using the [`LightClientBuidler`](crate::builder), the wallet lookahead will be used
/// to peek ahead and scan for additional scripts.
pub async fn add_script(&self, script: impl Into<ScriptBuf>) -> Result<(), ClientError> {
self.client.add_script(script).await
}
Expand Down

0 comments on commit 4f7d9e5

Please sign in to comment.