package utxostore import ( "fmt" "os" "path/filepath" "sort" "strings" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/wire" ) // TestKeyPairsValue: for every creation the merge writes, the bytes it writes // are the entries the diff recorded for that outpoint - value, script hash and // height included. The set is named by its bytes, so a pair that is right in // order and wrong in content shows up as a different name for the same entry // count, which is what the real-block comparison reported. func TestKeyPairsValue(t *testing.T) { dir := t.TempDir() funder, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710") base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funder}, Value: 50_0000_0000, Height: 1, Coinbase: true}} if _, err := WriteSet(dir, 0, base); err != nil { t.Fatal(err) } if _, err := Promote(dir, 0); err != nil { t.Fatal(err) } var touches []utxo.Touch set := append([]utxo.Entry(nil), base...) for h := uint32(80); h <= 105; h++ { raw, err := os.ReadFile(filepath.Join("..", "testdata", fmt.Sprintf("regtest-block-%d.hex", h))) if err != nil { t.Skip("no blocks") } b, err := wire.ParseBlock(mustDecode(t, strings.TrimSpace(string(raw)))) if err != nil { t.Fatal(err) } lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) }) ts, err := utxo.BlockTouches(b, h, lookup) if err != nil { t.Fatal(err) } touches = append(touches, ts...) set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatal(err) } } // What the diff says each creation is. want := make(map[wire.OutPoint]utxo.Entry, len(touches)) for _, tc := range touches { if !tc.Spent { want[tc.OutPoint] = tc.New } } // What the merge writes. if _, err := WriteSet(dir, 106, set); err != nil { t.Fatal(err) } keepScratch = true d := utxo.Diff{From: 0, To: 106, Touches: touches} if _, err := MergeRange(dir, 0, 106, d, chainhash.Hash{}); err != nil { t.Fatalf("merge: %v", err) } keepScratch = false createdPath := filepath.Join(dir, "keys-000000000-000000106.created") keys, _ := os.ReadFile(createdPath) vals, _ := os.ReadFile(createdPath + "v") t.Logf("%d keys, %d value records", len(keys)/36, len(vals)/EntrySize) bad := 0 for i := 0; i < len(keys)/36 && i < len(vals)/EntrySize; i++ { var op wire.OutPoint copy(op.Hash[:], keys[i*36:i*36+32]) op.Index = uint32(keys[i*36+32]) | uint32(keys[i*36+33])<<8 | uint32(keys[i*36+34])<<16 | uint32(keys[i*36+35])<<24 e := utxo.Decode(vals[i*EntrySize : (i+1)*EntrySize]) w, ok := want[op] if !ok { bad++ if bad < 4 { t.Logf(" key %d %s has no creation in the diff", i, short(op)) } continue } if e != w { bad++ if bad < 6 { t.Logf(" key %d %s: written v=%d h=%d, diff says v=%d h=%d", i, short(op), e.Value, e.Height, w.Value, w.Height) } } } if bad != 0 { t.Fatalf("%d of %d pairs disagree with the diff", bad, len(keys)/36) } t.Logf("every key is paired with the diff's own entry") } // TestKeyFileSorted: the key file the merge reads is in the package's order. func TestKeyFileSorted(t *testing.T) { src := "keys-000000000-000000106.created" _ = src _ = sort.Slice }