github.com/theQRL/go-zond@v0.1.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/theQRL/go-zond/common" 24 "github.com/theQRL/go-zond/core" 25 "github.com/theQRL/go-zond/core/rawdb" 26 "github.com/theQRL/go-zond/core/types" 27 "github.com/theQRL/go-zond/core/vm" 28 "github.com/theQRL/go-zond/crypto" 29 "github.com/theQRL/go-zond/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 (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 Config: params.AllCliqueProtocolChanges, 49 ExtraData: make([]byte, extraVanity+common.AddressLength+extraSeal), 50 Alloc: map[common.Address]core.GenesisAccount{ 51 addr: {Balance: big.NewInt(10000000000000000)}, 52 }, 53 BaseFee: big.NewInt(params.InitialBaseFee), 54 } 55 copy(genspec.ExtraData[extraVanity:], addr[:]) 56 57 // Generate a batch of blocks, each properly signed 58 chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, genspec, nil, engine, vm.Config{}, nil, nil) 59 defer chain.Stop() 60 61 _, blocks, _ := core.GenerateChainWithGenesis(genspec, engine, 3, func(i int, block *core.BlockGen) { 62 // The chain maker doesn't have access to a chain, so the difficulty will be 63 // lets unset (nil). Set it here to the correct value. 64 block.SetDifficulty(diffInTurn) 65 66 // We want to simulate an empty middle block, having the same state as the 67 // first one. The last is needs a state change again to force a reorg. 68 if i != 1 { 69 tx, err := types.SignTx(types.NewTransaction(block.TxNonce(addr), common.Address{0x00}, new(big.Int), params.TxGas, block.BaseFee(), nil), signer, key) 70 if err != nil { 71 panic(err) 72 } 73 block.AddTxWithChain(chain, tx) 74 } 75 }) 76 for i, block := range blocks { 77 header := block.Header() 78 if i > 0 { 79 header.ParentHash = blocks[i-1].Hash() 80 } 81 header.Extra = make([]byte, extraVanity+extraSeal) 82 header.Difficulty = diffInTurn 83 84 sig, _ := crypto.Sign(SealHash(header).Bytes(), key) 85 copy(header.Extra[len(header.Extra)-extraSeal:], sig) 86 blocks[i] = block.WithSeal(header) 87 } 88 // Insert the first two blocks and make sure the chain is valid 89 db = rawdb.NewMemoryDatabase() 90 chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil, nil) 91 defer chain.Stop() 92 93 if _, err := chain.InsertChain(blocks[:2]); err != nil { 94 t.Fatalf("failed to insert initial blocks: %v", err) 95 } 96 if head := chain.CurrentBlock().Number.Uint64(); head != 2 { 97 t.Fatalf("chain head mismatch: have %d, want %d", head, 2) 98 } 99 100 // Simulate a crash by creating a new chain on top of the database, without 101 // flushing the dirty states out. Insert the last block, triggering a sidechain 102 // reimport. 103 chain, _ = core.NewBlockChain(db, nil, genspec, nil, engine, vm.Config{}, nil, nil) 104 defer chain.Stop() 105 106 if _, err := chain.InsertChain(blocks[2:]); err != nil { 107 t.Fatalf("failed to insert final block: %v", err) 108 } 109 if head := chain.CurrentBlock().Number.Uint64(); head != 3 { 110 t.Fatalf("chain head mismatch: have %d, want %d", head, 3) 111 } 112 } 113 114 func TestSealHash(t *testing.T) { 115 have := SealHash(&types.Header{ 116 Difficulty: new(big.Int), 117 Number: new(big.Int), 118 Extra: make([]byte, 32+65), 119 BaseFee: new(big.Int), 120 }) 121 want := common.HexToHash("0xbd3d1fa43fbc4c5bfcc91b179ec92e2861df3654de60468beb908ff805359e8f") 122 if have != want { 123 t.Errorf("have %x, want %x", have, want) 124 } 125 }