From fe264cd273cfa8c09e17e6d1e39fcf82737d3479 Mon Sep 17 00:00:00 2001 From: t-bast Date: Wed, 21 Jun 2023 15:48:18 +0200 Subject: [PATCH] Make `SwapInManager` a peer field And initialize it once we have loaded channels from the DB. --- .../kotlin/fr/acinq/lightning/io/Peer.kt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt index 68191f2d8..f9a26e560 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt @@ -135,6 +135,8 @@ class Peer( private val _channelsFlow = MutableStateFlow>(HashMap()) val channelsFlow: StateFlow> get() = _channelsFlow + private var swapInManager: SwapInManager? = null + private var _channels by _channelsFlow val channels: Map get() = _channelsFlow.value @@ -227,6 +229,7 @@ class Peer( _channels = _channels + (it.channelId to state1) it.channelId } + swapInManager = SwapInManager(bootChannels, logger) logger.info { "restored ${channelIds.size} channels" } launch { watchSwapInWallet() } launch { @@ -237,8 +240,7 @@ class Peer( logger.info { "checking for timed out htlcs for channels: ${channelIds.joinToString(", ")}" } channelIds.forEach { input.send(WrappedChannelCommand(it, ChannelCommand.Commitment.CheckHtlcTimeout)) } } - val swapInManager = SwapInManager(bootChannels, logger) - run(swapInManager) + run() } launch { var previousState = connectionState.value @@ -683,14 +685,14 @@ class Peer( Secp256k1DHFunctions, Chacha20Poly1305CipherFunctions, SHA256HashFunctions ) - private suspend fun run(swapInManager: SwapInManager) { + private suspend fun run() { logger.info { "peer is active" } for (event in input) { - processEvent(swapInManager, event) + processEvent(event) } } - private suspend fun processEvent(swapInManager: SwapInManager, cmd: PeerCommand) { + private suspend fun processEvent(cmd: PeerCommand) { when (cmd) { is BytesReceived -> { val msg = try { @@ -1054,7 +1056,7 @@ class Peer( } } is WalletCommand -> { - swapInManager.process(cmd)?.let { input.send(it) } + swapInManager?.process(cmd)?.let { input.send(it) } } is Disconnected -> { logger.warning { "disconnecting channels" }