From efad66aee16ab62932e213868eb71f8b0a110f06 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sun, 5 Apr 2020 18:05:40 +0300 Subject: [PATCH] compiler: make Notify accept varargs --- pkg/compiler/codegen.go | 10 ++++++++-- pkg/compiler/syscall_test.go | 25 +++++++++++++++++++++++++ pkg/compiler/vm_test.go | 14 +++++++++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index ddd07a48e..fc4b49893 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -662,7 +662,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { // We can be sure builtins are of type *ast.Ident. c.convertBuiltin(n) case isSyscall(f): - c.convertSyscall(f.selector.Name, f.name) + c.convertSyscall(n, f.selector.Name, f.name) default: emit.Call(c.prog.BinWriter, opcode.CALL, f.label) } @@ -978,12 +978,18 @@ func (c *codegen) getByteArray(expr ast.Expr) []byte { } } -func (c *codegen) convertSyscall(api, name string) { +func (c *codegen) convertSyscall(expr *ast.CallExpr, api, name string) { api, ok := syscalls[api][name] if !ok { c.prog.Err = fmt.Errorf("unknown VM syscall api: %s", name) return } + switch name { + case "Notify": + numArgs := len(expr.Args) + emit.Int(c.prog.BinWriter, int64(numArgs)) + emit.Opcode(c.prog.BinWriter, opcode.PACK) + } emit.Syscall(c.prog.BinWriter, api) // This NOP instruction is basically not needed, but if we do, we have a diff --git a/pkg/compiler/syscall_test.go b/pkg/compiler/syscall_test.go index e60544fb5..1eaa53752 100644 --- a/pkg/compiler/syscall_test.go +++ b/pkg/compiler/syscall_test.go @@ -2,6 +2,10 @@ package compiler_test import ( "testing" + + "github.com/nspcc-dev/neo-go/pkg/vm" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestStoragePutGet(t *testing.T) { @@ -20,3 +24,24 @@ func TestStoragePutGet(t *testing.T) { ` eval(t, src, []byte("foo")) } + +func TestNotify(t *testing.T) { + src := `package foo + import "github.com/nspcc-dev/neo-go/pkg/interop/runtime" + func Main(arg int) { + runtime.Notify(arg, "sum", arg+1) + runtime.Notify() + runtime.Notify("single") + }` + + v, s := vmAndCompileInterop(t, src) + v.Estack().PushVal(11) + + require.NoError(t, v.Run()) + require.Equal(t, 3, len(s.events)) + + exp0 := []vm.StackItem{vm.NewBigIntegerItem(11), vm.NewByteArrayItem([]byte("sum")), vm.NewBigIntegerItem(12)} + assert.Equal(t, exp0, s.events[0].Value()) + assert.Equal(t, []vm.StackItem{}, s.events[1].Value()) + assert.Equal(t, []vm.StackItem{vm.NewByteArrayItem([]byte("single"))}, s.events[2].Value()) +} diff --git a/pkg/compiler/vm_test.go b/pkg/compiler/vm_test.go index 6d9d81e26..b97d14acd 100644 --- a/pkg/compiler/vm_test.go +++ b/pkg/compiler/vm_test.go @@ -53,6 +53,11 @@ func assertResult(t *testing.T, vm *vm.VM, result interface{}) { } func vmAndCompile(t *testing.T, src string) *vm.VM { + v, _ := vmAndCompileInterop(t, src) + return v +} + +func vmAndCompileInterop(t *testing.T, src string) (*vm.VM, *storagePlugin) { vm := vm.New() storePlugin := newStoragePlugin() @@ -61,12 +66,13 @@ func vmAndCompile(t *testing.T, src string) *vm.VM { b, err := compiler.Compile(strings.NewReader(src)) require.NoError(t, err) vm.Load(b) - return vm + return vm, storePlugin } type storagePlugin struct { mem map[string][]byte interops map[uint32]vm.InteropFunc + events []vm.StackItem } func newStoragePlugin() *storagePlugin { @@ -77,6 +83,7 @@ func newStoragePlugin() *storagePlugin { s.interops[vm.InteropNameToID([]byte("Neo.Storage.Get"))] = s.Get s.interops[vm.InteropNameToID([]byte("Neo.Storage.Put"))] = s.Put s.interops[vm.InteropNameToID([]byte("Neo.Storage.GetContext"))] = s.GetContext + s.interops[vm.InteropNameToID([]byte("Neo.Runtime.Notify"))] = s.Notify return s } @@ -89,6 +96,11 @@ func (s *storagePlugin) getInterop(id uint32) *vm.InteropFuncPrice { return nil } +func (s *storagePlugin) Notify(v *vm.VM) error { + s.events = append(s.events, v.Estack().Pop().Item()) + return nil +} + func (s *storagePlugin) Delete(vm *vm.VM) error { vm.Estack().Pop() key := vm.Estack().Pop().Bytes()