Skip to content

Commit 50b09e8

Browse files
committed
Merge bitcoin/bitcoin#29615: test: fix accurate multisig sigop count (BIP16), add unit test
3e9c736 test: fix accurate multisig sigop count (BIP16), add unit test (Sebastian Falbesoner) Pull request description: In the course of reviewing #29589 I noticed the following buggy call-site of `CScriptOp.decode_op_n` in the CScript's `GetSigOpCount` method: https://github.com/bitcoin/bitcoin/blob/4cc99df44aec4d104590aee46cf18318e22a8568/test/functional/test_framework/script.py#L591-L593 This should be `lastOpcode` rather than `opcode`. The latter is either OP_CHECKMULTISIG or OP_CHECKMULTISIGVERIFY at this point, so `decode_op_n` would result in an error. Also, in `CScript.raw_iter`, we have to return the op as `CScriptOp` type instead of a bare integer, otherwise we can't call the decode method on it. To prevent this in the future, add some simple unit tests for `GetSigOpCount`. Note that this was unnoticed, as the code part was never hit so far in the test framework. ACKs for top commit: achow101: ACK 3e9c736 Christewart: ACK 3e9c736 rkrux: tACK [3e9c736](bitcoin/bitcoin@3e9c736) hernanmarino: tACK 3e9c736 Tree-SHA512: 51647bb6d462fbd101effd851afdbd6ad198c0567888cd4fdcac389a9fb4bd3d7e648095c6944fd8875d36272107ebaabdc62d0e2423289055588c12294d05a7
2 parents 3c88eac + 3e9c736 commit 50b09e8

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

test/functional/test_framework/script.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def raw_iter(self):
483483
i = 0
484484
while i < len(self):
485485
sop_idx = i
486-
opcode = self[i]
486+
opcode = CScriptOp(self[i])
487487
i += 1
488488

489489
if opcode > OP_PUSHDATA4:
@@ -590,7 +590,7 @@ def GetSigOpCount(self, fAccurate):
590590
n += 1
591591
elif opcode in (OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY):
592592
if fAccurate and (OP_1 <= lastOpcode <= OP_16):
593-
n += opcode.decode_op_n()
593+
n += lastOpcode.decode_op_n()
594594
else:
595595
n += 20
596596
lastOpcode = opcode
@@ -782,6 +782,20 @@ def test_cscriptnum_encoding(self):
782782
for value in values:
783783
self.assertEqual(CScriptNum.decode(CScriptNum.encode(CScriptNum(value))), value)
784784

785+
def test_legacy_sigopcount(self):
786+
# test repeated single sig ops
787+
for n_ops in range(1, 100, 10):
788+
for singlesig_op in (OP_CHECKSIG, OP_CHECKSIGVERIFY):
789+
singlesigs_script = CScript([singlesig_op]*n_ops)
790+
self.assertEqual(singlesigs_script.GetSigOpCount(fAccurate=False), n_ops)
791+
self.assertEqual(singlesigs_script.GetSigOpCount(fAccurate=True), n_ops)
792+
# test multisig op (including accurate counting, i.e. BIP16)
793+
for n in range(1, 16+1):
794+
for multisig_op in (OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY):
795+
multisig_script = CScript([CScriptOp.encode_op_n(n), multisig_op])
796+
self.assertEqual(multisig_script.GetSigOpCount(fAccurate=False), 20)
797+
self.assertEqual(multisig_script.GetSigOpCount(fAccurate=True), n)
798+
785799
def BIP341_sha_prevouts(txTo):
786800
return sha256(b"".join(i.prevout.serialize() for i in txTo.vin))
787801

0 commit comments

Comments
 (0)