interop/runtime: disable notifications in dynamic scripts

That are only entry scripts today. See neo-project/neo#2796.
This commit is contained in:
Roman Khimov 2022-07-27 14:49:53 +03:00
parent 1ff588a11b
commit e5c59f8ddd
2 changed files with 16 additions and 1 deletions

View file

@ -73,6 +73,10 @@ func Notify(ic *interop.Context) error {
if len(name) > MaxEventNameLen { if len(name) > MaxEventNameLen {
return fmt.Errorf("event name must be less than %d", 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 // But it has to be serializable, otherwise we either have some broken
// (recursive) structure inside or an interop item that can't be used // (recursive) structure inside or an interop item that can't be used
// outside of the interop subsystem anyway. // outside of the interop subsystem anyway.

View file

@ -12,6 +12,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/interop" "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/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "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/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm"
@ -133,9 +134,12 @@ func TestLog(t *testing.T) {
func TestNotify(t *testing.T) { func TestNotify(t *testing.T) {
h := random.Uint160() h := random.Uint160()
caller := random.Uint160()
exe, err := nef.NewFile([]byte{1})
require.NoError(t, err)
newIC := func(name string, args interface{}) *interop.Context { newIC := func(name string, args interface{}) *interop.Context {
ic := &interop.Context{VM: vm.New(), DAO: &dao.Simple{}} 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(args)
ic.VM.Estack().PushVal(name) ic.VM.Estack().PushVal(name)
return ic return ic
@ -144,6 +148,13 @@ func TestNotify(t *testing.T) {
ic := newIC(string(make([]byte, MaxEventNameLen+1)), stackitem.NewArray([]stackitem.Item{stackitem.Null{}})) ic := newIC(string(make([]byte, MaxEventNameLen+1)), stackitem.NewArray([]stackitem.Item{stackitem.Null{}}))
require.Error(t, Notify(ic)) 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) { t.Run("recursive struct", func(t *testing.T) {
arr := stackitem.NewArray([]stackitem.Item{stackitem.Null{}}) arr := stackitem.NewArray([]stackitem.Item{stackitem.Null{}})
arr.Append(arr) arr.Append(arr)