From bb365f221ffae7efcdd54567a63b61f21c692a5a Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:12:21 -0500 Subject: [PATCH] txscript: Optimize IsUnspendable. This converts the IsUnspendable function to make use of a combination of raw script analysis and the new tokenizer instead of the far less efficient parseScript thereby significantly optimizing the function. It is important to note that this new implementation intentionally has a semantic difference from the existing implementation in that it will now report scripts that are larger than the max allowed script size are unspendable as well. Finally, the comment is modified to explicitly call out the script version semantics. The following is a before and after comparison of analyzing a large script: benchmark old ns/op new ns/op delta ----------------------------------------------------------- BenchmarkIsUnspendable 149899 860 -99.43% benchmark old allocs new allocs delta ----------------------------------------------------------- BenchmarkIsUnspendable 1 0 -100.00% benchmark old bytes new bytes delta ----------------------------------------------------------- BenchmarkIsUnspendable 466945 0 -100.00% --- txscript/script.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/txscript/script.go b/txscript/script.go index cedc85b0..845c4a4d 100644 --- a/txscript/script.go +++ b/txscript/script.go @@ -481,15 +481,20 @@ func checkScriptParses(scriptVersion uint16, script []byte) error { // IsUnspendable returns whether the passed public key script is unspendable, or // guaranteed to fail at execution. This allows inputs to be pruned instantly // when entering the UTXO set. In Decred, all zero value outputs are unspendable. +// +// 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 IsUnspendable(amount int64, pkScript []byte) bool { - if amount == 0 { + // The script is unspendable if starts with OP_RETURN or is guaranteed to + // fail at execution due to being larger than the max allowed script size. + if amount == 0 || len(pkScript) > MaxScriptSize || len(pkScript) > 0 && + pkScript[0] == OP_RETURN { + return true } - pops, err := parseScript(pkScript) - if err != nil { - return true - } - - return len(pops) > 0 && pops[0].opcode.value == OP_RETURN + // The script is unspendable if it is guaranteed to fail at execution. + const scriptVersion = 0 + return checkScriptParses(scriptVersion, pkScript) != nil }