package utxostore import ( "encoding/binary" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/wire" ) func qop2(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} } // TestDbgSpendMiddle2 varies one thing at a time from a passing case to the // failing one, so the trigger is named. func TestDbgSpendMiddle2(t *testing.T) { cases := []struct { name string base []uint32 touch []utxo.Touch }{ {"create-then-spend-one-base", []uint32{0, 100, 200, 300, 400}, []utxo.Touch{ {OutPoint: qop2(50), Height: 1, New: utxo.Entry{OutPoint: qop2(50), Value: 50, Height: 1}}, {OutPoint: qop2(200), Height: 1, Spent: true}, }}, {"spend-then-create", []uint32{0, 100, 200, 300, 400}, []utxo.Touch{ {OutPoint: qop2(200), Height: 1, Spent: true}, {OutPoint: qop2(50), Height: 1, New: utxo.Entry{OutPoint: qop2(50), Value: 50, Height: 1}}, }}, {"create-between-spend", []uint32{0, 100, 200, 300, 400}, []utxo.Touch{ {OutPoint: qop2(150), Height: 1, New: utxo.Entry{OutPoint: qop2(150), Value: 150, Height: 1}}, {OutPoint: qop2(200), Height: 1, Spent: true}, {OutPoint: qop2(250), Height: 1, New: utxo.Entry{OutPoint: qop2(250), Value: 250, Height: 1}}, }}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { dir := t.TempDir() var base []utxo.Entry for _, id := range c.base { base = append(base, utxo.Entry{OutPoint: qop2(id), Value: int64(id), Height: 0}) } if _, err := WriteSet(dir, 0, base); err != nil { t.Fatal(err) } if _, err := Promote(dir, 0); err != nil { t.Fatal(err) } want, err := utxo.ApplyRange(base, c.touch) if err != nil { t.Fatalf("walk: %v", err) } if _, err := CutRange(dir, 0, 1, c.touch); err != nil { t.Fatal(err) } d, _ := LoadDiff(dir, 0, 1) got, err := MergeRange(dir, 0, 1, d, chainhash.Hash{}) if err != nil { t.Fatalf("merge: %v", err) } if got != NameOf(want) { t.Fatalf("merge %s walk %s (%d entries)", got, NameOf(want), len(want)) } }) } }