fixture_test.go raw

   1  package utxobuild_test
   2  
   3  import (
   4  	"encoding/binary"
   5  	"fmt"
   6  	"testing"
   7  
   8  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
   9  
  10  	"git.smesh.lol/btcd-lite/internal/wire"
  11  )
  12  
  13  // txBytes builds one transaction: its inputs are given as outpoints and its
  14  // outputs as a script and a value. It is the only place the fixture encodes a
  15  // transaction, so a transaction parsed from these bytes is the transaction the
  16  // fixture meant.
  17  func txBytes(inputs []wire.OutPoint, outputs [][2]any) []byte {
  18  	var tx []byte
  19  	tx = binary.LittleEndian.AppendUint32(tx, 1)
  20  	tx = binary.AppendUvarint(tx, uint64(len(inputs)))
  21  	for _, in := range inputs {
  22  		tx = append(tx, in.Hash[:]...)
  23  		tx = binary.LittleEndian.AppendUint32(tx, in.Index)
  24  		tx = append(tx, 0) // empty scriptSig
  25  		tx = binary.LittleEndian.AppendUint32(tx, 0xffffffff)
  26  	}
  27  	tx = binary.AppendUvarint(tx, uint64(len(outputs)))
  28  	for _, out := range outputs {
  29  		tx = binary.LittleEndian.AppendUint64(tx, uint64(out[0].(int64)))
  30  		script := out[1].([]byte)
  31  		tx = binary.AppendUvarint(tx, uint64(len(script)))
  32  		tx = append(tx, script...)
  33  	}
  34  	tx = binary.LittleEndian.AppendUint32(tx, 0)
  35  	return tx
  36  }
  37  
  38  // chainOfBodies makes n bodies: each has one coinbase paying 50 BTC to a script
  39  // that names its height, and every fifth body after the tenth also has a
  40  // transaction spending the coinbase of the body five blocks before it, whose
  41  // one output pays 49.99999 BTC.
  42  //
  43  // The bytes are the source of truth: each body's transactions are encoded once,
  44  // parsed once, and the outpoint a later body spends is that same parse's txid.
  45  func chainOfBodies(n uint32) (bodies [][]byte, spends map[uint32]wire.OutPoint) {
  46  	spends = map[uint32]wire.OutPoint{}
  47  	coinbaseIDs := make([]chainhash.Hash, n)
  48  	for h := uint32(0); h < n; h++ {
  49  		script := []byte{0x51, byte(h)}
  50  		cb := txBytes([]wire.OutPoint{{Index: 0xffffffff}}, [][2]any{{int64(5_000_000_000), script}})
  51  		tx, err := wire.ParseTx(cb)
  52  		if err != nil {
  53  			panic(fmt.Sprintf("body %d coinbase: %v", h, err))
  54  		}
  55  		coinbaseIDs[h] = tx.TxID()
  56  		txs := [][]byte{cb}
  57  		if h >= 10 && h%5 == 0 {
  58  			// Spend the coinbase of the body five blocks back.
  59  			prev := wire.OutPoint{Hash: coinbaseIDs[h-5], Index: 0}
  60  			spend := txBytes([]wire.OutPoint{prev}, [][2]any{{int64(4_999_999_000), []byte{0x52, byte(h)}}})
  61  			spends[h] = prev
  62  			txs = append(txs, spend)
  63  		}
  64  		var ids []chainhash.Hash
  65  		for _, raw := range txs {
  66  			parsed, err := wire.ParseTx(raw)
  67  			if err != nil {
  68  				panic(err)
  69  			}
  70  			ids = append(ids, parsed.TxID())
  71  		}
  72  		root := wire.MerkleRoot(ids)
  73  		header := make([]byte, 80)
  74  		binary.LittleEndian.PutUint32(header[0:4], 1)
  75  		copy(header[36:68], root[:])
  76  		var body []byte
  77  		body = append(body, header...)
  78  		body = binary.AppendUvarint(body, uint64(len(txs)))
  79  		for _, raw := range txs {
  80  			body = append(body, raw...)
  81  		}
  82  		bodies = append(bodies, body)
  83  	}
  84  	return bodies, spends
  85  }
  86  
  87  // TestBodyChainHasSpends: the fixture spends what it claims to. A fixture whose
  88  // spend is not a spend cannot test a spend, and one round was lost to exactly
  89  // that.
  90  func TestBodyChainHasSpends(t *testing.T) {
  91  	bodies, spends := chainOfBodies(25)
  92  	if len(spends) == 0 {
  93  		t.Fatal("the fixture spends nothing")
  94  	}
  95  	for h, prev := range spends {
  96  		b, err := wire.ParseBlock(bodies[h])
  97  		if err != nil {
  98  			t.Fatalf("body %d: %v", h, err)
  99  		}
 100  		if len(b.Txs) != 2 {
 101  			t.Fatalf("body %d has %d transactions", h, len(b.Txs))
 102  		}
 103  		if got := b.Txs[1].TxIn[0].PreviousOutPoint; got != prev {
 104  			t.Fatalf("body %d spends %s, the fixture says %s", h, got, prev)
 105  		}
 106  		// The outpoint is the coinbase of the body five back.
 107  		funder, err := wire.ParseBlock(bodies[h-5])
 108  		if err != nil {
 109  			t.Fatal(err)
 110  		}
 111  		if prev.Hash != funder.Txs[0].TxID() {
 112  			t.Fatalf("body %d spends %s, body %d's coinbase is %s", h, prev, h-5, funder.Txs[0].TxID())
 113  		}
 114  	}
 115  	t.Logf("%d bodies, spends at %d", len(bodies), len(spends))
 116  }
 117