ethash_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, false); err != nil {
  44. t.Fatalf("unexpected verification error: %v", err)
  45. }
  46. case <-time.NewTimer(4 * 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. config := Config{
  59. CachesInMem: 3,
  60. CachesOnDisk: 10,
  61. CacheDir: tmpdir,
  62. PowMode: ModeTest,
  63. }
  64. e := New(config, nil, false)
  65. defer e.Close()
  66. workers := 8
  67. epochs := 100
  68. var wg sync.WaitGroup
  69. wg.Add(workers)
  70. for i := 0; i < workers; i++ {
  71. go verifyTest(&wg, e, i, epochs)
  72. }
  73. wg.Wait()
  74. }
  75. func verifyTest(wg *sync.WaitGroup, e *Ethash, workerIndex, epochs int) {
  76. defer wg.Done()
  77. const wiggle = 4 * epochLength
  78. r := rand.New(rand.NewSource(int64(workerIndex)))
  79. for epoch := 0; epoch < epochs; epoch++ {
  80. block := int64(epoch)*epochLength - wiggle/2 + r.Int63n(wiggle)
  81. if block < 0 {
  82. block = 0
  83. }
  84. header := &types.Header{Number: big.NewInt(block), Difficulty: big.NewInt(100)}
  85. e.verifySeal(nil, header, false)
  86. }
  87. }
  88. func TestRemoteSealer(t *testing.T) {
  89. ethash := NewTester(nil, false)
  90. defer ethash.Close()
  91. api := &API{ethash}
  92. if _, err := api.GetWork(); err != errNoMiningWork {
  93. t.Error("expect to return an error indicate there is no mining work")
  94. }
  95. header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
  96. block := types.NewBlockWithHeader(header)
  97. sealhash := ethash.SealHash(header)
  98. // Push new work.
  99. results := make(chan *types.Block)
  100. ethash.Seal(nil, block, results, nil)
  101. var (
  102. work [4]string
  103. err error
  104. )
  105. if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
  106. t.Error("expect to return a mining work has same hash")
  107. }
  108. if res := api.SubmitWork(types.BlockNonce{}, sealhash, common.Hash{}); res {
  109. t.Error("expect to return false when submit a fake solution")
  110. }
  111. // Push new block with same block number to replace the original one.
  112. header = &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(1000)}
  113. block = types.NewBlockWithHeader(header)
  114. sealhash = ethash.SealHash(header)
  115. ethash.Seal(nil, block, results, nil)
  116. if work, err = api.GetWork(); err != nil || work[0] != sealhash.Hex() {
  117. t.Error("expect to return the latest pushed work")
  118. }
  119. }
  120. func TestHashrate(t *testing.T) {
  121. var (
  122. hashrate = []hexutil.Uint64{100, 200, 300}
  123. expect uint64
  124. ids = []common.Hash{common.HexToHash("a"), common.HexToHash("b"), common.HexToHash("c")}
  125. )
  126. ethash := NewTester(nil, false)
  127. defer ethash.Close()
  128. if tot := ethash.Hashrate(); tot != 0 {
  129. t.Error("expect the result should be zero")
  130. }
  131. api := &API{ethash}
  132. for i := 0; i < len(hashrate); i += 1 {
  133. if res := api.SubmitHashrate(hashrate[i], ids[i]); !res {
  134. t.Error("remote miner submit hashrate failed")
  135. }
  136. expect += uint64(hashrate[i])
  137. }
  138. if tot := ethash.Hashrate(); tot != float64(expect) {
  139. t.Error("expect total hashrate should be same")
  140. }
  141. }
  142. func TestClosedRemoteSealer(t *testing.T) {
  143. ethash := NewTester(nil, false)
  144. time.Sleep(1 * time.Second) // ensure exit channel is listening
  145. ethash.Close()
  146. api := &API{ethash}
  147. if _, err := api.GetWork(); err != errEthashStopped {
  148. t.Error("expect to return an error to indicate ethash is stopped")
  149. }
  150. if res := api.SubmitHashrate(hexutil.Uint64(100), common.HexToHash("a")); res {
  151. t.Error("expect to return false when submit hashrate to a stopped ethash")
  152. }
  153. }