Skip to content

Commit 3e9c736

Browse files
committed
test: fix accurate multisig sigop count (BIP16), add unit test
1 parent 4cc99df commit 3e9c736

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)