miner_test.go 8.7 KB

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