2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
import (
|
2019-12-18 16:49:56 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/binary"
|
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"
|
2018-04-10 09:45:31 +00:00
|
|
|
)
|
2018-03-30 16:15:06 +00:00
|
|
|
|
|
|
|
// InteropFunc allows to hook into the VM.
|
|
|
|
type InteropFunc func(vm *VM) error
|
|
|
|
|
2019-12-18 16:49:56 +00:00
|
|
|
// InteropFuncPrice represents an interop function with a price.
|
|
|
|
type InteropFuncPrice struct {
|
|
|
|
Func InteropFunc
|
|
|
|
Price int
|
|
|
|
}
|
|
|
|
|
|
|
|
// interopIDFuncPrice adds an ID to the InteropFuncPrice.
|
|
|
|
type interopIDFuncPrice struct {
|
|
|
|
ID uint32
|
|
|
|
InteropFuncPrice
|
|
|
|
}
|
|
|
|
|
|
|
|
// InteropGetterFunc is a function that returns an interop function-price
|
|
|
|
// structure by the given interop ID.
|
|
|
|
type InteropGetterFunc func(uint32) *InteropFuncPrice
|
|
|
|
|
|
|
|
var defaultVMInterops = []interopIDFuncPrice{
|
|
|
|
{InteropNameToID([]byte("Neo.Runtime.Log")),
|
|
|
|
InteropFuncPrice{runtimeLog, 1}},
|
|
|
|
{InteropNameToID([]byte("Neo.Runtime.Notify")),
|
|
|
|
InteropFuncPrice{runtimeNotify, 1}},
|
|
|
|
{InteropNameToID([]byte("Neo.Runtime.Serialize")),
|
|
|
|
InteropFuncPrice{RuntimeSerialize, 1}},
|
|
|
|
{InteropNameToID([]byte("System.Runtime.Serialize")),
|
|
|
|
InteropFuncPrice{RuntimeSerialize, 1}},
|
|
|
|
{InteropNameToID([]byte("Neo.Runtime.Deserialize")),
|
|
|
|
InteropFuncPrice{RuntimeDeserialize, 1}},
|
|
|
|
{InteropNameToID([]byte("System.Runtime.Deserialize")),
|
|
|
|
InteropFuncPrice{RuntimeDeserialize, 1}},
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDefaultVMInterop(id uint32) *InteropFuncPrice {
|
|
|
|
n := sort.Search(len(defaultVMInterops), func(i int) bool {
|
|
|
|
return defaultVMInterops[i].ID >= id
|
|
|
|
})
|
|
|
|
if n < len(defaultVMInterops) && defaultVMInterops[n].ID == id {
|
|
|
|
return &defaultVMInterops[n].InteropFuncPrice
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InteropNameToID returns an identificator of the method based on its name.
|
|
|
|
func InteropNameToID(name []byte) uint32 {
|
|
|
|
h := sha256.Sum256(name)
|
|
|
|
return binary.LittleEndian.Uint32(h[:4])
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// runtimeLog handles the syscall "Neo.Runtime.Log" for printing and logging stuff.
|
2018-04-10 09:45:31 +00:00
|
|
|
func runtimeLog(vm *VM) error {
|
|
|
|
item := vm.Estack().Pop()
|
2019-10-03 13:12:24 +00:00
|
|
|
fmt.Printf("NEO-GO-VM (log) > %s\n", item.Value())
|
2018-04-10 09:45:31 +00:00
|
|
|
return nil
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// runtimeNotify handles the syscall "Neo.Runtime.Notify" for printing and logging stuff.
|
2018-04-10 09:45:31 +00:00
|
|
|
func runtimeNotify(vm *VM) error {
|
|
|
|
item := vm.Estack().Pop()
|
2019-10-03 13:12:24 +00:00
|
|
|
fmt.Printf("NEO-GO-VM (notify) > %s\n", 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-11-05 14:10:52 +00:00
|
|
|
// RuntimeSerialize handles syscalls System.Runtime.Serialize and Neo.Runtime.Serialize.
|
|
|
|
func RuntimeSerialize(vm *VM) error {
|
2019-11-05 08:36:13 +00:00
|
|
|
item := vm.Estack().Pop()
|
|
|
|
data, err := serializeItem(item.value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(data) > MaxItemSize {
|
|
|
|
return errors.New("too big item")
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.Estack().PushVal(data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:10:52 +00:00
|
|
|
// RuntimeDeserialize handles syscalls System.Runtime.Deserialize and Neo.Runtime.Deserialize.
|
|
|
|
func RuntimeDeserialize(vm *VM) error {
|
2019-11-05 08:36:13 +00:00
|
|
|
data := vm.Estack().Pop().Bytes()
|
|
|
|
|
|
|
|
item, err := deserializeItem(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vm.Estack().Push(&Element{value: item})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
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
|
|
|
|
})
|
|
|
|
}
|