From abc3b46f1ce9e3b7a9ad575f8c3ffbdaf8b047a4 Mon Sep 17 00:00:00 2001 From: BlockChainDev Date: Fri, 15 Mar 2019 22:54:52 +0000 Subject: [PATCH] Add popTwoIntegers convenience func --- pkg/vm/vm_ops_maths.go | 45 +++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/pkg/vm/vm_ops_maths.go b/pkg/vm/vm_ops_maths.go index f31aab6ec..b49c11d3e 100644 --- a/pkg/vm/vm_ops_maths.go +++ b/pkg/vm/vm_ops_maths.go @@ -1,19 +1,16 @@ package vm -import "github.com/CityOfZion/neo-go/pkg/vm/stack" +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 - } + + operandA, operandB, err := popTwoIntegers(ctx) + res, err := operandA.Add(operandB) if err != nil { return err @@ -23,3 +20,33 @@ func Add(ctx *stack.Context, istack *stack.Invocation) error { return nil } + +// 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 { + + operandA, operandB, err := popTwoIntegers(ctx) + + res, err := operandB.Sub(operandA) + if err != nil { + return err + } + + ctx.Estack.Push(res) + + return nil +} + +func popTwoIntegers(ctx *stack.Context) (*stack.Int, *stack.Int, error) { + operandA, err := ctx.Estack.PopInt() + if err != nil { + return nil, nil, err + } + operandB, err := ctx.Estack.PopInt() + if err != nil { + return nil, nil, err + } + + return operandA, operandB, nil +}