Skip to content

docs(wallet): add sync operation to bdk_wallet examples #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 80 additions & 9 deletions examples/example_wallet_electrum/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bdk_wallet::bitcoin::Txid;
use bdk_wallet::file_store::Store;
use bdk_wallet::Wallet;
use std::io::Write;
Expand Down Expand Up @@ -39,12 +40,12 @@ fn main() -> Result<(), anyhow::Error> {

let address = wallet.next_unused_address(KeychainKind::External);
wallet.persist(&mut db)?;
println!("Generated Address: {}", address);
println!("Generated Address: {address}");

let balance = wallet.balance();
println!("Wallet balance before syncing: {}", balance.total());

print!("Syncing...");
println!("=== Performing Full Sync ===");
let client = BdkElectrumClient::new(electrum_client::Client::new(ELECTRUM_URL)?);

// Populate the electrum client's transaction cache so it doesn't redownload transaction we
Expand All @@ -56,9 +57,9 @@ fn main() -> Result<(), anyhow::Error> {
let mut once = HashSet::<KeychainKind>::new();
move |k, spk_i, _| {
if once.insert(k) {
print!("\nScanning keychain [{:?}]", k);
print!("\nScanning keychain [{k:?}]");
}
print!(" {:<3}", spk_i);
print!(" {spk_i:<3}");
stdout.flush().expect("must flush");
}
});
Expand All @@ -71,13 +72,15 @@ fn main() -> Result<(), anyhow::Error> {
wallet.persist(&mut db)?;

let balance = wallet.balance();
println!("Wallet balance after syncing: {}", balance.total());
println!("Wallet balance after full sync: {}", balance.total());
println!(
"Wallet has {} transactions and {} utxos after full sync",
wallet.transactions().count(),
wallet.list_unspent().count()
);

if balance.total() < SEND_AMOUNT {
println!(
"Please send at least {} to the receiving address",
SEND_AMOUNT
);
println!("Please send at least {SEND_AMOUNT} to the receiving address");
std::process::exit(0);
}

Expand All @@ -92,5 +95,73 @@ fn main() -> Result<(), anyhow::Error> {
client.transaction_broadcast(&tx)?;
println!("Tx broadcasted! Txid: {}", tx.compute_txid());

let unconfirmed_txids: HashSet<Txid> = wallet
.transactions()
.filter(|tx| tx.chain_position.is_unconfirmed())
.map(|tx| tx.tx_node.txid)
.collect();

client.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));

println!("\n=== Performing Partial Sync ===\n");
print!("SCANNING: ");
let mut last_printed = 0;
let sync_request = wallet
.start_sync_with_revealed_spks()
.inspect(move |_, sync_progress| {
let progress_percent =
(100 * sync_progress.consumed()) as f32 / sync_progress.total() as f32;
let progress_percent = progress_percent.round() as u32;
if progress_percent.is_multiple_of(5) && progress_percent > last_printed {
print!("{progress_percent}% ");
std::io::stdout().flush().expect("must flush");
last_printed = progress_percent;
}
});
client.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));
let sync_update = client.sync(sync_request, BATCH_SIZE, false)?;
println!();

let mut evicted_txs = Vec::new();
for txid in unconfirmed_txids {
let tx_node = wallet
.tx_graph()
.full_txs()
.find(|full_tx| full_tx.txid == txid);
let wallet_tx = wallet.get_tx(txid);

let is_evicted = match wallet_tx {
Some(wallet_tx) => {
!wallet_tx.chain_position.is_unconfirmed()
&& !wallet_tx.chain_position.is_confirmed()
}
None => true,
};

if is_evicted {
if let Some(full_tx) = tx_node {
evicted_txs.push((full_tx.txid, full_tx.last_seen.unwrap_or(0)));
} else {
evicted_txs.push((txid, 0));
}
}
}

if !evicted_txs.is_empty() {
wallet.apply_evicted_txs(evicted_txs.clone());
println!("Applied {} evicted transactions", evicted_txs.len());
}

wallet.apply_update(sync_update)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good idea to also persist here. wdyt ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, I will do that. Thank you!

wallet.persist(&mut db)?;

let balance_after_sync = wallet.balance();
println!("Wallet balance after sync: {}", balance_after_sync.total());
println!(
"Wallet has {} transactions and {} utxos after partial sync",
wallet.transactions().count(),
wallet.list_unspent().count()
);

