github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/miner/miner_test.go (about) 1 // Copyright 2020 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 miner implements Ethereum block creation and mining. 18 package miner 19 20 import ( 21 "errors" 22 "testing" 23 "time" 24 25 "github.com/tirogen/go-ethereum/common" 26 "github.com/tirogen/go-ethereum/consensus/clique" 27 "github.com/tirogen/go-ethereum/core" 28 "github.com/tirogen/go-ethereum/core/rawdb" 29 "github.com/tirogen/go-ethereum/core/state" 30 "github.com/tirogen/go-ethereum/core/txpool" 31 "github.com/tirogen/go-ethereum/core/types" 32 "github.com/tirogen/go-ethereum/core/vm" 33 "github.com/tirogen/go-ethereum/eth/downloader" 34 "github.com/tirogen/go-ethereum/event" 35 "github.com/tirogen/go-ethereum/trie" 36 ) 37 38 type mockBackend struct { 39 bc *core.BlockChain 40 txPool *txpool.TxPool 41 } 42 43 func NewMockBackend(bc *core.BlockChain, txPool *txpool.TxPool) *mockBackend { 44 return &mockBackend{ 45 bc: bc, 46 txPool: txPool, 47 } 48 } 49 50 func (m *mockBackend) BlockChain() *core.BlockChain { 51 return m.bc 52 } 53 54 func (m *mockBackend) TxPool() *txpool.TxPool { 55 return m.txPool 56 } 57 58 func (m *mockBackend) StateAtBlock(block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (statedb *state.StateDB, err error) { 59 return nil, errors.New("not supported") 60 } 61 62 type testBlockChain struct { 63 statedb *state.StateDB 64 gasLimit uint64 65 chainHeadFeed *event.Feed 66 } 67 68 func (bc *testBlockChain) CurrentBlock() *types.Block { 69 return types.NewBlock(&types.Header{ 70 GasLimit: bc.gasLimit, 71 }, nil, nil, nil, trie.NewStackTrie(nil)) 72 } 73 74 func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { 75 return bc.CurrentBlock() 76 } 77 78 func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) { 79 return bc.statedb, nil 80 } 81 82 func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 83 return bc.chainHeadFeed.Subscribe(ch) 84 } 85 86 func TestMiner(t *testing.T) { 87 miner, mux, cleanup := createMiner(t) 88 defer cleanup(false) 89 miner.Start(common.HexToAddress("0x12345")) 90 waitForMiningState(t, miner, true) 91 // Start the downloader 92 mux.Post(downloader.StartEvent{}) 93 waitForMiningState(t, miner, false) 94 // Stop the downloader and wait for the update loop to run 95 mux.Post(downloader.DoneEvent{}) 96 waitForMiningState(t, miner, true) 97 98 // Subsequent downloader events after a successful DoneEvent should not cause the 99 // miner to start or stop. This prevents a security vulnerability 100 // that would allow entities to present fake high blocks that would 101 // stop mining operations by causing a downloader sync 102 // until it was discovered they were invalid, whereon mining would resume. 103 mux.Post(downloader.StartEvent{}) 104 waitForMiningState(t, miner, true) 105 106 mux.Post(downloader.FailedEvent{}) 107 waitForMiningState(t, miner, true) 108 } 109 110 // TestMinerDownloaderFirstFails tests that mining is only 111 // permitted to run indefinitely once the downloader sees a DoneEvent (success). 112 // An initial FailedEvent should allow mining to stop on a subsequent 113 // downloader StartEvent. 114 func TestMinerDownloaderFirstFails(t *testing.T) { 115 miner, mux, cleanup := createMiner(t) 116 defer cleanup(false) 117 miner.Start(common.HexToAddress("0x12345")) 118 waitForMiningState(t, miner, true) 119 // Start the downloader 120 mux.Post(downloader.StartEvent{}) 121 waitForMiningState(t, miner, false) 122 123 // Stop the downloader and wait for the update loop to run 124 mux.Post(downloader.FailedEvent{}) 125 waitForMiningState(t, miner, true) 126 127 // Since the downloader hasn't yet emitted a successful DoneEvent, 128 // we expect the miner to stop on next StartEvent. 129 mux.Post(downloader.StartEvent{}) 130 waitForMiningState(t, miner, false) 131 132 // Downloader finally succeeds. 133 mux.Post(downloader.DoneEvent{}) 134 waitForMiningState(t, miner, true) 135 136 // Downloader starts again. 137 // Since it has achieved a DoneEvent once, we expect miner 138 // state to be unchanged. 139 mux.Post(downloader.StartEvent{}) 140 waitForMiningState(t, miner, true) 141 142 mux.Post(downloader.FailedEvent{}) 143 waitForMiningState(t, miner, true) 144 } 145 146 func TestMinerStartStopAfterDownloaderEvents(t *testing.T) { 147 miner, mux, cleanup := createMiner(t) 148 defer cleanup(false) 149 miner.Start(common.HexToAddress("0x12345")) 150 waitForMiningState(t, miner, true) 151 // Start the downloader 152 mux.Post(downloader.StartEvent{}) 153 waitForMiningState(t, miner, false) 154 155 // Downloader finally succeeds. 156 mux.Post(downloader.DoneEvent{}) 157 waitForMiningState(t, miner, true) 158 159 miner.Stop() 160 waitForMiningState(t, miner, false) 161 162 miner.Start(common.HexToAddress("0x678910")) 163 waitForMiningState(t, miner, true) 164 165 miner.Stop() 166 waitForMiningState(t, miner, false) 167 } 168 169 func TestStartWhileDownload(t *testing.T) { 170 miner, mux, cleanup := createMiner(t) 171 defer cleanup(false) 172 waitForMiningState(t, miner, false) 173 miner.Start(common.HexToAddress("0x12345")) 174 waitForMiningState(t, miner, true) 175 // Stop the downloader and wait for the update loop to run 176 mux.Post(downloader.StartEvent{}) 177 waitForMiningState(t, miner, false) 178 // Starting the miner after the downloader should not work 179 miner.Start(common.HexToAddress("0x12345")) 180 waitForMiningState(t, miner, false) 181 } 182 183 func TestStartStopMiner(t *testing.T) { 184 miner, _, cleanup := createMiner(t) 185 defer cleanup(false) 186 waitForMiningState(t, miner, false) 187 miner.Start(common.HexToAddress("0x12345")) 188 waitForMiningState(t, miner, true) 189 miner.Stop() 190 waitForMiningState(t, miner, false) 191 } 192 193 func TestCloseMiner(t *testing.T) { 194 miner, _, cleanup := createMiner(t) 195 defer cleanup(true) 196 waitForMiningState(t, miner, false) 197 miner.Start(common.HexToAddress("0x12345")) 198 waitForMiningState(t, miner, true) 199 // Terminate the miner and wait for the update loop to run 200 miner.Close() 201 waitForMiningState(t, miner, false) 202 } 203 204 // TestMinerSetEtherbase checks that etherbase becomes set even if mining isn't 205 // possible at the moment 206 func TestMinerSetEtherbase(t *testing.T) { 207 miner, mux, cleanup := createMiner(t) 208 defer cleanup(false) 209 // Start with a 'bad' mining address 210 miner.Start(common.HexToAddress("0xdead")) 211 waitForMiningState(t, miner, true) 212 // Start the downloader 213 mux.Post(downloader.StartEvent{}) 214 waitForMiningState(t, miner, false) 215 // Now user tries to configure proper mining address 216 miner.Start(common.HexToAddress("0x1337")) 217 // Stop the downloader and wait for the update loop to run 218 mux.Post(downloader.DoneEvent{}) 219 220 waitForMiningState(t, miner, true) 221 // The miner should now be using the good address 222 if got, exp := miner.coinbase, common.HexToAddress("0x1337"); got != exp { 223 t.Fatalf("Wrong coinbase, got %x expected %x", got, exp) 224 } 225 } 226 227 // waitForMiningState waits until either 228 // * the desired mining state was reached 229 // * a timeout was reached which fails the test 230 func waitForMiningState(t *testing.T, m *Miner, mining bool) { 231 t.Helper() 232 233 var state bool 234 for i := 0; i < 100; i++ { 235 time.Sleep(10 * time.Millisecond) 236 if state = m.Mining(); state == mining { 237 return 238 } 239 } 240 t.Fatalf("Mining() == %t, want %t", state, mining) 241 } 242 243 func createMiner(t *testing.T) (*Miner, *event.TypeMux, func(skipMiner bool)) { 244 // Create Ethash config 245 config := Config{ 246 Etherbase: common.HexToAddress("123456789"), 247 } 248 // Create chainConfig 249 chainDB := rawdb.NewMemoryDatabase() 250 genesis := core.DeveloperGenesisBlock(15, 11_500_000, common.HexToAddress("12345")) 251 chainConfig, _, err := core.SetupGenesisBlock(chainDB, trie.NewDatabase(chainDB), genesis) 252 if err != nil { 253 t.Fatalf("can't create new chain config: %v", err) 254 } 255 // Create consensus engine 256 engine := clique.New(chainConfig.Clique, chainDB) 257 // Create Ethereum backend 258 bc, err := core.NewBlockChain(chainDB, nil, genesis, nil, engine, vm.Config{}, nil, nil) 259 if err != nil { 260 t.Fatalf("can't create new chain %v", err) 261 } 262 statedb, _ := state.New(common.Hash{}, state.NewDatabase(chainDB), nil) 263 blockchain := &testBlockChain{statedb, 10000000, new(event.Feed)} 264 265 pool := txpool.NewTxPool(testTxPoolConfig, chainConfig, blockchain) 266 backend := NewMockBackend(bc, pool) 267 // Create event Mux 268 mux := new(event.TypeMux) 269 // Create Miner 270 miner := New(backend, &config, chainConfig, mux, engine, nil) 271 cleanup := func(skipMiner bool) { 272 bc.Stop() 273 engine.Close() 274 pool.Stop() 275 if !skipMiner { 276 miner.Close() 277 } 278 } 279 return miner, mux, cleanup 280 }