package utxostore import ( "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" ) // TestWrittenEntries lists what the merge wrote, in order, next to what the // walk holds, so the entry that never got written is named rather than // inferred from a count. func TestWrittenEntries(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) } } d := utxo.Diff{From: 0, To: 106, Touches: touches} _, err := MergeRange(dir, 0, 106, d, chainhash.Hash{}) if err != nil { t.Fatalf("merge: %v", err) } raw, _ := os.ReadFile(filepath.Join(dir, SetFile(106))) written := make([]utxo.Entry, 0, len(raw)/EntrySize) buf := make([]byte, EntrySize) for i := 0; i+EntrySize <= len(raw); i += EntrySize { copy(buf, raw[i:i+EntrySize]) written = append(written, utxo.Decode(buf)) } have := map[wire.OutPoint]bool{} for _, e := range written { have[e.OutPoint] = true } t.Logf("%d written, %d in the walk", len(written), len(set)) shown := 0 for _, e := range set { if have[e.OutPoint] { continue } if shown < 8 { t.Logf(" not written: %s v=%d h=%d", short(e.OutPoint), e.Value, e.Height) } shown++ } if shown > 0 { t.Fatalf("%d entries the walk holds and the merge never wrote", shown) } }