miner_test.go 5.6 KB

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