@@ -41,7 +41,8 @@ sealed class ChannelEvent {
4141 val localParams : LocalParams ,
4242 val remoteInit : Init ,
4343 val channelFlags : Byte ,
44- val channelVersion : ChannelVersion
44+ val channelVersion : ChannelVersion ,
45+ val channelOrigin : ChannelOrigin ? = null
4546 ) : ChannelEvent() {
4647 init {
4748 require(channelVersion.hasStaticRemotekey) { " channel version $channelVersion is invalid (static_remote_key is not set)" }
@@ -91,6 +92,8 @@ sealed class ChannelAction {
9192 data class HtlcInfo (val channelId : ByteVector32 , val commitmentNumber : Long , val paymentHash : ByteVector32 , val cltvExpiry : CltvExpiry )
9293 data class StoreHtlcInfos (val htlcs : List <HtlcInfo >) : Storage()
9394 data class GetHtlcInfos (val revokedCommitTxId : ByteVector32 , val commitmentNumber : Long ) : Storage()
95+ data class StoreIncomingAmount (val amount : MilliSatoshi , val origin : ChannelOrigin ? ) : Storage()
96+ data class StoreOutgoingAmount (val amount : MilliSatoshi ) : Storage()
9497 }
9598
9699 data class ProcessIncomingHtlc (val add : UpdateAddHtlc ) : ChannelAction()
@@ -152,7 +155,20 @@ sealed class ChannelState {
152155 fun process (event : ChannelEvent ): Pair <ChannelState , List <ChannelAction >> {
153156 return try {
154157 val (newState, actions) = processInternal(event)
155- Pair (newState, newState.updateActions(actions))
158+ val actions1 = when {
159+ this is WaitForFundingCreated && newState is WaitForFundingConfirmed -> {
160+ actions + ChannelAction .Storage .StoreIncomingAmount (pushAmount, channelOrigin)
161+ }
162+ // we only want to fire the PaymentSent event when we transition to Closing for the first time
163+ this is WaitForInit && newState is Closing -> actions
164+ this is Closing && newState is Closing -> actions
165+ this is ChannelStateWithCommitments && newState is Closing -> {
166+ actions + ChannelAction .Storage .StoreOutgoingAmount (this .commitments.localCommit.spec.toLocal)
167+ }
168+ else -> actions
169+ }
170+ val actions2 = newState.updateActions(actions1)
171+ Pair (newState, actions2)
156172 } catch (t: Throwable ) {
157173 handleLocalError(event, t)
158174 }
@@ -174,6 +190,7 @@ sealed class ChannelState {
174190 it is ChannelAction .Message .Send && it.message is RevokeAndAck -> it.copy(message = it.message.copy(channelData = Serialization .encrypt(privateKey.value, this )))
175191 it is ChannelAction .Message .Send && it.message is ClosingSigned -> it.copy(message = it.message.copy(channelData = Serialization .encrypt(privateKey.value, this )))
176192 else -> it
193+
177194 }
178195 }
179196 else -> actions
@@ -544,10 +561,10 @@ data class WaitForInit(override val staticParams: StaticParams, override val cur
544561 // In order to allow TLV extensions and keep backwards-compatibility, we include an empty upfront_shutdown_script.
545562 // See https://github.com/lightningnetwork/lightning-rfc/pull/714.
546563 tlvStream = TlvStream (
547- if (event.channelVersion.isSet( ChannelVersion . ZERO_RESERVE_BIT )) {
548- listOf (ChannelTlv .UpfrontShutdownScript (ByteVector .empty), ChannelTlv . ChannelVersionTlv (event.channelVersion ))
549- } else {
550- listOf ( ChannelTlv .UpfrontShutdownScript ( ByteVector .empty ))
564+ buildList {
565+ add (ChannelTlv .UpfrontShutdownScript (ByteVector .empty))
566+ if (event.channelVersion.isSet( ChannelVersion . ZERO_RESERVE_BIT )) add( ChannelTlv . ChannelVersionTlv (event.channelVersion))
567+ if (event.channelOrigin != null ) add( ChannelTlv .ChannelOriginTlv (event.channelOrigin ))
551568 }
552569 )
553570 )
@@ -1108,6 +1125,7 @@ data class WaitForOpenChannel(
11081125 return Pair (Aborted (staticParams, currentTip, currentOnChainFeerates), listOf (ChannelAction .Message .Send (Error (temporaryChannelId, err.value.message))))
11091126 }
11101127 }
1128+ val channelOrigin = event.message.tlvStream.records.filterIsInstance<ChannelTlv .ChannelOriginTlv >().firstOrNull()?.channelOrigin
11111129
11121130 val fundingPubkey = keyManager.fundingPublicKey(localParams.fundingKeyPath).publicKey
11131131 val channelKeyPath = keyManager.channelKeyPath(localParams, channelVersion)
@@ -1160,6 +1178,7 @@ data class WaitForOpenChannel(
11601178 event.message.firstPerCommitmentPoint,
11611179 event.message.channelFlags,
11621180 channelVersion,
1181+ channelOrigin,
11631182 accept
11641183 )
11651184 Pair (nextState, listOf (ChannelAction .Message .Send (accept)))
@@ -1198,6 +1217,7 @@ data class WaitForFundingCreated(
11981217 val remoteFirstPerCommitmentPoint : PublicKey ,
11991218 val channelFlags : Byte ,
12001219 val channelVersion : ChannelVersion ,
1220+ val channelOrigin : ChannelOrigin ? ,
12011221 val lastSent : AcceptChannel
12021222) : ChannelState() {
12031223 override fun processInternal (event : ChannelEvent ): Pair <ChannelState , List <ChannelAction >> {
@@ -1271,7 +1291,16 @@ data class WaitForFundingCreated(
12711291 logger.info { " c:$channelId will wait for $fundingMinDepth confirmations" }
12721292 val watchSpent = WatchSpent (channelId, commitInput.outPoint.txid, commitInput.outPoint.index.toInt(), commitments.commitInput.txOut.publicKeyScript, BITCOIN_FUNDING_SPENT )
12731293 val watchConfirmed = WatchConfirmed (channelId, commitInput.outPoint.txid, commitments.commitInput.txOut.publicKeyScript, fundingMinDepth.toLong(), BITCOIN_FUNDING_DEPTHOK )
1274- val nextState = WaitForFundingConfirmed (staticParams, currentTip, currentOnChainFeerates, commitments, null , currentBlockHeight.toLong(), null , Either .Right (fundingSigned))
1294+ val nextState = WaitForFundingConfirmed (
1295+ staticParams,
1296+ currentTip,
1297+ currentOnChainFeerates,
1298+ commitments,
1299+ null ,
1300+ currentBlockHeight.toLong(),
1301+ null ,
1302+ Either .Right (fundingSigned)
1303+ )
12751304 val actions = listOf (
12761305 ChannelAction .Blockchain .SendWatch (watchSpent),
12771306 ChannelAction .Blockchain .SendWatch (watchConfirmed),
0 commit comments