core,vm: implement System.Runtime.GasLeft syscall

This commit is contained in:
Evgenii Stratonikov 2020-06-16 12:04:08 +03:00
parent a7d4fff897
commit a4e4439967
5 changed files with 31 additions and 0 deletions

View file

@ -29,6 +29,7 @@ var syscalls = map[string]map[string]string{
"GetEntryScriptHash": "System.Runtime.GetEntryScriptHash", "GetEntryScriptHash": "System.Runtime.GetEntryScriptHash",
"GetExecutingScriptHash": "System.Runtime.GetExecutingScriptHash", "GetExecutingScriptHash": "System.Runtime.GetExecutingScriptHash",
"GasLeft": "System.Runtime.GasLeft",
"GetTrigger": "System.Runtime.GetTrigger", "GetTrigger": "System.Runtime.GetTrigger",
"CheckWitness": "System.Runtime.CheckWitness", "CheckWitness": "System.Runtime.CheckWitness",
"Notify": "System.Runtime.Notify", "Notify": "System.Runtime.Notify",

View file

@ -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
}

View file

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/nspcc-dev/dbft/crypto" "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/core/state"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
@ -197,3 +198,13 @@ func TestContractCreateAccount(t *testing.T) {
require.Error(t, contractCreateStandardAccount(ic, v)) 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())
}

View file

@ -107,6 +107,7 @@ var systemInterops = []interop.Function{
{Name: "System.Json.Deserialize", Func: json.Deserialize, Price: 500000}, {Name: "System.Json.Deserialize", Func: json.Deserialize, Price: 500000},
{Name: "System.Json.Serialize", Func: json.Serialize, Price: 100000}, {Name: "System.Json.Serialize", Func: json.Serialize, Price: 100000},
{Name: "System.Runtime.CheckWitness", Func: runtime.CheckWitness, Price: 30000}, {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.GetCallingScriptHash", Func: engineGetCallingScriptHash, Price: 400},
{Name: "System.Runtime.GetEntryScriptHash", Func: engineGetEntryScriptHash, Price: 400}, {Name: "System.Runtime.GetEntryScriptHash", Func: engineGetEntryScriptHash, Price: 400},
{Name: "System.Runtime.GetExecutingScriptHash", Func: engineGetExecutingScriptHash, Price: 400}, {Name: "System.Runtime.GetExecutingScriptHash", Func: engineGetExecutingScriptHash, Price: 400},

View file

@ -54,3 +54,9 @@ func Application() byte {
func Verification() byte { func Verification() byte {
return 0x00 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
}