From 394b3d816e995d673e8ebfe0b867a170085502e6 Mon Sep 17 00:00:00 2001 From: Richard Myers Date: Wed, 8 Nov 2023 15:08:40 +0100 Subject: [PATCH] Update Peer to notify all channels that a peer was disconnected - Is this necessary? will closing the socket always cause a `Disconnected` message to be sent to the Peer? --- .../kotlin/fr/acinq/lightning/io/Peer.kt | 3 ++- .../acinq/lightning/io/peer/ConnectionTest.kt | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt index ea7b09962..717aa68fa 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt @@ -287,9 +287,10 @@ class Peer( } } - fun disconnect() { + suspend fun disconnect() { if (this::socket.isInitialized) socket.close() _connectionState.value = Connection.CLOSED(null) + input.send(Disconnected) } // Warning : lateinit vars have to be used AFTER their init to avoid any crashes diff --git a/src/commonTest/kotlin/fr/acinq/lightning/io/peer/ConnectionTest.kt b/src/commonTest/kotlin/fr/acinq/lightning/io/peer/ConnectionTest.kt index ca2e86f5a..b12d4c03f 100644 --- a/src/commonTest/kotlin/fr/acinq/lightning/io/peer/ConnectionTest.kt +++ b/src/commonTest/kotlin/fr/acinq/lightning/io/peer/ConnectionTest.kt @@ -1,13 +1,17 @@ package fr.acinq.lightning.io.peer -import fr.acinq.lightning.channel.states.Offline +import fr.acinq.lightning.Lightning import fr.acinq.lightning.channel.TestsHelper.reachNormal +import fr.acinq.lightning.channel.states.Offline import fr.acinq.lightning.io.Disconnected +import fr.acinq.lightning.io.MessageReceived import fr.acinq.lightning.tests.TestConstants import fr.acinq.lightning.tests.io.peer.newPeer import fr.acinq.lightning.tests.utils.LightningTestSuite import fr.acinq.lightning.tests.utils.runSuspendTest import fr.acinq.lightning.utils.Connection +import fr.acinq.lightning.wire.Stfu +import fr.acinq.lightning.wire.UpdateFulfillHtlc import kotlinx.coroutines.flow.first import kotlin.test.Test import kotlin.test.assertEquals @@ -25,4 +29,17 @@ class ConnectionTest : LightningTestSuite() { assertEquals(Connection.ESTABLISHED, peer.connectionState.value) } + @Test + fun `channel triggers a disconnect from a peer`() = runSuspendTest { + val (alice0, bob0) = reachNormal() + val peer = newPeer(alice0.staticParams.nodeParams, TestConstants.Alice.walletParams, bob0) { channels.addOrUpdateChannel(alice0.state) } + + // alice disconnects from bob if bob sends a forbidden message after stfu + peer.send(MessageReceived(0, Stfu(alice0.channelId, true)) ) + peer.send(MessageReceived(0, UpdateFulfillHtlc(alice0.channelId, 0, Lightning.randomBytes32()) )) + // Wait until alice is Offline + peer.channelsFlow.first { it.values.size == 1 && it.values.all { channelState -> channelState is Offline } } + assertEquals(Connection.CLOSED(null), peer.connectionState.value) + } + } \ No newline at end of file