Skip to content

Commit 753a1ce

Browse files
committed
Merge #526: [0.17] Add test for superfulous witness record in deserialization
310c4dc Add test for superfulous witness record in deserialization (Gregory Sanders) Pull request description: Backport of instagibbs/bitcoin@7c1ad7c Tree-SHA512: 4f67814e411209ef3424e2ae80bd3c2c4ec92e670742b795c3c79ac7c9eb4be92727472d1611e5dade8710207d47d6157db0b790d62ab8f27c79e7c6141660ae
2 parents f9cb345 + 310c4dc commit 753a1ce

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

test/functional/p2p_segwit.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
ser_vector,
4040
sha256,
4141
uint256_from_str,
42+
FromHex,
4243
)
4344
from test_framework.mininode import (
4445
P2PInterface,
@@ -83,6 +84,7 @@
8384
hex_str_to_bytes,
8485
sync_blocks,
8586
sync_mempools,
87+
assert_raises_rpc_error,
8688
)
8789
from test_framework import util
8890

@@ -277,6 +279,7 @@ def run_test(self):
277279
self.test_non_standard_witness()
278280
self.test_upgrade_after_activation()
279281
self.test_witness_sigops()
282+
self.test_superfluous_witness()
280283

281284
# Individual tests
282285

@@ -2126,5 +2129,33 @@ def test_witness_sigops(self):
21262129

21272130
# TODO: test p2sh sigop counting
21282131

2132+
def test_superfluous_witness(self):
2133+
# Serialization of tx that puts witness flag to 1 always
2134+
def serialize_with_bogus_witness(tx):
2135+
flags = 1
2136+
r = b""
2137+
r += struct.pack("<i", tx.nVersion)
2138+
r += struct.pack("<B", flags)
2139+
r += ser_vector(tx.vin)
2140+
r += ser_vector(tx.vout)
2141+
r += struct.pack("<I", tx.nLockTime)
2142+
if flags & 1:
2143+
if len(tx.wit.vtxinwit) != len(tx.vin):
2144+
# vtxinwit must have the same length as vin
2145+
tx.wit.vtxinwit = tx.wit.vtxinwit[:len(tx.vin)]
2146+
for i in range(len(tx.wit.vtxinwit), len(tx.vin)):
2147+
tx.wit.vtxinwit.append(CTxInWitness())
2148+
if len(tx.wit.vtxoutwit) != len(tx.vout):
2149+
# vtxoutwit must have the same length as vout
2150+
tx.wit.vtxoutwit = tx.wit.vtxoutwit[:len(tx.vout)]
2151+
for i in range(len(tx.wit.vtxoutwit), len(tx.vout)):
2152+
tx.wit.vtxoutwit.append(CTxOutWitness())
2153+
r += tx.wit.serialize()
2154+
return r
2155+
2156+
raw = self.nodes[0].createrawtransaction([{"txid":"00"*32, "vout":0}], {self.nodes[0].getnewaddress():1})
2157+
tx = FromHex(CTransaction(), raw)
2158+
assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, serialize_with_bogus_witness(tx).hex())
2159+
21292160
if __name__ == '__main__':
21302161
SegWitTest().main()

0 commit comments

Comments
 (0)