2019-11-05 08:36:13 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2019-11-05 08:36:13 +00:00
|
|
|
)
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// StackItemType represents type of the stack item.
|
|
|
|
type StackItemType byte
|
2019-11-05 08:36:13 +00:00
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// This block defines all known stack item types.
|
2019-11-05 08:36:13 +00:00
|
|
|
const (
|
2020-04-24 10:46:46 +00:00
|
|
|
AnyT StackItemType = 0x00
|
|
|
|
PointerT StackItemType = 0x10
|
|
|
|
BooleanT StackItemType = 0x20
|
|
|
|
IntegerT StackItemType = 0x21
|
|
|
|
ByteArrayT StackItemType = 0x28
|
|
|
|
BufferT StackItemType = 0x30
|
|
|
|
ArrayT StackItemType = 0x40
|
|
|
|
StructT StackItemType = 0x41
|
|
|
|
MapT StackItemType = 0x48
|
|
|
|
InteropT StackItemType = 0x60
|
2019-11-05 08:36:13 +00:00
|
|
|
)
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// String implements fmt.Stringer interface.
|
|
|
|
func (t StackItemType) String() string {
|
|
|
|
switch t {
|
|
|
|
case AnyT:
|
|
|
|
return "Any"
|
|
|
|
case PointerT:
|
|
|
|
return "Pointer"
|
|
|
|
case BooleanT:
|
|
|
|
return "Boolean"
|
|
|
|
case IntegerT:
|
|
|
|
return "Integer"
|
|
|
|
case ByteArrayT:
|
|
|
|
return "ByteArray"
|
|
|
|
case BufferT:
|
|
|
|
return "Buffer"
|
|
|
|
case ArrayT:
|
|
|
|
return "Array"
|
|
|
|
case StructT:
|
|
|
|
return "Struct"
|
|
|
|
case MapT:
|
|
|
|
return "Map"
|
|
|
|
case InteropT:
|
|
|
|
return "Interop"
|
|
|
|
default:
|
|
|
|
return "INVALID"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid checks if s is a well defined stack item type.
|
|
|
|
func (t StackItemType) IsValid() bool {
|
|
|
|
switch t {
|
|
|
|
case AnyT, PointerT, BooleanT, IntegerT, ByteArrayT, BufferT, ArrayT, StructT, MapT, InteropT:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 09:16:47 +00:00
|
|
|
// SerializeItem encodes given StackItem into the byte slice.
|
|
|
|
func SerializeItem(item StackItem) ([]byte, error) {
|
2019-11-05 08:36:13 +00:00
|
|
|
w := io.NewBufBinWriter()
|
2019-11-13 13:55:20 +00:00
|
|
|
EncodeBinaryStackItem(item, w.BinWriter)
|
2019-11-05 08:36:13 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return nil, w.Err
|
|
|
|
}
|
|
|
|
return w.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:55:20 +00:00
|
|
|
// EncodeBinaryStackItem encodes given StackItem into the given BinWriter. It's
|
|
|
|
// similar to io.Serializable's EncodeBinary, but works with StackItem
|
|
|
|
// interface.
|
|
|
|
func EncodeBinaryStackItem(item StackItem, w *io.BinWriter) {
|
|
|
|
serializeItemTo(item, w, make(map[StackItem]bool))
|
|
|
|
}
|
|
|
|
|
2019-11-05 08:57:55 +00:00
|
|
|
func serializeItemTo(item StackItem, w *io.BinWriter, seen map[StackItem]bool) {
|
|
|
|
if seen[item] {
|
2020-02-07 08:53:53 +00:00
|
|
|
w.Err = errors.New("recursive structures can't be serialized")
|
2019-11-05 08:57:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-05 08:36:13 +00:00
|
|
|
switch t := item.(type) {
|
|
|
|
case *ByteArrayItem:
|
2020-04-24 10:46:46 +00:00
|
|
|
w.WriteBytes([]byte{byte(ByteArrayT)})
|
2019-11-22 10:34:06 +00:00
|
|
|
w.WriteVarBytes(t.value)
|
2019-11-05 08:36:13 +00:00
|
|
|
case *BoolItem:
|
2020-04-24 10:46:46 +00:00
|
|
|
w.WriteBytes([]byte{byte(BooleanT)})
|
2019-12-12 15:52:23 +00:00
|
|
|
w.WriteBool(t.value)
|
2019-11-05 08:36:13 +00:00
|
|
|
case *BigIntegerItem:
|
2020-04-24 10:46:46 +00:00
|
|
|
w.WriteBytes([]byte{byte(IntegerT)})
|
2020-02-03 15:05:13 +00:00
|
|
|
w.WriteVarBytes(emit.IntToBytes(t.value))
|
2019-11-05 08:36:13 +00:00
|
|
|
case *InteropItem:
|
2020-02-07 08:53:53 +00:00
|
|
|
w.Err = errors.New("interop item can't be serialized")
|
2019-11-05 09:25:15 +00:00
|
|
|
case *ArrayItem, *StructItem:
|
2019-11-15 09:47:58 +00:00
|
|
|
seen[item] = true
|
|
|
|
|
2019-11-05 09:25:15 +00:00
|
|
|
_, isArray := t.(*ArrayItem)
|
|
|
|
if isArray {
|
2020-04-24 10:46:46 +00:00
|
|
|
w.WriteBytes([]byte{byte(ArrayT)})
|
2019-11-05 09:25:15 +00:00
|
|
|
} else {
|
2020-04-24 10:46:46 +00:00
|
|
|
w.WriteBytes([]byte{byte(StructT)})
|
2019-11-05 09:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
arr := t.Value().([]StackItem)
|
|
|
|
w.WriteVarUint(uint64(len(arr)))
|
|
|
|
for i := range arr {
|
|
|
|
serializeItemTo(arr[i], w, seen)
|
|
|
|
}
|
2019-11-05 08:36:13 +00:00
|
|
|
case *MapItem:
|
2019-11-15 09:47:58 +00:00
|
|
|
seen[item] = true
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
w.WriteBytes([]byte{byte(MapT)})
|
2019-11-05 08:57:55 +00:00
|
|
|
w.WriteVarUint(uint64(len(t.value)))
|
2020-03-31 10:30:38 +00:00
|
|
|
for i := range t.value {
|
|
|
|
serializeItemTo(t.value[i].Key, w, seen)
|
|
|
|
serializeItemTo(t.value[i].Value, w, seen)
|
2019-11-05 08:57:55 +00:00
|
|
|
}
|
2019-11-05 08:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 09:16:47 +00:00
|
|
|
// DeserializeItem decodes StackItem from the given byte slice.
|
|
|
|
func DeserializeItem(data []byte) (StackItem, error) {
|
2019-11-05 08:36:13 +00:00
|
|
|
r := io.NewBinReaderFromBuf(data)
|
2019-11-13 13:55:20 +00:00
|
|
|
item := DecodeBinaryStackItem(r)
|
2019-11-05 08:36:13 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:55:20 +00:00
|
|
|
// DecodeBinaryStackItem decodes previously serialized StackItem from the given
|
|
|
|
// reader. It's similar to the io.Serializable's DecodeBinary(), but implemented
|
|
|
|
// as a function because StackItem itself is an interface. Caveat: always check
|
|
|
|
// reader's error value before using the returned StackItem.
|
|
|
|
func DecodeBinaryStackItem(r *io.BinReader) StackItem {
|
2019-12-12 17:17:50 +00:00
|
|
|
var t = r.ReadB()
|
2019-11-05 08:36:13 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
switch StackItemType(t) {
|
|
|
|
case ByteArrayT:
|
2019-12-06 15:37:37 +00:00
|
|
|
data := r.ReadVarBytes()
|
2019-11-05 08:36:13 +00:00
|
|
|
return NewByteArrayItem(data)
|
2020-04-24 10:46:46 +00:00
|
|
|
case BooleanT:
|
2019-12-12 15:52:23 +00:00
|
|
|
var b = r.ReadBool()
|
2019-11-05 08:36:13 +00:00
|
|
|
return NewBoolItem(b)
|
2020-04-24 10:46:46 +00:00
|
|
|
case IntegerT:
|
2019-12-06 15:37:37 +00:00
|
|
|
data := r.ReadVarBytes()
|
2020-02-03 15:05:13 +00:00
|
|
|
num := emit.BytesToInt(data)
|
2019-11-05 09:07:08 +00:00
|
|
|
return &BigIntegerItem{
|
|
|
|
value: num,
|
|
|
|
}
|
2020-04-24 10:46:46 +00:00
|
|
|
case ArrayT, StructT:
|
2019-11-05 09:25:15 +00:00
|
|
|
size := int(r.ReadVarUint())
|
|
|
|
arr := make([]StackItem, size)
|
|
|
|
for i := 0; i < size; i++ {
|
2019-11-13 13:55:20 +00:00
|
|
|
arr[i] = DecodeBinaryStackItem(r)
|
2019-11-05 09:25:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
if StackItemType(t) == ArrayT {
|
2019-11-05 09:25:15 +00:00
|
|
|
return &ArrayItem{value: arr}
|
|
|
|
}
|
|
|
|
return &StructItem{value: arr}
|
2020-04-24 10:46:46 +00:00
|
|
|
case MapT:
|
2019-11-05 08:57:55 +00:00
|
|
|
size := int(r.ReadVarUint())
|
|
|
|
m := NewMapItem()
|
|
|
|
for i := 0; i < size; i++ {
|
2019-11-13 13:55:20 +00:00
|
|
|
key := DecodeBinaryStackItem(r)
|
2020-03-28 14:25:42 +00:00
|
|
|
value := DecodeBinaryStackItem(r)
|
2019-11-05 08:57:55 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
m.Add(key, value)
|
|
|
|
}
|
|
|
|
return m
|
2019-11-05 08:36:13 +00:00
|
|
|
default:
|
|
|
|
r.Err = errors.New("unknown type")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|