github.com/ethereum/go-ethereum@v1.16.1/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/crypto" 28 "github.com/ethereum/go-ethereum/params" 29 ) 30 31 // This test case is a repro of an annoying bug that took us forever to catch. 32 // In Clique PoA networks, consecutive blocks might have the same state root (no 33 // block subsidy, empty block). If a node crashes, the chain ends up losing the 34 // recent state and needs to regenerate it from blocks already in the database. 35 // The bug was that processing the block *prior* to an empty one **also 36 // completes** the empty one, ending up in a known-block error. 37 func TestReimportMirroredState(t *testing.T) { 38 // Initialize a Clique chain with a single signer 39 var ( 40 db = rawdb.NewMemoryDatabase() 41 key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 42 addr = crypto.PubkeyToAddress(key.PublicKey) 43 engine = New(params.AllCliqueProtocolChanges.Clique, db) 44 signer = new(types.HomesteadSigner) 45 ) 46 genspec := &core.Genesis{ 47 Config: params.AllCliqueProtocolChanges, 48 ExtraData: make([]byte, extraVanity+common.AddressLength+extraSeal), 49 Alloc: map[common.Address]types.Account{ 50 addr: {Balance: big.NewInt(10000000000000000)}, 51 }, 52 BaseFee: big.NewInt(params.InitialBaseFee), 53 } 54 copy(genspec.ExtraData[extraVanity:], addr[:]) 55 56 // Generate a batch of blocks, each properly signed 57 chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), genspec, engine, nil) 58 defer chain.Stop() 59 60 _, blocks, _ := core.GenerateChainWithGenesis(genspec, engine, 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, block.BaseFee(), 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 chain, _ = core.NewBlockChain(db, genspec, engine, nil) 90 defer chain.Stop() 91 92 if _, err := chain.InsertChain(blocks[:2]); err != nil { 93 t.Fatalf("failed to insert initial blocks: %v", err) 94 } 95 if head := chain.CurrentBlock().Number.Uint64(); head != 2 { 96 t.Fatalf("chain head mismatch: have %d, want %d", head, 2) 97 } 98 99 // Simulate a crash by creating a new chain on top of the database, without 100 // flushing the dirty states out. Insert the last block, triggering a sidechain 101 // reimport. 102 chain, _ = core.NewBlockChain(db, genspec, engine, nil) 103 defer chain.Stop() 104 105 if _, err := chain.InsertChain(blocks[2:]); err != nil { 106 t.Fatalf("failed to insert final block: %v", err) 107 } 108 if head := chain.CurrentBlock().Number.Uint64(); head != 3 { 109 t.Fatalf("chain head mismatch: have %d, want %d", head, 3) 110 } 111 } 112 113 func TestSealHash(t *testing.T) { 114 have := SealHash(&types.Header{ 115 Difficulty: new(big.Int), 116 Number: new(big.Int), 117 Extra: make([]byte, 32+65), 118 BaseFee: new(big.Int), 119 }) 120 want := common.HexToHash("0xbd3d1fa43fbc4c5bfcc91b179ec92e2861df3654de60468beb908ff805359e8f") 121 if have != want { 122 t.Errorf("have %x, want %x", have, want) 123 } 124 }