From bb956e127e419e16566dc3e114fa5091c64c77e5 Mon Sep 17 00:00:00 2001 From: eugene Date: Tue, 2 Aug 2022 12:54:47 -0400 Subject: [PATCH] server+htlcswitch: check waiting-close fwdpkgs in reforwardResponses Previously, the Switch would not check waiting-close channels' fwdpkgs for settles or fails to reforward. This could result in a force close in a rare edge case if a restart occurred at the wrong time. Now, waiting-close fwdpkgs are checked and the issue is avoided. --- htlcswitch/mock.go | 1 + htlcswitch/switch.go | 10 ++++++++-- server.go | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index b209e2c51..964584320 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -194,6 +194,7 @@ func initSwitchWithDB(startingHeight uint32, db *channeldb.DB) (*Switch, error) cfg := Config{ DB: db, FetchAllOpenChannels: db.ChannelStateDB().FetchAllOpenChannels, + FetchAllChannels: db.ChannelStateDB().FetchAllChannels, FetchClosedChannels: db.ChannelStateDB().FetchClosedChannels, SwitchPackager: channeldb.NewSwitchPackager(), FwdingLog: &mockForwardingLog{ diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index 2e60b8fff..c04a42df2 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -145,6 +145,10 @@ type Config struct { // channels from the channel database. FetchAllOpenChannels func() ([]*channeldb.OpenChannel, error) + // FetchAllChannels is a function that fetches all pending open, open, + // and waiting close channels from the database. + FetchAllChannels func() ([]*channeldb.OpenChannel, error) + // FetchClosedChannels is a function that fetches all closed channels // from the channel database. FetchClosedChannels func( @@ -2083,9 +2087,11 @@ func (s *Switch) reforwardResolutions() error { // reforwardResponses for every known, non-pending channel, loads all associated // forwarding packages and reforwards any Settle or Fail HTLCs found. This is -// used to resurrect the switch's mailboxes after a restart. +// used to resurrect the switch's mailboxes after a restart. This also runs for +// waiting close channels since there may be settles or fails that need to be +// reforwarded before they completely close. func (s *Switch) reforwardResponses() error { - openChannels, err := s.cfg.FetchAllOpenChannels() + openChannels, err := s.cfg.FetchAllChannels() if err != nil { return err } diff --git a/server.go b/server.go index 62527d478..e3b86da6c 100644 --- a/server.go +++ b/server.go @@ -630,6 +630,7 @@ func newServer(cfg *Config, listenAddrs []net.Addr, s.htlcSwitch, err = htlcswitch.New(htlcswitch.Config{ DB: dbs.ChanStateDB, FetchAllOpenChannels: s.chanStateDB.FetchAllOpenChannels, + FetchAllChannels: s.chanStateDB.FetchAllChannels, FetchClosedChannels: s.chanStateDB.FetchClosedChannels, LocalChannelClose: func(pubKey []byte, request *htlcswitch.ChanClose) {