test: fix accurate multisig sigop count (BIP16), add unit test

This commit is contained in:
Sebastian Falbesoner 2024-03-10 20:18:19 +01:00
parent 4cc99df44a
commit 3e9c736a26

View file

@ -483,7 +483,7 @@ class CScript(bytes):
i = 0
while i < len(self):
sop_idx = i
opcode = self[i]
opcode = CScriptOp(self[i])
i += 1
if opcode > OP_PUSHDATA4:
@ -590,7 +590,7 @@ class CScript(bytes):
n += 1
elif opcode in (OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY):
if fAccurate and (OP_1 <= lastOpcode <= OP_16):
n += opcode.decode_op_n()
n += lastOpcode.decode_op_n()
else:
n += 20
lastOpcode = opcode
@ -782,6 +782,20 @@ class TestFrameworkScript(unittest.TestCase):
for value in values:
self.assertEqual(CScriptNum.decode(CScriptNum.encode(CScriptNum(value))), value)
def test_legacy_sigopcount(self):
# test repeated single sig ops
for n_ops in range(1, 100, 10):
for singlesig_op in (OP_CHECKSIG, OP_CHECKSIGVERIFY):
singlesigs_script = CScript([singlesig_op]*n_ops)
self.assertEqual(singlesigs_script.GetSigOpCount(fAccurate=False), n_ops)
self.assertEqual(singlesigs_script.GetSigOpCount(fAccurate=True), n_ops)
# test multisig op (including accurate counting, i.e. BIP16)
for n in range(1, 16+1):
for multisig_op in (OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY):
multisig_script = CScript([CScriptOp.encode_op_n(n), multisig_op])
self.assertEqual(multisig_script.GetSigOpCount(fAccurate=False), 20)
self.assertEqual(multisig_script.GetSigOpCount(fAccurate=True), n)
def BIP341_sha_prevouts(txTo):
return sha256(b"".join(i.prevout.serialize() for i in txTo.vin))