From 900f7dde76799fce2413250a65593960efee3554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20B=C3=B6ckli?= Date: Wed, 20 Dec 2023 10:51:47 +0100 Subject: [PATCH] BAARC-86: Only fill gaps if primary --- blocktx/block_notifier_test.go | 11 +++++++- blocktx/peer_handler.go | 9 +++++++ blocktx/peer_handler_test.go | 49 ++++++++++++++++++++++++++++------ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/blocktx/block_notifier_test.go b/blocktx/block_notifier_test.go index 4c13fbf28..e99c7228c 100644 --- a/blocktx/block_notifier_test.go +++ b/blocktx/block_notifier_test.go @@ -3,6 +3,7 @@ package blocktx import ( "context" "errors" + "os" "testing" "time" @@ -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"), }, } @@ -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) diff --git a/blocktx/peer_handler.go b/blocktx/peer_handler.go index e2f7484e7..47bf6b6f6 100644 --- a/blocktx/peer_handler.go +++ b/blocktx/peer_handler.go @@ -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 diff --git a/blocktx/peer_handler_test.go b/blocktx/peer_handler_test.go index 0dc7f29b4..e8f22fa0f 100644 --- a/blocktx/peer_handler_test.go +++ b/blocktx/peer_handler_test.go @@ -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", @@ -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", }, } @@ -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() }) } }