github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/consensus/clique/clique_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package clique 19 20 import ( 21 "math/big" 22 "testing" 23 24 "github.com/AigarNetwork/aigar/common" 25 "github.com/AigarNetwork/aigar/core" 26 "github.com/AigarNetwork/aigar/core/rawdb" 27 "github.com/AigarNetwork/aigar/core/types" 28 "github.com/AigarNetwork/aigar/core/vm" 29 "github.com/AigarNetwork/aigar/crypto" 30 "github.com/AigarNetwork/aigar/params" 31 ) 32 33 // This test case is a repro of an annoying bug that took us forever to catch. 34 // In Clique PoA networks (Rinkeby, Görli, etc), consecutive blocks might have 35 // the same state root (no block subsidy, empty block). If a node crashes, the 36 // chain ends up losing the recent state and needs to regenerate it from blocks 37 // already in the database. The bug was that processing the block *prior* to an 38 // empty one **also completes** the empty one, ending up in a known-block error. 39 func TestReimportMirroredState(t *testing.T) { 40 // Initialize a Clique chain with a single signer 41 var ( 42 db = rawdb.NewMemoryDatabase() 43 key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 44 addr = crypto.PubkeyToAddress(key.PublicKey) 45 engine = New(params.AllCliqueProtocolChanges.Clique, db) 46 signer = new(types.HomesteadSigner) 47 ) 48 genspec := &core.Genesis{ 49 ExtraData: make([]byte, extraVanity+common.AddressLength+extraSeal), 50 Alloc: map[common.Address]core.GenesisAccount{ 51 addr: {Balance: big.NewInt(1)}, 52 }, 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) 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, nil, 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) 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, trigerring a sidechain 104 // reimport. 105 chain, _ = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, 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 }