|
36 | 36 | ser_vector,
|
37 | 37 | sha256,
|
38 | 38 | uint256_from_str,
|
| 39 | + FromHex, |
39 | 40 | )
|
40 | 41 | from test_framework.mininode import (
|
41 | 42 | P2PInterface,
|
|
77 | 78 | disconnect_nodes,
|
78 | 79 | get_bip9_status,
|
79 | 80 | hex_str_to_bytes,
|
| 81 | + assert_raises_rpc_error, |
80 | 82 | )
|
81 | 83 |
|
82 | 84 | # The versionbit bit used to signal activation of SegWit
|
@@ -269,6 +271,7 @@ def run_test(self):
|
269 | 271 | self.test_non_standard_witness()
|
270 | 272 | self.test_upgrade_after_activation()
|
271 | 273 | self.test_witness_sigops()
|
| 274 | + self.test_superfluous_witness() |
272 | 275 |
|
273 | 276 | # Individual tests
|
274 | 277 |
|
@@ -2034,5 +2037,31 @@ def test_witness_sigops(self):
|
2034 | 2037 |
|
2035 | 2038 | # TODO: test p2sh sigop counting
|
2036 | 2039 |
|
| 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 | + |
2037 | 2066 | if __name__ == '__main__':
|
2038 | 2067 | SegWitTest().main()
|
0 commit comments