msgtx_test.go raw
1 // Copyright (c) 2013-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4
5 package wire
6
7 import (
8 "bytes"
9 "errors"
10 "fmt"
11 "io"
12 "reflect"
13 "testing"
14
15 "github.com/btcsuite/btcd/chainhash/v2"
16 "github.com/davecgh/go-spew/spew"
17 "github.com/stretchr/testify/require"
18 )
19
20 // TestTx tests the MsgTx API.
21 func TestTx(t *testing.T) {
22 pver := ProtocolVersion
23
24 // Block 100000 hash.
25 hashStr := "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
26 hash, err := chainhash.NewHashFromStr(hashStr)
27 if err != nil {
28 t.Errorf("NewHashFromStr: %v", err)
29 }
30
31 // Ensure the command is expected value.
32 wantCmd := "tx"
33 msg := NewMsgTx(1)
34 if cmd := msg.Command(); cmd != wantCmd {
35 t.Errorf("NewMsgAddr: wrong command - got %v want %v",
36 cmd, wantCmd)
37 }
38
39 // Ensure max payload is expected value for latest protocol version.
40 wantPayload := uint32(1000 * 4000)
41 maxPayload := msg.MaxPayloadLength(pver)
42 if maxPayload != wantPayload {
43 t.Errorf("MaxPayloadLength: wrong max payload length for "+
44 "protocol version %d - got %v, want %v", pver,
45 maxPayload, wantPayload)
46 }
47
48 // Ensure we get the same transaction output point data back out.
49 // NOTE: This is a block hash and made up index, but we're only
50 // testing package functionality.
51 prevOutIndex := uint32(1)
52 prevOut := NewOutPoint(hash, prevOutIndex)
53 if !prevOut.Hash.IsEqual(hash) {
54 t.Errorf("NewOutPoint: wrong hash - got %v, want %v",
55 spew.Sprint(&prevOut.Hash), spew.Sprint(hash))
56 }
57 if prevOut.Index != prevOutIndex {
58 t.Errorf("NewOutPoint: wrong index - got %v, want %v",
59 prevOut.Index, prevOutIndex)
60 }
61 prevOutStr := fmt.Sprintf("%s:%d", hash.String(), prevOutIndex)
62 if s := prevOut.String(); s != prevOutStr {
63 t.Errorf("OutPoint.String: unexpected result - got %v, "+
64 "want %v", s, prevOutStr)
65 }
66
67 // Ensure we get the same transaction input back out.
68 sigScript := []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62}
69 witnessData := [][]byte{
70 {0x04, 0x31},
71 {0x01, 0x43},
72 }
73 txIn := NewTxIn(prevOut, sigScript, witnessData)
74 if !reflect.DeepEqual(&txIn.PreviousOutPoint, prevOut) {
75 t.Errorf("NewTxIn: wrong prev outpoint - got %v, want %v",
76 spew.Sprint(&txIn.PreviousOutPoint),
77 spew.Sprint(prevOut))
78 }
79 if !bytes.Equal(txIn.SignatureScript, sigScript) {
80 t.Errorf("NewTxIn: wrong signature script - got %v, want %v",
81 spew.Sdump(txIn.SignatureScript),
82 spew.Sdump(sigScript))
83 }
84 if !reflect.DeepEqual(txIn.Witness, TxWitness(witnessData)) {
85 t.Errorf("NewTxIn: wrong witness data - got %v, want %v",
86 spew.Sdump(txIn.Witness),
87 spew.Sdump(witnessData))
88 }
89
90 // Ensure we get the same transaction output back out.
91 txValue := int64(5000000000)
92 pkScript := []byte{
93 0x41, // OP_DATA_65
94 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
95 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
96 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
97 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
98 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
99 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
100 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
101 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
102 0xa6, // 65-byte signature
103 0xac, // OP_CHECKSIG
104 }
105 txOut := NewTxOut(txValue, pkScript)
106 if txOut.Value != txValue {
107 t.Errorf("NewTxOut: wrong pk script - got %v, want %v",
108 txOut.Value, txValue)
109
110 }
111 if !bytes.Equal(txOut.PkScript, pkScript) {
112 t.Errorf("NewTxOut: wrong pk script - got %v, want %v",
113 spew.Sdump(txOut.PkScript),
114 spew.Sdump(pkScript))
115 }
116
117 // Ensure transaction inputs are added properly.
118 msg.AddTxIn(txIn)
119 if !reflect.DeepEqual(msg.TxIn[0], txIn) {
120 t.Errorf("AddTxIn: wrong transaction input added - got %v, want %v",
121 spew.Sprint(msg.TxIn[0]), spew.Sprint(txIn))
122 }
123
124 // Ensure transaction outputs are added properly.
125 msg.AddTxOut(txOut)
126 if !reflect.DeepEqual(msg.TxOut[0], txOut) {
127 t.Errorf("AddTxIn: wrong transaction output added - got %v, want %v",
128 spew.Sprint(msg.TxOut[0]), spew.Sprint(txOut))
129 }
130
131 // Ensure the copy produced an identical transaction message.
132 newMsg := msg.Copy()
133 if !reflect.DeepEqual(newMsg, msg) {
134 t.Errorf("Copy: mismatched tx messages - got %v, want %v",
135 spew.Sdump(newMsg), spew.Sdump(msg))
136 }
137 }
138
139 // TestTxHash tests the ability to generate the hash of a transaction accurately.
140 func TestTxHash(t *testing.T) {
141 // Hash of first transaction from block 113875.
142 hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86"
143 wantHash, err := chainhash.NewHashFromStr(hashStr)
144 if err != nil {
145 t.Errorf("NewHashFromStr: %v", err)
146 return
147 }
148
149 // First transaction from block 113875.
150 msgTx := NewMsgTx(1)
151 txIn := TxIn{
152 PreviousOutPoint: OutPoint{
153 Hash: chainhash.Hash{},
154 Index: 0xffffffff,
155 },
156 SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62},
157 Sequence: 0xffffffff,
158 }
159 txOut := TxOut{
160 Value: 5000000000,
161 PkScript: []byte{
162 0x41, // OP_DATA_65
163 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
164 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
165 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
166 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
167 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
168 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
169 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
170 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
171 0xa6, // 65-byte signature
172 0xac, // OP_CHECKSIG
173 },
174 }
175 msgTx.AddTxIn(&txIn)
176 msgTx.AddTxOut(&txOut)
177 msgTx.LockTime = 0
178
179 // Ensure the hash produced is expected.
180 txHash := msgTx.TxHash()
181 if !txHash.IsEqual(wantHash) {
182 t.Errorf("TxHash: wrong hash - got %v, want %v",
183 spew.Sprint(txHash), spew.Sprint(wantHash))
184 }
185 }
186
187 // TestTxSha tests the ability to generate the wtxid, and txid of a transaction
188 // with witness inputs accurately.
189 func TestWTxSha(t *testing.T) {
190 hashStrTxid := "0f167d1385a84d1518cfee208b653fc9163b605ccf1b75347e2850b3e2eb19f3"
191 wantHashTxid, err := chainhash.NewHashFromStr(hashStrTxid)
192 if err != nil {
193 t.Errorf("NewShaHashFromStr: %v", err)
194 return
195 }
196 hashStrWTxid := "0858eab78e77b6b033da30f46699996396cf48fcf625a783c85a51403e175e74"
197 wantHashWTxid, err := chainhash.NewHashFromStr(hashStrWTxid)
198 if err != nil {
199 t.Errorf("NewShaHashFromStr: %v", err)
200 return
201 }
202
203 // From block 23157 in a past version of segnet.
204 msgTx := NewMsgTx(1)
205 txIn := TxIn{
206 PreviousOutPoint: OutPoint{
207 Hash: chainhash.Hash{
208 0xa5, 0x33, 0x52, 0xd5, 0x13, 0x57, 0x66, 0xf0,
209 0x30, 0x76, 0x59, 0x74, 0x18, 0x26, 0x3d, 0xa2,
210 0xd9, 0xc9, 0x58, 0x31, 0x59, 0x68, 0xfe, 0xa8,
211 0x23, 0x52, 0x94, 0x67, 0x48, 0x1f, 0xf9, 0xcd,
212 },
213 Index: 19,
214 },
215 Witness: [][]byte{
216 { // 70-byte signature
217 0x30, 0x43, 0x02, 0x1f, 0x4d, 0x23, 0x81, 0xdc,
218 0x97, 0xf1, 0x82, 0xab, 0xd8, 0x18, 0x5f, 0x51,
219 0x75, 0x30, 0x18, 0x52, 0x32, 0x12, 0xf5, 0xdd,
220 0xc0, 0x7c, 0xc4, 0xe6, 0x3a, 0x8d, 0xc0, 0x36,
221 0x58, 0xda, 0x19, 0x02, 0x20, 0x60, 0x8b, 0x5c,
222 0x4d, 0x92, 0xb8, 0x6b, 0x6d, 0xe7, 0xd7, 0x8e,
223 0xf2, 0x3a, 0x2f, 0xa7, 0x35, 0xbc, 0xb5, 0x9b,
224 0x91, 0x4a, 0x48, 0xb0, 0xe1, 0x87, 0xc5, 0xe7,
225 0x56, 0x9a, 0x18, 0x19, 0x70, 0x01,
226 },
227 { // 33-byte serialize pub key
228 0x03, 0x07, 0xea, 0xd0, 0x84, 0x80, 0x7e, 0xb7,
229 0x63, 0x46, 0xdf, 0x69, 0x77, 0x00, 0x0c, 0x89,
230 0x39, 0x2f, 0x45, 0xc7, 0x64, 0x25, 0xb2, 0x61,
231 0x81, 0xf5, 0x21, 0xd7, 0xf3, 0x70, 0x06, 0x6a,
232 0x8f,
233 },
234 },
235 Sequence: 0xffffffff,
236 }
237 txOut := TxOut{
238 Value: 395019,
239 PkScript: []byte{
240 0x00, // Version 0 witness program
241 0x14, // OP_DATA_20
242 0x9d, 0xda, 0xc6, 0xf3, 0x9d, 0x51, 0xe0, 0x39,
243 0x8e, 0x53, 0x2a, 0x22, 0xc4, 0x1b, 0xa1, 0x89,
244 0x40, 0x6a, 0x85, 0x23, // 20-byte pub key hash
245 },
246 }
247 msgTx.AddTxIn(&txIn)
248 msgTx.AddTxOut(&txOut)
249 msgTx.LockTime = 0
250
251 // Ensure the correct txid, and wtxid is produced as expected.
252 txid := msgTx.TxHash()
253 if !txid.IsEqual(wantHashTxid) {
254 t.Errorf("TxSha: wrong hash - got %v, want %v",
255 spew.Sprint(txid), spew.Sprint(wantHashTxid))
256 }
257 wtxid := msgTx.WitnessHash()
258 if !wtxid.IsEqual(wantHashWTxid) {
259 t.Errorf("WTxSha: wrong hash - got %v, want %v",
260 spew.Sprint(wtxid), spew.Sprint(wantHashWTxid))
261 }
262 }
263
264 // TestTxWire tests the MsgTx wire encode and decode for various numbers
265 // of transaction inputs and outputs and protocol versions.
266 func TestTxWire(t *testing.T) {
267 // Empty tx message.
268 noTx := NewMsgTx(1)
269 noTx.Version = 1
270 noTxEncoded := []byte{
271 0x01, 0x00, 0x00, 0x00, // Version
272 0x00, // Varint for number of input transactions
273 0x00, // Varint for number of output transactions
274 0x00, 0x00, 0x00, 0x00, // Lock time
275 }
276
277 tests := []struct {
278 in *MsgTx // Message to encode
279 out *MsgTx // Expected decoded message
280 buf []byte // Wire encoding
281 pver uint32 // Protocol version for wire encoding
282 enc MessageEncoding // Message encoding format
283 }{
284 // Latest protocol version with no transactions.
285 {
286 noTx,
287 noTx, noTxEncoded,
288 ProtocolVersion,
289 BaseEncoding,
290 },
291
292 // Latest protocol version with multiple transactions.
293 {
294 multiTx,
295 multiTx,
296 multiTxEncoded,
297 ProtocolVersion,
298 BaseEncoding,
299 },
300
301 // Protocol version BIP0035Version with no transactions.
302 {
303 noTx,
304 noTx,
305 noTxEncoded,
306 BIP0035Version,
307 BaseEncoding,
308 },
309
310 // Protocol version BIP0035Version with multiple transactions.
311 {
312 multiTx,
313 multiTx,
314 multiTxEncoded,
315 BIP0035Version,
316 BaseEncoding,
317 },
318
319 // Protocol version BIP0031Version with no transactions.
320 {
321 noTx,
322 noTx,
323 noTxEncoded,
324 BIP0031Version,
325 BaseEncoding,
326 },
327
328 // Protocol version BIP0031Version with multiple transactions.
329 {
330 multiTx,
331 multiTx,
332 multiTxEncoded,
333 BIP0031Version,
334 BaseEncoding,
335 },
336
337 // Protocol version NetAddressTimeVersion with no transactions.
338 {
339 noTx,
340 noTx,
341 noTxEncoded,
342 NetAddressTimeVersion,
343 BaseEncoding,
344 },
345
346 // Protocol version NetAddressTimeVersion with multiple transactions.
347 {
348 multiTx,
349 multiTx,
350 multiTxEncoded,
351 NetAddressTimeVersion,
352 BaseEncoding,
353 },
354
355 // Protocol version MultipleAddressVersion with no transactions.
356 {
357 noTx,
358 noTx,
359 noTxEncoded,
360 MultipleAddressVersion,
361 BaseEncoding,
362 },
363
364 // Protocol version MultipleAddressVersion with multiple transactions.
365 {
366 multiTx,
367 multiTx,
368 multiTxEncoded,
369 MultipleAddressVersion,
370 BaseEncoding,
371 },
372 }
373
374 t.Logf("Running %d tests", len(tests))
375 for i, test := range tests {
376 // Encode the message to wire format.
377 var buf bytes.Buffer
378 err := test.in.BtcEncode(&buf, test.pver, test.enc)
379 if err != nil {
380 t.Errorf("BtcEncode #%d error %v", i, err)
381 continue
382 }
383 if !bytes.Equal(buf.Bytes(), test.buf) {
384 t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
385 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
386 continue
387 }
388
389 // Decode the message from wire format.
390 var msg MsgTx
391 rbuf := bytes.NewReader(test.buf)
392 err = msg.BtcDecode(rbuf, test.pver, test.enc)
393 if err != nil {
394 t.Errorf("BtcDecode #%d error %v", i, err)
395 continue
396 }
397 if !reflect.DeepEqual(&msg, test.out) {
398 t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
399 spew.Sdump(&msg), spew.Sdump(test.out))
400 continue
401 }
402 }
403 }
404
405 // TestTxDecodeErrorClearsScripts checks that failed public decodes drop all
406 // script references and allow a later successful decode into the same receiver.
407 func TestTxDecodeErrorClearsScripts(t *testing.T) {
408 for _, enc := range []MessageEncoding{BaseEncoding, WitnessEncoding} {
409 for _, buffered := range []bool{false, true} {
410 name := fmt.Sprintf("encoding=%d/buffered=%t", enc, buffered)
411 t.Run(name, func(t *testing.T) {
412 tx := NewMsgTx(2)
413 var witness TxWitness
414 if enc == WitnessEncoding {
415 witness = TxWitness{{0x01, 0x02}, {0x03}}
416 }
417 tx.AddTxIn(NewTxIn(
418 &OutPoint{}, []byte{0x04, 0x05}, witness,
419 ))
420 tx.AddTxOut(NewTxOut(1, []byte{0x06, 0x07}))
421 tx.LockTime = 123
422
423 var serialized bytes.Buffer
424 require.NoError(t, tx.BtcEncode(&serialized, 0, enc))
425 data := serialized.Bytes()
426 reader := func(data []byte) io.Reader {
427 r := bytes.NewReader(data)
428 if buffered {
429 return r
430 }
431 return struct{ io.Reader }{r}
432 }
433
434 // Include early errors on a populated receiver as well
435 // as errors after scripts and witnesses have been staged.
436 for end := 0; end < len(data); end++ {
437 decoded := tx.Copy()
438 err := decoded.BtcDecode(reader(data[:end]), 0, enc)
439 require.Error(t, err, "prefix length %d", end)
440 require.Nil(t, decoded.TxIn, "prefix length %d", end)
441 require.Nil(t, decoded.TxOut, "prefix length %d", end)
442
443 require.NoError(t, decoded.BtcDecode(reader(data), 0, enc))
444 require.Equal(t, tx, decoded)
445 }
446 })
447 }
448 }
449 }
450
451 // TestTxWireErrors performs negative tests against wire encode and decode
452 // of MsgTx to confirm error paths work correctly.
453 func TestTxWireErrors(t *testing.T) {
454 // Use protocol version 60002 specifically here instead of the latest
455 // because the test data is using bytes encoded with that protocol
456 // version.
457 pver := uint32(60002)
458
459 tests := []struct {
460 in *MsgTx // Value to encode
461 buf []byte // Wire encoding
462 pver uint32 // Protocol version for wire encoding
463 enc MessageEncoding // Message encoding format
464 max int // Max size of fixed buffer to induce errors
465 writeErr error // Expected write error
466 readErr error // Expected read error
467 }{
468 // Force error in version.
469 {multiTx, multiTxEncoded, pver, BaseEncoding, 0, io.ErrShortWrite, io.EOF},
470 // Force error in number of transaction inputs.
471 {multiTx, multiTxEncoded, pver, BaseEncoding, 4, io.ErrShortWrite, io.EOF},
472 // Force error in transaction input previous block hash.
473 {multiTx, multiTxEncoded, pver, BaseEncoding, 5, io.ErrShortWrite, io.EOF},
474 // Force error in transaction input previous block output index.
475 {multiTx, multiTxEncoded, pver, BaseEncoding, 37, io.ErrShortWrite, io.EOF},
476 // Force error in transaction input signature script length.
477 {multiTx, multiTxEncoded, pver, BaseEncoding, 41, io.ErrShortWrite, io.EOF},
478 // Force error in transaction input signature script.
479 {multiTx, multiTxEncoded, pver, BaseEncoding, 42, io.ErrShortWrite, io.EOF},
480 // Force error in transaction input sequence.
481 {multiTx, multiTxEncoded, pver, BaseEncoding, 49, io.ErrShortWrite, io.EOF},
482 // Force error in number of transaction outputs.
483 {multiTx, multiTxEncoded, pver, BaseEncoding, 53, io.ErrShortWrite, io.EOF},
484 // Force error in transaction output value.
485 {multiTx, multiTxEncoded, pver, BaseEncoding, 54, io.ErrShortWrite, io.EOF},
486 // Force error in transaction output pk script length.
487 {multiTx, multiTxEncoded, pver, BaseEncoding, 62, io.ErrShortWrite, io.EOF},
488 // Force error in transaction output pk script.
489 {multiTx, multiTxEncoded, pver, BaseEncoding, 63, io.ErrShortWrite, io.EOF},
490 // Force error in transaction output lock time.
491 {multiTx, multiTxEncoded, pver, BaseEncoding, 206, io.ErrShortWrite, io.EOF},
492 }
493
494 t.Logf("Running %d tests", len(tests))
495 for i, test := range tests {
496 // Encode to wire format.
497 w := newFixedWriter(test.max)
498 err := test.in.BtcEncode(w, test.pver, test.enc)
499 if err != test.writeErr {
500 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
501 i, err, test.writeErr)
502 continue
503 }
504
505 // Decode from wire format.
506 var msg MsgTx
507 r := newFixedReader(test.max, test.buf)
508 err = msg.BtcDecode(r, test.pver, test.enc)
509 if err != test.readErr {
510 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
511 i, err, test.readErr)
512 continue
513 }
514 }
515 }
516
517 // TestTxSerialize tests MsgTx serialize and deserialize.
518 func TestTxSerialize(t *testing.T) {
519 noTx := NewMsgTx(1)
520 noTx.Version = 1
521 noTxEncoded := []byte{
522 0x01, 0x00, 0x00, 0x00, // Version
523 0x00, // Varint for number of input transactions
524 0x00, // Varint for number of output transactions
525 0x00, 0x00, 0x00, 0x00, // Lock time
526 }
527
528 tests := []struct {
529 in *MsgTx // Message to encode
530 out *MsgTx // Expected decoded message
531 buf []byte // Serialized data
532 pkScriptLocs []int // Expected output script locations
533 witness bool // Serialize using the witness encoding
534 }{
535 // No transactions.
536 {
537 noTx,
538 noTx,
539 noTxEncoded,
540 nil,
541 false,
542 },
543
544 // Multiple transactions.
545 {
546 multiTx,
547 multiTx,
548 multiTxEncoded,
549 multiTxPkScriptLocs,
550 false,
551 },
552 // Multiple outputs witness transaction.
553 {
554 multiWitnessTx,
555 multiWitnessTx,
556 multiWitnessTxEncoded,
557 multiWitnessTxPkScriptLocs,
558 true,
559 },
560 }
561
562 t.Logf("Running %d tests", len(tests))
563 for i, test := range tests {
564 // Serialize the transaction.
565 var buf bytes.Buffer
566 err := test.in.Serialize(&buf)
567 if err != nil {
568 t.Errorf("Serialize #%d error %v", i, err)
569 continue
570 }
571 if !bytes.Equal(buf.Bytes(), test.buf) {
572 t.Errorf("Serialize #%d\n got: %s want: %s", i,
573 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
574 continue
575 }
576
577 // Deserialize the transaction.
578 var tx MsgTx
579 rbuf := bytes.NewReader(test.buf)
580 if test.witness {
581 err = tx.Deserialize(rbuf)
582 } else {
583 err = tx.DeserializeNoWitness(rbuf)
584 }
585 if err != nil {
586 t.Errorf("Deserialize #%d error %v", i, err)
587 continue
588 }
589 if !reflect.DeepEqual(&tx, test.out) {
590 t.Errorf("Deserialize #%d\n got: %s want: %s", i,
591 spew.Sdump(&tx), spew.Sdump(test.out))
592 continue
593 }
594
595 // Ensure the public key script locations are accurate.
596 pkScriptLocs := test.in.PkScriptLocs()
597 if !reflect.DeepEqual(pkScriptLocs, test.pkScriptLocs) {
598 t.Errorf("PkScriptLocs #%d\n got: %s want: %s", i,
599 spew.Sdump(pkScriptLocs),
600 spew.Sdump(test.pkScriptLocs))
601 continue
602 }
603 for j, loc := range pkScriptLocs {
604 wantPkScript := test.in.TxOut[j].PkScript
605 gotPkScript := test.buf[loc : loc+len(wantPkScript)]
606 if !bytes.Equal(gotPkScript, wantPkScript) {
607 t.Errorf("PkScriptLocs #%d:%d\n unexpected "+
608 "script got: %s want: %s", i, j,
609 spew.Sdump(gotPkScript),
610 spew.Sdump(wantPkScript))
611 }
612 }
613 }
614 }
615
616 // TestTxSerializeErrors performs negative tests against wire encode and decode
617 // of MsgTx to confirm error paths work correctly.
618 func TestTxSerializeErrors(t *testing.T) {
619 tests := []struct {
620 in *MsgTx // Value to encode
621 buf []byte // Serialized data
622 max int // Max size of fixed buffer to induce errors
623 writeErr error // Expected write error
624 readErr error // Expected read error
625 }{
626 // Force error in version.
627 {multiTx, multiTxEncoded, 0, io.ErrShortWrite, io.EOF},
628 // Force error in number of transaction inputs.
629 {multiTx, multiTxEncoded, 4, io.ErrShortWrite, io.EOF},
630 // Force error in transaction input previous block hash.
631 {multiTx, multiTxEncoded, 5, io.ErrShortWrite, io.EOF},
632 // Force error in transaction input previous block output index.
633 {multiTx, multiTxEncoded, 37, io.ErrShortWrite, io.EOF},
634 // Force error in transaction input signature script length.
635 {multiTx, multiTxEncoded, 41, io.ErrShortWrite, io.EOF},
636 // Force error in transaction input signature script.
637 {multiTx, multiTxEncoded, 42, io.ErrShortWrite, io.EOF},
638 // Force error in transaction input sequence.
639 {multiTx, multiTxEncoded, 49, io.ErrShortWrite, io.EOF},
640 // Force error in number of transaction outputs.
641 {multiTx, multiTxEncoded, 53, io.ErrShortWrite, io.EOF},
642 // Force error in transaction output value.
643 {multiTx, multiTxEncoded, 54, io.ErrShortWrite, io.EOF},
644 // Force error in transaction output pk script length.
645 {multiTx, multiTxEncoded, 62, io.ErrShortWrite, io.EOF},
646 // Force error in transaction output pk script.
647 {multiTx, multiTxEncoded, 63, io.ErrShortWrite, io.EOF},
648 // Force error in transaction output lock time.
649 {multiTx, multiTxEncoded, 206, io.ErrShortWrite, io.EOF},
650 }
651
652 t.Logf("Running %d tests", len(tests))
653 for i, test := range tests {
654 // Serialize the transaction.
655 w := newFixedWriter(test.max)
656 err := test.in.Serialize(w)
657 if err != test.writeErr {
658 t.Errorf("Serialize #%d wrong error got: %v, want: %v",
659 i, err, test.writeErr)
660 continue
661 }
662
663 // Deserialize the transaction.
664 var tx MsgTx
665 r := newFixedReader(test.max, test.buf)
666 err = tx.Deserialize(r)
667 if err != test.readErr {
668 t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
669 i, err, test.readErr)
670 continue
671 }
672 }
673 }
674
675 // TestTxOverflowErrors performs tests to ensure deserializing transactions
676 // which are intentionally crafted to use large values for the variable number
677 // of inputs and outputs are handled properly. This could otherwise potentially
678 // be used as an attack vector.
679 func TestTxOverflowErrors(t *testing.T) {
680 // Use protocol version 70001 and transaction version 1 specifically
681 // here instead of the latest values because the test data is using
682 // bytes encoded with those versions.
683 pver := uint32(70001)
684 txVer := uint32(1)
685
686 tests := []struct {
687 buf []byte // Wire encoding
688 pver uint32 // Protocol version for wire encoding
689 enc MessageEncoding // Message encoding format
690 version uint32 // Transaction version
691 err error // Expected error
692 }{
693 // Transaction that claims to have ~uint64(0) inputs.
694 {
695 []byte{
696 0x00, 0x00, 0x00, 0x01, // Version
697 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
698 0xff, // Varint for number of input transactions
699 }, pver, BaseEncoding, txVer, &MessageError{},
700 },
701
702 // Transaction that claims to have ~uint64(0) outputs.
703 {
704 []byte{
705 0x00, 0x00, 0x00, 0x01, // Version
706 0x00, // Varint for number of input transactions
707 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
708 0xff, // Varint for number of output transactions
709 }, pver, BaseEncoding, txVer, &MessageError{},
710 },
711
712 // Transaction that has an input with a signature script that
713 // claims to have ~uint64(0) length.
714 {
715 []byte{
716 0x00, 0x00, 0x00, 0x01, // Version
717 0x01, // Varint for number of input transactions
718 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
719 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
720 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
721 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
722 0xff, 0xff, 0xff, 0xff, // Previous output index
723 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
724 0xff, // Varint for length of signature script
725 }, pver, BaseEncoding, txVer, &MessageError{},
726 },
727
728 // Transaction that has an output with a public key script
729 // that claims to have ~uint64(0) length.
730 {
731 []byte{
732 0x00, 0x00, 0x00, 0x01, // Version
733 0x01, // Varint for number of input transactions
734 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
735 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
736 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
737 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
738 0xff, 0xff, 0xff, 0xff, // Previous output index
739 0x00, // Varint for length of signature script
740 0xff, 0xff, 0xff, 0xff, // Sequence
741 0x01, // Varint for number of output transactions
742 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Transaction amount
743 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
744 0xff, // Varint for length of public key script
745 }, pver, BaseEncoding, txVer, &MessageError{},
746 },
747 }
748
749 t.Logf("Running %d tests", len(tests))
750 for i, test := range tests {
751 // Decode from wire format.
752 var msg MsgTx
753 r := bytes.NewReader(test.buf)
754 err := msg.BtcDecode(r, test.pver, test.enc)
755 if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
756 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
757 i, err, reflect.TypeOf(test.err))
758 continue
759 }
760
761 // Decode from wire format.
762 r = bytes.NewReader(test.buf)
763 err = msg.Deserialize(r)
764 if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
765 t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
766 i, err, reflect.TypeOf(test.err))
767 continue
768 }
769 }
770 }
771
772 // TestTxSerializeSizeStripped performs tests to ensure the serialize size for
773 // various transactions is accurate.
774 func TestTxSerializeSizeStripped(t *testing.T) {
775 // Empty tx message.
776 noTx := NewMsgTx(1)
777 noTx.Version = 1
778
779 tests := []struct {
780 in *MsgTx // Tx to encode
781 size int // Expected serialized size
782 }{
783 // No inputs or outputs.
784 {noTx, 10},
785
786 // Transcaction with an input and an output.
787 {multiTx, 210},
788
789 // Transaction with an input which includes witness data, and
790 // one output. Note that this uses SerializeSizeStripped which
791 // excludes the additional bytes due to witness data encoding.
792 {multiWitnessTx, 82},
793 }
794
795 t.Logf("Running %d tests", len(tests))
796 for i, test := range tests {
797 serializedSize := test.in.SerializeSizeStripped()
798 if serializedSize != test.size {
799 t.Errorf("MsgTx.SerializeSizeStripped: #%d got: %d, want: %d", i,
800 serializedSize, test.size)
801 continue
802 }
803 }
804 }
805
806 // TestTxID performs tests to ensure the serialize size for various transactions
807 // is accurate.
808 func TestTxID(t *testing.T) {
809 // Empty tx message.
810 noTx := NewMsgTx(1)
811 noTx.Version = 1
812
813 tests := []struct {
814 in *MsgTx // Tx to encode.
815 txid string // Expected transaction ID.
816 }{
817 // No inputs or outputs.
818 {noTx, "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43"},
819
820 // Transaction with an input and an output.
821 {multiTx, "0100d15a522ff38de05c164ca0a56379a1b77dd1e4805a6534dc9b3d88290e9d"},
822
823 // Transaction with an input which includes witness data, and
824 // one output.
825 {multiWitnessTx, "0f167d1385a84d1518cfee208b653fc9163b605ccf1b75347e2850b3e2eb19f3"},
826 }
827
828 for i, test := range tests {
829 txid := test.in.TxID()
830 require.Equal(t, test.txid, txid, "test #%d", i)
831 }
832 }
833
834 // TestTxWitnessSize performs tests to ensure that the serialized size for
835 // various types of transactions that include witness data is accurate.
836 func TestTxWitnessSize(t *testing.T) {
837 tests := []struct {
838 in *MsgTx // Tx to encode
839 size int // Expected serialized size w/ witnesses
840 }{
841 // Transaction with an input which includes witness data, and
842 // one output.
843 {multiWitnessTx, 190},
844 }
845
846 t.Logf("Running %d tests", len(tests))
847 for i, test := range tests {
848 serializedSize := test.in.SerializeSize()
849 if serializedSize != test.size {
850 t.Errorf("MsgTx.SerializeSize: #%d got: %d, want: %d", i,
851 serializedSize, test.size)
852 continue
853 }
854 }
855 }
856
857 // TestTxOutPointFromString performs tests to ensure that the outpoint string
858 // parser works as expected.
859 func TestTxOutPointFromString(t *testing.T) {
860 hashFromStr := func(hash string) chainhash.Hash {
861 h, _ := chainhash.NewHashFromStr(hash)
862 return *h
863 }
864
865 tests := []struct {
866 name string
867 input string
868 result *OutPoint
869 err bool
870 }{
871 {
872 name: "normal outpoint 1",
873 input: "2ebd15a7e758d5f4c7c74181b99e5b8586f88e0682dc13e09d92612a2b2bb0a2:1",
874 result: &OutPoint{
875 Hash: hashFromStr("2ebd15a7e758d5f4c7c74181b99e5b8586f88e0682dc13e09d92612a2b2bb0a2"),
876 Index: 1,
877 },
878 err: false,
879 },
880 {
881 name: "normal outpoint 2",
882 input: "94c7762a68ff164352bd31fd95fa875204e811c09acef40ba781787eb28e3b55:42",
883 result: &OutPoint{
884 Hash: hashFromStr("94c7762a68ff164352bd31fd95fa875204e811c09acef40ba781787eb28e3b55"),
885 Index: 42,
886 },
887 err: false,
888 },
889 {
890 name: "big index outpoint",
891 input: "94c7762a68ff164352bd31fd95fa875204e811c09acef40ba781787eb28e3b55:2147484242",
892 result: &OutPoint{
893 Hash: hashFromStr("94c7762a68ff164352bd31fd95fa875204e811c09acef40ba781787eb28e3b55"),
894 Index: 2147484242,
895 },
896 err: false,
897 },
898 {
899 name: "normal outpoint 2 with 31-byte txid",
900 input: "c7762a68ff164352bd31fd95fa875204e811c09acef40ba781787eb28e3b55:42",
901 result: &OutPoint{
902 Hash: hashFromStr("c7762a68ff164352bd31fd95fa875204e811c09acef40ba781787eb28e3b55"),
903 Index: 42,
904 },
905 err: true,
906 },
907 {
908 name: "bad string",
909 input: "not_outpoint_not_outpoint_not_outpoint",
910 result: nil,
911 err: true,
912 },
913 {
914 name: "empty string",
915 input: "",
916 result: nil,
917 err: true,
918 },
919 }
920
921 for _, test := range tests {
922 t.Run(test.name, func(t *testing.T) {
923 outpoint, err := NewOutPointFromString(test.input)
924
925 isErr := (err != nil)
926 require.Equal(t, isErr, test.err)
927
928 if !isErr {
929 require.Equal(t, test.result, outpoint)
930 }
931 })
932
933 }
934 }
935
936 // TestTxSuperfluousWitnessRecord ensures that btcd fails to parse a tx with
937 // the witness marker flag set but without any actual witnesses.
938 func TestTxSuperfluousWitnessRecord(t *testing.T) {
939 m := &MsgTx{}
940 rbuf := bytes.NewReader(multiWitnessFlagNoWitness)
941 err := m.BtcDecode(rbuf, ProtocolVersion, WitnessEncoding)
942 if !errors.Is(err, errSuperfluousWitnessRecord) {
943 t.Fatalf("should have failed with %v", errSuperfluousWitnessRecord)
944 }
945 }
946
947 // multiTx is a MsgTx with an input and output and used in various tests.
948 var multiTx = &MsgTx{
949 Version: 1,
950 TxIn: []*TxIn{
951 {
952 PreviousOutPoint: OutPoint{
953 Hash: chainhash.Hash{},
954 Index: 0xffffffff,
955 },
956 SignatureScript: []byte{
957 0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62,
958 },
959 Sequence: 0xffffffff,
960 },
961 },
962 TxOut: []*TxOut{
963 {
964 Value: 0x12a05f200,
965 PkScript: []byte{
966 0x41, // OP_DATA_65
967 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
968 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
969 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
970 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
971 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
972 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
973 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
974 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
975 0xa6, // 65-byte signature
976 0xac, // OP_CHECKSIG
977 },
978 },
979 {
980 Value: 0x5f5e100,
981 PkScript: []byte{
982 0x41, // OP_DATA_65
983 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
984 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
985 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
986 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
987 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
988 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
989 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
990 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
991 0xa6, // 65-byte signature
992 0xac, // OP_CHECKSIG
993 },
994 },
995 },
996 LockTime: 0,
997 }
998
999 // multiTxEncoded is the wire encoded bytes for multiTx using protocol version
1000 // 60002 and is used in the various tests.
1001 var multiTxEncoded = []byte{
1002 0x01, 0x00, 0x00, 0x00, // Version
1003 0x01, // Varint for number of input transactions
1004 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1005 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1006 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1007 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
1008 0xff, 0xff, 0xff, 0xff, // Previous output index
1009 0x07, // Varint for length of signature script
1010 0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62, // Signature script
1011 0xff, 0xff, 0xff, 0xff, // Sequence
1012 0x02, // Varint for number of output transactions
1013 0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
1014 0x43, // Varint for length of pk script
1015 0x41, // OP_DATA_65
1016 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
1017 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
1018 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
1019 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
1020 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
1021 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
1022 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
1023 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
1024 0xa6, // 65-byte signature
1025 0xac, // OP_CHECKSIG
1026 0x00, 0xe1, 0xf5, 0x05, 0x00, 0x00, 0x00, 0x00, // Transaction amount
1027 0x43, // Varint for length of pk script
1028 0x41, // OP_DATA_65
1029 0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
1030 0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
1031 0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
1032 0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
1033 0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
1034 0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
1035 0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
1036 0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
1037 0xa6, // 65-byte signature
1038 0xac, // OP_CHECKSIG
1039 0x00, 0x00, 0x00, 0x00, // Lock time
1040 }
1041
1042 // multiTxPkScriptLocs is the location information for the public key scripts
1043 // located in multiTx.
1044 var multiTxPkScriptLocs = []int{63, 139}
1045
1046 // multiWitnessTx is a MsgTx with an input with witness data, and an
1047 // output used in various tests.
1048 var multiWitnessTx = &MsgTx{
1049 Version: 1,
1050 TxIn: []*TxIn{
1051 {
1052 PreviousOutPoint: OutPoint{
1053 Hash: chainhash.Hash{
1054 0xa5, 0x33, 0x52, 0xd5, 0x13, 0x57, 0x66, 0xf0,
1055 0x30, 0x76, 0x59, 0x74, 0x18, 0x26, 0x3d, 0xa2,
1056 0xd9, 0xc9, 0x58, 0x31, 0x59, 0x68, 0xfe, 0xa8,
1057 0x23, 0x52, 0x94, 0x67, 0x48, 0x1f, 0xf9, 0xcd,
1058 },
1059 Index: 19,
1060 },
1061 SignatureScript: []byte{},
1062 Witness: [][]byte{
1063 { // 70-byte signature
1064 0x30, 0x43, 0x02, 0x1f, 0x4d, 0x23, 0x81, 0xdc,
1065 0x97, 0xf1, 0x82, 0xab, 0xd8, 0x18, 0x5f, 0x51,
1066 0x75, 0x30, 0x18, 0x52, 0x32, 0x12, 0xf5, 0xdd,
1067 0xc0, 0x7c, 0xc4, 0xe6, 0x3a, 0x8d, 0xc0, 0x36,
1068 0x58, 0xda, 0x19, 0x02, 0x20, 0x60, 0x8b, 0x5c,
1069 0x4d, 0x92, 0xb8, 0x6b, 0x6d, 0xe7, 0xd7, 0x8e,
1070 0xf2, 0x3a, 0x2f, 0xa7, 0x35, 0xbc, 0xb5, 0x9b,
1071 0x91, 0x4a, 0x48, 0xb0, 0xe1, 0x87, 0xc5, 0xe7,
1072 0x56, 0x9a, 0x18, 0x19, 0x70, 0x01,
1073 },
1074 { // 33-byte serialize pub key
1075 0x03, 0x07, 0xea, 0xd0, 0x84, 0x80, 0x7e, 0xb7,
1076 0x63, 0x46, 0xdf, 0x69, 0x77, 0x00, 0x0c, 0x89,
1077 0x39, 0x2f, 0x45, 0xc7, 0x64, 0x25, 0xb2, 0x61,
1078 0x81, 0xf5, 0x21, 0xd7, 0xf3, 0x70, 0x06, 0x6a,
1079 0x8f,
1080 },
1081 },
1082 Sequence: 0xffffffff,
1083 },
1084 },
1085 TxOut: []*TxOut{
1086 {
1087 Value: 395019,
1088 PkScript: []byte{ // p2wkh output
1089 0x00, // Version 0 witness program
1090 0x14, // OP_DATA_20
1091 0x9d, 0xda, 0xc6, 0xf3, 0x9d, 0x51, 0xe0, 0x39,
1092 0x8e, 0x53, 0x2a, 0x22, 0xc4, 0x1b, 0xa1, 0x89,
1093 0x40, 0x6a, 0x85, 0x23, // 20-byte pub key hash
1094 },
1095 },
1096 },
1097 }
1098
1099 // multiWitnessFlagNoWitness is the wire encoded bytes for multiWitnessTx with
1100 // the witness flag set but with witnesses omitted.
1101 var multiWitnessFlagNoWitness = []byte{
1102 0x1, 0x0, 0x0, 0x0, // Version
1103 TxFlagMarker, // Marker byte indicating 0 inputs, or a segwit encoded tx
1104 WitnessFlag, // Flag byte
1105 0x1, // Varint for number of inputs
1106 0xa5, 0x33, 0x52, 0xd5, 0x13, 0x57, 0x66, 0xf0,
1107 0x30, 0x76, 0x59, 0x74, 0x18, 0x26, 0x3d, 0xa2,
1108 0xd9, 0xc9, 0x58, 0x31, 0x59, 0x68, 0xfe, 0xa8,
1109 0x23, 0x52, 0x94, 0x67, 0x48, 0x1f, 0xf9, 0xcd, // Previous output hash
1110 0x13, 0x0, 0x0, 0x0, // Little endian previous output index
1111 0x0, // No sig script (this is a witness input)
1112 0xff, 0xff, 0xff, 0xff, // Sequence
1113 0x1, // Varint for number of outputs
1114 0xb, 0x7, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, // Output amount
1115 0x16, // Varint for length of pk script
1116 0x0, // Version 0 witness program
1117 0x14, // OP_DATA_20
1118 0x9d, 0xda, 0xc6, 0xf3, 0x9d, 0x51, 0xe0, 0x39,
1119 0x8e, 0x53, 0x2a, 0x22, 0xc4, 0x1b, 0xa1, 0x89,
1120 0x40, 0x6a, 0x85, 0x23, // 20-byte pub key hash
1121 0x00, // No item on the witness stack for the first input
1122 0x00, // No item on the witness stack for the second input
1123 0x0, 0x0, 0x0, 0x0, // Lock time
1124 }
1125
1126 // multiWitnessTxEncoded is the wire encoded bytes for multiWitnessTx including inputs
1127 // with witness data using protocol version 70012 and is used in the various
1128 // tests.
1129 var multiWitnessTxEncoded = []byte{
1130 0x1, 0x0, 0x0, 0x0, // Version
1131 TxFlagMarker, // Marker byte indicating 0 inputs, or a segwit encoded tx
1132 WitnessFlag, // Flag byte
1133 0x1, // Varint for number of inputs
1134 0xa5, 0x33, 0x52, 0xd5, 0x13, 0x57, 0x66, 0xf0,
1135 0x30, 0x76, 0x59, 0x74, 0x18, 0x26, 0x3d, 0xa2,
1136 0xd9, 0xc9, 0x58, 0x31, 0x59, 0x68, 0xfe, 0xa8,
1137 0x23, 0x52, 0x94, 0x67, 0x48, 0x1f, 0xf9, 0xcd, // Previous output hash
1138 0x13, 0x0, 0x0, 0x0, // Little endian previous output index
1139 0x0, // No sig script (this is a witness input)
1140 0xff, 0xff, 0xff, 0xff, // Sequence
1141 0x1, // Varint for number of outputs
1142 0xb, 0x7, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, // Output amount
1143 0x16, // Varint for length of pk script
1144 0x0, // Version 0 witness program
1145 0x14, // OP_DATA_20
1146 0x9d, 0xda, 0xc6, 0xf3, 0x9d, 0x51, 0xe0, 0x39,
1147 0x8e, 0x53, 0x2a, 0x22, 0xc4, 0x1b, 0xa1, 0x89,
1148 0x40, 0x6a, 0x85, 0x23, // 20-byte pub key hash
1149 0x2, // Two items on the witness stack
1150 0x46, // 70 byte stack item
1151 0x30, 0x43, 0x2, 0x1f, 0x4d, 0x23, 0x81, 0xdc,
1152 0x97, 0xf1, 0x82, 0xab, 0xd8, 0x18, 0x5f, 0x51,
1153 0x75, 0x30, 0x18, 0x52, 0x32, 0x12, 0xf5, 0xdd,
1154 0xc0, 0x7c, 0xc4, 0xe6, 0x3a, 0x8d, 0xc0, 0x36,
1155 0x58, 0xda, 0x19, 0x2, 0x20, 0x60, 0x8b, 0x5c,
1156 0x4d, 0x92, 0xb8, 0x6b, 0x6d, 0xe7, 0xd7, 0x8e,
1157 0xf2, 0x3a, 0x2f, 0xa7, 0x35, 0xbc, 0xb5, 0x9b,
1158 0x91, 0x4a, 0x48, 0xb0, 0xe1, 0x87, 0xc5, 0xe7,
1159 0x56, 0x9a, 0x18, 0x19, 0x70, 0x1,
1160 0x21, // 33 byte stack item
1161 0x3, 0x7, 0xea, 0xd0, 0x84, 0x80, 0x7e, 0xb7,
1162 0x63, 0x46, 0xdf, 0x69, 0x77, 0x0, 0xc, 0x89,
1163 0x39, 0x2f, 0x45, 0xc7, 0x64, 0x25, 0xb2, 0x61,
1164 0x81, 0xf5, 0x21, 0xd7, 0xf3, 0x70, 0x6, 0x6a,
1165 0x8f,
1166 0x0, 0x0, 0x0, 0x0, // Lock time
1167 }
1168
1169 // multiWitnessTxEncodedNonZeroFlag is an incorrect wire encoded bytes for
1170 // multiWitnessTx including inputs with witness data. Instead of the flag byte
1171 // being set to 0x01, the flag is 0x00, which should trigger a decoding error.
1172 var multiWitnessTxEncodedNonZeroFlag = []byte{
1173 0x1, 0x0, 0x0, 0x0, // Version
1174 TxFlagMarker, // Marker byte indicating 0 inputs, or a segwit encoded tx
1175 0x0, // Incorrect flag byte (should be 0x01)
1176 0x1, // Varint for number of inputs
1177 0xa5, 0x33, 0x52, 0xd5, 0x13, 0x57, 0x66, 0xf0,
1178 0x30, 0x76, 0x59, 0x74, 0x18, 0x26, 0x3d, 0xa2,
1179 0xd9, 0xc9, 0x58, 0x31, 0x59, 0x68, 0xfe, 0xa8,
1180 0x23, 0x52, 0x94, 0x67, 0x48, 0x1f, 0xf9, 0xcd, // Previous output hash
1181 0x13, 0x0, 0x0, 0x0, // Little endian previous output index
1182 0x0, // No sig script (this is a witness input)
1183 0xff, 0xff, 0xff, 0xff, // Sequence
1184 0x1, // Varint for number of outputs
1185 0xb, 0x7, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, // Output amount
1186 0x16, // Varint for length of pk script
1187 0x0, // Version 0 witness program
1188 0x14, // OP_DATA_20
1189 0x9d, 0xda, 0xc6, 0xf3, 0x9d, 0x51, 0xe0, 0x39,
1190 0x8e, 0x53, 0x2a, 0x22, 0xc4, 0x1b, 0xa1, 0x89,
1191 0x40, 0x6a, 0x85, 0x23, // 20-byte pub key hash
1192 0x2, // Two items on the witness stack
1193 0x46, // 70 byte stack item
1194 0x30, 0x43, 0x2, 0x1f, 0x4d, 0x23, 0x81, 0xdc,
1195 0x97, 0xf1, 0x82, 0xab, 0xd8, 0x18, 0x5f, 0x51,
1196 0x75, 0x30, 0x18, 0x52, 0x32, 0x12, 0xf5, 0xdd,
1197 0xc0, 0x7c, 0xc4, 0xe6, 0x3a, 0x8d, 0xc0, 0x36,
1198 0x58, 0xda, 0x19, 0x2, 0x20, 0x60, 0x8b, 0x5c,
1199 0x4d, 0x92, 0xb8, 0x6b, 0x6d, 0xe7, 0xd7, 0x8e,
1200 0xf2, 0x3a, 0x2f, 0xa7, 0x35, 0xbc, 0xb5, 0x9b,
1201 0x91, 0x4a, 0x48, 0xb0, 0xe1, 0x87, 0xc5, 0xe7,
1202 0x56, 0x9a, 0x18, 0x19, 0x70, 0x1,
1203 0x21, // 33 byte stack item
1204 0x3, 0x7, 0xea, 0xd0, 0x84, 0x80, 0x7e, 0xb7,
1205 0x63, 0x46, 0xdf, 0x69, 0x77, 0x0, 0xc, 0x89,
1206 0x39, 0x2f, 0x45, 0xc7, 0x64, 0x25, 0xb2, 0x61,
1207 0x81, 0xf5, 0x21, 0xd7, 0xf3, 0x70, 0x6, 0x6a,
1208 0x8f,
1209 0x0, 0x0, 0x0, 0x0, // Lock time
1210 }
1211
1212 // multiTxPkScriptLocs is the location information for the public key scripts
1213 // located in multiWitnessTx.
1214 var multiWitnessTxPkScriptLocs = []int{58}
1215
1216 // TestTxWitnessOverflowPanic ensures that decoding a witness tx where
1217 // cumulative witness item lengths exceed the script arena capacity
1218 // returns a decode error instead of panicking on an out-of-bounds
1219 // slice.
1220 func TestTxWitnessOverflowPanic(t *testing.T) {
1221 // Build a minimal witness tx with one input, zero outputs,
1222 // and two witness items whose combined claimed lengths
1223 // exceed the scriptArenaMaxAlloc (4 MiB) decode limit.
1224 //
1225 // Item 1: 3 000 000 bytes (fits in arena, < maxWitnessItemSize)
1226 // Item 2: 2 000 000 bytes (passes maxWitnessItemSize but
1227 // overflows remaining arena capacity of ~1.19 MiB)
1228 const (
1229 firstLen = 3_000_000
1230 secondLen = 2_000_000
1231 )
1232
1233 var buf bytes.Buffer
1234
1235 // tx version = 2
1236 buf.Write([]byte{0x02, 0x00, 0x00, 0x00})
1237
1238 // Segwit marker + flag
1239 buf.WriteByte(TxFlagMarker)
1240 buf.WriteByte(byte(WitnessFlag))
1241
1242 // 1 input
1243 WriteVarInt(&buf, 0, 1)
1244
1245 // Previous outpoint: 32-byte zero hash + index 0
1246 buf.Write(make([]byte, 32))
1247 buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
1248
1249 // Empty signature script
1250 WriteVarInt(&buf, 0, 0)
1251
1252 // Sequence
1253 buf.Write([]byte{0x00, 0x00, 0x00, 0x00})
1254
1255 // 0 outputs
1256 WriteVarInt(&buf, 0, 0)
1257
1258 // Witness: 2 stack items for the single input
1259 WriteVarInt(&buf, 0, 2)
1260
1261 // First witness item: firstLen bytes of zeros.
1262 WriteVarInt(&buf, 0, firstLen)
1263 buf.Write(make([]byte, firstLen))
1264
1265 // Second witness item: only write the varint claiming
1266 // secondLen bytes. The actual data is irrelevant because
1267 // the bounds check must reject before reading it.
1268 WriteVarInt(&buf, 0, secondLen)
1269
1270 // No locktime needed; decoding should fail before that.
1271
1272 var msg MsgTx
1273 r := bytes.NewReader(buf.Bytes())
1274 err := msg.BtcDecode(r, ProtocolVersion, WitnessEncoding)
1275 require.Error(t, err)
1276
1277 var msgErr *MessageError
1278 require.ErrorAs(t, err, &msgErr)
1279 }
1280