Skip to content

Commit e6f2dbb

Browse files
committed
feat(bitcoind_rpc)!: rm cp field from EmittedMempool
1 parent c93ed01 commit e6f2dbb

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

crates/bitcoind_rpc/src/lib.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929
//! for r in Emitter::new(&client, 709_632, None) {
3030
//! let update = r.expect("todo: deal with the error properly");
3131
//!
32-
//! if update.is_block() {
33-
//! let cp = update.checkpoint();
34-
//! println!("block {}:{}", cp.height(), cp.hash());
35-
//! } else {
36-
//! println!("mempool!");
32+
//! match update.checkpoint() {
33+
//! Some(cp) => println!("block {}:{}", cp.height(), cp.hash()),
34+
//! None => println!("mempool!"),
3735
//! }
3836
//! }
3937
//! ```
@@ -46,7 +44,7 @@
4644
use bdk_chain::{
4745
bitcoin::{Block, Transaction},
4846
indexed_tx_graph::Indexer,
49-
local_chain::CheckPoint,
47+
local_chain::{self, CheckPoint},
5048
Append, BlockId, ConfirmationHeightAnchor, ConfirmationTimeAnchor, TxGraph,
5149
};
5250
pub use bitcoincore_rpc;
@@ -77,13 +75,23 @@ impl EmittedUpdate {
7775
}
7876

7977
/// Get the emission's checkpoint.
80-
pub fn checkpoint(&self) -> CheckPoint {
78+
///
79+
/// The emission will only have a checkpoint if it is the [`EmittedUpdate::Block`] variant.
80+
pub fn checkpoint(&self) -> Option<CheckPoint> {
8181
match self {
82-
EmittedUpdate::Block(e) => e.checkpoint(),
83-
EmittedUpdate::Mempool(e) => e.checkpoint(),
82+
EmittedUpdate::Block(e) => Some(e.checkpoint()),
83+
EmittedUpdate::Mempool(_) => None,
8484
}
8585
}
8686

87+
/// Convenience method to get [`local_chain::Update`].
88+
pub fn chain_update(&self) -> Option<local_chain::Update> {
89+
Some(local_chain::Update {
90+
tip: self.checkpoint()?,
91+
introduce_older_blocks: false,
92+
})
93+
}
94+
8795
/// Transforms the emitted update into a [`TxGraph`] update.
8896
///
8997
/// The `tx_filter` parameter takes in a closure that filters out irrelevant transactions so
@@ -155,18 +163,11 @@ impl EmittedBlock {
155163
/// An emitted subset of mempool transactions.
156164
#[derive(Debug, Clone)]
157165
pub struct EmittedMempool {
158-
/// The checkpoint of the last-seen tip.
159-
pub cp: CheckPoint,
160166
/// Subset of mempool transactions.
161167
pub txs: Vec<(Transaction, u64)>,
162168
}
163169

164170
impl EmittedMempool {
165-
/// Get the emission's checkpoint.
166-
pub fn checkpoint(&self) -> CheckPoint {
167-
self.cp.clone()
168-
}
169-
170171
/// Transforms the emitted mempool into a [`TxGraph`] update.
171172
///
172173
/// The `tx_filter` parameter takes in a closure that filters out irrelevant transactions so
@@ -295,15 +296,7 @@ impl<'c, C: RpcApi> Emitter<'c, C> {
295296
},
296297
)
297298
.collect::<Result<Vec<_>, _>>()?;
298-
let cp = match &self.last_cp {
299-
Some(cp) => cp.clone(),
300-
None => {
301-
let hash = self.client.get_best_block_hash()?;
302-
let height = self.client.get_block_info(&hash)?.height as u32;
303-
CheckPoint::new(BlockId { height, hash })
304-
}
305-
};
306-
Ok(EmittedMempool { cp, txs })
299+
Ok(EmittedMempool { txs })
307300
}
308301

309302
/// Emits the next block (if any).

crates/bitcoind_rpc/tests/test_emitter.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,9 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
211211
for r in Emitter::new(&env.client, 0, chain.tip()) {
212212
let update = r?;
213213

214-
let _ = chain.apply_update(bdk_chain::local_chain::Update {
215-
tip: update.checkpoint(),
216-
introduce_older_blocks: false,
217-
})?;
214+
if let Some(chain_update) = update.chain_update() {
215+
let _ = chain.apply_update(chain_update)?;
216+
}
218217

219218
let tx_graph_update = update.into_tx_graph_update(
220219
bdk_bitcoind_rpc::indexer_filter(&mut indexed_tx_graph.index, &mut BTreeSet::new()),
@@ -299,10 +298,9 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
299298
let update = Emitter::new(&env.client, 0, chain.tip()).emit_update()?;
300299
assert!(update.is_block());
301300

302-
let _ = chain.apply_update(bdk_chain::local_chain::Update {
303-
tip: update.checkpoint(),
304-
introduce_older_blocks: false,
305-
})?;
301+
if let Some(chain_update) = update.chain_update() {
302+
let _ = chain.apply_update(chain_update)?;
303+
}
306304

307305
let tx_graph_update = update.into_tx_graph_update(
308306
bdk_bitcoind_rpc::indexer_filter(&mut indexed_tx_graph.index, &mut BTreeSet::new()),

example-crates/example_rpc/src/main.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,8 @@ fn main() -> anyhow::Result<()> {
192192
let mut start = Instant::now();
193193

194194
for (item, tip_height) in recv {
195-
let is_mempool = item.is_mempool();
195+
let chain_update = item.chain_update();
196196
let tip = item.checkpoint();
197-
let current_height = tip.height();
198197

199198
let db_changeset = {
200199
let mut indexed_changeset = indexed_tx_graph::ChangeSet::default();
@@ -211,10 +210,10 @@ fn main() -> anyhow::Result<()> {
211210
};
212211
indexed_changeset.append(graph.apply_update(graph_update));
213212

214-
let chain_changeset = chain.apply_update(local_chain::Update {
215-
tip,
216-
introduce_older_blocks: false,
217-
})?;
213+
let chain_changeset = match chain_update {
214+
Some(update) => chain.apply_update(update)?,
215+
None => local_chain::ChangeSet::default(),
216+
};
218217

219218
(chain_changeset, indexed_changeset)
220219
};
@@ -237,10 +236,9 @@ fn main() -> anyhow::Result<()> {
237236
};
238237
println!(
239238
"* scanned_to: {} / {} tip | total: {} sats",
240-
if is_mempool {
241-
"mempool".to_string()
242-
} else {
243-
current_height.to_string()
239+
match tip {
240+
Some(cp) => cp.height().to_string(),
241+
None => "mempool".to_string(),
244242
},
245243
tip_height,
246244
balance.confirmed

0 commit comments

Comments
 (0)