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 Dec 2, 2020
1 parent 664f77d commit c9f3980
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions chain/bitcoind_conn.go
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,32 @@ 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
btcInWarmup := false
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 == btcjson.ErrRPCInWarmup {
// Error code -28 indicates the bitcoind rpc is warming up.
// We will wait and recheck periodically until rpc is ready.
if btcInWarmup == false {
btcInWarmup = true
log.Info("Waiting for bitcoind RPC to finish warming up...")
}
time.Sleep(time.Second * 1)
} else {
return err
}
}
if btcInWarmup == true {
log.Info("Bitcoind finished warming up and RPC is ready")
}
if net != c.chainParams.Net {
return fmt.Errorf("expected network %v, got %v",
Expand Down

0 comments on commit c9f3980

Please sign in to comment.