sealer_test.go 6.7 KB

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