diff --git a/Cargo.lock b/Cargo.lock index 1472fe53..ea46b0e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7586,7 +7586,6 @@ dependencies = [ "swap", "tauri", "tauri-build", - "uuid", ] [[package]] diff --git a/src-gui/src/renderer/rpc.ts b/src-gui/src/renderer/rpc.ts index 2497ad7f..1d814702 100644 --- a/src-gui/src/renderer/rpc.ts +++ b/src-gui/src/renderer/rpc.ts @@ -4,7 +4,7 @@ import { rpcSetBalance, rpcSetSwapInfo } from "store/features/rpcSlice"; export async function checkBitcoinBalance() { // TODO: use tauri-bindgen here - const response = (await invoke("balance")) as { + const response = (await invoke("get_balance")) as { balance: number; }; @@ -12,7 +12,7 @@ export async function checkBitcoinBalance() { } export async function getRawSwapInfos() { - const response = await invoke("swap_infos_all"); + const response = await invoke("get_swap_infos_all"); (response as any[]).forEach((info) => store.dispatch(rpcSetSwapInfo(info))); } diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 20b36575..26533b9f 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -21,4 +21,3 @@ serde = { version = "1", features = [ "derive" ] } serde_json = "1" swap = { path = "../swap" } tauri = { version = "2.0.0-rc.1", features = [ "config-json5" ] } -uuid = "1.10.0" diff --git a/swap/src/api/request.rs b/swap/src/api/request.rs index 03a491ea..aecb6a20 100644 --- a/swap/src/api/request.rs +++ b/swap/src/api/request.rs @@ -146,111 +146,8 @@ pub enum Method { GetRawStates, } -// TODO: Simplify this using instrument macro -impl Method { - fn get_tracing_span(&self, log_reference_id: Option) -> Span { - let span = match self { - Method::Balance { .. } => { - debug_span!( - "method", - method_name = "Balance", - log_reference_id = field::Empty - ) - } - Method::BuyXmr(BuyXmrArgs { swap_id, .. }) => { - debug_span!("method", method_name="BuyXmr", swap_id=%swap_id, log_reference_id=field::Empty) - } - Method::CancelAndRefund(CancelAndRefundArgs { swap_id }) => { - debug_span!("method", method_name="CancelAndRefund", swap_id=%swap_id, log_reference_id=field::Empty) - } - Method::Resume(ResumeArgs { swap_id }) => { - debug_span!("method", method_name="Resume", swap_id=%swap_id, log_reference_id=field::Empty) - } - Method::Config => { - debug_span!( - "method", - method_name = "Config", - log_reference_id = field::Empty - ) - } - Method::ExportBitcoinWallet => { - debug_span!( - "method", - method_name = "ExportBitcoinWallet", - log_reference_id = field::Empty - ) - } - Method::GetCurrentSwap => { - debug_span!( - "method", - method_name = "GetCurrentSwap", - log_reference_id = field::Empty - ) - } - Method::GetSwapInfo { .. } => { - debug_span!( - "method", - method_name = "GetSwapInfo", - log_reference_id = field::Empty - ) - } - Method::History => { - debug_span!( - "method", - method_name = "History", - log_reference_id = field::Empty - ) - } - Method::ListSellers { .. } => { - debug_span!( - "method", - method_name = "ListSellers", - log_reference_id = field::Empty - ) - } - Method::MoneroRecovery { .. } => { - debug_span!( - "method", - method_name = "MoneroRecovery", - log_reference_id = field::Empty - ) - } - Method::GetRawStates => debug_span!( - "method", - method_name = "RawHistory", - log_reference_id = field::Empty - ), - Method::StartDaemon { .. } => { - debug_span!( - "method", - method_name = "StartDaemon", - log_reference_id = field::Empty - ) - } - Method::SuspendCurrentSwap => { - debug_span!( - "method", - method_name = "SuspendCurrentSwap", - log_reference_id = field::Empty - ) - } - Method::WithdrawBtc { .. } => { - debug_span!( - "method", - method_name = "WithdrawBtc", - log_reference_id = field::Empty - ) - } - }; - if let Some(log_reference_id) = log_reference_id { - span.record("log_reference_id", log_reference_id.as_str()); - } - span - } -} - -// #[tracing::instrument(fields(method = "suspend_current_swap"), skip(context))] -async fn suspend_current_swap(context: Arc) -> Result { +#[tracing::instrument(fields(method = "suspend_current_swap"), skip(context))] +pub async fn suspend_current_swap(context: Arc) -> Result { let swap_id = context.swap_lock.get_current_swap_id().await; if let Some(id_value) = swap_id { @@ -262,6 +159,7 @@ async fn suspend_current_swap(context: Arc) -> Result) -> Result> { let swap_ids = context.db.all().await?; let mut swap_infos = Vec::new(); @@ -274,7 +172,7 @@ pub async fn get_swap_infos_all(context: Arc) -> Result, @@ -391,7 +289,8 @@ pub async fn get_swap_info( }) } -async fn buy_xmr(buy_xmr: BuyXmrArgs, context: Arc) -> Result { +#[tracing::instrument(fields(method = "buy_xmr"), skip(context))] +pub async fn buy_xmr(buy_xmr: BuyXmrArgs, context: Arc) -> Result { let BuyXmrArgs { seller, bitcoin_change_address, @@ -564,7 +463,8 @@ async fn buy_xmr(buy_xmr: BuyXmrArgs, context: Arc) -> Result) -> Result { +#[tracing::instrument(fields(method = "resume_swap"), skip(context))] +pub async fn resume_swap(resume: ResumeArgs, context: Arc) -> Result { let ResumeArgs { swap_id } = resume; context.swap_lock.acquire_swap_lock(swap_id).await?; @@ -671,7 +571,8 @@ async fn resume_swap(resume: ResumeArgs, context: Arc) -> Result, ) -> Result { @@ -699,7 +600,8 @@ async fn cancel_and_refund( }) } -async fn get_history(context: Arc) -> Result { +#[tracing::instrument(fields(method = "get_history"), skip(context))] +pub async fn get_history(context: Arc) -> Result { let swaps = context.db.all().await?; let mut vec: Vec<(Uuid, String)> = Vec::new(); for (swap_id, state) in swaps { @@ -710,13 +612,15 @@ async fn get_history(context: Arc) -> Result { Ok(GetHistoryResponse { swaps: vec }) } -async fn get_raw_states(context: Arc) -> Result { +#[tracing::instrument(fields(method = "get_raw_states"), skip(context))] +pub async fn get_raw_states(context: Arc) -> Result { let raw_history = context.db.raw_all().await?; Ok(json!({ "raw_states": raw_history })) } -async fn get_config(context: Arc) -> Result { +#[tracing::instrument(fields(method = "get_config"), skip(context))] +pub async fn get_config(context: Arc) -> Result { let data_dir_display = context.config.data_dir.display(); tracing::info!(path=%data_dir_display, "Data directory"); tracing::info!(path=%format!("{}/logs", data_dir_display), "Log files directory"); @@ -734,7 +638,8 @@ async fn get_config(context: Arc) -> Result { })) } -async fn withdraw_btc( +#[tracing::instrument(fields(method = "withdraw_btc"), skip(context))] +pub async fn withdraw_btc( withdraw_btc: WithdrawBtcArgs, context: Arc, ) -> Result { @@ -768,7 +673,8 @@ async fn withdraw_btc( })) } -async fn start_daemon( +#[tracing::instrument(fields(method = "start_daemon"), skip(context))] +pub async fn start_daemon( start_daemon: StartDaemonArgs, context: Arc, ) -> Result { @@ -776,7 +682,7 @@ async fn start_daemon( // Default to 127.0.0.1:1234 let server_address = server_address.unwrap_or("127.0.0.1:1234".parse()?); - let (addr, server_handle) = rpc::run_server(server_address, Arc::clone(&context)).await?; + let (addr, server_handle) = rpc::run_server(server_address, context).await?; tracing::info!(%addr, "Started RPC server"); @@ -787,7 +693,7 @@ async fn start_daemon( Ok(json!({})) } -// #[instrument(fields(method = "get_balance"))] +#[tracing::instrument(fields(method = "get_balance"), skip(context))] pub async fn get_balance(balance: BalanceArgs, context: Arc) -> Result { let BalanceArgs { force_refresh } = balance; let bitcoin_wallet = context @@ -818,7 +724,8 @@ pub async fn get_balance(balance: BalanceArgs, context: Arc) -> Result< }) } -async fn list_sellers( +#[tracing::instrument(fields(method = "list_sellers"), skip(context))] +pub async fn list_sellers( list_sellers: ListSellersArgs, context: Arc, ) -> Result { @@ -868,7 +775,8 @@ async fn list_sellers( Ok(json!({ "sellers": sellers })) } -async fn export_bitcoin_wallet(context: Arc) -> Result { +#[tracing::instrument(fields(method = "export_bitcoin_wallet"), skip(context))] +pub async fn export_bitcoin_wallet(context: Arc) -> Result { let bitcoin_wallet = context .bitcoin_wallet .as_ref() @@ -881,7 +789,8 @@ async fn export_bitcoin_wallet(context: Arc) -> Result, ) -> Result { @@ -914,7 +823,8 @@ async fn monero_recovery( } } -async fn get_current_swap(context: Arc) -> Result { +#[tracing::instrument(fields(method = "get_current_swap"), skip(context))] +pub async fn get_current_swap(context: Arc) -> Result { Ok(json!({ "swap_id": context.swap_lock.get_current_swap_id().await })) @@ -946,19 +856,7 @@ impl Request { } pub async fn call(self, context: Arc) -> Result { - let method_span = self.cmd.get_tracing_span(self.log_reference.clone()); - - self.handle_cmd(context) - .instrument(method_span.clone()) - .await - .map_err(|err| { - method_span.in_scope(|| { - // The {:?} formatter is used to print the entire error chain - tracing::debug!(err = format!("{:?}", err), "API call resulted in an error"); - }); - err - }) - .map(|result| json!(result)) + unreachable!("This function should never be called") } } diff --git a/swap/src/rpc.rs b/swap/src/rpc.rs index 473111f2..3629b37b 100644 --- a/swap/src/rpc.rs +++ b/swap/src/rpc.rs @@ -28,10 +28,11 @@ pub async fn run_server( .set_middleware(middleware) .build(server_address) .await?; + let mut modules = RpcModule::new(()); { modules - .merge(methods::register_modules(Arc::clone(&context))?) + .merge(methods::register_modules(context)?) .expect("Could not register RPC modules") } diff --git a/swap/src/rpc/methods.rs b/swap/src/rpc/methods.rs index 2b8ef7da..d1f0b66c 100644 --- a/swap/src/rpc/methods.rs +++ b/swap/src/rpc/methods.rs @@ -1,6 +1,8 @@ use crate::api::request::{ + buy_xmr, cancel_and_refund, get_balance, get_current_swap, get_history, get_raw_states, + get_swap_info, list_sellers, monero_recovery, resume_swap, suspend_current_swap, withdraw_btc, BalanceArgs, BuyXmrArgs, CancelAndRefundArgs, GetSwapInfoArgs, ListSellersArgs, Method, - MoneroRecoveryArgs, Request, ResumeArgs, WithdrawBtcArgs, + MoneroRecoveryArgs, ResumeArgs, WithdrawBtcArgs, }; use crate::api::Context; use crate::bitcoin::bitcoin_address; @@ -8,18 +10,29 @@ use crate::monero::monero_address; use crate::{bitcoin, monero}; use anyhow::Result; use jsonrpsee::server::RpcModule; -use jsonrpsee::types::Params; use libp2p::core::Multiaddr; use std::collections::HashMap; use std::str::FromStr; use std::sync::Arc; use uuid::Uuid; -pub fn register_modules(context: Arc) -> Result>> { - let mut module = RpcModule::new(context); +trait ConvertToJsonRpseeError { + fn to_jsonrpsee_result(self) -> Result; +} + +impl ConvertToJsonRpseeError for Result { + fn to_jsonrpsee_result(self) -> Result { + self.map_err(|e| jsonrpsee_core::Error::Custom(e.to_string())) + } +} + +pub fn register_modules(outer_context: Arc) -> Result>> { + let mut module = RpcModule::new(outer_context); module.register_async_method("suspend_current_swap", |params, context| async move { - execute_request(params, Method::SuspendCurrentSwap, &context).await + suspend_current_swap(Arc::unwrap_or_clone(context)) + .await + .to_jsonrpsee_result() })?; module.register_async_method("get_swap_info", |params_raw, context| async move { @@ -32,12 +45,9 @@ pub fn register_modules(context: Arc) -> Result> let swap_id = as_uuid(swap_id) .ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?; - execute_request( - params_raw, - Method::GetSwapInfo(GetSwapInfoArgs { swap_id }), - &context, - ) - .await + get_swap_info(GetSwapInfoArgs { swap_id }, Arc::unwrap_or_clone(context)) + .await + .to_jsonrpsee_result() })?; module.register_async_method("get_bitcoin_balance", |params_raw, context| async move { @@ -53,20 +63,21 @@ pub fn register_modules(context: Arc) -> Result> jsonrpsee_core::Error::Custom("force_refesh is not a boolean".to_string()) })?; - execute_request( - params_raw, - Method::Balance(BalanceArgs { force_refresh }), - &context, - ) - .await + get_balance(BalanceArgs { force_refresh }, Arc::unwrap_or_clone(context)) + .await + .to_jsonrpsee_result() })?; module.register_async_method("get_history", |params, context| async move { - execute_request(params, Method::History, &context).await + get_history(Arc::unwrap_or_clone(context)) + .await + .to_jsonrpsee_result() })?; module.register_async_method("get_raw_states", |params, context| async move { - execute_request(params, Method::GetRawStates, &context).await + get_raw_states(Arc::unwrap_or_clone(context)) + .await + .to_jsonrpsee_result() })?; module.register_async_method("resume_swap", |params_raw, context| async move { @@ -79,7 +90,9 @@ pub fn register_modules(context: Arc) -> Result> let swap_id = as_uuid(swap_id) .ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?; - execute_request(params_raw, Method::Resume(ResumeArgs { swap_id }), &context).await + resume_swap(ResumeArgs { swap_id }, Arc::unwrap_or_clone(context)) + .await + .to_jsonrpsee_result() })?; module.register_async_method("cancel_refund_swap", |params_raw, context| async move { @@ -92,12 +105,12 @@ pub fn register_modules(context: Arc) -> Result> let swap_id = as_uuid(swap_id) .ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?; - execute_request( - params_raw, - Method::CancelAndRefund(CancelAndRefundArgs { swap_id }), - &context, + cancel_and_refund( + CancelAndRefundArgs { swap_id }, + Arc::unwrap_or_clone(context), ) .await + .to_jsonrpsee_result() })?; module.register_async_method( @@ -113,12 +126,12 @@ pub fn register_modules(context: Arc) -> Result> jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()) })?; - execute_request( - params_raw, - Method::MoneroRecovery(MoneroRecoveryArgs { swap_id }), - &context, + monero_recovery( + MoneroRecoveryArgs { swap_id }, + Arc::unwrap_or_clone(context), ) .await + .to_jsonrpsee_result() }, )?; @@ -144,15 +157,15 @@ pub fn register_modules(context: Arc) -> Result> let withdraw_address = bitcoin_address::validate(withdraw_address, context.config.env_config.bitcoin_network)?; - execute_request( - params_raw, - Method::WithdrawBtc(WithdrawBtcArgs { + withdraw_btc( + WithdrawBtcArgs { amount, address: withdraw_address, - }), - &context, + }, + Arc::unwrap_or_clone(context), ) .await + .to_jsonrpsee_result() })?; module.register_async_method("buy_xmr", |params_raw, context| async move { @@ -186,17 +199,17 @@ pub fn register_modules(context: Arc) -> Result> })?) .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?; - execute_request( - params_raw, - Method::BuyXmr(BuyXmrArgs { + buy_xmr( + BuyXmrArgs { + seller, bitcoin_change_address, monero_receive_address, - seller, swap_id: Uuid::new_v4(), - }), - &context, + }, + Arc::unwrap_or_clone(context), ) .await + .to_jsonrpsee_result() })?; module.register_async_method("list_sellers", |params_raw, context| async move { @@ -213,18 +226,20 @@ pub fn register_modules(context: Arc) -> Result> jsonrpsee_core::Error::Custom("Could not parse valid multiaddr".to_string()) })?; - execute_request( - params_raw, - Method::ListSellers(ListSellersArgs { + list_sellers( + ListSellersArgs { rendezvous_point: rendezvous_point.clone(), - }), - &context, + }, + Arc::unwrap_or_clone(context), ) .await + .to_jsonrpsee_result() })?; module.register_async_method("get_current_swap", |params, context| async move { - execute_request(params, Method::GetCurrentSwap, &context).await + get_current_swap(Arc::unwrap_or_clone(context)) + .await + .to_jsonrpsee_result() })?; Ok(module) @@ -237,23 +252,3 @@ fn as_uuid(json_value: &serde_json::Value) -> Option { None } } - -async fn execute_request( - params: Params<'static>, - cmd: Method, - context: &Arc, -) -> Result { - // If we fail to parse the params as a String HashMap, it's most likely because its an empty object - // In that case, we want to make sure not to fail the request, so we set the log_reference_id to None - // and swallow the error - let reference_id = params - .parse::>() - .ok() - .and_then(|params_parsed| params_parsed.get("log_reference_id").cloned()); - - let request = Request::with_id(cmd, reference_id.map(|log_ref| log_ref.to_string())); - request - .call(Arc::clone(context)) - .await - .map_err(|err| jsonrpsee_core::Error::Custom(format!("{:#}", err))) -}