9
9
"strconv"
10
10
"testing"
11
11
12
+ "github.com/ethereum/go-ethereum/common"
12
13
"github.com/ethereum/go-ethereum/core/state"
13
14
"github.com/ethereum/go-ethereum/core/types"
14
15
"github.com/ethereum/go-ethereum/ethdb"
@@ -44,7 +45,7 @@ func testFork(t *testing.T, bman *BlockProcessor, i, N int, f func(td1, td2 *big
44
45
// extend the fork
45
46
parent := bman2 .bc .CurrentBlock ()
46
47
chainB := makeChain (bman2 , parent , N , db , ForkSeed )
47
- err = bman2 .bc .InsertChain (chainB )
48
+ _ , err = bman2 .bc .InsertChain (chainB )
48
49
if err != nil {
49
50
t .Fatal ("Insert chain error for fork:" , err )
50
51
}
@@ -108,7 +109,7 @@ func loadChain(fn string, t *testing.T) (types.Blocks, error) {
108
109
}
109
110
110
111
func insertChain (done chan bool , chainMan * ChainManager , chain types.Blocks , t * testing.T ) {
111
- err := chainMan .InsertChain (chain )
112
+ _ , err := chainMan .InsertChain (chain )
112
113
if err != nil {
113
114
fmt .Println (err )
114
115
t .FailNow ()
@@ -369,18 +370,23 @@ func makeChainWithDiff(genesis *types.Block, d []int, seed byte) []*types.Block
369
370
return chain
370
371
}
371
372
372
- func TestReorg (t * testing.T ) {
373
- db , _ := ethdb .NewMemDatabase ()
373
+ func chm (genesis * types.Block , db common.Database ) * ChainManager {
374
374
var eventMux event.TypeMux
375
-
376
- genesis := GenesisBlock (db )
377
375
bc := & ChainManager {blockDb : db , stateDb : db , genesisBlock : genesis , eventMux : & eventMux }
378
376
bc .cache = NewBlockCache (100 )
379
377
bc .futureBlocks = NewBlockCache (100 )
380
378
bc .processor = bproc {}
381
379
bc .ResetWithGenesisBlock (genesis )
382
380
bc .txState = state .ManageState (bc .State ())
383
381
382
+ return bc
383
+ }
384
+
385
+ func TestReorgLongest (t * testing.T ) {
386
+ db , _ := ethdb .NewMemDatabase ()
387
+ genesis := GenesisBlock (db )
388
+ bc := chm (genesis , db )
389
+
384
390
chain1 := makeChainWithDiff (genesis , []int {1 , 2 , 4 }, 10 )
385
391
chain2 := makeChainWithDiff (genesis , []int {1 , 2 , 3 , 4 }, 11 )
386
392
@@ -394,3 +400,22 @@ func TestReorg(t *testing.T) {
394
400
}
395
401
}
396
402
}
403
+
404
+ func TestReorgShortest (t * testing.T ) {
405
+ db , _ := ethdb .NewMemDatabase ()
406
+ genesis := GenesisBlock (db )
407
+ bc := chm (genesis , db )
408
+
409
+ chain1 := makeChainWithDiff (genesis , []int {1 , 2 , 3 , 4 }, 10 )
410
+ chain2 := makeChainWithDiff (genesis , []int {1 , 10 }, 11 )
411
+
412
+ bc .InsertChain (chain1 )
413
+ bc .InsertChain (chain2 )
414
+
415
+ prev := bc .CurrentBlock ()
416
+ for block := bc .GetBlockByNumber (bc .CurrentBlock ().NumberU64 () - 1 ); block .NumberU64 () != 0 ; prev , block = block , bc .GetBlockByNumber (block .NumberU64 ()- 1 ) {
417
+ if prev .ParentHash () != block .Hash () {
418
+ t .Errorf ("parent hash mismatch %x - %x" , prev .ParentHash (), block .Hash ())
419
+ }
420
+ }
421
+ }
0 commit comments