package utxobuild_test import ( "encoding/binary" "fmt" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/btcd-lite/internal/wire" ) // txBytes builds one transaction: its inputs are given as outpoints and its // outputs as a script and a value. It is the only place the fixture encodes a // transaction, so a transaction parsed from these bytes is the transaction the // fixture meant. func txBytes(inputs []wire.OutPoint, outputs [][2]any) []byte { var tx []byte tx = binary.LittleEndian.AppendUint32(tx, 1) tx = binary.AppendUvarint(tx, uint64(len(inputs))) for _, in := range inputs { tx = append(tx, in.Hash[:]...) tx = binary.LittleEndian.AppendUint32(tx, in.Index) tx = append(tx, 0) // empty scriptSig tx = binary.LittleEndian.AppendUint32(tx, 0xffffffff) } tx = binary.AppendUvarint(tx, uint64(len(outputs))) for _, out := range outputs { tx = binary.LittleEndian.AppendUint64(tx, uint64(out[0].(int64))) script := out[1].([]byte) tx = binary.AppendUvarint(tx, uint64(len(script))) tx = append(tx, script...) } tx = binary.LittleEndian.AppendUint32(tx, 0) return tx } // chainOfBodies makes n bodies: each has one coinbase paying 50 BTC to a script // that names its height, and every fifth body after the tenth also has a // transaction spending the coinbase of the body five blocks before it, whose // one output pays 49.99999 BTC. // // The bytes are the source of truth: each body's transactions are encoded once, // parsed once, and the outpoint a later body spends is that same parse's txid. func chainOfBodies(n uint32) (bodies [][]byte, spends map[uint32]wire.OutPoint) { spends = map[uint32]wire.OutPoint{} coinbaseIDs := make([]chainhash.Hash, n) for h := uint32(0); h < n; h++ { script := []byte{0x51, byte(h)} cb := txBytes([]wire.OutPoint{{Index: 0xffffffff}}, [][2]any{{int64(5_000_000_000), script}}) tx, err := wire.ParseTx(cb) if err != nil { panic(fmt.Sprintf("body %d coinbase: %v", h, err)) } coinbaseIDs[h] = tx.TxID() txs := [][]byte{cb} if h >= 10 && h%5 == 0 { // Spend the coinbase of the body five blocks back. prev := wire.OutPoint{Hash: coinbaseIDs[h-5], Index: 0} spend := txBytes([]wire.OutPoint{prev}, [][2]any{{int64(4_999_999_000), []byte{0x52, byte(h)}}}) spends[h] = prev txs = append(txs, spend) } var ids []chainhash.Hash for _, raw := range txs { parsed, err := wire.ParseTx(raw) if err != nil { panic(err) } ids = append(ids, parsed.TxID()) } root := wire.MerkleRoot(ids) header := make([]byte, 80) binary.LittleEndian.PutUint32(header[0:4], 1) copy(header[36:68], root[:]) var body []byte body = append(body, header...) body = binary.AppendUvarint(body, uint64(len(txs))) for _, raw := range txs { body = append(body, raw...) } bodies = append(bodies, body) } return bodies, spends } // TestBodyChainHasSpends: the fixture spends what it claims to. A fixture whose // spend is not a spend cannot test a spend, and one round was lost to exactly // that. func TestBodyChainHasSpends(t *testing.T) { bodies, spends := chainOfBodies(25) if len(spends) == 0 { t.Fatal("the fixture spends nothing") } for h, prev := range spends { b, err := wire.ParseBlock(bodies[h]) if err != nil { t.Fatalf("body %d: %v", h, err) } if len(b.Txs) != 2 { t.Fatalf("body %d has %d transactions", h, len(b.Txs)) } if got := b.Txs[1].TxIn[0].PreviousOutPoint; got != prev { t.Fatalf("body %d spends %s, the fixture says %s", h, got, prev) } // The outpoint is the coinbase of the body five back. funder, err := wire.ParseBlock(bodies[h-5]) if err != nil { t.Fatal(err) } if prev.Hash != funder.Txs[0].TxID() { t.Fatalf("body %d spends %s, body %d's coinbase is %s", h, prev, h-5, funder.Txs[0].TxID()) } } t.Logf("%d bodies, spends at %d", len(bodies), len(spends)) }