Skip to content

Commit

Permalink
fix(bdk): set a reasonable default lookahead when creating a wallet
Browse files Browse the repository at this point in the history
Added wallet test "test_get_funded_wallet_balance_for_nonzero_index" to test new DEFAULT_LOOKAHEAD.

WIP: other tests still failing and need to be investigated.
  • Loading branch information
notmandatory committed Nov 28, 2023
1 parent 2194abb commit 9d3cb2b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
18 changes: 16 additions & 2 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,13 @@ impl<D> Wallet<D> {
let secp = Secp256k1::new();
let (chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash);
let mut index = KeychainTxOutIndex::<KeychainKind>::default();
index.set_lookahead_for_all(DEFAULT_LOOKAHEAD);

let (signers, change_signers) =
create_signers(&mut index, &secp, descriptor, change_descriptor, network)
.map_err(NewError::Descriptor)?;

let indexed_graph = IndexedTxGraph::new(index);
let mut indexed_graph = IndexedTxGraph::new(index);
indexed_graph.index.set_lookahead_for_all(DEFAULT_LOOKAHEAD);

let mut persist = Persist::new(db);
persist.stage(ChangeSet {
Expand Down Expand Up @@ -2312,6 +2312,20 @@ impl<D> Wallet<D> {
&self.indexed_graph.index
}

// /// Set's inner [`KeychainTxOutIndex`] look ahead count for all keychains.
// ///
// /// See [`KeychainTxOutIndex::set_lookahead_for_all`] for more details.
// pub fn set_spk_lookahead_for_all(&mut self, lookahead: u32) {
// self.indexed_graph.index.set_lookahead_for_all(lookahead);
// }
//
// /// Set's inner [`KeychainTxOutIndex`] look ahead count for the specified keychain.
// ///
// /// See [`KeychainTxOutIndex::set_lookahead`] for more details.
// pub fn set_lookahead(&mut self, keychain: &KeychainKind, lookahead: u32) {
// self.indexed_graph.index.set_lookahead(keychain, lookahead);
// }

/// Get a reference to the inner [`LocalChain`].
pub fn local_chain(&self) -> &LocalChain {
&self.chain
Expand Down
49 changes: 49 additions & 0 deletions crates/bdk/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,59 @@ pub fn get_funded_wallet(descriptor: &str) -> (Wallet, bitcoin::Txid) {
get_funded_wallet_with_change(descriptor, None)
}

// Return a fake wallet that appears to have one UTXO sent at a specific address index.
//
// The funded wallet containing a tx with one 76_000 sats input.
pub fn get_funded_wallet_at_index(descriptor: &str, address_index: AddressIndex) -> (Wallet, Txid) {
let mut wallet = Wallet::new_no_persist(descriptor, None, Network::Regtest).unwrap();
let utxo_address = wallet.get_address(address_index).address;

let tx0 = Transaction {
version: 1,
lock_time: bitcoin::absolute::LockTime::ZERO,
input: vec![TxIn {
previous_output: OutPoint {
txid: Txid::all_zeros(),
vout: 0,
},
script_sig: Default::default(),
sequence: Default::default(),
witness: Default::default(),
}],
output: vec![TxOut {
value: 76_000,
script_pubkey: utxo_address.script_pubkey(),
}],
};

wallet
.insert_checkpoint(BlockId {
height: 1_000,
hash: BlockHash::all_zeros(),
})
.unwrap();

wallet
.insert_tx(
tx0.clone(),
ConfirmationTime::Confirmed {
height: 1_000,
time: 100,
},
)
.unwrap();

(wallet, tx0.txid())
}

pub fn get_test_wpkh() -> &'static str {
"wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)"
}

pub fn get_test_wpkh_xprv() -> &'static str {
"wpkh(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/*)"
}

pub fn get_test_single_sig_csv() -> &'static str {
// and(pk(Alice),older(6))
"wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),older(6)))"
Expand Down
8 changes: 8 additions & 0 deletions crates/bdk/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ fn test_get_funded_wallet_balance() {
assert_eq!(wallet.get_balance().confirmed, 50_000);
}

#[test]
fn test_get_funded_wallet_balance_for_nonzero_index() {
// The funded wallet contains a tx with a 76_000 sats outputs to spk at index 1 that is found
// with the default look ahead.
let (wallet, _) = get_funded_wallet_at_index(get_test_wpkh_xprv(), Peek(1));
assert_eq!(wallet.get_balance().confirmed, 76_000);
}

#[test]
fn test_get_funded_wallet_sent_and_received() {
let (wallet, txid) = get_funded_wallet(get_test_wpkh());
Expand Down

0 comments on commit 9d3cb2b

Please sign in to comment.