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" ) func mkOp(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} } // TestWriteKeysThenReadBack is the merge's plumbing on its own: the key list // and the value file must hand back, for each key, the entry that key created. // It is a unit test of the two cursors because the merge's failure was exactly // this pairing. func TestWriteKeysThenReadBack(t *testing.T) { dir := t.TempDir() var ts []utxo.Touch for h := 0; h < 10; h++ { for k := 0; k < 5; k++ { id := uint32(h*5 + k) ts = append(ts, utxo.Touch{OutPoint: mkOp(id), Height: uint32(h), New: utxo.Entry{OutPoint: mkOp(id), Value: int64(id), Height: uint32(h)}}) } } d := utxo.Diff{From: 0, To: 10, Touches: ts} createdPath, spentPath := dir+"/kc", dir+"/ks" if err := writeKeys(createdPath, spentPath, d); err != nil { t.Fatal(err) } k, err := openKeys(createdPath) if err != nil { t.Fatal(err) } defer k.close() if err := k.openValues(createdPath + "v"); err != nil { t.Fatal(err) } var keys []wire.OutPoint var entries []utxo.Entry for k.have { keys = append(keys, k.cur) e, ok := k.entry(k.i) if !ok { t.Fatalf("no entry for key %d", k.i) } entries = append(entries, e) k.advance() } if len(keys) != 50 { t.Fatalf("%d keys", len(keys)) } for i := range keys { if entries[i].OutPoint != keys[i] { t.Fatalf("key %d is %x, its entry names %x", i, keys[i].Hash[:2], entries[i].OutPoint.Hash[:2]) } // The key's id is where op put it: little endian in the first four // bytes of the hash. id := int64(binary.LittleEndian.Uint32(keys[i].Hash[:4])) if entries[i].Value != id { t.Fatalf("key %d has value %d", i, entries[i].Value) } } } // TestMergeCreationsOnly runs the merge over a base with no entries and a diff // of creations only, which is the case that exposed the value file being read // at the wrong index. func TestMergeCreationsOnly(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 ts []utxo.Touch var want []utxo.Entry for h := 0; h < 10; h++ { var block []utxo.Touch for k := 0; k < 5; k++ { id := uint32(h*5 + k) e := utxo.Entry{OutPoint: mkOp(id), Value: int64(id), Height: uint32(h)} block = append(block, utxo.Touch{OutPoint: e.OutPoint, Height: uint32(h), New: e}) want = append(want, e) } ts = append(ts, block...) } 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, NameOf(want)) if err != nil { t.Fatal(err) } if got != NameOf(want) { t.Fatalf("merge gives %s, the set is %s", got, NameOf(want)) } } // TestMergeStreamCreationsCount checks the merge's output length for the // creations-only case, so a name match cannot hide a wrong count. func TestMergeStreamCreationsCount(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 ts []utxo.Touch for h := 0; h < 10; h++ { for k := 0; k < 5; k++ { id := uint32(h*5 + k) ts = append(ts, utxo.Touch{OutPoint: mkOp(id), Height: uint32(h), New: utxo.Entry{OutPoint: mkOp(id), Value: int64(id), Height: uint32(h)}}) } } if _, err := CutRange(dir, 0, 10, ts); err != nil { t.Fatal(err) } d, _ := LoadDiff(dir, 0, 10) if _, err := MergeRange(dir, 0, 10, d, chainhash.Hash{}); err != nil { t.Fatal(err) } got, err := LoadReplay(dir, 0) _ = got if err != nil { t.Fatal(err) } f, err := os.OpenFile(filepath.Join(dir, SetFile(10)), os.O_RDONLY, 0) if err != nil { t.Fatal(err) } fi, _ := f.Stat() f.Close() if want := int64(50 * EntrySize); fi.Size() != want { t.Fatalf("merged file is %d bytes, want %d", fi.Size(), want) } }