Skip to content

Commit afa1ab4

Browse files
committed
Fix blockchain_tests::test_send_to_bech32m_addr
Now works with latest released versions of rust-bitcoincore-rpc and bitcoind. Once these crates are updated to support creating descriptor wallets and add importdescriptors and bech32m support this test will need to be updated.
1 parent 632422a commit afa1ab4

File tree

3 files changed

+69
-24
lines changed

3 files changed

+69
-24
lines changed

Cargo.toml

+1-5
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,11 @@ test-rpc = ["rpc", "electrsd/electrs_0_8_10", "test-blockchains"]
9090
test-esplora = ["electrsd/legacy", "electrsd/esplora_a33e97e1", "test-blockchains"]
9191
test-md-docs = ["electrum"]
9292

93-
[patch.crates-io]
94-
core-rpc = { git="https://github.com/sandipndev/rust-bitcoincore-rpc", branch="bech32m-support" }
95-
bitcoind = { git="https://github.com/sandipndev/bitcoind", branch="create-wallet-updates" }
96-
9793
[dev-dependencies]
9894
lazy_static = "1.4"
9995
env_logger = "0.7"
10096
clap = "2.33"
101-
electrsd = { version= "0.12", features = ["trigger", "bitcoind_0_21_1"] }
97+
electrsd = { version= "0.12", features = ["trigger", "bitcoind_22_0"] }
10298

10399
[[example]]
104100
name = "address_validator"

src/blockchain/rpc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl ConfigurableBlockchain for RpcBlockchain {
370370
client.load_wallet(&wallet_name)?;
371371
debug!("wallet loaded {:?}", wallet_name);
372372
} else {
373-
client.create_wallet(&wallet_name, Some(true), None, None, None, None)?;
373+
client.create_wallet(&wallet_name, Some(true), None, None, None)?;
374374
debug!("wallet created {:?}", wallet_name);
375375
}
376376
}
@@ -445,7 +445,7 @@ where
445445
}
446446

