@@ -73,6 +73,7 @@ class Peer(val nodeParams: NodeParams,
7373
7474 context.system.eventStream.subscribe(self, classOf [CurrentFeerates ])
7575 context.system.eventStream.subscribe(self, classOf [CurrentBlockHeight ])
76+ context.system.eventStream.subscribe(self, classOf [LocalChannelDown ])
7677
7778 startWith(INSTANTIATING , Nothing )
7879
@@ -90,7 +91,7 @@ class Peer(val nodeParams: NodeParams,
9091 } else {
9192 None
9293 }
93- goto(DISCONNECTED ) using DisconnectedData (channels, activeChannels = 0 , PeerStorage (peerStorageData, written = true )) // when we restart, we will attempt to reconnect right away, but then we'll wait
94+ goto(DISCONNECTED ) using DisconnectedData (channels, activeChannels = Set .empty , PeerStorage (peerStorageData, written = true )) // when we restart, we will attempt to reconnect right away, but then we'll wait
9495 }
9596
9697 when(DISCONNECTED ) {
@@ -144,11 +145,11 @@ class Peer(val nodeParams: NodeParams,
144145 d.peerStorage.data.foreach(nodeParams.db.peers.updateStorage(remoteNodeId, _))
145146 stay() using d.copy(peerStorage = d.peerStorage.copy(written = true ))
146147
147- case Event (ChannelActivated , d : DisconnectedData ) =>
148- stay() using d.copy(activeChannels = d.activeChannels + 1 )
148+ case Event (e : ChannelReadyForPayments , d : DisconnectedData ) =>
149+ stay() using d.copy(activeChannels = d.activeChannels + e.channelId )
149150
150- case Event (ChannelDeactivated , d : DisconnectedData ) =>
151- stay() using d.copy(activeChannels = d.activeChannels - 1 )
151+ case Event (e : LocalChannelDown , d : DisconnectedData ) =>
152+ stay() using d.copy(activeChannels = d.activeChannels - e.channelId )
152153 }
153154
154155 when(CONNECTED ) {
@@ -423,7 +424,7 @@ class Peer(val nodeParams: NodeParams,
423424 }
424425 stay()
425426
426- case Event (e : ChannelReadyForPayments , _ : ConnectedData ) =>
427+ case Event (e : ChannelReadyForPayments , d : ConnectedData ) =>
427428 pendingOnTheFlyFunding.foreach {
428429 case (paymentHash, pending) =>
429430 pending.status match {
@@ -439,7 +440,10 @@ class Peer(val nodeParams: NodeParams,
439440 }
440441 }
441442 }
442- stay()
443+ stay() using d.copy(activeChannels = d.activeChannels + e.channelId)
444+
445+ case Event (e : LocalChannelDown , d : ConnectedData ) =>
446+ stay() using d.copy(activeChannels = d.activeChannels - e.channelId)
443447
444448 case Event (msg : HasChannelId , d : ConnectedData ) =>
445449 d.channels.get(FinalChannelId (msg.channelId)) match {
@@ -533,26 +537,25 @@ class Peer(val nodeParams: NodeParams,
533537 d.peerConnection forward unknownMsg
534538 stay()
535539
536- case Event (store : PeerStorageStore , d : ConnectedData ) if nodeParams.features.hasFeature(Features .ProvideStorage ) && d.activeChannels > 0 =>
537- // If we don't have any pending write operations, we write the updated peer storage to disk after a delay.
538- // This ensures that when we receive a burst of peer storage updates, we will rate-limit our IO disk operations.
539- // If we already have a pending write operation, we must not reset the timer, otherwise we may indefinitely delay
540- // writing to the DB and may never store our peer's backup.
541- if (d.peerStorage.written) {
542- startSingleTimer(" peer-storage-write" , WritePeerStorage , nodeParams.peerStorageConfig.writeDelay)
540+ case Event (store : PeerStorageStore , d : ConnectedData ) =>
541+ if (nodeParams.features.hasFeature(Features .ProvideStorage ) && d.activeChannels.nonEmpty) {
542+ // If we don't have any pending write operations, we write the updated peer storage to disk after a delay.
543+ // This ensures that when we receive a burst of peer storage updates, we will rate-limit our IO disk operations.
544+ // If we already have a pending write operation, we must not reset the timer, otherwise we may indefinitely delay
545+ // writing to the DB and may never store our peer's backup.
546+ if (d.peerStorage.written) {
547+ startSingleTimer(" peer-storage-write" , WritePeerStorage , nodeParams.peerStorageConfig.writeDelay)
548+ }
549+ stay() using d.copy(peerStorage = PeerStorage (Some (store.blob), written = false ))
550+ } else {
551+ log.debug(" ignoring peer storage (feature={}, channels={})" , nodeParams.features.hasFeature(Features .ProvideStorage ), d.activeChannels.mkString(" ," ))
552+ stay()
543553 }
544- stay() using d.copy(peerStorage = PeerStorage (Some (store.blob), written = false ))
545554
546555 case Event (WritePeerStorage , d : ConnectedData ) =>
547556 d.peerStorage.data.foreach(nodeParams.db.peers.updateStorage(remoteNodeId, _))
548557 stay() using d.copy(peerStorage = d.peerStorage.copy(written = true ))
549558
550- case Event (ChannelActivated , d : ConnectedData ) =>
551- stay() using d.copy(activeChannels = d.activeChannels + 1 )
552-
553- case Event (ChannelDeactivated , d : ConnectedData ) =>
554- stay() using d.copy(activeChannels = d.activeChannels - 1 )
555-
556559 case Event (unhandledMsg : LightningMessage , _) =>
557560 log.warning(" ignoring message {}" , unhandledMsg)
558561 stay()
@@ -784,7 +787,7 @@ class Peer(val nodeParams: NodeParams,
784787 context.system.eventStream.publish(PeerDisconnected (self, remoteNodeId))
785788 }
786789
787- private def gotoConnected (connectionReady : PeerConnection .ConnectionReady , channels : Map [ChannelId , ActorRef ], activeChannels : Int , peerStorage : PeerStorage ): State = {
790+ private def gotoConnected (connectionReady : PeerConnection .ConnectionReady , channels : Map [ChannelId , ActorRef ], activeChannels : Set [ ByteVector32 ] , peerStorage : PeerStorage ): State = {
788791 require(remoteNodeId == connectionReady.remoteNodeId, s " invalid nodeId: $remoteNodeId != ${connectionReady.remoteNodeId}" )
789792 log.debug(" got authenticated connection to address {}" , connectionReady.address)
790793
@@ -956,16 +959,16 @@ object Peer {
956959
957960 sealed trait Data {
958961 def channels : Map [_ <: ChannelId , ActorRef ] // will be overridden by Map[FinalChannelId, ActorRef] or Map[ChannelId, ActorRef]
959- def activeChannels : Int
962+ def activeChannels : Set [ ByteVector32 ] // channels that are available to process payments
960963 def peerStorage : PeerStorage
961964 }
962965 case object Nothing extends Data {
963966 override def channels = Map .empty
964- override def activeChannels : Int = 0
967+ override def activeChannels : Set [ ByteVector32 ] = Set .empty
965968 override def peerStorage : PeerStorage = PeerStorage (None , written = true )
966969 }
967- case class DisconnectedData (channels : Map [FinalChannelId , ActorRef ], activeChannels : Int , peerStorage : PeerStorage ) extends Data
968- case class ConnectedData (address : NodeAddress , peerConnection : ActorRef , localInit : protocol.Init , remoteInit : protocol.Init , channels : Map [ChannelId , ActorRef ], activeChannels : Int , currentFeerates : RecommendedFeerates , previousFeerates_opt : Option [RecommendedFeerates ], peerStorage : PeerStorage ) extends Data {
970+ case class DisconnectedData (channels : Map [FinalChannelId , ActorRef ], activeChannels : Set [ ByteVector32 ] , peerStorage : PeerStorage ) extends Data
971+ case class ConnectedData (address : NodeAddress , peerConnection : ActorRef , localInit : protocol.Init , remoteInit : protocol.Init , channels : Map [ChannelId , ActorRef ], activeChannels : Set [ ByteVector32 ] , currentFeerates : RecommendedFeerates , previousFeerates_opt : Option [RecommendedFeerates ], peerStorage : PeerStorage ) extends Data {
969972 val connectionInfo : ConnectionInfo = ConnectionInfo (address, peerConnection, localInit, remoteInit)
970973 def localFeatures : Features [InitFeature ] = localInit.features
971974 def remoteFeatures : Features [InitFeature ] = remoteInit.features
@@ -1080,9 +1083,5 @@ object Peer {
10801083 case class RelayUnknownMessage (unknownMessage : UnknownMessage )
10811084
10821085 case object WritePeerStorage
1083-
1084- case object ChannelActivated
1085-
1086- case object ChannelDeactivated
10871086 // @formatter:on
10881087}
0 commit comments