2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2019-10-10 13:47:27 +00:00
|
|
|
"encoding/binary"
|
2019-11-14 13:07:15 +00:00
|
|
|
"encoding/hex"
|
2018-04-02 15:04:42 +00:00
|
|
|
"encoding/json"
|
2018-03-30 16:15:06 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A StackItem represents the "real" value that is pushed on the stack.
|
|
|
|
type StackItem interface {
|
|
|
|
fmt.Stringer
|
|
|
|
Value() interface{}
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup duplicates current StackItem.
|
|
|
|
Dup() StackItem
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeStackItem(v interface{}) StackItem {
|
|
|
|
switch val := v.(type) {
|
|
|
|
case int:
|
2018-04-02 15:04:42 +00:00
|
|
|
return &BigIntegerItem{
|
2018-03-30 16:15:06 +00:00
|
|
|
value: big.NewInt(int64(val)),
|
|
|
|
}
|
2019-09-24 12:41:39 +00:00
|
|
|
case int64:
|
|
|
|
return &BigIntegerItem{
|
|
|
|
value: big.NewInt(val),
|
|
|
|
}
|
2019-10-10 13:47:27 +00:00
|
|
|
case uint8:
|
|
|
|
return &BigIntegerItem{
|
|
|
|
value: big.NewInt(int64(val)),
|
|
|
|
}
|
2019-10-08 09:29:18 +00:00
|
|
|
case uint16:
|
|
|
|
return &BigIntegerItem{
|
|
|
|
value: big.NewInt(int64(val)),
|
|
|
|
}
|
2019-10-03 13:23:21 +00:00
|
|
|
case uint32:
|
|
|
|
return &BigIntegerItem{
|
|
|
|
value: big.NewInt(int64(val)),
|
|
|
|
}
|
2019-10-10 13:47:27 +00:00
|
|
|
case uint64:
|
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(b, val)
|
|
|
|
bigInt := big.NewInt(0)
|
|
|
|
bigInt.SetBytes(b)
|
|
|
|
return &BigIntegerItem{
|
|
|
|
value: bigInt,
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
case []byte:
|
2018-04-02 15:04:42 +00:00
|
|
|
return &ByteArrayItem{
|
2018-03-30 16:15:06 +00:00
|
|
|
value: val,
|
|
|
|
}
|
2019-09-24 12:41:39 +00:00
|
|
|
case string:
|
|
|
|
return &ByteArrayItem{
|
|
|
|
value: []byte(val),
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
case bool:
|
2018-04-02 15:04:42 +00:00
|
|
|
return &BoolItem{
|
2018-03-30 16:15:06 +00:00
|
|
|
value: val,
|
|
|
|
}
|
|
|
|
case []StackItem:
|
2018-04-02 15:04:42 +00:00
|
|
|
return &ArrayItem{
|
2018-03-30 16:15:06 +00:00
|
|
|
value: val,
|
|
|
|
}
|
|
|
|
case *big.Int:
|
2018-04-02 15:04:42 +00:00
|
|
|
return &BigIntegerItem{
|
2018-03-30 16:15:06 +00:00
|
|
|
value: val,
|
|
|
|
}
|
|
|
|
case StackItem:
|
|
|
|
return val
|
2019-09-06 08:32:20 +00:00
|
|
|
case []int:
|
2019-10-18 15:38:33 +00:00
|
|
|
var a []StackItem
|
2019-09-06 08:32:20 +00:00
|
|
|
for _, i := range val {
|
|
|
|
a = append(a, makeStackItem(i))
|
|
|
|
}
|
|
|
|
return makeStackItem(a)
|
2018-03-30 16:15:06 +00:00
|
|
|
default:
|
|
|
|
panic(
|
|
|
|
fmt.Sprintf(
|
2018-04-02 15:04:42 +00:00
|
|
|
"invalid stack item type: %v (%v)",
|
2018-03-30 16:15:06 +00:00
|
|
|
val,
|
|
|
|
reflect.TypeOf(val),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// StructItem represents a struct on the stack.
|
|
|
|
type StructItem struct {
|
2018-03-30 16:15:06 +00:00
|
|
|
value []StackItem
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// NewStructItem returns an new StructItem object.
|
|
|
|
func NewStructItem(items []StackItem) *StructItem {
|
|
|
|
return &StructItem{
|
|
|
|
value: items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Value implements StackItem interface.
|
2018-04-02 15:04:42 +00:00
|
|
|
func (i *StructItem) Value() interface{} {
|
2018-03-30 16:15:06 +00:00
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
func (i *StructItem) String() string {
|
2018-03-30 16:15:06 +00:00
|
|
|
return "Struct"
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (i *StructItem) Dup() StackItem {
|
|
|
|
// it's a reference type, so no copying here.
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2019-09-18 11:03:15 +00:00
|
|
|
// Clone returns a Struct with all Struct fields copied by value.
|
|
|
|
// Array fields are still copied by reference.
|
|
|
|
func (i *StructItem) Clone() *StructItem {
|
|
|
|
ret := &StructItem{make([]StackItem, len(i.value))}
|
|
|
|
for j := range i.value {
|
|
|
|
switch t := i.value[j].(type) {
|
|
|
|
case *StructItem:
|
|
|
|
ret.value[j] = t.Clone()
|
|
|
|
default:
|
|
|
|
ret.value[j] = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// BigIntegerItem represents a big integer on the stack.
|
|
|
|
type BigIntegerItem struct {
|
2018-03-30 16:15:06 +00:00
|
|
|
value *big.Int
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// NewBigIntegerItem returns an new BigIntegerItem object.
|
|
|
|
func NewBigIntegerItem(value int) *BigIntegerItem {
|
|
|
|
return &BigIntegerItem{
|
|
|
|
value: big.NewInt(int64(value)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 09:07:08 +00:00
|
|
|
// Bytes converts i to a slice of bytes.
|
|
|
|
func (i *BigIntegerItem) Bytes() []byte {
|
2020-01-17 12:31:37 +00:00
|
|
|
return intToBytes(i.value)
|
2019-11-05 09:07:08 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Value implements StackItem interface.
|
2018-04-02 15:04:42 +00:00
|
|
|
func (i *BigIntegerItem) Value() interface{} {
|
2018-03-30 16:15:06 +00:00
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
func (i *BigIntegerItem) String() string {
|
2018-03-30 16:15:06 +00:00
|
|
|
return "BigInteger"
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (i *BigIntegerItem) Dup() StackItem {
|
|
|
|
n := new(big.Int)
|
|
|
|
return &BigIntegerItem{n.Set(i.value)}
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *BigIntegerItem) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(i.value)
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// BoolItem represents a boolean StackItem.
|
2018-04-02 15:04:42 +00:00
|
|
|
type BoolItem struct {
|
2018-03-30 16:15:06 +00:00
|
|
|
value bool
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// NewBoolItem returns an new BoolItem object.
|
|
|
|
func NewBoolItem(val bool) *BoolItem {
|
|
|
|
return &BoolItem{
|
|
|
|
value: val,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Value implements StackItem interface.
|
2018-04-02 15:04:42 +00:00
|
|
|
func (i *BoolItem) Value() interface{} {
|
2018-03-30 16:15:06 +00:00
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *BoolItem) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(i.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *BoolItem) String() string {
|
2018-03-30 16:15:06 +00:00
|
|
|
return "Bool"
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (i *BoolItem) Dup() StackItem {
|
|
|
|
return &BoolItem{i.value}
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// ByteArrayItem represents a byte array on the stack.
|
|
|
|
type ByteArrayItem struct {
|
2018-03-30 16:15:06 +00:00
|
|
|
value []byte
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// NewByteArrayItem returns an new ByteArrayItem object.
|
|
|
|
func NewByteArrayItem(b []byte) *ByteArrayItem {
|
|
|
|
return &ByteArrayItem{
|
|
|
|
value: b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Value implements StackItem interface.
|
2018-04-02 15:04:42 +00:00
|
|
|
func (i *ByteArrayItem) Value() interface{} {
|
2018-03-30 16:15:06 +00:00
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *ByteArrayItem) MarshalJSON() ([]byte, error) {
|
2019-11-14 13:07:15 +00:00
|
|
|
return json.Marshal(hex.EncodeToString(i.value))
|
2018-04-02 15:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ByteArrayItem) String() string {
|
2018-03-30 16:15:06 +00:00
|
|
|
return "ByteArray"
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (i *ByteArrayItem) Dup() StackItem {
|
|
|
|
a := make([]byte, len(i.value))
|
|
|
|
copy(a, i.value)
|
|
|
|
return &ByteArrayItem{a}
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// ArrayItem represents a new ArrayItem object.
|
|
|
|
type ArrayItem struct {
|
2018-03-30 16:15:06 +00:00
|
|
|
value []StackItem
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// NewArrayItem returns a new ArrayItem object.
|
|
|
|
func NewArrayItem(items []StackItem) *ArrayItem {
|
|
|
|
return &ArrayItem{
|
|
|
|
value: items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Value implements StackItem interface.
|
2018-04-02 15:04:42 +00:00
|
|
|
func (i *ArrayItem) Value() interface{} {
|
2018-03-30 16:15:06 +00:00
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *ArrayItem) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(i.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ArrayItem) String() string {
|
2018-03-30 16:15:06 +00:00
|
|
|
return "Array"
|
|
|
|
}
|
2019-09-24 12:06:23 +00:00
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (i *ArrayItem) Dup() StackItem {
|
|
|
|
// reference type
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2019-09-24 12:06:23 +00:00
|
|
|
// MapItem represents Map object.
|
|
|
|
type MapItem struct {
|
|
|
|
value map[interface{}]StackItem
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMapItem returns new MapItem object.
|
|
|
|
func NewMapItem() *MapItem {
|
|
|
|
return &MapItem{
|
|
|
|
value: make(map[interface{}]StackItem),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements StackItem interface.
|
|
|
|
func (i *MapItem) Value() interface{} {
|
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *MapItem) String() string {
|
|
|
|
return "Map"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has checks if map has specified key.
|
|
|
|
func (i *MapItem) Has(key StackItem) (ok bool) {
|
|
|
|
_, ok = i.value[toMapKey(key)]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (i *MapItem) Dup() StackItem {
|
|
|
|
// reference type
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2019-09-24 12:06:23 +00:00
|
|
|
// Add adds key-value pair to the map.
|
|
|
|
func (i *MapItem) Add(key, value StackItem) {
|
|
|
|
i.value[toMapKey(key)] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// toMapKey converts StackItem so that it can be used as a map key.
|
|
|
|
func toMapKey(key StackItem) interface{} {
|
|
|
|
switch t := key.(type) {
|
|
|
|
case *BoolItem:
|
|
|
|
return t.value
|
|
|
|
case *BigIntegerItem:
|
|
|
|
return t.value.Int64()
|
|
|
|
case *ByteArrayItem:
|
|
|
|
return string(t.value)
|
|
|
|
default:
|
|
|
|
panic("wrong key type")
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 17:11:01 +00:00
|
|
|
|
|
|
|
// InteropItem represents interop data on the stack.
|
|
|
|
type InteropItem struct {
|
|
|
|
value interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInteropItem returns new InteropItem object.
|
|
|
|
func NewInteropItem(value interface{}) *InteropItem {
|
|
|
|
return &InteropItem{
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements StackItem interface.
|
|
|
|
func (i *InteropItem) Value() interface{} {
|
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements stringer interface.
|
|
|
|
func (i *InteropItem) String() string {
|
|
|
|
return "InteropItem"
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (i *InteropItem) Dup() StackItem {
|
|
|
|
// reference type
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:11:01 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *InteropItem) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(i.value)
|
|
|
|
}
|