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