From e5c59f8dddc1b0798d9eaf2ea7b4089170d92a40 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 27 Jul 2022 14:49:53 +0300 Subject: [PATCH] interop/runtime: disable notifications in dynamic scripts That are only entry scripts today. See neo-project/neo#2796. --- pkg/core/interop/runtime/engine.go | 4 ++++ pkg/core/interop/runtime/engine_test.go | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/core/interop/runtime/engine.go b/pkg/core/interop/runtime/engine.go index 1223d878a..81dae7a2c 100644 --- a/pkg/core/interop/runtime/engine.go +++ b/pkg/core/interop/runtime/engine.go @@ -73,6 +73,10 @@ func Notify(ic *interop.Context) error { if len(name) > MaxEventNameLen { return fmt.Errorf("event name must be less than %d", MaxEventNameLen) } + if ic.VM.Context().NEF == nil { + return errors.New("notifications are not allowed in dynamic scripts") + } + // But it has to be serializable, otherwise we either have some broken // (recursive) structure inside or an interop item that can't be used // outside of the interop subsystem anyway. diff --git a/pkg/core/interop/runtime/engine_test.go b/pkg/core/interop/runtime/engine_test.go index fc3b99b85..4ddf628ae 100644 --- a/pkg/core/interop/runtime/engine_test.go +++ b/pkg/core/interop/runtime/engine_test.go @@ -12,6 +12,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" + "github.com/nspcc-dev/neo-go/pkg/smartcontract/nef" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm" @@ -133,9 +134,12 @@ func TestLog(t *testing.T) { func TestNotify(t *testing.T) { h := random.Uint160() + caller := random.Uint160() + exe, err := nef.NewFile([]byte{1}) + require.NoError(t, err) newIC := func(name string, args interface{}) *interop.Context { ic := &interop.Context{VM: vm.New(), DAO: &dao.Simple{}} - ic.VM.LoadScriptWithHash([]byte{1}, h, callflag.NoneFlag) + ic.VM.LoadNEFMethod(exe, caller, h, callflag.NoneFlag, true, 0, -1, nil) ic.VM.Estack().PushVal(args) ic.VM.Estack().PushVal(name) return ic @@ -144,6 +148,13 @@ func TestNotify(t *testing.T) { ic := newIC(string(make([]byte, MaxEventNameLen+1)), stackitem.NewArray([]stackitem.Item{stackitem.Null{}})) require.Error(t, Notify(ic)) }) + t.Run("dynamic script", func(t *testing.T) { + ic := &interop.Context{VM: vm.New(), DAO: &dao.Simple{}} + ic.VM.LoadScriptWithHash([]byte{1}, h, callflag.NoneFlag) + ic.VM.Estack().PushVal(stackitem.NewArray([]stackitem.Item{stackitem.Make(42)})) + ic.VM.Estack().PushVal("event") + require.Error(t, Notify(ic)) + }) t.Run("recursive struct", func(t *testing.T) { arr := stackitem.NewArray([]stackitem.Item{stackitem.Null{}}) arr.Append(arr)