-
Notifications
You must be signed in to change notification settings - Fork 5
/
server_test.go
121 lines (101 loc) · 3.31 KB
/
server_test.go
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
116
117
118
119
120
121
package main
import (
"encoding/json"
"testing"
"github.com/vipnode/ethstats/stats"
)
const authMsg = `{
"emit": [
"hello",
{
"id": "foo",
"info": {
"name": "foo",
"node": "Geth/v1.8.3-unstable/linux-amd64/go1.10",
"port": 30303,
"net": "1",
"protocol": "les/2",
"api": "No",
"os": "linux",
"os_v": "amd64",
"client": "0.1.1",
"canUpdateHistory": true
},
"secret": ""
}
]
}`
const pingMsg = `{
"emit": [
"node-ping",
{
"clientTime": "2018-05-17T21:15:15.389Z",
"id": "foo"
}
]
}`
// NOTE: For some reason geth uses time.Time.string() which includes a monotonic offset
// that does not unmarshal properly.
// Eg: {"clientTime": "2018-05-17 16:53:43.96985387 -0400 EDT m=+15.136170456"}
func TestParseAuth(t *testing.T) {
var emit EmitMessage
if err := emit.UnmarshalJSON([]byte(authMsg)); err != nil {
t.Fatalf("failed to parse: %q", err)
}
if emit.Topic != "hello" {
t.Errorf("unexpected emit topic: %q", emit.Topic)
}
report := stats.AuthReport{}
if err := json.Unmarshal(emit.Payload, &report); err != nil {
t.Errorf("failed to parse: %q", err)
}
if report.ID != "foo" {
t.Errorf("incorrect ID: %q", report.ID)
}
}
func TestParsePing(t *testing.T) {
var emitMsg EmitMessage
if err := emitMsg.UnmarshalJSON([]byte(pingMsg)); err != nil {
t.Fatalf("failed to parse: %q", err)
}
if emitMsg.Topic != "node-ping" {
t.Errorf("unexpected emit topic: %q", emitMsg.Topic)
}
var r stats.PingReport
if err := json.Unmarshal(emitMsg.Payload, &r); err != nil {
t.Fatalf("failed to parse: %q", err)
}
if r.ID != "foo" {
t.Errorf("incorrect ID: %q", r.ID)
}
if r.ClientTime.Second() != 15 {
t.Errorf("incorrect timestamp: %q", r.ClientTime)
}
}
const blockMsg = `{"emit":["block",{"block":{"number":5273251,"hash":"0xe11bc629a85375753ba5a043e5b44c05dedbdb484ed8956f9aec07bf3d93fde5","parentHash":"0x10aa19d73522d15cf004ca602b3b87d79bb903d5f7ba8745fc7959534047c7de","timestamp":1521317517,"miner":"0xb2930b35844a230f00e51431acae96fe543a0347","gasUsed":7984834,"gasLimit":7999992,"difficulty":"3291915733734816","totalDifficulty":"3102951517281028058241","transactions":[],"transactionsRoot":"0xe2fdfcc5707a06727f7624ae01c8a7128194b4fc88579375f2ab96e3bdc12d08","stateRoot":"0xdb34c6952061b45c9f4875ed70475cddd3cee0ba016afbd4c2418bbe9ca539d4","uncles":[]},"id":"a"}]}`
func TestParseBlock(t *testing.T) {
var emitMsg EmitMessage
if err := emitMsg.UnmarshalJSON([]byte(blockMsg)); err != nil {
t.Fatalf("failed to parse: %q", err)
}
if emitMsg.Topic != "block" {
t.Errorf("unexpected emit topic: %q", emitMsg.Topic)
}
node := Node{}
container := struct {
Block *stats.Block `json:"block"`
ID string `json:"id"`
}{
Block: &node.Block,
}
if err := json.Unmarshal(emitMsg.Payload, &container); err != nil {
t.Fatalf("failed to parse: %q", err)
}
if node.Block.Number.String() != "5273251" {
t.Errorf("incorrect block number: %q", node.Block.Number)
}
if node.Block.Hash.String() != "0xe11bc629a85375753ba5a043e5b44c05dedbdb484ed8956f9aec07bf3d93fde5" {
t.Errorf("incorrect block hash: %q", node.Block.Hash)
}
}
const statsMsg = `{"emit":["stats",{"id":"a","stats":{"active":true,"syncing":true,"mining":false,"hashrate":0,"peers":0,"gasPrice":0,"uptime":100}}]}`