From aeac1a294a34a403acfe6b36eb370a7cb3e72dc8 Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Tue, 18 Feb 2020 01:53:36 -0500 Subject: [PATCH] chain: wait for bitcoind rpc to warm up This modifies the `Start` method to handle the `RPC_IN_WARMUP` error code of `-28` by retrying the rpc call to determine the current network every second until it either succeeds or fails with a different code. The current behavior fails and terminates the connection upon receiving this error code. This change allows for connecting to a recently started bitcoind node and starting the client while bitcoind is still warming up. Related issues: https://github.com/lightningnetwork/lnd/issues/1533 & https://github.com/ExchangeUnion/xud-docker/issues/195 --- chain/bitcoind_conn.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/chain/bitcoind_conn.go b/chain/bitcoind_conn.go index 3b1fdd2da5..823a59b17a 100644 --- a/chain/bitcoind_conn.go +++ b/chain/bitcoind_conn.go @@ -147,9 +147,30 @@ func NewBitcoindConn(cfg *BitcoindConfig) (*BitcoindConn, error) { } // Verify that the node is running on the expected network. - net, err := getCurrentNet(client) - if err != nil { - return nil, err + var net wire.BitcoinNet + + btcInWarmup := false + for { + net, err = getCurrentNet(client) + if err == nil { + // No error means bitcoind is responsive and we can proceed. + break + } + if rpcErr, ok := err.(*btcjson.RPCError); ok && + rpcErr.Code == btcjson.ErrRPCInWarmup { + // Error code -28 indicates the bitcoind rpc is warming up. + // We will wait and recheck periodically until rpc is ready. + if !btcInWarmup { + btcInWarmup = true + log.Info("Waiting for bitcoind RPC to finish warming up...") + } + time.Sleep(time.Second * 1) + } else { + return nil, err + } + } + if btcInWarmup { + log.Info("Bitcoind finished warming up and RPC is ready") } if net != cfg.ChainParams.Net { return nil, fmt.Errorf("expected network %v, got %v",