| 
 | 1 | +use std::{  | 
 | 2 | +    collections::BTreeMap,  | 
 | 3 | +    io::{self, Write},  | 
 | 4 | +    sync::Mutex,  | 
 | 5 | +};  | 
 | 6 | + | 
 | 7 | +use bdk_chain::{  | 
 | 8 | +    bitcoin::{Address, Network, OutPoint, Txid},  | 
 | 9 | +    indexed_tx_graph::{IndexedAdditions, IndexedTxGraph},  | 
 | 10 | +    keychain::LocalChangeSet,  | 
 | 11 | +    local_chain::{CheckPoint, LocalChain},  | 
 | 12 | +    Append, ConfirmationTimeAnchor,  | 
 | 13 | +};  | 
 | 14 | + | 
 | 15 | +use bdk_esplora::{esplora_client, EsploraExt};  | 
 | 16 | + | 
 | 17 | +use example_cli::{  | 
 | 18 | +    anyhow::{self, Context},  | 
 | 19 | +    clap::{self, Parser, Subcommand},  | 
 | 20 | +    Keychain,  | 
 | 21 | +};  | 
 | 22 | + | 
 | 23 | +const DB_MAGIC: &[u8] = b"bdk_example_esplora";  | 
 | 24 | +const DB_PATH: &str = ".bdk_esplora_example.db";  | 
 | 25 | + | 
 | 26 | +#[derive(Subcommand, Debug, Clone)]  | 
 | 27 | +enum EsploraCommands {  | 
 | 28 | +    /// Scans the addresses in the wallet sing the esplora API.  | 
 | 29 | +    Scan {  | 
 | 30 | +        /// When a gap this large has been found for a keychain, it will stop.  | 
 | 31 | +        #[clap(long, default_value = "5")]  | 
 | 32 | +        stop_gap: usize,  | 
 | 33 | +        #[clap(flatten)]  | 
 | 34 | +        scan_options: ScanOptions,  | 
 | 35 | +    },  | 
 | 36 | +    /// Scans particular addresses using the esplora API.  | 
 | 37 | +    Sync {  | 
 | 38 | +        /// Scan all the unused addresses.  | 
 | 39 | +        #[clap(long)]  | 
 | 40 | +        unused_spks: bool,  | 
 | 41 | +        /// Scan every address that you have derived.  | 
 | 42 | +        #[clap(long)]  | 
 | 43 | +        all_spks: bool,  | 
 | 44 | +        /// Scan unspent outpoints for spends or changes to confirmation status of residing tx.  | 
 | 45 | +        #[clap(long)]  | 
 | 46 | +        utxos: bool,  | 
 | 47 | +        /// Scan unconfirmed transactions for updates.  | 
 | 48 | +        #[clap(long)]  | 
 | 49 | +        unconfirmed: bool,  | 
 | 50 | +        #[clap(flatten)]  | 
 | 51 | +        scan_options: ScanOptions,  | 
 | 52 | +    },  | 
 | 53 | +}  | 
 | 54 | + | 
 | 55 | +#[derive(Parser, Debug, Clone, PartialEq)]  | 
 | 56 | +pub struct ScanOptions {  | 
 | 57 | +    /// Max number of concurrent esplora server requests.  | 
 | 58 | +    #[clap(long, default_value = "1")]  | 
 | 59 | +    pub parallel_requests: usize,  | 
 | 60 | +}  | 
 | 61 | + | 
 | 62 | +fn main() -> anyhow::Result<()> {  | 
 | 63 | +    let (args, keymap, index, db, init_changeset) = example_cli::init::<  | 
 | 64 | +        EsploraCommands,  | 
 | 65 | +        LocalChangeSet<Keychain, ConfirmationTimeAnchor>,  | 
 | 66 | +    >(DB_MAGIC, DB_PATH)?;  | 
 | 67 | + | 
 | 68 | +    let graph = Mutex::new({  | 
 | 69 | +        let mut graph = IndexedTxGraph::new(index);  | 
 | 70 | +        graph.apply_additions(init_changeset.indexed_additions);  | 
 | 71 | +        graph  | 
 | 72 | +    });  | 
 | 73 | + | 
 | 74 | +    let chain = Mutex::new({  | 
 | 75 | +        let mut chain = LocalChain::default();  | 
 | 76 | +        chain.apply_changeset(&init_changeset.chain_changeset);  | 
 | 77 | +        chain  | 
 | 78 | +    });  | 
 | 79 | + | 
 | 80 | +    let esplora_url = match args.network {  | 
 | 81 | +        Network::Bitcoin => "https://blockstream.info/api",  | 
 | 82 | +        Network::Testnet => "https://blockstream.info/testnet/api",  | 
 | 83 | +        Network::Regtest => "http://localhost:3002",  | 
 | 84 | +        Network::Signet => "https://mempool.space/signet/api",  | 
 | 85 | +    };  | 
 | 86 | + | 
 | 87 | +    let client = esplora_client::Builder::new(esplora_url).build_blocking()?;  | 
 | 88 | + | 
 | 89 | +    // Match the given command. Exectute and return if command is provided by example_cli  | 
 | 90 | +    let esplora_cmd = match &args.command {  | 
 | 91 | +        // Command that are handled by the specify example  | 
 | 92 | +        example_cli::Commands::ChainSpecific(electrum_cmd) => electrum_cmd,  | 
 | 93 | +        // General commands handled by example_cli. Execute the cmd and return.  | 
 | 94 | +        general_cmd => {  | 
 | 95 | +            let res = example_cli::handle_commands(  | 
 | 96 | +                &graph,  | 
 | 97 | +                &db,  | 
 | 98 | +                &chain,  | 
 | 99 | +                &keymap,  | 
 | 100 | +                args.network,  | 
 | 101 | +                |tx| {  | 
 | 102 | +                    client  | 
 | 103 | +                        .broadcast(tx)  | 
 | 104 | +                        .map(|_| ())  | 
 | 105 | +                        .map_err(anyhow::Error::from)  | 
 | 106 | +                },  | 
 | 107 | +                general_cmd.clone(),  | 
 | 108 | +            );  | 
 | 109 | + | 
 | 110 | +            db.lock().unwrap().commit()?;  | 
 | 111 | +            return res;  | 
 | 112 | +        }  | 
 | 113 | +    };  | 
 | 114 | + | 
 | 115 | +    let (update_graph, update_keychain_indices) = match &esplora_cmd {  | 
 | 116 | +        EsploraCommands::Scan {  | 
 | 117 | +            stop_gap,  | 
 | 118 | +            scan_options,  | 
 | 119 | +        } => {  | 
 | 120 | +            let graph = graph.lock().unwrap();  | 
 | 121 | + | 
 | 122 | +            let keychain_spks = graph  | 
 | 123 | +                .index  | 
 | 124 | +                .spks_of_all_keychains()  | 
 | 125 | +                .into_iter()  | 
 | 126 | +                .map(|(keychain, iter)| {  | 
 | 127 | +                    let mut first = true;  | 
 | 128 | +                    let spk_iter = iter.inspect(move |(i, _)| {  | 
 | 129 | +                        if first {  | 
 | 130 | +                            eprint!("\nscanning {}: ", keychain);  | 
 | 131 | +                            first = false;  | 
 | 132 | +                        }  | 
 | 133 | +                        eprint!("{} ", i);  | 
 | 134 | +                        let _ = io::stdout().flush();  | 
 | 135 | +                    });  | 
 | 136 | +                    (keychain, spk_iter)  | 
 | 137 | +                })  | 
 | 138 | +                .collect::<BTreeMap<_, _>>();  | 
 | 139 | + | 
 | 140 | +            drop(graph);  | 
 | 141 | + | 
 | 142 | +            client  | 
 | 143 | +                .update_tx_graph(  | 
 | 144 | +                    keychain_spks,  | 
 | 145 | +                    core::iter::empty(),  | 
 | 146 | +                    core::iter::empty(),  | 
 | 147 | +                    *stop_gap,  | 
 | 148 | +                    scan_options.parallel_requests,  | 
 | 149 | +                )  | 
 | 150 | +                .context("scanning for transactions")?  | 
 | 151 | +        }  | 
 | 152 | +        EsploraCommands::Sync {  | 
 | 153 | +            mut unused_spks,  | 
 | 154 | +            all_spks,  | 
 | 155 | +            mut utxos,  | 
 | 156 | +            mut unconfirmed,  | 
 | 157 | +            scan_options,  | 
 | 158 | +        } => {  | 
 | 159 | +            // Get a short lock on the tracker to get the spks we're interested in  | 
 | 160 | +            let graph = graph.lock().unwrap();  | 
 | 161 | +            let chain = chain.lock().unwrap();  | 
 | 162 | +            let chain_tip = chain.tip().map(|cp| cp.block_id()).unwrap_or_default();  | 
 | 163 | + | 
 | 164 | +            if !(*all_spks || unused_spks || utxos || unconfirmed) {  | 
 | 165 | +                unused_spks = true;  | 
 | 166 | +                unconfirmed = true;  | 
 | 167 | +                utxos = true;  | 
 | 168 | +            } else if *all_spks {  | 
 | 169 | +                unused_spks = false;  | 
 | 170 | +            }  | 
 | 171 | + | 
 | 172 | +            let mut spks: Box<dyn Iterator<Item = bdk_chain::bitcoin::Script>> =  | 
 | 173 | +                Box::new(core::iter::empty());  | 
 | 174 | +            if *all_spks {  | 
 | 175 | +                let all_spks = graph  | 
 | 176 | +                    .index  | 
 | 177 | +                    .all_spks()  | 
 | 178 | +                    .iter()  | 
 | 179 | +                    .map(|(k, v)| (*k, v.clone()))  | 
 | 180 | +                    .collect::<Vec<_>>();  | 
 | 181 | +                spks = Box::new(spks.chain(all_spks.into_iter().map(|(index, script)| {  | 
 | 182 | +                    eprintln!("scanning {:?}", index);  | 
 | 183 | +                    script  | 
 | 184 | +                })));  | 
 | 185 | +            }  | 
 | 186 | +            if unused_spks {  | 
 | 187 | +                let unused_spks = graph  | 
 | 188 | +                    .index  | 
 | 189 | +                    .unused_spks(..)  | 
 | 190 | +                    .map(|(k, v)| (*k, v.clone()))  | 
 | 191 | +                    .collect::<Vec<_>>();  | 
 | 192 | +                spks = Box::new(spks.chain(unused_spks.into_iter().map(|(index, script)| {  | 
 | 193 | +                    eprintln!(  | 
 | 194 | +                        "Checking if address {} {:?} has been used",  | 
 | 195 | +                        Address::from_script(&script, args.network).unwrap(),  | 
 | 196 | +                        index  | 
 | 197 | +                    );  | 
 | 198 | + | 
 | 199 | +                    script  | 
 | 200 | +                })));  | 
 | 201 | +            }  | 
 | 202 | + | 
 | 203 | +            let mut outpoints: Box<dyn Iterator<Item = OutPoint>> = Box::new(core::iter::empty());  | 
 | 204 | + | 
 | 205 | +            if utxos {  | 
 | 206 | +                let init_outpoints = graph.index.outpoints().iter().cloned();  | 
 | 207 | + | 
 | 208 | +                let utxos = graph  | 
 | 209 | +                    .graph()  | 
 | 210 | +                    .filter_chain_unspents(&*chain, chain_tip, init_outpoints)  | 
 | 211 | +                    .map(|(_, utxo)| utxo)  | 
 | 212 | +                    .collect::<Vec<_>>();  | 
 | 213 | + | 
 | 214 | +                outpoints = Box::new(  | 
 | 215 | +                    utxos  | 
 | 216 | +                        .into_iter()  | 
 | 217 | +                        .inspect(|utxo| {  | 
 | 218 | +                            eprintln!(  | 
 | 219 | +                                "Checking if outpoint {} (value: {}) has been spent",  | 
 | 220 | +                                utxo.outpoint, utxo.txout.value  | 
 | 221 | +                            );  | 
 | 222 | +                        })  | 
 | 223 | +                        .map(|utxo| utxo.outpoint),  | 
 | 224 | +                );  | 
 | 225 | +            };  | 
 | 226 | + | 
 | 227 | +            let mut txids: Box<dyn Iterator<Item = Txid>> = Box::new(core::iter::empty());  | 
 | 228 | + | 
 | 229 | +            if unconfirmed {  | 
 | 230 | +                let unconfirmed_txids = graph  | 
 | 231 | +                    .graph()  | 
 | 232 | +                    .list_chain_txs(&*chain, chain_tip)  | 
 | 233 | +                    .filter(|canonical_tx| !canonical_tx.observed_as.is_confirmed())  | 
 | 234 | +                    .map(|canonical_tx| canonical_tx.node.txid)  | 
 | 235 | +                    .collect::<Vec<Txid>>();  | 
 | 236 | + | 
 | 237 | +                txids = Box::new(unconfirmed_txids.into_iter().inspect(|txid| {  | 
 | 238 | +                    eprintln!("Checking if {} is confirmed yet", txid);  | 
 | 239 | +                }));  | 
 | 240 | +            }  | 
 | 241 | + | 
 | 242 | +            // drop lock on graph and chain  | 
 | 243 | +            drop((graph, chain));  | 
 | 244 | + | 
 | 245 | +            (  | 
 | 246 | +                client  | 
 | 247 | +                    .update_tx_graph_without_keychain(  | 
 | 248 | +                        spks,  | 
 | 249 | +                        txids,  | 
 | 250 | +                        outpoints,  | 
 | 251 | +                        scan_options.parallel_requests,  | 
 | 252 | +                    )  | 
 | 253 | +                    .context("syncing transaction updates")?,  | 
 | 254 | +                Default::default(),  | 
 | 255 | +            )  | 
 | 256 | +        }  | 
 | 257 | +    };  | 
 | 258 | + | 
 | 259 | +    println!();  | 
 | 260 | + | 
 | 261 | +    let (heights_to_fetch, tip) = {  | 
 | 262 | +        let chain = &*chain.lock().unwrap();  | 
 | 263 | + | 
 | 264 | +        let heights_to_fetch = update_graph.missing_blocks(chain).collect::<Vec<_>>();  | 
 | 265 | +        let tip = chain.tip();  | 
 | 266 | +        (heights_to_fetch, tip)  | 
 | 267 | +    };  | 
 | 268 | + | 
 | 269 | +    #[cfg(debug_assertions)]  | 
 | 270 | +    println!(  | 
 | 271 | +        "old chain: {:?}",  | 
 | 272 | +        tip.iter()  | 
 | 273 | +            .flat_map(CheckPoint::iter)  | 
 | 274 | +            .map(|cp| cp.height())  | 
 | 275 | +            .collect::<Vec<_>>()  | 
 | 276 | +    );  | 
 | 277 | +    println!("prev tip: {}", tip.as_ref().map_or(0, CheckPoint::height));  | 
 | 278 | +    println!("missing blocks: {:?}", heights_to_fetch);  | 
 | 279 | + | 
 | 280 | +    let update = client  | 
 | 281 | +        .update_local_chain(tip, heights_to_fetch)  | 
 | 282 | +        .context("scanning for blocks")?;  | 
 | 283 | + | 
 | 284 | +    #[cfg(debug_assertions)]  | 
 | 285 | +    println!(  | 
 | 286 | +        "new chain: {:?}",  | 
 | 287 | +        update.tip.iter().map(|cp| cp.height()).collect::<Vec<_>>()  | 
 | 288 | +    );  | 
 | 289 | +    println!("new tip: {}", update.tip.height());  | 
 | 290 | + | 
 | 291 | +    // check that all anchors are part of the new tip's history  | 
 | 292 | +    #[cfg(debug_assertions)]  | 
 | 293 | +    {  | 
 | 294 | +        use bdk_chain::bitcoin::BlockHash;  | 
 | 295 | +        use bdk_chain::collections::HashMap;  | 
 | 296 | +        let chain_heights = update  | 
 | 297 | +            .tip  | 
 | 298 | +            .iter()  | 
 | 299 | +            .map(|cp| (cp.height(), cp.hash()))  | 
 | 300 | +            .collect::<HashMap<u32, BlockHash>>();  | 
 | 301 | +        for (anchor, _) in update_graph.all_anchors() {  | 
 | 302 | +            assert_eq!(anchor.anchor_block.height, anchor.confirmation_height);  | 
 | 303 | +            assert!(chain_heights.contains_key(&anchor.anchor_block.height));  | 
 | 304 | + | 
 | 305 | +            let remote_hash = chain_heights  | 
 | 306 | +                .get(&anchor.confirmation_height)  | 
 | 307 | +                .expect("must have block");  | 
 | 308 | + | 
 | 309 | +            // inform about mismatched blocks  | 
 | 310 | +            if remote_hash != &anchor.anchor_block.hash {  | 
 | 311 | +                println!("mismatched block @ {}!", anchor.confirmation_height);  | 
 | 312 | +                println!("\t- anchor_block: {}", anchor.anchor_block.hash);  | 
 | 313 | +                println!("\t-   from_chain: {}", remote_hash);  | 
 | 314 | +            }  | 
 | 315 | +        }  | 
 | 316 | +    }  | 
 | 317 | + | 
 | 318 | +    let db_changeset: LocalChangeSet<Keychain, ConfirmationTimeAnchor> = {  | 
 | 319 | +        let mut chain = chain.lock().unwrap();  | 
 | 320 | +        let mut graph = graph.lock().unwrap();  | 
 | 321 | + | 
 | 322 | +        let chain_changeset = chain.apply_update(update)?;  | 
 | 323 | + | 
 | 324 | +        let indexed_additions = {  | 
 | 325 | +            let mut additions = IndexedAdditions::default();  | 
 | 326 | +            let (_, index_additions) = graph.index.reveal_to_target_multi(&update_keychain_indices);  | 
 | 327 | +            additions.append(IndexedAdditions::from(index_additions));  | 
 | 328 | +            additions.append(graph.apply_update(update_graph));  | 
 | 329 | +            additions  | 
 | 330 | +        };  | 
 | 331 | + | 
 | 332 | +        LocalChangeSet {  | 
 | 333 | +            chain_changeset,  | 
 | 334 | +            indexed_additions,  | 
 | 335 | +        }  | 
 | 336 | +    };  | 
 | 337 | + | 
 | 338 | +    let mut db = db.lock().unwrap();  | 
 | 339 | +    db.stage(db_changeset);  | 
 | 340 | +    db.commit()?;  | 
 | 341 | +    Ok(())  | 
 | 342 | +}  | 
0 commit comments