worker_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // Copyright 2018 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
  17. import (
  18. "math/big"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/consensus"
  23. "github.com/ethereum/go-ethereum/consensus/clique"
  24. "github.com/ethereum/go-ethereum/consensus/ethash"
  25. "github.com/ethereum/go-ethereum/core"
  26. "github.com/ethereum/go-ethereum/core/rawdb"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/core/vm"
  29. "github.com/ethereum/go-ethereum/crypto"
  30. "github.com/ethereum/go-ethereum/ethdb"
  31. "github.com/ethereum/go-ethereum/event"
  32. "github.com/ethereum/go-ethereum/params"
  33. )
  34. var (
  35. // Test chain configurations
  36. testTxPoolConfig core.TxPoolConfig
  37. ethashChainConfig *params.ChainConfig
  38. cliqueChainConfig *params.ChainConfig
  39. // Test accounts
  40. testBankKey, _ = crypto.GenerateKey()
  41. testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey)
  42. testBankFunds = big.NewInt(1000000000000000000)
  43. testUserKey, _ = crypto.GenerateKey()
  44. testUserAddress = crypto.PubkeyToAddress(testUserKey.PublicKey)
  45. // Test transactions
  46. pendingTxs []*types.Transaction
  47. newTxs []*types.Transaction
  48. )
  49. func init() {
  50. testTxPoolConfig = core.DefaultTxPoolConfig
  51. testTxPoolConfig.Journal = ""
  52. ethashChainConfig = params.TestChainConfig
  53. cliqueChainConfig = params.TestChainConfig
  54. cliqueChainConfig.Clique = &params.CliqueConfig{
  55. Period: 10,
  56. Epoch: 30000,
  57. }
  58. tx1, _ := types.SignTx(types.NewTransaction(0, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
  59. pendingTxs = append(pendingTxs, tx1)
  60. tx2, _ := types.SignTx(types.NewTransaction(1, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
  61. newTxs = append(newTxs, tx2)
  62. }
  63. // testWorkerBackend implements worker.Backend interfaces and wraps all information needed during the testing.
  64. type testWorkerBackend struct {
  65. db ethdb.Database
  66. txPool *core.TxPool
  67. chain *core.BlockChain
  68. testTxFeed event.Feed
  69. uncleBlock *types.Block
  70. }
  71. func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, n int) *testWorkerBackend {
  72. var (
  73. db = rawdb.NewMemoryDatabase()
  74. gspec = core.Genesis{
  75. Config: chainConfig,
  76. Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
  77. }
  78. )
  79. switch engine.(type) {
  80. case *clique.Clique:
  81. gspec.ExtraData = make([]byte, 32+common.AddressLength+65)
  82. copy(gspec.ExtraData[32:], testBankAddress[:])
  83. case *ethash.Ethash:
  84. default:
  85. t.Fatalf("unexpected consensus engine type: %T", engine)
  86. }
  87. genesis := gspec.MustCommit(db)
  88. chain, _ := core.NewBlockChain(db, nil, gspec.Config, engine, vm.Config{}, nil)
  89. txpool := core.NewTxPool(testTxPoolConfig, chainConfig, chain)
  90. // Generate a small n-block chain and an uncle block for it
  91. if n > 0 {
  92. blocks, _ := core.GenerateChain(chainConfig, genesis, engine, db, n, func(i int, gen *core.BlockGen) {
  93. gen.SetCoinbase(testBankAddress)
  94. })
  95. if _, err := chain.InsertChain(blocks); err != nil {
  96. t.Fatalf("failed to insert origin chain: %v", err)
  97. }
  98. }
  99. parent := genesis
  100. if n > 0 {
  101. parent = chain.GetBlockByHash(chain.CurrentBlock().ParentHash())
  102. }
  103. blocks, _ := core.GenerateChain(chainConfig, parent, engine, db, 1, func(i int, gen *core.BlockGen) {
  104. gen.SetCoinbase(testUserAddress)
  105. })
  106. return &testWorkerBackend{
  107. db: db,
  108. chain: chain,
  109. txPool: txpool,
  110. uncleBlock: blocks[0],
  111. }
  112. }
  113. func (b *testWorkerBackend) BlockChain() *core.BlockChain { return b.chain }
  114. func (b *testWorkerBackend) TxPool() *core.TxPool { return b.txPool }
  115. func (b *testWorkerBackend) PostChainEvents(events []interface{}) {
  116. b.chain.PostChainEvents(events, nil)
  117. }
  118. func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, blocks int) (*worker, *testWorkerBackend) {
  119. backend := newTestWorkerBackend(t, chainConfig, engine, blocks)
  120. backend.txPool.AddLocals(pendingTxs)
  121. w := newWorker(chainConfig, engine, backend, new(event.TypeMux), time.Second, params.GenesisGasLimit, params.GenesisGasLimit, nil)
  122. w.setEtherbase(testBankAddress)
  123. return w, backend
  124. }
  125. func TestPendingStateAndBlockEthash(t *testing.T) {
  126. testPendingStateAndBlock(t, ethashChainConfig, ethash.NewFaker())
  127. }
  128. func TestPendingStateAndBlockClique(t *testing.T) {
  129. testPendingStateAndBlock(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase()))
  130. }
  131. func testPendingStateAndBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
  132. defer engine.Close()
  133. w, b := newTestWorker(t, chainConfig, engine, 0)
  134. defer w.close()
  135. // Ensure snapshot has been updated.
  136. time.Sleep(100 * time.Millisecond)
  137. block, state := w.pending()
  138. if block.NumberU64() != 1 {
  139. t.Errorf("block number mismatch: have %d, want %d", block.NumberU64(), 1)
  140. }
  141. if balance := state.GetBalance(testUserAddress); balance.Cmp(big.NewInt(1000)) != 0 {
  142. t.Errorf("account balance mismatch: have %d, want %d", balance, 1000)
  143. }
  144. b.txPool.AddLocals(newTxs)
  145. // Ensure the new tx events has been processed
  146. time.Sleep(100 * time.Millisecond)
  147. block, state = w.pending()
  148. if balance := state.GetBalance(testUserAddress); balance.Cmp(big.NewInt(2000)) != 0 {
  149. t.Errorf("account balance mismatch: have %d, want %d", balance, 2000)
  150. }
  151. }
  152. func TestEmptyWorkEthash(t *testing.T) {
  153. testEmptyWork(t, ethashChainConfig, ethash.NewFaker())
  154. }
  155. func TestEmptyWorkClique(t *testing.T) {
  156. testEmptyWork(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase()))
  157. }
  158. func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
  159. defer engine.Close()
  160. w, _ := newTestWorker(t, chainConfig, engine, 0)
  161. defer w.close()
  162. var (
  163. taskCh = make(chan struct{}, 2)
  164. taskIndex int
  165. )
  166. checkEqual := func(t *testing.T, task *task, index int) {
  167. receiptLen, balance := 0, big.NewInt(0)
  168. if index == 1 {
  169. receiptLen, balance = 1, big.NewInt(1000)
  170. }
  171. if len(task.receipts) != receiptLen {
  172. t.Errorf("receipt number mismatch: have %d, want %d", len(task.receipts), receiptLen)
  173. }
  174. if task.state.GetBalance(testUserAddress).Cmp(balance) != 0 {
  175. t.Errorf("account balance mismatch: have %d, want %d", task.state.GetBalance(testUserAddress), balance)
  176. }
  177. }
  178. w.newTaskHook = func(task *task) {
  179. if task.block.NumberU64() == 1 {
  180. checkEqual(t, task, taskIndex)
  181. taskIndex += 1
  182. taskCh <- struct{}{}
  183. }
  184. }
  185. w.fullTaskHook = func() {
  186. time.Sleep(100 * time.Millisecond)
  187. }
  188. // Ensure worker has finished initialization
  189. for {
  190. b := w.pendingBlock()
  191. if b != nil && b.NumberU64() == 1 {
  192. break
  193. }
  194. }
  195. w.start()
  196. for i := 0; i < 2; i += 1 {
  197. select {
  198. case <-taskCh:
  199. case <-time.NewTimer(2 * time.Second).C:
  200. t.Error("new task timeout")
  201. }
  202. }
  203. }
  204. func TestStreamUncleBlock(t *testing.T) {
  205. ethash := ethash.NewFaker()
  206. defer ethash.Close()
  207. w, b := newTestWorker(t, ethashChainConfig, ethash, 1)
  208. defer w.close()
  209. var taskCh = make(chan struct{})
  210. taskIndex := 0
  211. w.newTaskHook = func(task *task) {
  212. if task.block.NumberU64() == 2 {
  213. if taskIndex == 2 {
  214. have := task.block.Header().UncleHash
  215. want := types.CalcUncleHash([]*types.Header{b.uncleBlock.Header()})
  216. if have != want {
  217. t.Errorf("uncle hash mismatch: have %s, want %s", have.Hex(), want.Hex())
  218. }
  219. }
  220. taskCh <- struct{}{}
  221. taskIndex += 1
  222. }
  223. }
  224. w.skipSealHook = func(task *task) bool {
  225. return true
  226. }
  227. w.fullTaskHook = func() {
  228. time.Sleep(100 * time.Millisecond)
  229. }
  230. // Ensure worker has finished initialization
  231. for {
  232. b := w.pendingBlock()
  233. if b != nil && b.NumberU64() == 2 {
  234. break
  235. }
  236. }
  237. w.start()
  238. // Ignore the first two works
  239. for i := 0; i < 2; i += 1 {
  240. select {
  241. case <-taskCh:
  242. case <-time.NewTimer(time.Second).C:
  243. t.Error("new task timeout")
  244. }
  245. }
  246. b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.uncleBlock}})
  247. select {
  248. case <-taskCh:
  249. case <-time.NewTimer(time.Second).C:
  250. t.Error("new task timeout")
  251. }
  252. }
  253. func TestRegenerateMiningBlockEthash(t *testing.T) {
  254. testRegenerateMiningBlock(t, ethashChainConfig, ethash.NewFaker())
  255. }
  256. func TestRegenerateMiningBlockClique(t *testing.T) {
  257. testRegenerateMiningBlock(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase()))
  258. }
  259. func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
  260. defer engine.Close()
  261. w, b := newTestWorker(t, chainConfig, engine, 0)
  262. defer w.close()
  263. var taskCh = make(chan struct{})
  264. taskIndex := 0
  265. w.newTaskHook = func(task *task) {
  266. if task.block.NumberU64() == 1 {
  267. if taskIndex == 2 {
  268. receiptLen, balance := 2, big.NewInt(2000)
  269. if len(task.receipts) != receiptLen {
  270. t.Errorf("receipt number mismatch: have %d, want %d", len(task.receipts), receiptLen)
  271. }
  272. if task.state.GetBalance(testUserAddress).Cmp(balance) != 0 {
  273. t.Errorf("account balance mismatch: have %d, want %d", task.state.GetBalance(testUserAddress), balance)
  274. }
  275. }
  276. taskCh <- struct{}{}
  277. taskIndex += 1
  278. }
  279. }
  280. w.skipSealHook = func(task *task) bool {
  281. return true
  282. }
  283. w.fullTaskHook = func() {
  284. time.Sleep(100 * time.Millisecond)
  285. }
  286. // Ensure worker has finished initialization
  287. for {
  288. b := w.pendingBlock()
  289. if b != nil && b.NumberU64() == 1 {
  290. break
  291. }
  292. }
  293. w.start()
  294. // Ignore the first two works
  295. for i := 0; i < 2; i += 1 {
  296. select {
  297. case <-taskCh:
  298. case <-time.NewTimer(time.Second).C:
  299. t.Error("new task timeout")
  300. }
  301. }
  302. b.txPool.AddLocals(newTxs)
  303. time.Sleep(time.Second)
  304. select {
  305. case <-taskCh:
  306. case <-time.NewTimer(time.Second).C:
  307. t.Error("new task timeout")
  308. }
  309. }
  310. func TestAdjustIntervalEthash(t *testing.T) {
  311. testAdjustInterval(t, ethashChainConfig, ethash.NewFaker())
  312. }
  313. func TestAdjustIntervalClique(t *testing.T) {
  314. testAdjustInterval(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase()))
  315. }
  316. func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
  317. defer engine.Close()
  318. w, _ := newTestWorker(t, chainConfig, engine, 0)
  319. defer w.close()
  320. w.skipSealHook = func(task *task) bool {
  321. return true
  322. }
  323. w.fullTaskHook = func() {
  324. time.Sleep(100 * time.Millisecond)
  325. }
  326. var (
  327. progress = make(chan struct{}, 10)
  328. result = make([]float64, 0, 10)
  329. index = 0
  330. start = false
  331. )
  332. w.resubmitHook = func(minInterval time.Duration, recommitInterval time.Duration) {
  333. // Short circuit if interval checking hasn't started.
  334. if !start {
  335. return
  336. }
  337. var wantMinInterval, wantRecommitInterval time.Duration
  338. switch index {
  339. case 0:
  340. wantMinInterval, wantRecommitInterval = 3*time.Second, 3*time.Second
  341. case 1:
  342. origin := float64(3 * time.Second.Nanoseconds())
  343. estimate := origin*(1-intervalAdjustRatio) + intervalAdjustRatio*(origin/0.8+intervalAdjustBias)
  344. wantMinInterval, wantRecommitInterval = 3*time.Second, time.Duration(estimate)*time.Nanosecond
  345. case 2:
  346. estimate := result[index-1]
  347. min := float64(3 * time.Second.Nanoseconds())
  348. estimate = estimate*(1-intervalAdjustRatio) + intervalAdjustRatio*(min-intervalAdjustBias)
  349. wantMinInterval, wantRecommitInterval = 3*time.Second, time.Duration(estimate)*time.Nanosecond
  350. case 3:
  351. wantMinInterval, wantRecommitInterval = time.Second, time.Second
  352. }
  353. // Check interval
  354. if minInterval != wantMinInterval {
  355. t.Errorf("resubmit min interval mismatch: have %v, want %v ", minInterval, wantMinInterval)
  356. }
  357. if recommitInterval != wantRecommitInterval {
  358. t.Errorf("resubmit interval mismatch: have %v, want %v", recommitInterval, wantRecommitInterval)
  359. }
  360. result = append(result, float64(recommitInterval.Nanoseconds()))
  361. index += 1
  362. progress <- struct{}{}
  363. }
  364. // Ensure worker has finished initialization
  365. for {
  366. b := w.pendingBlock()
  367. if b != nil && b.NumberU64() == 1 {
  368. break
  369. }
  370. }
  371. w.start()
  372. time.Sleep(time.Second)
  373. start = true
  374. w.setRecommitInterval(3 * time.Second)
  375. select {
  376. case <-progress:
  377. case <-time.NewTimer(time.Second).C:
  378. t.Error("interval reset timeout")
  379. }
  380. w.resubmitAdjustCh <- &intervalAdjust{inc: true, ratio: 0.8}
  381. select {
  382. case <-progress:
  383. case <-time.NewTimer(time.Second).C:
  384. t.Error("interval reset timeout")
  385. }
  386. w.resubmitAdjustCh <- &intervalAdjust{inc: false}
  387. select {
  388. case <-progress:
  389. case <-time.NewTimer(time.Second).C:
  390. t.Error("interval reset timeout")
  391. }
  392. w.setRecommitInterval(500 * time.Millisecond)
  393. select {
  394. case <-progress:
  395. case <-time.NewTimer(time.Second).C:
  396. t.Error("interval reset timeout")
  397. }
  398. }