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 219c94fb..ceed8ce6 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 @@ -129,12 +129,15 @@ class GroupLightningRegtestJvmTest { return (result["total_amount"]!!.jsonPrimitive.content.toBigDecimal() * 100_000_000.toBigDecimal()).toLong() } - /** Waits for [condition], mining a block whenever the mempool holds something, so funding and closing confirm. */ - private suspend fun mineUntil(what: String, timeout: Duration = 3.minutes, condition: suspend () -> T?): T = try { + /** + * Waits for [condition], mining a block whenever the mempool holds something, so funding and closing + * confirm -- or every pass when [confirming], for what needs more than one confirmation, as a splice does. + */ + private suspend fun mineUntil(what: String, timeout: Duration = 3.minutes, confirming: Boolean = false, condition: suspend () -> T?): T = try { withTimeout(timeout) { var result = condition() while (result == null) { - if (mempoolSize() > 0) mine(1) + if (confirming || mempoolSize() > 0) mine(1) delay(1.seconds) result = condition() } @@ -275,6 +278,29 @@ class GroupLightningRegtestJvmTest { assertTrue(channel.oursMsat in 90_000_000..100_000_000, "the group holds the payment, less the LSP's fee: ${channel.oursMsat}") assertTrue(received.count { it!!.checks.single().agrees == true } >= 2, "the members who co-signed agree with the operator") + // Buy liquidity (docs/group-lightning-liquidity.md, Phase 8): a member prices 200,000 sats of inbound + // from the operator's snapshot, the group authorises it, the operator's node splices, and every + // co-signer checks the first commitment on the new output and the shared input before the group signs. + val priced = mineUntil("the LSP's prices in a member's snapshot", 1.minutes) { report(all[3])?.snapshot?.takeIf { it.fundingRates != null && it.feeratePerByte != null } } + val price = com.machankura.bennu.compose.wallet.lightning.LiquidityPrice.price(priced.fundingRates!!, 200_000, priced.feeratePerByte!!)!! + val buy = GroupLightningAuthorisations.prepareBuyLiquidity(wallet, channel.channelId, channel.capacitySat, 200_000, com.machankura.bennu.compose.wallet.lightning.LiquidityPrice.defaultCap(price)).getOrThrow() + signed(all, all[2], GroupLightningAuthorisations.proposeBuyLiquidity(all[2].db, all[2].room, all[2].publicKey, buy).id) + mineUntil("eclair to see the chain's tip", 1.minutes) { lsp("getinfo").jsonObject["blockHeight"]!!.jsonPrimitive.long.takeIf { it >= btc("getblockcount").jsonPrimitive.long } } + node.buyAuthorised() + val bought = mineUntil("every member to see the purchase locked", 4.minutes, confirming = true) { + all.map { report(it) }.takeIf { reports -> reports.all { r -> r?.snapshot?.purchases?.singleOrNull()?.state == com.machankura.bennu.compose.wallet.lightning.WalletWire.LOCKED } } + } + val spliced = bought.first()!!.snapshot.channels.single() + println("REGTEST bought: ${spliced.capacitySat} sats now, for ${bought.first()!!.snapshot.purchases.single().feeSat} sats; estimated ${price.totalSat}") + assertEquals(channel.channelId, spliced.channelId, "the same channel, grown") + assertTrue(spliced.capacitySat > channel.capacitySat + 190_000, "the channel grew by what was bought: ${spliced.capacitySat}") + assertTrue(bought.first()!!.snapshot.purchases.single().feeSat!! <= buy.maxFeeSat) + all.forEach { d -> assertEquals(0, fixture.lines(d, ChatMessage.TYPE_LIGHTNING_REFUSED).size, "device ${d.index} refused part of an honest purchase") } + // And a payment the channel had no room for before arrives over it, with no on-the-fly splice. + receive(all, 50_000) + val after = mineUntil("the 50,000 sats in a member's snapshot") { report(all[0])?.snapshot?.channels?.singleOrNull()?.takeIf { it.oursMsat > spliced.oursMsat + 45_000_000 } } + assertEquals(spliced.capacitySat, after.capacitySat, "no splice was needed to receive it") + // Pay: the group authorises paying the payer, and the operator's node pays it. val owed = payer("createinvoice", mapOf("description" to "back", "amountMsat" to "20000000")).jsonObject val pay = GroupLightningAuthorisations.prepare(wallet, owed["serialized"]!!.jsonPrimitive.content, null, 2_000_000).getOrThrow() @@ -328,7 +354,8 @@ class GroupLightningRegtestJvmTest { .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(3, count(ChatMessage.TYPE_LIGHTNING_PAYMENT_RECEIVED), "device ${d.index}: the three payments in") + assertEquals(1, count(ChatMessage.TYPE_LIGHTNING_LIQUIDITY_ADDED), "device ${d.index}: the liquidity bought") 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}") diff --git a/docs/group-lightning-liquidity.md b/docs/group-lightning-liquidity.md index 37ca0d7b..df5a83e2 100644 --- a/docs/group-lightning-liquidity.md +++ b/docs/group-lightning-liquidity.md @@ -36,6 +36,7 @@ differently, this table says so. | 5 | done: a ledger per funding output (schema 35, `IcebergChannelLedger` keyed by output, with `fundingTxIndex`, `parentOutPoint` and `purchaseId`), `CoSignerPolicy.openSplice`, the shared input checked against S1 to S6, and a payment that settles on both live commitments spending its budget once. `GroupLightningSpliceCoSigningJvmTest`, seven four-device tests on regtest and on mainnet, among them a purchase the script interpreter accepts | | 6 | done: `PeerHandle.fundingRates`, `feeratePerByte` and `buyLiquidity`, `GroupLightningNode.buyAuthorised` on the node's watch, and the snapshot's rates, feerate and purchases. Four tests with the stand-in peer in `GroupLightningLiquidityJvmTest` | | 7 | done, behind `GroupLightningRollout.LIQUIDITY_ENABLED`, which is off until Phase 10: `GroupLightningLiquidityScreen` and its route, view model and state; *Add liquidity* on the wallet's balance card, or the reason it is not there; a purchase's state on its channel row; *Inbound liquidity added* in the payment list; and two transcript lines from the snapshot, added and failed. `LiquidityPrice` works out the price on the member's device, and matches what eclair charged in Phase 0 to the sat. Tests: `LiquidityPriceJvmTest`, and two more cases in `LightningPaymentRowJvmTest`. `m3Audit` meets every budget | +| 8 | **run**, in `GroupLightningRegtestJvmTest`, between *receive* and *pay*: a member prices 200,000 sats from the operator's snapshot, the group authorises it at the default cap, the operator's node splices, every co-signer checks the splice and none refuses; the channel grows from 100,000 to 299,779 sats for 2,521 sats, the figure the member's device estimated; a 50,000-sat payment then arrives with no on-the-fly splice; and *pay*, *close* (124,295 sats to the onchain wallet) and *sweep* run on the spliced channel. Sixty seconds from a fresh network | | what the plan said | what it turned out to be | |---|---| @@ -60,6 +61,7 @@ differently, this table says so. | the payment row comes from the snapshot's `kind` | from a nullable `liquiditySat` on `GroupLightningPaymentRow`. It went into schema 35, which this branch made and nothing installed has, rather than a 36 | | Phase 10's gate | added here, off, so the screen and the node's purchases ship dark until the regtest run of Phase 8 passes. Co-signers check a purchase either way | | the screen's JVM tests cover each reason, the fee lines and the payment row | the reasons, the price and the payment row. The form itself is not rendered in a test: it needs the view model over a repository, and what it shows is `LiquidityPrice`'s, which is | +| Phase 8: a device stopped during the splice signs the next payment | not run: a device that misses a commitment is out of that channel's quorum whatever the commitment is, and that is the marks plan's to repair (Phase 5's row above) | | S4: the new funding amount is at least the old one plus the amount less `purchase.fees.miningFee` | less **our own share of the splice's mining fee**, which is the splice's negative local contribution. `purchase.fees.miningFee` is the LSP's inputs' share, which the group pays from its balance with the service fee, in the first commitment on the new output. Phoenix's estimate (`ChannelManagementFees.miningFee`) is the two shares together, so `estimate.total = -localContribution + purchase.fees.total`, and the authorisation's cap covers both | ### What Phase 0 found