sealer_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2018 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. "encoding/json"
  19. "io/ioutil"
  20. "math/big"
  21. "net/http"
  22. "net/http/httptest"
  23. "testing"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/internal/testlog"
  28. "github.com/ethereum/go-ethereum/log"
  29. )
  30. // Tests whether remote HTTP servers are correctly notified of new work.
  31. func TestRemoteNotify(t *testing.T) {
  32. // Start a simple web server to capture notifications.
  33. sink := make(chan [3]string)
  34. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  35. blob, err := ioutil.ReadAll(req.Body)
  36. if err != nil {
  37. t.Errorf("failed to read miner notification: %v", err)
  38. }
  39. var work [3]string
  40. if err := json.Unmarshal(blob, &work); err != nil {
  41. t.Errorf("failed to unmarshal miner notification: %v", err)
  42. }
  43. sink <- work
  44. }))
  45. defer server.Close()
  46. // Create the custom ethash engine.
  47. ethash := NewTester([]string{server.URL}, false)
  48. defer ethash.Close()
  49. // Stream a work task and ensure the notification bubbles out.
  50. header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
  51. block := types.NewBlockWithHeader(header)
  52. ethash.Seal(nil, block, nil, nil)
  53. select {
  54. case work := <-sink:
  55. if want := ethash.SealHash(header).Hex(); work[0] != want {
  56. t.Errorf("work packet hash mismatch: have %s, want %s", work[0], want)
  57. }
  58. if want := common.BytesToHash(SeedHash(header.Number.Uint64())).Hex(); work[1] != want {
  59. t.Errorf("work packet seed mismatch: have %s, want %s", work[1], want)
  60. }
  61. target := new(big.Int).Div(new(big.Int).Lsh(big.NewInt(1), 256), header.Difficulty)
  62. if want := common.BytesToHash(target.Bytes()).Hex(); work[2] != want {
  63. t.Errorf("work packet target mismatch: have %s, want %s", work[2], want)
  64. }
  65. case <-time.After(3 * time.Second):
  66. t.Fatalf("notification timed out")
  67. }
  68. }
  69. // Tests that pushing work packages fast to the miner doesn't cause any data race
  70. // issues in the notifications.
  71. func TestRemoteMultiNotify(t *testing.T) {
  72. // Start a simple web server to capture notifications.
  73. sink := make(chan [3]string, 64)
  74. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  75. blob, err := ioutil.ReadAll(req.Body)
  76. if err != nil {
  77. t.Errorf("failed to read miner notification: %v", err)
  78. }
  79. var work [3]string
  80. if err := json.Unmarshal(blob, &work); err != nil {
  81. t.Errorf("failed to unmarshal miner notification: %v", err)
  82. }
  83. sink <- work
  84. }))
  85. defer server.Close()
  86. // Create the custom ethash engine.
  87. ethash := NewTester([]string{server.URL}, false)
  88. ethash.config.Log = testlog.Logger(t, log.LvlWarn)
  89. defer ethash.Close()
  90. // Provide a results reader.
  91. // Otherwise the unread results will be logged asynchronously
  92. // and this can happen after the test is finished, causing a panic.
  93. results := make(chan *types.Block, cap(sink))
  94. // Stream a lot of work task and ensure all the notifications bubble out.
  95. for i := 0; i < cap(sink); i++ {
  96. header := &types.Header{Number: big.NewInt(int64(i)), Difficulty: big.NewInt(100)}
  97. block := types.NewBlockWithHeader(header)
  98. ethash.Seal(nil, block, results, nil)
  99. }
  100. for i := 0; i < cap(sink); i++ {
  101. select {
  102. case <-sink:
  103. <-results
  104. case <-time.After(10 * time.Second):
  105. t.Fatalf("notification %d timed out", i)
  106. }
  107. }
  108. }
  109. // Tests whether stale solutions are correctly processed.
  110. func TestStaleSubmission(t *testing.T) {
  111. ethash := NewTester(nil, true)
  112. defer ethash.Close()
  113. api := &API{ethash}
  114. fakeNonce, fakeDigest := types.BlockNonce{0x01, 0x02, 0x03}, common.HexToHash("deadbeef")
  115. testcases := []struct {
  116. headers []*types.Header
  117. submitIndex int
  118. submitRes bool
  119. }{
  120. // Case1: submit solution for the latest mining package
  121. {
  122. []*types.Header{
  123. {ParentHash: common.BytesToHash([]byte{0xa}), Number: big.NewInt(1), Difficulty: big.NewInt(100000000)},
  124. },
  125. 0,
  126. true,
  127. },
  128. // Case2: submit solution for the previous package but have same parent.
  129. {
  130. []*types.Header{
  131. {ParentHash: common.BytesToHash([]byte{0xb}), Number: big.NewInt(2), Difficulty: big.NewInt(100000000)},
  132. {ParentHash: common.BytesToHash([]byte{0xb}), Number: big.NewInt(2), Difficulty: big.NewInt(100000001)},
  133. },
  134. 0,
  135. true,
  136. },
  137. // Case3: submit stale but acceptable solution
  138. {
  139. []*types.Header{
  140. {ParentHash: common.BytesToHash([]byte{0xc}), Number: big.NewInt(3), Difficulty: big.NewInt(100000000)},
  141. {ParentHash: common.BytesToHash([]byte{0xd}), Number: big.NewInt(9), Difficulty: big.NewInt(100000000)},
  142. },
  143. 0,
  144. true,
  145. },
  146. // Case4: submit very old solution
  147. {
  148. []*types.Header{
  149. {ParentHash: common.BytesToHash([]byte{0xe}), Number: big.NewInt(10), Difficulty: big.NewInt(100000000)},
  150. {ParentHash: common.BytesToHash([]byte{0xf}), Number: big.NewInt(17), Difficulty: big.NewInt(100000000)},
  151. },
  152. 0,
  153. false,
  154. },
  155. }
  156. results := make(chan *types.Block, 16)
  157. for id, c := range testcases {
  158. for _, h := range c.headers {
  159. ethash.Seal(nil, types.NewBlockWithHeader(h), results, nil)
  160. }
  161. if res := api.SubmitWork(fakeNonce, ethash.SealHash(c.headers[c.submitIndex]), fakeDigest); res != c.submitRes {
  162. t.Errorf("case %d submit result mismatch, want %t, get %t", id+1, c.submitRes, res)
  163. }
  164. if !c.submitRes {
  165. continue
  166. }
  167. select {
  168. case res := <-results:
  169. if res.Header().Nonce != fakeNonce {
  170. t.Errorf("case %d block nonce mismatch, want %x, get %x", id+1, fakeNonce, res.Header().Nonce)
  171. }
  172. if res.Header().MixDigest != fakeDigest {
  173. t.Errorf("case %d block digest mismatch, want %x, get %x", id+1, fakeDigest, res.Header().MixDigest)
  174. }
  175. if res.Header().Difficulty.Uint64() != c.headers[c.submitIndex].Difficulty.Uint64() {
  176. t.Errorf("case %d block difficulty mismatch, want %d, get %d", id+1, c.headers[c.submitIndex].Difficulty, res.Header().Difficulty)
  177. }
  178. if res.Header().Number.Uint64() != c.headers[c.submitIndex].Number.Uint64() {
  179. t.Errorf("case %d block number mismatch, want %d, get %d", id+1, c.headers[c.submitIndex].Number.Uint64(), res.Header().Number.Uint64())
  180. }
  181. if res.Header().ParentHash != c.headers[c.submitIndex].ParentHash {
  182. t.Errorf("case %d block parent hash mismatch, want %s, get %s", id+1, c.headers[c.submitIndex].ParentHash.Hex(), res.Header().ParentHash.Hex())
  183. }
  184. case <-time.NewTimer(time.Second).C:
  185. t.Errorf("case %d fetch ethash result timeout", id+1)
  186. }
  187. }
  188. }