2020-06-16 09:04:08 +00:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
2021-07-14 12:05:28 +00:00
|
|
|
"encoding/binary"
|
2020-08-06 14:44:08 +00:00
|
|
|
"errors"
|
2021-08-30 20:43:17 +00:00
|
|
|
"math/big"
|
2020-08-06 14:44:08 +00:00
|
|
|
|
2020-06-16 09:04:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-06-16 09:30:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2022-04-21 14:53:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2021-07-14 12:05:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-06-16 09:30:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-06-16 09:04:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-06-16 09:30:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2021-07-14 12:05:28 +00:00
|
|
|
"github.com/twmb/murmur3"
|
2020-06-16 09:04:08 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GasLeft returns the remaining amount of GAS.
|
2020-08-07 11:37:49 +00:00
|
|
|
func GasLeft(ic *interop.Context) error {
|
|
|
|
if ic.VM.GasLimit == -1 {
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(ic.VM.GasLimit)))
|
2020-07-21 11:23:52 +00:00
|
|
|
} else {
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(ic.VM.GasLimit - ic.VM.GasConsumed())))
|
2020-07-21 11:23:52 +00:00
|
|
|
}
|
2020-06-16 09:04:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-16 09:30:25 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetNotifications returns notifications emitted in the current execution context.
|
2020-08-07 11:37:49 +00:00
|
|
|
func GetNotifications(ic *interop.Context) error {
|
|
|
|
item := ic.VM.Estack().Pop().Item()
|
2020-06-16 09:30:25 +00:00
|
|
|
notifications := ic.Notifications
|
|
|
|
if _, ok := item.(stackitem.Null); !ok {
|
|
|
|
b, err := item.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u, err := util.Uint160DecodeBytesBE(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
notifications = []state.NotificationEvent{}
|
|
|
|
for i := range ic.Notifications {
|
|
|
|
if ic.Notifications[i].ScriptHash.Equals(u) {
|
|
|
|
notifications = append(notifications, ic.Notifications[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(notifications) > vm.MaxStackSize {
|
|
|
|
return errors.New("too many notifications")
|
|
|
|
}
|
|
|
|
arr := stackitem.NewArray(make([]stackitem.Item, 0, len(notifications)))
|
|
|
|
for i := range notifications {
|
|
|
|
ev := stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(notifications[i].ScriptHash.BytesBE()),
|
2020-06-29 08:25:32 +00:00
|
|
|
stackitem.Make(notifications[i].Name),
|
2022-05-30 07:41:00 +00:00
|
|
|
stackitem.DeepCopy(notifications[i].Item, false).(*stackitem.Array),
|
2020-06-16 09:30:25 +00:00
|
|
|
})
|
|
|
|
arr.Append(ev)
|
|
|
|
}
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(arr)
|
2020-06-16 09:30:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-16 09:47:42 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetInvocationCounter returns how many times the current contract has been invoked during the current tx execution.
|
2020-08-07 11:37:49 +00:00
|
|
|
func GetInvocationCounter(ic *interop.Context) error {
|
2020-10-02 15:42:23 +00:00
|
|
|
currentScriptHash := ic.VM.GetCurrentScriptHash()
|
2021-11-19 17:25:58 +00:00
|
|
|
count, ok := ic.Invocations[currentScriptHash]
|
2020-06-16 09:47:42 +00:00
|
|
|
if !ok {
|
2020-10-02 15:42:23 +00:00
|
|
|
count = 1
|
2021-11-19 17:25:58 +00:00
|
|
|
ic.Invocations[currentScriptHash] = count
|
2020-06-16 09:47:42 +00:00
|
|
|
}
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(count))))
|
2020-06-16 09:47:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-06 11:20:25 +00:00
|
|
|
|
2022-04-21 14:53:11 +00:00
|
|
|
// GetAddressVersion returns the address version of the current protocol.
|
|
|
|
func GetAddressVersion(ic *interop.Context) error {
|
|
|
|
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(address.NEO3Prefix))))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:20:25 +00:00
|
|
|
// GetNetwork returns chain network number.
|
|
|
|
func GetNetwork(ic *interop.Context) error {
|
|
|
|
m := ic.Chain.GetConfig().Magic
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(m))))
|
2021-07-06 11:20:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-14 12:05:28 +00:00
|
|
|
|
|
|
|
// GetRandom returns pseudo-random number which depends on block nonce and transaction hash.
|
|
|
|
func GetRandom(ic *interop.Context) error {
|
|
|
|
res := murmur128(ic.NonceData[:], ic.Network)
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewBigInteger(bigint.FromBytesUnsigned(res)))
|
2021-07-14 12:05:28 +00:00
|
|
|
copy(ic.NonceData[:], res)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func murmur128(data []byte, seed uint32) []byte {
|
|
|
|
h1, h2 := murmur3.SeedSum128(uint64(seed), uint64(seed), data)
|
|
|
|
result := make([]byte, 16)
|
|
|
|
binary.LittleEndian.PutUint64(result, h1)
|
|
|
|
binary.LittleEndian.PutUint64(result[8:], h2)
|
|
|
|
return result
|
|
|
|
}
|