Merge pull request #1477 from nspcc-dev/fix/native

native: provide `name` method for all contracts
This commit is contained in:
Roman Khimov 2020-10-09 15:35:52 +03:00 committed by GitHub
commit 93df89da8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 5 deletions

View file

@ -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
}

View file

@ -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))
}

View file

@ -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)

View file

@ -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)

View file

@ -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))
}
}

View file

@ -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())
})
}
}