Using tracing and simplifying a lot of console output in Bitcoin driver
This commit is contained in:
35
Cargo.lock
generated
35
Cargo.lock
generated
@@ -2103,6 +2103,15 @@ version = "0.4.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
|
||||
|
||||
[[package]]
|
||||
name = "matchers"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
|
||||
dependencies = [
|
||||
"regex-automata 0.1.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
@@ -2769,8 +2778,17 @@ checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
"regex-automata 0.4.7",
|
||||
"regex-syntax 0.8.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
|
||||
dependencies = [
|
||||
"regex-syntax 0.6.29",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2781,9 +2799,15 @@ checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
"regex-syntax 0.8.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.4"
|
||||
@@ -3359,6 +3383,7 @@ dependencies = [
|
||||
"tokio",
|
||||
"toml",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
@@ -3858,10 +3883,14 @@ version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
|
||||
dependencies = [
|
||||
"matchers",
|
||||
"nu-ansi-term",
|
||||
"once_cell",
|
||||
"regex",
|
||||
"sharded-slab",
|
||||
"smallvec",
|
||||
"thread_local",
|
||||
"tracing",
|
||||
"tracing-core",
|
||||
"tracing-log",
|
||||
]
|
||||
|
||||
@@ -28,8 +28,8 @@ serde_json = "1.0.117"
|
||||
sha256 = "1.5.0"
|
||||
tokio = { version = "1.37.0", features = ["full"] }
|
||||
toml = "0.8.14"
|
||||
tracing = "0.1.32"
|
||||
# tracing-subscriber = "0.3.9"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = {version = "0.3", features = ["std", "env-filter"]}
|
||||
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -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)?;
|
||||
|
||||
@@ -1,43 +1,32 @@
|
||||
use bdk_wallet::bitcoin::{Amount, Network};
|
||||
|
||||
use crate::bitcoin::clients;
|
||||
use bdk_wallet::bitcoin::{Amount, Network};
|
||||
use tracing_subscriber::filter::EnvFilter;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
use tracing_subscriber::{fmt, layer::SubscriberExt};
|
||||
|
||||
pub(crate) async fn simple_transfer() -> Result<(), anyhow::Error> {
|
||||
tracing_setup();
|
||||
|
||||
let mut dave = clients::esplora::create_wallet("dave", Network::Signet)?;
|
||||
let mut sammy = clients::esplora::create_wallet("sammy", Network::Signet)?;
|
||||
|
||||
let _next_address = dave.next_unused_address()?;
|
||||
|
||||
let dave_balance = dave.balance();
|
||||
println!(
|
||||
"Dave wallet balance before syncing: {} sats",
|
||||
dave_balance.total()
|
||||
);
|
||||
let _ = dave.balance();
|
||||
|
||||
dave.sync().await?;
|
||||
|
||||
let dave_balance = dave.balance();
|
||||
println!("Wallet balance after syncing: {} sats", dave_balance);
|
||||
|
||||
let sammy_address = sammy.next_unused_address()?.address;
|
||||
println!("Sammy's address: {}", sammy_address);
|
||||
|
||||
let sammy_balance = sammy.balance();
|
||||
println!(
|
||||
"Sammy wallet balance before syncing: {} sats",
|
||||
sammy_balance
|
||||
);
|
||||
|
||||
let _sammy = sammy.balance();
|
||||
sammy.sync().await?;
|
||||
|
||||
let sammy_balance = sammy.balance();
|
||||
println!("Sammy wallet balance after syncing: {} sats", sammy_balance);
|
||||
let _sammy = sammy.balance();
|
||||
|
||||
let send_amount = Amount::from_sat(500);
|
||||
|
||||
if dave_balance.total() < send_amount {
|
||||
println!(
|
||||
"Please send at least {} sats to the receiving address",
|
||||
tracing::error!(
|
||||
"Please send at least {} sats to the receiving address. Exiting.",
|
||||
send_amount
|
||||
);
|
||||
std::process::exit(0);
|
||||
@@ -58,3 +47,12 @@ pub(crate) async fn htlc() -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn tracing_setup() {
|
||||
tracing_subscriber::registry()
|
||||
.with(fmt::layer())
|
||||
.with(EnvFilter::from_default_env())
|
||||
.init();
|
||||
|
||||
tracing::info!("Tracing initialized.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user