Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 82 additions & 3 deletions chainreg/blake2b_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
"os"
"path/filepath"
"syscall"
"time"

"github.com/btcsuite/btcd/btcjson"
Expand Down Expand Up @@ -63,6 +66,16 @@ const (
// repeated while the daemon runs.
DefaultChainIdentityRecheckInterval = 5 * time.Minute

// chainIdentityRetryInterval is how often the periodic re-check
// asks again while the node cannot answer (restarting, warming up).
chainIdentityRetryInterval = 15 * time.Second

// chainIdentityUnreachableGrace is how long the periodic re-check
// keeps asking a node that cannot answer before it treats the
// silence as a mismatch. A node restart takes seconds; a node that
// stays away this long is a problem either way.
chainIdentityUnreachableGrace = 5 * time.Minute

// chainIdentityPollInterval is how often the node is asked for its
// header count while waiting for it to reach the activation height.
chainIdentityPollInterval = 5 * time.Second
Expand Down Expand Up @@ -102,6 +115,10 @@ type ReducedDataStatus struct {
// BLAKE2b chain, or cannot be told apart from a node that is not.
type ErrWrongChain struct {
Reason string

// Unreachable is set when the node could not answer at all (it is
// restarting or warming up), which says nothing about its chain.
Unreachable bool
}

// Error implements the error interface.
Expand Down Expand Up @@ -129,6 +146,12 @@ type chainIdentityChecker struct {
pollInterval time.Duration
logInterval time.Duration

// retryInterval and unreachableGrace bound how long the periodic
// re-check waits for a node that cannot answer. Fields so tests can
// shorten them.
retryInterval time.Duration
unreachableGrace time.Duration

// now is a field so tests can pin timestamps.
now func() time.Time

Expand All @@ -148,6 +171,9 @@ func newChainIdentityChecker(rpc blake2bChainRPC, params BitcoinNetParams,
pollInterval: chainIdentityPollInterval,
logInterval: chainIdentityWaitLogInterval,
now: time.Now,

retryInterval: chainIdentityRetryInterval,
unreachableGrace: chainIdentityUnreachableGrace,
}
}

Expand Down Expand Up @@ -284,7 +310,8 @@ func (c *chainIdentityChecker) verifyOnce(height uint32) (chainhash.Hash,
nodeHash, err := c.rpc.GetBlockHash(int64(height))
if err != nil {
return chainhash.Hash{}, &ErrWrongChain{Reason: fmt.Sprintf(
"the node cannot serve block %d: %v", height, err)}
"the node cannot serve block %d: %v", height, err),
Unreachable: nodeUnreachable(err)}
}

hashParam, err := json.Marshal(nodeHash.String())
Expand All @@ -301,7 +328,7 @@ func (c *chainIdentityChecker) verifyOnce(height uint32) (chainhash.Hash,
if err != nil {
return chainhash.Hash{}, &ErrWrongChain{Reason: fmt.Sprintf(
"the node cannot serve the header of block %d: %v",
height, err)}
height, err), Unreachable: nodeUnreachable(err)}
}

var headerHex string
Expand Down Expand Up @@ -479,7 +506,10 @@ func (c *chainIdentityChecker) watch(height uint32, interval time.Duration,
return
}

