ethash_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2017 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 ethash
  17. import (
  18. "io/ioutil"
  19. "math/big"
  20. "math/rand"
  21. "os"
  22. "sync"
  23. "testing"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/common/hexutil"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. )
  29. // Tests that ethash works correctly in test mode.
  30. func TestTestMode(t *testing.T) {
  31. head := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
  32. ethash := NewTester()
  33. defer ethash.Close()
  34. block, err := ethash.Seal(nil, types.NewBlockWithHeader(head), nil)
  35. if err != nil {
  36. t.Fatalf("failed to seal block: %v", err)
  37. }
  38. head.Nonce = types.EncodeNonce(block.Nonce())
  39. head.MixDigest = block.MixDigest()
  40. if err := ethash.VerifySeal(nil, head); err != nil {
  41. t.Fatalf("unexpected verification error: %v", err)
  42. }
  43. }
  44. // This test checks that cache lru logic doesn't crash under load.
  45. // It reproduces https://github.com/ethereum/go-ethereum/issues/14943
  46. func TestCacheFileEvict(t *testing.T) {
  47. tmpdir, err := ioutil.TempDir("", "ethash-test")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. defer os.RemoveAll(tmpdir)
  52. e := New(Config{CachesInMem: 3, CachesOnDisk: 10, CacheDir: tmpdir, PowMode: ModeTest})
  53. defer e.Close()
  54. workers := 8
  55. epochs := 100
  56. var wg sync.WaitGroup
  57. wg.Add(workers)
  58. for i := 0; i < workers; i++ {
  59. go verifyTest(&wg, e, i, epochs)
  60. }
  61. wg.Wait()
  62. }
  63. func verifyTest(wg *sync.WaitGroup, e *Ethash, workerIndex, epochs int) {
  64. defer wg.Done()
  65. const wiggle = 4 * epochLength
  66. r := rand.New(rand.NewSource(int64(workerIndex)))
  67. for epoch := 0; epoch < epochs; epoch++ {
  68. block := int64(epoch)*epochLength - wiggle/2 + r.Int63n(wiggle)
  69. if block < 0 {
  70. block = 0
  71. }
  72. head := &types.Header{Number: big.NewInt(block), Difficulty: big.NewInt(100)}
  73. e.VerifySeal(nil, head)
  74. }
  75. }
  76. func TestRemoteSealer(t *testing.T) {
  77. ethash := NewTester()
  78. defer ethash.Close()
  79. api := &API{ethash}
  80. if _, err := api.GetWork(); err != errNoMiningWork {
  81. t.Error("expect to return an error indicate there is no mining work")
  82. }
  83. head := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
  84. block := types.NewBlockWithHeader(head)
  85. // Push new work.
  86. ethash.Seal(nil, block, nil)
  87. var (
  88. work [3]string
  89. err error
  90. )
  91. if work, err = api.GetWork(); err != nil || work[0] != block.HashNoNonce().Hex() {
  92. t.Error("expect to return a mining work has same hash")
  93. }
  94. if res := api.SubmitWork(types.BlockNonce{}, block.HashNoNonce(), common.Hash{}); res {
  95. t.Error("expect to return false when submit a fake solution")
  96. }
  97. // Push new block with same block number to replace the original one.
  98. head = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)}
  99. block = types.NewBlockWithHeader(head)
  100. ethash.Seal(nil, block, nil)
  101. if work, err = api.GetWork(); err != nil || work[0] != block.HashNoNonce().Hex() {
  102. t.Error("expect to return the latest pushed work")
  103. }
  104. // Push block with higher block number.
  105. newHead := &types.Header{Number: big.NewInt(2), Difficulty: big.NewInt(100)}
  106. newBlock := types.NewBlockWithHeader(newHead)
  107. ethash.Seal(nil, newBlock, nil)
  108. if res := api.SubmitWork(types.BlockNonce{}, block.HashNoNonce(), common.Hash{}); res {
  109. t.Error("expect to return false when submit a stale solution")
  110. }
  111. }
  112. func TestHashRate(t *testing.T) {
  113. var (
  114. ethash = NewTester()
  115. api = &API{ethash}
  116. hashrate = []hexutil.Uint64{100, 200, 300}
  117. expect uint64
  118. ids = []common.Hash{common.HexToHash("a"), common.HexToHash("b"), common.HexToHash("c")}
  119. )
  120. defer ethash.Close()
  121. if tot := ethash.Hashrate(); tot != 0 {
  122. t.Error("expect the result should be zero")
  123. }
  124. for i := 0; i < len(hashrate); i += 1 {
  125. if res := api.SubmitHashRate(hashrate[i], ids[i]); !res {
  126. t.Error("remote miner submit hashrate failed")
  127. }
  128. expect += uint64(hashrate[i])
  129. }
  130. if tot := ethash.Hashrate(); tot != float64(expect) {
  131. t.Error("expect total hashrate should be same")
  132. }
  133. }
  134. func TestClosedRemoteSealer(t *testing.T) {
  135. ethash := NewTester()
  136. // Make sure exit channel has been listened
  137. time.Sleep(1 * time.Second)
  138. ethash.Close()
  139. api := &API{ethash}
  140. if _, err := api.GetWork(); err != errEthashStopped {
  141. t.Error("expect to return an error to indicate ethash is stopped")
  142. }
  143. if res := api.SubmitHashRate(hexutil.Uint64(100), common.HexToHash("a")); res {
  144. t.Error("expect to return false when submit hashrate to a stopped ethash")
  145. }
  146. }