package main import ( "context" "io" "net/http" "net/http/httptest" "strings" "sync/atomic" "testing" "git.smesh.lol/nostrino/pkg/chain" ) // TestDroppedConnectionsAreRetried: a node closes an idle RPC connection on its // own schedule, and a poller that treated the resulting EOF as fatal exited // every minute and lost the tip it was following. func TestDroppedConnectionsAreRetried(t *testing.T) { var calls int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if atomic.AddInt32(&calls, 1) <= 2 { if hijack, ok := w.(http.Hijacker); ok { if conn, _, err := hijack.Hijack(); err == nil { _ = conn.Close() return } } return } w.Header().Set("Content-Type", "application/json") _, _ = io.WriteString(w, `{"result":978331,"error":null}`) })) defer server.Close() node, err := newNode(server.URL, "") if err != nil { t.Fatal(err) } tip, err := node.tip(context.Background()) if err != nil { t.Fatalf("tip after two dropped connections: %v", err) } if tip != 978331 { t.Fatalf("tip is %d, want 978331", tip) } if got := atomic.LoadInt32(&calls); got != 3 { t.Fatalf("the node was asked %d times, want 3", got) } } // TestAnswersAreNotRetried: a node error is the node answering. Asking again // gets the same answer, and a filter cache that retried it would turn a wrong // range into a slow one. func TestAnswersAreNotRetried(t *testing.T) { var calls int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { atomic.AddInt32(&calls, 1) w.Header().Set("Content-Type", "application/json") _, _ = io.WriteString(w, `{"result":null,"error":{"code":-8,"message":"Block height out of range"}}`) })) defer server.Close() node, err := newNode(server.URL, "") if err != nil { t.Fatal(err) } _, err = node.tip(context.Background()) if err == nil { t.Fatal("a node error was reported as success") } if !strings.Contains(err.Error(), "out of range") { t.Fatalf("the node's own message was lost: %v", err) } if got := atomic.LoadInt32(&calls); got != 1 { t.Fatalf("the node was asked %d times, want 1", got) } } // TestCredentialsAreNotRetried: a wrong password is an answer too. func TestCredentialsAreNotRetried(t *testing.T) { var calls int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { atomic.AddInt32(&calls, 1) w.WriteHeader(http.StatusUnauthorized) })) defer server.Close() node, err := newNode(server.URL, "") if err != nil { t.Fatal(err) } if _, err := node.tip(context.Background()); err == nil { t.Fatal("a refused credential was reported as success") } if got := atomic.LoadInt32(&calls); got != 1 { t.Fatalf("the node was asked %d times, want 1", got) } } // TestGenesisIsChecked: the chain name in the tags is what a client asks for, so // the node behind it has to be that chain. Pointing the mainnet profile at a // regtest node would publish nineteen thousand blocks of the wrong filters under // the name every wallet trusts, and the client would reject every one of them. func TestGenesisIsChecked(t *testing.T) { serve := func(hash string) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") _, _ = io.WriteString(w, `{"result":"`+hash+`","error":null}`) })) } profile := &chain.Bitcoin right := serve(profile.GenesisHash.String()) defer right.Close() node, err := newNode(right.URL, "") if err != nil { t.Fatal(err) } if err := checkGenesis(context.Background(), node, profile); err != nil { t.Fatalf("the right genesis was refused: %v", err) } wrong := serve(chain.Regtest.GenesisHash.String()) defer wrong.Close() node, err = newNode(wrong.URL, "") if err != nil { t.Fatal(err) } if err := checkGenesis(context.Background(), node, profile); err == nil { t.Fatal("a regtest node was accepted as btc") } }