diff --git a/pkg/compiler/syscall.go b/pkg/compiler/syscall.go index d5c7c5908..dd8394560 100644 --- a/pkg/compiler/syscall.go +++ b/pkg/compiler/syscall.go @@ -29,6 +29,7 @@ var syscalls = map[string]map[string]string{ "GetEntryScriptHash": "System.Runtime.GetEntryScriptHash", "GetExecutingScriptHash": "System.Runtime.GetExecutingScriptHash", + "GasLeft": "System.Runtime.GasLeft", "GetTrigger": "System.Runtime.GetTrigger", "CheckWitness": "System.Runtime.CheckWitness", "Notify": "System.Runtime.Notify", diff --git a/pkg/core/interop/runtime/util.go b/pkg/core/interop/runtime/util.go new file mode 100644 index 000000000..65ad6756c --- /dev/null +++ b/pkg/core/interop/runtime/util.go @@ -0,0 +1,12 @@ +package runtime + +import ( + "github.com/nspcc-dev/neo-go/pkg/core/interop" + "github.com/nspcc-dev/neo-go/pkg/vm" +) + +// GasLeft returns remaining amount of GAS. +func GasLeft(_ *interop.Context, v *vm.VM) error { + v.Estack().PushVal(int64(v.GasLimit - v.GasConsumed())) + return nil +} diff --git a/pkg/core/interop_system_test.go b/pkg/core/interop_system_test.go index 613351459..436284bbb 100644 --- a/pkg/core/interop_system_test.go +++ b/pkg/core/interop_system_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/nspcc-dev/dbft/crypto" + "github.com/nspcc-dev/neo-go/pkg/core/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/util" @@ -197,3 +198,13 @@ func TestContractCreateAccount(t *testing.T) { require.Error(t, contractCreateStandardAccount(ic, v)) }) } + +func TestRuntimeGasLeft(t *testing.T) { + v, ic, chain := createVM(t) + defer chain.Close() + + v.GasLimit = 100 + v.AddGas(58) + require.NoError(t, runtime.GasLeft(ic, v)) + require.EqualValues(t, 42, v.Estack().Pop().BigInt().Int64()) +} diff --git a/pkg/core/interops.go b/pkg/core/interops.go index b8d78bca6..3ad59985a 100644 --- a/pkg/core/interops.go +++ b/pkg/core/interops.go @@ -107,6 +107,7 @@ var systemInterops = []interop.Function{ {Name: "System.Json.Deserialize", Func: json.Deserialize, Price: 500000}, {Name: "System.Json.Serialize", Func: json.Serialize, Price: 100000}, {Name: "System.Runtime.CheckWitness", Func: runtime.CheckWitness, Price: 30000}, + {Name: "System.Runtime.GasLeft", Func: runtime.GasLeft, Price: 400}, {Name: "System.Runtime.GetCallingScriptHash", Func: engineGetCallingScriptHash, Price: 400}, {Name: "System.Runtime.GetEntryScriptHash", Func: engineGetEntryScriptHash, Price: 400}, {Name: "System.Runtime.GetExecutingScriptHash", Func: engineGetExecutingScriptHash, Price: 400}, diff --git a/pkg/interop/runtime/runtime.go b/pkg/interop/runtime/runtime.go index 916fed6fb..538f0f490 100644 --- a/pkg/interop/runtime/runtime.go +++ b/pkg/interop/runtime/runtime.go @@ -54,3 +54,9 @@ func Application() byte { func Verification() byte { return 0x00 } + +// GasLeft returns the amount of gas available for the current execution. +// This function uses `System.Runtime.GasLeft` syscall. +func GasLeft() int64 { + return 0 +}