package filter_test import ( "encoding/hex" "encoding/json" "os" "path/filepath" "runtime" "testing" "git.smesh.lol/btcd-lite/internal/filter" "git.smesh.lol/btcd-lite/internal/wire" ) // bip158Vector is one entry of the official BIP158 vector file, the layout // nostrino's pkg/filter carries. The previous output scripts are listed in the // order the block's non-coinbase inputs spend them. type bip158Vector struct { Height uint32 `json:"height"` BlockHash string `json:"blockHash"` Block string `json:"block"` PrevOutputScripts []string `json:"prevOutputScripts"` Filter string `json:"filter"` Note string `json:"note"` } // LoadVectors reads a vector file. The path defaults to the official testnet19 // vectors in nostrino; Tests can point BIP158_VECTORS at another file with the // same layout, which is how the fork's vectors are run through the same code. func LoadVectors(t *testing.T) []bip158Vector { t.Helper() path := os.Getenv("BIP158_VECTORS") if path == "" { // The vectors live in the nostrino checkout, found from this module's // replace directive rather than assumed to be beside it. _, file, _, _ := runtime.Caller(0) path = filepath.Join(filepath.Dir(file), "..", "..", "testdata", "bip158_testnet19.json") } raw, err := os.ReadFile(path) if err != nil { t.Fatalf("read vectors: %v", err) } var vectors []bip158Vector if err := json.Unmarshal(raw, &vectors); err != nil { t.Fatalf("parse vectors: %v", err) } if len(vectors) == 0 { t.Fatal("no vectors") } return vectors } // RunVectors rebuilds each filter from the block and its previous output // scripts and requires the exact serialized bytes. func RunVectors(t *testing.T, vectors []bip158Vector) { t.Helper() for _, v := range vectors { v := v t.Run(v.BlockHash, func(t *testing.T) { raw, err := hex.DecodeString(v.Block) if err != nil { t.Fatalf("decode block: %v", err) } block, err := wire.ParseBlock(raw) if err != nil { t.Fatalf("parse block: %v", err) } if got := block.BlockHash().String(); got != v.BlockHash { t.Fatalf("block hash %s, want %s", got, v.BlockHash) } // The vector's scripts are matched to input order, which is what // the lookup is keyed by here. scripts := make([][]byte, len(v.PrevOutputScripts)) for i, s := range v.PrevOutputScripts { b, err := hex.DecodeString(s) if err != nil { t.Fatalf("decode prev script %d: %v", i, err) } scripts[i] = b } next := 0 lookup := func(op wire.OutPoint) ([]byte, bool) { if next >= len(scripts) { return nil, false } s := scripts[next] next++ return s, true } got, err := filter.Build(block, lookup) if err != nil { t.Fatalf("build: %v", err) } want, err := hex.DecodeString(v.Filter) if err != nil { t.Fatalf("decode filter: %v", err) } if !filter.Equal(got, want) { t.Fatalf("filter mismatch\n got %x\nwant %x\nnote: %s", got, want, v.Note) } hit, err := filter.Match(got, block.BlockHash(), scripts) if err != nil { t.Fatalf("match: %v", err) } if len(scripts) > 0 && !hit { // The scripts are all in the element set by construction, so a // miss here would mean the built filter is not the vector's. t.Fatalf("built filter does not match its own input scripts") } }) } } func TestBIP158Vectors(t *testing.T) { RunVectors(t, LoadVectors(t)) }