miner_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. // Subsequent downloader events after a successful DoneEvent should not cause the
  80. // miner to start or stop. This prevents a security vulnerability
  81. // that would allow entities to present fake high blocks that would
  82. // stop mining operations by causing a downloader sync
  83. // until it was discovered they were invalid, whereon mining would resume.
  84. mux.Post(downloader.StartEvent{})
  85. waitForMiningState(t, miner, true)
  86. mux.Post(downloader.FailedEvent{})
  87. waitForMiningState(t, miner, true)
  88. }
  89. // TestMinerDownloaderFirstFails tests that mining is only
  90. // permitted to run indefinitely once the downloader sees a DoneEvent (success).
  91. // An initial FailedEvent should allow mining to stop on a subsequent
  92. // downloader StartEvent.
  93. func TestMinerDownloaderFirstFails(t *testing.T) {
  94. miner, mux := createMiner(t)
  95. miner.Start(common.HexToAddress("0x12345"))
  96. waitForMiningState(t, miner, true)
  97. // Start the downloader
  98. mux.Post(downloader.StartEvent{})
  99. waitForMiningState(t, miner, false)
  100. // Stop the downloader and wait for the update loop to run
  101. mux.Post(downloader.FailedEvent{})
  102. waitForMiningState(t, miner, true)
  103. // Since the downloader hasn't yet emitted a successful DoneEvent,
  104. // we expect the miner to stop on next StartEvent.
  105. mux.Post(downloader.StartEvent{})
  106. waitForMiningState(t, miner, false)
  107. // Downloader finally succeeds.
  108. mux.Post(downloader.DoneEvent{})
  109. waitForMiningState(t, miner, true)
  110. // Downloader starts again.
  111. // Since it has achieved a DoneEvent once, we expect miner
  112. // state to be unchanged.
  113. mux.Post(downloader.StartEvent{})
  114. waitForMiningState(t, miner, true)
  115. mux.Post(downloader.FailedEvent{})
  116. waitForMiningState(t, miner, true)
  117. }
  118. func TestMinerStartStopAfterDownloaderEvents(t *testing.T) {
  119. miner, mux := createMiner(t)
  120. miner.Start(common.HexToAddress("0x12345"))
  121. waitForMiningState(t, miner, true)
  122. // Start the downloader
  123. mux.Post(downloader.StartEvent{})
  124. waitForMiningState(t, miner, false)
  125. // Downloader finally succeeds.
  126. mux.Post(downloader.DoneEvent{})
  127. waitForMiningState(t, miner, true)
  128. miner.Stop()
  129. waitForMiningState(t, miner, false)
  130. miner.Start(common.HexToAddress("0x678910"))
  131. waitForMiningState(t, miner, true)
  132. miner.Stop()
  133. waitForMiningState(t, miner, false)
  134. }
  135. func TestStartWhileDownload(t *testing.T) {
  136. miner, mux := createMiner(t)
  137. waitForMiningState(t, miner, false)
  138. miner.Start(common.HexToAddress("0x12345"))
  139. waitForMiningState(t, miner, true)
  140. // Stop the downloader and wait for the update loop to run
  141. mux.Post(downloader.StartEvent{})
  142. waitForMiningState(t, miner, false)
  143. // Starting the miner after the downloader should not work
  144. miner.Start(common.HexToAddress("0x12345"))
  145. waitForMiningState(t, miner, false)
  146. }
  147. func TestStartStopMiner(t *testing.T) {
  148. miner, _ := createMiner(t)
  149. waitForMiningState(t, miner, false)
  150. miner.Start(common.HexToAddress("0x12345"))
  151. waitForMiningState(t, miner, true)
  152. miner.Stop()
  153. waitForMiningState(t, miner, false)
  154. }
  155. func TestCloseMiner(t *testing.T) {
  156. miner, _ := createMiner(t)
  157. waitForMiningState(t, miner, false)
  158. miner.Start(common.HexToAddress("0x12345"))
  159. waitForMiningState(t, miner, true)
  160. // Terminate the miner and wait for the update loop to run
  161. miner.Close()
  162. waitForMiningState(t, miner, false)
  163. }
  164. // waitForMiningState waits until either
  165. // * the desired mining state was reached
  166. // * a timeout was reached which fails the test
  167. func waitForMiningState(t *testing.T, m *Miner, mining bool) {
  168. t.Helper()
  169. var state bool
  170. for i := 0; i < 100; i++ {
  171. time.Sleep(10 * time.Millisecond)
  172. if state = m.Mining(); state == mining {
  173. return
  174. }
  175. }
  176. t.Fatalf("Mining() == %t, want %t", state, mining)
  177. }
  178. func createMiner(t *testing.T) (*Miner, *event.TypeMux) {
  179. // Create Ethash config
  180. config := Config{
  181. Etherbase: common.HexToAddress("123456789"),
  182. }
  183. // Create chainConfig
  184. memdb := memorydb.New()
  185. chainDB := rawdb.NewDatabase(memdb)
  186. genesis := core.DeveloperGenesisBlock(15, common.HexToAddress("12345"))
  187. chainConfig, _, err := core.SetupGenesisBlock(chainDB, genesis)
  188. if err != nil {
  189. t.Fatalf("can't create new chain config: %v", err)
  190. }
  191. // Create event Mux
  192. mux := new(event.TypeMux)
  193. // Create consensus engine
  194. engine := ethash.New(ethash.Config{}, []string{}, false)
  195. engine.SetThreads(-1)
  196. // Create isLocalBlock
  197. isLocalBlock := func(block *types.Block) bool {
  198. return true
  199. }
  200. // Create Ethereum backend
  201. limit := uint64(1000)
  202. bc, err := core.NewBlockChain(chainDB, new(core.CacheConfig), chainConfig, engine, vm.Config{}, isLocalBlock, &limit)
  203. if err != nil {
  204. t.Fatalf("can't create new chain %v", err)
  205. }
  206. statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
  207. blockchain := &testBlockChain{statedb, 10000000, new(event.Feed)}
  208. pool := core.NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain)
  209. backend := NewMockBackend(bc, pool)
  210. // Create Miner
  211. return New(backend, &config, chainConfig, mux, engine, isLocalBlock), mux
  212. }