core,vm: adjust binary (de-)serialization syscalls

Related #1027.

1. Move System.Runtime.(De)serialize to System.Binary.*
2. Rename compiler stubs.
3. Adjust opcode prices.
This commit is contained in:
Evgenii Stratonikov 2020-06-16 10:56:06 +03:00
parent b3507d490d
commit 18066143bd
6 changed files with 32 additions and 28 deletions

View file

@ -25,8 +25,8 @@ var syscalls = map[string]map[string]string{
"Notify": "System.Runtime.Notify", "Notify": "System.Runtime.Notify",
"Log": "System.Runtime.Log", "Log": "System.Runtime.Log",
"GetTime": "System.Runtime.GetTime", "GetTime": "System.Runtime.GetTime",
"Serialize": "System.Runtime.Serialize", "Serialize": "System.Binary.Serialize",
"Deserialize": "System.Runtime.Deserialize", "Deserialize": "System.Binary.Deserialize",
}, },
"blockchain": { "blockchain": {
"GetBlock": "System.Blockchain.GetBlock", "GetBlock": "System.Blockchain.GetBlock",

View file

@ -68,6 +68,8 @@ func getInteropFromSlice(ic *interop.Context, slice []interop.Function) func(uin
// All lists are sorted, keep 'em this way, please. // All lists are sorted, keep 'em this way, please.
var systemInterops = []interop.Function{ var systemInterops = []interop.Function{
{Name: "System.Binary.Deserialize", Func: runtimeDeserialize, Price: 500000},
{Name: "System.Binary.Serialize", Func: runtimeSerialize, Price: 100000},
{Name: "System.Blockchain.GetBlock", Func: bcGetBlock, Price: 250, {Name: "System.Blockchain.GetBlock", Func: bcGetBlock, Price: 250,
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates}, AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
{Name: "System.Blockchain.GetContract", Func: bcGetContract, Price: 100, {Name: "System.Blockchain.GetContract", Func: bcGetContract, Price: 100,
@ -104,14 +106,12 @@ var systemInterops = []interop.Function{
{Name: "System.Iterator.Keys", Func: iterator.Keys, Price: 1}, {Name: "System.Iterator.Keys", Func: iterator.Keys, Price: 1},
{Name: "System.Iterator.Values", Func: iterator.Values, Price: 1}, {Name: "System.Iterator.Values", Func: iterator.Values, Price: 1},
{Name: "System.Runtime.CheckWitness", Func: runtime.CheckWitness, Price: 200, RequiredFlags: smartcontract.AllowStates}, {Name: "System.Runtime.CheckWitness", Func: runtime.CheckWitness, Price: 200, RequiredFlags: smartcontract.AllowStates},
{Name: "System.Runtime.Deserialize", Func: runtimeDeserialize, Price: 1},
{Name: "System.Runtime.GetTime", Func: runtimeGetTime, Price: 1, {Name: "System.Runtime.GetTime", Func: runtimeGetTime, Price: 1,
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates}, AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
{Name: "System.Runtime.GetTrigger", Func: runtimeGetTrigger, Price: 1}, {Name: "System.Runtime.GetTrigger", Func: runtimeGetTrigger, Price: 1},
{Name: "System.Runtime.Log", Func: runtimeLog, Price: 1, RequiredFlags: smartcontract.AllowNotify}, {Name: "System.Runtime.Log", Func: runtimeLog, Price: 1, RequiredFlags: smartcontract.AllowNotify},
{Name: "System.Runtime.Notify", Func: runtimeNotify, Price: 1, RequiredFlags: smartcontract.AllowNotify}, {Name: "System.Runtime.Notify", Func: runtimeNotify, Price: 1, RequiredFlags: smartcontract.AllowNotify},
{Name: "System.Runtime.Platform", Func: runtimePlatform, Price: 1}, {Name: "System.Runtime.Platform", Func: runtimePlatform, Price: 1},
{Name: "System.Runtime.Serialize", Func: runtimeSerialize, Price: 1},
{Name: "System.Storage.Delete", Func: storageDelete, Price: StoragePrice, {Name: "System.Storage.Delete", Func: storageDelete, Price: StoragePrice,
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates}, AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates},
{Name: "System.Storage.Find", Func: storageFind, Price: 1000000, {Name: "System.Storage.Find", Func: storageFind, Price: 1000000,

View file

@ -0,0 +1,18 @@
/*
Package binary provides binary serialization routines.
*/
package binary
// Serialize serializes any given item into a byte slice. It works for all
// regular VM types (not ones from interop package) and allows to save them in
// storage or pass into Notify and then Deserialize them on the next run or in
// the external event receiver. It uses `System.Binary.Serialize` syscall.
func Serialize(item interface{}) []byte {
return nil
}
// Deserialize unpacks previously serialized value from a byte slice, it's the
// opposite of Serialize. It uses `System.Binary.Deserialize` syscall.
func Deserialize(b []byte) interface{} {
return nil
}

View file

@ -54,17 +54,3 @@ func Application() byte {
func Verification() byte { func Verification() byte {
return 0x00 return 0x00
} }
// Serialize serializes any given item into a byte slice. It works for all
// regular VM types (not ones from interop package) and allows to save them in
// storage or pass into Notify and then Deserialize them on the next run or in
// the external event receiver. It uses `System.Runtime.Serialize` syscall.
func Serialize(item interface{}) []byte {
return nil
}
// Deserialize unpacks previously serialized value from a byte slice, it's the
// opposite of Serialize. It uses `System.Runtime.Deserialize` syscall.
func Deserialize(b []byte) interface{} {
return nil
}

View file

@ -35,14 +35,14 @@ type interopIDFuncPrice struct {
type InteropGetterFunc func(uint32) *InteropFuncPrice type InteropGetterFunc func(uint32) *InteropFuncPrice
var defaultVMInterops = []interopIDFuncPrice{ var defaultVMInterops = []interopIDFuncPrice{
{emit.InteropNameToID([]byte("System.Binary.Deserialize")),
InteropFuncPrice{Func: RuntimeDeserialize, Price: 500000}},
{emit.InteropNameToID([]byte("System.Binary.Serialize")),
InteropFuncPrice{Func: RuntimeSerialize, Price: 100000}},
{emit.InteropNameToID([]byte("System.Runtime.Log")), {emit.InteropNameToID([]byte("System.Runtime.Log")),
InteropFuncPrice{Func: runtimeLog, Price: 1}}, InteropFuncPrice{Func: runtimeLog, Price: 1}},
{emit.InteropNameToID([]byte("System.Runtime.Notify")), {emit.InteropNameToID([]byte("System.Runtime.Notify")),
InteropFuncPrice{Func: runtimeNotify, Price: 1}}, InteropFuncPrice{Func: runtimeNotify, Price: 1}},
{emit.InteropNameToID([]byte("System.Runtime.Serialize")),
InteropFuncPrice{Func: RuntimeSerialize, Price: 1}},
{emit.InteropNameToID([]byte("System.Runtime.Deserialize")),
InteropFuncPrice{Func: RuntimeDeserialize, Price: 1}},
{emit.InteropNameToID([]byte("System.Enumerator.Create")), {emit.InteropNameToID([]byte("System.Enumerator.Create")),
InteropFuncPrice{Func: EnumeratorCreate, Price: 1}}, InteropFuncPrice{Func: EnumeratorCreate, Price: 1}},
{emit.InteropNameToID([]byte("System.Enumerator.Next")), {emit.InteropNameToID([]byte("System.Enumerator.Next")),
@ -87,7 +87,7 @@ func runtimeNotify(vm *VM) error {
return nil return nil
} }
// RuntimeSerialize handles syscalls System.Runtime.Serialize and System.Runtime.Serialize. // RuntimeSerialize handles System.Binary.Serialize syscall.
func RuntimeSerialize(vm *VM) error { func RuntimeSerialize(vm *VM) error {
item := vm.Estack().Pop() item := vm.Estack().Pop()
data, err := stackitem.SerializeItem(item.value) data, err := stackitem.SerializeItem(item.value)
@ -102,7 +102,7 @@ func RuntimeSerialize(vm *VM) error {
return nil return nil
} }
// RuntimeDeserialize handles syscalls System.Runtime.Deserialize and System.Runtime.Deserialize. // RuntimeDeserialize handles System.Binary.Deserialize syscall.
func RuntimeDeserialize(vm *VM) error { func RuntimeDeserialize(vm *VM) error {
data := vm.Estack().Pop().Bytes() data := vm.Estack().Pop().Bytes()

View file

@ -656,8 +656,8 @@ func getSyscallProg(name string) (prog []byte) {
} }
func getSerializeProg() (prog []byte) { func getSerializeProg() (prog []byte) {
prog = append(prog, getSyscallProg("System.Runtime.Serialize")...) prog = append(prog, getSyscallProg("System.Binary.Serialize")...)
prog = append(prog, getSyscallProg("System.Runtime.Deserialize")...) prog = append(prog, getSyscallProg("System.Binary.Deserialize")...)
prog = append(prog, byte(opcode.RET)) prog = append(prog, byte(opcode.RET))
return return
@ -762,7 +762,7 @@ func TestSerializeStruct(t *testing.T) {
} }
func TestDeserializeUnknown(t *testing.T) { func TestDeserializeUnknown(t *testing.T) {
prog := append(getSyscallProg("System.Runtime.Deserialize"), byte(opcode.RET)) prog := append(getSyscallProg("System.Binary.Deserialize"), byte(opcode.RET))
data, err := stackitem.SerializeItem(stackitem.NewBigInteger(big.NewInt(123))) data, err := stackitem.SerializeItem(stackitem.NewBigInteger(big.NewInt(123)))
require.NoError(t, err) require.NoError(t, err)
@ -798,7 +798,7 @@ func TestSerializeMapCompat(t *testing.T) {
emit.Bytes(buf.BinWriter, []byte("key")) emit.Bytes(buf.BinWriter, []byte("key"))
emit.Bytes(buf.BinWriter, []byte("value")) emit.Bytes(buf.BinWriter, []byte("value"))
emit.Opcode(buf.BinWriter, opcode.SETITEM) emit.Opcode(buf.BinWriter, opcode.SETITEM)
emit.Syscall(buf.BinWriter, "System.Runtime.Serialize") emit.Syscall(buf.BinWriter, "System.Binary.Serialize")
require.NoError(t, buf.Err) require.NoError(t, buf.Err)
vm := load(buf.Bytes()) vm := load(buf.Bytes())