From 42ff4a8fc7122513469a032c0749c0ecf4ae2055 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 9 Oct 2020 15:06:28 +0300 Subject: [PATCH] native: provide `name` method for all contracts --- pkg/core/native/designate.go | 4 ++++ pkg/core/native/native_nep5.go | 6 +----- pkg/core/native/oracle.go | 4 ++++ pkg/core/native/policy.go | 4 ++++ pkg/core/native/util.go | 8 ++++++++ pkg/core/native_contract_test.go | 24 ++++++++++++++++++++++++ 6 files changed, 45 insertions(+), 5 deletions(-) diff --git a/pkg/core/native/designate.go b/pkg/core/native/designate.go index 6da4f8481..8383d5c4f 100644 --- a/pkg/core/native/designate.go +++ b/pkg/core/native/designate.go @@ -67,6 +67,10 @@ func newDesignate() *Designate { md = newMethodAndPrice(s.designateAsRole, 0, smartcontract.AllowModifyStates) s.AddMethod(md, desc, false) + desc = newDescriptor("name", smartcontract.StringType) + md = newMethodAndPrice(nameMethod(designateName), 0, smartcontract.NoneFlag) + s.AddMethod(md, desc, true) + return s } diff --git a/pkg/core/native/native_nep5.go b/pkg/core/native/native_nep5.go index 56ab2d864..8ba337f82 100644 --- a/pkg/core/native/native_nep5.go +++ b/pkg/core/native/native_nep5.go @@ -53,7 +53,7 @@ func newNEP5Native(name string) *nep5TokenNative { n.Manifest.SupportedStandards = []string{manifest.NEP5StandardName} desc := newDescriptor("name", smartcontract.StringType) - md := newMethodAndPrice(n.Name, 0, smartcontract.NoneFlag) + md := newMethodAndPrice(nameMethod(name), 0, smartcontract.NoneFlag) n.AddMethod(md, desc, true) desc = newDescriptor("symbol", smartcontract.StringType) @@ -98,10 +98,6 @@ func (c *nep5TokenNative) Initialize(_ *interop.Context) error { return nil } -func (c *nep5TokenNative) Name(_ *interop.Context, _ []stackitem.Item) stackitem.Item { - return stackitem.NewByteArray([]byte(c.ContractMD.Name)) -} - func (c *nep5TokenNative) Symbol(_ *interop.Context, _ []stackitem.Item) stackitem.Item { return stackitem.NewByteArray([]byte(c.symbol)) } diff --git a/pkg/core/native/oracle.go b/pkg/core/native/oracle.go index d85ad7963..abe3b7228 100644 --- a/pkg/core/native/oracle.go +++ b/pkg/core/native/oracle.go @@ -111,6 +111,10 @@ func newOracle() *Oracle { md := newMethodAndPrice(o.request, oracleRequestPrice, smartcontract.AllowModifyStates) o.AddMethod(md, desc, false) + desc = newDescriptor("name", smartcontract.StringType) + md = newMethodAndPrice(nameMethod(oracleName), 0, smartcontract.NoneFlag) + o.AddMethod(md, desc, true) + desc = newDescriptor("finish", smartcontract.VoidType) md = newMethodAndPrice(o.finish, 0, smartcontract.AllowModifyStates) o.AddMethod(md, desc, false) diff --git a/pkg/core/native/policy.go b/pkg/core/native/policy.go index e5a881e46..d0e8f7533 100644 --- a/pkg/core/native/policy.go +++ b/pkg/core/native/policy.go @@ -126,6 +126,10 @@ func newPolicy() *Policy { md = newMethodAndPrice(p.unblockAccount, 3000000, smartcontract.AllowModifyStates) p.AddMethod(md, desc, false) + desc = newDescriptor("name", smartcontract.StringType) + md = newMethodAndPrice(nameMethod(policyName), 0, smartcontract.NoneFlag) + p.AddMethod(md, desc, true) + desc = newDescriptor("onPersist", smartcontract.VoidType) md = newMethodAndPrice(getOnPersistWrapper(p.OnPersist), 0, smartcontract.AllowModifyStates) p.AddMethod(md, desc, false) diff --git a/pkg/core/native/util.go b/pkg/core/native/util.go index 293581c34..97e02fc14 100644 --- a/pkg/core/native/util.go +++ b/pkg/core/native/util.go @@ -2,8 +2,10 @@ package native import ( "github.com/nspcc-dev/neo-go/pkg/core/dao" + "github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/io" + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" ) func getSerializableFromDAO(id int32, d dao.DAO, key []byte, item io.Serializable) error { @@ -15,3 +17,9 @@ func getSerializableFromDAO(id int32, d dao.DAO, key []byte, item io.Serializabl item.DecodeBinary(r) return r.Err } + +func nameMethod(name string) interop.Method { + return func(_ *interop.Context, _ []stackitem.Item) stackitem.Item { + return stackitem.NewByteArray([]byte(name)) + } +} diff --git a/pkg/core/native_contract_test.go b/pkg/core/native_contract_test.go index b3c5d545d..132e391dc 100644 --- a/pkg/core/native_contract_test.go +++ b/pkg/core/native_contract_test.go @@ -357,3 +357,27 @@ func TestNativeContract_InvokeOtherContract(t *testing.T) { require.Equal(t, int64(5), res.Stack[0].Value().(*big.Int).Int64()) }) } + +func TestAllContractsHaveName(t *testing.T) { + bc := newTestChain(t) + defer bc.Close() + for _, c := range bc.contracts.Contracts { + name := c.Metadata().Name + t.Run(name, func(t *testing.T) { + w := io.NewBufBinWriter() + emit.AppCallWithOperationAndArgs(w.BinWriter, c.Metadata().Hash, "name") + require.NoError(t, w.Err) + + tx := transaction.New(netmode.UnitTestNet, w.Bytes(), 1007570) + tx.ValidUntilBlock = bc.blockHeight + 1 + addSigners(tx) + require.NoError(t, signTx(bc, tx)) + require.NoError(t, bc.AddBlock(bc.newBlock(tx))) + + aer, err := bc.GetAppExecResult(tx.Hash()) + require.NoError(t, err) + require.Len(t, aer.Stack, 1) + require.Equal(t, []byte(name), aer.Stack[0].Value()) + }) + } +}