Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ sealed class ChannelState {
/** A channel state that is persisted to the DB. */
sealed class PersistedChannelState : ChannelState() {
abstract val channelId: ByteVector32
abstract val fundingTxId: TxId
abstract val fundingTxIndex: Long

fun ChannelContext.channelKeys(): ChannelKeys = when (val state = this@PersistedChannelState) {
is WaitForFundingSigned -> state.channelParams.localParams.channelKeys(keyManager)
Expand Down Expand Up @@ -397,6 +399,8 @@ sealed class ChannelStateWithCommitments : PersistedChannelState() {
// Remote nonces that must be used when signing the next remote commitment transaction (one per active commitment).
abstract val remoteNextCommitNonces: Map<TxId, IndividualNonce>
override val channelId: ByteVector32 get() = commitments.channelId
override val fundingTxId: TxId get() = commitments.latest.fundingTxId
override val fundingTxIndex: Long get() = commitments.latest.fundingTxIndex
val isChannelOpener: Boolean get() = commitments.channelParams.localParams.isChannelOpener
val paysCommitTxFees: Boolean get() = commitments.channelParams.localParams.paysCommitTxFees
val remoteNodeId: PublicKey get() = commitments.remoteNodeId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -880,9 +880,9 @@ data class Normal(
// We watch for confirmation in all cases, to allow pruning outdated commitments when transactions confirm.
val fundingScript = action.commitment.commitInput(channelKeys()).txOut.publicKeyScript
val watchConfirmed = WatchConfirmed(channelId, action.commitment.fundingTxId, fundingScript, staticParams.nodeParams.minDepthBlocks, WatchConfirmed.ChannelFundingDepthOk)
val commitments = commitments.add(action.commitment)
val commitments1 = commitments.add(action.commitment)
val remoteNextCommitNonces1 = remoteNextCommitNonces + listOfNotNull(action.nextRemoteCommitNonce?.let { action.commitment.fundingTxId to it }).toMap()
val nextState = [email protected](commitments = commitments, remoteNextCommitNonces = remoteNextCommitNonces1, spliceStatus = SpliceStatus.None)
val nextState = [email protected](commitments = commitments1, remoteNextCommitNonces = remoteNextCommitNonces1, spliceStatus = SpliceStatus.None)
val actions = buildList {
add(ChannelAction.Storage.StoreState(nextState))
action.fundingTx.signedTx?.let { add(ChannelAction.Blockchain.PublishTx(it, ChannelAction.Blockchain.PublishTx.Type.FundingTx)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.acinq.lightning.channel.states

import fr.acinq.bitcoin.ByteVector32
import fr.acinq.bitcoin.PublicKey
import fr.acinq.bitcoin.TxId
import fr.acinq.bitcoin.crypto.Pack
import fr.acinq.bitcoin.utils.Either
import fr.acinq.lightning.ChannelEvents
Expand Down Expand Up @@ -47,6 +48,8 @@ data class WaitForFundingSigned(
val channelOrigin: Origin?,
) : PersistedChannelState() {
override val channelId: ByteVector32 = channelParams.channelId
override val fundingTxId: TxId = signingSession.fundingTxId
override val fundingTxIndex: Long = signingSession.fundingParams.fundingTxIndex

override suspend fun ChannelContext.processInternal(cmd: ChannelCommand): Pair<ChannelState, List<ChannelAction>> {
return when (cmd) {
Expand Down
16 changes: 7 additions & 9 deletions modules/core/src/commonMain/kotlin/fr/acinq/lightning/io/Peer.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.acinq.lightning.io

import fr.acinq.bitcoin.*
import fr.acinq.bitcoin.crypto.musig2.IndividualNonce
import fr.acinq.bitcoin.utils.Either
import fr.acinq.lightning.*
import fr.acinq.lightning.Lightning.randomKey
Expand Down Expand Up @@ -302,7 +301,6 @@ class Peer(
delay(1.minutes)
}
}

}
launch {
suspend fun updateFeerates() {
Expand Down Expand Up @@ -339,8 +337,8 @@ class Peer(
logger.info { "restoring channel ${it.channelId} from local storage" }
val state = WaitForInit
val (state1, actions) = state.process(ChannelCommand.Init.Restore(it))
processActions(it.channelId, peerConnection, actions, state1)
_channels = _channels + (it.channelId to state1)
processActions(it.channelId, peerConnection, actions, state1)
it.channelId
}
logger.info { "restored ${channelIds.size} channels" }
Expand Down Expand Up @@ -940,7 +938,7 @@ class Peer(
}
}
is ChannelAction.Storage.StoreState -> {
logger.info { "storing state=${action.data::class.simpleName}" }
logger.info { "storing state=${action.data::class.simpleName} latestFundingTxId=${action.data.fundingTxId} latestFundingTxIndex=${action.data.fundingTxIndex}" }
db.channels.addOrUpdateChannel(action.data)
if (action.data is Closed) {
updatePeerStorage(nodeParams, channels - channelId, peerConnection, theirInit?.features, logger)
Expand Down Expand Up @@ -1322,8 +1320,8 @@ class Peer(
_channels[msg.channelId]?.let { state ->
val event1 = ChannelCommand.MessageReceived(msg)
val (state1, actions) = state.process(event1)
processActions(msg.channelId, peerConnection, actions, state1)
_channels = _channels + (msg.channelId to state1)
processActions(msg.channelId, peerConnection, actions, state1)
} ?: run {
logger.error { "received ${msg::class.simpleName} for unknown channel ${msg.channelId}" }
peerConnection?.send(Error(msg.channelId, "unknown channel"))
Expand All @@ -1334,8 +1332,8 @@ class Peer(
_channels.values.filterIsInstance<Normal>().find { it.matchesShortChannelId(msg.shortChannelId) }?.let { state ->
val event1 = ChannelCommand.MessageReceived(msg)
val (state1, actions) = state.process(event1)
processActions(state.channelId, peerConnection, actions, state1)
_channels = _channels + (state.channelId to state1)
processActions(state.channelId, peerConnection, actions, state1)
}
}
is WillAddHtlc -> when {
Expand Down Expand Up @@ -1400,8 +1398,8 @@ class Peer(
val state = _channels[cmd.watch.channelId] ?: error("channel ${cmd.watch.channelId} not found")
val event1 = ChannelCommand.WatchReceived(cmd.watch)
val (state1, actions) = state.process(event1)
processActions(cmd.watch.channelId, peerConnection, actions, state1)
_channels = _channels + (cmd.watch.channelId to state1)
processActions(cmd.watch.channelId, peerConnection, actions, state1)
}
}
is OpenChannel -> {
Expand Down Expand Up @@ -1709,14 +1707,14 @@ class Peer(
// this is for all channels
_channels.forEach { (key, value) ->
val (state1, actions) = value.process(cmd.channelCommand)
processActions(key, peerConnection, actions, state1)
_channels = _channels + (key to state1)
processActions(key, peerConnection, actions, state1)
}
} else {
_channels[cmd.channelId]?.let { state ->
val (state1, actions) = state.process(cmd.channelCommand)
processActions(cmd.channelId, peerConnection, actions, state1)
_channels = _channels + (cmd.channelId to state1)
processActions(cmd.channelId, peerConnection, actions, state1)
} ?: logger.error { "received ${cmd.channelCommand::class.simpleName} for an unknown channel ${cmd.channelId}" }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ suspend fun <T> MDCLogger.withMDC(mdc: Map<String, Any>, f: suspend (MDCLogger)
return f(logger)
}



/**
* Utility functions to build MDC for various objects without polluting main classes
*/
Expand Down Expand Up @@ -82,29 +80,33 @@ fun ChannelState.mdc(): Map<String, Any> {
return buildMap {
put("state", state.stateName)
when (state) {
WaitForInit -> {}
is WaitForOpenChannel -> put("temporaryChannelId", state.temporaryChannelId)
is WaitForAcceptChannel -> put("temporaryChannelId", state.temporaryChannelId)
is WaitForFundingCreated -> put("channelId", state.channelId)
is WaitForFundingSigned -> put("channelId", state.channelId)
is ChannelStateWithCommitments -> put("channelId", state.channelId)
is Offline -> put("channelId", state.state.channelId)
is Syncing -> put("channelId", state.state.channelId)
else -> {}
Aborted -> {}
}
when(state) {
is ChannelStateWithCommitments -> {
put("commitments", "active=${state.commitments.active.map { it.fundingTxIndex }} inactive=${state.commitments.inactive.map { it.fundingTxIndex }}")
put("balances", "toLocal=${state.commitments.latest.localCommit.spec.toLocal} toRemote=${state.commitments.latest.localCommit.spec.toRemote} capacity=${state.commitments.latest.fundingAmount}")
}
else -> {}
val commitments = when (state) {
is ChannelStateWithCommitments -> state.commitments
is Offline if state.state is ChannelStateWithCommitments -> state.state.commitments
is Syncing if state.state is ChannelStateWithCommitments -> state.state.commitments
else -> null
}
commitments?.let { commitments ->
put("commitments", "active=${commitments.active.map { it.fundingTxIndex }} inactive=${commitments.inactive.map { it.fundingTxIndex }}")
put("balances", "toLocal=${commitments.latest.localCommit.spec.toLocal} toRemote=${commitments.latest.localCommit.spec.toRemote} capacity=${commitments.latest.fundingAmount}")
}
}
}

fun LightningMessage.mdc(): Map<String, Any> {
val msg = this
return buildMap {
when(msg) {
when (msg) {
is HasTemporaryChannelId -> put("temporaryChannelId", msg.temporaryChannelId)
is HasChannelId -> put("channelId", msg.channelId)
else -> {}
Expand Down