Skip to content

Commit

Permalink
test(wallet): improve usage of test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Nov 5, 2024
1 parent 9bdf4cb commit ab27884
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 139 deletions.
70 changes: 35 additions & 35 deletions crates/wallet/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ use crate::{KeychainKind, Update, Wallet};
/// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
/// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
/// sats are the transaction fee.
pub fn get_funded_wallet(descriptor: &str, change_descriptor: &str) -> (Wallet, bitcoin::Txid) {
pub fn get_funded_wallet(descriptor: &str, change_descriptor: &str) -> (Wallet, Txid) {
new_funded_wallet(descriptor, Some(change_descriptor))
}

fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wallet, bitcoin::Txid) {
fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wallet, Txid) {
let params = if let Some(change_desc) = change_descriptor {
Wallet::create(descriptor.to_string(), change_desc.to_string())
} else {
Expand All @@ -40,34 +40,20 @@ fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wall
.unwrap();

let tx0 = Transaction {
version: transaction::Version::ONE,
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: Amount::from_sat(76_000),
script_pubkey: receive_address.script_pubkey(),
}],
..new_tx(0)
};

let tx1 = Transaction {
version: transaction::Version::ONE,
lock_time: bitcoin::absolute::LockTime::ZERO,
input: vec![TxIn {
previous_output: OutPoint {
txid: tx0.compute_txid(),
vout: 0,
},
script_sig: Default::default(),
sequence: Default::default(),
witness: Default::default(),
..Default::default()
}],
output: vec![
TxOut {
Expand All @@ -79,28 +65,32 @@ fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wall
script_pubkey: sendto_address.script_pubkey(),
},
],
..new_tx(0)
};

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

wallet.insert_tx(tx0.clone());
insert_tx(&mut wallet, tx0.clone());
insert_anchor(
&mut wallet,
tx0.compute_txid(),
Expand All @@ -113,7 +103,7 @@ fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wall
},
);

