@@ -119,7 +119,8 @@ func TestFindReorgEndIndex(t *testing.T) {
119119
120120 for _ , tt := range tests {
121121 t .Run (tt .name , func (t * testing.T ) {
122- result := findReorgEndIndex (tt .reversedBlockHeaders )
122+ result , err := findIndexOfFirstHashMismatch (tt .reversedBlockHeaders )
123+ assert .NoError (t , err )
123124 assert .Equal (t , tt .expectedIndex , result )
124125 })
125126 }
@@ -146,7 +147,7 @@ func TestNewReorgHandlerWithForceFromBlock(t *testing.T) {
146147 assert .Equal (t , big .NewInt (2000 ), handler .lastCheckedBlock )
147148}
148149
149- func TestFindFirstForkedBlockNumber (t * testing.T ) {
150+ func TestFindFirstReorgedBlockNumber (t * testing.T ) {
150151 mockRPC := mocks .NewMockIRPCClient (t )
151152 mockMainStorage := mocks .NewMockIMainStorage (t )
152153 mockOrchestratorStorage := mocks .NewMockIOrchestratorStorage (t )
@@ -158,6 +159,7 @@ func TestFindFirstForkedBlockNumber(t *testing.T) {
158159
159160 mockRPC .EXPECT ().GetChainID ().Return (big .NewInt (1 ))
160161 mockOrchestratorStorage .EXPECT ().GetLastReorgCheckedBlockNumber (big .NewInt (1 )).Return (big .NewInt (3 ), nil )
162+ mockRPC .EXPECT ().GetBlocksPerRequest ().Return (rpc.BlocksPerRequestConfig {Blocks : 100 })
161163 handler := NewReorgHandler (mockRPC , mockStorage )
162164
163165 reversedBlockHeaders := []common.BlockHeader {
@@ -172,13 +174,14 @@ func TestFindFirstForkedBlockNumber(t *testing.T) {
172174 {BlockNumber : big .NewInt (1 ), Data : common.Block {Hash : "hash1" , ParentHash : "hash0" }},
173175 })
174176
175- forkPoint , err := handler .findFirstForkedBlockNumber (reversedBlockHeaders )
177+ reorgedBlockNumbers := []* big.Int {}
178+ err := handler .findReorgedBlockNumbers (reversedBlockHeaders , & reorgedBlockNumbers )
176179
177180 assert .NoError (t , err )
178- assert .Equal (t , big .NewInt (3 ), forkPoint )
181+ assert .Equal (t , [] * big.Int { big . NewInt (3 )}, reorgedBlockNumbers )
179182}
180183
181- func TestFindFirstForkedBlockNumberWithLastBlockInSlice (t * testing.T ) {
184+ func TestFindAllReorgedBlockNumbersWithLastBlockInSliceAsValid (t * testing.T ) {
182185 mockRPC := mocks .NewMockIRPCClient (t )
183186 mockMainStorage := mocks .NewMockIMainStorage (t )
184187 mockOrchestratorStorage := mocks .NewMockIOrchestratorStorage (t )
@@ -189,12 +192,13 @@ func TestFindFirstForkedBlockNumberWithLastBlockInSlice(t *testing.T) {
189192 }
190193
191194 mockRPC .EXPECT ().GetChainID ().Return (big .NewInt (1 ))
195+ mockRPC .EXPECT ().GetBlocksPerRequest ().Return (rpc.BlocksPerRequestConfig {Blocks : 100 })
192196 mockOrchestratorStorage .EXPECT ().GetLastReorgCheckedBlockNumber (big .NewInt (1 )).Return (big .NewInt (3 ), nil )
193197 handler := NewReorgHandler (mockRPC , mockStorage )
194198
195199 reversedBlockHeaders := []common.BlockHeader {
196- {Number : big .NewInt (3 ), Hash : "hash3a" , ParentHash : "hash2a" },
197- {Number : big .NewInt (2 ), Hash : "hash2a" , ParentHash : "hash1" }, // <- fork starts from here
200+ {Number : big .NewInt (3 ), Hash : "hash3a" , ParentHash : "hash2a" }, // <- fork starts from here
201+ {Number : big .NewInt (2 ), Hash : "hash2a" , ParentHash : "hash1" },
198202 {Number : big .NewInt (1 ), Hash : "hash1" , ParentHash : "hash0" },
199203 }
200204
@@ -204,13 +208,14 @@ func TestFindFirstForkedBlockNumberWithLastBlockInSlice(t *testing.T) {
204208 {BlockNumber : big .NewInt (1 ), Data : common.Block {Hash : "hash1" , ParentHash : "hash0" }},
205209 })
206210
207- forkPoint , err := handler .findFirstForkedBlockNumber (reversedBlockHeaders )
211+ reorgedBlockNumbers := []* big.Int {}
212+ err := handler .findReorgedBlockNumbers (reversedBlockHeaders , & reorgedBlockNumbers )
208213
209214 assert .NoError (t , err )
210- assert .Equal (t , big .NewInt (2 ), forkPoint )
215+ assert .Equal (t , [] * big.Int { big . NewInt (3 ), big . NewInt ( 2 )}, reorgedBlockNumbers )
211216}
212217
213- func TestFindFirstForkedBlockNumberRecursively (t * testing.T ) {
218+ func TestReorgedBlockNumbersRecursively (t * testing.T ) {
214219 defer func () { config .Cfg = config.Config {} }()
215220 config .Cfg .ReorgHandler .BlocksPerScan = 3
216221
@@ -224,6 +229,7 @@ func TestFindFirstForkedBlockNumberRecursively(t *testing.T) {
224229 }
225230
226231 mockRPC .EXPECT ().GetChainID ().Return (big .NewInt (1 ))
232+ mockRPC .EXPECT ().GetBlocksPerRequest ().Return (rpc.BlocksPerRequestConfig {Blocks : 100 })
227233 mockOrchestratorStorage .EXPECT ().GetLastReorgCheckedBlockNumber (big .NewInt (1 )).Return (big .NewInt (3 ), nil )
228234 handler := NewReorgHandler (mockRPC , mockStorage )
229235
@@ -233,8 +239,7 @@ func TestFindFirstForkedBlockNumberRecursively(t *testing.T) {
233239 {BlockNumber : big .NewInt (4 ), Data : common.Block {Hash : "hash4" , ParentHash : "hash3" }},
234240 }).Once ()
235241
236- mockRPC .EXPECT ().GetBlocks ([]* big.Int {big .NewInt (4 ), big .NewInt (3 ), big .NewInt (2 ), big .NewInt (1 )}).Return ([]rpc.GetBlocksResult {
237- {BlockNumber : big .NewInt (4 ), Data : common.Block {Hash : "hash4" , ParentHash : "hash3" }},
242+ mockRPC .EXPECT ().GetBlocks ([]* big.Int {big .NewInt (3 ), big .NewInt (2 ), big .NewInt (1 )}).Return ([]rpc.GetBlocksResult {
238243 {BlockNumber : big .NewInt (3 ), Data : common.Block {Hash : "hash3" , ParentHash : "hash2" }},
239244 {BlockNumber : big .NewInt (2 ), Data : common.Block {Hash : "hash2" , ParentHash : "hash1" }},
240245 {BlockNumber : big .NewInt (1 ), Data : common.Block {Hash : "hash1" , ParentHash : "hash0" }},
@@ -246,17 +251,17 @@ func TestFindFirstForkedBlockNumberRecursively(t *testing.T) {
246251 {Number : big .NewInt (4 ), Hash : "hash4a" , ParentHash : "hash3a" },
247252 }
248253
249- mockMainStorage .EXPECT ().GetBlockHeadersDescending (big .NewInt (1 ), big .NewInt (1 ), big .NewInt (4 )).Return ([]common.BlockHeader {
250- {Number : big .NewInt (4 ), Hash : "hash4a" , ParentHash : "hash3a" },
251- {Number : big .NewInt (3 ), Hash : "hash3a" , ParentHash : "hash2" }, // <- fork starts from here
254+ mockMainStorage .EXPECT ().GetBlockHeadersDescending (big .NewInt (1 ), big .NewInt (1 ), big .NewInt (3 )).Return ([]common.BlockHeader {
255+ {Number : big .NewInt (3 ), Hash : "hash3a" , ParentHash : "hash2a" }, // <- end of reorged blocks
252256 {Number : big .NewInt (2 ), Hash : "hash2" , ParentHash : "hash1" },
253257 {Number : big .NewInt (1 ), Hash : "hash1" , ParentHash : "hash0" },
254258 }, nil )
255259
256- forkPoint , err := handler .findFirstForkedBlockNumber (initialBlockHeaders )
260+ reorgedBlockNumbers := []* big.Int {}
261+ err := handler .findReorgedBlockNumbers (initialBlockHeaders , & reorgedBlockNumbers )
257262
258263 assert .NoError (t , err )
259- assert .Equal (t , big .NewInt (3 ), forkPoint )
264+ assert .Equal (t , [] * big.Int { big . NewInt (6 ), big . NewInt ( 5 ), big . NewInt ( 4 ), big . NewInt ( 3 )}, reorgedBlockNumbers )
260265}
261266
262267func TestHandleReorg (t * testing.T ) {
@@ -278,14 +283,11 @@ func TestHandleReorg(t *testing.T) {
278283 })
279284 mockOrchestratorStorage .EXPECT ().GetLastReorgCheckedBlockNumber (big .NewInt (1 )).Return (big .NewInt (3 ), nil )
280285
281- reorgStart := big .NewInt (1 )
282- reorgEnd := big .NewInt (3 )
283-
284286 mockMainStorage .EXPECT ().DeleteBlockData (big .NewInt (1 ), mock .Anything ).Return (nil )
285287 mockMainStorage .EXPECT ().InsertBlockData (mock .Anything ).Return (nil )
286288
287289 handler := NewReorgHandler (mockRPC , mockStorage )
288- err := handler .handleReorg (reorgStart , reorgEnd )
290+ err := handler .handleReorg ([] * big. Int { big . NewInt ( 1 ), big . NewInt ( 2 ), big . NewInt ( 3 )} )
289291
290292 assert .NoError (t , err )
291293}
@@ -418,15 +420,15 @@ func TestHandleReorgWithLatestBlockReorged(t *testing.T) {
418420 {BlockNumber : big .NewInt (100 ), Data : common.Block {Hash : "hash100" , ParentHash : "hash99" }},
419421 })
420422
421- mockRPC .EXPECT ().GetFullBlocks ([]* big.Int {big .NewInt (101 ), big .NewInt (102 ), big .NewInt (103 ), big .NewInt (104 ), big .NewInt (105 ), big .NewInt (106 ), big .NewInt (107 ), big .NewInt (108 )}).Return ([]rpc.GetFullBlockResult {
422- {BlockNumber : big .NewInt (108 ), Data : common.BlockData {}},
423- {BlockNumber : big .NewInt (107 ), Data : common.BlockData {}},
424- {BlockNumber : big .NewInt (106 ), Data : common.BlockData {}},
425- {BlockNumber : big .NewInt (105 ), Data : common.BlockData {}},
426- {BlockNumber : big .NewInt (104 ), Data : common.BlockData {}},
427- {BlockNumber : big .NewInt (103 ), Data : common.BlockData {}},
428- {BlockNumber : big .NewInt (102 ), Data : common.BlockData {}},
423+ mockRPC .EXPECT ().GetFullBlocks ([]* big.Int {big .NewInt (108 ), big .NewInt (107 ), big .NewInt (106 ), big .NewInt (105 ), big .NewInt (104 ), big .NewInt (103 ), big .NewInt (102 ), big .NewInt (101 )}).Return ([]rpc.GetFullBlockResult {
429424 {BlockNumber : big .NewInt (101 ), Data : common.BlockData {}},
425+ {BlockNumber : big .NewInt (102 ), Data : common.BlockData {}},
426+ {BlockNumber : big .NewInt (103 ), Data : common.BlockData {}},
427+ {BlockNumber : big .NewInt (104 ), Data : common.BlockData {}},
428+ {BlockNumber : big .NewInt (105 ), Data : common.BlockData {}},
429+ {BlockNumber : big .NewInt (106 ), Data : common.BlockData {}},
430+ {BlockNumber : big .NewInt (107 ), Data : common.BlockData {}},
431+ {BlockNumber : big .NewInt (108 ), Data : common.BlockData {}},
430432 })
431433
432434 mockMainStorage .EXPECT ().DeleteBlockData (big .NewInt (1 ), mock .MatchedBy (func (blocks []* big.Int ) bool {
@@ -484,12 +486,12 @@ func TestHandleReorgWithManyBlocks(t *testing.T) {
484486 {BlockNumber : big .NewInt (100 ), Data : common.Block {Hash : "hash100" , ParentHash : "hash99" }},
485487 })
486488
487- mockRPC .EXPECT ().GetFullBlocks ([]* big.Int {big .NewInt (103 ), big .NewInt (104 ), big .NewInt (105 ), big .NewInt (106 ), big .NewInt (107 )}).Return ([]rpc.GetFullBlockResult {
488- {BlockNumber : big .NewInt (103 ), Data : common.BlockData {}},
489- {BlockNumber : big .NewInt (104 ), Data : common.BlockData {}},
490- {BlockNumber : big .NewInt (105 ), Data : common.BlockData {}},
491- {BlockNumber : big .NewInt (106 ), Data : common.BlockData {}},
489+ mockRPC .EXPECT ().GetFullBlocks ([]* big.Int {big .NewInt (107 ), big .NewInt (106 ), big .NewInt (105 ), big .NewInt (104 ), big .NewInt (103 )}).Return ([]rpc.GetFullBlockResult {
492490 {BlockNumber : big .NewInt (107 ), Data : common.BlockData {}},
491+ {BlockNumber : big .NewInt (106 ), Data : common.BlockData {}},
492+ {BlockNumber : big .NewInt (105 ), Data : common.BlockData {}},
493+ {BlockNumber : big .NewInt (104 ), Data : common.BlockData {}},
494+ {BlockNumber : big .NewInt (103 ), Data : common.BlockData {}},
493495 })
494496
495497 mockMainStorage .EXPECT ().DeleteBlockData (big .NewInt (1 ), mock .MatchedBy (func (blocks []* big.Int ) bool {
0 commit comments