refactor Add, Sub to return VMSTATE

add popTwoByteArrays helper function
This commit is contained in:
BlockChainDev 2019-03-16 22:09:34 +00:00
parent c7e32e7eb3
commit 17c53d1081

View file

@ -7,35 +7,39 @@ 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(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) error {
func Add(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) (Vmstate, error) {
operandA, operandB, err := popTwoIntegers(ctx)
if err != nil {
return FAULT, err
}
res, err := operandA.Add(operandB)
if err != nil {
return err
return FAULT, err
}
ctx.Estack.Push(res)
return nil
return NONE, 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(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) error {
func Sub(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) (Vmstate, error) {
operandA, operandB, err := popTwoIntegers(ctx)
if err != nil {
return FAULT, err
}
res, err := operandB.Sub(operandA)
if err != nil {
return err
return HALT, err
}
ctx.Estack.Push(res)
return nil
return NONE, nil
}
func popTwoIntegers(ctx *stack.Context) (*stack.Int, *stack.Int, error) {
@ -50,3 +54,17 @@ func popTwoIntegers(ctx *stack.Context) (*stack.Int, *stack.Int, error) {
return operandA, operandB, nil
}
func popTwoByteArrays(ctx *stack.Context) (*stack.ByteArray, *stack.ByteArray, error) {
// Pop first stack item and cast as byte array
ba1, err := ctx.Estack.PopByteArray()
if err != nil {
return nil, nil, err
}
// Pop second stack item and cast as byte array
ba2, err := ctx.Estack.PopByteArray()
if err != nil {
return nil, nil, err
}
return ba1, ba2, nil
}