@@ -577,112 +577,90 @@ func TestFetchStateSyncEvents_2(t *testing.T) {
577577 require .Equal (t , uint64 (6 ), lastStateID .Uint64 ())
578578}
579579
580- // Abs returns the absolute value of x.
581- func Abs (x int ) int {
582- if x < 0 {
583- return - x
584- }
585- return x
586- }
587-
588580func TestOutOfTurnSigning (t * testing.T ) {
589- log .Root ().SetHandler (log .LvlFilterHandler (log .LvlInfo , log .StreamHandler (os .Stderr , log .TerminalFormat (true ))))
590- fdlimit .Raise (2048 )
581+ init := buildEthereumInstance (t , rawdb .NewMemoryDatabase ())
582+ chain := init .ethereum .BlockChain ()
583+ engine := init .ethereum .Engine ()
584+ _bor := engine .(* bor.Bor )
591585
592- // Generate a batch of accounts to seal and fund with
593- faucets := make ([]* ecdsa.PrivateKey , 128 )
594- for i := 0 ; i < len (faucets ); i ++ {
595- faucets [i ], _ = crypto .GenerateKey ()
596- }
586+ defer _bor .Close ()
597587
598- // Create an Ethash network based off of the Ropsten config
599- // Generate a batch of accounts to seal and fund with
600- genesis := InitGenesis ( t , faucets , "./testdata/genesis_2val.json" , 8 )
588+ _ , heimdallSpan := loadSpanFromFile ( t )
589+ proposer := valset . NewValidator ( addr , 10 )
590+ heimdallSpan . ValidatorSet . Validators = append ( heimdallSpan . ValidatorSet . Validators , proposer )
601591
602- var (
603- stacks []* node.Node
604- nodes []* eth.Ethereum
605- enodes []* enode.Node
606- )
607- for i := 0 ; i < 2 ; i ++ {
608- // Start the node and wait until it's up
609- stack , ethBackend , err := InitMiner (genesis , keys [i ], true )
610- if err != nil {
611- panic (err )
612- }
613- defer stack .Close ()
592+ // add the block producer
593+ h , ctrl := getMockedHeimdallClient (t , heimdallSpan )
594+ defer ctrl .Finish ()
614595
615- for stack .Server ().NodeInfo ().Ports .Listener == 0 {
616- time .Sleep (250 * time .Millisecond )
617- }
618- // Connect the node to all the previous ones
619- for _ , n := range enodes {
620- stack .Server ().AddPeer (n )
621- }
622- // Start tracking the node and its enode
623- stacks = append (stacks , stack )
624- nodes = append (nodes , ethBackend )
625- enodes = append (enodes , stack .Server ().Self ())
626- }
596+ h .EXPECT ().Close ().AnyTimes ()
627597
628- // Iterate over all the nodes and start mining
629- time .Sleep (3 * time .Second )
630- for _ , node := range nodes {
631- if err := node .StartMining (1 ); err != nil {
632- panic (err )
598+ spanner := getMockedSpanner (t , heimdallSpan .ValidatorSet .Validators )
599+ _bor .SetSpanner (spanner )
600+ _bor .SetHeimdallClient (h )
601+
602+ db := init .ethereum .ChainDb ()
603+ block := init .genesis .ToBlock (db )
604+
605+ setDifficulty := func (header * types.Header ) {
606+ if IsSprintStart (header .Number .Uint64 ()) {
607+ header .Difficulty = big .NewInt (int64 (len (heimdallSpan .ValidatorSet .Validators )))
633608 }
634609 }
635610
636- for {
637-
638- // for block 1 to 8, the primary validator is node0
639- blockHeaderVal0 := nodes [0 ].BlockChain ().CurrentHeader ()
640- blockHeaderVal1 := nodes [1 ].BlockChain ().CurrentHeader ()
611+ for i := uint64 (1 ); i < spanSize ; i ++ {
612+ block = buildNextBlock (t , _bor , chain , block , nil , init .genesis .Config .Bor , nil , heimdallSpan .ValidatorSet .Validators , setDifficulty )
613+ insertNewBlock (t , chain , block )
614+ }
641615
642- // we remove peer connection between node0 and node1
643- if blockHeaderVal0 .Number .Uint64 () == 1 {
644- stacks [0 ].Server ().RemovePeer (enodes [1 ])
645- }
616+ // insert spanSize-th block
617+ // This account is one the out-of-turn validators for 1st (0-indexed) span
618+ signer := "c8deb0bea5c41afe8e37b4d1bd84e31adff11b09c8c96ff4b605003cce067cd9"
619+ signerKey , _ := hex .DecodeString (signer )
620+ newKey , _ := crypto .HexToECDSA (signer )
621+ newAddr := crypto .PubkeyToAddress (newKey .PublicKey )
622+ expectedSuccessionNumber := 2
646623
647- if blockHeaderVal1 .Number .Uint64 () == 9 {
648- break
649- }
624+ parentTime := block .Time ()
650625
626+ setParentTime := func (header * types.Header ) {
627+ header .Time = parentTime + 1
651628 }
652629
653- // function to get timestamp difference between two nodes of the same blockNumber
654- timeDiff := func (blockNum int ) int {
655- blockHeaderVal0 := nodes [0 ].BlockChain ().GetHeaderByNumber (uint64 (blockNum ))
656- blockHeaderVal1 := nodes [1 ].BlockChain ().GetHeaderByNumber (uint64 (blockNum ))
630+ const turn = 1
657631
658- return Abs (int (blockHeaderVal0 .Time - blockHeaderVal1 .Time ))
632+ setDifficulty = func (header * types.Header ) {
633+ header .Difficulty = big .NewInt (int64 (len (heimdallSpan .ValidatorSet .Validators )) - turn )
659634 }
660635
661- // check block 10 miner ; expected author is node1 signer
662- blockHeaderVal0 := nodes [0 ].BlockChain ().GetHeaderByNumber (3 )
663- blockHeaderVal1 := nodes [1 ].BlockChain ().GetHeaderByNumber (3 )
636+ block = buildNextBlock (t , _bor , chain , block , signerKey , init .genesis .Config .Bor , nil , heimdallSpan .ValidatorSet .Validators , setParentTime , setDifficulty )
637+ _ , err := chain .InsertChain ([]* types.Block {block })
638+ require .Equal (t ,
639+ bor.BlockTooSoonError {Number : spanSize , Succession : expectedSuccessionNumber },
640+ * err .(* bor.BlockTooSoonError ))
664641
665- // check both nodes have the same block 10
666- assert . Equal ( t , blockHeaderVal0 . Difficulty , common . Big2 )
642+ expectedDifficulty := uint64 ( len ( heimdallSpan . ValidatorSet . Validators ) - expectedSuccessionNumber - turn ) // len(validators) - succession
643+ header := block . Header ( )
667644
668- // check node0 has block mined by node1
669- assert . Equal ( t , blockHeaderVal1 . Difficulty , common . Big1 )
645+ diff := bor . CalcProducerDelay ( header . Number . Uint64 (), expectedSuccessionNumber , init . genesis . Config . Bor )
646+ header . Time += diff
670647
671- // node seperated at block 1, so at block 3, node0 should be at 3 seconds, and node1 should be at 5( 1 + 2*2 ) seconds
672- assert .Equal (t , timeDiff (3 ), 2 )
648+ sign (t , header , signerKey , init .genesis .Config .Bor )
673649
674- // check block 10 miner ; expected author is node1 signer
675- blockHeaderVal0 = nodes [0 ].BlockChain ().GetHeaderByNumber (7 )
676- blockHeaderVal1 = nodes [1 ].BlockChain ().GetHeaderByNumber (7 )
650+ block = types .NewBlockWithHeader (header )
677651
678- // check both nodes have the same block 10
679- assert .Equal (t , blockHeaderVal0 .Difficulty , common .Big2 )
652+ _ , err = chain .InsertChain ([]* types.Block {block })
653+ require .NotNil (t , err )
654+ require .Equal (t ,
655+ bor.WrongDifficultyError {Number : spanSize , Expected : expectedDifficulty , Actual : 3 , Signer : newAddr .Bytes ()},
656+ * err .(* bor.WrongDifficultyError ))
680657
681- // check node0 has block mined by node1
682- assert .Equal (t , blockHeaderVal1 .Difficulty , common .Big1 )
658+ header .Difficulty = new (big.Int ).SetUint64 (expectedDifficulty )
659+ sign (t , header , signerKey , init .genesis .Config .Bor )
660+ block = types .NewBlockWithHeader (header )
683661
684- // node seperated at block 1, so at block 7, node0 should be at 7 seconds, and node1 should be at 13( 1 + 2*6 ) seconds
685- assert . Equal (t , timeDiff ( 7 ), 6 )
662+ _ , err = chain . InsertChain ([] * types. Block { block })
663+ require . Nil (t , err )
686664}
687665
688666func TestSignerNotFound (t * testing.T ) {
@@ -1121,18 +1099,11 @@ func TestJaipurFork(t *testing.T) {
11211099
11221100 res , _ := loadSpanFromFile (t )
11231101
1124- currentValidators := []* valset.Validator {valset .NewValidator (addr , 10 )}
1125-
1126- spanner := getMockedSpanner (t , currentValidators )
1102+ spanner := getMockedSpanner (t , res .Result .ValidatorSet .Validators )
11271103 _bor .SetSpanner (spanner )
11281104
11291105 for i := uint64 (1 ); i < sprintSize ; i ++ {
1130- if IsSpanEnd (i ) {
1131- currentValidators = res .Result .ValidatorSet .Validators
1132- }
1133-
1134- block = buildNextBlock (t , _bor , chain , block , nil , init .genesis .Config .Bor , nil , currentValidators )
1135-
1106+ block = buildNextBlock (t , _bor , chain , block , nil , init .genesis .Config .Bor , nil , res .Result .ValidatorSet .Validators )
11361107 insertNewBlock (t , chain , block )
11371108
11381109 if block .Number ().Uint64 () == init .genesis .Config .Bor .JaipurBlock .Uint64 ()- 1 {
0 commit comments