From 0b59cb7ce15f244a67837379a64b70a7b361cb16 Mon Sep 17 00:00:00 2001 From: Oscar Lafarga Date: Sat, 4 Mar 2023 12:49:44 +0000 Subject: [PATCH] Add test for decoding txn with oversized witness --- wire/msgtx_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/wire/msgtx_test.go b/wire/msgtx_test.go index bcbe025d4c..ea1846ab4b 100644 --- a/wire/msgtx_test.go +++ b/wire/msgtx_test.go @@ -831,7 +831,57 @@ func TestTxWitnessLimits(t *testing.T) { return encodedTxnBuffer } + var createTxnWithOversizedWitness = func() []byte { + var encodedTxnBuffer []byte + + txnBeforeWitnessEncoded := []byte{ + 0x1, 0x0, 0x0, 0x0, // Version + TxFlagMarker, // Marker byte indicating 0 inputs, or a segwit encoded tx + WitnessFlag, // Flag byte + 0x1, // Varint for number of inputs + 0xa5, 0x33, 0x52, 0xd5, 0x13, 0x57, 0x66, 0xf0, + 0x30, 0x76, 0x59, 0x74, 0x18, 0x26, 0x3d, 0xa2, + 0xd9, 0xc9, 0x58, 0x31, 0x59, 0x68, 0xfe, 0xa8, + 0x23, 0x52, 0x94, 0x67, 0x48, 0x1f, 0xf9, 0xcd, // Previous output hash + 0x13, 0x0, 0x0, 0x0, // Little endian previous output index + 0x0, // No sig script (this is a witness input) + 0xff, 0xff, 0xff, 0xff, // Sequence + 0x1, // Varint for number of outputs + 0xb, 0x7, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, // Output amount + 0x16, // Varint for length of pk script + 0x0, // Version 0 witness program + 0x14, // OP_DATA_20 + 0x9d, 0xda, 0xc6, 0xf3, 0x9d, 0x51, 0xe0, 0x39, + 0x8e, 0x53, 0x2a, 0x22, 0xc4, 0x1b, 0xa1, 0x89, + 0x40, 0x6a, 0x85, 0x23, // 20-byte pub key hash + } + // Add the number of items on the witness stack + witnessItemCountEncoded := []byte{ + 0x1, // 1 item on the witness stack + } + encodedTxnBuffer = append( + txnBeforeWitnessEncoded, + witnessItemCountEncoded... + ) + // Add the size of the witness item + witnessLengthEncoded := []byte{ + 0xfe, 0x01, 0x09, 0x3d, 0x00, // 4,000,001 bytes + } + encodedTxnBuffer = append(encodedTxnBuffer, witnessLengthEncoded...) + // Add a witness item with 4,000,001 bytes of zeros + for i := uint(0); i < 4_000_001; i++ { + encodedTxnBuffer = append(encodedTxnBuffer, 0) + } + var locktimeEncoded = []byte{ + 0x0, 0x0, 0x0, 0x0, // Lock time + } + encodedTxnBuffer = append(encodedTxnBuffer, locktimeEncoded...) + + return encodedTxnBuffer + } + encodedTxnWithManyWitnessItems := createTxnWithManyWitnessItems() + encodedTxnWithOversizedWitness := createTxnWithOversizedWitness() tests := []struct { buf []byte // Wire encoding @@ -842,11 +892,19 @@ func TestTxWitnessLimits(t *testing.T) { }{ // Transaction with 1 input and 4,000,001 items in the witness stack { - encodedTxnWithManyWitnessItems, - pver, - WitnessEncoding, - txVer, - &MessageError{}, + buf: encodedTxnWithManyWitnessItems, + pver: pver, + enc: WitnessEncoding, + version: txVer, + err: &MessageError{}, + }, + // Transaction with 1 input and a large witness script + { + buf: encodedTxnWithOversizedWitness, + pver: pver, + enc: WitnessEncoding, + version: txVer, + err: &MessageError{}, }, }