|
39 | 39 | ser_vector, |
40 | 40 | sha256, |
41 | 41 | uint256_from_str, |
| 42 | + FromHex, |
42 | 43 | ) |
43 | 44 | from test_framework.mininode import ( |
44 | 45 | P2PInterface, |
|
83 | 84 | hex_str_to_bytes, |
84 | 85 | sync_blocks, |
85 | 86 | sync_mempools, |
| 87 | + assert_raises_rpc_error, |
86 | 88 | ) |
87 | 89 | from test_framework import util |
88 | 90 |
|
@@ -277,6 +279,7 @@ def run_test(self): |
277 | 279 | self.test_non_standard_witness() |
278 | 280 | self.test_upgrade_after_activation() |
279 | 281 | self.test_witness_sigops() |
| 282 | + self.test_superfluous_witness() |
280 | 283 |
|
281 | 284 | # Individual tests |
282 | 285 |
|
@@ -2126,5 +2129,33 @@ def test_witness_sigops(self): |
2126 | 2129 |
|
2127 | 2130 | # TODO: test p2sh sigop counting |
2128 | 2131 |
|
| 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 | + |
2129 | 2160 | if __name__ == '__main__': |
2130 | 2161 | SegWitTest().main() |
0 commit comments