Skip to content

Commit c1157a3

Browse files
committed
Move inner code to sync_onchain_wallet_inner for Esplora
Previously, we might have overlooked some cases that would exit the method and bubble up an error via `?` instead of propagating to all subscribers. Here, we split out the code to an inner method to ensure we always propagate.
1 parent fb34a27 commit c1157a3

File tree

1 file changed

+92
-90
lines changed

1 file changed

+92
-90
lines changed

src/chain/esplora.rs

Lines changed: 92 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -112,102 +112,104 @@ impl EsploraChainSource {
112112
})?;
113113
}
114114

115-
let res = {
116-
// If this is our first sync, do a full scan with the configured gap limit.
117-
// Otherwise just do an incremental sync.
118-
let incremental_sync =
119-
self.node_metrics.read().unwrap().latest_onchain_wallet_sync_timestamp.is_some();
120-
121-
macro_rules! get_and_apply_wallet_update {
122-
($sync_future: expr) => {{
123-
let now = Instant::now();
124-
match $sync_future.await {
125-
Ok(res) => match res {
126-
Ok(update) => match self.onchain_wallet.apply_update(update) {
127-
Ok(()) => {
128-
log_info!(
129-
self.logger,
130-
"{} of on-chain wallet finished in {}ms.",
131-
if incremental_sync { "Incremental sync" } else { "Sync" },
132-
now.elapsed().as_millis()
133-
);
134-
let unix_time_secs_opt = SystemTime::now()
135-
.duration_since(UNIX_EPOCH)
136-
.ok()
137-
.map(|d| d.as_secs());
138-
{
139-
let mut locked_node_metrics = self.node_metrics.write().unwrap();
140-
locked_node_metrics.latest_onchain_wallet_sync_timestamp = unix_time_secs_opt;
141-
write_node_metrics(
142-
&*locked_node_metrics,
143-
Arc::clone(&self.kv_store),
144-
Arc::clone(&self.logger)
145-
)?;
146-
}
147-
Ok(())
148-
},
149-
Err(e) => Err(e),
150-
},
151-
Err(e) => match *e {
152-
esplora_client::Error::Reqwest(he) => {
153-
log_error!(
154-
self.logger,
155-
"{} of on-chain wallet failed due to HTTP connection error: {}",
156-
if incremental_sync { "Incremental sync" } else { "Sync" },
157-
he
158-
);
159-
Err(Error::WalletOperationFailed)
160-
},
161-
_ => {
162-
log_error!(
163-
self.logger,
164-
"{} of on-chain wallet failed due to Esplora error: {}",
165-
if incremental_sync { "Incremental sync" } else { "Sync" },
166-
e
167-
);
168-
Err(Error::WalletOperationFailed)
169-
},
170-
},
171-
},
172-
Err(e) => {
173-
log_error!(
174-
self.logger,
175-
"{} of on-chain wallet timed out: {}",
176-
if incremental_sync { "Incremental sync" } else { "Sync" },
177-
e
178-
);
179-
Err(Error::WalletOperationTimeout)
180-
},
181-
}
182-
}}
183-
}
184-
185-
if incremental_sync {
186-
let sync_request = self.onchain_wallet.get_incremental_sync_request();
187-
let wallet_sync_timeout_fut = tokio::time::timeout(
188-
Duration::from_secs(BDK_WALLET_SYNC_TIMEOUT_SECS),
189-
self.esplora_client.sync(sync_request, BDK_CLIENT_CONCURRENCY),
190-
);
191-
get_and_apply_wallet_update!(wallet_sync_timeout_fut)
192-
} else {
193-
let full_scan_request = self.onchain_wallet.get_full_scan_request();
194-
let wallet_sync_timeout_fut = tokio::time::timeout(
195-
Duration::from_secs(BDK_WALLET_SYNC_TIMEOUT_SECS),
196-
self.esplora_client.full_scan(
197-
full_scan_request,
198-
BDK_CLIENT_STOP_GAP,
199-
BDK_CLIENT_CONCURRENCY,
200-
),
201-
);
202-
get_and_apply_wallet_update!(wallet_sync_timeout_fut)
203-
}
204-
};
115+
let res = self.sync_onchain_wallet_inner().await;
205116

