Merge pull request #2043 from nspcc-dev/runtime-getnetwork

interop/runtime: add `System.Runtime.GetNetwork`, fix #2038
This commit is contained in:
Roman Khimov 2021-07-12 10:46:41 +03:00 committed by GitHub
commit bc5a1b7bcd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 0 deletions

View file

@ -74,6 +74,7 @@ func TestSyscallExecution(t *testing.T) {
"runtime.GetEntryScriptHash": {interopnames.SystemRuntimeGetEntryScriptHash, nil, false},
"runtime.GetExecutingScriptHash": {interopnames.SystemRuntimeGetExecutingScriptHash, nil, false},
"runtime.GetInvocationCounter": {interopnames.SystemRuntimeGetInvocationCounter, nil, false},
"runtime.GetNetwork": {interopnames.SystemRuntimeGetNetwork, nil, false},
"runtime.GetNotifications": {interopnames.SystemRuntimeGetNotifications, []string{u160}, false},
"runtime.GetScriptContainer": {interopnames.SystemRuntimeGetScriptContainer, nil, false},
"runtime.GetTime": {interopnames.SystemRuntimeGetTime, nil, false},

View file

@ -24,6 +24,7 @@ const (
SystemRuntimeGetEntryScriptHash = "System.Runtime.GetEntryScriptHash"
SystemRuntimeGetExecutingScriptHash = "System.Runtime.GetExecutingScriptHash"
SystemRuntimeGetInvocationCounter = "System.Runtime.GetInvocationCounter"
SystemRuntimeGetNetwork = "System.Runtime.GetNetwork"
SystemRuntimeGetNotifications = "System.Runtime.GetNotifications"
SystemRuntimeGetScriptContainer = "System.Runtime.GetScriptContainer"
SystemRuntimeGetTime = "System.Runtime.GetTime"
@ -61,6 +62,7 @@ var names = []string{
SystemRuntimeGetEntryScriptHash,
SystemRuntimeGetExecutingScriptHash,
SystemRuntimeGetInvocationCounter,
SystemRuntimeGetNetwork,
SystemRuntimeGetNotifications,
SystemRuntimeGetScriptContainer,
SystemRuntimeGetTime,

View file

@ -67,3 +67,10 @@ func GetInvocationCounter(ic *interop.Context) error {
ic.VM.Estack().PushVal(count)
return nil
}
// GetNetwork returns chain network number.
func GetNetwork(ic *interop.Context) error {
m := ic.Chain.GetConfig().Magic
ic.VM.Estack().PushVal(uint32(m))
return nil
}

View file

@ -7,6 +7,7 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/internal/testchain"
"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/interop/contract"
@ -1202,6 +1203,25 @@ func TestLoadToken(t *testing.T) {
})
}
func TestRuntimeGetNetwork(t *testing.T) {
bc := newTestChain(t)
w := io.NewBufBinWriter()
emit.Syscall(w.BinWriter, interopnames.SystemRuntimeGetNetwork)
require.NoError(t, w.Err)
tx := transaction.New(w.Bytes(), 10_000)
tx.ValidUntilBlock = bc.BlockHeight() + 1
addSigners(neoOwner, tx)
require.NoError(t, testchain.SignTx(bc, tx))
require.NoError(t, bc.AddBlock(bc.newBlock(tx)))
aer, err := bc.GetAppExecResults(tx.Hash(), trigger.Application)
require.NoError(t, err)
checkResult(t, &aer[0], stackitem.Make(uint32(bc.config.Magic)))
}
func TestRuntimeBurnGas(t *testing.T) {
bc := newTestChain(t)

View file

@ -50,6 +50,7 @@ var systemInterops = []interop.Function{
{Name: interopnames.SystemRuntimeGetEntryScriptHash, Func: runtime.GetEntryScriptHash, Price: 1 << 4},
{Name: interopnames.SystemRuntimeGetExecutingScriptHash, Func: runtime.GetExecutingScriptHash, Price: 1 << 4},
{Name: interopnames.SystemRuntimeGetInvocationCounter, Func: runtime.GetInvocationCounter, Price: 1 << 4},
{Name: interopnames.SystemRuntimeGetNetwork, Func: runtime.GetNetwork, Price: 1 << 3},
{Name: interopnames.SystemRuntimeGetNotifications, Func: runtime.GetNotifications, Price: 1 << 8, ParamCount: 1},
{Name: interopnames.SystemRuntimeGetScriptContainer, Func: engineGetScriptContainer, Price: 1 << 3},
{Name: interopnames.SystemRuntimeGetTime, Func: runtime.GetTime, Price: 1 << 3, RequiredFlags: callflag.ReadStates},

View file

@ -46,6 +46,12 @@ func Notify(name string, args ...interface{}) {
neogointernal.Syscall2NoReturn("System.Runtime.Notify", name, args)
}
// GetNetwork returns network magic number. This function uses
// `System.Runtime.GetNetwork` syscall.
func GetNetwork() int {
return neogointernal.Syscall0("System.Runtime.GetNetwork").(int)
}
// GetTime returns the timestamp of the most recent block. Note that when running
// script in test mode this would be the last accepted (persisted) block in the
// chain, but when running as a part of the new block the time returned is the