wallet.insert_tx(tx1.clone());
insert_tx(&mut wallet, tx1.clone());
insert_anchor(
&mut wallet,
tx1.compute_txid(),
Expand All @@ -134,12 +124,12 @@ fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wall
/// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
/// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
/// sats are the transaction fee.
pub fn get_funded_wallet_single(descriptor: &str) -> (Wallet, bitcoin::Txid) {
pub fn get_funded_wallet_single(descriptor: &str) -> (Wallet, Txid) {
new_funded_wallet(descriptor, None)
}

/// Get funded segwit wallet
pub fn get_funded_wallet_wpkh() -> (Wallet, bitcoin::Txid) {
pub fn get_funded_wallet_wpkh() -> (Wallet, Txid) {
let (desc, change_desc) = get_test_wpkh_and_change_desc();
get_funded_wallet(desc, change_desc)
}
Expand Down Expand Up @@ -211,6 +201,16 @@ pub fn get_test_tr_dup_keys() -> &'static str {
"tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG,{pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})"
}

/// A new empty transaction with the given locktime
pub fn new_tx(locktime: u32) -> Transaction {
Transaction {
version: transaction::Version::ONE,
lock_time: absolute::LockTime::from_consensus(locktime),
input: vec![],
output: vec![],
}
}

/// Construct a new [`FeeRate`] from the given raw `sat_vb` feerate. This is
/// useful in cases where we want to create a feerate from a `f64`, as the
/// traditional [`FeeRate::from_sat_per_vb`] method will only accept an integer.
Expand Down Expand Up @@ -267,7 +267,7 @@ pub fn receive_output_to_address(
};

let txid = tx.compute_txid();
wallet.insert_tx(tx);
insert_tx(wallet, tx);

match pos {
ChainPosition::Confirmed(anchor) => {
Expand Down
43 changes: 8 additions & 35 deletions crates/wallet/src/wallet/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,55 +213,28 @@ impl FullyNodedExport {

#[cfg(test)]
mod test {
use alloc::string::ToString;
use core::str::FromStr;

use crate::std::string::ToString;
use bdk_chain::{BlockId, ConfirmationBlockTime};
use bitcoin::hashes::Hash;
use bitcoin::{transaction, BlockHash, Network, Transaction};
use chain::tx_graph;
use bdk_chain::BlockId;
use bitcoin::{hashes::Hash, BlockHash, Network};

use super::*;
use crate::test_utils::*;
use crate::Wallet;

fn get_test_wallet(descriptor: &str, change_descriptor: &str, network: Network) -> Wallet {
use crate::wallet::Update;
let mut wallet = Wallet::create(descriptor.to_string(), change_descriptor.to_string())
.network(network)
.create_wallet_no_persist()
.expect("must create wallet");
let transaction = Transaction {
input: vec![],
output: vec![],
version: transaction::Version::non_standard(0),
lock_time: bitcoin::absolute::LockTime::ZERO,
};
let txid = transaction.compute_txid();
let block_id = BlockId {
let block = BlockId {
height: 5000,
hash: BlockHash::all_zeros(),
};
wallet.insert_checkpoint(block_id).unwrap();
wallet
.insert_checkpoint(BlockId {
height: 5001,
hash: BlockHash::all_zeros(),
})
.unwrap();
wallet.insert_tx(transaction);
let anchor = ConfirmationBlockTime {
confirmation_time: 0,
block_id,
};
wallet
.apply_update(Update {
tx_update: tx_graph::TxUpdate {
anchors: [(anchor, txid)].into_iter().collect(),
..Default::default()
},
..Default::default()
})
.unwrap();
insert_checkpoint(&mut wallet, block);
receive_output_in_latest_block(&mut wallet, 10_000);

wallet
}

Expand Down
16 changes: 5 additions & 11 deletions crates/wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2609,6 +2609,7 @@ macro_rules! doctest_wallet {
use $crate::bitcoin::{BlockHash, Transaction, absolute, TxOut, Network, hashes::Hash};
use $crate::chain::{ConfirmationBlockTime, BlockId, TxGraph, tx_graph};
use $crate::{Update, KeychainKind, Wallet};
use $crate::test_utils::*;
let descriptor = "tr([73c5da0a/86'/0'/0']tprv8fMn4hSKPRC1oaCPqxDb1JWtgkpeiQvZhsr8W2xuy3GEMkzoArcAWTfJxYb6Wj8XNNDWEjfYKK4wGQXh3ZUXhDF2NcnsALpWTeSwarJt7Vc/0/*)";
let change_descriptor = "tr([73c5da0a/86'/0'/0']tprv8fMn4hSKPRC1oaCPqxDb1JWtgkpeiQvZhsr8W2xuy3GEMkzoArcAWTfJxYb6Wj8XNNDWEjfYKK4wGQXh3ZUXhDF2NcnsALpWTeSwarJt7Vc/1/*)";

Expand All @@ -2628,21 +2629,14 @@ macro_rules! doctest_wallet {
};
let txid = tx.compute_txid();
let block_id = BlockId { height: 500, hash: BlockHash::all_zeros() };
let _ = wallet.insert_checkpoint(block_id);
let _ = wallet.insert_checkpoint(BlockId { height: 1_000, hash: BlockHash::all_zeros() });
let _ = wallet.insert_tx(tx);
insert_checkpoint(&mut wallet, block_id);
insert_checkpoint(&mut wallet, BlockId { height: 1_000, hash: BlockHash::all_zeros() });
insert_tx(&mut wallet, tx);
let anchor = ConfirmationBlockTime {
confirmation_time: 50_000,
block_id,
};
let update = Update {
tx_update: tx_graph::TxUpdate {
anchors: [(anchor, txid)].into_iter().collect(),
..Default::default()
},
..Default::default()
};
wallet.apply_update(update).unwrap();
insert_anchor(&mut wallet, txid, anchor);
wallet
}}
}
Loading

0 comments on commit ab27884

Please sign in to comment.