@@ -993,19 +993,19 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
993993 }
994994
995995 s .chanRouter , err = routing .New (routing.Config {
996- SelfNode : selfNode .PubKeyBytes ,
997- RoutingGraph : graphsession .NewRoutingGraph (chanGraph ),
998- Chain : cc .ChainIO ,
999- Payer : s .htlcSwitch ,
1000- Control : s .controlTower ,
1001- MissionControl : s .missionControl ,
1002- SessionSource : paymentSessionSource ,
1003- GetLink : s .htlcSwitch .GetLinkByShortID ,
1004- NextPaymentID : sequencer .NextID ,
1005- PathFindingConfig : pathFindingConfig ,
1006- Clock : clock .NewDefaultClock (),
1007- ApplyChannelUpdate : s .graphBuilder .ApplyChannelUpdate ,
1008- FetchClosedChannels : s . chanStateDB . FetchClosedChannels ,
996+ SelfNode : selfNode .PubKeyBytes ,
997+ RoutingGraph : graphsession .NewRoutingGraph (chanGraph ),
998+ Chain : cc .ChainIO ,
999+ Payer : s .htlcSwitch ,
1000+ Control : s .controlTower ,
1001+ MissionControl : s .missionControl ,
1002+ SessionSource : paymentSessionSource ,
1003+ GetLink : s .htlcSwitch .GetLinkByShortID ,
1004+ NextPaymentID : sequencer .NextID ,
1005+ PathFindingConfig : pathFindingConfig ,
1006+ Clock : clock .NewDefaultClock (),
1007+ ApplyChannelUpdate : s .graphBuilder .ApplyChannelUpdate ,
1008+ ClosedSCIDs : s . fetchClosedChannelSCIDs () ,
10091009 })
10101010 if err != nil {
10111011 return nil , fmt .Errorf ("can't create router: %w" , err )
@@ -4830,3 +4830,52 @@ func shouldPeerBootstrap(cfg *Config) bool {
48304830 // covering the bootstrapping process.
48314831 return ! cfg .NoNetBootstrap && ! isDevNetwork
48324832}
4833+
4834+ // fetchClosedChannelSCIDs returns a set of SCIDs that have their force closing
4835+ // finished.
4836+ func (s * server ) fetchClosedChannelSCIDs () map [lnwire.ShortChannelID ]struct {} {
4837+ // Get a list of closed channels.
4838+ channels , err := s .chanStateDB .FetchClosedChannels (false )
4839+ if err != nil {
4840+ srvrLog .Errorf ("Failed to fetch closed channels: %v" , err )
4841+ return nil
4842+ }
4843+
4844+ // Save the SCIDs in a map.
4845+ closedSCIDs := make (map [lnwire.ShortChannelID ]struct {}, len (channels ))
4846+ for _ , c := range channels {
4847+ // If the channel is not pending, its FC has been finalized.
4848+ if ! c .IsPending {
4849+ closedSCIDs [c .ShortChanID ] = struct {}{}
4850+ }
4851+ }
4852+
4853+ // Double check whether the reported closed channel has indeed finished
4854+ // closing.
4855+ //
4856+ // NOTE: There are misalignments regarding when a channel's FC is
4857+ // marked as finalized. We double check the pending channels to make
4858+ // sure the returned SCIDs are indeed terminated.
4859+ //
4860+ // TODO(yy): fix the misalignments in `FetchClosedChannels`.
4861+ pendings , err := s .chanStateDB .FetchPendingChannels ()
4862+ if err != nil {
4863+ srvrLog .Errorf ("Failed to fetch pending channels: %v" , err )
4864+ return nil
4865+ }
4866+
4867+ for _ , c := range pendings {
4868+ if _ , ok := closedSCIDs [c .ShortChannelID ]; ! ok {
4869+ continue
4870+ }
4871+
4872+ // If the channel is still reported as pending, remove it from
4873+ // the map.
4874+ delete (closedSCIDs , c .ShortChannelID )
4875+
4876+ srvrLog .Warnf ("Channel=%v is prematurely marked as finalized" ,
4877+ c .ShortChannelID )
4878+ }
4879+
4880+ return closedSCIDs
4881+ }
0 commit comments