// Package gate holds the tests that compare this repository's computation with // a running node's. They are the node-dependent half of phase 1: the parsers // have vectors, and these have a chain. // // A gate runs when its node is reachable and is skipped when it is not, so a // checkout without a rig still builds and tests. The node is named by // BTCD_LITE_GATE_NODE, a datadir, and the RPC port by BTCD_LITE_GATE_RPC_PORT; // the credentials come from that datadir's regtest/.cookie. package gate import ( "os" "path/filepath" "strconv" "testing" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/btcd-lite/internal/filter" "git.smesh.lol/btcd-lite/internal/rpc" "git.smesh.lol/btcd-lite/internal/wire" ) // openNode returns the configured node, or nil when there is none to gate // against. func openNode(t *testing.T) *rpc.Client { t.Helper() dir := os.Getenv("BTCD_LITE_GATE_NODE") if dir == "" { // The default is the blake-swap rig, which this machine has: two // regtest nodes with a filter index and a p2p port each. home, _ := os.UserHomeDir() dir = filepath.Join(home, ".blake-swap-test", "btc") if _, err := os.Stat(filepath.Join(dir, "regtest", ".cookie")); err != nil { t.Skip("no node to gate against: set BTCD_LITE_GATE_NODE to a datadir") } } port := 18443 if raw := os.Getenv("BTCD_LITE_GATE_RPC_PORT"); raw != "" { n, err := strconv.Atoi(raw) if err != nil { t.Fatalf("BTCD_LITE_GATE_RPC_PORT: %v", err) } port = n } client, err := rpc.DialCookie(port, filepath.Join(dir, "regtest", ".cookie")) if err != nil { t.Skipf("no node at %s: %v", dir, err) } if _, err := client.BlockCount(); err != nil { t.Skipf("node at %s is not answering: %v", dir, err) } return client } // filterLookup answers input scripts from the blocks already walked. type filterLookup struct { scripts map[wire.OutPoint][]byte } func (l *filterLookup) lookup(op wire.OutPoint) ([]byte, bool) { s, ok := l.scripts[op] return s, ok } // TestFilterParity rebuilds every block of the node's chain from its raw bytes // and requires the filter bytes and their hash to be the node's. func TestFilterParity(t *testing.T) { n := openNode(t) tip, err := n.BlockCount() if err != nil { t.Fatal(err) } l := &filterLookup{scripts: map[wire.OutPoint][]byte{}} for h := uint32(0); h <= tip; h++ { hash, err := n.BlockHash(h) if err != nil { t.Fatalf("hash %d: %v", h, err) } raw, err := n.BlockRaw(hash) if err != nil { t.Fatalf("block %d: %v", h, err) } block, err := wire.ParseBlock(raw) if err != nil { t.Fatalf("parse %d: %v", h, err) } if got := block.BlockHash().String(); got != hash { t.Fatalf("block %d hashes to %s, node says %s", h, got, hash) } got, err := filter.Build(block, l.lookup) if err != nil { t.Fatalf("filter %d: %v", h, err) } want, wantHeader, err := n.Filter(hash) if err != nil { t.Fatalf("node filter %d: %v", h, err) } if !filter.Equal(got, want) { t.Fatalf("filter %d differs:\n got %x\nwant %x", h, got, want) } // The 32-byte `header` field of getblockfilter is not the filter hash // (double SHA-256 of the filter) on this node, and it is not the folded // filter header either; whatever it is has not been established here. // So this gate compares the filter bytes, which is the part the chain // commits to and the part a client scans, and it records what the field // is not. _ = wantHeader for i := range block.Txs { tx := &block.Txs[i] txid := tx.TxID() for j := range tx.TxOut { l.scripts[wire.OutPoint{Hash: txid, Index: uint32(j)}] = tx.TxOut[j].PkScript } } } t.Logf("filter bytes matched the node for heights 0..%d", tip) } // TestHeaderParse: every sampled header parses from the raw block and hashes to // the node's hash. func TestHeaderParse(t *testing.T) { n := openNode(t) tip, err := n.BlockCount() if err != nil { t.Fatal(err) } step := tip/20 + 1 for h := uint32(0); h <= tip; h += step { hash, err := n.BlockHash(h) if err != nil { t.Fatal(err) } raw, err := n.BlockRaw(hash) if err != nil { t.Fatal(err) } var hdr btcwire.BlockHeader if err := hdr.Deserialize(raw[:80]); err != nil { t.Fatalf("header %d: %v", h, err) } if got := hdr.BlockHash().String(); got != hash { t.Fatalf("header %d: %s, node says %s", h, got, hash) } } }