sealer_test.go 6.4 KB

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