2019-03-15 22:36:16 +00:00
|
|
|
package vm
|
|
|
|
|
2019-03-15 22:54:52 +00:00
|
|
|
import (
|
2019-03-26 23:15:13 +00:00
|
|
|
"math/big"
|
|
|
|
|
2019-03-15 22:54:52 +00:00
|
|
|
"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-26 21:19:41 +00:00
|
|
|
return FAULT, 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
|
|
|
|
2019-03-26 23:15:13 +00:00
|
|
|
// Inc increments the stack Item's value by 1.
|
|
|
|
// Returns an error if the item cannot be casted to an integer
|
2019-03-28 18:30:36 +00:00
|
|
|
// or if 1 cannot be added to the item.
|
2019-03-26 23:15:13 +00:00
|
|
|
func Inc(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
|
|
|
|
|
|
|
i, err := ctx.Estack.PopInt()
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
one, err := stack.NewInt(big.NewInt(1))
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := i.Add(one)
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Estack.Push(res)
|
|
|
|
|
|
|
|
return NONE, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dec decrements the stack Item's value by 1.
|
|
|
|
// Returns an error if the item cannot be casted to an integer
|
2019-03-28 18:30:36 +00:00
|
|
|
// or if 1 cannot be subtracted to the item.
|
2019-03-26 23:15:13 +00:00
|
|
|
func Dec(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
|
|
|
|
|
|
|
i, err := ctx.Estack.PopInt()
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
one, err := stack.NewInt(big.NewInt(1))
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := i.Sub(one)
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Estack.Push(res)
|
|
|
|
|
|
|
|
return NONE, nil
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:26:55 +00:00
|
|
|
// Abs pops an integer off of the stack and pushes its absolute value onto the stack.
|
|
|
|
// Returns an error if the popped value is not an integer or if the absolute value cannot be taken
|
|
|
|
func Abs(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
|
|
|
|
|
|
|
i, err := ctx.Estack.PopInt()
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
a, err := i.Abs()
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Estack.Push(a)
|
|
|
|
|
|
|
|
return NONE, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not flips the stack Item's value.
|
|
|
|
// If the value is True, it is flipped to False and viceversa.
|
|
|
|
func Not(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
|
|
|
|
|
|
|
b, err := ctx.Estack.PopBoolean()
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Estack.Push(b.Not())
|
|
|
|
|
|
|
|
return NONE, nil
|
|
|
|
}
|
|
|
|
|
2019-03-28 18:30:36 +00:00
|
|
|
// Sign puts the sign of the top stack Item on top of the stack.
|
|
|
|
// If value is negative, put -1;
|
|
|
|
// If positive, put 1;
|
|
|
|
// If value is zero, put 0.
|
|
|
|
func Sign(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
|
|
|
|
|
|
|
i, err := ctx.Estack.PopInt()
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := int64(i.Value().Sign())
|
|
|
|
sign, err := stack.NewInt(big.NewInt(s))
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Estack.Push(sign)
|
|
|
|
|
|
|
|
return NONE, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Negate flips the sign of the stack Item.
|
|
|
|
func Negate(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
|
|
|
|
|
|
|
i, err := ctx.Estack.PopInt()
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
a := big.NewInt(0).Neg(i.Value())
|
|
|
|
b, err := stack.NewInt(a)
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Estack.Push(b)
|
|
|
|
|
|
|
|
return NONE, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|