Skip to content

Commit

Permalink
Merge ValuedMammal/bdk-kyoto#48: feat(builder): use wallet lookahead …
Browse files Browse the repository at this point in the history
…for recovery

7cfb3f5 feat(builder): use wallet lookahead for recovery (Rob N)

Pull request description:

  i think this should resolve the recovery issue. developers should be specifying the lookahead if more than 25 scripts were used per keychain, so we just take that lookahead here.

  cc @ValuedMammal

ACKs for top commit:
  ValuedMammal:
    ACK 7cfb3f5

Tree-SHA512: d7402c027fa2a9ed3b68cd564ff42aaa8494e125d3e810747eea8da86180bb18c4505826104772c76de95c46683ffa2fa55f1d0aec6fc64fe55b14bc49f8f2d0
  • Loading branch information
ValuedMammal committed Sep 10, 2024
2 parents 45ec5a5 + 7cfb3f5 commit d5bdfaa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions examples/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ async fn main() -> anyhow::Result<()> {

let mut wallet = Wallet::create(desc, change_desc)
.network(Network::Signet)
.lookahead(30)
.create_wallet_no_persist()?;

// The light client builder handles the logic of inserting the SPKs
let (node, mut client) = LightClientBuilder::new(&wallet)
.scan_after(170_000)
.peers(peers)
.use_lookahead_scripts()
.build()
.unwrap();

Expand Down
27 changes: 21 additions & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use kyoto::{

use crate::Client;

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

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

impl<'a> LightClientBuilder<'a> {
Expand All @@ -81,6 +82,7 @@ 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 @@ -109,6 +111,14 @@ 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 @@ -193,15 +203,20 @@ impl<'a> LightClientBuilder<'a> {
node_builder =
node_builder.num_required_peers(self.connections.unwrap_or(RECOMMENDED_PEERS));
let mut spks: HashSet<ScriptBuf> = HashSet::new();
// Reveal 20 scripts ahead of the last revealed index so we don't miss any transactions.
for keychain in [KeychainKind::External, KeychainKind::Internal] {
for index in 0..=self
// The user may choose to recover a wallet with lookahead scripts
// or use the last revealed index plus some padding to find new transactions
let last_revealed = self
.wallet
.spk_index()
.last_revealed_index(keychain)
.unwrap_or(0)
+ TARGET_INDEX
{
.unwrap_or(0);
let lookahead_index = if self.lookahead {
last_revealed + self.wallet.spk_index().lookahead()
} else {
last_revealed + PEEK_INDEX
};
for index in 0..=lookahead_index {
spks.insert(self.wallet.peek_address(keychain, index).script_pubkey());
}
}
Expand Down

0 comments on commit d5bdfaa

Please sign in to comment.