From 0e021a9564e66d898ab5c7ae6f0d407fa8384ee2 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:12:06 -0500 Subject: [PATCH] txscript: Optimize ContainsStakeOpCodes. This converts the ContainsStakeOpCodes function to make use of the new tokenizer instead of the far less efficient parseScript thereby significantly optimizing the function. The following is a before and after comparison of analyzing a large script: benchmark old ns/op new ns/op delta ------------------------------------------------------------------ BenchmarkContainsStakeOpCodes 134599 968 -99.28% benchmark old allocs new allocs delta ------------------------------------------------------------------ BenchmarkContainsStakeOpCodes 1 0 -100.00% benchmark old bytes new bytes delta ------------------------------------------------------------------ BenchmarkContainsStakeOpCodes 466944 0 -100.00% --- txscript/standard.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/txscript/standard.go b/txscript/standard.go index 461719f9..d6ac4536 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -753,19 +753,20 @@ func getStakeOutSubscript(pkScript []byte) []byte { // ContainsStakeOpCodes returns whether or not a pkScript contains stake tagging // OP codes. +// +// 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. func ContainsStakeOpCodes(pkScript []byte) (bool, error) { - shPops, err := parseScript(pkScript) - if err != nil { - return false, err - } - - for _, pop := range shPops { - if isStakeOpcode(pop.opcode.value) { + const scriptVersion = 0 + tokenizer := MakeScriptTokenizer(scriptVersion, pkScript) + for tokenizer.Next() { + if isStakeOpcode(tokenizer.Opcode()) { return true, nil } } - return false, nil + return false, tokenizer.Err() } // CalcScriptInfo returns a structure providing data about the provided script