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" ) // TestRepro is the case the registration fixture hit: ten blocks, five // creations each, no spends. func TestRepro(t *testing.T) { dir := t.TempDir() if _, err := WriteSet(dir, 0, nil); err != nil { t.Fatal(err) } if _, err := Promote(dir, 0); err != nil { t.Fatal(err) } var set []utxo.Entry var ts []utxo.Touch for h := 0; h < 10; h++ { var block []utxo.Touch for k := 0; k < 5; k++ { n := uint32(h*5 + k) block = append(block, utxo.Touch{OutPoint: mkOp(n), Height: uint32(h), New: utxo.Entry{OutPoint: mkOp(n), Value: int64(n), Height: uint32(h)}}) } ts = append(ts, block...) var err error set, err = utxo.ApplyRange(set, block) if err != nil { t.Fatal(err) } } if _, err := CutRange(dir, 0, 10, ts); err != nil { t.Fatal(err) } d, err := LoadDiff(dir, 0, 10) if err != nil { t.Fatal(err) } got, err := MergeRange(dir, 0, 10, d, chainhash.Hash{}) if err != nil { t.Fatalf("merge: %v", err) } if got != NameOf(set) { // The written file is the merge's; its rows are what printed next. raw := readFile(t, dir, SetFile(10)) for i := 0; i+EntrySize <= len(raw); i += EntrySize { buf := make([]byte, EntrySize) copy(buf, raw[i:i+EntrySize]) e := utxo.Decode(buf) w := set[i/EntrySize] if e != w { t.Logf("row %d: file {op=%x v=%d h=%d cb=%v} replay {op=%x v=%d h=%d cb=%v}", i/EntrySize, e.OutPoint.Hash[:2], e.Value, e.Height, e.Coinbase, w.OutPoint.Hash[:2], w.Value, w.Height, w.Coinbase) } } t.Fatalf("merge %s, replay %s", got, NameOf(set)) } _ = binary.LittleEndian _ = wire.OutPoint{} } // TestEntryHeights checks what the merge would read for every created key, // before the merge runs: the entry file must hold each key's own record. func TestEntryHeights(t *testing.T) { dir := t.TempDir() var ts []utxo.Touch for h := 0; h < 10; h++ { for k := 0; k < 5; k++ { n := uint32(h*5 + k) ts = append(ts, utxo.Touch{OutPoint: mkOp(n), Height: uint32(h), New: utxo.Entry{OutPoint: mkOp(n), Value: int64(n), Height: uint32(h)}}) } } d := utxo.Diff{From: 0, To: 10, Touches: ts} if err := writeKeys(dir+"/kc", dir+"/ks", d); err != nil { t.Fatal(err) } k, err := openKeys(dir + "/kc") if err != nil { t.Fatal(err) } defer k.close() if err := k.openValues(dir + "/kcv"); err != nil { t.Fatal(err) } for i := 0; i < k.n; i++ { e, ok := k.entry(i) if !ok || e.Height != uint32(i/5) { t.Fatalf("entry %d: ok=%v height=%d, want %d", i, ok, e.Height, i/5) } } }