2019-03-15 22:36:16 +00:00
|
|
|
package vm
|
|
|
|
|
2019-03-15 22:54:52 +00:00
|
|
|
import (
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/stack"
|
|
|
|
)
|
2019-03-15 22:36:16 +00:00
|
|
|
|
|
|
|
// 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
|
2019-03-18 21:14:03 +00:00
|
|
|
func Add(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
2019-03-15 22:54:52 +00:00
|
|
|
|
|
|
|
operandA, operandB, err := popTwoIntegers(ctx)
|
2019-03-16 22:09:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
2019-03-15 22:54:52 +00:00
|
|
|
res, err := operandA.Add(operandB)
|
2019-03-15 22:36:16 +00:00
|
|
|
if err != nil {
|
2019-03-16 22:09:34 +00:00
|
|
|
return FAULT, err
|
2019-03-15 22:36:16 +00:00
|
|
|
}
|
2019-03-15 22:54:52 +00:00
|
|
|
|
|
|
|
ctx.Estack.Push(res)
|
|
|
|
|
2019-03-16 22:09:34 +00:00
|
|
|
return NONE, nil
|
2019-03-15 22:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sub subtracts two stack Items.
|
|
|
|
// Returns an error if either items cannot be casted to an integer
|
|
|
|
// or if integers cannot be subtracted together
|
2019-03-18 21:14:03 +00:00
|
|
|
func Sub(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
2019-03-15 22:54:52 +00:00
|
|
|
|
|
|
|
operandA, operandB, err := popTwoIntegers(ctx)
|
2019-03-16 22:09:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
2019-03-15 22:54:52 +00:00
|
|
|
res, err := operandB.Sub(operandA)
|
2019-03-15 22:36:16 +00:00
|
|
|
if err != nil {
|
2019-03-16 22:09:34 +00:00
|
|
|
return HALT, err
|
2019-03-15 22:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Estack.Push(res)
|
|
|
|
|
2019-03-16 22:09:34 +00:00
|
|
|
return NONE, nil
|
2019-03-15 22:36:16 +00:00
|
|
|
}
|
2019-03-15 22:54:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-03-16 22:09:34 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|