diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 1793be031..766e43422 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -26,11 +26,12 @@ func NewVM(script []byte) *VM { // ExecuteOp will execute one opcode for a given context func (v *VM) ExecuteOp(op stack.Instruction, ctx *stack.Context) error { + handleOp, ok := opFunc[op] if !ok { return fmt.Errorf("unknown opcode entered %v", op) } - err := handleOp(ctx, &v.InvocationStack) + err := handleOp(op, ctx, &v.InvocationStack) if err != nil { return err } diff --git a/pkg/vm/vm_ops.go b/pkg/vm/vm_ops.go index 36b22e472..f2424d81a 100644 --- a/pkg/vm/vm_ops.go +++ b/pkg/vm/vm_ops.go @@ -2,7 +2,15 @@ package vm import "github.com/CityOfZion/neo-go/pkg/vm/stack" -var opFunc = map[stack.Instruction]func(ctx *stack.Context, istack *stack.Invocation) error{ - stack.ADD: Add, - stack.SUB: Sub, +var opFunc = map[stack.Instruction]func(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) error{ + stack.ADD: Add, + stack.SUB: Sub, + stack.PUSHBYTES1: PushNBytes, + stack.PUSHBYTES75: PushNBytes, +} + +func init() { + for i := int(stack.PUSHBYTES1); i <= int(stack.PUSHBYTES75); i++ { + opFunc[stack.Instruction(i)] = PushNBytes + } } diff --git a/pkg/vm/vm_ops_maths.go b/pkg/vm/vm_ops_maths.go index b49c11d3e..7d6afad41 100644 --- a/pkg/vm/vm_ops_maths.go +++ b/pkg/vm/vm_ops_maths.go @@ -7,7 +7,7 @@ import ( // Add adds two stack Items together. // Returns an error if either items cannot be casted to an integer // or if integers cannot be added together -func Add(ctx *stack.Context, istack *stack.Invocation) error { +func Add(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) error { operandA, operandB, err := popTwoIntegers(ctx) @@ -24,7 +24,7 @@ func Add(ctx *stack.Context, istack *stack.Invocation) error { // Sub subtracts two stack Items. // Returns an error if either items cannot be casted to an integer // or if integers cannot be subtracted together -func Sub(ctx *stack.Context, istack *stack.Invocation) error { +func Sub(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) error { operandA, operandB, err := popTwoIntegers(ctx) diff --git a/pkg/vm/vm_ops_stackmani.go b/pkg/vm/vm_ops_stackmani.go new file mode 100644 index 000000000..c510eb7db --- /dev/null +++ b/pkg/vm/vm_ops_stackmani.go @@ -0,0 +1,19 @@ +package vm + +import ( + "github.com/CityOfZion/neo-go/pkg/vm/stack" +) + +// Stack Manipulation Opcodes + +// PushNBytes will Read N Bytes from the script and push it onto the stack +func PushNBytes(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) error { + + val, err := ctx.ReadBytes(int(op)) + if err != nil { + return err + } + ba := stack.NewByteArray(val) + ctx.Estack.Push(ba) + return nil +}