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"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// GasLeft returns 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
|
|
|
|
|
|
|
// GetNotifications returns notifications emitted by current contract execution.
|
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),
|
2020-08-04 07:13:36 +00:00
|
|
|
stackitem.DeepCopy(notifications[i].Item).(*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
|
|
|
|
|
|
|
// GetInvocationCounter returns how many times current contract was invoked during 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()
|
2020-10-05 10:08:55 +00:00
|
|
|
count, ok := ic.VM.Invocations[currentScriptHash]
|
2020-06-16 09:47:42 +00:00
|
|
|
if !ok {
|
2020-10-02 15:42:23 +00:00
|
|
|
count = 1
|
2020-10-05 10:08:55 +00:00
|
|
|
ic.VM.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
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|