Skip to content

Commit

Permalink
txscript: Optimize removeOpcodeRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
cfromknecht committed Feb 5, 2021
1 parent 1200b05 commit 897c0ec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
39 changes: 39 additions & 0 deletions txscript/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ func DisasmString(script []byte) (string, error) {

// removeOpcode will remove any opcode matching ``opcode'' from the opcode
// stream in pkscript
//
// DEPRECATED. Use removeOpcodeRaw instead.
func removeOpcode(pkscript []parsedOpcode, opcode byte) []parsedOpcode {
retScript := make([]parsedOpcode, 0, len(pkscript))
for _, pop := range pkscript {
Expand All @@ -279,6 +281,43 @@ func removeOpcode(pkscript []parsedOpcode, opcode byte) []parsedOpcode {
return retScript
}

// removeOpcodeRaw will return the script after removing any opcodes that match
// `opcode`. If the opcode does not appear in script, the original script will
// be returned unmodified. Otherwise, a new script will be allocated to contain
// the filtered script. This metehod assumes that the script parses
// successfully.
//
// 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 removeOpcodeRaw(script []byte, opcode byte) []byte {
// Avoid work when possible.
if len(script) == 0 {
return script
}

const scriptVersion = 0
var result []byte
var prevOffset int32

tokenizer := MakeScriptTokenizer(scriptVersion, script)
for tokenizer.Next() {
if tokenizer.Opcode() == opcode {
if result == nil {
result = make([]byte, 0, len(script))
result = append(result, script[:prevOffset]...)
}
} else if result != nil {
result = append(result, script[prevOffset:tokenizer.ByteIndex()]...)
}
prevOffset = tokenizer.ByteIndex()
}
if result == nil {
return script
}
return result
}

// isCanonicalPush returns true if the opcode is either not a push instruction
// or the data associated with the push instruction uses the smallest
// instruction to do the job. False otherwise.
Expand Down
7 changes: 3 additions & 4 deletions txscript/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3981,13 +3981,12 @@ func TestRemoveOpcodes(t *testing.T) {
// tstRemoveOpcode is a convenience function to parse the provided
// raw script, remove the passed opcode, then unparse the result back
// into a raw script.
const scriptVersion = 0
tstRemoveOpcode := func(script []byte, opcode byte) ([]byte, error) {
pops, err := parseScript(script)
if err != nil {
if err := checkScriptParses(scriptVersion, script); err != nil {
return nil, err
}
pops = removeOpcode(pops, opcode)
return unparseScript(pops)
return removeOpcodeRaw(script, opcode), nil
}

for _, test := range tests {
Expand Down

0 comments on commit 897c0ec

Please sign in to comment.