From c12482355f281cecf9be33269c2a06b5c1262a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20B=C3=B6ckli?= Date: Wed, 20 Dec 2023 14:37:45 +0100 Subject: [PATCH] BAARC-86: Rotate request for missing blocks through peers --- blocktx/block_notifier.go | 11 ++++++++++- blocktx/block_notifier_test.go | 16 +++++++++++++++- blocktx/peer_handler.go | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/blocktx/block_notifier.go b/blocktx/block_notifier.go index 9430705e1..517729309 100644 --- a/blocktx/block_notifier.go +++ b/blocktx/block_notifier.go @@ -122,15 +122,24 @@ func NewBlockNotifier(storeI store.Interface, l utils.Logger, blockCh chan *bloc bn.quitFillBlockGapComplete <- struct{}{} }() + peerIndex := 0 for { select { case <-bn.quitFillBlockGap: return case <-bn.fillGapsTicker.C: - err := peerHandler.FillGaps(peers[0]) + if peerIndex >= len(peers) { + peerIndex = 0 + } + + l.Infof("requesting missing blocks from peer %d", peerIndex) + + err := peerHandler.FillGaps(peers[peerIndex]) if err != nil { l.Errorf("failed to fill gaps: %v", err) } + + peerIndex++ } } }() diff --git a/blocktx/block_notifier_test.go b/blocktx/block_notifier_test.go index e99c7228c..50ec20dc0 100644 --- a/blocktx/block_notifier_test.go +++ b/blocktx/block_notifier_test.go @@ -63,6 +63,20 @@ func TestNewBlockNotifier(t *testing.T) { ZMQ: 28332, }, }, + { + Host: "127.0.0.2", + Port: config.PeerPort{ + P2P: 18333, + ZMQ: 28332, + }, + }, + { + Host: "127.0.0.3", + Port: config.PeerPort{ + P2P: 18333, + ZMQ: 28332, + }, + }, } logger := gocore.Log("test", gocore.NewLogLevelFromString("INFO")) @@ -70,7 +84,7 @@ func TestNewBlockNotifier(t *testing.T) { notifier, err := NewBlockNotifier(storeMock, logger, blockCh, peerHandler, peerSettings, wire.TestNet3, WithFillGapsInterval(time.Millisecond*30)) require.NoError(t, err) - time.Sleep(55 * time.Millisecond) + time.Sleep(120 * time.Millisecond) peerHandler.Shutdown() notifier.Shutdown() }) diff --git a/blocktx/peer_handler.go b/blocktx/peer_handler.go index 47bf6b6f6..8d408822e 100644 --- a/blocktx/peer_handler.go +++ b/blocktx/peer_handler.go @@ -403,7 +403,7 @@ func (bs *PeerHandler) FillGaps(peer p2p.PeerI) error { return nil } - bs.logger.Infof("filling block gap for hash %s at height %d", gaps.Hash.String(), gaps.Height) + bs.logger.Infof("requesting missing block hash %s at height %d", gaps.Hash.String(), gaps.Height) pair := utils.NewPair(gaps.Hash, peer) utils.SafeSend(bs.workerCh, pair)