package utxostore import ( "encoding/binary" "fmt" "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" ) func e3(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} } func TestDbg3(t *testing.T) { var base []utxo.Entry for i := uint32(0); i < 7; i++ { e := utxo.Entry{OutPoint: e3(i * 10), Value: 5_000_000_000, Height: i % 4, Coinbase: true} if i%2 == 1 { e.Value = 0 } base = append(base, e) } var ts []utxo.Touch for i := uint32(0); i < 8; i++ { id := 5 + i*10 e := utxo.Entry{OutPoint: e3(id), Value: 5_000_000_000, Height: 4 + i%4, Coinbase: true} if i%2 == 1 { e.Value = 0 } ts = append(ts, utxo.Touch{OutPoint: e3(id), Height: 4, New: e}) } want, _ := utxo.ApplyRange(base, ts) dir := t.TempDir() _, _ = WriteSet(dir, 0, base) _, _ = Promote(dir, 0) _, _ = CutRange(dir, 0, 1, ts) d, _ := LoadDiff(dir, 0, 1) keepScratch = true got, err := MergeRange(dir, 0, 1, d, chainhash.Hash{}) keepScratch = false raw, rerr := os.ReadFile(filepath.Join(dir, SetFile(1))) t.Logf("merge %s walk %s err %v fileReadErr %v", got, NameOf(want), err, rerr) buf := make([]byte, EntrySize) var gotSet []utxo.Entry for i := 0; i+EntrySize <= len(raw); i += EntrySize { copy(buf, raw[i:i+EntrySize]) gotSet = append(gotSet, utxo.Decode(buf)) } t.Logf("walk %d, merge %d", len(want), len(gotSet)) for i := 0; i < len(want) || i < len(gotSet); i++ { var w, g string if i < len(want) { w = fmt.Sprintf("idx=%02d v=%d h=%d", want[i].OutPoint.Index, want[i].Value, want[i].Height) } if i < len(gotSet) { g = fmt.Sprintf("idx=%02d v=%d h=%d", gotSet[i].OutPoint.Index, gotSet[i].Value, gotSet[i].Height) } if w != g { t.Logf(" DIFF %d walk [%s] merge [%s]", i, w, g) } } }