Skip to content

Commit fcfb7dd

Browse files
committed
Merge #2210: docs(rpc): add Emitter::next_block and Emitter::mempool examples
30fa179 docs(rpc): add Emitter::next_block and Emitter::mempool examples (Kyle 🐆) Pull request description: Adds `# Example` rustdoc to `Emitter::next_block` and `Emitter::mempool`, which were flagged as missing in the #2006 review. Originally this PR also removed `example_bitcoind_rpc_polling`, but #2006 has since merged and removed all example crates, so that part is dropped after rebase. Only the doc additions remain. Part of the #2006 follow-up to document the components previously covered by the `examples/` crates. ACKs for top commit: ValuedMammal: ACK 30fa179 Tree-SHA512: 28ade24c33f9714f94282a4594024bff9ed304c461b477b7ba5e95de64fbc5951c23a6a93ee623cc764e1e31971aaccac3aaebe4f615637d5b2de6d56bc32b56
2 parents 6d03fc3 + 30fa179 commit fcfb7dd

1 file changed

Lines changed: 61 additions & 3 deletions

File tree

crates/bitcoind_rpc/src/lib.rs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,42 @@ where
101101

102102
/// Emit mempool transactions and any evicted [`Txid`]s.
103103
///
104-
/// This method returns a [`MempoolEvent`] containing the full transactions (with their
105-
/// first-seen unix timestamps) that were emitted, and [`MempoolEvent::evicted`] which are
106-
/// any [`Txid`]s which were previously seen in the mempool and are now missing. Evicted txids
104+
/// This method returns a [`MempoolEvent`] containing the full transactions that were emitted,
105+
/// each stamped with the call's `sync_time` (unix seconds captured at the start of the call,
106+
/// not when the node first saw the transaction), and [`MempoolEvent::evicted`] which are any
107+
/// [`Txid`]s which were previously seen in the mempool and are now missing. The timestamp
108+
/// advances on each poll, so callers should treat it as a "last seen" value. Evicted txids
107109
/// are only reported once the emitter’s checkpoint matches the RPC’s best block in both height
108110
/// and hash. Until `next_block()` advances the checkpoint to tip, `mempool()` will always
109111
/// return an empty `evicted` set.
112+
///
113+
/// # Example
114+
///
115+
/// ```no_run
116+
/// use bdk_bitcoind_rpc::{
117+
/// bitcoincore_rpc::{Auth, Client},
118+
/// Emitter, NO_EXPECTED_MEMPOOL_TXS,
119+
/// };
120+
/// # use bdk_core::CheckPoint;
121+
/// # use bitcoin::{constants::genesis_block, Network};
122+
///
123+
/// let client = Client::new("127.0.0.1:8332", Auth::None)?;
124+
/// # let last_cp = CheckPoint::new(0, genesis_block(Network::Bitcoin).block_hash());
125+
/// // Use the checkpoint from your receiving structure (e.g. `LocalChain::tip()`).
126+
/// let mut emitter = Emitter::new(&client, last_cp, 0, NO_EXPECTED_MEMPOOL_TXS);
127+
///
128+
/// // Drain blocks first so evictions can be reported once the checkpoint reaches tip.
129+
/// while emitter.next_block()?.is_some() {}
130+
///
131+
/// let event = emitter.mempool()?;
132+
/// for (tx, seen_at) in event.update {
133+
/// // index unconfirmed `tx` (last seen at `seen_at`)
134+
/// }
135+
/// for (txid, evicted_at) in event.evicted {
136+
/// // mark `txid` as evicted from the mempool at `evicted_at`
137+
/// }
138+
/// # Ok::<_, bdk_bitcoind_rpc::bitcoincore_rpc::Error>(())
139+
/// ```
110140
#[cfg(feature = "std")]
111141
pub fn mempool(&mut self) -> Result<MempoolEvent, bitcoincore_rpc::Error> {
112142
let sync_time = std::time::UNIX_EPOCH
@@ -199,6 +229,34 @@ where
199229
}
200230

201231
/// Emit the next block height and block (if any).
232+
///
233+
/// Call this repeatedly until it returns `Ok(None)`, which means the chain tip has been
234+
/// reached. Each [`BlockEvent`] carries the block alongside the [`CheckPoint`] it connects to,
235+
/// so it can be applied to BDK structures that require block connectivity.
236+
///
237+
/// # Example
238+
///
239+
/// ```no_run
240+
/// use bdk_bitcoind_rpc::{
241+
/// bitcoincore_rpc::{Auth, Client},
242+
/// Emitter, NO_EXPECTED_MEMPOOL_TXS,
243+
/// };
244+
/// # use bdk_core::CheckPoint;
245+
/// # use bitcoin::{constants::genesis_block, Network};
246+
///
247+
/// let client = Client::new("127.0.0.1:8332", Auth::None)?;
248+
/// let start_height = 0;
249+
/// # let last_cp = CheckPoint::new(0, genesis_block(Network::Bitcoin).block_hash());
250+
/// // Use the checkpoint from your receiving structure (e.g. `LocalChain::tip()`).
251+
/// let mut emitter = Emitter::new(&client, last_cp, start_height, NO_EXPECTED_MEMPOOL_TXS);
252+
///
253+
/// while let Some(event) = emitter.next_block()? {
254+
/// let height = event.block_height();
255+
/// let connected_to = event.connected_to();
256+
/// // apply `event.block` to your chain and tx graph here
257+
/// }
258+
/// # Ok::<_, bdk_bitcoind_rpc::bitcoincore_rpc::Error>(())
259+
/// ```
202260
pub fn next_block(&mut self) -> Result<Option<BlockEvent<Block>>, bitcoincore_rpc::Error> {
203261
if let Some((checkpoint, block)) = poll(self, move |hash, client| client.get_block(hash))? {
204262
// Stop tracking unconfirmed transactions that have been confirmed in this block.

0 commit comments

Comments
 (0)