From 748a7d3eeac80c80f00652ccd55c036fd7e4fcc1 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 16 Sep 2022 11:37:33 +0300 Subject: [PATCH] nns: add SetAdmin event Port https://github.com/neo-project/non-native-contracts/pull/11. --- examples/nft-nd-nns/nns.go | 2 ++ examples/nft-nd-nns/nns.yml | 8 ++++++++ examples/nft-nd-nns/nns_test.go | 24 ++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/examples/nft-nd-nns/nns.go b/examples/nft-nd-nns/nns.go index e83454d49..8aa2d8de7 100644 --- a/examples/nft-nd-nns/nns.go +++ b/examples/nft-nd-nns/nns.go @@ -462,8 +462,10 @@ func SetAdmin(name string, admin interop.Hash160) { if !runtime.CheckWitness(ns.Owner) { panic("not witnessed by owner") } + oldAdm := ns.Admin ns.Admin = admin putNameState(ctx, ns) + runtime.Notify("SetAdmin", name, oldAdm, admin) } // SetRecord updates record of the specified type and ID. diff --git a/examples/nft-nd-nns/nns.yml b/examples/nft-nd-nns/nns.yml index 2dc880ca2..a99234437 100644 --- a/examples/nft-nd-nns/nns.yml +++ b/examples/nft-nd-nns/nns.yml @@ -15,6 +15,14 @@ events: type: Integer - name: tokenId type: ByteArray + - name: SetAdmin + parameters: + - name: name + type: String + - name: oldAdmin + type: Hash160 + - name: newAdmin + type: Hash160 permissions: - hash: fffdc93764dbaddd97c48f252a53ea4643faa3fd methods: ["update"] diff --git a/examples/nft-nd-nns/nns_test.go b/examples/nft-nd-nns/nns_test.go index d03ce9c30..ac79c1310 100644 --- a/examples/nft-nd-nns/nns_test.go +++ b/examples/nft-nd-nns/nns_test.go @@ -9,6 +9,7 @@ import ( nns "github.com/nspcc-dev/neo-go/examples/nft-nd-nns" "github.com/nspcc-dev/neo-go/pkg/compiler" "github.com/nspcc-dev/neo-go/pkg/core/interop/storage" + "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/neotest" "github.com/nspcc-dev/neo-go/pkg/neotest/chain" "github.com/nspcc-dev/neo-go/pkg/smartcontract" @@ -369,7 +370,17 @@ func TestSetAdmin(t *testing.T) { cOwner.InvokeFail(t, "not witnessed by admin", "setAdmin", "neo.com", admin.ScriptHash()) cAdmin.InvokeFail(t, "not witnessed by owner", "setAdmin", "neo.com", admin.ScriptHash()) cc := c.WithSigners(owner, admin) - cc.Invoke(t, stackitem.Null{}, "setAdmin", "neo.com", admin.ScriptHash()) + h := cc.Invoke(t, stackitem.Null{}, "setAdmin", "neo.com", admin.ScriptHash()) + cc.CheckTxNotificationEvent(t, h, 0, state.NotificationEvent{ + ScriptHash: cc.Hash, + Name: "SetAdmin", + Item: stackitem.NewArray([]stackitem.Item{ + stackitem.NewByteArray([]byte("neo.com")), + stackitem.Null{}, + stackitem.NewByteArray(admin.ScriptHash().BytesBE()), + }), + }) + props := stackitem.NewMap() props.Add(stackitem.Make("name"), stackitem.Make("neo.com")) props.Add(stackitem.Make("expiration"), stackitem.Make(expectedExpiration)) @@ -384,7 +395,16 @@ func TestSetAdmin(t *testing.T) { t.Run("set admin to null", func(t *testing.T) { cAdmin.Invoke(t, stackitem.Null{}, "addRecord", "neo.com", int64(nns.TXT), "sometext") - cOwner.Invoke(t, stackitem.Null{}, "setAdmin", "neo.com", nil) + h = cOwner.Invoke(t, stackitem.Null{}, "setAdmin", "neo.com", nil) + cc.CheckTxNotificationEvent(t, h, 0, state.NotificationEvent{ + ScriptHash: cc.Hash, + Name: "SetAdmin", + Item: stackitem.NewArray([]stackitem.Item{ + stackitem.NewByteArray([]byte("neo.com")), + stackitem.NewByteArray(admin.ScriptHash().BytesBE()), + stackitem.Null{}, + }), + }) cAdmin.InvokeFail(t, "not witnessed by admin", "deleteRecords", "neo.com", int64(nns.TXT)) }) }