Skip to content

Commit

Permalink
Add test for decoding txn with oversized witness
Browse files Browse the repository at this point in the history
  • Loading branch information
otech47 committed Mar 4, 2023
1 parent b035335 commit 0b59cb7
Showing 1 changed file with 63 additions and 5 deletions.
68 changes: 63 additions & 5 deletions wire/msgtx_test.go
Expand Up @@ -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
Expand All @@ -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{},
},
}

Expand Down

0 comments on commit 0b59cb7

Please sign in to comment.