From 626deed7e2c9e207f085b3e0b53c9e78e42d45e2 Mon Sep 17 00:00:00 2001 From: pm47 Date: Thu, 30 Mar 2023 16:34:10 +0200 Subject: [PATCH] store restored channel immediately When we restore a channel from a peer backup, we need to store the channel in database as soon as possible, so we are exactly in the same state as if the channel was restored from local db. This applies whether we are restoring from empty or outdated local db. --- .../kotlin/fr/acinq/lightning/channel/Syncing.kt | 8 +++++++- src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/fr/acinq/lightning/channel/Syncing.kt b/src/commonMain/kotlin/fr/acinq/lightning/channel/Syncing.kt index feba9c9ff..d3e095276 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/channel/Syncing.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/channel/Syncing.kt @@ -63,7 +63,13 @@ data class Syncing(val state: ChannelStateWithCommitments, val waitForTheirReest yourLastCommitmentSecret = PrivateKey(yourLastPerCommitmentSecret), myCurrentPerCommitmentPoint = myCurrentPerCommitmentPoint ).withChannelData(nextState.commitments.remoteChannelData) - val actions = listOf(ChannelAction.Message.Send(channelReestablish)) + val actions = buildList { + if (nextState != state) { + // we just restored from backup + add(ChannelAction.Storage.StoreState(nextState)) + } + add(ChannelAction.Message.Send(channelReestablish)) + } // now apply their reestablish message to the restored state val (nextState1, actions1) = Syncing(nextState, waitForTheirReestablishMessage = false).processInternal(event) Pair(nextState1, actions + actions1) diff --git a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt index b4375d170..5e55eb8c6 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt @@ -613,6 +613,7 @@ class Peer( is Try.Success -> { logger.warning { "n:$remoteNodeId restoring channelId=${msg.channelId} from peer backup" } val backup = decrypted.result + db.channels.addOrUpdateChannel(backup) val state = WaitForInit(StaticParams(nodeParams, remoteNodeId), currentTipFlow.filterNotNull().first(), onChainFeeratesFlow.filterNotNull().first()) val event1 = ChannelEvent.Restore(backup as ChannelState) val (state1, actions1) = state.process(event1)