Skip to content

Commit 7decfbb

Browse files
committed
deps(bdk_testenv): bump electrsd to 0.34.0
1 parent a09f12a commit 7decfbb

File tree

2 files changed

+50
-53
lines changed

2 files changed

+50
-53
lines changed

crates/testenv/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ workspace = true
1717

1818
[dependencies]
1919
bdk_chain = { path = "../chain", version = "0.23.0", default-features = false }
20-
electrsd = { version = "0.28.0", features = [ "legacy" ], default-features = false }
20+
electrsd = { version = "0.34.0", features = [ "legacy" ], default-features = false }
2121

2222
[dev-dependencies]
2323
bdk_testenv = { path = "." }
2424

2525
[features]
2626
default = ["std", "download"]
27-
download = ["electrsd/bitcoind_25_0", "electrsd/esplora_a33e97e1"]
27+
download = ["electrsd/corepc-node_28_0", "electrsd/esplora_a33e97e1"]
2828
std = ["bdk_chain/std"]
2929
serde = ["bdk_chain/serde"]
3030

crates/testenv/src/lib.rs

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,28 @@ use bdk_chain::{
1111
local_chain::CheckPoint,
1212
BlockId,
1313
};
14-
use bitcoincore_rpc::{
15-
bitcoincore_rpc_json::{GetBlockTemplateModes, GetBlockTemplateRules},
16-
RpcApi,
17-
};
18-
use electrsd::bitcoind::anyhow::Context;
14+
use electrsd::{corepc_client::client_sync::{TemplateRequest, TemplateRules}, corepc_node::anyhow::Context};
1915

2016
pub use electrsd;
21-
pub use electrsd::bitcoind;
22-
pub use electrsd::bitcoind::anyhow;
23-
pub use electrsd::bitcoind::bitcoincore_rpc;
17+
pub use electrsd::corepc_client;
18+
pub use electrsd::corepc_node;
19+
pub use electrsd::corepc_node::anyhow;
2420
pub use electrsd::electrum_client;
2521
use electrsd::electrum_client::ElectrumApi;
26-
use std::time::Duration;
22+
use std::{str::FromStr, time::Duration};
2723

2824
/// Struct for running a regtest environment with a single `bitcoind` node with an `electrs`
2925
/// instance connected to it.
3026
pub struct TestEnv {
31-
pub bitcoind: electrsd::bitcoind::BitcoinD,
27+
pub bitcoind: electrsd::corepc_node::Node,
3228
pub electrsd: electrsd::ElectrsD,
3329
}
3430

