diff --git a/.gitignore b/.gitignore index 95285763a3..f3ee3a8e47 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ Cargo.lock # Example persisted files. *.db +bdk_wallet_esplora_async_example.dat +bdk_wallet_esplora_blocking_example.dat diff --git a/crates/bdk/src/wallet/error.rs b/crates/bdk/src/wallet/error.rs index a90083fa12..b4a77b5855 100644 --- a/crates/bdk/src/wallet/error.rs +++ b/crates/bdk/src/wallet/error.rs @@ -246,7 +246,7 @@ impl

From for CreateTxError

{ impl std::error::Error for CreateTxError

{} #[derive(Debug)] -/// Error returned by [`Wallet::build_fee_bump`] +/// Error returned by [`crate::Wallet::build_fee_bump`] pub enum BuildFeeBumpError { /// Happens when trying to spend an UTXO that is not in the internal database UnknownUtxo(OutPoint), diff --git a/example-crates/example_esplora/src/main.rs b/example-crates/example_esplora/src/main.rs index e922057066..ea3bb26004 100644 --- a/example-crates/example_esplora/src/main.rs +++ b/example-crates/example_esplora/src/main.rs @@ -1,9 +1,10 @@ use std::{ - collections::{BTreeMap, BTreeSet}, + collections::BTreeMap, io::{self, Write}, sync::Mutex, }; +use bdk_chain::spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult}; use bdk_chain::{ bitcoin::{constants::genesis_block, Address, Network, OutPoint, ScriptBuf, Txid}, indexed_tx_graph::{self, IndexedTxGraph}, @@ -82,7 +83,7 @@ impl EsploraArgs { Network::Bitcoin => "https://blockstream.info/api", Network::Testnet => "https://blockstream.info/testnet/api", Network::Regtest => "http://localhost:3002", - Network::Signet => "https://mempool.space/signet/api", + Network::Signet => "http://signet.bitcoindevkit.net", _ => panic!("unsupported network"), }); @@ -157,7 +158,7 @@ fn main() -> anyhow::Result<()> { // after an initial scan. // Syncing: We only check for specified spks, utxos and txids to update their confirmation // status or fetch missing transactions. - let indexed_tx_graph_changeset = match &esplora_cmd { + let (chain_changeset, indexed_tx_graph_changeset) = match &esplora_cmd { EsploraCommands::Scan { stop_gap, scan_options, @@ -189,8 +190,15 @@ fn main() -> anyhow::Result<()> { // is reached. It returns a `TxGraph` update (`graph_update`) and a structure that // represents the last active spk derivation indices of keychains // (`keychain_indices_update`). - let (graph_update, last_active_indices) = client - .full_scan(keychain_spks, *stop_gap, scan_options.parallel_requests) + let mut request = FullScanRequest::new(chain.lock().unwrap().tip()); + request.add_spks_by_keychain(keychain_spks); + + let FullScanResult { + graph_update, + chain_update, + last_active_indices, + } = client + .full_scan(request, *stop_gap, scan_options.parallel_requests) .context("scanning for transactions")?; let mut graph = graph.lock().expect("mutex must not be poisoned"); @@ -201,7 +209,14 @@ fn main() -> anyhow::Result<()> { let (_, index_changeset) = graph.index.reveal_to_target_multi(&last_active_indices); let mut indexed_tx_graph_changeset = graph.apply_update(graph_update); indexed_tx_graph_changeset.append(index_changeset.into()); - indexed_tx_graph_changeset + + // apply the local chain update + let chain_changeset = { + println!("\ncurrent tip: {}", chain_update.tip.height()); + chain.lock().unwrap().apply_update(chain_update)? + }; + + (chain_changeset, indexed_tx_graph_changeset) } EsploraCommands::Sync { mut unused_spks, @@ -306,42 +321,35 @@ fn main() -> anyhow::Result<()> { })); } } + let request = { + let chain = chain.lock().expect("mutex must not be poisoned"); + let mut request = SyncRequest::new(chain.tip()); + request.add_spks(spks); + request.add_txids(txids); + request.add_outpoints(outpoints); + request + }; + let SyncResult { + graph_update, + chain_update, + } = client.sync(request, scan_options.parallel_requests)?; - let graph_update = - client.sync(spks, txids, outpoints, scan_options.parallel_requests)?; + // apply the local chain update + let chain_changeset = { + let mut chain = chain.lock().expect("mutex must not be poisoned"); + println!("current tip: {}", chain_update.tip.height()); + chain.apply_update(chain_update)? + }; - graph.lock().unwrap().apply_update(graph_update) + ( + chain_changeset, + graph.lock().unwrap().apply_update(graph_update), + ) } }; println!(); - // Now that we're done updating the `IndexedTxGraph`, it's time to update the `LocalChain`! We - // want the `LocalChain` to have data about all the anchors in the `TxGraph` - for this reason, - // we want retrieve the blocks at the heights of the newly added anchors that are missing from - // our view of the chain. - let (missing_block_heights, tip) = { - let chain = &*chain.lock().unwrap(); - let missing_block_heights = indexed_tx_graph_changeset - .graph - .missing_heights_from(chain) - .collect::>(); - let tip = chain.tip(); - (missing_block_heights, tip) - }; - - println!("prev tip: {}", tip.height()); - println!("missing block heights: {:?}", missing_block_heights); - - // Here, we actually fetch the missing blocks and create a `local_chain::Update`. - let chain_changeset = { - let chain_update = client - .update_local_chain(tip, missing_block_heights) - .context("scanning for blocks")?; - println!("new tip: {}", chain_update.tip.height()); - chain.lock().unwrap().apply_update(chain_update)? - }; - // We persist the changes let mut db = db.lock().unwrap(); db.stage((chain_changeset, indexed_tx_graph_changeset)); diff --git a/example-crates/wallet_esplora_async/Cargo.toml b/example-crates/wallet_esplora_async/Cargo.toml index c588a87aa6..8e71ea9930 100644 --- a/example-crates/wallet_esplora_async/Cargo.toml +++ b/example-crates/wallet_esplora_async/Cargo.toml @@ -11,3 +11,5 @@ bdk_esplora = { path = "../../crates/esplora", features = ["async-https"] } bdk_file_store = { path = "../../crates/file_store" } tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] } anyhow = "1" +env_logger = { version = "0.10", default-features = false, features = ["humantime"] } +log = "0.4.20" diff --git a/example-crates/wallet_esplora_async/src/main.rs b/example-crates/wallet_esplora_async/src/main.rs index 690cd87e24..9b7a394161 100644 --- a/example-crates/wallet_esplora_async/src/main.rs +++ b/example-crates/wallet_esplora_async/src/main.rs @@ -1,5 +1,8 @@ -use std::{io::Write, str::FromStr}; +use env_logger::Env; +use std::env; +use std::str::FromStr; +use bdk::chain::spk_client::{FullScanResult, SyncResult}; use bdk::{ bitcoin::{Address, Network}, wallet::{AddressIndex, Update}, @@ -7,6 +10,7 @@ use bdk::{ }; use bdk_esplora::{esplora_client, EsploraAsyncExt}; use bdk_file_store::Store; +use log::info; const DB_MAGIC: &str = "bdk_wallet_esplora_async_example"; const SEND_AMOUNT: u64 = 5000; @@ -15,63 +19,93 @@ const PARALLEL_REQUESTS: usize = 5; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { - let db_path = std::env::temp_dir().join("bdk-esplora-async-example"); + env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); + + let args: Vec = env::args().collect(); + let cmd = args.get(1); + + let db_path = "bdk_wallet_esplora_async_example.dat"; let db = Store::::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?; let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)"; let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)"; + let network = Network::Signet; let mut wallet = Wallet::new_or_load( external_descriptor, Some(internal_descriptor), db, - Network::Testnet, + network, )?; let address = wallet.try_get_address(AddressIndex::New)?; - println!("Generated Address: {}", address); + info!("Generated Address: {}", address); let balance = wallet.get_balance(); - println!("Wallet balance before syncing: {} sats", balance.total()); - - print!("Syncing..."); - let client = - esplora_client::Builder::new("https://blockstream.info/testnet/api").build_async()?; - - let prev_tip = wallet.latest_checkpoint(); - let keychain_spks = wallet - .all_unbounded_spk_iters() - .into_iter() - .map(|(k, k_spks)| { - let mut once = Some(()); - let mut stdout = std::io::stdout(); - let k_spks = k_spks - .inspect(move |(spk_i, _)| match once.take() { - Some(_) => print!("\nScanning keychain [{:?}]", k), - None => print!(" {:<3}", spk_i), - }) - .inspect(move |_| stdout.flush().expect("must flush")); - (k, k_spks) - }) - .collect(); - let (update_graph, last_active_indices) = client - .full_scan(keychain_spks, STOP_GAP, PARALLEL_REQUESTS) - .await?; - let missing_heights = update_graph.missing_heights(wallet.local_chain()); - let chain_update = client.update_local_chain(prev_tip, missing_heights).await?; - let update = Update { - last_active_indices, - graph: update_graph, - chain: Some(chain_update), - }; + info!("Wallet balance: {} sats", balance.total()); + + let client = esplora_client::Builder::new("http://signet.bitcoindevkit.net").build_async()?; + let (update, cmd) = match cmd.map(|c| c.as_str()) { + Some(cmd) if cmd == "fullscan" => { + info!("Start full scan..."); + // 1. get data required to do a wallet full_scan + let mut request = wallet.full_scan_request(); + request.inspect_spks(move |(i, s)| { + info!("scanning index: {}, address: {}", i, Address::from_script(s, network).expect("address")); + }); + // 2. full scan to discover wallet transactions and update blockchain + let FullScanResult { + graph_update, + chain_update, + last_active_indices, + } = client + .full_scan(request, STOP_GAP, PARALLEL_REQUESTS) + .await?; + // 3. create wallet update + Ok(( + Update { + last_active_indices, + graph: graph_update, + chain: Some(chain_update), + }, + cmd, + )) + } + Some(cmd) if cmd == "sync" => { + info!("Start sync..."); + // 1. get data required to do a wallet sync, if also syncing previously used addresses set unused_spks_only = false + let mut request = wallet.sync_revealed_spks_request(); + request.inspect_spks(move |s| { + info!("syncing address: {}", Address::from_script(s, network).expect("address")); + }); + // 2. sync unused wallet spks (addresses), unconfirmed tx, utxos and update blockchain + let SyncResult { + graph_update, + chain_update, + } = client.sync(request, PARALLEL_REQUESTS).await?; + // 3. create wallet update + Ok(( + Update { + graph: graph_update, + chain: Some(chain_update), + ..Update::default() + }, + cmd, + )) + } + _ => Err(()), + } + .expect("Specify if you want to do a wallet 'fullscan' or a 'sync'."); + + // 4. apply update to wallet wallet.apply_update(update)?; + // 5. commit wallet update to database wallet.commit()?; - println!(); let balance = wallet.get_balance(); - println!("Wallet balance after syncing: {} sats", balance.total()); + info!("Wallet balance after {}: {} sats", cmd, balance.total()); if balance.total() < SEND_AMOUNT { - println!( + info!( "Please send at least {} sats to the receiving address", SEND_AMOUNT ); @@ -79,7 +113,7 @@ async fn main() -> Result<(), anyhow::Error> { } let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")? - .require_network(Network::Testnet)?; + .require_network(network)?; let mut tx_builder = wallet.build_tx(); tx_builder @@ -92,7 +126,7 @@ async fn main() -> Result<(), anyhow::Error> { let tx = psbt.extract_tx(); client.broadcast(&tx).await?; - println!("Tx broadcasted! Txid: {}", tx.txid()); + info!("Tx broadcasted! Txid: {}", tx.txid()); Ok(()) } diff --git a/example-crates/wallet_esplora_blocking/Cargo.toml b/example-crates/wallet_esplora_blocking/Cargo.toml index 0679bd8f38..5d6ed9b007 100644 --- a/example-crates/wallet_esplora_blocking/Cargo.toml +++ b/example-crates/wallet_esplora_blocking/Cargo.toml @@ -11,3 +11,5 @@ bdk = { path = "../../crates/bdk" } bdk_esplora = { path = "../../crates/esplora", features = ["blocking"] } bdk_file_store = { path = "../../crates/file_store" } anyhow = "1" +env_logger = { version = "0.10", default-features = false, features = ["humantime"] } +log = "0.4.20" diff --git a/example-crates/wallet_esplora_blocking/src/main.rs b/example-crates/wallet_esplora_blocking/src/main.rs index 73bfdd5598..3a4407a104 100644 --- a/example-crates/wallet_esplora_blocking/src/main.rs +++ b/example-crates/wallet_esplora_blocking/src/main.rs @@ -1,10 +1,14 @@ -const DB_MAGIC: &str = "bdk_wallet_esplora_example"; +const DB_MAGIC: &str = "bdk_wallet_esplora_blocking_example"; const SEND_AMOUNT: u64 = 1000; const STOP_GAP: usize = 5; const PARALLEL_REQUESTS: usize = 1; -use std::{io::Write, str::FromStr}; +use env_logger::Env; +use log::info; +use std::env; +use std::str::FromStr; +use bdk::chain::spk_client::{FullScanResult, SyncResult}; use bdk::{ bitcoin::{Address, Network}, wallet::{AddressIndex, Update}, @@ -14,64 +18,93 @@ use bdk_esplora::{esplora_client, EsploraExt}; use bdk_file_store::Store; fn main() -> Result<(), anyhow::Error> { - let db_path = std::env::temp_dir().join("bdk-esplora-example"); + env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); + + let args: Vec = env::args().collect(); + let cmd = args.get(1); + + let db_path = "bdk_wallet_esplora_blocking_example.dat"; let db = Store::::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?; let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)"; let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)"; + let network = Network::Signet; let mut wallet = Wallet::new_or_load( external_descriptor, Some(internal_descriptor), db, - Network::Testnet, + network, )?; let address = wallet.try_get_address(AddressIndex::New)?; - println!("Generated Address: {}", address); + info!("Generated Address: {}", address); let balance = wallet.get_balance(); - println!("Wallet balance before syncing: {} sats", balance.total()); + info!("Wallet balance: {} sats", balance.total()); - print!("Syncing..."); let client = - esplora_client::Builder::new("https://blockstream.info/testnet/api").build_blocking()?; - - let prev_tip = wallet.latest_checkpoint(); - let keychain_spks = wallet - .all_unbounded_spk_iters() - .into_iter() - .map(|(k, k_spks)| { - let mut once = Some(()); - let mut stdout = std::io::stdout(); - let k_spks = k_spks - .inspect(move |(spk_i, _)| match once.take() { - Some(_) => print!("\nScanning keychain [{:?}]", k), - None => print!(" {:<3}", spk_i), - }) - .inspect(move |_| stdout.flush().expect("must flush")); - (k, k_spks) - }) - .collect(); - - let (update_graph, last_active_indices) = - client.full_scan(keychain_spks, STOP_GAP, PARALLEL_REQUESTS)?; - let missing_heights = update_graph.missing_heights(wallet.local_chain()); - let chain_update = client.update_local_chain(prev_tip, missing_heights)?; - let update = Update { - last_active_indices, - graph: update_graph, - chain: Some(chain_update), - }; + esplora_client::Builder::new("http://signet.bitcoindevkit.net").build_blocking()?; + + let (update, cmd) = match cmd.map(|c| c.as_str()) { + Some(cmd) if cmd == "fullscan" => { + info!("Start full scan..."); + // 1. get data required to do a wallet full_scan + let mut request = wallet.full_scan_request(); + request.inspect_spks(move |(i, s)| { + info!("scanning index: {}, address: {}", i, Address::from_script(s, network).expect("address")); + }); + // 2. full scan to discover wallet transactions + let FullScanResult { + graph_update, + chain_update, + last_active_indices, + } = client.full_scan(request, STOP_GAP, PARALLEL_REQUESTS)?; + // 3. create wallet update + Ok(( + Update { + last_active_indices, + graph: graph_update, + chain: Some(chain_update), + }, + cmd, + )) + } + Some(cmd) if cmd == "sync" => { + info!("Start sync..."); + // 1. get data required to do a wallet sync, if also syncing previously used addresses set unused_spks_only = false + let mut request = wallet.sync_revealed_spks_request(); + request.inspect_spks(move |s| { + info!("syncing address: {}", Address::from_script(s, network).expect("address")); + }); + // 2. sync unused wallet spks (addresses), unconfirmed tx, and utxos + let SyncResult { + graph_update, + chain_update, + } = client.sync(request, PARALLEL_REQUESTS)?; + // 3. create wallet update + Ok(( + Update { + graph: graph_update, + chain: Some(chain_update), + ..Update::default() + }, + cmd, + )) + } + _ => Err(()), + } + .expect("Specify if you want to do a wallet 'fullscan' or a 'sync'."); + // 4. apply update to wallet wallet.apply_update(update)?; + // 5. commit wallet update to database wallet.commit()?; - println!(); let balance = wallet.get_balance(); - println!("Wallet balance after syncing: {} sats", balance.total()); + info!("Wallet balance after {}: {} sats", cmd, balance.total()); if balance.total() < SEND_AMOUNT { - println!( + info!( "Please send at least {} sats to the receiving address", SEND_AMOUNT ); @@ -79,7 +112,7 @@ fn main() -> Result<(), anyhow::Error> { } let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")? - .require_network(Network::Testnet)?; + .require_network(network)?; let mut tx_builder = wallet.build_tx(); tx_builder @@ -92,7 +125,7 @@ fn main() -> Result<(), anyhow::Error> { let tx = psbt.extract_tx(); client.broadcast(&tx)?; - println!("Tx broadcasted! Txid: {}", tx.txid()); + info!("Tx broadcasted! Txid: {}", tx.txid()); Ok(()) }