sealer_test.go 7.5 KB

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