From 94385d2f4a603b9869e348b49112c93b3985b66e Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Tue, 27 Oct 2026 09:01:18 +0200 Subject: [PATCH] fix(lightning): a channel's close is not a payment sent, and a sent amount leaves out its fee Found by the regtest run, now that it checks the transcript. lightning-kmp files a channel's close as an outgoing payment with no payment hash, which wrote "sent" beside each closed line, and an outgoing payment's amount includes its fee, so 20,000 sats with an 84-sat fee read as 20,084 sent. GroupLightningRegtestJvmTest now asserts every device's lines: one per channel opened and closed, one per payment, and no raw JSON, refusal or alarm. Co-Authored-By: Claude Opus 5.5 --- .../compose/managers/GroupLightningReports.kt | 8 +++++- .../managers/GroupLightningRegtestJvmTest.kt | 25 +++++++++++++++++++ .../managers/GroupLightningReportsJvmTest.kt | 9 ++++--- docs/group-lightning-transcript.md | 13 ++++++++-- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupLightningReports.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupLightningReports.kt index 5f358b58..3fa21198 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupLightningReports.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/GroupLightningReports.kt @@ -147,6 +147,10 @@ object GroupLightningReports { snapshot.payments.forEach { payment -> val completedAt = payment.completedAt?.let(Instant::fromEpochMilliseconds) ?: return@forEach + // lightning-kmp files a channel's close as an outgoing payment, with no payment hash: every + // Lightning payment has one. The channel's closed line already says it, and a second line + // would read as the group having paid the money away. Found by the regtest run. + if (!payment.incoming && payment.paymentHash == null) return@forEach val (type, content) = when { payment.incoming && payment.succeeded -> ChatMessage.TYPE_LIGHTNING_PAYMENT_RECEIVED to buildString { append("The group's Lightning wallet received ${sats(payment.amountMsat)}") @@ -154,8 +158,10 @@ object GroupLightningReports { append(".") } payment.incoming -> return@forEach + // An outgoing payment's amount is what left the wallet, its fee included; the line + // says what reached the recipient and the fee beside it. payment.succeeded -> ChatMessage.TYPE_LIGHTNING_PAYMENT_SENT to buildString { - append("The group's Lightning wallet sent ${sats(payment.amountMsat)}") + append("The group's Lightning wallet sent ${sats(payment.amountMsat - payment.feesMsat)}") if (payment.feesMsat > 0) append(", with ${Satoshi((payment.feesMsat + 999) / 1000).grouped()} sats in fees") append(".") } diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupLightningRegtestJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupLightningRegtestJvmTest.kt index cfc230de..219c94fb 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupLightningRegtestJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupLightningRegtestJvmTest.kt @@ -1,5 +1,6 @@ package com.machankura.bennu.compose.managers +import com.machankura.bennu.compose.database.model.ChatMessage import co.touchlab.kermit.Severity import co.touchlab.kermit.StaticConfig import co.touchlab.kermit.platformLogWriter @@ -312,6 +313,30 @@ class GroupLightningRegtestJvmTest { assertTrue(swept - beforeSweep > 70_000, "the sweep paid the onchain wallet") assertNotNull(sweeper.db.groupTransactionDao().getByChatRoomId(fixture.roomId).firstOrNull { it.confirmedAt != null || it.seenAt != null }) assertEquals(1, held.size) + + // The room's account of all that, on every device (docs/group-lightning-transcript.md, + // Phase 5): one line per fact however many snapshots repeated it, and no raw JSON. Waited + // for, since the last snapshots are still reaching the members. + val channels = setOf(channel.channelId, second.channelId) + mineUntil("every member to be told both channels closed") { + all.all { d -> fixture.lines(d, ChatMessage.TYPE_LIGHTNING_CHANNEL_CLOSED).mapNotNull { it.lightningFactKey }.toSet() == channels }.takeIf { it } + } + all.forEach { d -> + suspend fun count(type: String) = fixture.lines(d, type).size + d.db.chatMessageDao().getChatMessagesByChatRoomId(fixture.roomId).map { it.chatMessage } + .filter { it.messageType in ChatMessage.LIGHTNING_TYPES || it.messageType in ChatMessage.GROUP_LIGHTNING_SIGNED_TYPES } + .forEach { println("REGTEST line, device ${d.index}: ${it.messageType} ${it.content}") } + assertEquals(2, count(ChatMessage.TYPE_LIGHTNING_CHANNEL_OPENED), "device ${d.index}: one opened line per channel") + assertEquals(2, count(ChatMessage.TYPE_LIGHTNING_CHANNEL_CLOSED), "device ${d.index}: one closed line per channel") + assertEquals(2, count(ChatMessage.TYPE_LIGHTNING_PAYMENT_RECEIVED), "device ${d.index}: the two payments in") + assertEquals(1, count(ChatMessage.TYPE_LIGHTNING_PAYMENT_SENT), "device ${d.index}: the payment out") + assertEquals(1, count(ChatMessage.TYPE_LIGHTNING_SEED_SHARED), "device ${d.index}") + assertEquals(1, count(ChatMessage.TYPE_LIGHTNING_INVOICE_REQUESTED), "device ${d.index}") + assertEquals(1, count(ChatMessage.TYPE_LIGHTNING_INVOICE_ISSUED), "device ${d.index}") + assertEquals(0, count(ChatMessage.TYPE_LIGHTNING_REFUSED), "device ${d.index}: an honest operator is never refused") + assertEquals(0, count(ChatMessage.TYPE_LIGHTNING_EQUIVOCATION), "device ${d.index}") + assertEquals(0, count("unsupported"), "device ${d.index}: no raw JSON in the room") + } node.stop() } } diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupLightningReportsJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupLightningReportsJvmTest.kt index 2f0c3dcb..598aa447 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupLightningReportsJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupLightningReportsJvmTest.kt @@ -226,11 +226,14 @@ class GroupLightningReportsJvmTest { @Test fun `what the snapshots say happened is said once, on every device`() = runBlocking { val (all, node) = running() - val sent = WalletWire.PaymentView("p2", incoming = false, amountMsat = 5_000_000, feesMsat = 1_500, completedAt = 1_700_000_100_000, succeeded = true) - val failed = WalletWire.PaymentView("p3", incoming = false, amountMsat = 7_000_000, feesMsat = 0, completedAt = 1_700_000_200_000, succeeded = false) + // As lightning-kmp reports them: an outgoing amount includes its fee, and a channel's close is + // an outgoing payment with no payment hash. + val sent = WalletWire.PaymentView("p2", incoming = false, amountMsat = 5_001_500, feesMsat = 1_500, paymentHash = "ab".repeat(32), completedAt = 1_700_000_100_000, succeeded = true) + val close = WalletWire.PaymentView("p5", incoming = false, amountMsat = 400_000_000, feesMsat = 0, completedAt = 1_700_000_300_000, succeeded = true) + val failed = WalletWire.PaymentView("p3", incoming = false, amountMsat = 7_000_000, feesMsat = 0, paymentHash = "cd".repeat(32), completedAt = 1_700_000_200_000, succeeded = false) val pending = WalletWire.PaymentView("p4", incoming = true, amountMsat = 1_000_000, feesMsat = 0, completedAt = null, succeeded = false) nodes.single().channels = listOf(channel) - nodes.single().payments += listOf(payment, sent, failed, pending) + nodes.single().payments += listOf(payment, sent, failed, pending, close) node.report(term = 1) fixture.pump(all, setOf(GroupLightningEvents.SNAPSHOT)) // The same facts again, and then the channel closing. diff --git a/docs/group-lightning-transcript.md b/docs/group-lightning-transcript.md index 4205380f..cd465737 100644 --- a/docs/group-lightning-transcript.md +++ b/docs/group-lightning-transcript.md @@ -24,8 +24,17 @@ build chose differently the table below says so. Every phase was checked with `:composeApp:jvmTest` (1,598 tests; one, `IdentitySwitchJvmTest`, timed out under the full run's load and passes alone), -`:composeApp:compileDebugKotlinAndroid` and `m3-audit.sh --check`. The four-device -regtest run and a look at a room on a device have not been done. +`:composeApp:compileDebugKotlinAndroid` and `m3-audit.sh --check`. + +The regtest run (`GroupLightningRegtestJvmTest`) now checks the transcript too: on every +device, one opened and one closed line per channel, one line per payment, the seed, the +invoice pair, and no raw JSON, refusal or alarm. Its first run found two things Phase 5's +unit test had not: lightning-kmp files a channel's close as an outgoing payment with no +payment hash, which wrote a "sent" line beside each closed one, and an outgoing payment's +amount includes its fee, so 20,000 sats with an 84-sat fee read as 20,084 sent. Both are +fixed, and the unit test's payments are now shaped as lightning-kmp reports them. The +wallet screen's payment list reads the same two fields the same way and still shows both; +that is the screen's, and not changed here. A look at a room on a device has not been done. Read [group-lightning-wallet.md](./group-lightning-wallet.md) for the wallet itself and