ethash_test.go 5.2 KB

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