Skip to content

Commit 8321aaa

Browse files
committed
Merge #1040: Add cli esplora example
f41cc1c fix: s/index_tx_graph/indexed_tx_graph/g (LLFourn) da8cfd3 feat: add cli example for `esplora` (志宇) Pull request description: ### Description This PR builds on top of #1034 and adds a cli-example for our `esplora` chain-src crate. ### Notes to the reviewers Don't merge this until #1034 is merged. The only relevant commit is 5ff0412. ### Changelog notice * Add cli-example for `esplora`. ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: ~* [ ] I've added tests for the new feature~ * [x] I've added docs for the new feature ACKs for top commit: danielabrozzoni: ACK f41cc1c notmandatory: ACK f41cc1c Tree-SHA512: a41fa456a9509f75feea0af013deaaad846cc6b60e5e6671672630a716e8c962361cbc9bb2d62c68e96d5fdb9e580912c19ff5fcab1acaf604b5b4a10eb40cee
2 parents 93e8eaf + f41cc1c commit 8321aaa

File tree

7 files changed

+386
-10
lines changed

7 files changed

+386
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ members = [
77
"crates/esplora",
88
"example-crates/example_cli",
99
"example-crates/example_electrum",
10+
"example-crates/example_esplora",
1011
"example-crates/wallet_electrum",
1112
"example-crates/wallet_esplora_blocking",
1213
"example-crates/wallet_esplora_async",

crates/bdk/src/wallet/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<D> Wallet<D> {
248248

249249
let changeset = db.load_from_persistence().map_err(NewError::Persist)?;
250250
chain.apply_changeset(&changeset.chain);
251-
indexed_graph.apply_changeset(changeset.index_tx_graph);
251+
indexed_graph.apply_changeset(changeset.indexed_tx_graph);
252252

253253
let persist = Persist::new(db);
254254

crates/chain/src/keychain.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,26 +133,26 @@ pub struct WalletChangeSet<K, A> {
133133
/// ChangeSet to [`IndexedTxGraph`].
134134
///
135135
/// [`IndexedTxGraph`]: crate::indexed_tx_graph::IndexedTxGraph
136-
pub index_tx_graph: indexed_tx_graph::ChangeSet<A, ChangeSet<K>>,
136+
pub indexed_tx_graph: indexed_tx_graph::ChangeSet<A, ChangeSet<K>>,
137137
}
138138

139139
impl<K, A> Default for WalletChangeSet<K, A> {
140140
fn default() -> Self {
141141
Self {
142142
chain: Default::default(),
143-
index_tx_graph: Default::default(),
143+
indexed_tx_graph: Default::default(),
144144
}
145145
}
146146
}
147147

148148
impl<K: Ord, A: Anchor> Append for WalletChangeSet<K, A> {
149149
fn append(&mut self, other: Self) {
150150
Append::append(&mut self.chain, other.chain);
151-
Append::append(&mut self.index_tx_graph, other.index_tx_graph);
151+
Append::append(&mut self.indexed_tx_graph, other.indexed_tx_graph);
152152
}
153153

154154
fn is_empty(&self) -> bool {
155-
self.chain.is_empty() && self.index_tx_graph.is_empty()
155+
self.chain.is_empty() && self.indexed_tx_graph.is_empty()
156156
}
157157
}
158158

@@ -166,9 +166,9 @@ impl<K, A> From<local_chain::ChangeSet> for WalletChangeSet<K, A> {
166166
}
167167

168168
impl<K, A> From<indexed_tx_graph::ChangeSet<A, ChangeSet<K>>> for WalletChangeSet<K, A> {
169-
fn from(index_tx_graph: indexed_tx_graph::ChangeSet<A, ChangeSet<K>>) -> Self {
169+
fn from(indexed_tx_graph: indexed_tx_graph::ChangeSet<A, ChangeSet<K>>) -> Self {
170170
Self {
171-
index_tx_graph,
171+
indexed_tx_graph,
172172
..Default::default()
173173
}
174174
}

crates/chain/src/tx_graph.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,45 @@ impl<A> ChangeSet<A> {
10661066
})
10671067
.chain(self.txouts.iter().map(|(op, txout)| (*op, txout)))
10681068
}
1069+
1070+
/// Iterates over the heights of that the new transaction anchors in this changeset.
1071+
///
1072+
/// This is useful if you want to find which heights you need to fetch data about in order to
1073+
/// confirm or exclude these anchors.
1074+
///
1075+
/// See also: [`TxGraph::missing_heights`]
1076+
pub fn anchor_heights(&self) -> impl Iterator<Item = u32> + '_
1077+
where
1078+
A: Anchor,
1079+
{
1080+
let mut dedup = None;
1081+
self.anchors
1082+
.iter()
1083+
.map(|(a, _)| a.anchor_block().height)
1084+
.filter(move |height| {
1085+
let duplicate = dedup == Some(*height);
1086+
dedup = Some(*height);
1087+
!duplicate
1088+
})
1089+
}
1090+
1091+
/// Returns an iterator for the [`anchor_heights`] in this changeset that are not included in
1092+
/// `local_chain`. This tells you which heights you need to include in `local_chain` in order
1093+
/// for it to conclusively act as a [`ChainOracle`] for the transaction anchors this changeset
1094+
/// will add.
1095+
///
1096+
/// [`ChainOracle`]: crate::ChainOracle
1097+
/// [`anchor_heights`]: Self::anchor_heights
1098+
pub fn missing_heights_from<'a>(
1099+
&'a self,
1100+
local_chain: &'a LocalChain,
1101+
) -> impl Iterator<Item = u32> + 'a
1102+
where
1103+
A: Anchor,
1104+
{
1105+
self.anchor_heights()
1106+
.filter(move |height| !local_chain.blocks().contains_key(height))
1107+
}
10691108
}
10701109

10711110
impl<A: Ord> Append for ChangeSet<A> {

example-crates/example_electrum/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn main() -> anyhow::Result<()> {
6868

6969
let graph = Mutex::new({
7070
let mut graph = IndexedTxGraph::new(index);
71-
graph.apply_changeset(init_changeset.index_tx_graph);
71+
graph.apply_changeset(init_changeset.indexed_tx_graph);
7272
graph
7373
});
7474

@@ -277,7 +277,7 @@ fn main() -> anyhow::Result<()> {
277277

278278
let chain = chain.apply_update(final_update.chain)?;
279279

280-
let index_tx_graph = {
280+
let indexed_tx_graph = {
281281
let mut changeset =
282282
indexed_tx_graph::ChangeSet::<ConfirmationHeightAnchor, _>::default();
283283
let (_, indexer) = graph
@@ -292,7 +292,7 @@ fn main() -> anyhow::Result<()> {
292292
};
293293

294294
ChangeSet {
295-
index_tx_graph,
295+
indexed_tx_graph,
296296
chain,
297297
}
298298
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "example_esplora"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
bdk_chain = { path = "../../crates/chain", features = ["serde"] }
10+
bdk_esplora = { path = "../../crates/esplora", features = ["blocking"] }
11+
example_cli = { path = "../example_cli" }
12+

0 commit comments

Comments
 (0)