From 847a262d78933c57e6062151ceef8aba17896264 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Fri, 19 Apr 2019 01:46:52 -0700 Subject: [PATCH] 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% --- txscript/standard.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/txscript/standard.go b/txscript/standard.go index 85b28582..5d5c6687 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -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 }