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" ) // loadNodeBodies reads the captured node bodies. func loadNodeBodies(t *testing.T, n uint32) [][]byte { t.Helper() out := make([][]byte, 0, n) for h := uint32(0); h < n; h++ { 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) } out = append(out, b) } return out } // TestNodeBodiesRange5 walks the node's first five bodies and merges them // against an empty base, the way the builder does at checkpoint 5. func TestNodeBodiesRange5(t *testing.T) { for n := uint32(1); n <= 21; n++ { bodies := loadNodeBodies(t, n) var set []utxo.Entry var touches []utxo.Touch for h, raw := range bodies { b, err := wire.ParseBlock(raw) if err != nil { t.Fatalf("body %d: %v", h, err) } lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) }) ts, err := utxo.BlockTouches(b, uint32(h), lookup) if err != nil { t.Fatalf("body %d touches: %v", h, err) } touches = append(touches, ts...) set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatalf("body %d apply: %v", h, err) } } dir := t.TempDir() if _, err := WriteSet(dir, 0, nil); err != nil { t.Fatal(err) } if _, err := Promote(dir, 0); err != nil { t.Fatal(err) } if _, err := CutRange(dir, 0, n, touches); err != nil { t.Fatal(err) } d, err := LoadDiff(dir, 0, n) if err != nil { t.Fatal(err) } got, err := MergeRange(dir, 0, n, d, NameOf(set)) if err != nil { t.Logf("n=%d FAILS: %v", n, err) continue } _ = got t.Logf("n=%d ok, %d entries", n, len(set)) } _ = chainhash.Hash{} } // TestNodeBodiesTwoRanges is the builder's shape: a checkpoint at 4, then the // range 4..8 merged against it. Every single range from 0 passes, so if this // fails the fault is in what the merge does from a base, not in the touches. func TestNodeBodiesTwoRanges(t *testing.T) { bodies := loadNodeBodies(t, 9) var set []utxo.Entry var first, second []utxo.Touch for h, raw := range bodies { b, err := wire.ParseBlock(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, uint32(h), lookup) if err != nil { t.Fatalf("body %d touches: %v", h, err) } if h < 4 { first = append(first, ts...) } else { second = append(second, ts...) } set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatalf("body %d apply: %v", h, err) } } dir := t.TempDir() if _, err := WriteSet(dir, 0, nil); err != nil { t.Fatal(err) } if _, err := Promote(dir, 0); err != nil { t.Fatal(err) } if _, err := CutRange(dir, 0, 4, first); err != nil { t.Fatal(err) } d1, _ := LoadDiff(dir, 0, 4) if _, err := MergeRange(dir, 0, 4, d1, chainhash.Hash{}); err != nil { t.Fatalf("checkpoint 4: %v", err) } if _, err := CutRange(dir, 4, 8, second); err != nil { t.Fatal(err) } d2, err := LoadDiff(dir, 4, 8) if err != nil { t.Fatal(err) } // The base at 4 is the file the first merge wrote; the walk's set at 8 is // what the second merge must reproduce. got, err := MergeRange(dir, 4, 8, d2, NameOf(set)) if err != nil { t.Fatalf("checkpoint 8: %v", err) } t.Logf("two ranges over node bodies agree: %s", got) } // TestNodeBodiesSecondRangeEntries prints what the two sides hold for the second // range, entry by entry. func TestNodeBodiesSecondRangeEntries(t *testing.T) { bodies := loadNodeBodies(t, 9) var set []utxo.Entry var first, second []utxo.Touch for h, raw := range bodies { b, err := wire.ParseBlock(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, uint32(h), lookup) if err != nil { t.Fatal(err) } if h < 4 { first = append(first, ts...) } else { second = append(second, ts...) } set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatal(err) } } dir := t.TempDir() _, _ = WriteSet(dir, 0, nil) _, _ = Promote(dir, 0) _, _ = CutRange(dir, 0, 4, first) d1, _ := LoadDiff(dir, 0, 4) cp4, _ := MergeRange(dir, 0, 4, d1, chainhash.Hash{}) t.Logf("checkpoint 4: %s", cp4) // What the checkpoint at 4 holds, read back. raw, _ := os.ReadFile(filepath.Join(dir, SetFile(4))) buf := make([]byte, EntrySize) var at4 []utxo.Entry for i := 0; i+EntrySize <= len(raw); i += EntrySize { copy(buf, raw[i:i+EntrySize]) at4 = append(at4, utxo.Decode(buf)) } t.Logf("checkpoint 4 holds %d entries", len(at4)) _, _ = CutRange(dir, 4, 8, second) d2, _ := LoadDiff(dir, 4, 8) _, err := MergeRange(dir, 4, 8, d2, NameOf(set)) t.Logf("merge 4..8 err %v; walk 8 has %d entries", err, len(set)) raw, _ = os.ReadFile(filepath.Join(dir, SetFile(8))) var at8 []utxo.Entry for i := 0; i+EntrySize <= len(raw); i += EntrySize { copy(buf, raw[i:i+EntrySize]) at8 = append(at8, utxo.Decode(buf)) } t.Logf("merge 4..8 wrote %d entries", len(at8)) for i := 0; i < len(set) && i < len(at8); i++ { if set[i] != at8[i] { t.Logf("first difference at %d: walk v=%d h=%d, merge v=%d h=%d (idx %d vs %d)", i, set[i].Value, set[i].Height, at8[i].Value, at8[i].Height, set[i].OutPoint.Index, at8[i].OutPoint.Index) break } } } // TestNodeBodiesSecondRangeWalkFromBase walks the second range starting from the // checkpoint file the first merge wrote, rather than from the running set. If // that walk matches the merge, the merge is right and the earlier walk was // wrong about the base it was given. func TestNodeBodiesSecondRangeWalkFromBase(t *testing.T) { bodies := loadNodeBodies(t, 9) var first []utxo.Touch var secondRaw [][]byte for h, raw := range bodies { if h < 4 { b, err := wire.ParseBlock(raw) if err != nil { t.Fatal(err) } ts, err := utxo.BlockTouches(b, uint32(h), nil) if err != nil { t.Fatal(err) } first = append(first, ts...) } else { secondRaw = append(secondRaw, raw) } } dir := t.TempDir() _, _ = WriteSet(dir, 0, nil) _, _ = Promote(dir, 0) _, _ = CutRange(dir, 0, 4, first) d1, _ := LoadDiff(dir, 0, 4) cp4, err := MergeRange(dir, 0, 4, d1, chainhash.Hash{}) if err != nil { t.Fatal(err) } t.Logf("checkpoint 4 %s", cp4) // The walk from the file. raw, _ := os.ReadFile(filepath.Join(dir, SetFile(4))) buf := make([]byte, EntrySize) var set []utxo.Entry for i := 0; i+EntrySize <= len(raw); i += EntrySize { copy(buf, raw[i:i+EntrySize]) set = append(set, utxo.Decode(buf)) } var second []utxo.Touch for i, rawBody := range secondRaw { h := uint32(4 + i) b, err := wire.ParseBlock(rawBody) 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.Fatalf("body %d: %v", h, err) } second = append(second, ts...) set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatalf("body %d apply: %v", h, err) } } if _, err := CutRange(dir, 4, 8, second); err != nil { t.Fatal(err) } d2, _ := LoadDiff(dir, 4, 8) got, err := MergeRange(dir, 4, 8, d2, NameOf(set)) if err != nil { t.Fatalf("merge from the file: %v", err) } t.Logf("walk from the file agrees with the merge: %s", got) } // TestNodeBodiesSecondRangeTouches prints the second range's touches and the // base they are applied to, so the difference between the walk and the merge // is visible in the data rather than in the code. func TestNodeBodiesSecondRangeTouches(t *testing.T) { bodies := loadNodeBodies(t, 9) var first []utxo.Touch for h, raw := range bodies { if h >= 4 { break } b, _ := wire.ParseBlock(raw) ts, err := utxo.BlockTouches(b, uint32(h), nil) if err != nil { t.Fatal(err) } first = append(first, ts...) } dir := t.TempDir() _, _ = WriteSet(dir, 0, nil) _, _ = Promote(dir, 0) _, _ = CutRange(dir, 0, 4, first) d1, _ := LoadDiff(dir, 0, 4) _, _ = MergeRange(dir, 0, 4, d1, chainhash.Hash{}) raw, _ := os.ReadFile(filepath.Join(dir, SetFile(4))) buf := make([]byte, EntrySize) var set []utxo.Entry for i := 0; i+EntrySize <= len(raw); i += EntrySize { copy(buf, raw[i:i+EntrySize]) set = append(set, utxo.Decode(buf)) } t.Logf("base at 4: %d entries", len(set)) var second []utxo.Touch for h := uint32(4); h < 8; h++ { b, _ := wire.ParseBlock(bodies[h]) 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.Fatalf("body %d: %v", h, err) } for _, tc := range ts { t.Logf(" body %d touch idx=%d spent=%v v=%d", h, tc.OutPoint.Index, tc.Spent, tc.New.Value) } second = append(second, ts...) set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatalf("body %d apply: %v", h, err) } } t.Logf("walks to %d entries", len(set)) }