sealer_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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()})
  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)
  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 daa 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()})
  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)
  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. }