Skip to content

Commit

Permalink
feat(esplora)!: depend on bdk_core instead of bdk_chain
Browse files Browse the repository at this point in the history
Helper methods have been changed to use concrete types (instead of
`A: Anchor` generic) - since `Anchor` is still part of `bdk_chain`.

Also fix esplora docs.
  • Loading branch information
evanlinjin committed Aug 24, 2024
1 parent 0d302f5 commit fea8eed
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 50 deletions.
3 changes: 2 additions & 1 deletion crates/esplora/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bdk_chain = { path = "../chain", version = "0.17.0", default-features = false }
bdk_core = { path = "../core", version = "0.1", default-features = false }
esplora-client = { version = "0.9.0", default-features = false }
async-trait = { version = "0.1.66", optional = true }
futures = { version = "0.3.26", optional = true }
miniscript = { version = "12.0.0", optional = true, default-features = false }

[dev-dependencies]
bdk_chain = { path = "../chain", version = "0.17.0" }
bdk_testenv = { path = "../testenv", default-features = false }
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }

Expand Down
4 changes: 2 additions & 2 deletions crates/esplora/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ For full examples, refer to [`example-crates/wallet_esplora_blocking`](https://g
[`bdk_chain`]: https://docs.rs/bdk-chain/
[`EsploraExt`]: crate::EsploraExt
[`EsploraAsyncExt`]: crate::EsploraAsyncExt
[`SyncRequest`]: bdk_chain::spk_client::SyncRequest
[`FullScanRequest`]: bdk_chain::spk_client::FullScanRequest
[`SyncRequest`]: bdk_core::spk_client::SyncRequest
[`FullScanRequest`]: bdk_core::spk_client::FullScanRequest
[`sync`]: crate::EsploraExt::sync
[`full_scan`]: crate::EsploraExt::full_scan
39 changes: 21 additions & 18 deletions crates/esplora/src/async_ext.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use std::collections::{BTreeSet, HashSet};

use async_trait::async_trait;
use bdk_chain::spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult};
use bdk_chain::{
use bdk_core::collections::{BTreeMap, BTreeSet, HashSet};
use bdk_core::spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult};
use bdk_core::{
bitcoin::{BlockHash, OutPoint, ScriptBuf, Txid},
collections::BTreeMap,
local_chain::CheckPoint,
BlockId, ConfirmationBlockTime,
tx_graph, BlockId, CheckPoint, ConfirmationBlockTime, Indexed,
};
use bdk_chain::{tx_graph, Anchor, Indexed};
use futures::{stream::FuturesOrdered, TryStreamExt};

