txscript: Optimize typeOfScript witness-pubkey-hash

This continues the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of witness pubkey hash scripts
to use raw script analysis and the new tokenizer.

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

benchmark                          old ns/op     new ns/op     delta
BenchmarkIsWitnessPubKeyHash-8     61688         62839         +1.87%

benchmark                          old allocs     new allocs     delta
BenchmarkIsWitnessPubKeyHash-8     1              1              +0.00%

benchmark                          old bytes     new bytes     delta
BenchmarkIsWitnessPubKeyHash-8     311299        311299        +0.00%
This commit is contained in:
Conner Fromknecht 2019-04-19 01:46:52 -07:00 committed by Olaoluwa Osuntokun
parent 78046b3815
commit 847a262d78
No known key found for this signature in database
GPG key ID: 3BBD59E99B280306

View file

@ -355,6 +355,7 @@ func extractWitnessPubKeyHash(script []byte) []byte {
return script[2:22] return script[2:22]
} }
return nil return nil
} }
@ -438,6 +439,8 @@ func typeOfScript(scriptVersion uint16, script []byte) ScriptClass {
return PubKeyHashTy return PubKeyHashTy
case isScriptHashScript(script): case isScriptHashScript(script):
return ScriptHashTy return ScriptHashTy
case isWitnessPubKeyHashScript(script):
return WitnessV0PubKeyHashTy
case isMultisigScript(scriptVersion, script): case isMultisigScript(scriptVersion, script):
return MultiSigTy return MultiSigTy
case isNullDataScript(scriptVersion, script): case isNullDataScript(scriptVersion, script):
@ -449,9 +452,7 @@ func typeOfScript(scriptVersion uint16, script []byte) ScriptClass {
return NonStandardTy return NonStandardTy
} }
if isWitnessPubKeyHash(pops) { if isWitnessScriptHash(pops) {
return WitnessV0PubKeyHashTy
} else if isWitnessScriptHash(pops) {
return WitnessV0ScriptHashTy return WitnessV0ScriptHashTy
} }