miner_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 TestStartStopMiner(t *testing.T) {
  87. miner, _ := createMiner(t)
  88. waitForMiningState(t, miner, false)
  89. miner.Start(common.HexToAddress("0x12345"))
  90. waitForMiningState(t, miner, true)
  91. miner.Stop()
  92. waitForMiningState(t, miner, false)
  93. }
  94. func TestCloseMiner(t *testing.T) {
  95. miner, _ := createMiner(t)
  96. waitForMiningState(t, miner, false)
  97. miner.Start(common.HexToAddress("0x12345"))
  98. waitForMiningState(t, miner, true)
  99. // Terminate the miner and wait for the update loop to run
  100. miner.Close()
  101. waitForMiningState(t, miner, false)
  102. }
  103. // waitForMiningState waits until either
  104. // * the desired mining state was reached
  105. // * a timeout was reached which fails the test
  106. func waitForMiningState(t *testing.T, m *Miner, mining bool) {
  107. t.Helper()
  108. var state bool
  109. for i := 0; i < 100; i++ {
  110. if state = m.Mining(); state == mining {
  111. return
  112. }
  113. time.Sleep(10 * time.Millisecond)
  114. }
  115. t.Fatalf("Mining() == %t, want %t", state, mining)
  116. }
  117. func createMiner(t *testing.T) (*Miner, *event.TypeMux) {
  118. // Create Ethash config
  119. config := Config{
  120. Etherbase: common.HexToAddress("123456789"),
  121. }
  122. // Create chainConfig
  123. memdb := memorydb.New()
  124. chainDB := rawdb.NewDatabase(memdb)
  125. genesis := core.DeveloperGenesisBlock(15, common.HexToAddress("12345"))
  126. chainConfig, _, err := core.SetupGenesisBlock(chainDB, genesis)
  127. if err != nil {
  128. t.Fatalf("can't create new chain config: %v", err)
  129. }
  130. // Create event Mux
  131. mux := new(event.TypeMux)
  132. // Create consensus engine
  133. engine := ethash.New(ethash.Config{}, []string{}, false)
  134. engine.SetThreads(-1)
  135. // Create isLocalBlock
  136. isLocalBlock := func(block *types.Block) bool {
  137. return true
  138. }
  139. // Create Ethereum backend
  140. limit := uint64(1000)
  141. bc, err := core.NewBlockChain(chainDB, new(core.CacheConfig), chainConfig, engine, vm.Config{}, isLocalBlock, &limit)
  142. if err != nil {
  143. t.Fatalf("can't create new chain %v", err)
  144. }
  145. statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
  146. blockchain := &testBlockChain{statedb, 10000000, new(event.Feed)}
  147. pool := core.NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain)
  148. backend := NewMockBackend(bc, pool)
  149. // Create Miner
  150. return New(backend, &config, chainConfig, mux, engine, isLocalBlock), mux
  151. }