ethash_test.go 5.1 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. header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
  32. ethash := NewTester(nil)
  33. defer ethash.Close()
  34. block, err := ethash.Seal(nil, types.NewBlockWithHeader(header), nil)
  35. if err != nil {
  36. t.Fatalf("failed to seal block: %v", err)
  37. }
  38. header.Nonce = types.EncodeNonce(block.Nonce())
  39. header.MixDigest = block.MixDigest()
  40. if err := ethash.VerifySeal(nil, header); 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}, nil)
  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. header := &types.Header{Number: big.NewInt(block), Difficulty: big.NewInt(100)}
  73. e.VerifySeal(nil, header)
  74. }
  75. }
  76. func TestRemoteSealer(t *testing.T) {
  77. ethash := NewTester(nil)
  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. header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
  84. block := types.NewBlockWithHeader(header)
  85. sealhash := ethash.SealHash(header)
  86. // Push new work.
  87. ethash.Seal(nil, block, nil)
  88. var (
  89. work [3]string
  90. err error
  91. )
  92. if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
  93. t.Error("expect to return a mining work has same hash")
  94. }
  95. if res := api.SubmitWork(types.BlockNonce{}, sealhash, common.Hash{}); res {
  96. t.Error("expect to return false when submit a fake solution")
  97. }
  98. // Push new block with same block number to replace the original one.
  99. header = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)}
  100. block = types.NewBlockWithHeader(header)
  101. sealhash = ethash.SealHash(header)
  102. ethash.Seal(nil, block, nil)
  103. if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
  104. t.Error("expect to return the latest pushed work")
  105. }
  106. // Push block with higher block number.
  107. newHead := &types.Header{Number: big.NewInt(2), Difficulty: big.NewInt(100)}
  108. newBlock := types.NewBlockWithHeader(newHead)
  109. newSealhash := ethash.SealHash(newHead)
  110. ethash.Seal(nil, newBlock, nil)
  111. if res := api.SubmitWork(types.BlockNonce{}, newSealhash, common.Hash{}); res {
  112. t.Error("expect to return false when submit a stale solution")
  113. }
  114. }
  115. func TestHashRate(t *testing.T) {
  116. var (
  117. hashrate = []hexutil.Uint64{100, 200, 300}
  118. expect uint64
  119. ids = []common.Hash{common.HexToHash("a"), common.HexToHash("b"), common.HexToHash("c")}
  120. )
  121. ethash := NewTester(nil)
  122. defer ethash.Close()
  123. if tot := ethash.Hashrate(); tot != 0 {
  124. t.Error("expect the result should be zero")
  125. }
  126. api := &API{ethash}
  127. for i := 0; i < len(hashrate); i += 1 {
  128. if res := api.SubmitHashRate(hashrate[i], ids[i]); !res {
  129. t.Error("remote miner submit hashrate failed")
  130. }
  131. expect += uint64(hashrate[i])
  132. }
  133. if tot := ethash.Hashrate(); tot != float64(expect) {
  134. t.Error("expect total hashrate should be same")
  135. }
  136. }
  137. func TestClosedRemoteSealer(t *testing.T) {
  138. ethash := NewTester(nil)
  139. time.Sleep(1 * time.Second) // ensure exit channel is listening
  140. ethash.Close()
  141. api := &API{ethash}
  142. if _, err := api.GetWork(); err != errEthashStopped {
  143. t.Error("expect to return an error to indicate ethash is stopped")
  144. }
  145. if res := api.SubmitHashRate(hexutil.Uint64(100), common.HexToHash("a")); res {
  146. t.Error("expect to return false when submit hashrate to a stopped ethash")
  147. }
  148. }