package utxostore import ( "crypto/sha256" "encoding/binary" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/wire" ) func hcOp(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} } // TestNamesAgainstRawSHA is the measurement the merge question turns on: what // NameOf and WriteSet each claim a set is called, against double SHA-256 of // the bytes computed here with the standard library and nothing from this // package. func TestNamesAgainstRawSHA(t *testing.T) { set := []utxo.Entry{ {OutPoint: hcOp(5), Value: 5, Height: 0}, {OutPoint: hcOp(1), Value: 1, Height: 1}, {OutPoint: hcOp(9), Value: 9, Height: 2, Coinbase: true, ScriptHash: [8]byte{7}}, } raw := make([]byte, 0, len(set)*EntrySize) buf := make([]byte, EntrySize) for i := range set { set[i].Encode(buf) raw = append(raw, buf...) } first := sha256.Sum256(raw) second := sha256.Sum256(first[:]) var want chainhash.Hash copy(want[:], second[:]) if got := chainhash.DoubleHashH(raw); got != want { t.Fatalf("DoubleHashH of the bytes is %s, raw double SHA is %s", got, want) } if got := NameOf(set); got != want { t.Fatalf("NameOf says %s, raw double SHA of the encoded set is %s", got, want) } dir := t.TempDir() if got, err := WriteSet(dir, 3, set); err != nil { t.Fatalf("WriteSet: %v", err) } else if got != want { t.Fatalf("WriteSet says %s, raw double SHA is %s", got, want) } // The same set with its entries swapped must have another name: a name // that ignores order would make every reordering of a set the same set. swapped := []utxo.Entry{set[1], set[0], set[2]} if NameOf(swapped) == NameOf(set) { t.Fatal("two orderings of one set have one name") } // One field changed is another name. changed := append([]utxo.Entry(nil), set...) changed[2].Coinbase = false if NameOf(changed) == NameOf(set) { t.Fatal("a set and the same set with a changed field have one name") } }