github.com/ethereum/go-ethereum@v1.16.1/core/dao_test.go (about) 1 // Copyright 2016 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 core 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/consensus/ethash" 24 "github.com/ethereum/go-ethereum/core/rawdb" 25 "github.com/ethereum/go-ethereum/params" 26 ) 27 28 // Tests that DAO-fork enabled clients can properly filter out fork-commencing 29 // blocks based on their extradata fields. 30 func TestDAOForkRangeExtradata(t *testing.T) { 31 forkBlock := big.NewInt(32) 32 chainConfig := *params.NonActivatedConfig 33 chainConfig.HomesteadBlock = big.NewInt(0) 34 35 // Generate a common prefix for both pro-forkers and non-forkers 36 gspec := &Genesis{ 37 BaseFee: big.NewInt(params.InitialBaseFee), 38 Config: &chainConfig, 39 } 40 genDb, prefix, _ := GenerateChainWithGenesis(gspec, ethash.NewFaker(), int(forkBlock.Int64()-1), func(i int, gen *BlockGen) {}) 41 42 // Create the concurrent, conflicting two nodes 43 proDb := rawdb.NewMemoryDatabase() 44 proConf := *params.NonActivatedConfig 45 proConf.HomesteadBlock = big.NewInt(0) 46 proConf.DAOForkBlock = forkBlock 47 proConf.DAOForkSupport = true 48 progspec := &Genesis{ 49 BaseFee: big.NewInt(params.InitialBaseFee), 50 Config: &proConf, 51 } 52 proBc, _ := NewBlockChain(proDb, progspec, ethash.NewFaker(), nil) 53 defer proBc.Stop() 54 55 conDb := rawdb.NewMemoryDatabase() 56 conConf := *params.NonActivatedConfig 57 conConf.HomesteadBlock = big.NewInt(0) 58 conConf.DAOForkBlock = forkBlock 59 conConf.DAOForkSupport = false 60 congspec := &Genesis{ 61 BaseFee: big.NewInt(params.InitialBaseFee), 62 Config: &conConf, 63 } 64 conBc, _ := NewBlockChain(conDb, congspec, ethash.NewFaker(), nil) 65 defer conBc.Stop() 66 67 if _, err := proBc.InsertChain(prefix); err != nil { 68 t.Fatalf("pro-fork: failed to import chain prefix: %v", err) 69 } 70 if _, err := conBc.InsertChain(prefix); err != nil { 71 t.Fatalf("con-fork: failed to import chain prefix: %v", err) 72 } 73 // Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks 74 for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ { 75 // Create a pro-fork block, and try to feed into the no-fork chain 76 bc, _ := NewBlockChain(rawdb.NewMemoryDatabase(), congspec, ethash.NewFaker(), nil) 77 78 blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().Number.Uint64())) 79 for j := 0; j < len(blocks)/2; j++ { 80 blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j] 81 } 82 if _, err := bc.InsertChain(blocks); err != nil { 83 t.Fatalf("failed to import contra-fork chain for expansion: %v", err) 84 } 85 if err := bc.triedb.Commit(bc.CurrentHeader().Root, false); err != nil { 86 t.Fatalf("failed to commit contra-fork head for expansion: %v", err) 87 } 88 bc.Stop() 89 blocks, _ = GenerateChain(&proConf, conBc.GetBlockByHash(conBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {}) 90 if _, err := conBc.InsertChain(blocks); err == nil { 91 t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0]) 92 } 93 // Create a proper no-fork block for the contra-forker 94 blocks, _ = GenerateChain(&conConf, conBc.GetBlockByHash(conBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {}) 95 if _, err := conBc.InsertChain(blocks); err != nil { 96 t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err) 97 } 98 // Create a no-fork block, and try to feed into the pro-fork chain 99 bc, _ = NewBlockChain(rawdb.NewMemoryDatabase(), progspec, ethash.NewFaker(), nil) 100 101 blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().Number.Uint64())) 102 for j := 0; j < len(blocks)/2; j++ { 103 blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j] 104 } 105 if _, err := bc.InsertChain(blocks); err != nil { 106 t.Fatalf("failed to import pro-fork chain for expansion: %v", err) 107 } 108 if err := bc.triedb.Commit(bc.CurrentHeader().Root, false); err != nil { 109 t.Fatalf("failed to commit pro-fork head for expansion: %v", err) 110 } 111 bc.Stop() 112 blocks, _ = GenerateChain(&conConf, proBc.GetBlockByHash(proBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {}) 113 if _, err := proBc.InsertChain(blocks); err == nil { 114 t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0]) 115 } 116 // Create a proper pro-fork block for the pro-forker 117 blocks, _ = GenerateChain(&proConf, proBc.GetBlockByHash(proBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {}) 118 if _, err := proBc.InsertChain(blocks); err != nil { 119 t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err) 120 } 121 } 122 // Verify that contra-forkers accept pro-fork extra-datas after forking finishes 123 bc, _ := NewBlockChain(rawdb.NewMemoryDatabase(), congspec, ethash.NewFaker(), nil) 124 defer bc.Stop() 125 126 blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().Number.Uint64())) 127 for j := 0; j < len(blocks)/2; j++ { 128 blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j] 129 } 130 if _, err := bc.InsertChain(blocks); err != nil { 131 t.Fatalf("failed to import contra-fork chain for expansion: %v", err) 132 } 133 if err := bc.triedb.Commit(bc.CurrentHeader().Root, false); err != nil { 134 t.Fatalf("failed to commit contra-fork head for expansion: %v", err) 135 } 136 blocks, _ = GenerateChain(&proConf, conBc.GetBlockByHash(conBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {}) 137 if _, err := conBc.InsertChain(blocks); err != nil { 138 t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err) 139 } 140 // Verify that pro-forkers accept contra-fork extra-datas after forking finishes 141 bc, _ = NewBlockChain(rawdb.NewMemoryDatabase(), progspec, ethash.NewFaker(), nil) 142 defer bc.Stop() 143 144 blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().Number.Uint64())) 145 for j := 0; j < len(blocks)/2; j++ { 146 blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j] 147 } 148 if _, err := bc.InsertChain(blocks); err != nil { 149 t.Fatalf("failed to import pro-fork chain for expansion: %v", err) 150 } 151 if err := bc.triedb.Commit(bc.CurrentHeader().Root, false); err != nil { 152 t.Fatalf("failed to commit pro-fork head for expansion: %v", err) 153 } 154 blocks, _ = GenerateChain(&conConf, proBc.GetBlockByHash(proBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {}) 155 if _, err := proBc.InsertChain(blocks); err != nil { 156 t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err) 157 } 158 }