Skip to content

Commit

Permalink
Merge pull request #2043 from nspcc-dev/runtime-getnetwork
Browse files Browse the repository at this point in the history
interop/runtime: add `System.Runtime.GetNetwork`, fix #2038
  • Loading branch information
roman-khimov authored Jul 12, 2021
2 parents 1853d0c + 96a42cd commit bc5a1b7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/compiler/syscall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 2 additions & 0 deletions pkg/core/interop/interopnames/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -61,6 +62,7 @@ var names = []string{
SystemRuntimeGetEntryScriptHash,
SystemRuntimeGetExecutingScriptHash,
SystemRuntimeGetInvocationCounter,
SystemRuntimeGetNetwork,
SystemRuntimeGetNotifications,
SystemRuntimeGetScriptContainer,
SystemRuntimeGetTime,
Expand Down
7 changes: 7 additions & 0 deletions pkg/core/interop/runtime/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions pkg/core/interop_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions pkg/core/interops.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
6 changes: 6 additions & 0 deletions pkg/interop/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bc5a1b7

Please sign in to comment.