From ca32ad7f9ee7134728ee0925ceac7705b4ee618d Mon Sep 17 00:00:00 2001 From: Pierre-Marie Padiou Date: Fri, 18 Aug 2023 11:52:51 +0200 Subject: [PATCH] Check swap-in wallet at each block (#515) The `WalletState` class contains absolute blockheights for utxos, and does not contain the current block height, so it is constant as soon as an utxo confirms. Even if `ElectrumMiniWallet` explicitly updated `_walletStateFlow` (which it doesn't), it would be ignored because `MutableStateFlow` deduplicates events. We need to look for swap-ins when either the wallet state or the current blockheight changes, otherwise we will only call `TrySwapIn` when the utxo reaches `weak` confirmation, and not when it graduates from `weak` to `deep` confirmation. --- src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt index f43feb44c..6bd2a0d78 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt @@ -388,11 +388,10 @@ class Peer( } private suspend fun watchSwapInWallet() { - swapInWallet.walletStateFlow - .filter { it.consistent } - .collect { - val currentBlockHeight = currentTipFlow.filterNotNull().first().first - swapInCommands.send(SwapInCommand.TrySwapIn(currentBlockHeight, it, walletParams.swapInConfirmations, trustedSwapInTxs)) + swapInWallet.walletStateFlow.combine(currentTipFlow.filterNotNull()) { walletState, currentTip -> currentTip.first to walletState } + .filter { (_, walletState) -> walletState.consistent } + .collect { (currentBlockHeight, walletState) -> + swapInCommands.send(SwapInCommand.TrySwapIn(currentBlockHeight, walletState, walletParams.swapInConfirmations, trustedSwapInTxs)) } }