From 2d8b1faadf9627a5aa55407bdd3dd618c0427737 Mon Sep 17 00:00:00 2001 From: Daniela Brozzoni Date: Wed, 24 Aug 2022 23:06:53 +0100 Subject: [PATCH] Use async with esplora-reqwest We previously had the esplora-reqwest feature, but it would use sync reqwest, as the "async-interface" feature in BDK wasn't set. This commit sets this feature so that using `esplora-reqwest` always uses async mode. --- CHANGELOG.md | 1 + Cargo.lock | 24 ++++++++++++++++++++++++ Cargo.toml | 6 ++++-- src/handlers.rs | 13 +++++++++---- src/main.rs | 5 ++++- 5 files changed, 42 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41f1481..a28e5ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Rewrite relevant doc comments as `structopt` help document. - Update `bdk` and `bdk-reserves` to v0.19.0. - Change default database to `sqlite`. +- Change the `esplora-reqwest` feature to always use async mode ## [0.5.0] diff --git a/Cargo.lock b/Cargo.lock index 49c1c25..bc089ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -149,6 +149,7 @@ dependencies = [ "rustyline", "serde_json", "structopt", + "tokio", "zeroize", ] @@ -1227,6 +1228,16 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "once_cell" version = "1.12.0" @@ -2151,12 +2162,25 @@ dependencies = [ "libc", "memchr", "mio", + "num_cpus", "once_cell", "pin-project-lite", "socket2", + "tokio-macros", "winapi", ] +[[package]] +name = "tokio-macros" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio-socks" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index 8ec2599..2a9e75d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ fd-lock = { version = "=3.0.2", optional = true } regex = { version = "1", optional = true } bdk-reserves = { version = "0.19", optional = true} electrsd = { version= "0.12", features = ["trigger", "bitcoind_22_0"], optional = true} +tokio = { version = "1", features = ["rt", "macros", "rt-multi-thread"] } [features] default = ["repl", "sqlite-db"] @@ -44,7 +45,8 @@ electrum = ["bdk/electrum"] compact_filters = ["bdk/compact_filters"] esplora = [] esplora-ureq = ["esplora", "bdk/use-esplora-ureq"] -esplora-reqwest = ["esplora", "bdk/use-esplora-reqwest"] +async-interface = ["bdk/async-interface"] +esplora-reqwest = ["esplora", "bdk/use-esplora-reqwest", "async-interface"] # Use this to consensus verify transactions at sync time verify = ["bdk/verify"] @@ -65,4 +67,4 @@ regtest-node = [] regtest-bitcoin = ["regtest-node" , "rpc", "electrsd"] regtest-electrum = ["regtest-node", "electrum", "electrsd/electrs_0_8_10"] regtest-esplora-ureq = ["regtest-node", "esplora-ureq", "electrsd/esplora_a33e97e1"] -regtest-esplora-reqwest = ["regtest-node", "esplora-reqwest", "electrsd/esplora_a33e97e1"] \ No newline at end of file +regtest-esplora-reqwest = ["regtest-node", "esplora-reqwest", "electrsd/esplora_a33e97e1"] diff --git a/src/handlers.rs b/src/handlers.rs index 3806b03..aa25b5e 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -65,6 +65,7 @@ use bdk::{ bitcoin::{Address, OutPoint, TxOut}, blockchain::Capability, }; +use bdk_macros::maybe_async; #[cfg(any( feature = "electrum", feature = "esplora", @@ -72,7 +73,6 @@ use bdk::{ feature = "rpc" ))] use bdk_macros::maybe_await; -use bdk_macros::maybe_async; #[cfg(feature = "reserves")] use bdk_reserves::reserves::verify_proof; #[cfg(feature = "reserves")] @@ -557,6 +557,7 @@ pub fn get_outpoints_for_address( .collect() } +#[maybe_async] pub fn handle_command( cli_opts: CliOpts, network: Network, @@ -577,7 +578,11 @@ pub fn handle_command( let database = open_database(&wallet_opts)?; let blockchain = new_blockchain(network, &wallet_opts, &_backend)?; let wallet = new_wallet(network, &wallet_opts, database)?; - let result = handle_online_wallet_subcommand(&wallet, &blockchain, online_subcommand)?; + let result = maybe_await!(handle_online_wallet_subcommand( + &wallet, + &blockchain, + online_subcommand + ))?; serde_json::to_string_pretty(&result)? } CliSubCommand::Wallet { @@ -657,11 +662,11 @@ pub fn handle_command( ))] ReplSubCommand::OnlineWalletSubCommand(online_subcommand) => { let blockchain = new_blockchain(network, &wallet_opts, &_backend)?; - handle_online_wallet_subcommand( + maybe_await!(handle_online_wallet_subcommand( &wallet, &blockchain, online_subcommand, - ) + )) } ReplSubCommand::OfflineWalletSubCommand(offline_subcommand) => { handle_offline_wallet_subcommand( diff --git a/src/main.rs b/src/main.rs index edb3e4d..fced154 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,11 +24,14 @@ use log::{debug, error, warn}; use crate::commands::CliOpts; use crate::handlers::*; use bdk::{bitcoin, Error}; +use bdk_macros::{maybe_async, maybe_await}; use structopt::StructOpt; #[cfg(feature = "repl")] const REPL_LINE_SPLIT_REGEX: &str = r#""([^"]*)"|'([^']*)'|([\w\-]+)"#; +#[maybe_async] +#[cfg_attr(feature = "async-interface", tokio::main)] fn main() { env_logger::init(); @@ -96,7 +99,7 @@ fn main() { #[cfg(not(feature = "regtest-node"))] let backend = Nodes::None; - match handle_command(cli_opts, network, backend) { + match maybe_await!(handle_command(cli_opts, network, backend)) { Ok(result) => println!("{}", result), Err(e) => { match e {