- Add Op to handleOP func signature

- Add PushNBytes OPcode
This commit is contained in:
BlockChainDev 2019-03-15 23:37:54 +00:00
parent 9a59755745
commit 80fd427517
4 changed files with 34 additions and 6 deletions

View file

@ -26,11 +26,12 @@ func NewVM(script []byte) *VM {
// ExecuteOp will execute one opcode for a given context // ExecuteOp will execute one opcode for a given context
func (v *VM) ExecuteOp(op stack.Instruction, ctx *stack.Context) error { func (v *VM) ExecuteOp(op stack.Instruction, ctx *stack.Context) error {
handleOp, ok := opFunc[op] handleOp, ok := opFunc[op]
if !ok { if !ok {
return fmt.Errorf("unknown opcode entered %v", op) return fmt.Errorf("unknown opcode entered %v", op)
} }
err := handleOp(ctx, &v.InvocationStack) err := handleOp(op, ctx, &v.InvocationStack)
if err != nil { if err != nil {
return err return err
} }

View file

@ -2,7 +2,15 @@ package vm
import "github.com/CityOfZion/neo-go/pkg/vm/stack" import "github.com/CityOfZion/neo-go/pkg/vm/stack"
var opFunc = map[stack.Instruction]func(ctx *stack.Context, istack *stack.Invocation) error{ var opFunc = map[stack.Instruction]func(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) error{
stack.ADD: Add, stack.ADD: Add,
stack.SUB: Sub, 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
}
} }

View file

@ -7,7 +7,7 @@ import (
// Add adds two stack Items together. // Add adds two stack Items together.
// Returns an error if either items cannot be casted to an integer // Returns an error if either items cannot be casted to an integer
// or if integers cannot be added together // 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) operandA, operandB, err := popTwoIntegers(ctx)
@ -24,7 +24,7 @@ func Add(ctx *stack.Context, istack *stack.Invocation) error {
// Sub subtracts two stack Items. // Sub subtracts two stack Items.
// Returns an error if either items cannot be casted to an integer // Returns an error if either items cannot be casted to an integer
// or if integers cannot be subtracted together // 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) operandA, operandB, err := popTwoIntegers(ctx)

View file

@ -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
}