Skip to content

Commit

Permalink
chain: wait for bitcoind rpc to warm up
Browse files Browse the repository at this point in the history
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: lightningnetwork/lnd#1533 &
https://github.com/ExchangeUnion/xud-docker/issues/195
  • Loading branch information
sangaman committed Jun 10, 2021
1 parent 6ab9b61 commit aeac1a2
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions chain/bitcoind_conn.go
Expand Up @@ -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",
Expand Down

0 comments on commit aeac1a2

Please sign in to comment.