From c8332cc9a7ecb05812e25a9b3bac565c9656f701 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 16 Mar 2014 23:07:20 -0500 Subject: [PATCH] Correct num expected inputs calc for multisig. This commit corrects the number of expected inputs for a multi-sig script to include the additional item that is popped from the stack due to the OP_CHECKMULTISIG consensus bug (which is required and properly performed). Note this issue did NOT affect the consensus critical code and hence would not cause a chain fork. It did however, cause standard p2sh multisig txns to be rejected from the mempool as nonstandard. The tx rejected as non-standard which prompted this was spotted by @mbelshe on IRC. ok @owainga --- script.go | 12 +++++++----- script_test.go | 7 ++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/script.go b/script.go index 0854931b..11f0dab3 100644 --- a/script.go +++ b/script.go @@ -1128,11 +1128,13 @@ func expectedInputs(pops []parsedOpcode, class ScriptClass) int { return 1 case MultiSigTy: // Standard multisig has a push a small number for the number - // of sigs and number of keys. - // Check the first push instruction to see how many arguments - // are expected. typeOfScript already checked this so we know - // it'll be a small int. - return asSmallInt(pops[0].opcode) + // of sigs and number of keys. Check the first push instruction + // to see how many arguments are expected. typeOfScript already + // checked this so we know it'll be a small int. Also, due to + // the original bitcoind bug where OP_CHECKMULTISIG pops an + // additional item from the stack, add an extra expected input + // for the extra push that is required to compensate. + return asSmallInt(pops[0].opcode) + 1 case NullDataTy: fallthrough default: diff --git a/script_test.go b/script_test.go index d1eb604e..1fb6e7a3 100644 --- a/script_test.go +++ b/script_test.go @@ -1200,7 +1200,7 @@ var txTests = []txTest{ scriptInfo: btcscript.ScriptInfo{ PkScriptClass: btcscript.ScriptHashTy, NumInputs: 2, - ExpectedInputs: 1, + ExpectedInputs: 2, SigOps: 1, }, }, @@ -1780,6 +1780,7 @@ func TestScriptInfo(t *testing.T) { name: "multisig script", sigScript: []byte{btcscript.OP_TRUE, btcscript.OP_TRUE, btcscript.OP_TRUE, + btcscript.OP_0, // Extra arg for OP_CHECKMULTISIG bug }, pkScript: []byte{ btcscript.OP_3, btcscript.OP_DATA_33, @@ -1802,8 +1803,8 @@ func TestScriptInfo(t *testing.T) { bip16: true, scriptInfo: btcscript.ScriptInfo{ PkScriptClass: btcscript.MultiSigTy, - NumInputs: 3, - ExpectedInputs: 3, + NumInputs: 4, + ExpectedInputs: 4, SigOps: 3, }, },