package utxostore import ( "encoding/hex" "fmt" "os" "path/filepath" "strings" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/wire" ) func nodeBody(t *testing.T, h uint32) []byte { t.Helper() raw, err := os.ReadFile(filepath.Join("..", "..", "utxobuild", "testdata", fmt.Sprintf("body-%03d.hex", h))) if err != nil { t.Skipf("no captured body %d", h) } b, err := hex.DecodeString(strings.TrimSpace(string(raw))) if err != nil { t.Fatal(err) } return b } // nodeEntriesAt4 is the set the first four node bodies build. func nodeEntriesAt4(t *testing.T) []utxo.Entry { t.Helper() var set []utxo.Entry for h := uint32(0); h < 4; h++ { b, err := wire.ParseBlock(nodeBody(t, h)) if err != nil { t.Fatal(err) } ts, err := utxo.BlockTouches(b, h, nil) if err != nil { t.Fatal(err) } set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatal(err) } } return set } // nodeSecondRange applies the bodies 4..7 to base and returns the touches and // the set they produce. func nodeSecondRange(t *testing.T, base []utxo.Entry) ([]utxo.Touch, []utxo.Entry) { t.Helper() set := append([]utxo.Entry(nil), base...) var ts []utxo.Touch for h := uint32(4); h < 8; h++ { b, err := wire.ParseBlock(nodeBody(t, h)) if err != nil { t.Fatal(err) } lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) }) block, err := utxo.BlockTouches(b, h, lookup) if err != nil { t.Fatalf("body %d: %v", h, err) } ts = append(ts, block...) set, err = utxo.ApplyRange(set, block) if err != nil { t.Fatalf("body %d apply: %v", h, err) } } return ts, set } var _ = chainhash.Hash{} // nodeScriptsAt4 is the set at 4 with the script hashes the entries carry, so a // dump can show whether a difference is in the script or in the height. func nodeScriptsAt4(t *testing.T) []utxo.Entry { return nodeEntriesAt4(t) }