3531
/// Configuration parameters.
3632
#[derive(Debug)]
3733
pub struct Config<'a> {
3834
/// [`bitcoind::Conf`]
39-
pub bitcoind: bitcoind::Conf<'a>,
35+
pub bitcoind: corepc_node::Conf<'a>,
4036
/// [`electrsd::Conf`]
4137
pub electrsd: electrsd::Conf<'a>,
4238
}
@@ -46,7 +42,7 @@ impl Default for Config<'_> {
4642
/// which is required for testing `bdk_esplora`.
4743
fn default() -> Self {
4844
Self {
49-
bitcoind: bitcoind::Conf::default(),
45+
bitcoind: corepc_node::Conf::default(),
5046
electrsd: {
5147
let mut conf = electrsd::Conf::default();
5248
conf.http_enabled = true;
@@ -66,11 +62,11 @@ impl TestEnv {
6662
pub fn new_with_config(config: Config) -> anyhow::Result<Self> {
6763
let bitcoind_exe = match std::env::var("BITCOIND_EXE") {
6864
Ok(path) => path,
69-
Err(_) => bitcoind::downloaded_exe_path().context(
65+
Err(_) => corepc_node::downloaded_exe_path().context(
7066
"you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature",
7167
)?,
7268
};
73-
let bitcoind = bitcoind::BitcoinD::with_conf(bitcoind_exe, &config.bitcoind)?;
69+
let bitcoind = corepc_node::Node::with_conf(bitcoind_exe, &config.bitcoind)?;
7470

7571
let electrs_exe = match std::env::var("ELECTRS_EXE") {
7672
Ok(path) => path,
@@ -88,7 +84,7 @@ impl TestEnv {
8884
}
8985

9086
/// Exposes the [`RpcApi`] calls from [`bitcoincore_rpc`].
91-
pub fn rpc_client(&self) -> &impl RpcApi {
87+
pub fn rpc_client(&self) -> &corepc_node::Client {
9288
&self.bitcoind.client
9389
}
9490

@@ -119,26 +115,21 @@ impl TestEnv {
119115
) -> anyhow::Result<Vec<BlockHash>> {
120116
let coinbase_address = match address {
121117
Some(address) => address,
122-
None => self
123-
.bitcoind
124-
.client
125-
.get_new_address(None, None)?
126-
.assume_checked(),
118+
None => self.bitcoind.client.new_address()?,
127119
};
128120
let block_hashes = self
129121
.bitcoind
130122
.client
131-
.generate_to_address(count as _, &coinbase_address)?;
123+
.generate_to_address(count as _, &coinbase_address)?
124+
.into_model()?
125+
.0;
132126
Ok(block_hashes)
133127
}
134128

135129
/// Mine a block that is guaranteed to be empty even with transactions in the mempool.
136130
pub fn mine_empty_block(&self) -> anyhow::Result<(usize, BlockHash)> {
137-
let bt = self.bitcoind.client.get_block_template(
138-
GetBlockTemplateModes::Template,
139-
&[GetBlockTemplateRules::SegWit],
140-
&[],
141-
)?;
131+
let request = TemplateRequest { rules: vec![TemplateRules::Segwit] };
132+
let bt = self.bitcoind.client.get_block_template(&request)?;
142133

143134
let txdata = vec![Transaction {
144135
version: transaction::Version::ONE,
@@ -147,7 +138,7 @@ impl TestEnv {
147138
previous_output: bdk_chain::bitcoin::OutPoint::default(),
148139
script_sig: ScriptBuf::builder()
149140
.push_int(bt.height as _)
150-
// randomn number so that re-mining creates unique block
141+
// random number so that re-mining creates unique block
151142
.push_int(random())
152143
.into_script(),
153144
sequence: bdk_chain::bitcoin::Sequence::default(),
@@ -159,18 +150,19 @@ impl TestEnv {
159150
}],
160151
}];
161152

162-
let bits: [u8; 4] = bt
163-
.bits
164-
.clone()
165-
.try_into()
166-
.expect("rpc provided us with invalid bits");
153+
// TODO: (@leonardo) double-check if an `.into_bytes()` wouldn't be enough instead.
154+
let bits: [u8; 4] =
155+
bdk_chain::bitcoin::consensus::encode::deserialize_hex::<Vec<u8>>(&bt.bits)?
156+
.clone()
157+
.try_into()
158+
.expect("rpc provided us with invalid bits");
167159

168160
let mut block = Block {
169161
header: Header {
170162
version: bdk_chain::bitcoin::block::Version::default(),
171-
prev_blockhash: bt.previous_block_hash,
163+
prev_blockhash: BlockHash::from_str(&bt.previous_block_hash)?,
172164
merkle_root: TxMerkleNode::all_zeros(),
173-
time: Ord::max(bt.min_time, std::time::UNIX_EPOCH.elapsed()?.as_secs()) as u32,
165+
time: Ord::max(bt.min_time, std::time::UNIX_EPOCH.elapsed()?.as_secs() as u32),
174166
bits: CompactTarget::from_consensus(u32::from_be_bytes(bits)),
175167
nonce: 0,
176168
},
@@ -187,6 +179,7 @@ impl TestEnv {
187179
}
188180

189181
self.bitcoind.client.submit_block(&block)?;
182+
190183
Ok((bt.height as usize, block.block_hash()))
191184
}
192185

@@ -237,18 +230,16 @@ impl TestEnv {
237230

238231
/// Invalidate a number of blocks of a given size `count`.
239232
pub fn invalidate_blocks(&self, count: usize) -> anyhow::Result<()> {
240-
let mut hash = self.bitcoind.client.get_best_block_hash()?;
233+
let mut hash = self.bitcoind.client.get_best_block_hash()?.block_hash()?;
241234
for _ in 0..count {
242-
let prev_hash = self
243-
.bitcoind
244-
.client
245-
.get_block_info(&hash)?
246-
.previousblockhash;
247-
self.bitcoind.client.invalidate_block(&hash)?;
248-
match prev_hash {
249-
Some(prev_hash) => hash = prev_hash,
250-
None => break,
251-
}
235+
let prev_hash = self.bitcoind.client.get_block(hash)?.header.prev_blockhash;
236+
self.bitcoind.client.invalidate_block(hash)?;
237+
hash = prev_hash
238+
// TODO: (@leonardo) It requires a double check if there is any side-effect with this break removal.
239+
// match prev_hash {
240+
// Some(prev_hash) => hash = prev_hash,
241+
// None => break,
242+
// }
252243
}
253244
Ok(())
254245
}
@@ -289,7 +280,8 @@ impl TestEnv {
289280
let txid = self
290281
.bitcoind
291282
.client
292-
.send_to_address(address, amount, None, None, None, None, None, None)?;
283+
.send_to_address(address, amount)?
284+
.txid()?;
293285
Ok(txid)
294286
}
295287

@@ -300,14 +292,19 @@ impl TestEnv {
300292
.client
301293
.get_block_hash(height as u64)
302294
.ok()
303-
.map(|hash| BlockId { height, hash })
295+
.map(|get_block_hash| {
296+
let hash = get_block_hash
297+
.block_hash()
298+
.expect("should `successfully convert to `BlockHash` from `GetBlockHash`");
299+
BlockId { height, hash }
300+
})
304301
}))
305302
.expect("must craft tip")
306303
}
307304

308305
/// Get the genesis hash of the blockchain.
309306
pub fn genesis_hash(&self) -> anyhow::Result<BlockHash> {
310-
let hash = self.bitcoind.client.get_block_hash(0)?;
307+
let hash = self.bitcoind.client.get_block_hash(0)?.into_model()?.0;
311308
Ok(hash)
312309
}
313310
}
@@ -317,7 +314,7 @@ impl TestEnv {
317314
mod test {
318315
use crate::TestEnv;
319316
use core::time::Duration;
320-
use electrsd::bitcoind::{anyhow::Result, bitcoincore_rpc::RpcApi};
317+
use electrsd::corepc_node::anyhow::Result;
321318

322319
/// This checks that reorgs initiated by `bitcoind` is detected by our `electrsd` instance.
323320
#[test]
@@ -327,15 +324,15 @@ mod test {
327324
// Mine some blocks.
328325
env.mine_blocks(101, None)?;
329326
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
330-
let height = env.bitcoind.client.get_block_count()?;
327+
let height = env.bitcoind.client.get_block_count()?.into_model().0;
331328
let blocks = (0..=height)
332329
.map(|i| env.bitcoind.client.get_block_hash(i))
333330
.collect::<Result<Vec<_>, _>>()?;
334331

335332
// Perform reorg on six blocks.
336333
env.reorg(6)?;
337334
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
338-
let reorged_height = env.bitcoind.client.get_block_count()?;
335+
let reorged_height = env.bitcoind.client.get_block_count()?.into_model().0;
339336
let reorged_blocks = (0..=height)
340337
.map(|i| env.bitcoind.client.get_block_hash(i))
341338
.collect::<Result<Vec<_>, _>>()?;

0 commit comments

Comments
 (0)