2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
import (
|
2019-11-05 08:36:13 +00:00
|
|
|
"errors"
|
2018-04-10 09:45:31 +00:00
|
|
|
"fmt"
|
2019-12-18 16:49:56 +00:00
|
|
|
"sort"
|
2020-04-15 14:13:50 +00:00
|
|
|
|
2020-08-13 07:41:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2018-04-10 09:45:31 +00:00
|
|
|
)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2020-07-28 13:38:00 +00:00
|
|
|
// interopIDFuncPrice adds an ID to the InteropFuncPrice.
|
|
|
|
type interopIDFuncPrice struct {
|
|
|
|
ID uint32
|
|
|
|
Func func(vm *VM) error
|
2020-07-22 16:40:32 +00:00
|
|
|
Price int64
|
2020-12-29 10:45:49 +00:00
|
|
|
RequiredFlags callflag.CallFlag
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:38:00 +00:00
|
|
|
var defaultVMInterops = []interopIDFuncPrice{
|
2020-08-14 10:50:52 +00:00
|
|
|
{ID: interopnames.ToID([]byte(interopnames.SystemRuntimeLog)),
|
2020-12-29 10:45:49 +00:00
|
|
|
Func: runtimeLog, Price: 1 << 15, RequiredFlags: callflag.AllowNotify},
|
2020-08-14 10:50:52 +00:00
|
|
|
{ID: interopnames.ToID([]byte(interopnames.SystemRuntimeNotify)),
|
2020-12-29 10:45:49 +00:00
|
|
|
Func: runtimeNotify, Price: 1 << 15, RequiredFlags: callflag.AllowNotify},
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:38:00 +00:00
|
|
|
func init() {
|
|
|
|
sort.Slice(defaultVMInterops, func(i, j int) bool { return defaultVMInterops[i].ID < defaultVMInterops[j].ID })
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:38:00 +00:00
|
|
|
func defaultSyscallHandler(v *VM, id uint32) error {
|
2019-12-18 16:49:56 +00:00
|
|
|
n := sort.Search(len(defaultVMInterops), func(i int) bool {
|
|
|
|
return defaultVMInterops[i].ID >= id
|
|
|
|
})
|
2020-07-28 13:38:00 +00:00
|
|
|
if n >= len(defaultVMInterops) || defaultVMInterops[n].ID != id {
|
|
|
|
return errors.New("syscall not found")
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
2020-07-28 13:38:00 +00:00
|
|
|
d := defaultVMInterops[n]
|
|
|
|
if !v.Context().callFlag.Has(d.RequiredFlags) {
|
|
|
|
return fmt.Errorf("missing call flags: %05b vs %05b", v.Context().callFlag, d.RequiredFlags)
|
|
|
|
}
|
|
|
|
return d.Func(v)
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 08:49:39 +00:00
|
|
|
// runtimeLog handles the syscall "System.Runtime.Log" for printing and logging stuff.
|
2018-04-10 09:45:31 +00:00
|
|
|
func runtimeLog(vm *VM) error {
|
2020-07-29 08:18:51 +00:00
|
|
|
msg := vm.Estack().Pop().String()
|
|
|
|
fmt.Printf("NEO-GO-VM (log) > %s\n", msg)
|
2018-04-10 09:45:31 +00:00
|
|
|
return nil
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 08:49:39 +00:00
|
|
|
// runtimeNotify handles the syscall "System.Runtime.Notify" for printing and logging stuff.
|
2018-04-10 09:45:31 +00:00
|
|
|
func runtimeNotify(vm *VM) error {
|
2020-07-29 08:18:51 +00:00
|
|
|
name := vm.Estack().Pop().String()
|
2018-04-10 09:45:31 +00:00
|
|
|
item := vm.Estack().Pop()
|
2020-07-29 08:18:51 +00:00
|
|
|
fmt.Printf("NEO-GO-VM (notify) > [%s] %s\n", name, item.Value())
|
2018-04-10 09:45:31 +00:00
|
|
|
return nil
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2019-11-05 08:36:13 +00:00
|
|
|
|
2019-12-18 16:49:56 +00:00
|
|
|
// init sorts the global defaultVMInterops value.
|
|
|
|
func init() {
|
|
|
|
sort.Slice(defaultVMInterops, func(i, j int) bool {
|
|
|
|
return defaultVMInterops[i].ID < defaultVMInterops[j].ID
|
|
|
|
})
|
|
|
|
}
|