From 05aa488a877faa2fabdac38e024f9f72aa3abe4a Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:11:38 -0500 Subject: [PATCH] txscript: Add benchmark for IsPayToPubKey --- txscript/bench_test.go | 15 +++++++++++++++ txscript/script.go | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/txscript/bench_test.go b/txscript/bench_test.go index 3b1ed40d..6415c595 100644 --- a/txscript/bench_test.go +++ b/txscript/bench_test.go @@ -129,3 +129,18 @@ func BenchmarkDisasmString(b *testing.B) { } } } + +// BenchmarkIsPubKeyScript benchmarks how long it takes to analyze a very large +// script to determine if it is a standard pay-to-pubkey script. +func BenchmarkIsPubKeyScript(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++ { + _ = IsPayToPubKey(script) + } +} diff --git a/txscript/script.go b/txscript/script.go index 3dfd82e1..c642069c 100644 --- a/txscript/script.go +++ b/txscript/script.go @@ -63,6 +63,16 @@ func isScriptHash(pops []parsedOpcode) bool { pops[2].opcode.value == OP_EQUAL } +// IsPayToPubKey returns true if the script is in the standard pay-to-pubkey +// (P2PK) format, false otherwise. +func IsPayToPubKey(script []byte) bool { + pops, err := parseScript(script) + if err != nil { + return false + } + return isPubkey(pops) +} + // IsPayToScriptHash returns true if the script is in the standard // pay-to-script-hash (P2SH) format, false otherwise. func IsPayToScriptHash(script []byte) bool {