filter_test.go raw

   1  package filter_test
   2  
   3  import (
   4  	"encoding/hex"
   5  	"encoding/json"
   6  	"os"
   7  	"path/filepath"
   8  	"runtime"
   9  	"testing"
  10  
  11  	"git.smesh.lol/btcd-lite/internal/filter"
  12  	"git.smesh.lol/btcd-lite/internal/wire"
  13  )
  14  
  15  // bip158Vector is one entry of the official BIP158 vector file, the layout
  16  // nostrino's pkg/filter carries. The previous output scripts are listed in the
  17  // order the block's non-coinbase inputs spend them.
  18  type bip158Vector struct {
  19  	Height            uint32   `json:"height"`
  20  	BlockHash         string   `json:"blockHash"`
  21  	Block             string   `json:"block"`
  22  	PrevOutputScripts []string `json:"prevOutputScripts"`
  23  	Filter            string   `json:"filter"`
  24  	Note              string   `json:"note"`
  25  }
  26  
  27  // LoadVectors reads a vector file. The path defaults to the official testnet19
  28  // vectors in nostrino; Tests can point BIP158_VECTORS at another file with the
  29  // same layout, which is how the fork's vectors are run through the same code.
  30  func LoadVectors(t *testing.T) []bip158Vector {
  31  	t.Helper()
  32  	path := os.Getenv("BIP158_VECTORS")
  33  	if path == "" {
  34  		// The vectors live in the nostrino checkout, found from this module's
  35  		// replace directive rather than assumed to be beside it.
  36  		_, file, _, _ := runtime.Caller(0)
  37  		path = filepath.Join(filepath.Dir(file), "..", "..", "testdata", "bip158_testnet19.json")
  38  	}
  39  	raw, err := os.ReadFile(path)
  40  	if err != nil {
  41  		t.Fatalf("read vectors: %v", err)
  42  	}
  43  	var vectors []bip158Vector
  44  	if err := json.Unmarshal(raw, &vectors); err != nil {
  45  		t.Fatalf("parse vectors: %v", err)
  46  	}
  47  	if len(vectors) == 0 {
  48  		t.Fatal("no vectors")
  49  	}
  50  	return vectors
  51  }
  52  
  53  // RunVectors rebuilds each filter from the block and its previous output
  54  // scripts and requires the exact serialized bytes.
  55  func RunVectors(t *testing.T, vectors []bip158Vector) {
  56  	t.Helper()
  57  	for _, v := range vectors {
  58  		v := v
  59  		t.Run(v.BlockHash, func(t *testing.T) {
  60  			raw, err := hex.DecodeString(v.Block)
  61  			if err != nil {
  62  				t.Fatalf("decode block: %v", err)
  63  			}
  64  			block, err := wire.ParseBlock(raw)
  65  			if err != nil {
  66  				t.Fatalf("parse block: %v", err)
  67  			}
  68  			if got := block.BlockHash().String(); got != v.BlockHash {
  69  				t.Fatalf("block hash %s, want %s", got, v.BlockHash)
  70  			}
  71  			// The vector's scripts are matched to input order, which is what
  72  			// the lookup is keyed by here.
  73  			scripts := make([][]byte, len(v.PrevOutputScripts))
  74  			for i, s := range v.PrevOutputScripts {
  75  				b, err := hex.DecodeString(s)
  76  				if err != nil {
  77  					t.Fatalf("decode prev script %d: %v", i, err)
  78  				}
  79  				scripts[i] = b
  80  			}
  81  			next := 0
  82  			lookup := func(op wire.OutPoint) ([]byte, bool) {
  83  				if next >= len(scripts) {
  84  					return nil, false
  85  				}
  86  				s := scripts[next]
  87  				next++
  88  				return s, true
  89  			}
  90  			got, err := filter.Build(block, lookup)
  91  			if err != nil {
  92  				t.Fatalf("build: %v", err)
  93  			}
  94  			want, err := hex.DecodeString(v.Filter)
  95  			if err != nil {
  96  				t.Fatalf("decode filter: %v", err)
  97  			}
  98  			if !filter.Equal(got, want) {
  99  				t.Fatalf("filter mismatch\n got %x\nwant %x\nnote: %s", got, want, v.Note)
 100  			}
 101  			hit, err := filter.Match(got, block.BlockHash(), scripts)
 102  			if err != nil {
 103  				t.Fatalf("match: %v", err)
 104  			}
 105  			if len(scripts) > 0 && !hit {
 106  				// The scripts are all in the element set by construction, so a
 107  				// miss here would mean the built filter is not the vector's.
 108  				t.Fatalf("built filter does not match its own input scripts")
 109  			}
 110  		})
 111  	}
 112  }
 113  
 114  func TestBIP158Vectors(t *testing.T) { RunVectors(t, LoadVectors(t)) }
 115