|
37 | 37 | ser_vector, |
38 | 38 | sha256, |
39 | 39 | uint256_from_str, |
| 40 | + FromHex, |
40 | 41 | ) |
41 | 42 | from test_framework.mininode import ( |
42 | 43 | P2PInterface, |
|
81 | 82 | hex_str_to_bytes, |
82 | 83 | sync_blocks, |
83 | 84 | sync_mempools, |
| 85 | + assert_raises_rpc_error, |
84 | 86 | ) |
85 | 87 |
|
86 | 88 | # The versionbit bit used to signal activation of SegWit |
@@ -273,6 +275,7 @@ def run_test(self): |
273 | 275 | self.test_non_standard_witness() |
274 | 276 | self.test_upgrade_after_activation() |
275 | 277 | self.test_witness_sigops() |
| 278 | + self.test_superfluous_witness() |
276 | 279 |
|
277 | 280 | # Individual tests |
278 | 281 |
|
@@ -2039,5 +2042,31 @@ def test_witness_sigops(self): |
2039 | 2042 |
|
2040 | 2043 | # TODO: test p2sh sigop counting |
2041 | 2044 |
|
| 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 | + |
2042 | 2071 | if __name__ == '__main__': |
2043 | 2072 | SegWitTest().main() |
0 commit comments