github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/clique/clique_test.go (about)

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