node_test.go raw
1 package main
2
3 import (
4 "context"
5 "io"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "sync/atomic"
10 "testing"
11
12 "git.smesh.lol/nostrino/pkg/chain"
13 )
14
15 // TestDroppedConnectionsAreRetried: a node closes an idle RPC connection on its
16 // own schedule, and a poller that treated the resulting EOF as fatal exited
17 // every minute and lost the tip it was following.
18 func TestDroppedConnectionsAreRetried(t *testing.T) {
19 var calls int32
20
21 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22 if atomic.AddInt32(&calls, 1) <= 2 {
23 if hijack, ok := w.(http.Hijacker); ok {
24 if conn, _, err := hijack.Hijack(); err == nil {
25 _ = conn.Close()
26
27 return
28 }
29 }
30
31 return
32 }
33
34 w.Header().Set("Content-Type", "application/json")
35 _, _ = io.WriteString(w, `{"result":978331,"error":null}`)
36 }))
37 defer server.Close()
38
39 node, err := newNode(server.URL, "")
40 if err != nil {
41 t.Fatal(err)
42 }
43
44 tip, err := node.tip(context.Background())
45 if err != nil {
46 t.Fatalf("tip after two dropped connections: %v", err)
47 }
48
49 if tip != 978331 {
50 t.Fatalf("tip is %d, want 978331", tip)
51 }
52
53 if got := atomic.LoadInt32(&calls); got != 3 {
54 t.Fatalf("the node was asked %d times, want 3", got)
55 }
56 }
57
58 // TestAnswersAreNotRetried: a node error is the node answering. Asking again
59 // gets the same answer, and a filter cache that retried it would turn a wrong
60 // range into a slow one.
61 func TestAnswersAreNotRetried(t *testing.T) {
62 var calls int32
63
64 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
65 atomic.AddInt32(&calls, 1)
66
67 w.Header().Set("Content-Type", "application/json")
68 _, _ = io.WriteString(w, `{"result":null,"error":{"code":-8,"message":"Block height out of range"}}`)
69 }))
70 defer server.Close()
71
72 node, err := newNode(server.URL, "")
73 if err != nil {
74 t.Fatal(err)
75 }
76
77 _, err = node.tip(context.Background())
78 if err == nil {
79 t.Fatal("a node error was reported as success")
80 }
81
82 if !strings.Contains(err.Error(), "out of range") {
83 t.Fatalf("the node's own message was lost: %v", err)
84 }
85
86 if got := atomic.LoadInt32(&calls); got != 1 {
87 t.Fatalf("the node was asked %d times, want 1", got)
88 }
89 }
90
91 // TestCredentialsAreNotRetried: a wrong password is an answer too.
92 func TestCredentialsAreNotRetried(t *testing.T) {
93 var calls int32
94
95 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
96 atomic.AddInt32(&calls, 1)
97 w.WriteHeader(http.StatusUnauthorized)
98 }))
99 defer server.Close()
100
101 node, err := newNode(server.URL, "")
102 if err != nil {
103 t.Fatal(err)
104 }
105
106 if _, err := node.tip(context.Background()); err == nil {
107 t.Fatal("a refused credential was reported as success")
108 }
109
110 if got := atomic.LoadInt32(&calls); got != 1 {
111 t.Fatalf("the node was asked %d times, want 1", got)
112 }
113 }
114
115 // TestGenesisIsChecked: the chain name in the tags is what a client asks for, so
116 // the node behind it has to be that chain. Pointing the mainnet profile at a
117 // regtest node would publish nineteen thousand blocks of the wrong filters under
118 // the name every wallet trusts, and the client would reject every one of them.
119 func TestGenesisIsChecked(t *testing.T) {
120 serve := func(hash string) *httptest.Server {
121 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
122 w.Header().Set("Content-Type", "application/json")
123 _, _ = io.WriteString(w, `{"result":"`+hash+`","error":null}`)
124 }))
125 }
126
127 profile := &chain.Bitcoin
128
129 right := serve(profile.GenesisHash.String())
130 defer right.Close()
131
132 node, err := newNode(right.URL, "")
133 if err != nil {
134 t.Fatal(err)
135 }
136
137 if err := checkGenesis(context.Background(), node, profile); err != nil {
138 t.Fatalf("the right genesis was refused: %v", err)
139 }
140
141 wrong := serve(chain.Regtest.GenesisHash.String())
142 defer wrong.Close()
143
144 node, err = newNode(wrong.URL, "")
145 if err != nil {
146 t.Fatal(err)
147 }
148
149 if err := checkGenesis(context.Background(), node, profile); err == nil {
150 t.Fatal("a regtest node was accepted as btc")
151 }
152 }
153