From 59de72f44626971cb441d845d9ea5929e2249bda Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 5 Sep 2019 17:20:53 +0300 Subject: [PATCH] vm: implement INVERT instruction --- pkg/vm/vm.go | 5 +++++ pkg/vm/vm_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index dfc172fe2..e5eeb15d8 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -402,6 +402,11 @@ func (v *VM) execute(ctx *Context, op Instruction) { v.estack.PushVal(reflect.DeepEqual(a,b)) // Bit operations. + case INVERT: + // inplace + a := v.estack.Peek(0).BigInt() + a.Not(a) + case AND: b := v.estack.Pop().BigInt() a := v.estack.Pop().BigInt() diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 8d13973ba..7abb7bc72 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -447,6 +447,40 @@ func TestXDROPgood(t *testing.T) { assert.Equal(t, int64(1), vm.estack.Peek(1).BigInt().Int64()) } +func TestINVERTbadNoitem(t *testing.T) { + prog := makeProgram(INVERT) + vm := load(prog) + vm.Run() + assert.Equal(t, true, vm.state.HasFlag(faultState)) +} + +func TestINVERTgood1(t *testing.T) { + prog := makeProgram(INVERT) + vm := load(prog) + vm.estack.PushVal(0) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, int64(-1), vm.estack.Peek(0).BigInt().Int64()) +} + +func TestINVERTgood2(t *testing.T) { + prog := makeProgram(INVERT) + vm := load(prog) + vm.estack.PushVal(-1) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, int64(0), vm.estack.Peek(0).BigInt().Int64()) +} + +func TestINVERTgood3(t *testing.T) { + prog := makeProgram(INVERT) + vm := load(prog) + vm.estack.PushVal(0x69) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, int64(-0x6A), vm.estack.Peek(0).BigInt().Int64()) +} + func makeProgram(opcodes ...Instruction) []byte { prog := make([]byte, len(opcodes)+1) // RET for i := 0; i < len(opcodes); i++ {