From deb1f6d3d82cc37675aa054b288cbc35db389f02 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 16 May 2022 11:19:15 +0300 Subject: [PATCH] core: emit notification events in a separate method --- pkg/core/interop/context.go | 9 +++++++++ pkg/core/interop/runtime/engine.go | 8 +------- pkg/core/native/designate.go | 13 ++++--------- pkg/core/native/management.go | 7 +------ pkg/core/native/native_nep17.go | 15 +++++---------- pkg/core/native/oracle.go | 28 ++++++++++------------------ 6 files changed, 30 insertions(+), 50 deletions(-) diff --git a/pkg/core/interop/context.go b/pkg/core/interop/context.go index e6d2c30e6..9444fd3d4 100644 --- a/pkg/core/interop/context.go +++ b/pkg/core/interop/context.go @@ -380,3 +380,12 @@ func (ic *Context) IsHardforkEnabled(hf config.Hardfork) bool { } return len(ic.Hardforks) == 0 // Enable each hard-fork by default. } + +// AddNotification creates notification event and appends it to the notification list. +func (ic *Context) AddNotification(hash util.Uint160, name string, item *stackitem.Array) { + ic.Notifications = append(ic.Notifications, state.NotificationEvent{ + ScriptHash: hash, + Name: name, + Item: item, + }) +} diff --git a/pkg/core/interop/runtime/engine.go b/pkg/core/interop/runtime/engine.go index b80bb8a98..f34358479 100644 --- a/pkg/core/interop/runtime/engine.go +++ b/pkg/core/interop/runtime/engine.go @@ -6,7 +6,6 @@ import ( "math/big" "github.com/nspcc-dev/neo-go/pkg/core/interop" - "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "go.uber.org/zap" ) @@ -69,12 +68,7 @@ func Notify(ic *interop.Context) error { if len(bytes) > MaxNotificationSize { return fmt.Errorf("notification size shouldn't exceed %d", MaxNotificationSize) } - ne := state.NotificationEvent{ - ScriptHash: ic.VM.GetCurrentScriptHash(), - Name: name, - Item: stackitem.DeepCopy(stackitem.NewArray(args)).(*stackitem.Array), - } - ic.Notifications = append(ic.Notifications, ne) + ic.AddNotification(ic.VM.GetCurrentScriptHash(), name, stackitem.DeepCopy(stackitem.NewArray(args)).(*stackitem.Array)) return nil } diff --git a/pkg/core/native/designate.go b/pkg/core/native/designate.go index d4d92cdf6..e843031ef 100644 --- a/pkg/core/native/designate.go +++ b/pkg/core/native/designate.go @@ -15,7 +15,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/native/noderoles" - "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/stateroot" "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/crypto/hash" @@ -386,14 +385,10 @@ func (s *Designate) DesignateAsRole(ic *interop.Context, r noderoles.Role, pubs return fmt.Errorf("failed to update Designation role data cache: %w", err) } - ic.Notifications = append(ic.Notifications, state.NotificationEvent{ - ScriptHash: s.Hash, - Name: DesignationEventName, - Item: stackitem.NewArray([]stackitem.Item{ - stackitem.NewBigInteger(big.NewInt(int64(r))), - stackitem.NewBigInteger(big.NewInt(int64(ic.Block.Index))), - }), - }) + ic.AddNotification(s.Hash, DesignationEventName, stackitem.NewArray([]stackitem.Item{ + stackitem.NewBigInteger(big.NewInt(int64(r))), + stackitem.NewBigInteger(big.NewInt(int64(ic.Block.Index))), + })) return nil } diff --git a/pkg/core/native/management.go b/pkg/core/native/management.go index 26c8b9add..92de9015d 100644 --- a/pkg/core/native/management.go +++ b/pkg/core/native/management.go @@ -607,12 +607,7 @@ func (m *Management) getNextContractID(d *dao.Simple) (int32, error) { } func (m *Management) emitNotification(ic *interop.Context, name string, hash util.Uint160) { - ne := state.NotificationEvent{ - ScriptHash: m.Hash, - Name: name, - Item: stackitem.NewArray([]stackitem.Item{addrToStackItem(&hash)}), - } - ic.Notifications = append(ic.Notifications, ne) + ic.AddNotification(m.Hash, name, stackitem.NewArray([]stackitem.Item{addrToStackItem(&hash)})) } func checkScriptAndMethods(script []byte, methods []manifest.Method) error { diff --git a/pkg/core/native/native_nep17.go b/pkg/core/native/native_nep17.go index 57538590e..e0963530e 100644 --- a/pkg/core/native/native_nep17.go +++ b/pkg/core/native/native_nep17.go @@ -165,16 +165,11 @@ func (c *nep17TokenNative) postTransfer(ic *interop.Context, from, to *util.Uint } func (c *nep17TokenNative) emitTransfer(ic *interop.Context, from, to *util.Uint160, amount *big.Int) { - ne := state.NotificationEvent{ - ScriptHash: c.Hash, - Name: "Transfer", - Item: stackitem.NewArray([]stackitem.Item{ - addrToStackItem(from), - addrToStackItem(to), - stackitem.NewBigInteger(amount), - }), - } - ic.Notifications = append(ic.Notifications, ne) + ic.AddNotification(c.Hash, "Transfer", stackitem.NewArray([]stackitem.Item{ + addrToStackItem(from), + addrToStackItem(to), + stackitem.NewBigInteger(amount), + })) } // updateAccBalance adds the specified amount to the acc's balance. If requiredBalance diff --git a/pkg/core/native/oracle.go b/pkg/core/native/oracle.go index 380f45e76..800423656 100644 --- a/pkg/core/native/oracle.go +++ b/pkg/core/native/oracle.go @@ -272,15 +272,11 @@ func (o *Oracle) FinishInternal(ic *interop.Context) error { if err != nil { return ErrRequestNotFound } + ic.AddNotification(o.Hash, "OracleResponse", stackitem.NewArray([]stackitem.Item{ + stackitem.Make(resp.ID), + stackitem.Make(req.OriginalTxID.BytesBE()), + })) - ic.Notifications = append(ic.Notifications, state.NotificationEvent{ - ScriptHash: o.Hash, - Name: "OracleResponse", - Item: stackitem.NewArray([]stackitem.Item{ - stackitem.Make(resp.ID), - stackitem.Make(req.OriginalTxID.BytesBE()), - }), - }) origTx, _, err := ic.DAO.GetTransaction(req.OriginalTxID) if err != nil { return ErrRequestNotFound @@ -382,16 +378,12 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string } else { filterNotif = stackitem.Null{} } - ic.Notifications = append(ic.Notifications, state.NotificationEvent{ - ScriptHash: o.Hash, - Name: "OracleRequest", - Item: stackitem.NewArray([]stackitem.Item{ - stackitem.Make(id), - stackitem.Make(ic.VM.GetCallingScriptHash().BytesBE()), - stackitem.Make(url), - filterNotif, - }), - }) + ic.AddNotification(o.Hash, "OracleRequest", stackitem.NewArray([]stackitem.Item{ + stackitem.Make(id), + stackitem.Make(ic.VM.GetCallingScriptHash().BytesBE()), + stackitem.Make(url), + filterNotif, + })) req := &state.OracleRequest{ OriginalTxID: o.getOriginalTxID(ic.DAO, ic.Tx), GasForResponse: gas.Uint64(),