Skip to content

Commit f52f04d

Browse files
committed
wip(fix(tests)): on bdk_esplora, bdk_electrum and bdk_bitcoind_rpc
1 parent d835d4d commit f52f04d

File tree

7 files changed

+187
-156
lines changed

7 files changed

+187
-156
lines changed

Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,19 @@ authors = ["Bitcoin Dev Kit Developers"]
2525
[workspace.lints.clippy]
2626
print_stdout = "deny"
2727
print_stderr = "deny"
28+
29+
[patch.crates-io.corepc-types]
30+
git = "https://github.com/rust-bitcoin/corepc.git"
31+
branch = "master"
32+
33+
[patch.crates-io.corepc-client]
34+
git = "https://github.com/rust-bitcoin/corepc.git"
35+
branch = "master"
36+
37+
[patch.crates-io.corepc-node]
38+
git = "https://github.com/rust-bitcoin/corepc.git"
39+
branch = "master"
40+
41+
[patch.crates-io.electrsd]
42+
git = "https://github.com/oleonardolima/electrsd.git"
43+
branch = "deps/bump-corepc-deps"

crates/electrum/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,19 @@ use-openssl = ["electrum-client/use-openssl"]
2929
[[test]]
3030
name = "test_electrum"
3131
required-features = ["use-rustls"]
32+
33+
[patch.crates-io.corepc-types]
34+
git = "https://github.com/rust-bitcoin/corepc.git"
35+
branch = "master"
36+
37+
[patch.crates-io.corepc-client]
38+
git = "https://github.com/rust-bitcoin/corepc.git"
39+
branch = "master"
40+
41+
[patch.crates-io.corepc-node]
42+
git = "https://github.com/rust-bitcoin/corepc.git"
43+
branch = "master"
44+
45+
[patch.crates-io.electrsd]
46+
git = "https://github.com/oleonardolima/electrsd.git"
47+
branch = "deps/bump-corepc-deps"

crates/electrum/tests/test_electrum.rs

Lines changed: 52 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ use bdk_chain::{
77
};
88
use bdk_core::bitcoin::{
99
key::{Secp256k1, UntweakedPublicKey},
10-
Network,
10+
Denomination, Network,
1111
};
1212
use bdk_electrum::BdkElectrumClient;
13-
use bdk_testenv::{
14-
anyhow,
15-
bitcoincore_rpc::{json::CreateRawTransactionInput, RawTx, RpcApi},
16-
TestEnv,
17-
};
13+
use bdk_testenv::{anyhow, TestEnv};
1814
use core::time::Duration;
1915
use electrum_client::ElectrumApi;
2016
use std::collections::{BTreeSet, HashMap, HashSet};
@@ -88,7 +84,8 @@ pub fn detect_receive_tx_cancel() -> anyhow::Result<()> {
8884
let client = BdkElectrumClient::new(electrum_client);
8985

9086
let mut graph = IndexedTxGraph::<ConfirmationBlockTime, _>::new(SpkTxOutIndex::<()>::default());
91-
let (chain, _) = LocalChain::from_genesis_hash(env.bitcoind.client.get_block_hash(0)?);
87+
let (chain, _) =
88+
LocalChain::from_genesis_hash(env.bitcoind.client.get_block_hash(0)?.block_hash()?);
9289

9390
// Get receiving address.
9491
let receiver_spk = get_test_spk();
@@ -99,11 +96,13 @@ pub fn detect_receive_tx_cancel() -> anyhow::Result<()> {
9996

10097
// Select a UTXO to use as an input for constructing our test transactions.
10198
let selected_utxo = rpc_client
102-
.list_unspent(None, None, None, Some(false), None)?
99+
.list_unspent()?
100+
.0
103101
.into_iter()
104102
// Find a block reward tx.
105-
.find(|utxo| utxo.amount == Amount::from_int_btc(50))
106-
.expect("Must find a block reward UTXO");
103+
.find(|utxo| utxo.amount == Amount::from_int_btc(50).to_btc())
104+
.expect("Must find a block reward UTXO")
105+
.into_model()?;
107106

108107
// Derive the sender's address from the selected UTXO.
109108
let sender_spk = selected_utxo.script_pub_key.clone();
@@ -122,9 +121,9 @@ pub fn detect_receive_tx_cancel() -> anyhow::Result<()> {
122121
receiver_addr.to_string(),
123122
selected_utxo.amount - SEND_TX_FEE,
124123
)]);
125-
let send_tx = rpc_client.create_raw_transaction(&inputs, &send_tx_outputs, None, Some(true))?;
124+
let send_tx = rpc_client.create_raw_transaction(&inputs, &send_tx_outputs)?;
126125
let send_tx = rpc_client
127-
.sign_raw_transaction_with_wallet(send_tx.raw_hex(), None, None)?
126+
.sign_raw_transaction_with_wallet(send_tx.raw_hex())?
128127
.transaction()?;
129128

