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.

Note: this function was recently optimized in master, so the gains here
are less noticable than other optimizations.

The following is a before and after comparison of analyzing a large
script:

benchmark                    old ns/op     new ns/op     delta
BenchmarkIsUnspendable-8     656           584           -10.98%

benchmark                    old allocs     new allocs     delta
BenchmarkIsUnspendable-8     1              0              -100.00%

benchmark                    old bytes     new bytes     delta
BenchmarkIsUnspendable-8     1             0             -100.00%
This commit is contained in:
Dave Collins 2019-03-13 01:12:21 -05:00 committed by Olaoluwa Osuntokun
parent 77660b7fb0
commit e98b7c1a9a
No known key found for this signature in database
GPG key ID: 3BBD59E99B280306

View file

@ -917,15 +917,22 @@ func checkScriptParses(scriptVersion uint16, script []byte) error {
// IsUnspendable returns whether the passed public key script is unspendable, or // IsUnspendable returns whether the passed public key script is unspendable, or
// guaranteed to fail at execution. This allows inputs to be pruned instantly // guaranteed to fail at execution. This allows inputs to be pruned instantly
// when entering the UTXO set. // when entering the UTXO set.
//
// 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(pkScript []byte) bool { func IsUnspendable(pkScript []byte) bool {
// Not provably unspendable // The script is unspendable if starts with OP_RETURN or is guaranteed
if len(pkScript) == 0 { // to fail at execution due to being larger than the max allowed script
return false // size.
} switch {
firstOpcode, err := checkScriptTemplateParseable(pkScript, &opcodeArray) case len(pkScript) > 0 && pkScript[0] == OP_RETURN:
if err != nil { return true
case len(pkScript) > MaxScriptSize:
return true return true
} }
return firstOpcode != nil && *firstOpcode == OP_RETURN // The script is unspendable if it is guaranteed to fail at execution.
const scriptVersion = 0
return checkScriptParses(scriptVersion, pkScript) != nil
} }