diff --git a/chainreg/chainregistry.go b/chainreg/chainregistry.go index 02da263912..a1ac3fc845 100644 --- a/chainreg/chainregistry.go +++ b/chainreg/chainregistry.go @@ -504,7 +504,19 @@ func NewPartialChainControl(cfg *Config) (*PartialChainControl, func(), error) { cc.HealthCheck = func() error { _, err := chainConn.RawRequest(cmd, nil) - return err + if err != nil { + return err + } + + // Make sure the bitcoind chain backend maintains a + // healthy connection to the network by checking the + // number of outbound peers. + err = checkOutboundPeers(chainConn) + if err != nil { + return err + } + + return nil } case "btcd": @@ -613,7 +625,19 @@ func NewPartialChainControl(cfg *Config) (*PartialChainControl, func(), error) { // Use a query for our best block as a health check. cc.HealthCheck = func() error { _, _, err := cc.ChainSource.GetBestBlock() - return err + if err != nil { + return err + } + + // Make sure the btcd chain backend maintains a + // healthy connection to the network by checking the + // number of outbound peers. + err = checkOutboundPeers(chainRPC.Client) + if err != nil { + return err + } + + return nil } // If we're not in simnet or regtest mode, then we'll attempt @@ -840,3 +864,28 @@ var ( }, } ) + +// checkOutboundPeers checks the number of outbound peers connected to the +// provided RPC client. If the number of outbound peers is below 6, a warning +// is logged. This function is intended to ensure that the chain backend +// maintains a healthy connection to the network. +func checkOutboundPeers(client *rpcclient.Client) error { + peers, err := client.GetPeerInfo() + if err != nil { + return err + } + + var outboundPeers int + for _, peer := range peers { + if !peer.Inbound { + outboundPeers++ + } + } + + if outboundPeers < 6 { + log.Warnf("The number of outbound peers (%d) is "+ + "below the expected minimum of 6.", outboundPeers) + } + + return nil +} diff --git a/docs/release-notes/release-notes-0.18.0.md b/docs/release-notes/release-notes-0.18.0.md index c4ca138e79..51aadcc4c9 100644 --- a/docs/release-notes/release-notes-0.18.0.md +++ b/docs/release-notes/release-notes-0.18.0.md @@ -263,6 +263,11 @@ bitcoin peers' feefilter values into account](https://github.com/lightningnetwor types](https://github.com/lightningnetwork/lnd/pull/8554) defined in `btcd/rpcclient`. +`[checkOutboundPeers](https://github.com/lightningnetwork/lnd/pull/8576) is + added to `chainHealthCheck` to make sure chain backend `bitcoind` and `btcd` + maintains a healthy connection to the network by checking the number of + outbound peers if they are below 6. + ### Logging * [Add the htlc amount](https://github.com/lightningnetwork/lnd/pull/8156) to contract court logs in case of timed-out HTLCs in order to easily spot dust