From d64dbbbb5d808736c2bab49f06f536413193019f Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:11:17 -0500 Subject: [PATCH] txscript: Add benchmarks for IsMutlsigSigScript. --- txscript/bench_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++ txscript/standard.go | 18 ++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/txscript/bench_test.go b/txscript/bench_test.go index 49be4958c6..41d79ccba1 100644 --- a/txscript/bench_test.go +++ b/txscript/bench_test.go @@ -219,3 +219,51 @@ func BenchmarkIsMultisigScript(b *testing.B) { } } } + +// BenchmarkIsMultisigSigScript benchmarks how long it takes IsMultisigSigScript +// to analyze a very large script. +func BenchmarkIsMultisigSigScriptLarge(b *testing.B) { + script, err := genComplexScript() + if err != nil { + b.Fatalf("failed to create benchmark script: %v", err) + } + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if IsMultisigSigScript(script) { + b.Fatalf("script should NOT be reported as mutisig sig script") + } + } +} + +// BenchmarkIsMultisigSigScript benchmarks how long it takes IsMultisigSigScript +// to analyze both a 1-of-2 multisig public key script (which should be false) +// and a signature script comprised of a pay-to-script-hash 1-of-2 multisig +// redeem script (which should be true). +func BenchmarkIsMultisigSigScript(b *testing.B) { + multisigShortForm := "1 " + + "DATA_33 " + + "0x030478aaaa2be30772f1e69e581610f1840b3cf2fe7228ee0281cd599e5746f81e " + + "DATA_33 " + + "0x0284f4d078b236a9ff91661f8ffbe012737cd3507566f30fd97d25f2b23539f3cd " + + "2 CHECKMULTISIG" + pkScript := mustParseShortForm(multisigShortForm) + + sigHex := "0x304402205795c3ab6ba11331eeac757bf1fc9c34bef0c7e1a9c8bd5eebb8" + + "82f3b79c5838022001e0ab7b4c7662e4522dc5fa479e4b4133fa88c6a53d895dc1d5" + + "2eddc7bbcf2801 " + sigScript := mustParseShortForm("DATA_71 " + sigHex + "DATA_71 " + + multisigShortForm) + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + if IsMultisigSigScript(pkScript) { + b.Fatalf("script should NOT be reported as mutisig sig script") + } + if !IsMultisigSigScript(sigScript) { + b.Fatalf("script should be reported as a mutisig sig script") + } + } +} diff --git a/txscript/standard.go b/txscript/standard.go index 44a757bf56..13c82d2c36 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -361,6 +361,24 @@ func IsMultisigScript(script []byte) (bool, error) { return isMultisigScript(scriptVersion, script), nil } +// IsMultisigSigScript takes a script, parses it, then returns whether or +// not it is a multisignature script. +func IsMultisigSigScript(script []byte) bool { + if len(script) == 0 || script == nil { + return false + } + pops, err := parseScript(script) + if err != nil { + return false + } + subPops, err := parseScript(pops[len(pops)-1].data) + if err != nil { + return false + } + + return isMultiSig(subPops) +} + // isNullData returns true if the passed script is a null data transaction, // false otherwise. func isNullData(pops []parsedOpcode) bool {