Skip to content

Commit

Permalink
chore: remove anyhow dev dependency from electrum and esplora
Browse files Browse the repository at this point in the history
  • Loading branch information
LagginTimes committed Apr 26, 2024
1 parent ec34ad1 commit 50d1792
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 63 deletions.
3 changes: 1 addition & 2 deletions crates/electrum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ electrum-client = { version = "0.19" }
#rustls = { version = "=0.21.1", optional = true, features = ["dangerous_configuration"] }

[dev-dependencies]
bdk_testenv = { path = "../testenv", default-features = false }
anyhow = "1"
bdk_testenv = { path = "../testenv", default-features = false }
30 changes: 16 additions & 14 deletions crates/electrum/tests/test_electrum.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use anyhow::Result;
use bdk_chain::{
bitcoin::{hashes::Hash, Address, Amount, ScriptBuf, WScriptHash},
keychain::Balance,
local_chain::LocalChain,
ConfirmationTimeHeightAnchor, IndexedTxGraph, SpkTxOutIndex,
};
use bdk_electrum::{ElectrumExt, ElectrumUpdate};
use bdk_testenv::{electrsd::bitcoind::bitcoincore_rpc::RpcApi, TestEnv};
use bdk_testenv::{
electrsd::bitcoind::{anyhow::Error, bitcoincore_rpc::RpcApi},
TestEnv,
};

