Skip to content

Commit 1074412

Browse files
committed
feat(rpc): introduce FilterIter
1 parent 7969898 commit 1074412

File tree

5 files changed

+533
-1
lines changed

5 files changed

+533
-1
lines changed

crates/bitcoind_rpc/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ bitcoincore-rpc = { version = "0.19.0" }
2121
bdk_core = { path = "../core", version = "0.3.0", default-features = false }
2222

2323
[dev-dependencies]
24+
bdk_bitcoind_rpc = { path = ".", default-features = true }
2425
bdk_testenv = { path = "../testenv", default-features = false }
2526
bdk_chain = { path = "../chain" }
2627

2728
[features]
2829
default = ["std"]
2930
std = ["bitcoin/std", "bdk_core/std"]
3031
serde = ["bitcoin/serde", "bdk_core/serde"]
32+
33+
[[example]]
34+
name = "bip158"
35+
required-features = ["std"]
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#![allow(clippy::print_stdout)]
2+
use std::time::Instant;
3+
4+
use anyhow::Context;
5+
use bdk_bitcoind_rpc::bip158::{Event, EventInner, FilterIter};
6+
use bdk_chain::bitcoin::{constants::genesis_block, secp256k1::Secp256k1, Network};
7+
use bdk_chain::indexer::keychain_txout::KeychainTxOutIndex;
8+
use bdk_chain::local_chain::LocalChain;
9+
use bdk_chain::miniscript::Descriptor;
10+
use bdk_chain::{BlockId, ConfirmationBlockTime, IndexedTxGraph, SpkIterator};
11+
use bdk_testenv::anyhow;
12+
13+
// This example shows how BDK chain and tx-graph structures are updated using compact
14+
// filters syncing. Assumes a connection can be made to a bitcoin node via environment
15+
// variables `RPC_URL` and `RPC_COOKIE`.
16+
17+
// Usage: `cargo run -p bdk_bitcoind_rpc --example bip158`
18+
19+
const EXTERNAL: &str = "tr([7d94197e]tprv8ZgxMBicQKsPe1chHGzaa84k1inY2nAXUL8iPSyWESPrEst4E5oCFXhPATqj5fvw34LDknJz7rtXyEC4fKoXryUdc9q87pTTzfQyv61cKdE/86'/1'/0'/0/*)#uswl2jj7";
20+
const INTERNAL: &str = "tr([7d94197e]tprv8ZgxMBicQKsPe1chHGzaa84k1inY2nAXUL8iPSyWESPrEst4E5oCFXhPATqj5fvw34LDknJz7rtXyEC4fKoXryUdc9q87pTTzfQyv61cKdE/86'/1'/0'/1/*)#dyt7h8zx";
21+
const SPK_COUNT: u32 = 25;
22+
const NETWORK: Network = Network::Signet;
23+
24+
const START_HEIGHT: u32 = 170_000;
25+
const START_HASH: &str = "00000041c812a89f084f633e4cf47e819a2f6b1c0a15162355a930410522c99d";
26+
27+
fn main() -> anyhow::Result<()> {
28+
// Setup receiving chain and graph structures.
29+
let secp = Secp256k1::new();
30+
let (descriptor, _) = Descriptor::parse_descriptor(&secp, EXTERNAL)?;
31+
let (change_descriptor, _) = Descriptor::parse_descriptor(&secp, INTERNAL)?;
32+
let (mut chain, _) = LocalChain::from_genesis_hash(genesis_block(NETWORK).block_hash());
33+
let mut graph = IndexedTxGraph::<ConfirmationBlockTime, KeychainTxOutIndex<&str>>::new({
34+
let mut index = KeychainTxOutIndex::default();
35+
index.insert_descriptor("external", descriptor.clone())?;
36+
index.insert_descriptor("internal", change_descriptor.clone())?;
37+
index
38+
});
39+
40+
// Assume a minimum birthday height
41+
let block = BlockId {
42+
height: START_HEIGHT,
43+
hash: START_HASH.parse()?,
44+
};
45+
let _ = chain.insert_block(block)?;
46+
47+
// Configure RPC client
48+
let url = std::env::var("RPC_URL").context("must set RPC_URL")?;
49+
let cookie = std::env::var("RPC_COOKIE").context("must set RPC_COOKIE")?;
50+
let rpc_client =
51+
bitcoincore_rpc::Client::new(&url, bitcoincore_rpc::Auth::CookieFile(cookie.into()))?;
52+
53+
// Initialize block emitter
54+
let cp = chain.tip();
55+
let start_height = cp.height();
56+
let mut emitter = FilterIter::new_with_checkpoint(&rpc_client, cp);
57+
for (_, desc) in graph.index.keychains() {
58+
let spks = SpkIterator::new_with_range(desc, 0..SPK_COUNT).map(|(_, spk)| spk);
59+
emitter.add_spks(spks);
60+
}
61+
62+
let start = Instant::now();
63+
64+
// Sync
65+
if let Some(tip) = emitter.get_tip()? {
66+
let blocks_to_scan = tip.height - start_height;
67+
68+
for event in emitter.by_ref() {
69+
let event = event?;
70+
let curr = event.height();
71+
// apply relevant blocks
72+
if let Event::Block(EventInner { height, ref block }) = event {
73+
let _ = graph.apply_block_relevant(block, height);
74+
println!("Matched block {}", curr);
75+
}
76+
if curr % 1000 == 0 {
77+
let progress = (curr - start_height) as f32 / blocks_to_scan as f32;
78+
println!("[{:.2}%]", progress * 100.0);
79+
}
80+
}
81+
// update chain
82+
if let Some(tip) = emitter.chain_update() {
83+
let _ = chain.apply_update(tip)?;
84+
}
85+
}
86+
87+
println!("\ntook: {}s", start.elapsed().as_secs());
88+
println!("Local tip: {}", chain.tip().height());
89+
let unspent: Vec<_> = graph
90+
.graph()
91+
.filter_chain_unspents(
92+
&chain,
93+
chain.tip().block_id(),
94+
graph.index.outpoints().clone(),
95+
)
96+
.collect();
97+
if !unspent.is_empty() {
98+
println!("\nUnspent");
99+
for (index, utxo) in unspent {
100+
// (k, index) | value | outpoint |
101+
println!("{:?} | {} | {}", index, utxo.txout.value, utxo.outpoint,);
102+
}
103+
}
104+
105+
Ok(())
106+
}

0 commit comments

Comments
 (0)