Skip to content

Commit 749a6d4

Browse files
committed
Add testutils feature and test-wallet feature to expose wallet::get_funded_wallet()
1 parent adceafa commit 749a6d4

File tree

5 files changed

+65
-54
lines changed

5 files changed

+65
-54
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,20 @@ rpc = ["bitcoincore-rpc"]
6262

6363

6464
# Debug/Test features
65+
testutils = []
6566
test-blockchains = ["bitcoincore-rpc", "electrum-client"]
6667
test-electrum = ["electrum", "electrsd/electrs_0_8_10", "test-blockchains"]
6768
test-rpc = ["rpc", "electrsd/electrs_0_8_10", "test-blockchains"]
6869
test-esplora = ["esplora", "electrsd/legacy", "electrsd/esplora_a33e97e1", "test-blockchains"]
6970
test-md-docs = ["electrum"]
71+
test-wallet = []
7072

7173
[dev-dependencies]
7274
lazy_static = "1.4"
7375
env_logger = "0.7"
7476
clap = "2.33"
7577
electrsd = { version= "0.8", features = ["trigger", "bitcoind_0_21_1"] }
78+
bdk = { path = ".", features = ["testutils", "test-wallet"] }
7679

7780
[[example]]
7881
name = "address_validator"

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,7 @@ pub fn version() -> &'static str {
262262
env!("CARGO_PKG_VERSION", "unknown")
263263
}
264264

265-
// We should consider putting this under a feature flag but we need the macro in doctets so we need
266-
// to wait until https://github.com/rust-lang/rust/issues/67295 is fixed.
267-
//
265+
#[cfg(feature = "testutils")]
268266
// Stuff in here is too rough to document atm
269267
#[doc(hidden)]
270268
pub mod testutils;

src/psbt/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ impl PsbtUtils for Psbt {
4343
mod test {
4444
use crate::bitcoin::TxIn;
4545
use crate::psbt::Psbt;
46-
use crate::wallet::test::{get_funded_wallet, get_test_wpkh};
46+
use crate::wallet::get_funded_wallet;
47+
use crate::wallet::test::get_test_wpkh;
4748
use crate::wallet::AddressIndex;
4849
use crate::SignOptions;
4950
use std::str::FromStr;

src/wallet/address_validator.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ mod test {
115115
use std::sync::Arc;
116116

117117
use super::*;
118-
use crate::wallet::test::{get_funded_wallet, get_test_wpkh};
118+
use crate::wallet::get_funded_wallet;
119+
use crate::wallet::test::get_test_wpkh;
119120
use crate::wallet::AddressIndex::New;
120121

121122
#[derive(Debug)]

src/wallet/mod.rs

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ where
167167
secp,
168168
})
169169
}
170+
171+
/// Get the Bitcoin network the wallet is using.
172+
pub fn network(&self) -> Network {
173+
self.network
174+
}
170175
}
171176

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

1537-
/// Get the Bitcoin network the wallet is using.
1538-
pub fn network(&self) -> Network {
1539-
self.network
1540-
}
1541-
15421542
/// Broadcast a transaction to the network
15431543
#[maybe_async]
15441544
pub fn broadcast(&self, tx: Transaction) -> Result<Txid, Error> {
@@ -1548,6 +1548,58 @@ where
15481548
}
15491549
}
15501550

1551+
#[cfg(feature = "test-wallet")]
1552+
use crate::database::memory::MemoryDatabase;
1553+
1554+
#[cfg(feature = "test-wallet")]
1555+
#[doc(hidden)]
1556+
pub fn get_funded_wallet(
1557+
descriptor: &str,
1558+
) -> (
1559+
Wallet<(), MemoryDatabase>,
1560+
(String, Option<String>),
1561+
bitcoin::Txid,
1562+
) {
1563+
use crate::testutils;
1564+
use std::str::FromStr;
1565+
1566+
let descriptors = testutils!(@descriptors (descriptor));
1567+
let wallet = Wallet::new_offline(
1568+
&descriptors.0,
1569+
None,
1570+
Network::Regtest,
1571+
MemoryDatabase::new(),
1572+
)
1573+
.unwrap();
1574+
1575+
let funding_address_kix = 0;
1576+
1577+
let tx_meta = testutils! {
1578+
@tx ( (@external descriptors, funding_address_kix) => 50_000 ) (@confirmations 1)
1579+
};
1580+
1581+
wallet
1582+
.database
1583+
.borrow_mut()
1584+
.set_script_pubkey(
1585+
&bitcoin::Address::from_str(&tx_meta.output.get(0).unwrap().to_address)
1586+
.unwrap()
1587+
.script_pubkey(),
1588+
KeychainKind::External,
1589+
funding_address_kix,
1590+
)
1591+
.unwrap();
1592+
wallet
1593+
.database
1594+
.borrow_mut()
1595+
.set_last_index(KeychainKind::External, funding_address_kix)
1596+
.unwrap();
1597+
1598+
let txid = crate::populate_test_db!(wallet.database.borrow_mut(), tx_meta, Some(100));
1599+
1600+
(wallet, descriptors, txid)
1601+
}
1602+
15511603
#[cfg(test)]
15521604
pub(crate) mod test {
15531605
use std::str::FromStr;
@@ -1672,50 +1724,6 @@ pub(crate) mod test {
16721724
"wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),after(100000)))"
16731725
}
16741726

1675-
pub(crate) fn get_funded_wallet(
1676-
descriptor: &str,
1677-
) -> (
1678-
Wallet<(), MemoryDatabase>,
1679-
(String, Option<String>),
1680-
bitcoin::Txid,
1681-
) {
1682-
let descriptors = testutils!(@descriptors (descriptor));
1683-
let wallet = Wallet::new_offline(
1684-
&descriptors.0,
1685-
None,
1686-
Network::Regtest,
1687-
MemoryDatabase::new(),
1688-
)
1689-
.unwrap();
1690-
1691-
let funding_address_kix = 0;
1692-
1693-
let tx_meta = testutils! {
1694-
@tx ( (@external descriptors, funding_address_kix) => 50_000 ) (@confirmations 1)
1695-
};
1696-
1697-
wallet
1698-
.database
1699-
.borrow_mut()
1700-
.set_script_pubkey(
1701-
&bitcoin::Address::from_str(&tx_meta.output.get(0).unwrap().to_address)
1702-
.unwrap()
1703-
.script_pubkey(),
1704-
KeychainKind::External,
1705-
funding_address_kix,
1706-
)
1707-
.unwrap();
1708-
wallet
1709-
.database
1710-
.borrow_mut()
1711-
.set_last_index(KeychainKind::External, funding_address_kix)
1712-
.unwrap();
1713-
1714-
let txid = crate::populate_test_db!(wallet.database.borrow_mut(), tx_meta, Some(100));
1715-
1716-
(wallet, descriptors, txid)
1717-
}
1718-
17191727
macro_rules! assert_fee_rate {
17201728
($tx:expr, $fees:expr, $fee_rate:expr $( ,@dust_change $( $dust_change:expr )* )* $( ,@add_signature $( $add_signature:expr )* )* ) => ({
17211729
let mut tx = $tx.clone();

0 commit comments

Comments
 (0)