From 98d8685a8e5613bfb929d1b11a436b8406390a1c Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Sun, 26 Nov 2023 22:19:38 +0100 Subject: [PATCH] Allow API caller to retrieve last synced bitcoin balane and avoid costly sync --- swap/src/api/request.rs | 31 ++++++++++++++++++++++--------- swap/src/cli/command.rs | 4 +++- swap/src/rpc/methods.rs | 10 ++++++++-- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/swap/src/api/request.rs b/swap/src/api/request.rs index acf6777b..d24dade9 100644 --- a/swap/src/api/request.rs +++ b/swap/src/api/request.rs @@ -50,7 +50,9 @@ pub enum Method { amount: Option, address: bitcoin::Address, }, - Balance, + Balance { + force_refresh: bool, + }, ListSellers { rendezvous_point: Multiaddr, }, @@ -69,7 +71,7 @@ pub enum Method { impl Method { fn get_tracing_span(&self, log_reference_id: Option) -> Span { let span = match self { - Method::Balance => { + Method::Balance { .. } => { debug_span!( "method", method_name = "Balance", @@ -260,7 +262,7 @@ impl Request { state2.cancel_timelock, state2.punish_timelock, )) - }else { + } else { None } } else { @@ -702,18 +704,29 @@ impl Request { Ok(json!({})) } - Method::Balance => { + Method::Balance { force_refresh } => { let bitcoin_wallet = context .bitcoin_wallet .as_ref() .context("Could not get Bitcoin wallet")?; - bitcoin_wallet.sync().await?; + if force_refresh { + bitcoin_wallet.sync().await?; + } + let bitcoin_balance = bitcoin_wallet.balance().await?; - tracing::info!( - balance = %bitcoin_balance, - "Checked Bitcoin balance", - ); + + if force_refresh { + tracing::info!( + balance = %bitcoin_balance, + "Checked Bitcoin balance", + ); + } else { + tracing::debug!( + balance = %bitcoin_balance, + "Current Bitcoin balance as of last sync", + ); + } Ok(json!({ "balance": bitcoin_balance.to_sat() diff --git a/swap/src/cli/command.rs b/swap/src/cli/command.rs index d207ca98..0652fe40 100644 --- a/swap/src/cli/command.rs +++ b/swap/src/cli/command.rs @@ -109,7 +109,9 @@ where (context, request) } CliCommand::Balance { bitcoin } => { - let request = Request::new(Method::Balance); + let request = Request::new(Method::Balance { + force_refresh: true, + }); let context = Context::build( Some(bitcoin), diff --git a/swap/src/rpc/methods.rs b/swap/src/rpc/methods.rs index c38cb703..2cd7fb18 100644 --- a/swap/src/rpc/methods.rs +++ b/swap/src/rpc/methods.rs @@ -34,8 +34,14 @@ pub fn register_modules(context: Arc) -> Result> .await })?; - module.register_async_method("get_bitcoin_balance", |params, context| async move { - execute_request(params, Method::Balance, &context).await + module.register_async_method("get_bitcoin_balance", |params_raw, context| async move { + let params: HashMap = params_raw.parse()?; + + let force_refresh = *params.get("force_refresh").ok_or_else(|| { + jsonrpsee_core::Error::Custom("Does not contain force_refresh".to_string()) + })?; + + execute_request(params_raw, Method::Balance { force_refresh }, &context).await })?; module.register_async_method("get_history", |params, context| async move {