native: call native contracts by ID instead of name

Fix #1666.
This commit is contained in:
Roman Khimov 2021-01-16 00:17:31 +03:00
parent f39ede9869
commit 6ecc6f0422
17 changed files with 48 additions and 55 deletions

Binary file not shown.

View file

@ -20,7 +20,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
"github.com/nspcc-dev/neo-go/pkg/core/mempool"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -557,7 +556,7 @@ func TestVerifyTx(t *testing.T) {
})
tx.Scripts = append(tx.Scripts, transaction.Witness{})
t.Run("NonZeroVerification", func(t *testing.T) {
script, _ := state.CreateNativeContractHash(nativenames.Oracle)
script, _ := state.CreateNativeContractHash(-6) //oracleContractID
w := io.NewBufBinWriter()
emit.Opcodes(w.BinWriter, opcode.ABORT)
emit.Bytes(w.BinWriter, util.Uint160{}.BytesBE())
@ -975,7 +974,7 @@ func TestVerifyTx(t *testing.T) {
fee.Opcode(bc.GetBaseExecFee(), // Notary verification script
opcode.PUSHDATA1, opcode.RET, // invocation script
opcode.PUSHDATA1, opcode.RET, // arguments for native verification call
opcode.PUSHDATA1, opcode.SYSCALL, opcode.RET) + // Neo.Native.Call
opcode.PUSHINT8, opcode.SYSCALL, opcode.RET) + // Neo.Native.Call
native.NotaryVerificationPrice // Notary witness verification price
tx.Scripts = []transaction.Witness{
{

View file

@ -108,10 +108,11 @@ type ContractMD struct {
}
// NewContractMD returns Contract with the specified list of methods.
func NewContractMD(name string) *ContractMD {
func NewContractMD(name string, id int32) *ContractMD {
c := &ContractMD{
Name: name,
Methods: make(map[string]MethodAndPrice),
Name: name,
ContractID: id,
Methods: make(map[string]MethodAndPrice),
}
// NEF is now stored in contract state and affects state dump.
@ -119,7 +120,7 @@ func NewContractMD(name string) *ContractMD {
c.NEF.Header.Compiler = "ScriptBuilder"
c.NEF.Header.Magic = nef.Magic
c.NEF.Header.Version = "3.0"
c.NEF.Script, c.Hash = state.CreateNativeContractHash(c.Name)
c.NEF.Script, c.Hash = state.CreateNativeContractHash(id)
c.NEF.Checksum = c.NEF.CalculateChecksum()
c.Manifest = *manifest.DefaultManifest(name)

View file

@ -2,19 +2,18 @@ package native
import (
"testing"
// "github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
)
// Compatibility test. hashes are taken directly from C# node.
func TestNativeHashes(t *testing.T) {
/*
require.Equal(t, "136ec44854ad9a714901eb7d714714f1791203f2", newDesignate(false).Hash.StringLE())
require.Equal(t, "a6a6c15dcdc9b997dac448b6926522d22efeedfb", newGAS().Hash.StringLE())
require.Equal(t, "081514120c7894779309255b7fb18b376cec731a", newManagement().Hash.StringLE())
require.Equal(t, "0a46e2e37c9987f570b4af253fb77e7eef0f72b6", newNEO().Hash.StringLE())
// Not yet a part of NEO.
//require.Equal(t, "", newNotary().Hash.StringLE()())
require.Equal(t, "b1c37d5847c2ae36bdde31d0cc833a7ad9667f8f", newOracle().Hash.StringLE())
require.Equal(t, "dde31084c0fdbebc7f5ed5f53a38905305ccee14", newPolicy().Hash.StringLE())
*/
require.Equal(t, "bee421fdbb3e791265d2104cb34934f53fcc0e45", newManagement().Hash.StringLE())
require.Equal(t, "4961bf0ab79370b23dc45cde29f568d0e0fa6e93", newNEO().Hash.StringLE())
require.Equal(t, "9ac04cf223f646de5f7faccafe34e30e5d4382a2", newGAS().Hash.StringLE())
require.Equal(t, "c939a4af1c762e5edca36d4b61c06ba82c4c6ff5", newPolicy().Hash.StringLE())
require.Equal(t, "f4bbd95569e8dda2cb84eb609a1488ddd0d9fa91", newDesignate(false).Hash.StringLE())
require.Equal(t, "8cd3889136056b3304ec59f6d424b8767710ed79", newOracle().Hash.StringLE())
// Not yet a part of NEO.
//require.Equal(t, "", newNotary().Hash.StringLE()())
}

View file

@ -41,7 +41,7 @@ type oraclesData struct {
}
const (
designateContractID = -4
designateContractID = -5
// maxNodeCount is the maximum number of nodes to set the role for.
maxNodeCount = 32
@ -72,8 +72,7 @@ func (s *Designate) isValidRole(r Role) bool {
}
func newDesignate(p2pSigExtensionsEnabled bool) *Designate {
s := &Designate{ContractMD: *interop.NewContractMD(nativenames.Designation)}
s.ContractID = designateContractID
s := &Designate{ContractMD: *interop.NewContractMD(nativenames.Designation, designateContractID)}
s.p2pSigExtensionsEnabled = p2pSigExtensionsEnabled
desc := newDescriptor("getDesignatedByRole", smartcontract.ArrayType,

View file

@ -12,16 +12,16 @@ import (
// Call calls specified native contract method.
func Call(ic *interop.Context) error {
name := ic.VM.Estack().Pop().String()
id := int32(ic.VM.Estack().Pop().BigInt().Int64())
var c interop.Contract
for _, ctr := range ic.Natives {
if ctr.Metadata().Name == name {
if ctr.Metadata().ContractID == id {
c = ctr
break
}
}
if c == nil {
return fmt.Errorf("native contract %s not found", name)
return fmt.Errorf("native contract %d not found", id)
}
h := ic.VM.GetCurrentScriptHash()
if !h.Equals(c.Metadata().Hash) {
@ -33,7 +33,7 @@ func Call(ic *interop.Context) error {
return fmt.Errorf("method %s not found", operation)
}
if !ic.VM.Context().GetCallFlags().Has(m.RequiredFlags) {
return fmt.Errorf("missing call flags for native %s `%s` operation call: %05b vs %05b", name, operation, ic.VM.Context().GetCallFlags(), m.RequiredFlags)
return fmt.Errorf("missing call flags for native %d `%s` operation call: %05b vs %05b", id, operation, ic.VM.Context().GetCallFlags(), m.RequiredFlags)
}
// Native contract prices are not multiplied by `BaseExecFee`.
if !ic.VM.AddGas(m.Price) {

View file

@ -36,6 +36,8 @@ type Management struct {
const StoragePrice = 100000
const (
managementContractID = -1
prefixContract = 8
defaultMinimumDeploymentFee = 10_00000000
@ -59,7 +61,7 @@ func makeContractKey(h util.Uint160) []byte {
// newManagement creates new Management native contract.
func newManagement() *Management {
var m = &Management{
ContractMD: *interop.NewContractMD(nativenames.Management),
ContractMD: *interop.NewContractMD(nativenames.Management, managementContractID),
contracts: make(map[util.Uint160]*state.Contract),
}

View file

@ -18,7 +18,7 @@ type GAS struct {
NEO *NEO
}
const gasContractID = -2
const gasContractID = -3
// GASFactor is a divisor for finding GAS integral value.
const GASFactor = NEOTotalSupply
@ -27,12 +27,11 @@ const initialGAS = 30000000
// newGAS returns GAS native contract.
func newGAS() *GAS {
g := &GAS{}
nep17 := newNEP17Native(nativenames.Gas)
nep17 := newNEP17Native(nativenames.Gas, gasContractID)
nep17.symbol = "GAS"
nep17.decimals = 8
nep17.factor = GASFactor
nep17.incBalance = g.increaseBalance
nep17.ContractID = gasContractID
g.nep17TokenNative = *nep17

View file

@ -51,7 +51,7 @@ type NEO struct {
}
const (
neoContractID = -1
neoContractID = -2
// NEOTotalSupply is the total amount of NEO in the system.
NEOTotalSupply = 100000000
// prefixCandidate is a prefix used to store validator's data.
@ -94,12 +94,11 @@ func makeValidatorKey(key *keys.PublicKey) []byte {
// newNEO returns NEO native contract.
func newNEO() *NEO {
n := &NEO{}
nep17 := newNEP17Native(nativenames.Neo)
nep17 := newNEP17Native(nativenames.Neo, neoContractID)
nep17.symbol = "NEO"
nep17.decimals = 0
nep17.factor = 1
nep17.incBalance = n.increaseBalance
nep17.ContractID = neoContractID
n.nep17TokenNative = *nep17
n.votesChanged.Store(true)

View file

@ -42,8 +42,8 @@ func (c *nep17TokenNative) Metadata() *interop.ContractMD {
return &c.ContractMD
}
func newNEP17Native(name string) *nep17TokenNative {
n := &nep17TokenNative{ContractMD: *interop.NewContractMD(name)}
func newNEP17Native(name string, id int32) *nep17TokenNative {
n := &nep17TokenNative{ContractMD: *interop.NewContractMD(name, id)}
n.Manifest.SupportedStandards = []string{manifest.NEP17StandardName}
desc := newDescriptor("symbol", smartcontract.StringType)

View file

@ -52,8 +52,7 @@ var maxNotValidBeforeDeltaKey = []byte{10}
// newNotary returns Notary native contract.
func newNotary() *Notary {
n := &Notary{ContractMD: *interop.NewContractMD(nativenames.Notary)}
n.ContractID = notaryContractID
n := &Notary{ContractMD: *interop.NewContractMD(nativenames.Notary, notaryContractID)}
desc := newDescriptor("onPayment", smartcontract.VoidType,
manifest.NewParameter("from", smartcontract.Hash160Type),

View file

@ -38,7 +38,7 @@ type Oracle struct {
}
const (
oracleContractID = -5
oracleContractID = -6
maxURLLength = 256
maxFilterLength = 128
maxCallbackLength = 32
@ -52,7 +52,7 @@ const (
var oracleScript []byte
func init() {
_, h := state.CreateNativeContractHash(nativenames.Oracle)
_, h := state.CreateNativeContractHash(oracleContractID)
w := io.NewBufBinWriter()
emit.Int(w.BinWriter, 0)
emit.Opcodes(w.BinWriter, opcode.NEWARRAY)
@ -86,8 +86,7 @@ func GetOracleResponseScript() []byte {
}
func newOracle() *Oracle {
o := &Oracle{ContractMD: *interop.NewContractMD(nativenames.Oracle)}
o.ContractID = oracleContractID
o := &Oracle{ContractMD: *interop.NewContractMD(nativenames.Oracle, oracleContractID)}
desc := newDescriptor("request", smartcontract.VoidType,
manifest.NewParameter("url", smartcontract.StringType),

View file

@ -21,7 +21,7 @@ import (
)
const (
policyContractID = -3
policyContractID = -4
defaultMaxBlockSize = 1024 * 256
defaultMaxTransactionsPerBlock = 512
@ -81,9 +81,7 @@ var _ interop.Contract = (*Policy)(nil)
// newPolicy returns Policy native contract.
func newPolicy() *Policy {
p := &Policy{ContractMD: *interop.NewContractMD(nativenames.Policy)}
p.ContractID = policyContractID
p := &Policy{ContractMD: *interop.NewContractMD(nativenames.Policy, policyContractID)}
desc := newDescriptor("getMaxTransactionsPerBlock", smartcontract.IntegerType)
md := newMethodAndPrice(p.getMaxTransactionsPerBlock, 1000000, callflag.ReadStates)

View file

@ -61,7 +61,7 @@ const testSumPrice = 1 << 15 * interop.DefaultBaseExecFee // same as contract.Ca
func newTestNative() *testNative {
tn := &testNative{
meta: *interop.NewContractMD("Test.Native.Sum"),
meta: *interop.NewContractMD("Test.Native.Sum", 0),
blocks: make(chan uint32, 1),
}
desc := &manifest.Method{
@ -181,10 +181,9 @@ func TestNativeContract_Invoke(t *testing.T) {
// System.Contract.Call + "sum" itself + opcodes for pushing arguments.
price := int64(testSumPrice * 2)
price += 3 * fee.Opcode(chain.GetBaseExecFee(), opcode.PUSHINT8, opcode.PUSHDATA1)
price += 2 * fee.Opcode(chain.GetBaseExecFee(), opcode.SYSCALL)
price += 3 * fee.Opcode(chain.GetBaseExecFee(), opcode.PUSHINT8)
price += 2 * fee.Opcode(chain.GetBaseExecFee(), opcode.SYSCALL, opcode.PUSHDATA1, opcode.PUSHINT8)
price += fee.Opcode(chain.GetBaseExecFee(), opcode.PACK)
price += fee.Opcode(chain.GetBaseExecFee(), opcode.PUSHINT8)
res, err := invokeContractMethod(chain, price, tn.Metadata().Hash, "sum", int64(14), int64(28))
require.NoError(t, err)
checkResult(t, res, stackitem.Make(42))
@ -237,7 +236,7 @@ func TestNativeContract_InvokeInternal(t *testing.T) {
v.Estack().PushVal(14)
v.Estack().PushVal(28)
v.Estack().PushVal("sum")
v.Estack().PushVal(tn.Metadata().Name)
v.Estack().PushVal(tn.Metadata().ContractID)
require.NoError(t, native.Call(ic))

View file

@ -124,9 +124,9 @@ func CreateContractHash(sender util.Uint160, script []byte) util.Uint160 {
}
// CreateNativeContractHash returns script and hash for the native contract.
func CreateNativeContractHash(name string) ([]byte, util.Uint160) {
func CreateNativeContractHash(id int32) ([]byte, util.Uint160) {
w := io.NewBufBinWriter()
emit.String(w.BinWriter, name)
emit.Int(w.BinWriter, int64(id))
emit.Syscall(w.BinWriter, interopnames.SystemContractCallNative)
if w.Err != nil {
panic(w.Err)

View file

@ -57,7 +57,7 @@ type rpcTestCase struct {
}
const testContractHash = "0b3bc97e94ed99e32dda46c9ecd2d3626979af06"
const deploymentTxHash = "632b179910b368a34d0b71a1289b24f833186e86ce16b3f805c1ac247a721e16"
const deploymentTxHash = "4288bb6ad12426a9e34f6af4c050bc291798a46958443d614f457a9a12f087c2"
const genesisBlockHash = "0542f4350c6e236d0509bcd98188b0034bfbecc1a0c7fcdb8e4295310d468b70"
const verifyContractHash = "d2da8ee8c0bf6c5bf3dda1ef671dbf5fef7226e9"
@ -155,12 +155,12 @@ var rpcTestCases = map[string][]rpcTestCase{
},
{
name: "positive, by id",
params: `[0]`,
params: `[1]`,
result: func(e *executor) interface{} { return &state.Contract{} },
check: func(t *testing.T, e *executor, cs interface{}) {
res, ok := cs.(*state.Contract)
require.True(t, ok)
assert.Equal(t, int32(0), res.ID)
assert.Equal(t, int32(1), res.ID)
},
},
{
@ -180,7 +180,7 @@ var rpcTestCases = map[string][]rpcTestCase{
check: func(t *testing.T, e *executor, cs interface{}) {
res, ok := cs.(*state.Contract)
require.True(t, ok)
assert.Equal(t, int32(-3), res.ID)
assert.Equal(t, int32(-4), res.ID)
},
},
{

Binary file not shown.