From c7fb4c3bdf9ad324a832acbceb9cdef32a513592 Mon Sep 17 00:00:00 2001 From: BlockChainDev Date: Fri, 15 Mar 2019 22:36:16 +0000 Subject: [PATCH] - Add `Add` OpCode - Add Opcode Function map --- pkg/vm/vm_ops.go | 7 +++++++ pkg/vm/vm_ops_maths.go | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 pkg/vm/vm_ops.go create mode 100644 pkg/vm/vm_ops_maths.go diff --git a/pkg/vm/vm_ops.go b/pkg/vm/vm_ops.go new file mode 100644 index 000000000..456552597 --- /dev/null +++ b/pkg/vm/vm_ops.go @@ -0,0 +1,7 @@ +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, +} diff --git a/pkg/vm/vm_ops_maths.go b/pkg/vm/vm_ops_maths.go new file mode 100644 index 000000000..f31aab6ec --- /dev/null +++ b/pkg/vm/vm_ops_maths.go @@ -0,0 +1,25 @@ +package vm + +import "github.com/CityOfZion/neo-go/pkg/vm/stack" + +// 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 { + operandA, err := ctx.Estack.PopInt() + if err != nil { + return err + } + operandB, err := ctx.Estack.PopInt() + if err != nil { + return err + } + res, err := operandA.Add(operandB) + if err != nil { + return err + } + + ctx.Estack.Push(res) + + return nil +}