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.
This commit is contained in:
Dave Collins 2019-03-13 01:12:56 -05:00 committed by Olaoluwa Osuntokun
parent 710bd5646e
commit 54036e8bab
No known key found for this signature in database
GPG key ID: 3BBD59E99B280306
3 changed files with 11 additions and 14 deletions

View file

@ -10,6 +10,7 @@ import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"math/big" "math/big"
"strings"
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
@ -331,8 +332,10 @@ func (vm *Engine) executeOpcode(pop *parsedOpcode) error {
// provided position in the script. It does no error checking and leaves that // provided position in the script. It does no error checking and leaves that
// to the caller to provide a valid offset. // to the caller to provide a valid offset.
func (vm *Engine) disasm(scriptIdx int, scriptOff int) string { func (vm *Engine) disasm(scriptIdx int, scriptOff int) string {
return fmt.Sprintf("%02x:%04x: %s", scriptIdx, scriptOff, var buf strings.Builder
vm.scripts[scriptIdx][scriptOff].print(false)) 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 // validPC returns an error if the current script position is valid for

View file

@ -740,14 +740,6 @@ func disasmOpcode(buf *strings.Builder, op *opcode, data []byte, compact bool) {
buf.WriteString(fmt.Sprintf(" 0x%02x", data)) 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 // 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. // a script. This is used for unparsing scripts from parsed opcodes.
func (pop *parsedOpcode) bytes() ([]byte, error) { func (pop *parsedOpcode) bytes() ([]byte, error) {

View file

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