Skip to content

Commit

Permalink
Merge pull request #223 from bitcoin-sv/fix/blocktx-fill-gaps
Browse files Browse the repository at this point in the history
Only fill gaps if primary
  • Loading branch information
boecklim authored Dec 20, 2023
2 parents e24ac2f + 900f7dd commit 89e17a3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
11 changes: 10 additions & 1 deletion blocktx/block_notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blocktx
import (
"context"
"errors"
"os"
"testing"
"time"

Expand All @@ -18,16 +19,21 @@ import (
func TestNewBlockNotifier(t *testing.T) {
hash822014, err := chainhash.NewHashFromStr("0000000000000000025855b62f4c2e3732dad363a6f2ead94e4657ef96877067")
require.NoError(t, err)
hostname, err := os.Hostname()
require.NoError(t, err)

tt := []struct {
name string
hostname string
getBlockGapsErr error
}{
{
name: "success",
name: "success",
hostname: hostname,
},
{
name: "error getting block gaps",
hostname: hostname,
getBlockGapsErr: errors.New("failed to get block gaps"),
},
}
Expand All @@ -43,6 +49,9 @@ func TestNewBlockNotifier(t *testing.T) {
},
}, tc.getBlockGapsErr
},
PrimaryBlocktxFunc: func(ctx context.Context) (string, error) {
return tc.hostname, nil
},
}
blockCh := make(chan *blocktx_api.Block)

Expand Down
9 changes: 9 additions & 0 deletions blocktx/peer_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ func (bs *PeerHandler) HandleBlock(wireMsg wire.Message, peer p2p.PeerI) error {
}

func (bs *PeerHandler) FillGaps(peer p2p.PeerI) error {
primary, err := bs.CheckPrimary()
if err != nil {
return err
}

if !primary {
return nil
}

blockHeightGaps, err := bs.store.GetBlockGaps(context.Background())
if err != nil {
return err
Expand Down
49 changes: 41 additions & 8 deletions blocktx/peer_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,20 +310,28 @@ func TestHandleBlock(t *testing.T) {
}

func TestFillGaps(t *testing.T) {
hostname, err := os.Hostname()
require.NoError(t, err)
hash822014, err := chainhash.NewHashFromStr("0000000000000000025855b62f4c2e3732dad363a6f2ead94e4657ef96877067")
require.NoError(t, err)
hash822019, err := chainhash.NewHashFromStr("00000000000000000364332e1bbd61dc928141b9469c5daea26a4b506efc9656")
require.NoError(t, err)
tt := []struct {
name string
blockGaps []*store.BlockGap
getBlockGapsErr error

expectedErrorStr string
name string
blockGaps []*store.BlockGap
getBlockGapsErr error
primaryBlocktxErr error
hostname string

expectedGetBlockGapsCalls int
expectedErrorStr string
}{
{
name: "success - no gaps",
blockGaps: []*store.BlockGap{},
hostname: hostname,

expectedGetBlockGapsCalls: 1,
},
{
name: "success - 2 gaps",
Expand All @@ -337,13 +345,34 @@ func TestFillGaps(t *testing.T) {
Hash: hash822019,
},
},
hostname: hostname,

expectedGetBlockGapsCalls: 1,
},
{
name: "error getting block gaps",
blockGaps: []*store.BlockGap{},
getBlockGapsErr: errors.New("failed to get block gaps"),
hostname: hostname,

expectedGetBlockGapsCalls: 1,
expectedErrorStr: "failed to get block gaps",
},
{
name: "not primary",
blockGaps: []*store.BlockGap{},
hostname: "not primary",

expectedErrorStr: "failed to get block gaps",
expectedGetBlockGapsCalls: 0,
},
{
name: "check primary - error",
blockGaps: []*store.BlockGap{},
hostname: "not primary",
primaryBlocktxErr: errors.New("failed to check primary"),

expectedGetBlockGapsCalls: 0,
expectedErrorStr: "failed to check primary",
},
}

Expand All @@ -355,19 +384,23 @@ func TestFillGaps(t *testing.T) {
GetBlockGapsFunc: func(ctx context.Context) ([]*store.BlockGap, error) {
return tc.blockGaps, tc.getBlockGapsErr
},
PrimaryBlocktxFunc: func(ctx context.Context) (string, error) {
return tc.hostname, tc.primaryBlocktxErr
},
}

peerHandler := NewPeerHandler(blockTxLogger, storeMock, bockChannel, 100)
peer := &MockedPeer{}
err = peerHandler.FillGaps(peer)

require.Equal(t, tc.expectedGetBlockGapsCalls, len(storeMock.GetBlockGapsCalls()))
peerHandler.Shutdown()
if tc.expectedErrorStr == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, tc.expectedErrorStr)
return
}

peerHandler.Shutdown()
})
}
}

0 comments on commit 89e17a3

Please sign in to comment.