206117
self.onchain_wallet_sync_status.lock().unwrap().propagate_result_to_subscribers(res);
207118

208119
res
209120
}
210121

122+
async fn sync_onchain_wallet_inner(&self) -> Result<(), Error> {
123+
// If this is our first sync, do a full scan with the configured gap limit.
124+
// Otherwise just do an incremental sync.
125+
let incremental_sync =
126+
self.node_metrics.read().unwrap().latest_onchain_wallet_sync_timestamp.is_some();
127+
128+
macro_rules! get_and_apply_wallet_update {
129+
($sync_future: expr) => {{
130+
let now = Instant::now();
131+
match $sync_future.await {
132+
Ok(res) => match res {
133+
Ok(update) => match self.onchain_wallet.apply_update(update) {
134+
Ok(()) => {
135+
log_info!(
136+
self.logger,
137+
"{} of on-chain wallet finished in {}ms.",
138+
if incremental_sync { "Incremental sync" } else { "Sync" },
139+
now.elapsed().as_millis()
140+
);
141+
let unix_time_secs_opt = SystemTime::now()
142+
.duration_since(UNIX_EPOCH)
143+
.ok()
144+
.map(|d| d.as_secs());
145+
{
146+
let mut locked_node_metrics = self.node_metrics.write().unwrap();
147+
locked_node_metrics.latest_onchain_wallet_sync_timestamp = unix_time_secs_opt;
148+
write_node_metrics(
149+
&*locked_node_metrics,
150+
Arc::clone(&self.kv_store),
151+
Arc::clone(&self.logger)
152+
)?;
153+
}
154+
Ok(())
155+
},
156+
Err(e) => Err(e),
157+
},
158+
Err(e) => match *e {
159+
esplora_client::Error::Reqwest(he) => {
160+
log_error!(
161+
self.logger,
162+
"{} of on-chain wallet failed due to HTTP connection error: {}",
163+
if incremental_sync { "Incremental sync" } else { "Sync" },
164+
he
165+
);
166+
Err(Error::WalletOperationFailed)
167+
},
168+
_ => {
169+
log_error!(
170+
self.logger,
171+
"{} of on-chain wallet failed due to Esplora error: {}",
172+
if incremental_sync { "Incremental sync" } else { "Sync" },
173+
e
174+
);
175+
Err(Error::WalletOperationFailed)
176+
},
177+
},
178+
},
179+
Err(e) => {
180+
log_error!(
181+
self.logger,
182+
"{} of on-chain wallet timed out: {}",
183+
if incremental_sync { "Incremental sync" } else { "Sync" },
184+
e
185+
);
186+
Err(Error::WalletOperationTimeout)
187+
},
188+
}
189+
}}
190+
}
191+
192+
if incremental_sync {
193+
let sync_request = self.onchain_wallet.get_incremental_sync_request();
194+
let wallet_sync_timeout_fut = tokio::time::timeout(
195+
Duration::from_secs(BDK_WALLET_SYNC_TIMEOUT_SECS),
196+
self.esplora_client.sync(sync_request, BDK_CLIENT_CONCURRENCY),
197+
);
198+
get_and_apply_wallet_update!(wallet_sync_timeout_fut)
199+
} else {
200+
let full_scan_request = self.onchain_wallet.get_full_scan_request();
201+
let wallet_sync_timeout_fut = tokio::time::timeout(
202+
Duration::from_secs(BDK_WALLET_SYNC_TIMEOUT_SECS),
203+
self.esplora_client.full_scan(
204+
full_scan_request,
205+
BDK_CLIENT_STOP_GAP,
206+
BDK_CLIENT_CONCURRENCY,
207+
),
208+
);
209+
get_and_apply_wallet_update!(wallet_sync_timeout_fut)
210+
}
211+
}
212+
211213
pub(super) async fn sync_lightning_wallet(
212214
&self, channel_manager: Arc<ChannelManager>, chain_monitor: Arc<ChainMonitor>,
213215
output_sweeper: Arc<Sweeper>,

0 commit comments

Comments
 (0)