Skip to content

Commit

Permalink
txscript: Make isDisabled accept raw opcode.
Browse files Browse the repository at this point in the history
This converts the isDisabled function defined on a parsed opcode to a
standalone function which accepts an opcode as a byte instead in order
to make it more flexible for raw script analysis.

It also updates all callers accordingly.
  • Loading branch information
davecgh authored and cfromknecht committed Feb 5, 2021
1 parent 9351643 commit a3d3df3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
42 changes: 41 additions & 1 deletion txscript/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,52 @@ func (vm *Engine) isBranchExecuting() bool {
return vm.condStack[len(vm.condStack)-1] == OpCondTrue
}

// isOpcodeDisabled returns whether or not the opcode is disabled and thus is
// always bad to see in the instruction stream (even if turned off by a
// conditional).
func isOpcodeDisabled(opcode byte) bool {
switch opcode {
case OP_CAT:
return true
case OP_SUBSTR:
return true
case OP_LEFT:
return true
case OP_RIGHT:
return true
case OP_INVERT:
return true
case OP_AND:
return true
case OP_OR:
return true
case OP_XOR:
return true
case OP_2MUL:
return true
case OP_2DIV:
return true
case OP_MUL:
return true
case OP_DIV:
return true
case OP_MOD:
return true
case OP_LSHIFT:
return true
case OP_RSHIFT:
return true
default:
return false
}
}

// executeOpcode peforms execution on the passed opcode. It takes into account
// whether or not it is hidden by conditionals, but some rules still must be
// tested in this case.
func (vm *Engine) executeOpcode(pop *parsedOpcode) error {
// Disabled opcodes are fail on program counter.
if pop.isDisabled() {
if isOpcodeDisabled(pop.opcode.value) {
str := fmt.Sprintf("attempt to execute disabled opcode %s",
pop.opcode.name)
return scriptError(ErrDisabledOpcode, str)
Expand Down
39 changes: 0 additions & 39 deletions txscript/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,45 +619,6 @@ type parsedOpcode struct {
data []byte
}

// isDisabled returns whether or not the opcode is disabled and thus is always
// bad to see in the instruction stream (even if turned off by a conditional).
func (pop *parsedOpcode) isDisabled() bool {
switch pop.opcode.value {
case OP_CAT:
return true
case OP_SUBSTR:
return true
case OP_LEFT:
return true
case OP_RIGHT:
return true
case OP_INVERT:
return true
case OP_AND:
return true
case OP_OR:
return true
case OP_XOR:
return true
case OP_2MUL:
return true
case OP_2DIV:
return true
case OP_MUL:
return true
case OP_DIV:
return true
case OP_MOD:
return true
case OP_LSHIFT:
return true
case OP_RSHIFT:
return true
default:
return false
}
}

// checkParseableInScript checks whether or not the current opcode is able to be
// parsed at a certain position in a script.
// This returns the position of the next opcode to be parsed in the script.
Expand Down

0 comments on commit a3d3df3

Please sign in to comment.