2020-05-18 14:11:27 +00:00
|
|
|
/*
|
|
|
|
Package runtime provides various service functions related to execution environment.
|
|
|
|
It has similar function to Runtime class in .net framwork for Neo.
|
|
|
|
*/
|
2018-08-20 08:59:35 +00:00
|
|
|
package runtime
|
|
|
|
|
2021-02-05 16:02:09 +00:00
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
|
|
|
|
)
|
2020-08-28 07:47:15 +00:00
|
|
|
|
2020-08-10 14:51:32 +00:00
|
|
|
// Trigger values to compare with GetTrigger result.
|
|
|
|
const (
|
2020-10-29 16:14:49 +00:00
|
|
|
OnPersist byte = 0x01
|
|
|
|
PostPersist byte = 0x02
|
2020-08-10 14:51:32 +00:00
|
|
|
Application byte = 0x40
|
|
|
|
Verification byte = 0x20
|
|
|
|
)
|
|
|
|
|
2021-04-29 08:33:21 +00:00
|
|
|
// BurnGas burns provided amount of GAS. It uses `System.Runtime.BurnGas` syscall.
|
|
|
|
func BurnGas(gas int) {
|
|
|
|
neogointernal.Syscall1NoReturn("System.Runtime.BurnGas", gas)
|
|
|
|
}
|
|
|
|
|
2020-05-18 14:11:27 +00:00
|
|
|
// CheckWitness verifies if the given script hash (160-bit BE value in a 20 byte
|
|
|
|
// slice) or key (compressed serialized 33-byte form) is one of the signers of
|
2020-06-10 08:49:39 +00:00
|
|
|
// this invocation. It uses `System.Runtime.CheckWitness` syscall.
|
2020-05-18 14:11:27 +00:00
|
|
|
func CheckWitness(hashOrKey []byte) bool {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall1("System.Runtime.CheckWitness", hashOrKey).(bool)
|
2018-08-20 08:59:35 +00:00
|
|
|
}
|
2018-08-21 08:55:03 +00:00
|
|
|
|
2020-05-18 14:11:27 +00:00
|
|
|
// Log instructs VM to log the given message. It's mostly used for debugging
|
|
|
|
// purposes as these messages are not saved anywhere normally and usually are
|
2020-06-10 08:49:39 +00:00
|
|
|
// only visible in the VM logs. This function uses `System.Runtime.Log` syscall.
|
2021-02-05 16:02:09 +00:00
|
|
|
func Log(message string) {
|
|
|
|
neogointernal.Syscall1NoReturn("System.Runtime.Log", message)
|
|
|
|
}
|
2018-08-21 10:57:48 +00:00
|
|
|
|
2020-05-18 14:11:27 +00:00
|
|
|
// Notify sends a notification (collecting all arguments in an array) to the
|
2020-06-29 08:25:32 +00:00
|
|
|
// executing environment. Unlike Log it can accept any data along with the event name
|
|
|
|
// and resulting notification is saved in application log. It's intended to be used as a
|
2020-05-18 14:11:27 +00:00
|
|
|
// part of contract's API to external systems, these events can be monitored
|
|
|
|
// from outside and act upon accordingly. This function uses
|
2020-06-10 08:49:39 +00:00
|
|
|
// `System.Runtime.Notify` syscall.
|
2021-02-05 16:02:09 +00:00
|
|
|
func Notify(name string, args ...interface{}) {
|
|
|
|
neogointernal.Syscall2NoReturn("System.Runtime.Notify", name, args)
|
|
|
|
}
|
2018-08-21 08:55:03 +00:00
|
|
|
|
2020-05-18 14:11:27 +00:00
|
|
|
// 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
|
|
|
|
// time of this (currently being processed) block. This function uses
|
2020-06-10 08:49:39 +00:00
|
|
|
// `System.Runtime.GetTime` syscall.
|
2018-08-21 10:57:48 +00:00
|
|
|
func GetTime() int {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall0("System.Runtime.GetTime").(int)
|
2018-08-21 10:57:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 14:11:27 +00:00
|
|
|
// GetTrigger returns the smart contract invocation trigger which can be either
|
|
|
|
// verification or application. It can be used to differentiate running contract
|
|
|
|
// as a part of verification process from running it as a regular application.
|
|
|
|
// Some interop functions (especially ones that change the state in any way) are
|
|
|
|
// not available when running with verification trigger. This function uses
|
2020-06-10 08:49:39 +00:00
|
|
|
// `System.Runtime.GetTrigger` syscall.
|
2018-08-21 10:57:48 +00:00
|
|
|
func GetTrigger() byte {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall0("System.Runtime.GetTrigger").(byte)
|
2018-08-21 10:57:48 +00:00
|
|
|
}
|
2018-08-21 08:55:03 +00:00
|
|
|
|
2020-06-16 09:04:08 +00:00
|
|
|
// GasLeft returns the amount of gas available for the current execution.
|
|
|
|
// This function uses `System.Runtime.GasLeft` syscall.
|
2021-03-04 10:14:24 +00:00
|
|
|
func GasLeft() int {
|
|
|
|
return neogointernal.Syscall0("System.Runtime.GasLeft").(int)
|
2020-06-16 09:04:08 +00:00
|
|
|
}
|
2020-06-16 09:30:25 +00:00
|
|
|
|
|
|
|
// GetNotifications returns notifications emitted by contract h.
|
|
|
|
// 'nil' literal means no filtering. It returns slice consisting of following elements:
|
|
|
|
// [ scripthash of notification's contract , emitted item ].
|
|
|
|
// This function uses `System.Runtime.GetNotifications` syscall.
|
2020-08-28 07:47:15 +00:00
|
|
|
func GetNotifications(h interop.Hash160) [][]interface{} {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall1("System.Runtime.GetNotifications", h).([][]interface{})
|
2020-06-16 09:30:25 +00:00
|
|
|
}
|
2020-06-16 09:47:42 +00:00
|
|
|
|
|
|
|
// GetInvocationCounter returns how many times current contract was invoked during current tx execution.
|
|
|
|
// This function uses `System.Runtime.GetInvocationCounter` syscall.
|
|
|
|
func GetInvocationCounter() int {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall0("System.Runtime.GetInvocationCounter").(int)
|
2020-06-16 09:47:42 +00:00
|
|
|
}
|
2020-07-21 12:11:17 +00:00
|
|
|
|
|
|
|
// Platform returns the platform name, which is set to be `NEO`. This function uses
|
|
|
|
// `System.Runtime.Platform` syscall.
|
|
|
|
func Platform() []byte {
|
2021-02-05 16:02:09 +00:00
|
|
|
return neogointernal.Syscall0("System.Runtime.Platform").([]byte)
|
2020-07-21 12:11:17 +00:00
|
|
|
}
|