Using tracing and simplifying a lot of console output in Bitcoin driver

This commit is contained in:
Dave Hrycyszyn
2024-07-24 17:33:40 +01:00
parent b78aadabff
commit 479abcbba2
4 changed files with 66 additions and 68 deletions

View File

@@ -52,42 +52,8 @@ impl EsploraWallet {
/// Syncs the wallet with the latest state of the Bitcoin blockchain
pub(crate) async fn sync(&mut self) -> Result<(), anyhow::Error> {
print!("Syncing...");
fn generate_inspect(
kind: KeychainKind,
) -> impl FnMut(u32, &Script) + Send + Sync + 'static {
let mut once = Some(());
let mut stdout = std::io::stdout();
move |spk_i, _| {
match once.take() {
Some(_) => print!("\nScanning keychain [{:?}]", kind),
None => print!(" {:<3}", spk_i),
};
stdout.flush().expect("must flush");
}
}
let request = self
.wallet
.start_full_scan()
.inspect_spks_for_all_keychains({
let mut once = BTreeSet::<KeychainKind>::new();
move |keychain, spk_i, _| {
match once.insert(keychain) {
true => print!("\nScanning keychain [{:?}]", keychain),
false => print!(" {:<3}", spk_i),
}
std::io::stdout().flush().expect("must flush")
}
})
.inspect_spks_for_keychain(
KeychainKind::External,
generate_inspect(KeychainKind::External),
)
.inspect_spks_for_keychain(
KeychainKind::Internal,
generate_inspect(KeychainKind::Internal),
);
tracing::info!("{} full scan sync start", self.name);
let request = self.wallet.start_full_scan();
let mut update = self
.client
.full_scan(request, STOP_GAP, PARALLEL_REQUESTS)
@@ -96,7 +62,7 @@ impl EsploraWallet {
let _ = update.graph_update.update_last_seen_unconfirmed(now);
self.wallet.apply_update(update)?;
self.persist_local()?;
println!("Sync complete for {}", self.name);
tracing::info!("{} sync complete", self.name);
Ok(())
}
@@ -110,7 +76,7 @@ impl EsploraWallet {
pub(crate) fn next_unused_address(&mut self) -> Result<AddressInfo, anyhow::Error> {
let address = self.wallet.next_unused_address(KeychainKind::External);
self.persist_local()?;
println!(
tracing::info!(
"Generated address: https://mutinynet.com/address/{}",
address
);
@@ -119,6 +85,11 @@ impl EsploraWallet {
/// Returns the balance of the wallet.
pub(crate) fn balance(&self) -> bdk_wallet::wallet::Balance {
tracing::info!(
"{}'s balance is {}",
self.name,
self.wallet.balance().total()
);
self.wallet.balance()
}
@@ -127,7 +98,7 @@ impl EsploraWallet {
&self,
tx: &bitcoin::Transaction,
) -> Result<(), esplora_client::Error> {
println!(
tracing::info!(
"{} broadcasting tx https://mutinynet.com/tx/{}",
self.name,
tx.compute_txid()
@@ -143,7 +114,7 @@ pub(crate) fn create_wallet(name: &str, network: Network) -> anyhow::Result<Espl
let mnemonic_path = crate::utils::side_paths(keys_dir).1; // TODO: this tuple stinks
let mnemonic_words = fs::read_to_string(mnemonic_path).expect("couldn't read bitcoin key file");
println!("Creating wallet from mnemonic: {mnemonic_words}");
tracing::info!("Creating {name}'s wallet from mnemonic: {mnemonic_words}");
let mnemonic = Mnemonic::parse(mnemonic_words).unwrap();
// Generate the extended key
@@ -156,7 +127,7 @@ pub(crate) fn create_wallet(name: &str, network: Network) -> anyhow::Result<Espl
.expect("problem converting xkey to xprv")
.to_string();
println!("Setting up esplora database for {name}");
tracing::info!("Setting up esplora database for {name}");
let db_path = format!("/tmp/{name}-bdk-esplora-async-example.sqlite");
let conn = Connection::open(db_path)?;