Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ use crate::wallet::error::{BuildFeeBumpError, CreateTxError, MiniscriptPsbtError

const COINBASE_MATURITY: u32 = 100;

// The default "gap limit" for a wallet.
const DEFAULT_LOOKAHEAD: u32 = 1_000;

/// A Bitcoin wallet
///
/// The `Wallet` struct acts as a way of coherently interfacing with output descriptors and related transactions.
Expand Down Expand Up @@ -461,7 +464,8 @@ impl<D> Wallet<D> {
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 @@ -2308,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