ethash_test.go 5.0 KB

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