github.hscsec.cn/scroll-tech/go-ethereum@v1.9.7/consensus/clique/clique_test.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package clique
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core"
    25  	"github.com/ethereum/go-ethereum/core/rawdb"
    26  	"github.com/ethereum/go-ethereum/core/types"
    27  	"github.com/ethereum/go-ethereum/core/vm"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  	"github.com/ethereum/go-ethereum/params"
    30  )
    31  
    32  // This test case is a repro of an annoying bug that took us forever to catch.
    33  // In Clique PoA networks (Rinkeby, Görli, etc), consecutive blocks might have
    34  // the same state root (no block subsidy, empty block). If a node crashes, the
    35  // chain ends up losing the recent state and needs to regenerate it from blocks
    36  // already in the database. The bug was that processing the block *prior* to an
    37  // empty one **also completes** the empty one, ending up in a known-block error.
    38  func TestReimportMirroredState(t *testing.T) {
    39  	// Initialize a Clique chain with a single signer
    40  	var (
    41  		db     = rawdb.NewMemoryDatabase()
    42  		key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    43  		addr   = crypto.PubkeyToAddress(key.PublicKey)
    44  		engine = New(params.AllCliqueProtocolChanges.Clique, db)
    45  		signer = new(types.HomesteadSigner)
    46  	)
    47  	genspec := &core.Genesis{
    48  		ExtraData: make([]byte, extraVanity+common.AddressLength+extraSeal),
    49  		Alloc: map[common.Address]core.GenesisAccount{
    50  			addr: {Balance: big.NewInt(1)},
    51  		},
    52  	}
    53  	copy(genspec.ExtraData[extraVanity:], addr[:])
    54  	genesis := genspec.MustCommit(db)
    55  
    56  	// Generate a batch of blocks, each properly signed
    57  	chain, _ := core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil)
    58  	defer chain.Stop()
    59  
    60  	blocks, _ := core.GenerateChain(params.AllCliqueProtocolChanges, genesis, engine, db, 3, func(i int, block *core.BlockGen) {
    61  		// The chain maker doesn't have access to a chain, so the difficulty will be
    62  		// lets unset (nil). Set it here to the correct value.
    63  		block.SetDifficulty(diffInTurn)
    64  
    65  		// We want to simulate an empty middle block, having the same state as the
    66  		// first one. The last is needs a state change again to force a reorg.
    67  		if i != 1 {
    68  			tx, err := types.SignTx(types.NewTransaction(block.TxNonce(addr), common.Address{0x00}, new(big.Int), params.TxGas, nil, nil), signer, key)
    69  			if err != nil {
    70  				panic(err)
    71  			}
    72  			block.AddTxWithChain(chain, tx)
    73  		}
    74  	})
    75  	for i, block := range blocks {
    76  		header := block.Header()
    77  		if i > 0 {
    78  			header.ParentHash = blocks[i-1].Hash()
    79  		}
    80  		header.Extra = make([]byte, extraVanity+extraSeal)
    81  		header.Difficulty = diffInTurn
    82  
    83  		sig, _ := crypto.Sign(SealHash(header).Bytes(), key)
    84  		copy(header.Extra[len(header.Extra)-extraSeal:], sig)
    85  		blocks[i] = block.WithSeal(header)
    86  	}
    87  	// Insert the first two blocks and make sure the chain is valid
    88  	db = rawdb.NewMemoryDatabase()
    89  	genspec.MustCommit(db)
    90  
    91  	chain, _ = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil)
    92  	defer chain.Stop()
    93  
    94  	if _, err := chain.InsertChain(blocks[:2]); err != nil {
    95  		t.Fatalf("failed to insert initial blocks: %v", err)
    96  	}
    97  	if head := chain.CurrentBlock().NumberU64(); head != 2 {
    98  		t.Fatalf("chain head mismatch: have %d, want %d", head, 2)
    99  	}
   100  
   101  	// Simulate a crash by creating a new chain on top of the database, without
   102  	// flushing the dirty states out. Insert the last block, trigerring a sidechain
   103  	// reimport.
   104  	chain, _ = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil)
   105  	defer chain.Stop()
   106  
   107  	if _, err := chain.InsertChain(blocks[2:]); err != nil {
   108  		t.Fatalf("failed to insert final block: %v", err)
   109  	}
   110  	if head := chain.CurrentBlock().NumberU64(); head != 3 {
   111  		t.Fatalf("chain head mismatch: have %d, want %d", head, 3)
   112  	}
   113  }