hash, err := c.verifyOnce(height)
hash, err := c.verifyWaiting(height, quit)
if errors.Is(err, errQuit) {
return
}
if err != nil {
c.writeStatus(ChainIdentityStatus{
State: ChainIdentityRefused,
Expand All @@ -504,6 +534,55 @@ func (c *chainIdentityChecker) watch(height uint32, interval time.Duration,
}
}

// errQuit reports that the daemon is shutting down.
var errQuit = errors.New("chain identity check stopped")

// verifyWaiting is verifyOnce for the periodic re-check: while the node
// cannot answer at all (a restart, the RPC warm-up) it asks again every
// retryInterval, and only gives up after unreachableGrace. A node that
// answers with another chain is reported at once.
func (c *chainIdentityChecker) verifyWaiting(height uint32,
quit <-chan struct{}) (chainhash.Hash, error) {

deadline := c.now().Add(c.unreachableGrace)
logged := false
for {
hash, err := c.verifyOnce(height)
var wrong *ErrWrongChain
if err == nil || !errors.As(err, &wrong) ||
!wrong.Unreachable || !c.now().Before(deadline) {

return hash, err
}
if !logged {
c.log.Warnf("The Bitcoin node cannot answer the "+
"chain check (%v); asking again for up to %v",
err, c.unreachableGrace)
logged = true
}

select {
case <-time.After(c.retryInterval):
case <-quit:
return chainhash.Hash{}, errQuit
}
}
}

// nodeUnreachable tells a node that could not answer (connection refused or
// dropped, RPC warm-up) from one that answered something wrong.
func nodeUnreachable(err error) bool {
var rpcErr *btcjson.RPCError
if errors.As(err, &rpcErr) {
return rpcErr.Code == btcjson.ErrRPCInWarmup
}
var netErr net.Error
return errors.As(err, &netErr) ||
errors.Is(err, syscall.ECONNREFUSED) ||
errors.Is(err, syscall.ECONNRESET) ||
errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF)
}

// writeStatus records the outcome for the wrappers. Failing to write is
// logged and otherwise ignored: the file is for showing the state, never for
// deciding it.
Expand Down
62 changes: 62 additions & 0 deletions chainreg/blake2b_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -67,6 +70,8 @@ func (s *scriptedRPC) setHeaders(headers int32) {
}

func (s *scriptedRPC) GetBlockHash(h int64) (*chainhash.Hash, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.hashErr != nil {
return nil, s.hashErr
}
Expand Down Expand Up @@ -375,6 +380,63 @@ func TestChainIdentityWatchStopsOnMismatch(t *testing.T) {
require.Equal(t, ChainIdentityRefused, readStatus(t, path).State)
}

// TestChainIdentityWatchWaitsForRestartingNode: a node that cannot answer
// for a moment (restarting, RPC warm-up) is asked again rather than taken
// for a chain switch; one that stays away past the grace is reported.
func TestChainIdentityWatchWaitsForRestartingNode(t *testing.T) {
rpc := mainnetRPC(t)
c := newTestChecker(t, rpc, BitcoinMainNetParams, "")
_, err := c.run(make(chan struct{}))
require.NoError(t, err)
c.retryInterval = time.Millisecond
c.unreachableGrace = time.Hour

// The node is warming up: the re-check asks again and, once the node
// answers, carries on without reporting anything.
warmup := &btcjson.RPCError{
Code: btcjson.ErrRPCInWarmup,
Message: "Loading P2P addresses…",
}
rpc.hashErr = warmup
go func() {
time.Sleep(20 * time.Millisecond)
rpc.mu.Lock()
rpc.hashErr = nil
rpc.mu.Unlock()
}()
_, err = c.verifyWaiting(Blake2bMainnetActivationHeight,
make(chan struct{}))
require.NoError(t, err)

// Past the grace, silence is reported like a mismatch.
rpc.hashErr = warmup
c.unreachableGrace = 5 * time.Millisecond
_, err = c.verifyWaiting(Blake2bMainnetActivationHeight,
make(chan struct{}))
require.ErrorContains(t, err, "Loading P2P addresses")

// An answer that names another chain is never waited for.
rpc.hashErr = &btcjson.RPCError{
Code: btcjson.ErrRPCOutOfRange,
Message: "Block height out of range",
}
c.unreachableGrace = time.Hour
_, err = c.verifyWaiting(Blake2bMainnetActivationHeight,
make(chan struct{}))
require.ErrorContains(t, err, "out of range")
}

// TestNodeUnreachable: only a node that could not answer is waited for.
func TestNodeUnreachable(t *testing.T) {
require.True(t, nodeUnreachable(&btcjson.RPCError{
Code: btcjson.ErrRPCInWarmup}))
require.True(t, nodeUnreachable(syscall.ECONNREFUSED))
require.True(t, nodeUnreachable(fmt.Errorf("post: %w", io.EOF)))
require.False(t, nodeUnreachable(&btcjson.RPCError{
Code: btcjson.ErrRPCOutOfRange}))
require.False(t, nodeUnreachable(errors.New("no such block")))
}

// TestChainIdentityStatusFileAtomic: the file is written whole and the
// temporary file does not linger.
func TestChainIdentityStatusFileAtomic(t *testing.T) {
Expand Down
Loading