Skip to content

Commit cc556e4

Browse files
committed
Add test for superfluous witness record in deserialization
1 parent 25b0786 commit cc556e4

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
@@ -36,6 +36,7 @@
3636
ser_vector,
3737
sha256,
3838
uint256_from_str,
39+
FromHex,
3940
)
4041
from test_framework.mininode import (
4142
P2PInterface,
@@ -77,6 +78,7 @@
7778
disconnect_nodes,
7879
get_bip9_status,
7980
hex_str_to_bytes,
81+
assert_raises_rpc_error,
8082
)
8183

8284
# The versionbit bit used to signal activation of SegWit
@@ -269,6 +271,7 @@ def run_test(self):
269271
self.test_non_standard_witness()
270272
self.test_upgrade_after_activation()
271273
self.test_witness_sigops()
274+
self.test_superfluous_witness()
272275

273276
# Individual tests
274277

@@ -2034,5 +2037,31 @@ def test_witness_sigops(self):
20342037

20352038
# TODO: test p2sh sigop counting
20362039

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

0 commit comments

Comments
 (0)