github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/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/DxChainNetwork/dxc/common" 24 "github.com/DxChainNetwork/dxc/core" 25 "github.com/DxChainNetwork/dxc/core/rawdb" 26 "github.com/DxChainNetwork/dxc/core/types" 27 "github.com/DxChainNetwork/dxc/core/vm" 28 "github.com/DxChainNetwork/dxc/crypto" 29 "github.com/DxChainNetwork/dxc/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(10000000000000000)}, 51 }, 52 BaseFee: big.NewInt(params.InitialBaseFee), 53 } 54 copy(genspec.ExtraData[extraVanity:], addr[:]) 55 genesis := genspec.MustCommit(db) 56 57 // Generate a batch of blocks, each properly signed 58 chain, _ := core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil) 59 defer chain.Stop() 60 61 blocks, _ := core.GenerateChain(params.AllCliqueProtocolChanges, genesis, engine, db, 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 genspec.MustCommit(db) 91 92 chain, _ = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil) 93 defer chain.Stop() 94 95 if _, err := chain.InsertChain(blocks[:2]); err != nil { 96 t.Fatalf("failed to insert initial blocks: %v", err) 97 } 98 if head := chain.CurrentBlock().NumberU64(); head != 2 { 99 t.Fatalf("chain head mismatch: have %d, want %d", head, 2) 100 } 101 102 // Simulate a crash by creating a new chain on top of the database, without 103 // flushing the dirty states out. Insert the last block, triggering a sidechain 104 // reimport. 105 chain, _ = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil) 106 defer chain.Stop() 107 108 if _, err := chain.InsertChain(blocks[2:]); err != nil { 109 t.Fatalf("failed to insert final block: %v", err) 110 } 111 if head := chain.CurrentBlock().NumberU64(); head != 3 { 112 t.Fatalf("chain head mismatch: have %d, want %d", head, 3) 113 } 114 } 115 116 func TestSealHash(t *testing.T) { 117 have := SealHash(&types.Header{ 118 Difficulty: new(big.Int), 119 Number: new(big.Int), 120 Extra: make([]byte, 32+65), 121 BaseFee: new(big.Int), 122 }) 123 want := common.HexToHash("0xbd3d1fa43fbc4c5bfcc91b179ec92e2861df3654de60468beb908ff805359e8f") 124 if have != want { 125 t.Errorf("have %x, want %x", have, want) 126 } 127 }