Sync bitcoin wallet before initial max_giveable call

This commit is contained in:
binarybaron 2023-08-24 14:26:28 +02:00 committed by binarybaron
parent 99dfa895bb
commit cff51b341d
2 changed files with 89 additions and 45 deletions

View file

@ -19,7 +19,7 @@ use std::future::Future;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tracing::{debug_span, Instrument, Span, field}; use tracing::{debug_span, field, Instrument, Span};
use uuid::Uuid; use uuid::Uuid;
//TODO: Request and Method can be combined into a single enum //TODO: Request and Method can be combined into a single enum
@ -71,7 +71,9 @@ pub enum Method {
impl Method { impl Method {
fn get_tracing_span(&self, log_reference_id: Option<String>) -> Span { fn get_tracing_span(&self, log_reference_id: Option<String>) -> Span {
let span = match self { 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, .. } => { Method::BuyXmr { swap_id, .. } => {
debug_span!("method", name="BuyXmr", swap_id=%swap_id, log_reference_id=field::Empty) debug_span!("method", name="BuyXmr", swap_id=%swap_id, log_reference_id=field::Empty)
} }
@ -81,32 +83,72 @@ impl Method {
Method::Resume { swap_id } => { Method::Resume { swap_id } => {
debug_span!("method", name="Resume", swap_id=%swap_id, log_reference_id=field::Empty) 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 => { Method::ExportBitcoinWallet => {
debug_span!("method", name = "ExportBitcoinWallet", log_reference_id=field::Empty) debug_span!(
"method",
name = "ExportBitcoinWallet",
log_reference_id = field::Empty
)
} }
Method::GetCurrentSwap => { Method::GetCurrentSwap => {
debug_span!("method", name = "GetCurrentSwap", log_reference_id=field::Empty) debug_span!(
"method",
name = "GetCurrentSwap",
log_reference_id = field::Empty
)
} }
Method::GetSwapInfo { .. } => { 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 { .. } => { Method::ListSellers { .. } => {
debug_span!("method", name = "ListSellers", log_reference_id=field::Empty) debug_span!(
"method",
name = "ListSellers",
log_reference_id = field::Empty
)
} }
Method::MoneroRecovery { .. } => { 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 { .. } => { Method::StartDaemon { .. } => {
debug_span!("method", name = "StartDaemon", log_reference_id=field::Empty) debug_span!(
"method",
name = "StartDaemon",
log_reference_id = field::Empty
)
} }
Method::SuspendCurrentSwap => { Method::SuspendCurrentSwap => {
debug_span!("method", name = "SuspendCurrentSwap", log_reference_id=field::Empty) debug_span!(
"method",
name = "SuspendCurrentSwap",
log_reference_id = field::Empty
)
} }
Method::WithdrawBtc { .. } => { 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 { if let Some(log_reference_id) = log_reference_id {
@ -118,11 +160,17 @@ impl Method {
impl Request { impl Request {
pub fn new(cmd: Method) -> 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<String>) -> Request { pub fn with_id(cmd: Method, id: Option<String>) -> 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 // 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<FB, TB, FMG, TMG, FS, TS, FFE, TFE>(
max_giveable_fn: FMG, max_giveable_fn: FMG,
sync: FS, sync: FS,
estimate_fee: FFE, estimate_fee: FFE,
) -> Result<(bitcoin::Amount, bitcoin::Amount)> ) -> Result<(Amount, Amount)>
where where
TB: Future<Output = Result<bitcoin::Amount>>, TB: Future<Output = Result<Amount>>,
FB: Fn() -> TB, FB: Fn() -> TB,
TMG: Future<Output = Result<bitcoin::Amount>>, TMG: Future<Output = Result<Amount>>,
FMG: Fn() -> TMG, FMG: Fn() -> TMG,
TS: Future<Output = Result<()>>, TS: Future<Output = Result<()>>,
FS: Fn() -> TS, FS: Fn() -> TS,
FFE: Fn(bitcoin::Amount) -> TFE, FFE: Fn(Amount) -> TFE,
TFE: Future<Output = Result<bitcoin::Amount>>, TFE: Future<Output = Result<Amount>>,
{ {
tracing::debug!("Requesting quote"); tracing::debug!("Requesting quote");
let bid_quote = bid_quote.await?; let bid_quote = bid_quote.await?;
if bid_quote.max_quantity == bitcoin::Amount::ZERO { if bid_quote.max_quantity == Amount::ZERO {
bail!(ZeroQuoteReceived) bail!(ZeroQuoteReceived)
} }
@ -724,9 +772,10 @@ where
"Received quote", "Received quote",
); );
sync().await?;
let mut max_giveable = max_giveable_fn().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 deposit_address = get_new_address.await?;
let minimum_amount = bid_quote.min_quantity; let minimum_amount = bid_quote.min_quantity;
let maximum_amount = bid_quote.max_quantity; let maximum_amount = bid_quote.max_quantity;

View file

@ -5,11 +5,11 @@ use crate::monero::monero_address;
use crate::{bitcoin, monero}; use crate::{bitcoin, monero};
use anyhow::Result; use anyhow::Result;
use jsonrpsee::server::RpcModule; use jsonrpsee::server::RpcModule;
use jsonrpsee::types::Params;
use libp2p::core::Multiaddr; use libp2p::core::Multiaddr;
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use jsonrpsee::types::Params;
use uuid::Uuid; use uuid::Uuid;
pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> { pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
@ -31,11 +31,10 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
execute_request( execute_request(
params_raw, params_raw,
Method::GetSwapInfo { Method::GetSwapInfo { swap_id: *swap_id },
swap_id: *swap_id,
},
&context, &context,
).await )
.await
}) })
.unwrap(); .unwrap();
@ -65,13 +64,7 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()) jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string())
})?; })?;
execute_request( execute_request(params_raw, Method::Resume { swap_id: *swap_id }, &context).await
params_raw,
Method::Resume {
swap_id: *swap_id,
},
&context,
).await
}) })
.unwrap(); .unwrap();
@ -85,11 +78,10 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
execute_request( execute_request(
params_raw, params_raw,
Method::CancelAndRefund { Method::CancelAndRefund { swap_id: *swap_id },
swap_id: *swap_id,
},
&context, &context,
).await )
.await
}) })
.unwrap(); .unwrap();
@ -123,7 +115,8 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
address: withdraw_address, address: withdraw_address,
}, },
&context, &context,
).await )
.await
}) })
.expect("Could not register RPC method withdraw_btc"); .expect("Could not register RPC method withdraw_btc");
module module
@ -168,7 +161,8 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
swap_id: Uuid::new_v4(), swap_id: Uuid::new_v4(),
}, },
&context, &context,
).await )
.await
}) })
.unwrap(); .unwrap();
module module
@ -184,7 +178,8 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
rendezvous_point: rendezvous_point.clone(), rendezvous_point: rendezvous_point.clone(),
}, },
&context, &context,
).await )
.await
}) })
.unwrap(); .unwrap();
@ -202,15 +197,15 @@ async fn execute_request(
cmd: Method, cmd: Method,
context: &Arc<Context>, context: &Arc<Context>,
) -> Result<serde_json::Value, jsonrpsee_core::Error> { ) -> Result<serde_json::Value, jsonrpsee_core::Error> {
let params_parsed = params.parse::<HashMap<String, String>>() let params_parsed = params
.parse::<HashMap<String, String>>()
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?; .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
let reference_id = params_parsed let reference_id = params_parsed.get("log_reference_id");
.get("log_reference_id");
let request = Request::with_id(cmd, reference_id.map(|s| s.clone())); let request = Request::with_id(cmd, reference_id.map(|s| s.clone()));
request request
.call(Arc::clone(context)) .call(Arc::clone(context))
.await .await
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string())) .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))
} }