130129
// Create and sign the `undo_send_tx` transaction. This redirects funds back to the sender
@@ -133,10 +132,9 @@ pub fn detect_receive_tx_cancel() -> anyhow::Result<()> {
133132
sender_addr.to_string(),
134133
selected_utxo.amount - UNDO_SEND_TX_FEE,
135134
)]);
136-
let undo_send_tx =
137-
rpc_client.create_raw_transaction(&inputs, &undo_send_outputs, None, Some(true))?;
135+
let undo_send_tx = rpc_client.create_raw_transaction(&inputs, &undo_send_outputs)?;
138136
let undo_send_tx = rpc_client
139-
.sign_raw_transaction_with_wallet(undo_send_tx.raw_hex(), None, None)?
137+
.sign_raw_transaction_with_wallet(undo_send_tx.raw_hex())?
140138
.transaction()?;
141139

142140
// Sync after broadcasting the `send_tx`. Ensure that we detect and receive the `send_tx`.
@@ -198,7 +196,7 @@ pub fn chained_mempool_tx_sync() -> anyhow::Result<()> {
198196
let electrum_client = electrum_client::Client::new(env.electrsd.electrum_url.as_str())?;
199197

200198
let tracked_addr = rpc_client
201-
.get_new_address(None, None)?
199+
.new_address()?
202200
.require_network(Network::Regtest)?;
203201

204202
env.mine_blocks(100, None)?;
@@ -208,7 +206,8 @@ pub fn chained_mempool_tx_sync() -> anyhow::Result<()> {
208206

209207
// Create second unconfirmed tx that spends the first.
210208
let utxo = rpc_client
211-
.list_unspent(None, Some(0), None, Some(true), None)?
209+
.list_unspent()?
210+
.0
212211
.into_iter()
213212
.find(|utxo| utxo.script_pub_key == tracked_addr.script_pubkey())
214213
.expect("must find the newly created utxo");
@@ -223,11 +222,9 @@ pub fn chained_mempool_tx_sync() -> anyhow::Result<()> {
223222
utxo.amount - Amount::from_sat(1000),
224223
)]
225224
.into(),
226-
None,
227-
None,
228225
)?;
229226
let signed_tx = rpc_client
230-
.sign_raw_transaction_with_wallet(tx_that_spends_unconfirmed.raw_hex(), None, None)?
227+
.sign_raw_transaction_with_wallet(tx_that_spends_unconfirmed.raw_hex())?
231228
.transaction()?;
232229
let txid2 = rpc_client.send_raw_transaction(signed_tx.raw_hex())?;
233230

@@ -271,26 +268,16 @@ pub fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> {
271268
];
272269

273270
let _block_hashes = env.mine_blocks(101, None)?;
274-
let txid1 = env.bitcoind.client.send_to_address(
275-
&receive_address1,
276-
Amount::from_sat(10000),
277-
None,
278-
None,
279-
None,
280-
None,
281-
Some(1),
282-
None,
283-
)?;
284-
let txid2 = env.bitcoind.client.send_to_address(
285-
&receive_address0,
286-
Amount::from_sat(20000),
287-
None,
288-
None,
289-
None,
290-
None,
291-
Some(1),
292-
None,
293-
)?;
271+
let txid1 = env
272+
.bitcoind
273+
.client
274+
.send_to_address(&receive_address1, Amount::from_sat(10000))?
275+
.txid();
276+
let txid2 = env
277+
.bitcoind
278+
.client
279+
.send_to_address(&receive_address0, Amount::from_sat(20000))?
280+
.txid();
294281
env.mine_blocks(1, None)?;
295282
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
296283

