From cff51b341d9cc46a4b9ad6f095f05dc0bd44be04 Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Thu, 24 Aug 2023 14:26:28 +0200 Subject: [PATCH] Sync bitcoin wallet before initial max_giveable call --- swap/src/api/request.rs | 93 +++++++++++++++++++++++++++++++---------- swap/src/rpc/methods.rs | 41 ++++++++---------- 2 files changed, 89 insertions(+), 45 deletions(-) diff --git a/swap/src/api/request.rs b/swap/src/api/request.rs index f3a7df74..110e7b2a 100644 --- a/swap/src/api/request.rs +++ b/swap/src/api/request.rs @@ -19,7 +19,7 @@ use std::future::Future; use std::net::SocketAddr; use std::sync::Arc; use std::time::Duration; -use tracing::{debug_span, Instrument, Span, field}; +use tracing::{debug_span, field, Instrument, Span}; use uuid::Uuid; //TODO: Request and Method can be combined into a single enum @@ -71,7 +71,9 @@ pub enum Method { impl Method { fn get_tracing_span(&self, log_reference_id: Option) -> Span { let span = match self { - Method::Balance => debug_span!("method", name = "Balance", log_reference_id=field::Empty), + Method::Balance => { + debug_span!("method", name = "Balance", log_reference_id = field::Empty) + } Method::BuyXmr { swap_id, .. } => { debug_span!("method", name="BuyXmr", swap_id=%swap_id, log_reference_id=field::Empty) } @@ -81,32 +83,72 @@ impl Method { Method::Resume { swap_id } => { debug_span!("method", name="Resume", swap_id=%swap_id, log_reference_id=field::Empty) } - Method::Config => debug_span!("method", name = "Config", log_reference_id=field::Empty), + Method::Config => { + debug_span!("method", name = "Config", log_reference_id = field::Empty) + } Method::ExportBitcoinWallet => { - debug_span!("method", name = "ExportBitcoinWallet", log_reference_id=field::Empty) + debug_span!( + "method", + name = "ExportBitcoinWallet", + log_reference_id = field::Empty + ) } Method::GetCurrentSwap => { - debug_span!("method", name = "GetCurrentSwap", log_reference_id=field::Empty) + debug_span!( + "method", + name = "GetCurrentSwap", + log_reference_id = field::Empty + ) } Method::GetSwapInfo { .. } => { - debug_span!("method", name = "GetSwapInfo", log_reference_id=field::Empty) + debug_span!( + "method", + name = "GetSwapInfo", + log_reference_id = field::Empty + ) + } + Method::History => { + debug_span!("method", name = "History", log_reference_id = field::Empty) } - Method::History => debug_span!("method", name = "History", log_reference_id=field::Empty), Method::ListSellers { .. } => { - debug_span!("method", name = "ListSellers", log_reference_id=field::Empty) + debug_span!( + "method", + name = "ListSellers", + log_reference_id = field::Empty + ) } Method::MoneroRecovery { .. } => { - debug_span!("method", name = "MoneroRecovery", log_reference_id=field::Empty) + debug_span!( + "method", + name = "MoneroRecovery", + log_reference_id = field::Empty + ) } - Method::RawHistory => debug_span!("method", name = "RawHistory", log_reference_id=field::Empty), + Method::RawHistory => debug_span!( + "method", + name = "RawHistory", + log_reference_id = field::Empty + ), Method::StartDaemon { .. } => { - debug_span!("method", name = "StartDaemon", log_reference_id=field::Empty) + debug_span!( + "method", + name = "StartDaemon", + log_reference_id = field::Empty + ) } Method::SuspendCurrentSwap => { - debug_span!("method", name = "SuspendCurrentSwap", log_reference_id=field::Empty) + debug_span!( + "method", + name = "SuspendCurrentSwap", + log_reference_id = field::Empty + ) } Method::WithdrawBtc { .. } => { - debug_span!("method", name = "WithdrawBtc", log_reference_id=field::Empty) + debug_span!( + "method", + name = "WithdrawBtc", + log_reference_id = field::Empty + ) } }; if let Some(log_reference_id) = log_reference_id { @@ -118,11 +160,17 @@ impl Method { impl Request { pub fn new(cmd: Method) -> Request { - Request { cmd, log_reference: None } + Request { + cmd, + log_reference: None, + } } pub fn with_id(cmd: Method, id: Option) -> Request { - Request { cmd, log_reference: id } + Request { + cmd, + log_reference: id, + } } // We pass the outer tracing span down to this function such that it can be passed down to other spawned tokio tasks @@ -699,21 +747,21 @@ pub async fn determine_btc_to_swap( max_giveable_fn: FMG, sync: FS, estimate_fee: FFE, -) -> Result<(bitcoin::Amount, bitcoin::Amount)> +) -> Result<(Amount, Amount)> where - TB: Future>, + TB: Future>, FB: Fn() -> TB, - TMG: Future>, + TMG: Future>, FMG: Fn() -> TMG, TS: Future>, FS: Fn() -> TS, - FFE: Fn(bitcoin::Amount) -> TFE, - TFE: Future>, + FFE: Fn(Amount) -> TFE, + TFE: Future>, { tracing::debug!("Requesting quote"); let bid_quote = bid_quote.await?; - if bid_quote.max_quantity == bitcoin::Amount::ZERO { + if bid_quote.max_quantity == Amount::ZERO { bail!(ZeroQuoteReceived) } @@ -724,9 +772,10 @@ where "Received quote", ); + sync().await?; let mut max_giveable = max_giveable_fn().await?; - if max_giveable == bitcoin::Amount::ZERO || max_giveable < bid_quote.min_quantity { + if max_giveable == Amount::ZERO || max_giveable < bid_quote.min_quantity { let deposit_address = get_new_address.await?; let minimum_amount = bid_quote.min_quantity; let maximum_amount = bid_quote.max_quantity; diff --git a/swap/src/rpc/methods.rs b/swap/src/rpc/methods.rs index 32559a1b..9144cfbb 100644 --- a/swap/src/rpc/methods.rs +++ b/swap/src/rpc/methods.rs @@ -5,11 +5,11 @@ 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 jsonrpsee::types::Params; use uuid::Uuid; pub fn register_modules(context: Arc) -> RpcModule> { @@ -31,11 +31,10 @@ pub fn register_modules(context: Arc) -> RpcModule> { execute_request( params_raw, - Method::GetSwapInfo { - swap_id: *swap_id, - }, + Method::GetSwapInfo { swap_id: *swap_id }, &context, - ).await + ) + .await }) .unwrap(); @@ -65,13 +64,7 @@ pub fn register_modules(context: Arc) -> RpcModule> { jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()) })?; - execute_request( - params_raw, - Method::Resume { - swap_id: *swap_id, - }, - &context, - ).await + execute_request(params_raw, Method::Resume { swap_id: *swap_id }, &context).await }) .unwrap(); @@ -85,11 +78,10 @@ pub fn register_modules(context: Arc) -> RpcModule> { execute_request( params_raw, - Method::CancelAndRefund { - swap_id: *swap_id, - }, + Method::CancelAndRefund { swap_id: *swap_id }, &context, - ).await + ) + .await }) .unwrap(); @@ -123,7 +115,8 @@ pub fn register_modules(context: Arc) -> RpcModule> { address: withdraw_address, }, &context, - ).await + ) + .await }) .expect("Could not register RPC method withdraw_btc"); module @@ -168,7 +161,8 @@ pub fn register_modules(context: Arc) -> RpcModule> { swap_id: Uuid::new_v4(), }, &context, - ).await + ) + .await }) .unwrap(); module @@ -184,7 +178,8 @@ pub fn register_modules(context: Arc) -> RpcModule> { rendezvous_point: rendezvous_point.clone(), }, &context, - ).await + ) + .await }) .unwrap(); @@ -202,15 +197,15 @@ async fn execute_request( cmd: Method, context: &Arc, ) -> Result { - let params_parsed = params.parse::>() + let params_parsed = params + .parse::>() .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?; - let reference_id = params_parsed - .get("log_reference_id"); + let reference_id = params_parsed.get("log_reference_id"); let request = Request::with_id(cmd, reference_id.map(|s| s.clone())); request .call(Arc::clone(context)) .await .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string())) -} \ No newline at end of file +}