diff --git a/test/functional/p2p_segwit.py b/test/functional/p2p_segwit.py index 6224e9c5ddc..41e6764fc09 100755 --- a/test/functional/p2p_segwit.py +++ b/test/functional/p2p_segwit.py @@ -29,6 +29,7 @@ from test_framework.messages import ( MSG_WTX, NODE_NETWORK, NODE_WITNESS, + WITNESS_SCALE_FACTOR, msg_no_witness_block, msg_getdata, msg_headers, @@ -1872,6 +1873,7 @@ class SegWitTest(BitcoinTestFramework): # sig ops outputs = (MAX_SIGOP_COST // sigops_per_script) + 2 extra_sigops_available = MAX_SIGOP_COST % sigops_per_script + p2sh_outputs = MAX_SIGOP_COST // (sigops_per_script * WITNESS_SCALE_FACTOR) + 1 # We chose the number of checkmultisigs/checksigs to make this work: assert extra_sigops_available < 100 # steer clear of MAX_OPS_PER_SCRIPT @@ -1895,6 +1897,7 @@ class SegWitTest(BitcoinTestFramework): tx.vout.append(CTxOut(split_value, script_pubkey)) tx.vout[-2].scriptPubKey = script_pubkey_toomany tx.vout[-1].scriptPubKey = script_pubkey_justright + tx.vout += [CTxOut(0, script_to_p2sh_script(witness_script)) for _ in range(p2sh_outputs)] block_1 = self.build_next_block() self.update_witness_block_with_transactions(block_1, [tx]) @@ -1923,7 +1926,7 @@ class SegWitTest(BitcoinTestFramework): tx2.vout.append(CTxOut(0, script_pubkey_checksigs)) tx2.vin.pop() tx2.wit.vtxinwit.pop() - tx2.vout[0].nValue -= tx.vout[-2].nValue + tx2.vout[0].nValue -= tx.vout[outputs - 2].nValue block_3 = self.build_next_block() self.update_witness_block_with_transactions(block_3, [tx2]) test_witness_block(self.nodes[0], self.test_node, block_3, accepted=False, reason='bad-blk-sigops') @@ -1949,7 +1952,20 @@ class SegWitTest(BitcoinTestFramework): self.update_witness_block_with_transactions(block_5, [tx2]) test_witness_block(self.nodes[0], self.test_node, block_5, accepted=True) - # TODO: test p2sh sigop counting + p2sh_tx = CTransaction() + p2sh_tx.vin = [CTxIn(COutPoint(tx.txid_int, outputs + i), CScript([witness_script])) for i in range(p2sh_outputs)] + p2sh_tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) + block_6 = self.build_next_block() + self.update_witness_block_with_transactions(block_6, [p2sh_tx]) + test_witness_block(self.nodes[0], self.test_node, block_6, accepted=False, reason='bad-blk-sigops') + + # Add witness data to verify that the transaction's P2SH sigops are still counted. + p2sh_tx.vin.append(CTxIn(COutPoint(tx.txid_int, outputs - 2), b"")) + p2sh_tx.wit.vtxinwit = [CTxInWitness() for _ in p2sh_tx.vin] + p2sh_tx.wit.vtxinwit[-1].scriptWitness.stack = [witness_script_toomany] + block_7 = self.build_next_block() + self.update_witness_block_with_transactions(block_7, [p2sh_tx]) + test_witness_block(self.nodes[0], self.test_node, block_7, accepted=False, reason='bad-blk-sigops') # Cleanup and prep for next test self.utxo.pop(0)