@@ -337,7 +324,8 @@ pub fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> {
337324
let tx_fee = env
338325
.bitcoind
339326
.client
340-
.get_transaction(&tx.compute_txid(), None)
327+
.get_transaction(&tx.compute_txid())?
328+
.into
341329
.expect("Tx must exist")
342330
.fee
343331
.expect("Fee must exist")
@@ -393,16 +381,11 @@ pub fn test_update_tx_graph_stop_gap() -> anyhow::Result<()> {
393381
.collect();
394382

395383
// Then receive coins on the 4th address.
396-
let txid_4th_addr = env.bitcoind.client.send_to_address(
397-
&addresses[3],
398-
Amount::from_sat(10000),
399-
None,
400-
None,
401-
None,
402-
None,
403-
Some(1),
404-
None,
405-
)?;
384+
let txid_4th_addr = env
385+
.bitcoind
386+
.client
387+
.send_to_address(&addresses[3], Amount::from_sat(10000))?
388+
.txid()?;
406389
env.mine_blocks(1, None)?;
407390
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
408391

@@ -437,16 +420,11 @@ pub fn test_update_tx_graph_stop_gap() -> anyhow::Result<()> {
437420
assert_eq!(full_scan_update.last_active_indices[&0], 3);
438421

439422
// Now receive a coin on the last address.
440-
let txid_last_addr = env.bitcoind.client.send_to_address(
441-
&addresses[addresses.len() - 1],
442-
Amount::from_sat(10000),
443-
None,
444-
None,
445-
None,
446-
None,
447-
Some(1),
448-
None,
449-
)?;
423+
let txid_last_addr = env
424+
.bitcoind
425+
.client
426+
.send_to_address(&addresses[addresses.len() - 1], Amount::from_sat(10000))?
427+
.txid()?;
450428
env.mine_blocks(1, None)?;
451429
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
452430

@@ -498,11 +476,7 @@ fn test_sync() -> anyhow::Result<()> {
498476
let client = BdkElectrumClient::new(electrum_client);
499477

500478
// Setup addresses.
501-
let addr_to_mine = env
502-
.bitcoind
503-
.client
504-
.get_new_address(None, None)?
505-
.assume_checked();
479+
let addr_to_mine = env.bitcoind.client.new_address()?;
506480
let spk_to_track = ScriptBuf::new_p2wsh(&WScriptHash::all_zeros());
507481
let addr_to_track = Address::from_script(&spk_to_track, bdk_chain::bitcoin::Network::Regtest)?;
508482

@@ -610,7 +584,7 @@ fn test_sync() -> anyhow::Result<()> {
610584
let tx_fee = env
611585
.bitcoind
612586
.client
613-
.get_transaction(&tx.txid, None)
587+
.get_transaction(&tx.txid)?
614588
.expect("Tx must exist")
615589
.fee
616590
.expect("Fee must exist")
@@ -641,16 +615,13 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
641615
let client = BdkElectrumClient::new(electrum_client);
642616

643617
// Setup addresses.
644-
let addr_to_mine = env
645-
.bitcoind
646-
.client
647-
.get_new_address(None, None)?
648-
.assume_checked();
618+
let addr_to_mine = env.bitcoind.client.new_address()?;
649619
let spk_to_track = ScriptBuf::new_p2wsh(&WScriptHash::all_zeros());
650620
let addr_to_track = Address::from_script(&spk_to_track, bdk_chain::bitcoin::Network::Regtest)?;
651621

652622
// Setup receiver.
653-
let (mut recv_chain, _) = LocalChain::from_genesis_hash(env.bitcoind.client.get_block_hash(0)?);
623+
let (mut recv_chain, _) =
624+
LocalChain::from_genesis_hash(env.bitcoind.client.get_block_hash(0)?.block_hash()?);
654625
let mut recv_graph = IndexedTxGraph::<ConfirmationBlockTime, _>::new({
655626
let mut recv_index = SpkTxOutIndex::default();
656627
recv_index.insert_spk((), spk_to_track.clone());
@@ -737,7 +708,8 @@ fn test_sync_with_coinbase() -> anyhow::Result<()> {
737708
let addr_to_track = Address::from_script(&spk_to_track, bdk_chain::bitcoin::Network::Regtest)?;
738709

739710
// Setup receiver.
740-
let (mut recv_chain, _) = LocalChain::from_genesis_hash(env.bitcoind.client.get_block_hash(0)?);
711+
let (mut recv_chain, _) =
712+
LocalChain::from_genesis_hash(env.bitcoind.client.get_block_hash(0)?.block_hash()?);
741713
let mut recv_graph = IndexedTxGraph::<ConfirmationBlockTime, _>::new({
742714
let mut recv_index = SpkTxOutIndex::default();
743715
recv_index.insert_spk((), spk_to_track.clone());
@@ -772,7 +744,8 @@ fn test_check_fee_calculation() -> anyhow::Result<()> {
772744
let addr_to_track = Address::from_script(&spk_to_track, bdk_chain::bitcoin::Network::Regtest)?;
773745

774746
// Setup receiver.
775-
let (mut recv_chain, _) = LocalChain::from_genesis_hash(env.bitcoind.client.get_block_hash(0)?);
747+
let (mut recv_chain, _) =
748+
LocalChain::from_genesis_hash(env.bitcoind.client.get_block_hash(0)?.block_hash()?);
776749
let mut recv_graph = IndexedTxGraph::<ConfirmationBlockTime, _>::new({
777750
let mut recv_index = SpkTxOutIndex::default();
778751
recv_index.insert_spk((), spk_to_track.clone());
@@ -784,10 +757,7 @@ fn test_check_fee_calculation() -> anyhow::Result<()> {
784757

785758
// Send a preliminary tx such that the new utxo in Core's wallet
786759
// becomes the input of the next tx
787-
let new_addr = env
788-
.rpc_client()
789-
.get_new_address(None, None)?
790-
.assume_checked();
760+
let new_addr = env.rpc_client().new_address()?;
791761
let prev_amt = SEND_AMOUNT + FEE_AMOUNT;
792762
env.send(&new_addr, prev_amt)?;
793763
let prev_block_hash = env.mine_blocks(1, None)?.into_iter().next();
@@ -864,11 +834,12 @@ fn test_check_fee_calculation() -> anyhow::Result<()> {
864834
let tx_fee = env
865835
.bitcoind
866836
.client
867-
.get_transaction(&tx.txid, None)
837+
.get_transaction(tx.txid)
868838
.expect("Tx must exist")
869839
.fee
840+
.map(|fee| Amount::from_float_in(fee.abs(), Denomination::BTC))
870841
.expect("Fee must exist")
871-
.abs()
842+
.expect("Amount parsing should succeed")
872843
.to_sat() as u64;
873844

874845
// Check that the calculated fee matches the fee from the transaction data.

crates/esplora/src/async_ext.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ mod test {
546546
BlockId,
547547
};
548548
use bdk_core::ConfirmationBlockTime;
549-
use bdk_testenv::{anyhow, bitcoincore_rpc::RpcApi, TestEnv};
549+
use bdk_testenv::{anyhow, TestEnv};
550550
use esplora_client::Builder;
551551

552552
use crate::async_ext::{chain_update, fetch_latest_blocks};
@@ -626,7 +626,11 @@ mod test {
626626
ConfirmationBlockTime {
627627
block_id: BlockId {
628628
height,
629-
hash: env.bitcoind.client.get_block_hash(height as _)?,
629+
hash: env
630+
.bitcoind
631+
.client
632+
.get_block_hash(height as _)?
633+
.block_hash()?,
630634
},
631635
confirmation_time: height as _,
632636
},
@@ -666,7 +670,11 @@ mod test {
666670
ConfirmationBlockTime {
667671
block_id: BlockId {
668672
height,
669-
hash: env.bitcoind.client.get_block_hash(height as _)?,
673+
hash: env
674+
.bitcoind
675+
.client
676+
.get_block_hash(height as _)?
677+
.block_hash()?,
670678
},
671679
confirmation_time: height as _,
672680
},

0 commit comments

Comments
 (0)