mirror of
https://github.com/FlipsideCrypto/dcrd.git
synced 2026-02-06 10:56:47 +00:00
txscript: Optimize ContainsStakeOpCodes.
This converts the ContainsStakeOpCodes function to make use of the new tokenizer instead of the far less efficient parseScript thereby significantly optimizing the function. The following is a before and after comparison of analyzing a large script: benchmark old ns/op new ns/op delta ------------------------------------------------------------------ BenchmarkContainsStakeOpCodes 134599 968 -99.28% benchmark old allocs new allocs delta ------------------------------------------------------------------ BenchmarkContainsStakeOpCodes 1 0 -100.00% benchmark old bytes new bytes delta ------------------------------------------------------------------ BenchmarkContainsStakeOpCodes 466944 0 -100.00%
This commit is contained in:
parent
bfab5dbb93
commit
0e021a9564
@ -753,19 +753,20 @@ func getStakeOutSubscript(pkScript []byte) []byte {
|
||||
|
||||
// ContainsStakeOpCodes returns whether or not a pkScript contains stake tagging
|
||||
// OP codes.
|
||||
//
|
||||
// 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 ContainsStakeOpCodes(pkScript []byte) (bool, error) {
|
||||
shPops, err := parseScript(pkScript)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, pop := range shPops {
|
||||
if isStakeOpcode(pop.opcode.value) {
|
||||
const scriptVersion = 0
|
||||
tokenizer := MakeScriptTokenizer(scriptVersion, pkScript)
|
||||
for tokenizer.Next() {
|
||||
if isStakeOpcode(tokenizer.Opcode()) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
return false, tokenizer.Err()
|
||||
}
|
||||
|
||||
// CalcScriptInfo returns a structure providing data about the provided script
|
||||
|
||||
Loading…
Reference in New Issue
Block a user