From 96a42cd0117cbdd3e40eee14d793c71b94de4537 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Tue, 6 Jul 2021 14:20:25 +0300 Subject: [PATCH] interop: add `System.Runtime.GetNetwork`, fix #2038 Signed-off-by: Evgeniy Stratonikov --- pkg/compiler/syscall_test.go | 1 + pkg/core/interop/interopnames/names.go | 2 ++ pkg/core/interop/runtime/util.go | 7 +++++++ pkg/core/interop_system_test.go | 20 ++++++++++++++++++++ pkg/core/interops.go | 1 + pkg/interop/runtime/runtime.go | 6 ++++++ 6 files changed, 37 insertions(+) diff --git a/pkg/compiler/syscall_test.go b/pkg/compiler/syscall_test.go index 048362af2..a41564437 100644 --- a/pkg/compiler/syscall_test.go +++ b/pkg/compiler/syscall_test.go @@ -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}, diff --git a/pkg/core/interop/interopnames/names.go b/pkg/core/interop/interopnames/names.go index 5f094754c..e075d37d1 100644 --- a/pkg/core/interop/interopnames/names.go +++ b/pkg/core/interop/interopnames/names.go @@ -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, diff --git a/pkg/core/interop/runtime/util.go b/pkg/core/interop/runtime/util.go index de17a0b59..b6f372bf2 100644 --- a/pkg/core/interop/runtime/util.go +++ b/pkg/core/interop/runtime/util.go @@ -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 +} diff --git a/pkg/core/interop_system_test.go b/pkg/core/interop_system_test.go index a64b45aa1..1468f8f65 100644 --- a/pkg/core/interop_system_test.go +++ b/pkg/core/interop_system_test.go @@ -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) diff --git a/pkg/core/interops.go b/pkg/core/interops.go index 209b49f77..20ad26dca 100644 --- a/pkg/core/interops.go +++ b/pkg/core/interops.go @@ -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}, diff --git a/pkg/interop/runtime/runtime.go b/pkg/interop/runtime/runtime.go index 174fc0a91..c5e36a7f1 100644 --- a/pkg/interop/runtime/runtime.go +++ b/pkg/interop/runtime/runtime.go @@ -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