fn get_balance(
recv_chain: &LocalChain,
recv_graph: &IndexedTxGraph<ConfirmationTimeHeightAnchor, SpkTxOutIndex<()>>,
) -> Result<Balance> {
) -> Result<Balance, Error> {
let chain_tip = recv_chain.tip().block_id();
let outpoints = recv_graph.index.outpoints().clone();
let balance = recv_graph
Expand All @@ -27,7 +29,7 @@ fn get_balance(
/// 3. Mine extra block to confirm sent tx.
/// 4. Check [`Balance`] to ensure tx is confirmed.
#[test]
fn scan_detects_confirmed_tx() -> Result<()> {
fn scan_detects_confirmed_tx() -> Result<(), Error> {
const SEND_AMOUNT: Amount = Amount::from_sat(10_000);

let env = TestEnv::new()?;
Expand Down Expand Up @@ -68,9 +70,9 @@ fn scan_detects_confirmed_tx() -> Result<()> {

let missing = relevant_txids.missing_full_txs(recv_graph.graph());
let graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, missing)?;
let _ = recv_chain
.apply_update(chain_update)
.map_err(|err| anyhow::anyhow!("LocalChain update error: {:?}", err))?;
let _ = recv_chain.apply_update(chain_update).map_err(|err| {
bdk_testenv::electrsd::bitcoind::anyhow::anyhow!("LocalChain update error: {:?}", err)
})?;
let _ = recv_graph.apply_update(graph_update);

// Check to see if tx is confirmed.
Expand All @@ -92,7 +94,7 @@ fn scan_detects_confirmed_tx() -> Result<()> {
/// 3. Perform 8 separate reorgs on each block with a confirmed tx.
/// 4. Check [`Balance`] after each reorg to ensure unconfirmed amount is correct.
#[test]
fn tx_can_become_unconfirmed_after_reorg() -> Result<()> {
fn tx_can_become_unconfirmed_after_reorg() -> Result<(), Error> {
const REORG_COUNT: usize = 8;
const SEND_AMOUNT: Amount = Amount::from_sat(10_000);

Expand Down Expand Up @@ -134,9 +136,9 @@ fn tx_can_become_unconfirmed_after_reorg() -> Result<()> {

let missing = relevant_txids.missing_full_txs(recv_graph.graph());
let graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, missing)?;
let _ = recv_chain
.apply_update(chain_update)
.map_err(|err| anyhow::anyhow!("LocalChain update error: {:?}", err))?;
let _ = recv_chain.apply_update(chain_update).map_err(|err| {
bdk_testenv::electrsd::bitcoind::anyhow::anyhow!("LocalChain update error: {:?}", err)
})?;
let _ = recv_graph.apply_update(graph_update.clone());

// Retain a snapshot of all anchors before reorg process.
Expand Down Expand Up @@ -164,9 +166,9 @@ fn tx_can_become_unconfirmed_after_reorg() -> Result<()> {

let missing = relevant_txids.missing_full_txs(recv_graph.graph());
let graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, missing)?;
let _ = recv_chain
.apply_update(chain_update)
.map_err(|err| anyhow::anyhow!("LocalChain update error: {:?}", err))?;
let _ = recv_chain.apply_update(chain_update).map_err(|err| {
bdk_testenv::electrsd::bitcoind::anyhow::anyhow!("LocalChain update error: {:?}", err)
})?;

// Check to see if a new anchor is added during current reorg.
if !initial_anchors.is_superset(graph_update.all_anchors()) {
Expand Down
1 change: 0 additions & 1 deletion crates/esplora/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ miniscript = { version = "11.0.0", optional = true, default-features = false }
[dev-dependencies]
bdk_testenv = { path = "../testenv", default_features = false }
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
anyhow = "1"

[features]
default = ["std", "async-https", "blocking-https-rustls"]
Expand Down
50 changes: 27 additions & 23 deletions crates/esplora/src/async_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ mod test {

/// Ensure that update does not remove heights (from original), and all anchor heights are included.
#[tokio::test]
pub async fn test_finalize_chain_update() -> anyhow::Result<()> {
pub async fn test_finalize_chain_update() -> bdk_testenv::electrsd::bitcoind::anyhow::Result<()>
{
struct TestCase<'a> {
name: &'a str,
/// Initial blockchain height to start the env with.
Expand Down Expand Up @@ -485,16 +486,18 @@ mod test {
let anchors = t
.initial_cps
.iter()
.map(|&height| -> anyhow::Result<_> {
Ok((
BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
},
Txid::all_zeros(),
))
})
.collect::<anyhow::Result<BTreeSet<_>>>()?;
.map(
|&height| -> bdk_testenv::electrsd::bitcoind::anyhow::Result<_> {
Ok((
BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
},
Txid::all_zeros(),
))
},
)
.collect::<bdk_testenv::electrsd::bitcoind::anyhow::Result<BTreeSet<_>>>()?;
let update = chain_update(
&client,
&fetch_latest_blocks(&client).await?,
Expand All @@ -519,11 +522,12 @@ mod test {
}

// craft update
let update = {
let anchors = t
let update =
{
let anchors = t
.anchors
.iter()
.map(|&(height, txid)| -> anyhow::Result<_> {
.map(|&(height, txid)| -> bdk_testenv::electrsd::bitcoind::anyhow::Result<_> {
Ok((
BlockId {
height,
Expand All @@ -532,15 +536,15 @@ mod test {
txid,
))
})
.collect::<anyhow::Result<_>>()?;
chain_update(
&client,
&fetch_latest_blocks(&client).await?,
&local_chain.tip(),
&anchors,
)
.await?
};
.collect::<bdk_testenv::electrsd::bitcoind::anyhow::Result<_>>()?;
chain_update(
&client,
&fetch_latest_blocks(&client).await?,
&local_chain.tip(),
&anchors,
)
.await?
};

// apply update
let mut updated_local_chain = local_chain.clone();
Expand Down
49 changes: 26 additions & 23 deletions crates/esplora/src/blocking_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ mod test {

/// Ensure that update does not remove heights (from original), and all anchor heights are included.
#[test]
pub fn test_finalize_chain_update() -> anyhow::Result<()> {
pub fn test_finalize_chain_update() -> bdk_testenv::electrsd::bitcoind::anyhow::Result<()> {
struct TestCase<'a> {
name: &'a str,
/// Initial blockchain height to start the env with.
Expand Down Expand Up @@ -498,16 +498,18 @@ mod test {
let anchors = t
.initial_cps
.iter()
.map(|&height| -> anyhow::Result<_> {
Ok((
BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
},
Txid::all_zeros(),
))
})
.collect::<anyhow::Result<BTreeSet<_>>>()?;
.map(
|&height| -> bdk_testenv::electrsd::bitcoind::anyhow::Result<_> {
Ok((
BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
},
Txid::all_zeros(),
))
},
)
.collect::<bdk_testenv::electrsd::bitcoind::anyhow::Result<BTreeSet<_>>>()?;
let update = chain_update(
&client,
&fetch_latest_blocks(&client)?,
Expand All @@ -531,11 +533,12 @@ mod test {
}

// craft update
let update = {
let anchors = t
let update =
{
let anchors = t
.anchors
.iter()
.map(|&(height, txid)| -> anyhow::Result<_> {
.map(|&(height, txid)| -> bdk_testenv::electrsd::bitcoind::anyhow::Result<_> {
Ok((
BlockId {
height,
Expand All @@ -544,14 +547,14 @@ mod test {
txid,
))
})
.collect::<anyhow::Result<_>>()?;
chain_update(
&client,
&fetch_latest_blocks(&client)?,
&local_chain.tip(),
&anchors,
)?
};
.collect::<bdk_testenv::electrsd::bitcoind::anyhow::Result<_>>()?;
chain_update(
&client,
&fetch_latest_blocks(&client)?,
&local_chain.tip(),
&anchors,
)?
};

// apply update
let mut updated_local_chain = local_chain.clone();
Expand Down Expand Up @@ -598,7 +601,7 @@ mod test {
}

#[test]
fn update_local_chain() -> anyhow::Result<()> {
fn update_local_chain() -> bdk_testenv::electrsd::bitcoind::anyhow::Result<()> {
const TIP_HEIGHT: u32 = 50;

let env = TestEnv::new()?;
Expand Down

0 comments on commit 50d1792

Please sign in to comment.