Skip to content

Commit b18838b

Browse files
committed
Serialize rebalance trigger checks
Run rebalance trigger checks while holding the balance mutex so concurrent callers cannot all authorize work from the same stale wallet state. This keeps trusted and on-chain rebalances from being queued more than once when rapid payment activity invokes the rebalancer from multiple paths at the same time.
1 parent c8f6c92 commit b18838b

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

  • graduated-rebalancer/src

graduated-rebalancer/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,22 +256,23 @@ where
256256

257257
/// Does a trusted to lightning rebalance if needed
258258
pub async fn do_trusted_rebalance_if_needed(&self) {
259+
let _lock = self.balance_mutex.lock().await;
259260
if let Some(params) = self.trigger.needs_trusted_rebalance().await {
260-
self.do_trusted_rebalance(params).await;
261+
self.do_trusted_rebalance_locked(params).await;
261262
}
262263
}
263264

264265
/// Does an on-chain to lightning rebalance if needed
265266
pub async fn do_onchain_rebalance_if_needed(&self) {
267+
let _lock = self.balance_mutex.lock().await;
266268
if let Some(params) = self.trigger.needs_onchain_rebalance().await {
267-
self.do_onchain_rebalance(params).await;
269+
self.do_onchain_rebalance_locked(params).await;
268270
}
269271
}
270272

271273
/// Perform a rebalance from trusted to lightning wallet
272-
async fn do_trusted_rebalance(&self, params: TriggerParams) {
274+
async fn do_trusted_rebalance_locked(&self, params: TriggerParams) {
273275
let transfer_amt = params.amount;
274-
let _lock = self.balance_mutex.lock().await;
275276
log_info!(self.logger, "Initiating rebalance");
276277

277278
if let Ok(inv) = self.ln_wallet.get_bolt11_invoice(Some(transfer_amt)).await {
@@ -340,9 +341,7 @@ where
340341
}
341342

342343
/// Perform on-chain to lightning rebalance by opening a channel or splicing into an existing one
343-
async fn do_onchain_rebalance(&self, params: TriggerParams) {
344-
let _lock = self.balance_mutex.lock().await;
345-
344+
async fn do_onchain_rebalance_locked(&self, params: TriggerParams) {
346345
let (channel_outpoint, user_channel_id) = if self.ln_wallet.has_channel_with_lsp() {
347346
log_info!(self.logger, "Splicing into channel with LSP with on-chain funds");
348347

0 commit comments

Comments
 (0)