Skip to content

Commit 6b4c37a

Browse files
committed
deps(bdk_testenv): bump electrsd to 0.32.0
1 parent 71bf53d commit 6b4c37a

File tree

2 files changed

+74
-44
lines changed

2 files changed

+74
-44
lines changed

crates/testenv/Cargo.toml

Lines changed: 3 additions & 3 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.21.1", default-features = false }
20-
electrsd = { version = "0.28.0", features = [ "legacy" ], default-features = false }
20+
electrsd = { version = "0.32.0", features = [ "legacy" ], default-features = false }
2121

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

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

crates/testenv/src/lib.rs

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,31 @@ use bdk_chain::{
77
ScriptBuf, ScriptHash, Transaction, TxIn, TxOut, Txid,
88
},
99
local_chain::CheckPoint,
10+
serde::{Deserialize, Serialize},
1011
BlockId,
1112
};
12-
use bitcoincore_rpc::{
13-
bitcoincore_rpc_json::{GetBlockTemplateModes, GetBlockTemplateRules},
14-
RpcApi,
15-
};
16-
use electrsd::bitcoind::anyhow::Context;
13+
use electrsd::corepc_node::anyhow::Context;
1714

1815
pub use electrsd;
19-
pub use electrsd::bitcoind;
20-
pub use electrsd::bitcoind::anyhow;
21-
pub use electrsd::bitcoind::bitcoincore_rpc;
16+
pub use electrsd::corepc_client;
17+
pub use electrsd::corepc_node;
18+
pub use electrsd::corepc_node::anyhow;
2219
pub use electrsd::electrum_client;
2320
use electrsd::electrum_client::ElectrumApi;
2421
use std::time::Duration;
2522

2623
/// Struct for running a regtest environment with a single `bitcoind` node with an `electrs`
2724
/// instance connected to it.
2825
pub struct TestEnv {
29-
pub bitcoind: electrsd::bitcoind::BitcoinD,
26+
pub bitcoind: electrsd::corepc_node::Node,
3027
pub electrsd: electrsd::ElectrsD,
3128
}
3229

