|
| 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 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum/common" |
| 25 | + "github.com/ethereum/go-ethereum/consensus/ethash" |
| 26 | + "github.com/ethereum/go-ethereum/core" |
| 27 | + "github.com/ethereum/go-ethereum/core/rawdb" |
| 28 | + "github.com/ethereum/go-ethereum/core/state" |
| 29 | + "github.com/ethereum/go-ethereum/core/types" |
| 30 | + "github.com/ethereum/go-ethereum/core/vm" |
| 31 | + "github.com/ethereum/go-ethereum/eth/downloader" |
| 32 | + "github.com/ethereum/go-ethereum/ethdb/memorydb" |
| 33 | + "github.com/ethereum/go-ethereum/event" |
| 34 | + "github.com/ethereum/go-ethereum/params" |
| 35 | + "github.com/ethereum/go-ethereum/trie" |
| 36 | +) |
| 37 | + |
| 38 | +type mockBackend struct { |
| 39 | + bc *core.BlockChain |
| 40 | + txPool *core.TxPool |
| 41 | +} |
| 42 | + |
| 43 | +func NewMockBackend(bc *core.BlockChain, txPool *core.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() *core.TxPool { |
| 55 | + return m.txPool |
| 56 | +} |
| 57 | + |
| 58 | +type testBlockChain struct { |
| 59 | + statedb *state.StateDB |
| 60 | + gasLimit uint64 |
| 61 | + chainHeadFeed *event.Feed |
| 62 | +} |
| 63 | + |
| 64 | +func (bc *testBlockChain) CurrentBlock() *types.Block { |
| 65 | + return types.NewBlock(&types.Header{ |
| 66 | + GasLimit: bc.gasLimit, |
| 67 | + }, nil, nil, nil, new(trie.Trie)) |
| 68 | +} |
| 69 | + |
| 70 | +func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { |
| 71 | + return bc.CurrentBlock() |
| 72 | +} |
| 73 | + |
| 74 | +func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) { |
| 75 | + return bc.statedb, nil |
| 76 | +} |
| 77 | + |
| 78 | +func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { |
| 79 | + return bc.chainHeadFeed.Subscribe(ch) |
| 80 | +} |
| 81 | + |
| 82 | +func TestMiner(t *testing.T) { |
| 83 | + miner, mux := createMiner(t) |
| 84 | + miner.Start(common.HexToAddress("0x12345")) |
| 85 | + waitForMiningState(t, miner, true) |
| 86 | + // Start the downloader |
| 87 | + mux.Post(downloader.StartEvent{}) |
| 88 | + waitForMiningState(t, miner, false) |
| 89 | + // Stop the downloader and wait for the update loop to run |
| 90 | + mux.Post(downloader.DoneEvent{}) |
| 91 | + waitForMiningState(t, miner, true) |
| 92 | + // Start the downloader and wait for the update loop to run |
| 93 | + mux.Post(downloader.StartEvent{}) |
| 94 | + waitForMiningState(t, miner, false) |
| 95 | + // Stop the downloader and wait for the update loop to run |
| 96 | + mux.Post(downloader.FailedEvent{}) |
| 97 | + waitForMiningState(t, miner, true) |
| 98 | +} |
| 99 | + |
| 100 | +func TestStartStopMiner(t *testing.T) { |
| 101 | + miner, _ := createMiner(t) |
| 102 | + waitForMiningState(t, miner, false) |
| 103 | + miner.Start(common.HexToAddress("0x12345")) |
| 104 | + waitForMiningState(t, miner, true) |
| 105 | + miner.Stop() |
| 106 | + waitForMiningState(t, miner, false) |
| 107 | +} |
| 108 | + |
| 109 | +func TestCloseMiner(t *testing.T) { |
| 110 | + miner, _ := createMiner(t) |
| 111 | + waitForMiningState(t, miner, false) |
| 112 | + miner.Start(common.HexToAddress("0x12345")) |
| 113 | + waitForMiningState(t, miner, true) |
| 114 | + // Terminate the miner and wait for the update loop to run |
| 115 | + miner.Close() |
| 116 | + waitForMiningState(t, miner, false) |
| 117 | +} |
| 118 | + |
| 119 | +// waitForMiningState waits until either |
| 120 | +// * the desired mining state was reached |
| 121 | +// * a timeout was reached which fails the test |
| 122 | +func waitForMiningState(t *testing.T, m *Miner, mining bool) { |
| 123 | + t.Helper() |
| 124 | + |
| 125 | + var state bool |
| 126 | + for i := 0; i < 100; i++ { |
| 127 | + if state = m.Mining(); state == mining { |
| 128 | + return |
| 129 | + } |
| 130 | + time.Sleep(10 * time.Millisecond) |
| 131 | + } |
| 132 | + t.Fatalf("Mining() == %t, want %t", state, mining) |
| 133 | +} |
| 134 | + |
| 135 | +func createMiner(t *testing.T) (*Miner, *event.TypeMux) { |
| 136 | + // Create Ethash config |
| 137 | + config := Config{ |
| 138 | + Etherbase: common.HexToAddress("123456789"), |
| 139 | + } |
| 140 | + // Create chainConfig |
| 141 | + memdb := memorydb.New() |
| 142 | + chainDB := rawdb.NewDatabase(memdb) |
| 143 | + genesis := core.DeveloperGenesisBlock(15, common.HexToAddress("12345")) |
| 144 | + chainConfig, _, err := core.SetupGenesisBlock(chainDB, genesis) |
| 145 | + if err != nil { |
| 146 | + t.Fatalf("can't create new chain config: %v", err) |
| 147 | + } |
| 148 | + // Create event Mux |
| 149 | + mux := new(event.TypeMux) |
| 150 | + // Create consensus engine |
| 151 | + engine := ethash.New(ethash.Config{}, []string{}, false) |
| 152 | + engine.SetThreads(-1) |
| 153 | + // Create isLocalBlock |
| 154 | + isLocalBlock := func(block *types.Block) bool { |
| 155 | + return true |
| 156 | + } |
| 157 | + // Create Ethereum backend |
| 158 | + limit := uint64(1000) |
| 159 | + bc, err := core.NewBlockChain(chainDB, new(core.CacheConfig), chainConfig, engine, vm.Config{}, isLocalBlock, &limit) |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("can't create new chain %v", err) |
| 162 | + } |
| 163 | + statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) |
| 164 | + blockchain := &testBlockChain{statedb, 10000000, new(event.Feed)} |
| 165 | + |
| 166 | + pool := core.NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain) |
| 167 | + backend := NewMockBackend(bc, pool) |
| 168 | + // Create Miner |
| 169 | + return New(backend, &config, chainConfig, mux, engine, isLocalBlock), mux |
| 170 | +} |
0 commit comments