Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testutils and test-wallet features #412

Closed
wants to merge 2 commits into from
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,20 @@ rpc = ["bitcoincore-rpc"]


# Debug/Test features
testutils = []
test-blockchains = ["bitcoincore-rpc", "electrum-client"]
test-electrum = ["electrum", "electrsd/electrs_0_8_10", "test-blockchains"]
test-rpc = ["rpc", "electrsd/electrs_0_8_10", "test-blockchains"]
test-esplora = ["esplora", "electrsd/legacy", "electrsd/esplora_a33e97e1", "test-blockchains"]
test-md-docs = ["electrum"]
test-wallet = []

[dev-dependencies]
lazy_static = "1.4"
env_logger = "0.7"
clap = "2.33"
electrsd = { version= "0.8", features = ["trigger", "bitcoind_0_21_1"] }
bdk = { path = ".", default-features = false, features = ["testutils", "test-wallet"] }

[[example]]
name = "address_validator"
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ pub fn version() -> &'static str {
env!("CARGO_PKG_VERSION", "unknown")
}

// We should consider putting this under a feature flag but we need the macro in doctets so we need
// to wait until https://github.com/rust-lang/rust/issues/67295 is fixed.
//
#[cfg(feature = "testutils")]
// Stuff in here is too rough to document atm
#[doc(hidden)]
pub mod testutils;
2 changes: 1 addition & 1 deletion src/psbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl PsbtUtils for Psbt {
mod test {
use crate::bitcoin::TxIn;
use crate::psbt::Psbt;
use crate::wallet::test::{get_funded_wallet, get_test_wpkh};
use crate::wallet::AddressIndex;
use crate::wallet::{get_funded_wallet, test::get_test_wpkh};
use crate::SignOptions;
use std::str::FromStr;

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/address_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ mod test {
use std::sync::Arc;

use super::*;
use crate::wallet::test::{get_funded_wallet, get_test_wpkh};
use crate::wallet::AddressIndex::New;
use crate::wallet::{get_funded_wallet, test::get_test_wpkh};

#[derive(Debug)]
struct TestValidator;
Expand Down
114 changes: 61 additions & 53 deletions src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ where
secp,
})
}

/// Get the Bitcoin network the wallet is using.
pub fn network(&self) -> Network {
self.network
}
}

/// The address index selection strategy to use to derived an address from the wallet's external
Expand Down Expand Up @@ -1534,11 +1539,6 @@ where
&self.client
}

/// Get the Bitcoin network the wallet is using.
pub fn network(&self) -> Network {
self.network
}

/// Broadcast a transaction to the network
#[maybe_async]
pub fn broadcast(&self, tx: Transaction) -> Result<Txid, Error> {
Expand All @@ -1548,21 +1548,73 @@ where
}
}

#[cfg(test)]
pub(crate) mod test {
#[cfg(feature = "test-wallet")]
use crate::database::memory::MemoryDatabase;

#[cfg(feature = "test-wallet")]
#[doc(hidden)]
/// Return a fake wallet that appears to be funded for testing.
pub fn get_funded_wallet(
descriptor: &str,
) -> (
Wallet<(), MemoryDatabase>,
(String, Option<String>),
bitcoin::Txid,
) {
use crate::testutils;
use std::str::FromStr;

let descriptors = testutils!(@descriptors (descriptor));
let wallet = Wallet::new_offline(
&descriptors.0,
None,
Network::Regtest,
MemoryDatabase::new(),
)
.unwrap();

let funding_address_kix = 0;

let tx_meta = testutils! {
@tx ( (@external descriptors, funding_address_kix) => 50_000 ) (@confirmations 1)
};

wallet
.database
.borrow_mut()
.set_script_pubkey(
&bitcoin::Address::from_str(&tx_meta.output.get(0).unwrap().to_address)
.unwrap()
.script_pubkey(),
KeychainKind::External,
funding_address_kix,
)
.unwrap();
wallet
.database
.borrow_mut()
.set_last_index(KeychainKind::External, funding_address_kix)
.unwrap();

let txid = crate::populate_test_db!(wallet.database.borrow_mut(), tx_meta, Some(100));

(wallet, descriptors, txid)
}

#[cfg(test)]
pub(crate) mod test {
use bitcoin::{util::psbt, Network};

use crate::database::memory::MemoryDatabase;
use crate::database::Database;
use crate::types::KeychainKind;

use super::*;
use crate::signer::{SignOptions, SignerError};
use crate::testutils;
use crate::wallet::AddressIndex::{LastUnused, New, Peek, Reset};

use crate::testutils;
use std::str::FromStr;

#[test]
fn test_cache_addresses_fixed() {
let db = MemoryDatabase::new();
Expand Down Expand Up @@ -1672,50 +1724,6 @@ pub(crate) mod test {
"wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),after(100000)))"
}

pub(crate) fn get_funded_wallet(
descriptor: &str,
) -> (
Wallet<(), MemoryDatabase>,
(String, Option<String>),
bitcoin::Txid,
) {
let descriptors = testutils!(@descriptors (descriptor));
let wallet = Wallet::new_offline(
&descriptors.0,
None,
Network::Regtest,
MemoryDatabase::new(),
)
.unwrap();

let funding_address_kix = 0;

let tx_meta = testutils! {
@tx ( (@external descriptors, funding_address_kix) => 50_000 ) (@confirmations 1)
};

wallet
.database
.borrow_mut()
.set_script_pubkey(
&bitcoin::Address::from_str(&tx_meta.output.get(0).unwrap().to_address)
.unwrap()
.script_pubkey(),
KeychainKind::External,
funding_address_kix,
)
.unwrap();
wallet
.database
.borrow_mut()
.set_last_index(KeychainKind::External, funding_address_kix)
.unwrap();

let txid = crate::populate_test_db!(wallet.database.borrow_mut(), tx_meta, Some(100));

(wallet, descriptors, txid)
}

macro_rules! assert_fee_rate {
($tx:expr, $fees:expr, $fee_rate:expr $( ,@dust_change $( $dust_change:expr )* )* $( ,@add_signature $( $add_signature:expr )* )* ) => ({
let mut tx = $tx.clone();
Expand Down