3330
/// Configuration parameters.
3431
#[derive(Debug)]
3532
pub struct Config<'a> {
3633
/// [`bitcoind::Conf`]
37-
pub bitcoind: bitcoind::Conf<'a>,
34+
pub bitcoind: corepc_node::Conf<'a>,
3835
/// [`electrsd::Conf`]
3936
pub electrsd: electrsd::Conf<'a>,
4037
}
@@ -44,7 +41,7 @@ impl Default for Config<'_> {
4441
/// which is required for testing `bdk_esplora`.
4542
fn default() -> Self {
4643
Self {
47-
bitcoind: bitcoind::Conf::default(),
44+
bitcoind: corepc_node::Conf::default(),
4845
electrsd: {
4946
let mut conf = electrsd::Conf::default();
5047
conf.http_enabled = true;
@@ -54,6 +51,20 @@ impl Default for Config<'_> {
5451
}
5552
}
5653

54+
/// Models the result of "getblocktemplate" minimally
55+
#[derive(Clone, PartialEq, Eq, Debug, bdk_chain::serde::Deserialize, bdk_chain::serde::Serialize)]
56+
pub struct GetBlockTemplateResult {
57+
/// The compressed difficulty in hexadecimal
58+
pub bits: String,
59+
/// The previous block hash the current template is mining on
60+
pub previousblockhash: bdk_chain::bitcoin::BlockHash,
61+
/// The height of the block we will be mining: `current height + 1`
62+
pub height: u64,
63+
/// The minimum timestamp appropriate for the next block time. Expressed as
64+
/// UNIX timestamp.
65+
pub mintime: u64,
66+
}
67+
5768
impl TestEnv {
5869
/// Construct a new [`TestEnv`] instance with the default configuration used by BDK.
5970
pub fn new() -> anyhow::Result<Self> {
@@ -64,11 +75,11 @@ impl TestEnv {
6475
pub fn new_with_config(config: Config) -> anyhow::Result<Self> {
6576
let bitcoind_exe = match std::env::var("BITCOIND_EXE") {
6677
Ok(path) => path,
67-
Err(_) => bitcoind::downloaded_exe_path().context(
78+
Err(_) => corepc_node::downloaded_exe_path().context(
6879
"you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature",
6980
)?,
7081
};
71-
let bitcoind = bitcoind::BitcoinD::with_conf(bitcoind_exe, &config.bitcoind)?;
82+
let bitcoind = corepc_node::Node::with_conf(bitcoind_exe, &config.bitcoind)?;
7283

7384
let electrs_exe = match std::env::var("ELECTRS_EXE") {
7485
Ok(path) => path,
@@ -86,7 +97,7 @@ impl TestEnv {
8697
}
8798

8899
/// Exposes the [`RpcApi`] calls from [`bitcoincore_rpc`].
89-
pub fn rpc_client(&self) -> &impl RpcApi {
100+
pub fn rpc_client(&self) -> &corepc_node::Client {
90101
&self.bitcoind.client
91102
}
92103

@@ -117,26 +128,33 @@ impl TestEnv {
117128
) -> anyhow::Result<Vec<BlockHash>> {
118129
let coinbase_address = match address {
119130
Some(address) => address,
120-
None => self
121-
.bitcoind
122-
.client
123-
.get_new_address(None, None)?
124-
.assume_checked(),
131+
None => self.bitcoind.client.new_address()?,
125132
};
126133
let block_hashes = self
127134
.bitcoind
128135
.client
129-
.generate_to_address(count as _, &coinbase_address)?;
136+
.generate_to_address(count as _, &coinbase_address)?
137+
.into_model()?
138+
.0;
130139
Ok(block_hashes)
131140
}
132141

142+
fn get_block_template(&self) -> anyhow::Result<GetBlockTemplateResult> {
143+
let argument = r#"{"mode": "template", "rules": "segwit", "capabilities": []}"#;
144+
self.bitcoind.client.call(
145+
"getblocktemplate",
146+
&[corepc_node::serde_json::to_value(argument)?],
147+
);
148+
}
149+
133150
/// Mine a block that is guaranteed to be empty even with transactions in the mempool.
134151
pub fn mine_empty_block(&self) -> anyhow::Result<(usize, BlockHash)> {
135-
let bt = self.bitcoind.client.get_block_template(
136-
GetBlockTemplateModes::Template,
137-
&[GetBlockTemplateRules::SegWit],
138-
&[],
139-
)?;
152+
// let bt = self.bitcoind.client.get_block_template(
153+
// GetBlockTemplateModes::Template,
154+
// &[GetBlockTemplateRules::SegWit],
155+
// &[],
156+
// )?;
157+
let bt = self.get_block_template()?;
140158

141159
let txdata = vec![Transaction {
142160
version: transaction::Version::ONE,
@@ -184,7 +202,15 @@ impl TestEnv {
184202
}
185203
}
186204

187-
self.bitcoind.client.submit_block(&block)?;
205+
let block_hex: String = bdk_chain::bitcoin::consensus::encode::serialize_hex(&block);
206+
match self.bitcoind.client.call(
207+
"submitblock",
208+
&[corepc_node::serde_json::to_value(block_hex)?],
209+
) {
210+
Ok(corepc_node::serde_json::Value::Null) => Ok(()),
211+
Ok(res) => Err(corepc_client::client_sync::Error::Returned(res.to_string())),
212+
Err(err) => Err(err.into()),
213+
}?;
188214
Ok((bt.height as usize, block.block_hash()))
189215
}
190216

@@ -235,18 +261,16 @@ impl TestEnv {
235261

236262
/// Invalidate a number of blocks of a given size `count`.
237263
pub fn invalidate_blocks(&self, count: usize) -> anyhow::Result<()> {
238-
let mut hash = self.bitcoind.client.get_best_block_hash()?;
264+
let mut hash = self.bitcoind.client.get_best_block_hash()?.block_hash()?;
239265
for _ in 0..count {
240-
let prev_hash = self
241-
.bitcoind
242-
.client
243-
.get_block_info(&hash)?
244-
.previousblockhash;
245-
self.bitcoind.client.invalidate_block(&hash)?;
246-
match prev_hash {
247-
Some(prev_hash) => hash = prev_hash,
248-
None => break,
249-
}
266+
let prev_hash = self.bitcoind.client.get_block(hash)?.header.prev_blockhash;
267+
self.bitcoind.client.invalidate_block(hash)?;
268+
hash = prev_hash
269+
// TODO: (@leonardo) It requires a double check if there is any side-effect with this break removal.
270+
// match prev_hash {
271+
// Some(prev_hash) => hash = prev_hash,
272+
// None => break,
273+
// }
250274
}
251275
Ok(())
252276
}
@@ -287,7 +311,8 @@ impl TestEnv {
287311
let txid = self
288312
.bitcoind
289313
.client
290-
.send_to_address(address, amount, None, None, None, None, None, None)?;
314+
.send_to_address(address, amount)?
315+
.txid()?;
291316
Ok(txid)
292317
}
293318

@@ -298,14 +323,19 @@ impl TestEnv {
298323
.client
299324
.get_block_hash(height as u64)
300325
.ok()
301-
.map(|hash| BlockId { height, hash })
326+
.map(|get_block_hash| {
327+
let hash = get_block_hash
328+
.block_hash()
329+
.expect("should `successfully convert to `BlockHash` from `GetBlockHash`");
330+
BlockId { height, hash }
331+
})
302332
}))
303333
.expect("must craft tip")
304334
}
305335

306336
/// Get the genesis hash of the blockchain.
307337
pub fn genesis_hash(&self) -> anyhow::Result<BlockHash> {
308-
let hash = self.bitcoind.client.get_block_hash(0)?;
338+
let hash = self.bitcoind.client.get_block_hash(0)?.into_model()?.0;
309339
Ok(hash)
310340
}
311341
}
@@ -324,7 +354,7 @@ mod test {
324354
// Mine some blocks.
325355
env.mine_blocks(101, None)?;
326356
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
327-
let height = env.bitcoind.client.get_block_count()?;
357+
let height = env.bitcoind.client.get_block_count()?.into_model().0;
328358
let blocks = (0..=height)
329359
.map(|i| env.bitcoind.client.get_block_hash(i))
330360
.collect::<Result<Vec<_>, _>>()?;

0 commit comments

Comments
 (0)