Skip to content

Commit

Permalink
txscript: Convert to use non-parsed opcode disasm.
Browse files Browse the repository at this point in the history
This converts the engine's current program counter disasembly to make
use of the standalone disassembly function to remove the dependency on
the parsed opcode struct.

It also updates the tests accordingly.
  • Loading branch information
davecgh authored and cfromknecht committed Feb 5, 2021
1 parent f2ab844 commit 2258e94
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
7 changes: 5 additions & 2 deletions txscript/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"crypto/sha256"
"fmt"
"math/big"
"strings"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/wire"
Expand Down Expand Up @@ -331,8 +332,10 @@ func (vm *Engine) executeOpcode(pop *parsedOpcode) error {
// provided position in the script. It does no error checking and leaves that
// to the caller to provide a valid offset.
func (vm *Engine) disasm(scriptIdx int, scriptOff int) string {
return fmt.Sprintf("%02x:%04x: %s", scriptIdx, scriptOff,
vm.scripts[scriptIdx][scriptOff].print(false))
var buf strings.Builder
pop := vm.scripts[scriptIdx][scriptOff]
disasmOpcode(&buf, pop.opcode, pop.data, false)
return fmt.Sprintf("%02x:%04x: %s", scriptIdx, scriptOff, buf.String())
}

// validPC returns an error if the current script position is valid for
Expand Down
8 changes: 0 additions & 8 deletions txscript/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,14 +740,6 @@ func disasmOpcode(buf *strings.Builder, op *opcode, data []byte, compact bool) {
buf.WriteString(fmt.Sprintf(" 0x%02x", data))
}

// print returns a human-readable string representation of the opcode for use
// in script disassembly.
func (pop *parsedOpcode) print(compact bool) string {
var buf strings.Builder
disasmOpcode(&buf, pop.opcode, pop.data, compact)
return buf.String()
}

// bytes returns any data associated with the opcode encoded as it would be in
// a script. This is used for unparsing scripts from parsed opcodes.
func (pop *parsedOpcode) bytes() ([]byte, error) {
Expand Down
10 changes: 6 additions & 4 deletions txscript/opcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ func TestOpcodeDisasm(t *testing.T) {
expectedStr = "OP_UNKNOWN" + strconv.Itoa(opcodeVal)
}

pop := parsedOpcode{opcode: &opcodeArray[opcodeVal], data: data}
gotStr := pop.print(true)
var buf strings.Builder
disasmOpcode(&buf, &opcodeArray[opcodeVal], data, true)
gotStr := buf.String()
if gotStr != expectedStr {
t.Errorf("pop.print (opcode %x): Unexpected disasm "+
"string - got %v, want %v", opcodeVal, gotStr,
Expand Down Expand Up @@ -193,8 +194,9 @@ func TestOpcodeDisasm(t *testing.T) {
expectedStr = "OP_UNKNOWN" + strconv.Itoa(opcodeVal)
}

pop := parsedOpcode{opcode: &opcodeArray[opcodeVal], data: data}
gotStr := pop.print(false)
var buf strings.Builder
disasmOpcode(&buf, &opcodeArray[opcodeVal], data, false)
gotStr := buf.String()
if gotStr != expectedStr {
t.Errorf("pop.print (opcode %x): Unexpected disasm "+
"string - got %v, want %v", opcodeVal, gotStr,
Expand Down

0 comments on commit 2258e94

Please sign in to comment.