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 Roy Lee
parent 9cfaf49024
commit 918278a251

View file

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