package utxostore import ( "fmt" "os" "path/filepath" "strings" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/wire" ) // TestKeyFileOrder compares the order the diff's creations sort into in memory // with the order writeKeys puts them on disk, over the captured blocks. A // difference there is the merge reading keys in one order and walking in // another. func TestKeyFileOrder(t *testing.T) { dir := t.TempDir() funder, _ := chainhash.NewHashFromStr("36e95a2200bb470593863bf444790eaa31323f2728a0b9d8688a6294776c6710") base := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: *funder}, Value: 50_0000_0000, Height: 1, Coinbase: true}} if _, err := WriteSet(dir, 0, base); err != nil { t.Fatal(err) } if _, err := Promote(dir, 0); err != nil { t.Fatal(err) } var touches []utxo.Touch set := append([]utxo.Entry(nil), base...) for h := uint32(80); h <= 105; h++ { raw, err := os.ReadFile(filepath.Join("..", "testdata", fmt.Sprintf("regtest-block-%d.hex", h))) if err != nil { t.Skip("no blocks") } b, err := wire.ParseBlock(mustDecode(t, strings.TrimSpace(string(raw)))) if err != nil { t.Fatal(err) } lookup := utxo.Lookup(func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Find(set, op) }) ts, err := utxo.BlockTouches(b, h, lookup) if err != nil { t.Fatal(err) } touches = append(touches, ts...) set, err = utxo.ApplyRange(set, ts) if err != nil { t.Fatal(err) } } diff := utxo.Diff{From: 0, To: 106, Touches: touches} keepScratch = true defer func() { keepScratch = false }() if _, err := MergeRange(dir, 0, 106, diff, chainhash.Hash{}); err != nil { t.Fatalf("merge: %v", err) } // In memory: every creation, sorted by the package's own comparison. var inMemory []wire.OutPoint for _, tc := range touches { if !tc.Spent { inMemory = append(inMemory, tc.OutPoint) } } // The set's comparison, which is the one both sides must agree on. sortBySet(inMemory) // On disk: the key file the merge reads. raw, err := os.ReadFile(filepath.Join(dir, fmt.Sprintf("keys-%09d-%09d.created", diff.From, diff.To))) if err != nil { t.Skip("the merge removed its scratch files; the order is compared on a copy instead") } onDisk := make([]wire.OutPoint, 0, len(raw)/36) onDisk = readKeys(raw) t.Logf("%d creations in memory, %d on disk", len(inMemory), len(onDisk)) for i := 0; i < len(inMemory) && i < len(onDisk); i++ { if cmpOp(inMemory[i], onDisk[i]) != 0 { for j := i; j < i+3 && j < len(inMemory) && j < len(onDisk); j++ { // Printed from the end of the hash, which is where the two // outpoints above differ. ia, ib := reverse(inMemory[j].Hash), reverse(onDisk[j].Hash) t.Logf(" %d memory %x:%d | disk %x:%d", j, ia[:3], inMemory[j].Index, ib[:3], onDisk[j].Index) } t.Fatalf("the key file's order differs from the sort at %d", i) } } if len(inMemory) != len(onDisk) { t.Fatalf("%d in memory, %d on disk", len(inMemory), len(onDisk)) } } func readKeys(raw []byte) []wire.OutPoint { out := make([]wire.OutPoint, 0, len(raw)/36) for i := 0; i+36 <= len(raw); i += 36 { var op wire.OutPoint copy(op.Hash[:], raw[i:i+32]) op.Index = uint32(raw[i+32]) | uint32(raw[i+33])<<8 | uint32(raw[i+34])<<16 | uint32(raw[i+35])<<24 out = append(out, op) } return out }