use crate::{insert_anchor_from_status, insert_prevouts};
Expand Down Expand Up @@ -209,11 +205,11 @@ async fn fetch_block(
///
/// We want to have a corresponding checkpoint per anchor height. However, checkpoints fetched
/// should not surpass `latest_blocks`.
async fn chain_update<A: Anchor>(
async fn chain_update(
client: &esplora_client::AsyncClient,
latest_blocks: &BTreeMap<u32, BlockHash>,
local_tip: &CheckPoint,
anchors: &BTreeSet<(A, Txid)>,
anchors: &BTreeSet<(ConfirmationBlockTime, Txid)>,
) -> Result<CheckPoint, Error> {
let mut point_of_agreement = None;
let mut conflicts = vec![];
Expand Down Expand Up @@ -242,8 +238,8 @@ async fn chain_update<A: Anchor>(
.extend(conflicts.into_iter().rev())
.expect("evicted are in order");

for anchor in anchors {
let height = anchor.0.anchor_block().height;
for (anchor, _txid) in anchors {
let height = anchor.block_id.height;
if tip.get(height).is_none() {
let hash = match fetch_block(client, latest_blocks, height).await? {
Some(hash) => hash,
Expand Down Expand Up @@ -494,6 +490,7 @@ mod test {
local_chain::LocalChain,
BlockId,
};
use bdk_core::ConfirmationBlockTime;
use bdk_testenv::{anyhow, bitcoincore_rpc::RpcApi, TestEnv};
use esplora_client::Builder;

Expand Down Expand Up @@ -572,9 +569,12 @@ mod test {
.iter()
.map(|&height| -> anyhow::Result<_> {
Ok((
BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
ConfirmationBlockTime {
block_id: BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
},
confirmation_time: height as _,
},
Txid::all_zeros(),
))
Expand Down Expand Up @@ -610,9 +610,12 @@ mod test {
.iter()
.map(|&(height, txid)| -> anyhow::Result<_> {
Ok((
BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
ConfirmationBlockTime {
block_id: BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
},
confirmation_time: height as _,
},
txid,
))
Expand Down
50 changes: 28 additions & 22 deletions crates/esplora/src/blocking_ext.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use std::collections::{BTreeSet, HashSet};
use std::thread::JoinHandle;

use bdk_chain::collections::BTreeMap;
use bdk_chain::spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult};
use bdk_chain::{
use bdk_core::collections::{BTreeMap, BTreeSet, HashSet};
use bdk_core::spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult};
use bdk_core::{
bitcoin::{BlockHash, OutPoint, ScriptBuf, Txid},
local_chain::CheckPoint,
BlockId, ConfirmationBlockTime,
tx_graph, BlockId, CheckPoint, ConfirmationBlockTime, Indexed,
};
use bdk_chain::{tx_graph, Anchor, Indexed};
use esplora_client::{OutputStatus, Tx};
use std::thread::JoinHandle;

use crate::{insert_anchor_from_status, insert_prevouts};

Expand Down Expand Up @@ -199,11 +195,11 @@ fn fetch_block(
///
/// We want to have a corresponding checkpoint per anchor height. However, checkpoints fetched
/// should not surpass `latest_blocks`.
fn chain_update<A: Anchor>(
fn chain_update(
client: &esplora_client::BlockingClient,
latest_blocks: &BTreeMap<u32, BlockHash>,
local_tip: &CheckPoint,
anchors: &BTreeSet<(A, Txid)>,
anchors: &BTreeSet<(ConfirmationBlockTime, Txid)>,
) -> Result<CheckPoint, Error> {
let mut point_of_agreement = None;
let mut conflicts = vec![];
Expand Down Expand Up @@ -232,8 +228,8 @@ fn chain_update<A: Anchor>(
.extend(conflicts.into_iter().rev())
.expect("evicted are in order");

for anchor in anchors {
let height = anchor.0.anchor_block().height;
for (anchor, _) in anchors {
let height = anchor.block_id.height;
if tip.get(height).is_none() {
let hash = match fetch_block(client, latest_blocks, height)? {
Some(hash) => hash,
Expand Down Expand Up @@ -475,6 +471,7 @@ mod test {
use bdk_chain::bitcoin::Txid;
use bdk_chain::local_chain::LocalChain;
use bdk_chain::BlockId;
use bdk_core::ConfirmationBlockTime;
use bdk_testenv::{anyhow, bitcoincore_rpc::RpcApi, TestEnv};
use esplora_client::{BlockHash, Builder};
use std::collections::{BTreeMap, BTreeSet};
Expand Down Expand Up @@ -561,9 +558,12 @@ mod test {
.iter()
.map(|&height| -> anyhow::Result<_> {
Ok((
BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
ConfirmationBlockTime {
block_id: BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
},
confirmation_time: height as _,
},
Txid::all_zeros(),
))
Expand Down Expand Up @@ -598,9 +598,12 @@ mod test {
.iter()
.map(|&(height, txid)| -> anyhow::Result<_> {
Ok((
BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
ConfirmationBlockTime {
block_id: BlockId {
height,
hash: env.bitcoind.client.get_block_hash(height as _)?,
},
confirmation_time: height as _,
},
txid,
))
Expand Down Expand Up @@ -794,9 +797,12 @@ mod test {
let txid: Txid = bdk_chain::bitcoin::hashes::Hash::hash(
&format!("txid_at_height_{}", h).into_bytes(),
);
let anchor = BlockId {
height: h,
hash: anchor_blockhash,
let anchor = ConfirmationBlockTime {
block_id: BlockId {
height: h,
hash: anchor_blockhash,
},
confirmation_time: h as _,
};
(anchor, txid)
})
Expand Down
9 changes: 2 additions & 7 deletions crates/esplora/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@
//! Just like how [`EsploraExt`] extends the functionality of an
//! [`esplora_client::BlockingClient`], [`EsploraAsyncExt`] is the async version which extends
//! [`esplora_client::AsyncClient`].
//!
//! [`TxGraph`]: bdk_chain::tx_graph::TxGraph
//! [`LocalChain`]: bdk_chain::local_chain::LocalChain
//! [`ChainOracle`]: bdk_chain::ChainOracle
//! [`example_esplora`]: https://github.com/bitcoindevkit/bdk/tree/master/example-crates/example_esplora
use bdk_chain::bitcoin::{Amount, OutPoint, TxOut, Txid};
use bdk_chain::{tx_graph, BlockId, ConfirmationBlockTime};
use bdk_core::bitcoin::{Amount, OutPoint, TxOut, Txid};
use bdk_core::{tx_graph, BlockId, ConfirmationBlockTime};
use esplora_client::TxStatus;

pub use esplora_client;
Expand Down

0 comments on commit fea8eed

Please sign in to comment.