Skip to content

Commit 615a5fc

Browse files
committed
f Use fn instead of macro to apply wallet updates
1 parent c60e52a commit 615a5fc

File tree

1 file changed

+56
-34
lines changed

1 file changed

+56
-34
lines changed

src/chain/mod.rs

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use lightning_block_sync::poll::{ChainPoller, ChainTip, ValidatedBlockHeader};
3939
use lightning_block_sync::SpvClient;
4040

4141
use bdk_esplora::EsploraAsyncExt;
42+
use bdk_wallet::Update as BdkUpdate;
4243

4344
use esplora_client::AsyncClient as EsploraAsyncClient;
4445

@@ -755,40 +756,40 @@ impl ChainSource {
755756
let incremental_sync =
756757
node_metrics.read().unwrap().latest_onchain_wallet_sync_timestamp.is_some();
757758

758-
macro_rules! get_and_apply_wallet_update {
759-
($sync_future: expr) => {{
760-
let now = Instant::now();
761-
let update_res = $sync_future.await;
762-
match update_res {
763-
Ok(update) => match onchain_wallet.apply_update(update) {
764-
Ok(()) => {
765-
log_info!(
766-
logger,
767-
"{} of on-chain wallet finished in {}ms.",
768-
if incremental_sync { "Incremental sync" } else { "Sync" },
769-
now.elapsed().as_millis()
770-
);
771-
let unix_time_secs_opt = SystemTime::now()
772-
.duration_since(UNIX_EPOCH)
773-
.ok()
774-
.map(|d| d.as_secs());
775-
{
776-
let mut locked_node_metrics = node_metrics.write().unwrap();
777-
locked_node_metrics.latest_onchain_wallet_sync_timestamp =
778-
unix_time_secs_opt;
779-
write_node_metrics(
780-
&*locked_node_metrics,
781-
Arc::clone(&kv_store),
782-
Arc::clone(&logger),
783-
)?;
784-
}
785-
Ok(())
786-
},
787-
Err(e) => Err(e),
759+
fn apply_wallet_update(
760+
update_res: Result<BdkUpdate, Error>, incremental_sync: bool, now: Instant,
761+
node_metrics: Arc<RwLock<NodeMetrics>>, onchain_wallet: Arc<Wallet>,
762+
kv_store: Arc<DynStore>, logger: Arc<Logger>,
763+
) -> Result<(), Error> {
764+
match update_res {
765+
Ok(update) => match onchain_wallet.apply_update(update) {
766+
Ok(()) => {
767+
log_info!(
768+
logger,
769+
"{} of on-chain wallet finished in {}ms.",
770+
if incremental_sync { "Incremental sync" } else { "Sync" },
771+
now.elapsed().as_millis()
772+
);
773+
let unix_time_secs_opt = SystemTime::now()
774+
.duration_since(UNIX_EPOCH)
775+
.ok()
776+
.map(|d| d.as_secs());
777+
{
778+
let mut locked_node_metrics = node_metrics.write().unwrap();
779+
locked_node_metrics.latest_onchain_wallet_sync_timestamp =
780+
unix_time_secs_opt;
781+
write_node_metrics(
782+
&*locked_node_metrics,
783+
Arc::clone(&kv_store),
784+
Arc::clone(&logger),
785+
)?;
786+
}
787+
Ok(())
788788
},
789789
Err(e) => Err(e),
790-
}
791-
}};
790+
},
791+
Err(e) => Err(e),
792+
}
792793
}
793794

794795
let cached_txs = onchain_wallet.get_cached_txs();
@@ -797,12 +798,33 @@ impl ChainSource {
797798
let incremental_sync_request = onchain_wallet.get_incremental_sync_request();
798799
let incremental_sync_fut = electrum_client
799800
.get_incremental_sync_wallet_update(incremental_sync_request, cached_txs);
800-
get_and_apply_wallet_update!(incremental_sync_fut)
801+
802+
let now = Instant::now();
803+
let update_res = incremental_sync_fut.await.map(|u| u.into());
804+
apply_wallet_update(
805+
update_res,
806+
incremental_sync,
807+
now,
808+
Arc::clone(&node_metrics),
809+
Arc::clone(&onchain_wallet),
810+
Arc::clone(&kv_store),
811+
Arc::clone(&logger),
812+
)
801813
} else {
802814
let full_scan_request = onchain_wallet.get_full_scan_request();
803815
let full_scan_fut =
804816
electrum_client.get_full_scan_wallet_update(full_scan_request, cached_txs);
805-
get_and_apply_wallet_update!(full_scan_fut)
817+
let now = Instant::now();
818+
let update_res = full_scan_fut.await.map(|u| u.into());
819+
apply_wallet_update(
820+
update_res,
821+
incremental_sync,
822+
now,
823+
Arc::clone(&node_metrics),
824+
Arc::clone(&onchain_wallet),
825+
Arc::clone(&kv_store),
826+
Arc::clone(&logger),
827+
)
806828
};
807829

808830
onchain_wallet_sync_status.lock().unwrap().propagate_result_to_subscribers(res);

0 commit comments

Comments
 (0)