Ok(())
}
92 changes: 81 additions & 11 deletions examples/example_wallet_esplora_async/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::{collections::BTreeSet, io::Write};

use anyhow::Ok;
use bdk_esplora::{esplora_client, EsploraAsyncExt};
use bdk_wallet::{
bitcoin::{Amount, Network},
bitcoin::{Amount, Network, Txid},
rusqlite::Connection,
KeychainKind, SignOptions, Wallet,
};
use std::{
collections::{BTreeSet, HashSet},
io::Write,
};

const SEND_AMOUNT: Amount = Amount::from_sat(5000);
const STOP_GAP: usize = 5;
Expand Down Expand Up @@ -42,17 +44,17 @@ async fn main() -> Result<(), anyhow::Error> {
let balance = wallet.balance();
println!("Wallet balance before syncing: {}", balance.total());

print!("Syncing...");
println!("=== Performing Full Sync ===");
let client = esplora_client::Builder::new(ESPLORA_URL).build_async()?;

let request = wallet.start_full_scan().inspect({
let mut stdout = std::io::stdout();
let mut once = BTreeSet::<KeychainKind>::new();
move |keychain, spk_i, _| {
if once.insert(keychain) {
print!("\nScanning keychain [{:?}]", keychain);
print!("\nScanning keychain [{keychain:?}]");
}
print!(" {:<3}", spk_i);
print!(" {spk_i:<3}");
stdout.flush().expect("must flush")
}
});
Expand All @@ -66,13 +68,15 @@ async fn main() -> Result<(), anyhow::Error> {
println!();

let balance = wallet.balance();
println!("Wallet balance after syncing: {}", balance.total());
println!("Wallet balance after full sync: {}", balance.total());
println!(
"Wallet has {} transactions and {} utxos after full sync",
wallet.transactions().count(),
wallet.list_unspent().count()
);

if balance.total() < SEND_AMOUNT {
println!(
"Please send at least {} to the receiving address",
SEND_AMOUNT
);
println!("Please send at least {SEND_AMOUNT} to the receiving address");
std::process::exit(0);
}

Expand All @@ -87,5 +91,71 @@ async fn main() -> Result<(), anyhow::Error> {
client.broadcast(&tx).await?;
println!("Tx broadcasted! Txid: {}", tx.compute_txid());

let unconfirmed_txids: HashSet<Txid> = wallet
.transactions()
.filter(|tx| tx.chain_position.is_unconfirmed())
.map(|tx| tx.tx_node.txid)
.collect();

println!("\n=== Performing Partial Sync ===\n");
print!("SCANNING: ");
let mut printed = 0;
let sync_request = wallet
.start_sync_with_revealed_spks()
.inspect(move |_, sync_progress| {
let progress_percent =
(100 * sync_progress.consumed()) as f32 / sync_progress.total() as f32;
let progress_percent = progress_percent.round() as u32;
if progress_percent.is_multiple_of(5) && progress_percent > printed {
print!("{progress_percent}% ");
std::io::stdout().flush().expect("must flush");
printed = progress_percent;
}
});
let sync_update = client.sync(sync_request, PARALLEL_REQUESTS).await?;
println!();

let mut evicted_txs = Vec::new();
for txid in unconfirmed_txids {
let tx_node = wallet
.tx_graph()
.full_txs()
.find(|full_tx| full_tx.txid == txid);
let wallet_tx = wallet.get_tx(txid);

let is_evicted = match wallet_tx {
Some(wallet_tx) => {
!wallet_tx.chain_position.is_unconfirmed()
&& !wallet_tx.chain_position.is_confirmed()
}
None => true,
};

if is_evicted {
if let Some(full_tx) = tx_node {
evicted_txs.push((full_tx.txid, full_tx.last_seen.unwrap_or(0)));
} else {
evicted_txs.push((txid, 0));
}
}
}

if !evicted_txs.is_empty() {
let evicted_count = evicted_txs.len();
wallet.apply_evicted_txs(evicted_txs);
println!("Applied {evicted_count} evicted transactions");
}

wallet.apply_update(sync_update)?;
wallet.persist(&mut conn)?;

let balance_after_sync = wallet.balance();
println!("Wallet balance after sync: {}", balance_after_sync.total());
println!(
"Wallet has {} transactions and {} utxos after partial sync",
wallet.transactions().count(),
wallet.list_unspent().count()
);

Ok(())
}
83 changes: 74 additions & 9 deletions examples/example_wallet_esplora_blocking/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{collections::BTreeSet, io::Write};

use bdk_esplora::{esplora_client, EsploraExt};
use bdk_wallet::{
bitcoin::{Amount, Network},
bitcoin::{Amount, Network, Txid},
file_store::Store,
KeychainKind, SignOptions, Wallet,
};
use std::{
collections::{BTreeSet, HashSet},
io::Write,
};

const DB_MAGIC: &str = "bdk_wallet_esplora_example";
const DB_PATH: &str = "bdk-example-esplora-blocking.db";
Expand Down Expand Up @@ -52,9 +54,9 @@ fn main() -> Result<(), anyhow::Error> {
let mut once = BTreeSet::<KeychainKind>::new();
move |keychain, spk_i, _| {
if once.insert(keychain) {
print!("\nScanning keychain [{:?}] ", keychain);
print!("\nScanning keychain [{keychain:?}] ");
}
print!(" {:<3}", spk_i);
print!(" {spk_i:<3}");
stdout.flush().expect("must flush")
}
});
Expand All @@ -69,10 +71,7 @@ fn main() -> Result<(), anyhow::Error> {
println!("Wallet balance after syncing: {}", balance.total());

if balance.total() < SEND_AMOUNT {
println!(
"Please send at least {} to the receiving address",
SEND_AMOUNT
);
println!("Please send at least {SEND_AMOUNT} to the receiving address");
std::process::exit(0);
}

Expand All @@ -87,5 +86,71 @@ fn main() -> Result<(), anyhow::Error> {
client.broadcast(&tx)?;
println!("Tx broadcasted! Txid: {}", tx.compute_txid());

let unconfirmed_txids: HashSet<Txid> = wallet
.transactions()
.filter(|tx| tx.chain_position.is_unconfirmed())
.map(|tx| tx.tx_node.txid)
.collect();

println!("\n=== Performing Partial Sync ===\n");
print!("SCANNING: ");
let mut printed = 0;
let sync_request = wallet
.start_sync_with_revealed_spks()
.inspect(move |_, sync_progress| {
let progress_percent =
(100 * sync_progress.consumed()) as f32 / sync_progress.total() as f32;
let progress_percent = progress_percent.round() as u32;
if progress_percent.is_multiple_of(5) && progress_percent > printed {
print!("{progress_percent}% ");
std::io::stdout().flush().expect("must flush");
printed = progress_percent;
}
});
let sync_update = client.sync(sync_request, PARALLEL_REQUESTS)?;
println!();

let mut evicted_txs = Vec::new();
for txid in unconfirmed_txids {
let tx_node = wallet
.tx_graph()
.full_txs()
.find(|full_tx| full_tx.txid == txid);
let wallet_tx = wallet.get_tx(txid);

let is_evicted = match wallet_tx {
Some(wallet_tx) => {
!wallet_tx.chain_position.is_unconfirmed()
&& !wallet_tx.chain_position.is_confirmed()
}
None => true,
};

if is_evicted {
if let Some(full_tx) = tx_node {
evicted_txs.push((full_tx.txid, full_tx.last_seen.unwrap_or(0)));
} else {
evicted_txs.push((txid, 0));
}
}
}

if !evicted_txs.is_empty() {
let evicted_count = evicted_txs.len();
wallet.apply_evicted_txs(evicted_txs);
println!("Applied {evicted_count} evicted transactions");
}

wallet.apply_update(sync_update)?;
wallet.persist(&mut db)?;

let balance_after_sync = wallet.balance();
println!("Wallet balance after sync: {}", balance_after_sync.total());
println!(
"Wallet has {} transactions and {} utxos after partial sync",
wallet.transactions().count(),
wallet.list_unspent().count()
);

Ok(())
}
5 changes: 1 addition & 4 deletions examples/example_wallet_rpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ fn main() -> anyhow::Result<()> {
wallet.apply_block_connected_to(&block_emission.block, height, connected_to)?;
wallet.persist(&mut db)?;
let elapsed = start_apply_block.elapsed().as_secs_f32();
println!(
"Applied block {} at height {} in {}s",
hash, height, elapsed
);
println!("Applied block {hash} at height {height} in {elapsed}s",);
}
Emission::Mempool(event) => {
let start_apply_mempool = Instant::now();
Expand Down
Loading