package utxo_test import ( "crypto/sha256" "testing" "git.smesh.lol/btcd-lite/internal/utxo" "git.smesh.lol/btcd-lite/internal/wire" ) func txid(b byte) [32]byte { return sha256.Sum256([]byte{b}) } // mkTx builds a transaction with the given prevout ids and output values. func mkTx(txidBytes byte, spends []uint32, values ...int64) wire.Tx { t := wire.Tx{Version: 1} for _, id := range spends { t.TxIn = append(t.TxIn, wire.TxIn{ PreviousOutPoint: wire.OutPoint{Hash: txid(byte(id & 0xff)), Index: id >> 8}, Sequence: 0xffffffff, }) } for _, v := range values { t.TxOut = append(t.TxOut, wire.TxOut{Value: v, PkScript: []byte{0x51, txidBytes}}) } return t } func coinbaseTx(value int64) wire.Tx { t := wire.Tx{Version: 1, TxIn: []wire.TxIn{{PreviousOutPoint: wire.OutPoint{Index: 0xffffffff}, SignatureScript: []byte{1, 0x50}}}} t.TxOut = append(t.TxOut, wire.TxOut{Value: value, PkScript: []byte{0x51}}) return t } // TestBlockTouches: a block that spends a coin spends it, creates its outputs, // and a coinbase input is not a spend. func TestBlockTouches(t *testing.T) { cb := coinbaseTx(50) spend := mkTx(2, []uint32{0}, 30, 20) spend.TxIn[0].PreviousOutPoint = wire.OutPoint{Hash: txid(9), Index: 7} b := &wire.Block{Txs: []wire.Tx{cb, spend}} held := map[wire.OutPoint]bool{{Hash: txid(9), Index: 7}: true} lookup := func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Entry{OutPoint: op}, held[op] } touches, err := utxo.BlockTouches(b, 100, lookup) if err != nil { t.Fatal(err) } spends, creates := 0, 0 for _, tt := range touches { if tt.Spent { spends++ if tt.OutPoint != (wire.OutPoint{Hash: txid(9), Index: 7}) { t.Fatalf("unexpected spend %s", tt.OutPoint) } } else { creates++ if tt.New.Height != 100 || tt.New.ScriptHash == [8]byte{} { t.Fatalf("creation record incomplete: %+v", tt.New) } } } if spends != 1 || creates != 3 { t.Fatalf("spends %d creates %d, want 1 and 3", spends, creates) } // The coinbase output is marked, the others are not. for _, tt := range touches { if tt.Spent { continue } isCoinbaseOutput := tt.New.OutPoint.Hash == sumOf(cb) if tt.New.Coinbase != isCoinbaseOutput { t.Fatalf("coinbase flag %v for %s", tt.New.Coinbase, tt.New.OutPoint) } } } func sumOf(t wire.Tx) [32]byte { return t.TxID() } // TestBlockTouchesRefusesUnknownSpend: a spend the lookup does not hold fails, // because a block that spends a coin the chain does not have cannot be applied. func TestBlockTouchesRefusesUnknownSpend(t *testing.T) { cb := coinbaseTx(50) spend := mkTx(2, nil, 10) spend.TxIn = append(spend.TxIn, wire.TxIn{PreviousOutPoint: wire.OutPoint{Hash: txid(1), Index: 0}}) b := &wire.Block{Txs: []wire.Tx{cb, spend}} _, err := utxo.BlockTouches(b, 1, func(wire.OutPoint) (utxo.Entry, bool) { return utxo.Entry{}, false }) if err == nil { t.Fatal("a block spending an unknown outpoint produced touches") } // With a nil lookup it does not ask: the plot replays from its own start. if _, err := utxo.BlockTouches(b, 1, nil); err != nil { t.Fatalf("nil lookup refused: %v", err) } } // TestBlockTouchesIntraBlock: an output created and spent in one block is // neither created nor spent from the set's point of view. func TestBlockTouchesIntraBlock(t *testing.T) { cb := coinbaseTx(50) a := mkTx(1, nil, 40) a.TxIn = append(a.TxIn, wire.TxIn{PreviousOutPoint: wire.OutPoint{Hash: txid(5), Index: 0}}) held := map[wire.OutPoint]bool{{Hash: txid(5), Index: 0}: true} bTx := mkTx(2, nil, 39) bTx.TxIn = append(bTx.TxIn, wire.TxIn{PreviousOutPoint: wire.OutPoint{Hash: a.TxID(), Index: 0}}) b := &wire.Block{Txs: []wire.Tx{cb, a, bTx}} touches, err := utxo.BlockTouches(b, 2, func(op wire.OutPoint) (utxo.Entry, bool) { return utxo.Entry{OutPoint: op}, held[op] }) if err != nil { t.Fatal(err) } intra := wire.OutPoint{Hash: a.TxID(), Index: 0} for _, tt := range touches { if tt.OutPoint == intra { t.Fatalf("intra-block outpoint reported as %s", map[bool]string{true: "a spend", false: "a creation"}[tt.Spent]) } } // The set walk sees a creation from a, a spend of it by b and the coinbase // and b's outputs: applying the touches to a set holding txid(5):0 leaves // exactly (1 creation from cb, b's output), because a's 40 was spent inside // the block. start := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: txid(5), Index: 0}, Value: 1}} end, err := utxo.ApplyRange(start, touches) if err != nil { t.Fatal(err) } // cb output + b's output; txid(5):0 is gone; a's output never existed in the set. if len(end) != 2 { t.Fatalf("set after the block has %d entries, want 2", len(end)) } }