Skip to content

Commit 8603108

Browse files
instagibbsMarcoFalke
authored andcommitted
Add test for superfluous witness record in deserialization
Github-Pull: #15893 Rebased-From: cc556e4
1 parent 5a58ddb commit 8603108

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/functional/p2p_segwit.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
ser_vector,
3838
sha256,
3939
uint256_from_str,
40+
FromHex,
4041
)
4142
from test_framework.mininode import (
4243
P2PInterface,
@@ -81,6 +82,7 @@
8182
hex_str_to_bytes,
8283
sync_blocks,
8384
sync_mempools,
85+
assert_raises_rpc_error,
8486
)
8587

8688
# The versionbit bit used to signal activation of SegWit
@@ -273,6 +275,7 @@ def run_test(self):
273275
self.test_non_standard_witness()
274276
self.test_upgrade_after_activation()
275277
self.test_witness_sigops()
278+
self.test_superfluous_witness()
276279

277280
# Individual tests
278281

@@ -2039,5 +2042,31 @@ def test_witness_sigops(self):
20392042

20402043
# TODO: test p2sh sigop counting
20412044

2045+
def test_superfluous_witness(self):
2046+
# Serialization of tx that puts witness flag to 1 always
2047+
def serialize_with_bogus_witness(tx):
2048+
flags = 1
2049+
r = b""
2050+
r += struct.pack("<i", tx.nVersion)
2051+
if flags:
2052+
dummy = []
2053+
r += ser_vector(dummy)
2054+
r += struct.pack("<B", flags)
2055+
r += ser_vector(tx.vin)
2056+
r += ser_vector(tx.vout)
2057+
if flags & 1:
2058+
if (len(tx.wit.vtxinwit) != len(tx.vin)):
2059+
# vtxinwit must have the same length as vin
2060+
tx.wit.vtxinwit = tx.wit.vtxinwit[:len(tx.vin)]
2061+
for i in range(len(tx.wit.vtxinwit), len(tx.vin)):
2062+
tx.wit.vtxinwit.append(CTxInWitness())
2063+
r += tx.wit.serialize()
2064+
r += struct.pack("<I", tx.nLockTime)
2065+
return r
2066+
2067+
raw = self.nodes[0].createrawtransaction([{"txid":"00"*32, "vout":0}], {self.nodes[0].getnewaddress():1})
2068+
tx = FromHex(CTransaction(), raw)
2069+
assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, serialize_with_bogus_witness(tx).hex())
2070+
20422071
if __name__ == '__main__':
20432072
SegWitTest().main()

0 commit comments

Comments
 (0)