447447
/// return the wallets available in default wallet directory
448-
//TODO use core_rpc method when PR #179 lands
448+
//TODO use bitcoincore_rpc method when PR #179 lands
449449
fn list_wallet_dir(client: &Client) -> Result<Vec<String>, Error> {
450450
#[derive(Deserialize)]
451451
struct Name {

src/testutils/blockchain_tests.rs

+66-17
Original file line numberDiff line numberDiff line change
@@ -913,48 +913,97 @@ macro_rules! bdk_blockchain_tests {
913913
#[test]
914914
fn test_send_to_bech32m_addr() {
915915
use std::str::FromStr;
916-
use core_rpc::RpcApi;
917-
use core_rpc::core_rpc_json::ImportDescriptorRequest;
916+
use serde;
917+
use serde_json;
918+
use serde::Serialize;
919+
use bitcoincore_rpc::jsonrpc::serde_json::Value;
920+
use bitcoincore_rpc::{Auth, Client, RpcApi};
918921

919922
let (wallet, descriptors, mut test_client) = init_single_sig();
920923

924+
// TODO remove once rust-bitcoincore-rpc with PR 199 released
925+
// https://github.com/rust-bitcoin/rust-bitcoincore-rpc/pull/199
926+
/// Import Descriptor Request
927+
#[derive(Serialize, Clone, PartialEq, Eq, Debug)]
928+
pub struct ImportDescriptorRequest {
929+
pub active: bool,
930+
#[serde(rename = "desc")]
931+
pub descriptor: String,
932+
pub range: [i64; 2],
933+
pub next_index: i64,
934+
pub timestamp: String,
935+
pub internal: bool,
936+
}
937+
938+
// TODO remove once rust-bitcoincore-rpc with PR 199 released
939+
impl ImportDescriptorRequest {
940+
/// Create a new Import Descriptor request providing just the descriptor and internal flags
941+
pub fn new(descriptor: &str, internal: bool) -> Self {
942+
ImportDescriptorRequest {
943+
descriptor: descriptor.to_string(),
944+
internal,
945+
active: true,
946+
range: [0, 100],
947+
next_index: 0,
948+
timestamp: "now".to_string(),
949+
}
950+
}
951+
}
952+
953+
// 1. Create and add descriptors to a test bitcoind node taproot wallet
954+
955+
// TODO replace once rust-bitcoincore-rpc with PR 174 released
956+
// https://github.com/rust-bitcoin/rust-bitcoincore-rpc/pull/174
957+
let _createwallet_result: Value = test_client.bitcoind.client.call("createwallet", &["taproot_wallet".into(),false.into(),true.into(),serde_json::to_value("").unwrap(), false.into(), true.into()]).unwrap();
958+
959+
// TODO replace once bitcoind released with support for rust-bitcoincore-rpc PR 174
960+
let taproot_wallet_client = Client::new(&test_client.bitcoind.rpc_url_with_wallet("taproot_wallet"), Auth::CookieFile(test_client.bitcoind.params.cookie_file.clone())).unwrap();
961+
921962
let wallet_descriptor = "tr(tprv8ZgxMBicQKsPdBtxmEMPnNq58KGusNAimQirKFHqX2yk2D8q1v6pNLiKYVAdzDHy2w3vF4chuGfMvNtzsbTTLVXBcdkCA1rje1JG6oksWv8/86h/1h/0h/0/*)#y283ssmn";
922963
let change_descriptor = "tr(tprv8ZgxMBicQKsPdBtxmEMPnNq58KGusNAimQirKFHqX2yk2D8q1v6pNLiKYVAdzDHy2w3vF4chuGfMvNtzsbTTLVXBcdkCA1rje1JG6oksWv8/86h/1h/0h/1/*)#47zsd9tt";
923964

924-
test_client.bitcoind.client
925-
.import_descriptors(
926-
vec![
965+
let tr_descriptors = vec![
927966
ImportDescriptorRequest::new(wallet_descriptor, false),
928967
ImportDescriptorRequest::new(change_descriptor, false),
929-
]
930-
).unwrap();
968+
];
969+
970+
// TODO replace once rust-bitcoincore-rpc with PR 199 released
971+
let _import_result: Value = taproot_wallet_client.call("importdescriptors", &[serde_json::to_value(tr_descriptors).unwrap()]).unwrap();
972+
973+
// 2. Get a new bech32m address from test bitcoind node taproot wallet
931974

932-
let node_addr = test_client.get_node_address(Some(core_rpc::core_rpc_json::AddressType::Bech32m));
975+
// TODO replace once rust-bitcoincore-rpc with PR 199 released
976+
let node_addr: bitcoin::Address = taproot_wallet_client.call("getnewaddress", &["test address".into(), "bech32m".into()]).unwrap();
933977
assert_eq!(node_addr, bitcoin::Address::from_str("bcrt1pj5y3f0fu4y7g98k4v63j9n0xvj3lmln0cpwhsjzknm6nt0hr0q7qnzwsy9").unwrap());
934978

935-
let new_bech32m_addr = test_client.get_new_address(Some("test bech32m"), Some(core_rpc::core_rpc_json::AddressType::Bech32m)).unwrap();
936-
assert_eq!(new_bech32m_addr, bitcoin::Address::from_str("bcrt1pxa4h86c5gc8x65un8nz546wy7hqxv7wljrv5sxukayh3xwnw23fs80jdf9").unwrap());
979+
// 3. Send 50_000 sats from test bitcoind node to test BDK wallet
937980

938981
test_client.receive(testutils! {
939982
@tx ( (@external descriptors, 0) => 50_000 )
940983
});
941984

942985
wallet.sync(noop_progress(), None).unwrap();
943-
assert_eq!(wallet.get_balance().unwrap(), 50_000, "incorrect balance");
986+
assert_eq!(wallet.get_balance().unwrap(), 50_000, "wallet has incorrect balance");
987+
988+
// 4. Send 25_000 sats from test BDK wallet to test bitcoind node taproot wallet
944989

945990
let mut builder = wallet.build_tx();
946991
builder.add_recipient(node_addr.script_pubkey(), 25_000);
947992
let (mut psbt, details) = builder.finish().unwrap();
948993
let finalized = wallet.sign(&mut psbt, Default::default()).unwrap();
949-
assert!(finalized, "Cannot finalize transaction");
994+
assert!(finalized, "wallet cannot finalize transaction");
950995
let tx = psbt.extract_tx();
951-
println!("{}", bitcoin::consensus::encode::serialize_hex(&tx));
952-
wallet.broadcast(tx).unwrap();
996+
wallet.broadcast(&tx).unwrap();
953997
wallet.sync(noop_progress(), None).unwrap();
954-
assert_eq!(wallet.get_balance().unwrap(), details.received, "incorrect balance after send");
998+
assert_eq!(wallet.get_balance().unwrap(), details.received, "wallet has incorrect balance after send");
999+
assert_eq!(wallet.list_transactions(false).unwrap().len(), 2, "wallet has incorrect number of txs");
1000+
assert_eq!(wallet.list_unspent().unwrap().len(), 1, "wallet has incorrect number of unspents");
1001+
test_client.generate(1, None);
9551002

956-
assert_eq!(wallet.list_transactions(false).unwrap().len(), 2, "incorrect number of txs");
957-
assert_eq!(wallet.list_unspent().unwrap().len(), 1, "incorrect number of unspents");
1003+
// 5. Verify 25_000 sats are received by test bitcoind node taproot wallet
1004+
1005+
let taproot_balance = taproot_wallet_client.get_balance(None, None).unwrap();
1006+
assert_eq!(taproot_balance.as_sat(), 25_000, "node has incorrect taproot wallet balance");
9581007
}
9591008
}
9601009
};

0 commit comments

Comments
 (0)