Skip to content

Commit dc096c7

Browse files
committed
handle unmerged blocks
1 parent c9f44af commit dc096c7

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

internal/orchestrator/reorg_handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ func findReorgEndIndex(reversedBlockHeaders []common.BlockHeader) (index int) {
125125
currentBlock := reversedBlockHeaders[i]
126126
previousBlock := reversedBlockHeaders[i+1]
127127

128+
if currentBlock.Number.Cmp(previousBlock.Number) == 0 { // unmerged block
129+
continue
130+
}
128131
if currentBlock.ParentHash != previousBlock.Hash {
129132
log.Debug().
130133
Str("currentBlockNumber", currentBlock.Number.String()).

internal/orchestrator/reorg_handler_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,75 @@ func TestHandleReorgWithManyBlocks(t *testing.T) {
500500
assert.NoError(t, err)
501501
assert.Equal(t, big.NewInt(109), mostRecentBlockChecked)
502502
}
503+
504+
func TestHandleReorgWithDuplicateBlocks(t *testing.T) {
505+
defer func() { config.Cfg = config.Config{} }()
506+
config.Cfg.ReorgHandler.BlocksPerScan = 10
507+
508+
mockRPC := mocks.NewMockIRPCClient(t)
509+
mockMainStorage := mocks.NewMockIMainStorage(t)
510+
mockOrchestratorStorage := mocks.NewMockIOrchestratorStorage(t)
511+
512+
mockStorage := storage.IStorage{
513+
MainStorage: mockMainStorage,
514+
OrchestratorStorage: mockOrchestratorStorage,
515+
}
516+
517+
mockRPC.EXPECT().GetChainID().Return(big.NewInt(1))
518+
mockOrchestratorStorage.EXPECT().GetLastReorgCheckedBlockNumber(big.NewInt(1)).Return(big.NewInt(6268164), nil)
519+
520+
mockMainStorage.EXPECT().LookbackBlockHeaders(big.NewInt(1), 10, big.NewInt(6268172)).Return([]common.BlockHeader{
521+
{Number: big.NewInt(6268172), Hash: "0x69d2044d27d2879c309fd885eb0c7d915c9aeed9b28df460d3b52cb4ccf888d8", ParentHash: "0xbf44d12afe40ef30effa32ed45c8d26d854ffba1c8ad781117117e7d18ca157f"},
522+
{Number: big.NewInt(6268172), Hash: "0x69d2044d27d2879c309fd885eb0c7d915c9aeed9b28df460d3b52cb4ccf888d8", ParentHash: "0xbf44d12afe40ef30effa32ed45c8d26d854ffba1c8ad781117117e7d18ca157f"},
523+
{Number: big.NewInt(6268171), Hash: "0xbf44d12afe40ef30effa32ed45c8d26d854ffba1c8ad781117117e7d18ca157f", ParentHash: "0x54d0a7822d69b73e097684fd6311c57f05f79430c188292e73b2c31b1db8170a"},
524+
{Number: big.NewInt(6268170), Hash: "0x54d0a7822d69b73e097684fd6311c57f05f79430c188292e73b2c31b1db8170a", ParentHash: "0x0f265b8f03a1ac837626411d0827bd1bf344ad447032141ae4e1eebd241db8bf"},
525+
{Number: big.NewInt(6268169), Hash: "0x0f265b8f03a1ac837626411d0827bd1bf344ad447032141ae4e1eebd241db8bf", ParentHash: "0xc39c3263522577a77add820c259c39402462d222ad145cbe2aead910a06fcbf8"},
526+
{Number: big.NewInt(6268168), Hash: "0xc39c3263522577a77add820c259c39402462d222ad145cbe2aead910a06fcbf8", ParentHash: "0xa3fb3ca0a7823d048752781b56202d2b777236e4b5d9b880070f2f8390212fb4"},
527+
{Number: big.NewInt(6268167), Hash: "0xa3fb3ca0a7823d048752781b56202d2b777236e4b5d9b880070f2f8390212fb4", ParentHash: "0xe29e2d5a6d55248456c6642cfb7888bb796972c77d522acda54c2213d7ad4091"},
528+
{Number: big.NewInt(6268167), Hash: "0xa3fb3ca0a7823d048752781b56202d2b777236e4b5d9b880070f2f8390212fb4", ParentHash: "0xe29e2d5a6d55248456c6642cfb7888bb796972c77d522acda54c2213d7ad4091"},
529+
{Number: big.NewInt(6268166), Hash: "0xe29e2d5a6d55248456c6642cfb7888bb796972c77d522acda54c2213d7ad4091", ParentHash: "0x39704dfd56a8ed3aaf0845f38edd0f911b4b53c9e0bcaeee2646d0045af13934"},
530+
{Number: big.NewInt(6268165), Hash: "0x39704dfd56a8ed3aaf0845f38edd0f911b4b53c9e0bcaeee2646d0045af13934", ParentHash: "0xe58ec77634cd09cc3ae8991f4e36be6b84fe9d23e8716b4cca1fb69e91e8b8a1"},
531+
}, nil)
532+
533+
handler := NewReorgHandler(mockRPC, mockStorage)
534+
mostRecentBlockChecked, err := handler.RunFromBlock(big.NewInt(6268172))
535+
536+
assert.NoError(t, err)
537+
assert.Equal(t, big.NewInt(6268172), mostRecentBlockChecked)
538+
}
539+
540+
func TestNothingIsDoneForCorrectBlocks(t *testing.T) {
541+
defer func() { config.Cfg = config.Config{} }()
542+
config.Cfg.ReorgHandler.BlocksPerScan = 10
543+
544+
mockRPC := mocks.NewMockIRPCClient(t)
545+
mockMainStorage := mocks.NewMockIMainStorage(t)
546+
mockOrchestratorStorage := mocks.NewMockIOrchestratorStorage(t)
547+
548+
mockStorage := storage.IStorage{
549+
MainStorage: mockMainStorage,
550+
OrchestratorStorage: mockOrchestratorStorage,
551+
}
552+
553+
mockRPC.EXPECT().GetChainID().Return(big.NewInt(1))
554+
mockOrchestratorStorage.EXPECT().GetLastReorgCheckedBlockNumber(big.NewInt(1)).Return(big.NewInt(6268164), nil)
555+
556+
mockMainStorage.EXPECT().LookbackBlockHeaders(big.NewInt(1), 10, big.NewInt(6268173)).Return([]common.BlockHeader{
557+
{Number: big.NewInt(6268173), Hash: "0xa281ed679e6f7d0ede5fffdd3528348f303bc456d8d83e6bbe7ad0708f8f9b10", ParentHash: "0x69d2044d27d2879c309fd885eb0c7d915c9aeed9b28df460d3b52cb4ccf888d8"},
558+
{Number: big.NewInt(6268172), Hash: "0x69d2044d27d2879c309fd885eb0c7d915c9aeed9b28df460d3b52cb4ccf888d8", ParentHash: "0xbf44d12afe40ef30effa32ed45c8d26d854ffba1c8ad781117117e7d18ca157f"},
559+
{Number: big.NewInt(6268171), Hash: "0xbf44d12afe40ef30effa32ed45c8d26d854ffba1c8ad781117117e7d18ca157f", ParentHash: "0x54d0a7822d69b73e097684fd6311c57f05f79430c188292e73b2c31b1db8170a"},
560+
{Number: big.NewInt(6268170), Hash: "0x54d0a7822d69b73e097684fd6311c57f05f79430c188292e73b2c31b1db8170a", ParentHash: "0x0f265b8f03a1ac837626411d0827bd1bf344ad447032141ae4e1eebd241db8bf"},
561+
{Number: big.NewInt(6268169), Hash: "0x0f265b8f03a1ac837626411d0827bd1bf344ad447032141ae4e1eebd241db8bf", ParentHash: "0xc39c3263522577a77add820c259c39402462d222ad145cbe2aead910a06fcbf8"},
562+
{Number: big.NewInt(6268168), Hash: "0xc39c3263522577a77add820c259c39402462d222ad145cbe2aead910a06fcbf8", ParentHash: "0xa3fb3ca0a7823d048752781b56202d2b777236e4b5d9b880070f2f8390212fb4"},
563+
{Number: big.NewInt(6268167), Hash: "0xa3fb3ca0a7823d048752781b56202d2b777236e4b5d9b880070f2f8390212fb4", ParentHash: "0xe29e2d5a6d55248456c6642cfb7888bb796972c77d522acda54c2213d7ad4091"},
564+
{Number: big.NewInt(6268166), Hash: "0xe29e2d5a6d55248456c6642cfb7888bb796972c77d522acda54c2213d7ad4091", ParentHash: "0x39704dfd56a8ed3aaf0845f38edd0f911b4b53c9e0bcaeee2646d0045af13934"},
565+
{Number: big.NewInt(6268165), Hash: "0x39704dfd56a8ed3aaf0845f38edd0f911b4b53c9e0bcaeee2646d0045af13934", ParentHash: "0xe58ec77634cd09cc3ae8991f4e36be6b84fe9d23e8716b4cca1fb69e91e8b8a1"},
566+
{Number: big.NewInt(6268164), Hash: "0xe58ec77634cd09cc3ae8991f4e36be6b84fe9d23e8716b4cca1fb69e91e8b8a1", ParentHash: "0xd4be1054851a009a2c50407a8679dc2e20b4116386a212ec63900cb31b01e4e5"},
567+
}, nil)
568+
569+
handler := NewReorgHandler(mockRPC, mockStorage)
570+
mostRecentBlockChecked, err := handler.RunFromBlock(big.NewInt(6268173))
571+
572+
assert.NoError(t, err)
573+
assert.Equal(t, big.NewInt(6268173), mostRecentBlockChecked)
574+
}

0 commit comments

Comments
 (0)