Implemented LTE, GTE opcode (#260)

* Implemented LTE, GTE opcode
This commit is contained in:
dauTT 2019-04-03 00:43:52 +02:00 committed by decentralisedkev
parent 7bf4d691a9
commit 045db09af2
4 changed files with 131 additions and 26 deletions

View file

@ -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) {

View file

@ -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,

View file

@ -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()

View file

@ -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())
}