From 4d31d1599dc351183246aa4e85c9d77b199105f9 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:11:15 -0500 Subject: [PATCH] txscript: Add benchmarks for IsMutlsigScript. --- txscript/bench_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++ txscript/standard.go | 21 ++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/txscript/bench_test.go b/txscript/bench_test.go index e47cf21f..49be4958 100644 --- a/txscript/bench_test.go +++ b/txscript/bench_test.go @@ -174,3 +174,48 @@ func BenchmarkIsPayToScriptHash(b *testing.B) { _ = IsPayToScriptHash(script) } } + +// BenchmarkIsMultisigScriptLarge benchmarks how long it takes IsMultisigScript +// to analyze a very large script. +func BenchmarkIsMultisigScriptLarge(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++ { + isMultisig, err := IsMultisigScript(script) + if err != nil { + b.Fatalf("unexpected err: %v", err) + } + if isMultisig { + b.Fatalf("script should NOT be reported as mutisig script") + } + } +} + +// BenchmarkIsMultisigScript benchmarks how long it takes IsMultisigScript to +// analyze a 1-of-2 multisig public key script. +func BenchmarkIsMultisigScript(b *testing.B) { + multisigShortForm := "1 " + + "DATA_33 " + + "0x030478aaaa2be30772f1e69e581610f1840b3cf2fe7228ee0281cd599e5746f81e " + + "DATA_33 " + + "0x0284f4d078b236a9ff91661f8ffbe012737cd3507566f30fd97d25f2b23539f3cd " + + "2 CHECKMULTISIG" + pkScript := mustParseShortForm(multisigShortForm) + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + isMultisig, err := IsMultisigScript(pkScript) + if err != nil { + b.Fatalf("unexpected err: %v", err) + } + if !isMultisig { + b.Fatalf("script should be reported as a mutisig script") + } + } +} diff --git a/txscript/standard.go b/txscript/standard.go index a02f8723..25483aeb 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -248,6 +248,27 @@ func isMultiSig(pops []parsedOpcode) bool { return true } +// IsMultisigScript returns whether or not the passed script is a standard +// multisignature script. +// +// NOTE: This function is only valid for version 0 scripts. Since the function +// does not accept a script version, the results are undefined for other script +// versions. +// +// The error is DEPRECATED and will be removed in the major version bump. +func IsMultisigScript(script []byte) (bool, error) { + if len(script) == 0 || script == nil { + return false, nil + } + + pops, err := parseScript(script) + if err != nil { + return false, err + } + + return isMultiSig(pops), nil +} + // isNullData returns true if the passed script is a null data transaction, // false otherwise. func isNullData(pops []parsedOpcode) bool {