miner_test.go 8.1 KB

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