From 1370ed5434587240da75be359dffcb833fe97edc Mon Sep 17 00:00:00 2001 From: luhrhenz Date: Thu, 8 Oct 2026 11:24:54 +0100 Subject: [PATCH] refactor(testenv): move TxTemplate from chain tests to bdk_testenv Move TxTemplate, TxInTemplate, TxOutTemplate, and TxTemplateEnv into bdk_testenv behind a miniscript feature gate so they can be reused by benchmarks and other crates' tests. Ref #1936 --- crates/chain/Cargo.toml | 2 +- crates/chain/tests/common/mod.rs | 4 +--- crates/testenv/Cargo.toml | 3 +++ crates/testenv/src/lib.rs | 3 +++ .../{chain/tests/common => testenv/src}/tx_template.rs | 9 ++------- 5 files changed, 10 insertions(+), 11 deletions(-) rename crates/{chain/tests/common => testenv/src}/tx_template.rs (97%) diff --git a/crates/chain/Cargo.toml b/crates/chain/Cargo.toml index e474f99714..52ea1cb1c8 100644 --- a/crates/chain/Cargo.toml +++ b/crates/chain/Cargo.toml @@ -27,7 +27,7 @@ rusqlite = { version = "0.31.0", features = ["bundled"], optional = true } [dev-dependencies] rand = "0.8" proptest = "1.2.0" -bdk_testenv = { path = "../testenv" } +bdk_testenv = { path = "../testenv", features = ["miniscript"] } criterion = { version = "0.7" } [features] diff --git a/crates/chain/tests/common/mod.rs b/crates/chain/tests/common/mod.rs index cb3ee66f37..24b30c1f84 100644 --- a/crates/chain/tests/common/mod.rs +++ b/crates/chain/tests/common/mod.rs @@ -1,5 +1,3 @@ #![cfg(feature = "miniscript")] -mod tx_template; -#[allow(unused_imports)] -pub use tx_template::*; +pub use bdk_testenv::tx_template::*; diff --git a/crates/testenv/Cargo.toml b/crates/testenv/Cargo.toml index 6cedcf4d30..869ec650fa 100644 --- a/crates/testenv/Cargo.toml +++ b/crates/testenv/Cargo.toml @@ -19,6 +19,8 @@ workspace = true bdk_chain = { path = "../chain", version = "0.23.1", default-features = false } electrsd = { version = "0.38.0", features = [ "legacy" ], default-features = false } bitcoin = { version = "0.32.0", default-features = false } +miniscript = { version = "13.0.0", optional = true, default-features = false } +rand = { version = "0.8", optional = true } [dev-dependencies] bdk_testenv = { path = "." } @@ -28,6 +30,7 @@ default = ["std", "download"] download = ["electrsd/bitcoind_download", "electrsd/bitcoind_28_2", "electrsd/esplora_a33e97e1"] std = ["bdk_chain/std", "bitcoin/rand-std"] serde = ["bdk_chain/serde"] +miniscript = ["dep:miniscript", "bdk_chain/miniscript", "rand"] [package.metadata.docs.rs] no-default-features = true diff --git a/crates/testenv/src/lib.rs b/crates/testenv/src/lib.rs index 3c3f8a6f48..7513922418 100644 --- a/crates/testenv/src/lib.rs +++ b/crates/testenv/src/lib.rs @@ -2,6 +2,9 @@ pub mod utils; +#[cfg(feature = "miniscript")] +pub mod tx_template; + use anyhow::Context; use bdk_chain::bitcoin::{ block::Header, hash_types::TxMerkleNode, hex::FromHex, script::PushBytesBuf, transaction, diff --git a/crates/chain/tests/common/tx_template.rs b/crates/testenv/src/tx_template.rs similarity index 97% rename from crates/chain/tests/common/tx_template.rs rename to crates/testenv/src/tx_template.rs index 7bdac78a50..399c54e9d8 100644 --- a/crates/chain/tests/common/tx_template.rs +++ b/crates/testenv/src/tx_template.rs @@ -1,6 +1,4 @@ -#![cfg(feature = "miniscript")] - -use bdk_testenv::utils::DESCRIPTORS; +use crate::utils::DESCRIPTORS; use rand::distributions::{Alphanumeric, DistString}; use std::collections::HashMap; @@ -27,7 +25,6 @@ pub struct TxTemplate<'a, A> { pub assume_canonical: bool, } -#[allow(dead_code)] pub enum TxInTemplate<'a> { /// This will give a random txid and vout. Bogus, @@ -45,14 +42,12 @@ pub struct TxOutTemplate { pub spk_index: Option, // some = get spk from SpkTxOutIndex, none = random spk } -#[allow(unused)] impl TxOutTemplate { pub fn new(value: u64, spk_index: Option) -> Self { TxOutTemplate { value, spk_index } } } -#[allow(dead_code)] pub struct TxTemplateEnv<'a, A> { pub tx_graph: TxGraph, pub indexer: SpkTxOutIndex, @@ -60,7 +55,7 @@ pub struct TxTemplateEnv<'a, A> { pub canonicalization_params: CanonicalParams, } -#[allow(dead_code)] +/// Build a [`TxGraph`] and associated indexer from a series of [`TxTemplate`]s. pub fn init_graph<'a, A: Anchor + Clone + 'a>( tx_templates: impl IntoIterator>, ) -> TxTemplateEnv<'a, A> {