Skip to content

Commit

Permalink
Update Peer to notify all channels that a peer was disconnected
Browse files Browse the repository at this point in the history
 - Is this necessary? will closing the socket always cause a `Disconnected` message to be sent to the Peer?
  • Loading branch information
remyers committed Nov 8, 2023
1 parent 02adb84 commit 394b3d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}

}

0 comments on commit 394b3d8

Please sign in to comment.