@@ -27,6 +27,8 @@ import (
27
27
"testing"
28
28
"time"
29
29
30
+ "github.com/ethereum/go-ethereum/consensus/clique"
31
+
30
32
"golang.org/x/crypto/sha3"
31
33
32
34
"github.com/ethereum/go-ethereum/common"
@@ -644,3 +646,118 @@ func TestGetRootByDiffHash(t *testing.T) {
644
646
testGetRootByDiffHash (t , chain1 , chain2 , 24 , types .StatusBlockNewer )
645
647
testGetRootByDiffHash (t , chain1 , chain2 , 35 , types .StatusBlockTooNew )
646
648
}
649
+
650
+ func newBlockChainWithCliqueEngine (blocks int ) * BlockChain {
651
+ signer := types.HomesteadSigner {}
652
+ db := rawdb .NewMemoryDatabase ()
653
+ engine := clique .New (params .AllCliqueProtocolChanges .Clique , db )
654
+ genspec := & Genesis {
655
+ //Config: params.TestChainConfig,
656
+ ExtraData : make ([]byte , 32 + common .AddressLength + 65 ),
657
+ Alloc : GenesisAlloc {testAddr : {Balance : big .NewInt (100000000000000000 )}},
658
+ }
659
+ copy (genspec .ExtraData [32 :], testAddr [:])
660
+ genesis := genspec .MustCommit (db )
661
+
662
+ chain , _ := NewBlockChain (db , nil , params .AllCliqueProtocolChanges , engine , vm.Config {}, nil , nil )
663
+ generator := func (i int , block * BlockGen ) {
664
+ // The chain maker doesn't have access to a chain, so the difficulty will be
665
+ // lets unset (nil). Set it here to the correct value.
666
+ // block.SetCoinbase(testAddr)
667
+ block .SetDifficulty (big .NewInt (2 ))
668
+
669
+ for idx , testBlock := range testBlocks {
670
+ // Specific block setting, the index in this generator has 1 diff from specified blockNr.
671
+ if i + 1 == testBlock .blockNr {
672
+ for _ , testTransaction := range testBlock .txs {
673
+ var transaction * types.Transaction
674
+ if testTransaction .to == nil {
675
+ transaction = types .NewContractCreation (block .TxNonce (testAddr ),
676
+ testTransaction .value , uint64 (commonGas ), nil , testTransaction .data )
677
+ } else {
678
+ transaction = types .NewTransaction (block .TxNonce (testAddr ), * testTransaction .to ,
679
+ testTransaction .value , uint64 (commonGas ), nil , testTransaction .data )
680
+ }
681
+ tx , err := types .SignTx (transaction , signer , testKey )
682
+ if err != nil {
683
+ panic (err )
684
+ }
685
+ block .AddTxWithChain (chain , tx )
686
+ }
687
+ break
688
+ }
689
+
690
+ // Default block setting.
691
+ if idx == len (testBlocks )- 1 {
692
+ // We want to simulate an empty middle block, having the same state as the
693
+ // first one. The last is needs a state change again to force a reorg.
694
+ for _ , testTransaction := range testBlocks [0 ].txs {
695
+ tx , err := types .SignTx (types .NewTransaction (block .TxNonce (testAddr ), * testTransaction .to ,
696
+ testTransaction .value , uint64 (commonGas ), nil , testTransaction .data ), signer , testKey )
697
+ if err != nil {
698
+ panic (err )
699
+ }
700
+ block .AddTxWithChain (chain , tx )
701
+ }
702
+ }
703
+ }
704
+
705
+ }
706
+ bs , _ := GenerateChain (params .AllCliqueProtocolChanges , genesis , engine , db , blocks , generator )
707
+ for i , block := range bs {
708
+ header := block .Header ()
709
+ if i > 0 {
710
+ header .ParentHash = bs [i - 1 ].Hash ()
711
+ }
712
+ header .Extra = make ([]byte , 32 + 65 )
713
+ header .Difficulty = big .NewInt (2 )
714
+
715
+ sig , _ := crypto .Sign (clique .SealHash (header ).Bytes (), testKey )
716
+ copy (header .Extra [len (header .Extra )- 65 :], sig )
717
+ bs [i ] = block .WithSeal (header )
718
+ }
719
+
720
+ if _ , err := chain .InsertChain (bs ); err != nil {
721
+ panic (err )
722
+ }
723
+
724
+ return chain
725
+ }
726
+
727
+ func TestGenerateDiffLayer (t * testing.T ) {
728
+ blockNum := 32
729
+ chain := newBlockChainWithCliqueEngine (blockNum )
730
+ defer chain .Stop ()
731
+
732
+ for blockNr := 1 ; blockNr <= blockNum ; blockNr ++ {
733
+ block := chain .GetBlockByNumber (uint64 (blockNr ))
734
+ if block == nil {
735
+ t .Fatal ("block should not be nil" )
736
+ }
737
+
738
+ expDiffLayer := chain .GetTrustedDiffLayer (block .Hash ())
739
+ if expDiffLayer == nil {
740
+ // Skip empty block.
741
+ if blockNr == 15 {
742
+ continue
743
+ }
744
+ t .Fatalf ("unexpected nil diff layer, block number: %v, block hash: %v" , blockNr , block .Hash ())
745
+ }
746
+ expDiffHash , err := GetTrustedDiffHash (expDiffLayer )
747
+ if err != nil {
748
+ t .Fatalf ("compute diff hash failed: %v" , err )
749
+ }
750
+
751
+ diffLayer , err := chain .GenerateDiffLayer (block .Hash ())
752
+ if err != nil || diffLayer == nil {
753
+ t .Fatalf ("generate diff layer failed: %v" , err )
754
+ }
755
+ diffHash , err := GetTrustedDiffHash (diffLayer )
756
+ if err != nil {
757
+ t .Fatalf ("compute diff hash failed: %v" , err )
758
+ }
759
+ if expDiffHash != diffHash {
760
+ t .Fatalf ("generated wrong diff layer for block: %d, expected hash: %v, real hash: %v" , blockNr , expDiffHash , diffHash )
761
+ }
762
+ }
763
+ }
0 commit comments