package utxostore import ( "encoding/binary" "os" "path/filepath" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/wire" ) // TestCreationBelowASpentBaseEntry is the whole of it: one base entry, spent, and // one creation that sorts before it. The merge must write the creation and drop // the base entry; the walk does the same. func TestCreationBelowASpentBaseEntry(t *testing.T) { base := []utxo.Entry{ {OutPoint: sop(0), Value: 0}, {OutPoint: sop(200), Value: 200}, } ts := []utxo.Touch{ {OutPoint: sop(50), Height: 1, New: utxo.Entry{OutPoint: sop(50), Value: 50, Height: 1}}, {OutPoint: sop(200), Height: 1, Spent: true}, } want, err := utxo.ApplyRange(base, ts) if err != nil { t.Fatal(err) } dir := t.TempDir() if _, err := WriteSet(dir, 0, base); err != nil { t.Fatal(err) } if _, err := Promote(dir, 0); err != nil { t.Fatal(err) } if _, err := CutRange(dir, 0, 1, ts); err != nil { t.Fatal(err) } d, err := LoadDiff(dir, 0, 1) if err != nil { t.Fatal(err) } got, err := MergeRange(dir, 0, 1, d, NameOf(want)) if err != nil { t.Fatalf("merge: %v", err) } if got != NameOf(want) { raw, _ := os.ReadFile(filepath.Join(dir, SetFile(1))) buf := make([]byte, EntrySize) var vals []int64 for i := 0; i+EntrySize <= len(raw); i += EntrySize { copy(buf, raw[i:i+EntrySize]) vals = append(vals, utxo.Decode(buf).Value) } var wantVals []int64 for _, e := range want { wantVals = append(wantVals, e.Value) } t.Fatalf("merge %s wrote %v, walk %s is %v", got, vals, NameOf(want), wantVals) } _ = chainhash.Hash{} } func sop(i uint32) wire.OutPoint { var h [32]byte binary.LittleEndian.PutUint32(h[:4], i) binary.BigEndian.PutUint32(h[28:], i) return wire.OutPoint{Hash: h} } // TestCreationBetweenBaseEntries: a creation that sorts between two base // entries, one of which the range spends. The entry the writer must not lose is // the base entry that survives, and the creation must appear once. // // The merge passed one buffer to the created path and then wrote the base entry // from it, so the creation came out twice and the surviving base entry came out // never. The values made it look like a duplicate; the set was wrong. func TestCreationBetweenBaseEntries(t *testing.T) { base := []utxo.Entry{ {OutPoint: sop(0), Value: 0}, {OutPoint: sop(100), Value: 100}, {OutPoint: sop(200), Value: 200}, {OutPoint: sop(300), Value: 300}, {OutPoint: sop(400), Value: 400}, } ts := []utxo.Touch{ {OutPoint: sop(50), Height: 1, New: utxo.Entry{OutPoint: sop(50), Value: 50, Height: 1}}, {OutPoint: sop(200), Height: 1, Spent: true}, } want, err := utxo.ApplyRange(base, ts) if err != nil { t.Fatal(err) } dir := t.TempDir() if _, err := WriteSet(dir, 0, base); err != nil { t.Fatal(err) } if _, err := Promote(dir, 0); err != nil { t.Fatal(err) } if _, err := CutRange(dir, 0, 1, ts); err != nil { t.Fatal(err) } d, err := LoadDiff(dir, 0, 1) if err != nil { t.Fatal(err) } got, err := MergeRange(dir, 0, 1, d, NameOf(want)) if err != nil { t.Fatalf("merge: %v", err) } if got != NameOf(want) { raw, _ := os.ReadFile(filepath.Join(dir, SetFile(1))) buf := make([]byte, EntrySize) var vals []int64 for i := 0; i+EntrySize <= len(raw); i += EntrySize { copy(buf, raw[i:i+EntrySize]) vals = append(vals, utxo.Decode(buf).Value) } var wantVals []int64 for _, e := range want { wantVals = append(wantVals, e.Value) } t.Fatalf("merge wrote %v, the walk is %v", vals, wantVals) } }