From 045db09af24ddee70287105685f99d877dd45cf7 Mon Sep 17 00:00:00 2001 From: dauTT <30392990+dauTT@users.noreply.github.com> Date: Wed, 3 Apr 2019 00:43:52 +0200 Subject: [PATCH] Implemented LTE, GTE opcode (#260) * Implemented LTE, GTE opcode --- pkg/vm/stack/Int.go | 14 ++++++ pkg/vm/vm_ops.go | 2 + pkg/vm/vm_ops_maths.go | 52 +++++++++++++++++----- pkg/vm/vm_ops_maths_test.go | 89 ++++++++++++++++++++++++++++++------- 4 files changed, 131 insertions(+), 26 deletions(-) diff --git a/pkg/vm/stack/Int.go b/pkg/vm/stack/Int.go index 3226904ee..a10c099c7 100644 --- a/pkg/vm/stack/Int.go +++ b/pkg/vm/stack/Int.go @@ -100,6 +100,20 @@ func (i *Int) Value() *big.Int { return i.val } +// Lte returns a bool value from the comparison of two integers, a and b. +// value is true if a <= b. +// value is false if a > b. +func (i *Int) Lte(s *Int) bool { + return i.Value().Cmp(s.Value()) != 1 +} + +// Gte returns a bool value from the comparison of two integers, a and b. +// value is true if a >= b. +// value is false if a < b. +func (i *Int) Gte(s *Int) bool { + return i.Value().Cmp(s.Value()) != -1 +} + // Abs returns a stack integer whose underlying value is // the absolute value of the original stack integer. func (i *Int) Abs() (*Int, error) { diff --git a/pkg/vm/vm_ops.go b/pkg/vm/vm_ops.go index d50609462..0a9c6f173 100644 --- a/pkg/vm/vm_ops.go +++ b/pkg/vm/vm_ops.go @@ -10,7 +10,9 @@ var opFunc = map[stack.Instruction]stackInfo{ stack.BOOLAND: BoolAnd, stack.BOOLOR: BoolOr, stack.LT: Lt, + stack.LTE: Lte, stack.GT: Gt, + stack.GTE: Gte, stack.SHR: Shr, stack.SHL: Shl, stack.INC: Inc, diff --git a/pkg/vm/vm_ops_maths.go b/pkg/vm/vm_ops_maths.go index e23a7a4a0..9bab3e48e 100644 --- a/pkg/vm/vm_ops_maths.go +++ b/pkg/vm/vm_ops_maths.go @@ -317,17 +317,36 @@ func Negate(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, return NONE, 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 - } +// Lte pops two integers, a and b, off of the stack and pushes a boolean the stack +// whose value is true if a's value is less than or equal to b's value. +// Returns an error if either items cannot be casted to an integer +func Lte(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) { - return operandA, operandB, nil + operandA, operandB, err := popTwoIntegers(ctx) + if err != nil { + return FAULT, err + } + res := operandB.Lte(operandA) + + ctx.Estack.Push(stack.NewBoolean(res)) + + return NONE, nil +} + +// Gte pops two integers, a and b, off of the stack and pushes a boolean the stack +// whose value is true if a's value is greated than or equal to b's value. +// Returns an error if either items cannot be casted to an integer +func Gte(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) { + + operandA, operandB, err := popTwoIntegers(ctx) + if err != nil { + return FAULT, err + } + res := operandB.Gte(operandA) + + ctx.Estack.Push(stack.NewBoolean(res)) + + return NONE, nil } // Shl pops two integers, a and b, off of the stack and pushes an integer to the stack @@ -402,6 +421,19 @@ func Gt(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rsta return NONE, 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 +} + func popTwoByteArrays(ctx *stack.Context) (*stack.ByteArray, *stack.ByteArray, error) { // Pop first stack item and cast as byte array ba1, err := ctx.Estack.PopByteArray() diff --git a/pkg/vm/vm_ops_maths_test.go b/pkg/vm/vm_ops_maths_test.go index b8478f29b..14f83b1ec 100644 --- a/pkg/vm/vm_ops_maths_test.go +++ b/pkg/vm/vm_ops_maths_test.go @@ -385,6 +385,64 @@ func TestNegateOp(t *testing.T) { assert.Equal(t, int64(20), item.Value().Int64()) } +func TestLteOp(t *testing.T) { + + v := VM{} + + a, err := stack.NewInt(big.NewInt(10)) + assert.Nil(t, err) + + b, err := stack.NewInt(big.NewInt(10)) + assert.Nil(t, err) + + ctx := stack.NewContext([]byte{}) + ctx.Estack.Push(a).Push(b) + + // b is the first item pop. + // a is the second item pop. + // we perform a <= b and place + // the result on top of the evaluation + // stack + v.executeOp(stack.LTE, ctx) + + // Stack should have one item + assert.Equal(t, 1, ctx.Estack.Len()) + + item, err := ctx.Estack.PopBoolean() + assert.Nil(t, err) + + assert.Equal(t, true, item.Value()) +} + +func TestGteOp(t *testing.T) { + + v := VM{} + + a, err := stack.NewInt(big.NewInt(10)) + assert.Nil(t, err) + + b, err := stack.NewInt(big.NewInt(2)) + assert.Nil(t, err) + + ctx := stack.NewContext([]byte{}) + ctx.Estack.Push(a).Push(b) + + // b is the first item pop. + // a is the second item pop. + // we perform a >= b and place + // the result on top of the evaluation + // stack + v.executeOp(stack.GTE, ctx) + + // Stack should have one item + assert.Equal(t, 1, ctx.Estack.Len()) + + item, err := ctx.Estack.PopBoolean() + assert.Nil(t, err) + + assert.Equal(t, true, item.Value()) +} + func TestShlOp(t *testing.T) { v := VM{} @@ -455,48 +513,48 @@ func TestShrOp(t *testing.T) { func TestBoolAndOp(t *testing.T) { - v := VM{} + v := VM{} - a := stack.NewBoolean(true) + a := stack.NewBoolean(true) b := stack.NewBoolean(true) - ctx := stack.NewContext([]byte{}) + ctx := stack.NewContext([]byte{}) ctx.Estack.Push(a).Push(b) - v.executeOp(stack.BOOLAND, ctx) + v.executeOp(stack.BOOLAND, ctx) - // Stack should have one item + // Stack should have one item assert.Equal(t, 1, ctx.Estack.Len()) - item, err := ctx.Estack.PopBoolean() + item, err := ctx.Estack.PopBoolean() if err != nil { t.Fail() } - assert.Equal(t, true, item.Value()) + assert.Equal(t, true, item.Value()) } - func TestBoolOrOp(t *testing.T) { +func TestBoolOrOp(t *testing.T) { - v := VM{} + v := VM{} - a := stack.NewBoolean(false) + a := stack.NewBoolean(false) b := stack.NewBoolean(true) - ctx := stack.NewContext([]byte{}) + ctx := stack.NewContext([]byte{}) ctx.Estack.Push(a).Push(b) - v.executeOp(stack.BOOLOR, ctx) + v.executeOp(stack.BOOLOR, ctx) - // Stack should have one item + // Stack should have one item assert.Equal(t, 1, ctx.Estack.Len()) - item, err := ctx.Estack.PopBoolean() + item, err := ctx.Estack.PopBoolean() if err != nil { t.Fail() } - assert.Equal(t, true, item.Value()) + assert.Equal(t, true, item.Value()) } func TestLtOp(t *testing.T) { @@ -556,4 +614,3 @@ func TestGtOp(t *testing.T) { assert.Equal(t, true, item.Value()) } -