From e7f9415e4f5348c479d4f27ccc4c1767a50a4854 Mon Sep 17 00:00:00 2001 From: "Owain G. Ainsworth" Date: Thu, 25 Jul 2013 14:27:58 +0100 Subject: [PATCH] Return 0 for bad scripts from sigops code instead of an error. matches how bitcoind behaves. --- opcode_test.go | 30 ++---------------------------- script.go | 22 +++++++++++----------- script_test.go | 32 ++------------------------------ 3 files changed, 15 insertions(+), 69 deletions(-) diff --git a/opcode_test.go b/opcode_test.go index d0420e33..e5423d20 100644 --- a/opcode_test.go +++ b/opcode_test.go @@ -4004,20 +4004,7 @@ func TestDisasmStrings(t *testing.T) { // us coverage over a wider range of opcodes. func TestSigOps(t *testing.T) { for _, test := range detailedTests { - count, err := btcscript.GetSigOpCount(test.script) - if err != nil { - if err != test.disassemblyerr { - t.Errorf("%s: unexpected error. exp \"%v\""+ - "got \"%v\"", test.name, - test.disassemblyerr, err) - } - continue - } - if test.disassemblyerr != nil { - t.Errorf("%s: no error when expected \"%v\"", - test.name, test.disassemblyerr) - continue - } + count := btcscript.GetSigOpCount(test.script) if count != test.nSigOps { t.Errorf("%s: expected count of %d, got %d", test.name, test.nSigOps, count) @@ -4035,21 +4022,8 @@ func TestSigOps(t *testing.T) { // using real transactions to provide a bit more coverage. func TestPreciseSigOps(t *testing.T) { for _, test := range detailedTests { - count, err := btcscript.GetPreciseSigOpCount( + count := btcscript.GetPreciseSigOpCount( []byte{btcscript.OP_1}, test.script, false) - if err != nil { - if err != test.disassemblyerr { - t.Errorf("%s: unexpected error. exp \"%v\""+ - "got \"%v\"", test.name, - test.disassemblyerr, err) - } - continue - } - if test.disassemblyerr != nil { - t.Errorf("%s: no error when expected \"%v\"", - test.name, test.disassemblyerr) - continue - } if count != test.nPreciseSigOps { t.Errorf("%s: expected count of %d, got %d", test.name, test.nPreciseSigOps, count) diff --git a/script.go b/script.go index 75b2c867..4e7dcf91 100644 --- a/script.go +++ b/script.go @@ -763,51 +763,51 @@ func (s *Script) SetAltStack(data [][]byte) { // GetSigOpCount provides a quick count of the number of signature operations // in a script. a CHECKSIG operations counts for 1, and a CHECK_MULTISIG for 20. -func GetSigOpCount(script []byte) (int, error) { +func GetSigOpCount(script []byte) int { pops, err := parseScript(script) if err != nil { - return 0, err + return 0 } - return getSigOpCount(pops, false), nil + return getSigOpCount(pops, false) } // GetPreciseSigOpCount returns the number of signature operations in // scriptPubKey. If bip16 is true then scriptSig may be searched for the // Pay-To-Script-Hash script in order to find the precise number of signature // operations in the transaction. -func GetPreciseSigOpCount(scriptSig, scriptPubKey []byte, bip16 bool) (int, error) { +func GetPreciseSigOpCount(scriptSig, scriptPubKey []byte, bip16 bool) int { pops, err := parseScript(scriptPubKey) if err != nil { - return 0, err + return 0 } // non P2SH transactions just treated as normal. if !(bip16 && isScriptHash(pops)) { - return getSigOpCount(pops, true), nil + return getSigOpCount(pops, true) } // Ok so this is P2SH, get the contained script and count it.. sigPops, err := parseScript(scriptSig) if err != nil { - return 0, err + return 0 } if !isPushOnly(sigPops) || len(sigPops) == 0 { - return 0, nil + return 0 } shScript := sigPops[len(sigPops)-1].data // Means that sigPops is jus OP_1 - OP_16, no sigops there. if shScript == nil { - return 0, nil + return 0 } shPops, err := parseScript(shScript) if err != nil { - return 0, err + return 0 } - return getSigOpCount(shPops, true), nil + return getSigOpCount(shPops, true) } // getSigOpCount is the implementation function for counting the number of diff --git a/script_test.go b/script_test.go index 0e2751fc..1a420f66 100644 --- a/script_test.go +++ b/script_test.go @@ -21,7 +21,6 @@ type txTest struct { err error // Failure of Executre shouldFail bool // Execute should fail with nonspecified error. nSigOps int // result of GetPreciseSigOpsCount - sigOpsErr error // failure of GetPreciseSigOpsCount() } var txTests = []txTest{ @@ -1079,7 +1078,6 @@ var txTests = []txTest{ idx: 0, err: btcscript.StackErrShortScript, bip16: true, - sigOpsErr: btcscript.StackErrShortScript, }, txTest{ // sigscript changed so to be non pushonly. @@ -1247,22 +1245,9 @@ func TestGetPreciseSignOps(t *testing.T) { // First we go over the range of tests in testTx and count the sigops in // them. for _, test := range txTests { - count, err := btcscript.GetPreciseSigOpCount( + count := btcscript.GetPreciseSigOpCount( test.tx.TxIn[test.idx].SignatureScript, test.pkScript, test.bip16) - // all tx currently parse - if err != nil { - if err != test.sigOpsErr { - t.Errorf("%s: unexpected error. got \"%v\"", - test.name, err) - } - continue - } - if test.sigOpsErr != nil { - t.Errorf("%s: expected error \"%v\" but got success", - test.name, err) - continue - } if count != test.nSigOps { t.Errorf("%s: expected count of %d, got %d", test.name, test.nSigOps, count) @@ -1321,21 +1306,8 @@ func TestGetPreciseSignOps(t *testing.T) { btcscript.OP_EQUAL, } for _, test := range psocTests { - count, err := btcscript.GetPreciseSigOpCount( + count := btcscript.GetPreciseSigOpCount( test.scriptSig, pkScript, true) - // all tx currently parse - if err != nil { - if err != test.err { - t.Errorf("%s: unexpected error. got \"%v\" exp: \"%v\"", - test.name, err, test.err) - } - continue - } - if test.err != nil { - t.Errorf("%s: expected error \"%v\" got none", - test.name, test.err) - continue - } if count != test.nSigOps { t.Errorf("%s: expected count of %d, got %d", test.name, test.nSigOps, count)