diff --git a/swap/src/api.rs b/swap/src/api.rs index 2362d766..0cf889dd 100644 --- a/swap/src/api.rs +++ b/swap/src/api.rs @@ -226,7 +226,9 @@ impl Context { bitcoin_wallet: Some(bob_bitcoin_wallet), monero_wallet: Some(bob_monero_wallet), config, - db: open_db(db_path).await.expect("Could not open sqlite database"), + db: open_db(db_path) + .await + .expect("Could not open sqlite database"), monero_rpc_process: None, swap_lock: Arc::new(SwapLock::new()), } diff --git a/swap/src/api/request.rs b/swap/src/api/request.rs index 488ea605..182cec6f 100644 --- a/swap/src/api/request.rs +++ b/swap/src/api/request.rs @@ -70,7 +70,11 @@ 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) + debug_span!( + "method", + method_name = "Balance", + log_reference_id = field::Empty + ) } Method::BuyXmr { swap_id, .. } => { debug_span!("method", method_name="BuyXmr", swap_id=%swap_id, log_reference_id=field::Empty) @@ -82,7 +86,11 @@ impl Method { 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) + debug_span!( + "method", + method_name = "Config", + log_reference_id = field::Empty + ) } Method::ExportBitcoinWallet => { debug_span!( @@ -106,7 +114,11 @@ impl Method { ) } Method::History => { - debug_span!("method", method_name = "History", log_reference_id = field::Empty) + debug_span!( + "method", + method_name = "History", + log_reference_id = field::Empty + ) } Method::ListSellers { .. } => { debug_span!( @@ -179,9 +191,7 @@ impl Request { if let Some(id_value) = swap_id { context.swap_lock.send_suspend_signal().await?; - Ok(json!({ - "swapId": id_value - })) + Ok(json!({ "swapId": id_value })) } else { bail!("No swap is currently running") } @@ -301,7 +311,12 @@ impl Request { } => { context.swap_lock.acquire_swap_lock(swap_id).await?; - let bitcoin_wallet = Arc::clone(&context.bitcoin_wallet.as_ref().expect("Could not find Bitcoin wallet")); + let bitcoin_wallet = Arc::clone( + &context + .bitcoin_wallet + .as_ref() + .expect("Could not find Bitcoin wallet"), + ); let env_config = context.config.env_config; let seed = context.config.seed.clone().context("Could not get seed")?; @@ -415,9 +430,7 @@ impl Request { } tracing::debug!(%swap_id, "Swap completed"); Ok::<_, anyhow::Error>(()) - } => { - - } + } => { } }; context .swap_lock @@ -462,7 +475,7 @@ impl Request { .context("Could not get Tor SOCKS5 port")?, behaviour, ) - .await?; + .await?; let our_peer_id = swarm.local_peer_id(); tracing::debug!(peer_id = %our_peer_id, "Network layer initialized"); @@ -494,36 +507,40 @@ impl Request { context.config.env_config, event_loop_handle, monero_receive_address, - ).await?; + ) + .await?; - tokio::spawn(async move { - tokio::select! { - _ = async { - let handle = tokio::spawn(event_loop.run().in_current_span()); + tokio::spawn( + async move { + tokio::select! { + _ = async { + let handle = tokio::spawn(event_loop.run().in_current_span()); - tokio::select! { - event_loop_result = handle => { - event_loop_result?; - }, - swap_result = bob::run(swap) => { - swap_result?; - } - }; - Ok::<(), anyhow::Error>(()) - } => { - - }, - _ = context.swap_lock.listen_for_swap_force_suspension() => { - tracing::debug!("Shutdown signal received, exiting"); - - } + tokio::select! { + event_loop_result = handle => { + event_loop_result?; + }, + swap_result = bob::run(swap) => { + swap_result?; + } + }; + Ok::<(), anyhow::Error>(()) + } => { + + }, + _ = context.swap_lock.listen_for_swap_force_suspension() => { + tracing::debug!("Shutdown signal received, exiting"); + + } + } + context + .swap_lock + .release_swap_lock() + .await + .expect("Could not release swap lock"); } - context - .swap_lock - .release_swap_lock() - .await - .expect("Could not release swap lock"); - }.in_current_span()); + .in_current_span(), + ); Ok(json!({ "result": "ok", })) @@ -741,10 +758,7 @@ impl Request { } pub async fn call(self, context: Arc) -> Result { - let method_span = self - .cmd - .get_tracing_span(self.log_reference.clone()) - ; + let method_span = self.cmd.get_tracing_span(self.log_reference.clone()); self.handle_cmd(context).instrument(method_span).await } diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index 468d607d..152c8730 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -785,7 +785,7 @@ impl Client { if !self.script_history.contains_key(&script) { self.script_history.insert(script.clone(), vec![]); - + // When we first subscribe to a script we want to immediately fetch its status // Otherwise we would have to wait for the next sync interval, which can take a minute // This would result in potentially inaccurate status updates until that next sync interval is hit diff --git a/swap/src/rpc/methods.rs b/swap/src/rpc/methods.rs index 6425aae0..c38cb703 100644 --- a/swap/src/rpc/methods.rs +++ b/swap/src/rpc/methods.rs @@ -15,13 +15,65 @@ use uuid::Uuid; pub fn register_modules(context: Arc) -> Result>> { let mut module = RpcModule::new(context); - module - .register_async_method("suspend_current_swap", |params, context| async move { - execute_request(params, Method::SuspendCurrentSwap, &context).await - })?; + module.register_async_method("suspend_current_swap", |params, context| async move { + execute_request(params, Method::SuspendCurrentSwap, &context).await + })?; - module - .register_async_method("get_swap_info", |params_raw, context| async move { + module.register_async_method("get_swap_info", |params_raw, context| async move { + let params: HashMap = params_raw.parse()?; + + let swap_id = params + .get("swap_id") + .ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()))?; + + execute_request( + params_raw, + Method::GetSwapInfo { swap_id: *swap_id }, + &context, + ) + .await + })?; + + module.register_async_method("get_bitcoin_balance", |params, context| async move { + execute_request(params, Method::Balance, &context).await + })?; + + module.register_async_method("get_history", |params, context| async move { + execute_request(params, Method::History, &context).await + })?; + + module.register_async_method("get_raw_states", |params, context| async move { + execute_request(params, Method::GetRawStates, &context).await + })?; + + module.register_async_method("resume_swap", |params_raw, context| async move { + let params: HashMap = params_raw.parse()?; + + let swap_id = params + .get("swap_id") + .ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()))?; + + execute_request(params_raw, Method::Resume { swap_id: *swap_id }, &context).await + })?; + + module.register_async_method("cancel_refund_swap", |params_raw, context| async move { + let params: HashMap = params_raw.parse()?; + + let swap_id = params + .get("swap_id") + .ok_or_else(|| jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()))?; + + execute_request( + params_raw, + Method::CancelAndRefund { swap_id: *swap_id }, + &context, + ) + .await + })?; + + module.register_async_method( + "get_monero_recovery_info", + |params_raw, context| async move { let params: HashMap = params_raw.parse()?; let swap_id = params.get("swap_id").ok_or_else(|| { @@ -30,179 +82,109 @@ pub fn register_modules(context: Arc) -> Result> execute_request( params_raw, - Method::GetSwapInfo { swap_id: *swap_id }, + Method::MoneroRecovery { swap_id: *swap_id }, &context, ) .await - })?; + }, + )?; - module - .register_async_method("get_bitcoin_balance", |params, context| async move { - execute_request(params, Method::Balance, &context).await - })?; + module.register_async_method("withdraw_btc", |params_raw, context| async move { + let params: HashMap = params_raw.parse()?; - module - .register_async_method("get_history", |params, context| async move { - execute_request(params, Method::History, &context).await - })?; - - module - .register_async_method("get_raw_states", |params, context| async move { - execute_request(params, Method::GetRawStates, &context).await - })?; - - module - .register_async_method("resume_swap", |params_raw, context| async move { - let params: HashMap = params_raw.parse()?; - - let swap_id = params.get("swap_id").ok_or_else(|| { - jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()) - })?; - - execute_request(params_raw, Method::Resume { swap_id: *swap_id }, &context).await - })?; - - module - .register_async_method("cancel_refund_swap", |params_raw, context| async move { - let params: HashMap = params_raw.parse()?; - - let swap_id = params.get("swap_id").ok_or_else(|| { - jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()) - })?; - - execute_request( - params_raw, - Method::CancelAndRefund { swap_id: *swap_id }, - &context, + let amount = if let Some(amount_str) = params.get("amount") { + Some( + ::bitcoin::Amount::from_str_in(amount_str, ::bitcoin::Denomination::Bitcoin) + .map_err(|_| { + jsonrpsee_core::Error::Custom("Unable to parse amount".to_string()) + })?, ) - .await - })?; + } else { + None + }; - module - .register_async_method( - "get_monero_recovery_info", - |params_raw, context| async move { - let params: HashMap = params_raw.parse()?; + let withdraw_address = + bitcoin::Address::from_str(params.get("address").ok_or_else(|| { + jsonrpsee_core::Error::Custom("Does not contain address".to_string()) + })?) + .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?; + let withdraw_address = + bitcoin_address::validate(withdraw_address, context.config.env_config.bitcoin_network)?; - let swap_id = params.get("swap_id").ok_or_else(|| { - jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()) - })?; + execute_request( + params_raw, + Method::WithdrawBtc { + amount, + address: withdraw_address, + }, + &context, + ) + .await + })?; - execute_request( - params_raw, - Method::MoneroRecovery { swap_id: *swap_id }, - &context, - ) - .await - })?; + module.register_async_method("buy_xmr", |params_raw, context| async move { + let params: HashMap = params_raw.parse()?; - module - .register_async_method("withdraw_btc", |params_raw, context| async move { - let params: HashMap = params_raw.parse()?; - - let amount = if let Some(amount_str) = params.get("amount") { - Some( - ::bitcoin::Amount::from_str_in(amount_str, ::bitcoin::Denomination::Bitcoin) - .map_err(|_| { - jsonrpsee_core::Error::Custom("Unable to parse amount".to_string()) - })?, - ) - } else { - None - }; - - let withdraw_address = - bitcoin::Address::from_str(params.get("address").ok_or_else(|| { - jsonrpsee_core::Error::Custom("Does not contain address".to_string()) - })?) - .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?; - let withdraw_address = bitcoin_address::validate( - withdraw_address, - context.config.env_config.bitcoin_network, - )?; - - execute_request( - params_raw, - Method::WithdrawBtc { - amount, - address: withdraw_address, - }, - &context, - ) - .await - })?; - - module - .register_async_method("buy_xmr", |params_raw, context| async move { - let params: HashMap = params_raw.parse()?; - - let bitcoin_change_address = bitcoin::Address::from_str( - params.get("bitcoin_change_address").ok_or_else(|| { - jsonrpsee_core::Error::Custom( - "Does not contain bitcoin_change_address".to_string(), - ) - })?, - ) + let bitcoin_change_address = + bitcoin::Address::from_str(params.get("bitcoin_change_address").ok_or_else(|| { + jsonrpsee_core::Error::Custom("Does not contain bitcoin_change_address".to_string()) + })?) .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?; - let bitcoin_change_address = bitcoin_address::validate( - bitcoin_change_address, - context.config.env_config.bitcoin_network, - )?; + let bitcoin_change_address = bitcoin_address::validate( + bitcoin_change_address, + context.config.env_config.bitcoin_network, + )?; - let monero_receive_address = monero::Address::from_str( - params.get("monero_receive_address").ok_or_else(|| { - jsonrpsee_core::Error::Custom( - "Does not contain monero_receiveaddress".to_string(), - ) - })?, - ) + let monero_receive_address = + monero::Address::from_str(params.get("monero_receive_address").ok_or_else(|| { + jsonrpsee_core::Error::Custom("Does not contain monero_receiveaddress".to_string()) + })?) .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?; - let monero_receive_address = monero_address::validate( - monero_receive_address, - context.config.env_config.monero_network, - )?; + let monero_receive_address = monero_address::validate( + monero_receive_address, + context.config.env_config.monero_network, + )?; - let seller = Multiaddr::from_str(params.get("seller").ok_or_else(|| { + let seller = + Multiaddr::from_str(params.get("seller").ok_or_else(|| { jsonrpsee_core::Error::Custom("Does not contain seller".to_string()) })?) .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?; - execute_request( - params_raw, - Method::BuyXmr { - bitcoin_change_address, - monero_receive_address, - seller, - swap_id: Uuid::new_v4(), - }, - &context, - ) - .await + execute_request( + params_raw, + Method::BuyXmr { + bitcoin_change_address, + monero_receive_address, + seller, + swap_id: Uuid::new_v4(), + }, + &context, + ) + .await + })?; + + module.register_async_method("list_sellers", |params_raw, context| async move { + let params: HashMap = params_raw.parse()?; + let rendezvous_point = params.get("rendezvous_point").ok_or_else(|| { + jsonrpsee_core::Error::Custom("Does not contain rendezvous_point".to_string()) })?; - module - .register_async_method("list_sellers", |params_raw, context| async move { - let params: HashMap = params_raw.parse()?; - let rendezvous_point = params.get("rendezvous_point").ok_or_else(|| { - jsonrpsee_core::Error::Custom("Does not contain rendezvous_point".to_string()) - })?; + execute_request( + params_raw, + Method::ListSellers { + rendezvous_point: rendezvous_point.clone(), + }, + &context, + ) + .await + })?; - execute_request( - params_raw, - Method::ListSellers { - rendezvous_point: rendezvous_point.clone(), - }, - &context, - ) - .await - })?; - - module - .register_async_method("get_current_swap", |params, context| async move { - execute_request(params, Method::GetCurrentSwap, &context).await - })?; + module.register_async_method("get_current_swap", |params, context| async move { + execute_request(params, Method::GetCurrentSwap, &context).await + })?; Ok(module) }