feat(lightning): the group node acts as requests arrive, and the mainnet run is recorded
On the first mainnet run, most of the wait for an invoice (six seconds) and for an authorised payment to leave (five) was the node's ten-second poll. The node now watches the room's invoice requests (30364) and authorisations (30363, which carry channel actions too), and acts the moment either changes. The poll stays, for expiries and snapshots. Answering, paying and closing each take one lock, so a wakeup, the poll and a direct call never act twice on the same request: payAuthorised checks hasPaid, which suspends, before it marks a payment in hand. Advances lightning-kmp-app to the commit that keeps payment preimages out of a release build's Logcat. The plan doc records the run with ACINQ's mainnet LSP: an on-the-fly 25,000-sat taproot channel, 8,558 sats received, and a 21-sat payment sent. Every co-signer approved every commitment. The doc also notes that a payment larger than the channel's 16,442 sats of inbound would need the splice the co-signers still refuse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
309a486de9
commit
515ddea2be
7 files changed
+106
-13
No files matched your search
+7
@@ -28,6 +28,13 @@ abstract class GroupSignedEventDao {
|
||||
@Query("SELECT * FROM GroupSignedEvent WHERE chatRoomId = :chatRoomId AND kind = :kind ORDER BY createdAt ASC, id ASC")
|
||||
abstract suspend fun getByChatRoomIdAndKind(chatRoomId: String, kind: Kind): List<GroupSignedEvent>
|
||||
|
||||
/**
|
||||
* How many events of one kind a room has signed, as it changes: what the group's Lightning node watches
|
||||
* to act on a payment or channel action the moment the group authorises it, rather than at its next poll.
|
||||
*/
|
||||
@Query("SELECT COUNT(*) FROM GroupSignedEvent WHERE chatRoomId = :chatRoomId AND kind = :kind")
|
||||
abstract fun observeCountByChatRoomIdAndKind(chatRoomId: String, kind: Kind): Flow<Int>
|
||||
|
||||
/**
|
||||
* Every signed event of one kind, whatever room it was signed in.
|
||||
*
|
||||
|
||||
+7
@@ -62,6 +62,13 @@ interface MarmotInnerEventDao {
|
||||
@Query("SELECT * FROM MarmotInnerEvent WHERE chatRoomId = :chatRoomId AND kind IN (:kinds) ORDER BY createdAt ASC")
|
||||
suspend fun getByChatRoomAndKinds(chatRoomId: String, kinds: List<Int>): List<MarmotInnerEvent>
|
||||
|
||||
/**
|
||||
* How many of a room's inner events are of the given kinds, as it changes: what the group's Lightning
|
||||
* node watches to answer an invoice request the moment one arrives, rather than at its next poll.
|
||||
*/
|
||||
@Query("SELECT COUNT(*) FROM MarmotInnerEvent WHERE chatRoomId = :chatRoomId AND kind IN (:kinds)")
|
||||
fun observeCountByChatRoomAndKinds(chatRoomId: String, kinds: List<Int>): Flow<Int>
|
||||
|
||||
@Query("DELETE FROM MarmotInnerEvent WHERE id = :id")
|
||||
suspend fun deleteById(id: String)
|
||||
|
||||
|
||||
+50
-11
@@ -4,6 +4,7 @@ import co.touchlab.kermit.Logger
|
||||
import com.machankura.bennu.compose.database.BennuDatabase
|
||||
import com.machankura.bennu.compose.database.model.intermdiate.LocalChatRoom
|
||||
import com.machankura.bennu.compose.nostr.lightning.GroupLightningAuthorisationEvent
|
||||
import com.machankura.bennu.compose.nostr.lightning.GroupLightningEvents
|
||||
import com.machankura.bennu.compose.nostr.lightning.GroupLightningWalletStatementEvent
|
||||
import com.machankura.bennu.compose.wallet.GroupChainClients
|
||||
import com.machankura.bennu.compose.wallet.lightning.GroupNodeKeyManager
|
||||
@@ -59,11 +60,16 @@ import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.conflate
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
|
||||
/**
|
||||
* The group's Lightning node, on the operator's device (`docs/group-lightning-wallet.md`, Phase 8).
|
||||
@@ -151,6 +157,9 @@ class GroupLightningNode(
|
||||
private var transport: GroupIcebergTransport? = null
|
||||
private var watching: Job? = null
|
||||
private var connecting: Job? = null
|
||||
private var waking: Job? = null
|
||||
/** Held while the node acts on what the room asked of it, so no two callers act on one request at once. */
|
||||
private val acting = Mutex()
|
||||
private var lastReported: Triple<List<WalletWire.ChannelView>, List<WalletWire.PaymentView>, List<GroupSweep.Input>>? = null
|
||||
private var lastReportedAt: kotlin.time.Instant? = null
|
||||
|
||||
@@ -194,6 +203,7 @@ class GroupLightningNode(
|
||||
lastReported = null
|
||||
report(term, force = true)
|
||||
watchForHandover(term)
|
||||
wakeOnRequests()
|
||||
logger.i("The group's node ${wallet.statement.nodeId.take(16)} is running, term $term")
|
||||
return _state.value
|
||||
}
|
||||
@@ -211,6 +221,8 @@ class GroupLightningNode(
|
||||
// First, or the disconnect below would read as a drop and be redialled.
|
||||
connecting?.cancel()
|
||||
connecting = null
|
||||
waking?.cancel()
|
||||
waking = null
|
||||
peer?.disconnect()
|
||||
transport?.stop()
|
||||
peer = null
|
||||
@@ -242,8 +254,8 @@ class GroupLightningNode(
|
||||
}
|
||||
|
||||
/** Makes an invoice for every request in the room that has none yet. */
|
||||
internal suspend fun answerInvoiceRequests() {
|
||||
val handle = peer ?: return
|
||||
internal suspend fun answerInvoiceRequests() = acting.withLock {
|
||||
val handle = peer ?: return@withLock
|
||||
GroupLightningReports.unanswered(database, localChatRoom.chatRoom.id, walletKey).forEach { request ->
|
||||
val bolt11 = handle.createInvoice(request.amountMsat, request.description)
|
||||
GroupLightningReports.answer(database, localChatRoom, userPublicKey, request, bolt11)
|
||||
@@ -257,8 +269,8 @@ class GroupLightningNode(
|
||||
* that fails is not tried again: the group signs a new authorisation for another attempt, and the
|
||||
* co-signers' budgets are what stop either from paying twice.
|
||||
*/
|
||||
internal suspend fun payAuthorised() {
|
||||
val handle = peer ?: return
|
||||
internal suspend fun payAuthorised() = acting.withLock {
|
||||
val handle = peer ?: return@withLock
|
||||
GroupLightningAuthorisations.toPay(database, wallet, now()).forEach { pay ->
|
||||
if (pay.paymentHash in paying || handle.hasPaid(pay.paymentHash)) return@forEach
|
||||
paying += pay.paymentHash
|
||||
@@ -277,12 +289,12 @@ class GroupLightningNode(
|
||||
* the onchain wallet's first address, which is where the channel's own closing script already
|
||||
* points, or by force.
|
||||
*/
|
||||
internal suspend fun closeAuthorised() {
|
||||
val handle = peer ?: return
|
||||
internal suspend fun closeAuthorised() = acting.withLock {
|
||||
val handle = peer ?: return@withLock
|
||||
val open = handle.channels().map { it.channelId }.toSet()
|
||||
val actions = GroupLightningAuthorisations.channelActions(database, localChatRoom.chatRoom.id, wallet.statement, now())
|
||||
if (actions.isEmpty()) return
|
||||
val onchain = GroupWalletManager.walletFor(database, localChatRoom.chatRoom.id, chain, wallet.statement.onchainAccount) ?: return
|
||||
if (actions.isEmpty()) return@withLock
|
||||
val onchain = GroupWalletManager.walletFor(database, localChatRoom.chatRoom.id, chain, wallet.statement.onchainAccount) ?: return@withLock
|
||||
val closeTo = GroupWalletDerivation.derive(onchain.account, GroupWalletDerivation.RECEIVE, 0).pubkeyScript
|
||||
actions.filter { it.channelId in open }.forEach { action ->
|
||||
if (!closing.add("${action.action}:${action.channelId}")) return@forEach
|
||||
@@ -339,13 +351,40 @@ class GroupLightningNode(
|
||||
return@launch
|
||||
}
|
||||
runCatching { report(term) }.onFailure { logger.e("Could not report the wallet to the room", it) }
|
||||
runCatching { answerInvoiceRequests() }.onFailure { logger.e("Could not answer an invoice request", it) }
|
||||
runCatching { payAuthorised() }.onFailure { logger.e("Could not make an authorised payment", it) }
|
||||
runCatching { closeAuthorised() }.onFailure { logger.e("Could not close an authorised channel", it) }
|
||||
act()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Answers invoice requests, and makes the payments and channel actions the group has authorised. Each
|
||||
* takes [acting] itself, so the poll, a wakeup and a direct call never pay or answer the same thing twice.
|
||||
*/
|
||||
private suspend fun act() {
|
||||
runCatching { answerInvoiceRequests() }.onFailure { logger.e("Could not answer an invoice request", it) }
|
||||
runCatching { payAuthorised() }.onFailure { logger.e("Could not make an authorised payment", it) }
|
||||
runCatching { closeAuthorised() }.onFailure { logger.e("Could not close an authorised channel", it) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Acts the moment the room's invoice requests or authorisations change, rather than at the next
|
||||
* [handoverCheck]: on the first mainnet run that poll was most of the wait for an invoice and for a
|
||||
* payment to leave once the group had signed it. The poll still runs, for expiries and snapshots.
|
||||
*/
|
||||
private fun wakeOnRequests() {
|
||||
waking?.cancel()
|
||||
val roomId = localChatRoom.chatRoom.id
|
||||
waking = scope.launch {
|
||||
combine(
|
||||
database.marmotInnerEventDao().observeCountByChatRoomAndKinds(roomId, listOf(GroupLightningEvents.INVOICE_REQUEST)),
|
||||
database.groupSignedEventDao().observeCountByChatRoomIdAndKind(roomId, GroupLightningAuthorisationEvent.KIND),
|
||||
) { requests, authorisations -> requests to authorisations }
|
||||
.distinctUntilChanged()
|
||||
.conflate()
|
||||
.collect { act() }
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/** A lightning-kmp payment as the room hears of it. */
|
||||
internal fun paymentView(payment: WalletPayment, incoming: Boolean) = WalletWire.PaymentView(
|
||||
|
||||
Reference in new issue
Block a user