From 9b369a042c7421c52faaa5d48f62c77f1a196067 Mon Sep 17 00:00:00 2001 From: binarybaron Date: Sun, 11 Aug 2024 23:55:52 +0200 Subject: [PATCH] wip: Tauri event system --- src-tauri/src/lib.rs | 20 +--- swap/Cargo.toml | 228 +++++++++++++++++++++---------------------- swap/src/api.rs | 42 ++++---- 3 files changed, 142 insertions(+), 148 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 2655e3eb..01e3d1d9 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -11,7 +11,7 @@ use swap::{ }, cli::command::{Bitcoin, Monero}, }; -use tauri::{Manager, RunEvent, State}; +use tauri::{Emitter, Manager, State}; trait ToStringResult { fn to_string_result(self) -> Result; @@ -89,8 +89,8 @@ fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box> true, None, ) - .await - .unwrap(); + .await? + .with_tauri_handle(app.app_handle().to_owned()); app.manage(Arc::new(context)); }); @@ -107,16 +107,6 @@ pub fn run() { withdraw_btc ]) .setup(setup) - .build(tauri::generate_context!()) - .expect("error while building tauri application") - .run(|app, event| match event { - RunEvent::Exit | RunEvent::ExitRequested { .. } => { - let context = app.state::>().inner(); - - if let Err(err) = context.cleanup() { - println!("Cleanup failed {}", err); - } - } - _ => {} - }) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); } diff --git a/swap/Cargo.toml b/swap/Cargo.toml index f30af3a5..2a8fb66e 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "swap" version = "0.13.2" -authors = [ "The COMIT guys " ] +authors = ["The COMIT guys "] edition = "2021" description = "XMR/BTC trustless atomic swaps." @@ -9,118 +9,7 @@ description = "XMR/BTC trustless atomic swaps." name = "swap" [dependencies] -anyhow = "1" -async-compression = { version = "0.3", features = [ "bzip2", "tokio" ] } -async-trait = "0.1" -atty = "0.2" -backoff = { version = "0.4", features = [ "tokio" ] } -base64 = "0.22" -bdk = "0.28" -big-bytes = "1" -bitcoin = { version = "0.29", features = [ "rand", "serde" ] } -bmrng = "0.5" -comfy-table = "7.1" -config = { version = "0.14", default-features = false, features = [ "toml" ] } -conquer-once = "0.4" -curve25519-dalek = { package = "curve25519-dalek-ng", version = "4" } -data-encoding = "2.6" -dialoguer = "0.11" -digest = "0.10.7" -directories-next = "2" -ecdsa_fun = { version = "0.10", default-features = false, features = [ - "libsecp_compat", - "serde", - "adaptor", -] } -ed25519-dalek = "1" -erased-serde = "0.4.5" -futures = { version = "0.3", default-features = false } -hex = "0.4" -hyper = "0.14.20" -itertools = "0.13" -jsonrpsee = { version = "0.16.2", features = [ "server" ] } -jsonrpsee-core = "0.16.2" -libp2p = { version = "0.42.2", default-features = false, features = [ - "tcp-tokio", - "yamux", - "mplex", - "dns-tokio", - "noise", - "request-response", - "websocket", - "ping", - "rendezvous", - "identify", -] } -monero = { version = "0.12", features = [ "serde_support" ] } -monero-rpc = { path = "../monero-rpc" } -pem = "3.0" -proptest = "1" -qrcode = "0.14" -rand = "0.8" -rand_chacha = "0.3" -reqwest = { version = "0.12", features = [ - "http2", - "rustls-tls", - "stream", - "socks", -], default-features = false } -rust_decimal = { version = "1", features = [ "serde-float" ] } -rust_decimal_macros = "1" -serde = { version = "1", features = [ "derive" ] } -serde_cbor = "0.11" -serde_json = "1" -serde_with = { version = "1", features = [ "macros" ] } -sha2 = "0.10" -sigma_fun = { version = "0.7", default-features = false, features = [ - "ed25519", - "serde", - "secp256k1", - "alloc", -] } -sqlx = { version = "0.6.3", features = [ - "sqlite", - "runtime-tokio-rustls", - "offline", -] } -structopt = "0.3" -strum = { version = "0.26", features = [ "derive" ] } -thiserror = "1" -time = "0.3" -tokio = { version = "1", features = [ - "rt-multi-thread", - "time", - "macros", - "sync", - "process", - "fs", - "net", - "parking_lot", -] } -tokio-socks = "0.5" -tokio-tungstenite = { version = "0.15", features = [ "rustls-tls" ] } -tokio-util = { version = "0.7", features = [ "io", "codec" ] } -toml = "0.8" -torut = { version = "0.2", default-features = false, features = [ - "v3", - "control", -] } -tower = { version = "0.4.13", features = [ "full" ] } -tower-http = { version = "0.3.4", features = [ "full" ] } -tracing = { version = "0.1", features = [ "attributes" ] } -tracing-appender = "0.2" -tracing-futures = { version = "0.2", features = [ "std-future", "futures-03" ] } -tracing-subscriber = { version = "0.3", default-features = false, features = [ - "fmt", - "ansi", - "env-filter", - "time", - "tracing-log", - "json", -] } -url = { version = "2", features = [ "serde" ] } -uuid = { version = "1.9", features = [ "serde", "v4" ] } -void = "1" +tauri = { version = "2.0.0-rc.1" } [target.'cfg(not(windows))'.dependencies] tokio-tar = "0.3" @@ -132,7 +21,7 @@ zip = "0.5" bitcoin-harness = { git = "https://github.com/delta1/bitcoin-harness-rs.git", rev = "80cc8d05db2610d8531011be505b7bee2b5cdf9f" } get-port = "3" hyper = "1.3" -jsonrpsee = { version = "0.16.2", features = [ "ws-client" ] } +jsonrpsee = { version = "0.16.2", features = ["ws-client"] } mockito = "1.4" monero-harness = { path = "../monero-harness" } port_check = "0.2" @@ -150,3 +39,114 @@ vergen = { version = "8.3", default-features = false, features = [ "git", "git2", ] } +async-compression = { version = "0.3", features = ["bzip2", "tokio"] } +async-trait = "0.1" +atty = "0.2" +backoff = { version = "0.4", features = ["tokio"] } +base64 = "0.22" +bdk = "0.28" +big-bytes = "1" +bitcoin = { version = "0.29", features = ["rand", "serde"] } +bmrng = "0.5" +comfy-table = "7.1" +config = { version = "0.14", default-features = false, features = ["toml"] } +conquer-once = "0.4" +curve25519-dalek = { package = "curve25519-dalek-ng", version = "4" } +data-encoding = "2.6" +dialoguer = "0.11" +digest = "0.10.7" +directories-next = "2" +ecdsa_fun = { version = "0.10", default-features = false, features = [ + "libsecp_compat", + "serde", + "adaptor", +] } +ed25519-dalek = "1" +erased-serde = "0.4.5" +futures = { version = "0.3", default-features = false } +hex = "0.4" +hyper = "0.14.20" +itertools = "0.13" +jsonrpsee = { version = "0.16.2", features = ["server"] } +jsonrpsee-core = "0.16.2" +libp2p = { version = "0.42.2", default-features = false, features = [ + "tcp-tokio", + "yamux", + "mplex", + "dns-tokio", + "noise", + "request-response", + "websocket", + "ping", + "rendezvous", + "identify", +] } +monero = { version = "0.12", features = ["serde_support"] } +monero-rpc = { path = "../monero-rpc" } +pem = "3.0" +proptest = "1" +qrcode = "0.14" +rand = "0.8" +rand_chacha = "0.3" +reqwest = { version = "0.12", features = [ + "http2", + "rustls-tls", + "stream", + "socks", +], default-features = false } +rust_decimal = { version = "1", features = ["serde-float"] } +rust_decimal_macros = "1" +serde = { version = "1", features = ["derive"] } +serde_cbor = "0.11" +serde_json = "1" +serde_with = { version = "1", features = ["macros"] } +sha2 = "0.10" +sigma_fun = { version = "0.7", default-features = false, features = [ + "ed25519", + "serde", + "secp256k1", + "alloc", +] } +sqlx = { version = "0.6.3", features = [ + "sqlite", + "runtime-tokio-rustls", + "offline", +] } +structopt = "0.3" +strum = { version = "0.26", features = ["derive"] } +thiserror = "1" +time = "0.3" +tokio = { version = "1", features = [ + "rt-multi-thread", + "time", + "macros", + "sync", + "process", + "fs", + "net", + "parking_lot", +] } +tokio-socks = "0.5" +tokio-tungstenite = { version = "0.15", features = ["rustls-tls"] } +tokio-util = { version = "0.7", features = ["io", "codec"] } +toml = "0.8" +torut = { version = "0.2", default-features = false, features = [ + "v3", + "control", +] } +tower = { version = "0.4.13", features = ["full"] } +tower-http = { version = "0.3.4", features = ["full"] } +tracing = { version = "0.1", features = ["attributes"] } +tracing-appender = "0.2" +tracing-futures = { version = "0.2", features = ["std-future", "futures-03"] } +tracing-subscriber = { version = "0.3", default-features = false, features = [ + "fmt", + "ansi", + "env-filter", + "time", + "tracing-log", + "json", +] } +url = { version = "2", features = ["serde"] } +uuid = { version = "1.9", features = ["serde", "v4"] } +void = "1" diff --git a/swap/src/api.rs b/swap/src/api.rs index 75427810..3eb6868e 100644 --- a/swap/src/api.rs +++ b/swap/src/api.rs @@ -7,14 +7,15 @@ use crate::network::rendezvous::XmrBtcNamespace; use crate::protocol::Database; use crate::seed::Seed; use crate::{bitcoin, cli, monero}; -use anyhow::{anyhow, bail, Context as AnyContext, Error, Result}; +use anyhow::{bail, Context as AnyContext, Error, Result}; use futures::future::try_join_all; use std::fmt; use std::future::Future; use std::net::SocketAddr; use std::path::PathBuf; -use std::sync::{Arc, Mutex, Once}; -use tokio::sync::{broadcast, broadcast::Sender, Mutex as TokioMutex, RwLock}; +use std::sync::{Arc, Once}; +use tauri::AppHandle; +use tokio::sync::{broadcast, broadcast::Sender, Mutex, RwLock}; use tokio::task::JoinHandle; use url::Url; @@ -36,7 +37,7 @@ pub struct Config { use uuid::Uuid; #[derive(Default)] -pub struct PendingTaskList(TokioMutex>>); +pub struct PendingTaskList(Mutex>>); impl PendingTaskList { pub async fn spawn(&self, future: F) @@ -164,10 +165,11 @@ pub struct Context { pub db: Arc, bitcoin_wallet: Option>, monero_wallet: Option>, - monero_rpc_process: Option>>, + monero_rpc_process: Option>, pub swap_lock: Arc, pub config: Config, pub tasks: Arc, + tauri_handle: Option>, } #[allow(clippy::too_many_arguments)] @@ -228,7 +230,7 @@ impl Context { db: open_db(data_dir.join("sqlite")).await?, bitcoin_wallet, monero_wallet, - monero_rpc_process: monero_rpc_process.map(|prc| Arc::new(Mutex::new(prc))), + monero_rpc_process: monero_rpc_process.map(Arc::new), config: Config { tor_socks5_port, namespace: XmrBtcNamespace::from_is_testnet(is_testnet), @@ -242,11 +244,25 @@ impl Context { }, swap_lock: Arc::new(SwapLock::new()), tasks: Arc::new(PendingTaskList::default()), + tauri_handle: None, }; Ok(context) } + pub fn with_tauri_handle(mut self, tauri_handle: Arc) -> Self { + self.tauri_handle = Some(tauri_handle); + self + } + + pub fn emit_tauri_event(mut self, event: &str, payload: S) -> Result<()> { + self.tauri_handle + .ok_or_else(|| anyhow!("Tauri handle not set"))? + .emit(event, payload)?; + + Ok(()) + } + pub async fn for_harness( seed: Seed, env_config: EnvConfig, @@ -266,21 +282,9 @@ impl Context { monero_rpc_process: None, swap_lock: Arc::new(SwapLock::new()), tasks: Arc::new(PendingTaskList::default()), + tauri_handle: None, } } - - pub fn cleanup(&self) -> Result<()> { - if let Some(ref monero_rpc_process) = self.monero_rpc_process { - let mut process = monero_rpc_process - .lock() - .map_err(|_| anyhow!("Failed to lock monero_rpc_process for cleanup"))?; - - process.kill()?; - println!("Killed monero-wallet-rpc process"); - } - - Ok(()) - } } impl fmt::Debug for Context {