aboutsummaryrefslogtreecommitdiffstats
path: root/consensus/ethash/sealer_test.go
blob: 6d8a770495459dc541a39d049f67aa535fd0394c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package ethash

import (
    "encoding/json"
    "io/ioutil"
    "math/big"
    "net"
    "net/http"
    "testing"
    "time"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
)

// Tests whether remote HTTP servers are correctly notified of new work.
func TestRemoteNotify(t *testing.T) {
    // Start a simple webserver to capture notifications
    sink := make(chan [3]string)

    server := &http.Server{
        Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
            blob, err := ioutil.ReadAll(req.Body)
            if err != nil {
                t.Fatalf("failed to read miner notification: %v", err)
            }
            var work [3]string
            if err := json.Unmarshal(blob, &work); err != nil {
                t.Fatalf("failed to unmarshal miner notification: %v", err)
            }
            sink <- work
        }),
    }
    // Open a custom listener to extract its local address
    listener, err := net.Listen("tcp", "localhost:0")
    if err != nil {
        t.Fatalf("failed to open notification server: %v", err)
    }
    defer listener.Close()

    go server.Serve(listener)

    // Create the custom ethash engine
    ethash := NewTester([]string{"http://" + listener.Addr().String()})
    defer ethash.Close()

    // Stream a work task and ensure the notification bubbles out
    header := &types.Header{Number: big.NewInt(1), Difficulty: big.NewInt(100)}
    block := types.NewBlockWithHeader(header)

    ethash.Seal(nil, block, nil)
    select {
    case work := <-sink:
        if want := header.HashNoNonce().Hex(); work[0] != want {
            t.Errorf("work packet hash mismatch: have %s, want %s", work[0], want)
        }
        if want := common.BytesToHash(SeedHash(header.Number.Uint64())).Hex(); work[1] != want {
            t.Errorf("work packet seed mismatch: have %s, want %s", work[1], want)
        }
        target := new(big.Int).Div(new(big.Int).Lsh(big.NewInt(1), 256), header.Difficulty)
        if want := common.BytesToHash(target.Bytes()).Hex(); work[2] != want {
            t.Errorf("work packet target mismatch: have %s, want %s", work[2], want)
        }
    case <-time.After(time.Second):
        t.Fatalf("notification timed out")
    }
}

// Tests that pushing work packages fast to the miner doesn't cause any daa race
// issues in the notifications.
func TestRemoteMultiNotify(t *testing.T) {
    // Start a simple webserver to capture notifications
    sink := make(chan [3]string, 1024)

    server := &http.Server{
        Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
            blob, err := ioutil.ReadAll(req.Body)
            if err != nil {
                t.Fatalf("failed to read miner notification: %v", err)
            }
            var work [3]string
            if err := json.Unmarshal(blob, &work); err != nil {
                t.Fatalf("failed to unmarshal miner notification: %v", err)
            }
            sink <- work
        }),
    }
    // Open a custom listener to extract its local address
    listener, err := net.Listen("tcp", "localhost:0")
    if err != nil {
        t.Fatalf("failed to open notification server: %v", err)
    }
    defer listener.Close()

    go server.Serve(listener)

    // Create the custom ethash engine
    ethash := NewTester([]string{"http://" + listener.Addr().String()})
    defer ethash.Close()

    // Stream a lot of work task and ensure all the notifications bubble out
    for i := 0; i < cap(sink); i++ {
        header := &types.Header{Number: big.NewInt(int64(i)), Difficulty: big.NewInt(100)}
        block := types.NewBlockWithHeader(header)

        ethash.Seal(nil, block, nil)
    }
    for i := 0; i < cap(sink); i++ {
        select {
        case <-sink:
        case <-time.After(250 * time.Millisecond):
            t.Fatalf("notification %d timed out", i)
        }
    }
}