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 issue: lightningnetwork/lnd#1533
  • Loading branch information
sangaman committed Feb 18, 2020
1 parent 704cd18 commit 5261bc0
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions chain/bitcoind_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync/atomic"
"time"

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
Expand Down Expand Up @@ -139,10 +140,24 @@ func (c *BitcoindConn) Start() error {
return nil
}

var (
net wire.BitcoinNet
err error
)

// Verify that the node is running on the expected network.
net, err := c.getCurrentNet()
if err != nil {
return err
for {
net, err = c.getCurrentNet()
if err == nil {
// No error means bitcoind is responsive and we can proceed.
break
} else if err.(*btcjson.RPCError).Code == -28 {
// Error code -28 indicates the bitcoind rpc is warming up.
// We will wait and recheck periodically until rpc is ready.
time.Sleep(time.Second * 1)
} else {
return err
}
}
if net != c.chainParams.Net {
return fmt.Errorf("expected network %v, got %v",
Expand Down

0 comments on commit 5261bc0

Please sign in to comment.