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" ) // testOp is an outpoint whose key is its id in the first four bytes, which is // what a test needs to tell one key from another in a failure message. func testOp(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} } // TestTwoRounds: two ranges of creations only, merged one after the other, // each reproducing the replay's set name. It is the case the registration // fixture exercised and the one the earlier streaming test missed, because // that test created and spent within one range. func TestTwoRounds(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 prev := uint32(0) for round := 0; round < 2; round++ { var ts []utxo.Touch for h := 0; h < 10; h++ { var block []utxo.Touch for k := 0; k < 5; k++ { id := uint32(round*50 + h*5 + k) e := utxo.Entry{OutPoint: testOp(id), Value: int64(id), Height: uint32(h)} block = append(block, utxo.Touch{OutPoint: e.OutPoint, Height: uint32(h), New: e}) } ts = append(ts, block...) var err error set, err = utxo.ApplyRange(set, block) if err != nil { t.Fatal(err) } } to := prev + 10 if _, err := CutRange(dir, prev, to, ts); err != nil { t.Fatal(err) } d, err := LoadDiff(dir, prev, to) if err != nil { t.Fatal(err) } got, err := MergeRange(dir, prev, to, d, NameOf(set)) if err != nil { t.Fatalf("round %d: %v", round, err) } if got != NameOf(set) { t.Fatalf("round %d: merge gives %s, replay gives %s", round, got, NameOf(set)) } prev = to } _ = chainhash.Hash{} }