From: Alekos Filini Date: Thu, 7 May 2020 15:36:45 +0000 (+0200) Subject: [blockchain] Use async I/O in the various blockchain impls X-Git-Tag: 0.1.0-beta.1~29 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/struct.EncoderStringWriter.html?a=commitdiff_plain;h=41127592d981a4e6a483325e9b32046409a917ea;p=bdk-cli [blockchain] Use async I/O in the various blockchain impls --- diff --git a/Cargo.toml b/Cargo.toml index 2ef3a52..7bb030e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "magical-bitcoin-wallet" version = "0.1.0" +edition = "2018" authors = ["Riccardo Casatta ", "Alekos Filini "] [dependencies] @@ -10,21 +11,24 @@ miniscript = { version = "0.12" } serde = { version = "^1.0", features = ["derive"] } serde_json = { version = "^1.0" } base64 = "^0.11" +async-trait = "0.1" # Optional dependencies sled = { version = "0.31.0", optional = true } -electrum-client = { version = "0.1.0-beta.5", optional = true } -reqwest = { version = "0.10", optional = true, features = ["blocking", "json"] } +electrum-client = { git = "https://github.com/MagicalBitcoin/rust-electrum-client.git", optional = true } +reqwest = { version = "0.10", optional = true, features = ["json"] } +futures = { version = "0.3", optional = true } [features] minimal = [] compiler = ["miniscript/compiler"] default = ["key-value-db", "electrum"] electrum = ["electrum-client"] -esplora = ["reqwest"] +esplora = ["reqwest", "futures"] key-value-db = ["sled"] [dev-dependencies] +tokio = { version = "0.2", features = ["macros"] } lazy_static = "1.4" rustyline = "5.0" # newer version requires 2018 edition clap = "2.33" diff --git a/examples/repl.rs b/examples/repl.rs index 13ee095..74d366e 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -9,6 +9,7 @@ extern crate rustyline; use std::fs; use std::path::PathBuf; use std::str::FromStr; +use std::sync::Arc; use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; @@ -72,7 +73,8 @@ fn outpoint_validator(s: String) -> Result<(), String> { parse_outpoint(&s).map(|_| ()) } -fn main() { +#[tokio::main] +async fn main() { env_logger::init(); let app = App::new("Magical Bitcoin Wallet") @@ -264,7 +266,9 @@ fn main() { .unwrap(); debug!("database opened successfully"); - let client = Client::new(matches.value_of("server").unwrap()).unwrap(); + let client = Client::new(matches.value_of("server").unwrap()) + .await + .unwrap(); let wallet = Wallet::new( descriptor, change_descriptor, @@ -272,14 +276,20 @@ fn main() { tree, ElectrumBlockchain::from(client), ) + .await .unwrap(); + let wallet = Arc::new(wallet); // TODO: print errors in a nice way - let handle_matches = |matches: ArgMatches<'_>| { + async fn handle_matches(wallet: Arc>, matches: ArgMatches<'_>) + where + C: magical_bitcoin_wallet::blockchain::OnlineBlockchain, + D: magical_bitcoin_wallet::database::BatchDatabase, + { if let Some(_sub_matches) = matches.subcommand_matches("get_new_address") { println!("{}", wallet.get_new_address().unwrap().to_string()); } else if let Some(_sub_matches) = matches.subcommand_matches("sync") { - wallet.sync(None, None).unwrap(); + wallet.sync(None, None).await.unwrap(); } else if let Some(_sub_matches) = matches.subcommand_matches("list_unspent") { for utxo in wallet.list_unspent().unwrap() { println!("{} value {} SAT", utxo.outpoint, utxo.txout.value); @@ -344,7 +354,7 @@ fn main() { } else if let Some(sub_matches) = matches.subcommand_matches("broadcast") { let psbt = base64::decode(sub_matches.value_of("psbt").unwrap()).unwrap(); let psbt: PartiallySignedTransaction = deserialize(&psbt).unwrap(); - let (txid, _) = wallet.broadcast(psbt).unwrap(); + let (txid, _) = wallet.broadcast(psbt).await.unwrap(); println!("TXID: {}", txid); } @@ -372,7 +382,7 @@ fn main() { continue; } - handle_matches(matches.unwrap()); + handle_matches(Arc::clone(&wallet), matches.unwrap()).await; } Err(ReadlineError::Interrupted) => continue, Err(ReadlineError::Eof) => break, @@ -385,6 +395,6 @@ fn main() { // rl.save_history("history.txt").unwrap(); } else { - handle_matches(matches); + handle_matches(wallet, matches).await; } }