github.com/core-coin/go-core/v2@v2.1.9/consensus/clique/clique_test.go (about) 1 // Copyright 2019 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core 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/core-coin/go-core/v2/common" 24 "github.com/core-coin/go-core/v2/core" 25 "github.com/core-coin/go-core/v2/core/rawdb" 26 "github.com/core-coin/go-core/v2/core/types" 27 "github.com/core-coin/go-core/v2/core/vm" 28 "github.com/core-coin/go-core/v2/crypto" 29 "github.com/core-coin/go-core/v2/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 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.UnmarshalPrivateKeyHex("ab856a9af6b0b651dd2f43b5e12193652ec1701c4da6f1c0d2a366ac4b9dabc9433ef09e41ca129552bd2c029086d9b03604de872a3b343204") 43 addr = key.Address() 44 engine = New(params.AllCliqueProtocolChanges.Clique, db) 45 signer = types.NewNucleusSigner(params.AllCliqueProtocolChanges.NetworkID) 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, err := core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil) 58 if err != nil { 59 t.Fatalf("cannot create a new block chain: %v", err) 60 } 61 defer chain.Stop() 62 63 blocks, _ := core.GenerateChain(params.AllCliqueProtocolChanges, genesis, engine, db, 3, func(i int, block *core.BlockGen) { 64 // The chain maker doesn't have access to a chain, so the difficulty will be 65 // lets unset (nil). Set it here to the correct value. 66 block.SetDifficulty(diffInTurn) 67 68 // We want to simulate an empty middle block, having the same state as the 69 // first one. The last is needs a state change again to force a reorg. 70 if i != 1 { 71 tx, err := types.SignTx(types.NewTransaction(block.TxNonce(addr), common.Address{0x00}, new(big.Int), params.TxEnergy, nil, nil), signer, key) 72 if err != nil { 73 panic(err) 74 } 75 block.AddTxWithChain(chain, tx) 76 } 77 }) 78 for i, block := range blocks { 79 header := block.Header() 80 if i > 0 { 81 header.ParentHash = blocks[i-1].Hash() 82 } 83 header.Extra = make([]byte, extraVanity+extraSeal) 84 header.Difficulty = diffInTurn 85 86 sig, err := crypto.Sign(SealHash(header).Bytes(), key) 87 if err != nil { 88 t.Fatalf("cannot sign: %v", err) 89 } 90 copy(header.Extra[len(header.Extra)-extraSeal:], sig) 91 blocks[i] = block.WithSeal(header) 92 } 93 // Insert the first two blocks and make sure the chain is valid 94 db = rawdb.NewMemoryDatabase() 95 genspec.MustCommit(db) 96 97 chain, err = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil) 98 if err != nil { 99 t.Fatalf("cannot create a new block chain: %v", err) 100 } 101 defer chain.Stop() 102 103 if _, err := chain.InsertChain(blocks[:2]); err != nil { 104 t.Fatalf("failed to insert initial blocks: %v", err) 105 } 106 if head := chain.CurrentBlock().NumberU64(); head != 2 { 107 t.Fatalf("chain head mismatch: have %d, want %d", head, 2) 108 } 109 110 // Simulate a crash by creating a new chain on top of the database, without 111 // flushing the dirty states out. Insert the last block, triggering a sidechain 112 // reimport. 113 chain, err = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil) 114 if err != nil { 115 t.Fatalf("cannot create a new block chain: %v", err) 116 } 117 defer chain.Stop() 118 119 if _, err := chain.InsertChain(blocks[2:]); err != nil { 120 t.Fatalf("failed to insert final block: %v", err) 121 } 122 if head := chain.CurrentBlock().NumberU64(); head != 3 { 123 t.Fatalf("chain head mismatch: have %d, want %d", head, 3) 124 } 125 }