block_test.go raw
1 package utxo_test
2
3 import (
4 "crypto/sha256"
5 "testing"
6
7 "git.smesh.lol/btcd-lite/internal/utxo"
8 "git.smesh.lol/btcd-lite/internal/wire"
9 )
10
11 func txid(b byte) [32]byte {
12 return sha256.Sum256([]byte{b})
13 }
14
15 // mkTx builds a transaction with the given prevout ids and output values.
16 func mkTx(txidBytes byte, spends []uint32, values ...int64) wire.Tx {
17 t := wire.Tx{Version: 1}
18 for _, id := range spends {
19 t.TxIn = append(t.TxIn, wire.TxIn{
20 PreviousOutPoint: wire.OutPoint{Hash: txid(byte(id & 0xff)), Index: id >> 8},
21 Sequence: 0xffffffff,
22 })
23 }
24 for _, v := range values {
25 t.TxOut = append(t.TxOut, wire.TxOut{Value: v, PkScript: []byte{0x51, txidBytes}})
26 }
27 return t
28 }
29
30 func coinbaseTx(value int64) wire.Tx {
31 t := wire.Tx{Version: 1, TxIn: []wire.TxIn{{PreviousOutPoint: wire.OutPoint{Index: 0xffffffff}, SignatureScript: []byte{1, 0x50}}}}
32 t.TxOut = append(t.TxOut, wire.TxOut{Value: value, PkScript: []byte{0x51}})
33 return t
34 }
35
36 // TestBlockTouches: a block that spends a coin spends it, creates its outputs,
37 // and a coinbase input is not a spend.
38 func TestBlockTouches(t *testing.T) {
39 cb := coinbaseTx(50)
40 spend := mkTx(2, []uint32{0}, 30, 20)
41 spend.TxIn[0].PreviousOutPoint = wire.OutPoint{Hash: txid(9), Index: 7}
42 b := &wire.Block{Txs: []wire.Tx{cb, spend}}
43
44 held := map[wire.OutPoint]bool{{Hash: txid(9), Index: 7}: true}
45 lookup := func(op wire.OutPoint) (utxo.Entry, bool) {
46 return utxo.Entry{OutPoint: op}, held[op]
47 }
48 touches, err := utxo.BlockTouches(b, 100, lookup)
49 if err != nil {
50 t.Fatal(err)
51 }
52 spends, creates := 0, 0
53 for _, tt := range touches {
54 if tt.Spent {
55 spends++
56 if tt.OutPoint != (wire.OutPoint{Hash: txid(9), Index: 7}) {
57 t.Fatalf("unexpected spend %s", tt.OutPoint)
58 }
59 } else {
60 creates++
61 if tt.New.Height != 100 || tt.New.ScriptHash == [8]byte{} {
62 t.Fatalf("creation record incomplete: %+v", tt.New)
63 }
64 }
65 }
66 if spends != 1 || creates != 3 {
67 t.Fatalf("spends %d creates %d, want 1 and 3", spends, creates)
68 }
69 // The coinbase output is marked, the others are not.
70 for _, tt := range touches {
71 if tt.Spent {
72 continue
73 }
74 isCoinbaseOutput := tt.New.OutPoint.Hash == sumOf(cb)
75 if tt.New.Coinbase != isCoinbaseOutput {
76 t.Fatalf("coinbase flag %v for %s", tt.New.Coinbase, tt.New.OutPoint)
77 }
78 }
79 }
80
81 func sumOf(t wire.Tx) [32]byte {
82 return t.TxID()
83 }
84
85 // TestBlockTouchesRefusesUnknownSpend: a spend the lookup does not hold fails,
86 // because a block that spends a coin the chain does not have cannot be applied.
87 func TestBlockTouchesRefusesUnknownSpend(t *testing.T) {
88 cb := coinbaseTx(50)
89 spend := mkTx(2, nil, 10)
90 spend.TxIn = append(spend.TxIn, wire.TxIn{PreviousOutPoint: wire.OutPoint{Hash: txid(1), Index: 0}})
91 b := &wire.Block{Txs: []wire.Tx{cb, spend}}
92 _, err := utxo.BlockTouches(b, 1, func(wire.OutPoint) (utxo.Entry, bool) { return utxo.Entry{}, false })
93 if err == nil {
94 t.Fatal("a block spending an unknown outpoint produced touches")
95 }
96 // With a nil lookup it does not ask: the plot replays from its own start.
97 if _, err := utxo.BlockTouches(b, 1, nil); err != nil {
98 t.Fatalf("nil lookup refused: %v", err)
99 }
100 }
101
102 // TestBlockTouchesIntraBlock: an output created and spent in one block is
103 // neither created nor spent from the set's point of view.
104 func TestBlockTouchesIntraBlock(t *testing.T) {
105 cb := coinbaseTx(50)
106 a := mkTx(1, nil, 40)
107 a.TxIn = append(a.TxIn, wire.TxIn{PreviousOutPoint: wire.OutPoint{Hash: txid(5), Index: 0}})
108 held := map[wire.OutPoint]bool{{Hash: txid(5), Index: 0}: true}
109 bTx := mkTx(2, nil, 39)
110 bTx.TxIn = append(bTx.TxIn, wire.TxIn{PreviousOutPoint: wire.OutPoint{Hash: a.TxID(), Index: 0}})
111 b := &wire.Block{Txs: []wire.Tx{cb, a, bTx}}
112 touches, err := utxo.BlockTouches(b, 2, func(op wire.OutPoint) (utxo.Entry, bool) {
113 return utxo.Entry{OutPoint: op}, held[op]
114 })
115 if err != nil {
116 t.Fatal(err)
117 }
118 intra := wire.OutPoint{Hash: a.TxID(), Index: 0}
119 for _, tt := range touches {
120 if tt.OutPoint == intra {
121 t.Fatalf("intra-block outpoint reported as %s", map[bool]string{true: "a spend", false: "a creation"}[tt.Spent])
122 }
123 }
124 // The set walk sees a creation from a, a spend of it by b and the coinbase
125 // and b's outputs: applying the touches to a set holding txid(5):0 leaves
126 // exactly (1 creation from cb, b's output), because a's 40 was spent inside
127 // the block.
128 start := []utxo.Entry{{OutPoint: wire.OutPoint{Hash: txid(5), Index: 0}, Value: 1}}
129 end, err := utxo.ApplyRange(start, touches)
130 if err != nil {
131 t.Fatal(err)
132 }
133 // cb output + b's output; txid(5):0 is gone; a's output never existed in the set.
134 if len(end) != 2 {
135 t.Fatalf("set after the block has %d entries, want 2", len(end))
136 }
137 }
138