2019-11-05 08:36:13 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stackItemType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
byteArrayT stackItemType = 0x00
|
|
|
|
booleanT stackItemType = 0x01
|
|
|
|
integerT stackItemType = 0x02
|
|
|
|
arrayT stackItemType = 0x80
|
|
|
|
structT stackItemType = 0x81
|
|
|
|
mapT stackItemType = 0x82
|
|
|
|
)
|
|
|
|
|
|
|
|
func serializeItem(item StackItem) ([]byte, error) {
|
|
|
|
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] {
|
|
|
|
w.Err = errors.New("recursive structures are not supported")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-05 08:36:13 +00:00
|
|
|
switch t := item.(type) {
|
|
|
|
case *ByteArrayItem:
|
2019-12-06 15:22:21 +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:
|
2019-12-06 15:22:21 +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:
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes([]byte{byte(integerT)})
|
2020-01-17 12:31:37 +00:00
|
|
|
w.WriteVarBytes(intToBytes(t.value))
|
2019-11-05 08:36:13 +00:00
|
|
|
case *InteropItem:
|
|
|
|
w.Err = errors.New("not supported")
|
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 {
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes([]byte{byte(arrayT)})
|
2019-11-05 09:25:15 +00:00
|
|
|
} else {
|
2019-12-06 15:22:21 +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
|
|
|
|
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes([]byte{byte(mapT)})
|
2019-11-05 08:57:55 +00:00
|
|
|
w.WriteVarUint(uint64(len(t.value)))
|
|
|
|
for k, v := range t.value {
|
|
|
|
serializeItemTo(v, w, seen)
|
|
|
|
serializeItemTo(makeStackItem(k), w, seen)
|
|
|
|
}
|
2019-11-05 08:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserializeItem(data []byte) (StackItem, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
case integerT:
|
2019-12-06 15:37:37 +00:00
|
|
|
data := r.ReadVarBytes()
|
2020-01-17 12:31:37 +00:00
|
|
|
num := bytesToInt(data)
|
2019-11-05 09:07:08 +00:00
|
|
|
return &BigIntegerItem{
|
|
|
|
value: num,
|
|
|
|
}
|
2019-11-05 09:25:15 +00:00
|
|
|
case arrayT, structT:
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if stackItemType(t) == arrayT {
|
|
|
|
return &ArrayItem{value: arr}
|
|
|
|
}
|
|
|
|
return &StructItem{value: arr}
|
2019-11-05 08:36:13 +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
|
|
|
value := DecodeBinaryStackItem(r)
|
|
|
|
key := 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
|
|
|
|
}
|
|
|
|
}
|