From 280c0629307836bbbe094cb8a05ce7c0ed2db6a4 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:12:56 -0500 Subject: [PATCH] txscript: Convert to use non-parsed opcode disasm. 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. --- txscript/engine.go | 7 +++++-- txscript/opcode.go | 8 -------- txscript/opcode_test.go | 10 ++++++---- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/txscript/engine.go b/txscript/engine.go index 8f2d47a5..b8e303ea 100644 --- a/txscript/engine.go +++ b/txscript/engine.go @@ -8,6 +8,7 @@ package txscript import ( "fmt" "math/big" + "strings" "github.com/decred/dcrd/dcrec/secp256k1" "github.com/decred/dcrd/wire" @@ -252,8 +253,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 diff --git a/txscript/opcode.go b/txscript/opcode.go index 7caf09c9..d897dcf7 100644 --- a/txscript/opcode.go +++ b/txscript/opcode.go @@ -682,14 +682,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) { diff --git a/txscript/opcode_test.go b/txscript/opcode_test.go index ae46686a..87e2db87 100644 --- a/txscript/opcode_test.go +++ b/txscript/opcode_test.go @@ -142,8 +142,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, @@ -208,8 +209,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,