2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2020-03-11 13:44:10 +00:00
|
|
|
"bytes"
|
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"
|
2020-03-11 13:04:28 +00:00
|
|
|
"errors"
|
2018-03-30 16:15:06 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
2020-02-03 15:05:13 +00:00
|
|
|
|
2020-05-06 13:31:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-05-06 13:31:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2018-03-30 16:15:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2020-04-28 08:23:58 +00:00
|
|
|
// Bool converts StackItem to a boolean value.
|
|
|
|
Bool() bool
|
2020-03-11 13:04:28 +00:00
|
|
|
// TryBytes converts StackItem to a byte slice.
|
|
|
|
TryBytes() ([]byte, error)
|
2020-03-19 15:21:56 +00:00
|
|
|
// TryInteger converts StackItem to an integer.
|
|
|
|
TryInteger() (*big.Int, error)
|
2020-03-11 13:44:10 +00:00
|
|
|
// Equals checks if 2 StackItems are equal.
|
|
|
|
Equals(s StackItem) bool
|
2020-02-21 14:56:28 +00:00
|
|
|
// ToContractParameter converts StackItem to smartcontract.Parameter
|
2020-03-03 09:52:56 +00:00
|
|
|
ToContractParameter(map[StackItem]bool) smartcontract.Parameter
|
2020-04-24 10:46:46 +00:00
|
|
|
// Type returns stack item type.
|
|
|
|
Type() StackItemType
|
2020-04-28 08:24:02 +00:00
|
|
|
// Convert converts StackItem to another type.
|
|
|
|
Convert(StackItemType) (StackItem, error)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
var errInvalidConversion = errors.New("invalid conversion type")
|
|
|
|
|
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:
|
2020-04-29 14:13:46 +00:00
|
|
|
return NewBigIntegerItem(val)
|
2018-03-30 16:15:06 +00:00
|
|
|
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:
|
2020-03-18 08:45:15 +00:00
|
|
|
i64T := reflect.TypeOf(int64(0))
|
|
|
|
if reflect.TypeOf(val).ConvertibleTo(i64T) {
|
|
|
|
i64Val := reflect.ValueOf(val).Convert(i64T).Interface()
|
|
|
|
return makeStackItem(i64Val)
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
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),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
// convertPrimitive converts primitive item to a specified type.
|
|
|
|
func convertPrimitive(item StackItem, typ StackItemType) (StackItem, error) {
|
|
|
|
if item.Type() == typ {
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
switch typ {
|
|
|
|
case IntegerT:
|
|
|
|
bi, err := item.TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewBigIntegerItem(bi), nil
|
|
|
|
case ByteArrayT:
|
|
|
|
b, err := item.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewByteArrayItem(b), nil
|
|
|
|
case BufferT:
|
|
|
|
panic("TODO") // #877
|
|
|
|
case BooleanT:
|
|
|
|
return NewBoolItem(item.Bool()), nil
|
|
|
|
default:
|
|
|
|
return nil, errInvalidConversion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-28 08:23:58 +00:00
|
|
|
// Bool implements StackItem interface.
|
|
|
|
func (i *StructItem) Bool() bool { return true }
|
|
|
|
|
2020-03-11 13:04:28 +00:00
|
|
|
// TryBytes implements StackItem interface.
|
|
|
|
func (i *StructItem) TryBytes() ([]byte, error) {
|
|
|
|
return nil, errors.New("can't convert Struct to ByteArray")
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:21:56 +00:00
|
|
|
// TryInteger implements StackItem interface.
|
|
|
|
func (i *StructItem) TryInteger() (*big.Int, error) {
|
|
|
|
return nil, errors.New("can't convert Struct to Integer")
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:44:10 +00:00
|
|
|
// Equals implements StackItem interface.
|
|
|
|
func (i *StructItem) Equals(s StackItem) bool {
|
|
|
|
if i == s {
|
|
|
|
return true
|
|
|
|
} else if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
val, ok := s.(*StructItem)
|
|
|
|
if !ok || len(i.value) != len(val.value) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for j := range i.value {
|
|
|
|
if !i.value[j].Equals(val.value[j]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:56:28 +00:00
|
|
|
// ToContractParameter implements StackItem interface.
|
2020-03-03 09:52:56 +00:00
|
|
|
func (i *StructItem) ToContractParameter(seen map[StackItem]bool) smartcontract.Parameter {
|
2020-02-21 14:56:28 +00:00
|
|
|
var value []smartcontract.Parameter
|
2020-03-03 09:52:56 +00:00
|
|
|
|
|
|
|
if !seen[i] {
|
|
|
|
seen[i] = true
|
|
|
|
for _, stackItem := range i.value {
|
|
|
|
parameter := stackItem.ToContractParameter(seen)
|
|
|
|
value = append(value, parameter)
|
|
|
|
}
|
2020-02-21 14:56:28 +00:00
|
|
|
}
|
|
|
|
return smartcontract.Parameter{
|
|
|
|
Type: smartcontract.ArrayType,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// Type implements StackItem interface.
|
|
|
|
func (i *StructItem) Type() StackItemType { return StructT }
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
// Convert implements StackItem interface.
|
|
|
|
func (i *StructItem) Convert(typ StackItemType) (StackItem, error) {
|
|
|
|
switch typ {
|
|
|
|
case StructT:
|
|
|
|
return i, nil
|
|
|
|
case ArrayT:
|
|
|
|
return NewArrayItem(i.value), nil
|
|
|
|
case BooleanT:
|
|
|
|
return NewBoolItem(i.Bool()), nil
|
|
|
|
default:
|
|
|
|
return nil, errInvalidConversion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-23 08:50:03 +00:00
|
|
|
// NullItem represents null on the stack.
|
|
|
|
type NullItem struct{}
|
|
|
|
|
|
|
|
// String implements StackItem interface.
|
|
|
|
func (i NullItem) String() string {
|
|
|
|
return "Null"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements StackItem interface.
|
|
|
|
func (i NullItem) Value() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dup implements StackItem interface.
|
|
|
|
// There is no need to perform a real copy here,
|
|
|
|
// as NullItem has no internal state.
|
|
|
|
func (i NullItem) Dup() StackItem {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2020-04-28 08:23:58 +00:00
|
|
|
// Bool implements StackItem interface.
|
|
|
|
func (i NullItem) Bool() bool { return false }
|
|
|
|
|
2020-03-23 08:50:03 +00:00
|
|
|
// TryBytes implements StackItem interface.
|
|
|
|
func (i NullItem) TryBytes() ([]byte, error) {
|
|
|
|
return nil, errors.New("can't convert Null to ByteArray")
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:21:56 +00:00
|
|
|
// TryInteger implements StackItem interface.
|
|
|
|
func (i NullItem) TryInteger() (*big.Int, error) {
|
|
|
|
return nil, errors.New("can't convert Null to Integer")
|
|
|
|
}
|
|
|
|
|
2020-03-23 08:50:03 +00:00
|
|
|
// Equals implements StackItem interface.
|
|
|
|
func (i NullItem) Equals(s StackItem) bool {
|
|
|
|
_, ok := s.(NullItem)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToContractParameter implements StackItem interface.
|
|
|
|
func (i NullItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
|
|
|
return smartcontract.Parameter{
|
|
|
|
Type: smartcontract.AnyType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// Type implements StackItem interface.
|
|
|
|
func (i NullItem) Type() StackItemType { return AnyT }
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
// Convert implements StackItem interface.
|
|
|
|
func (i NullItem) Convert(typ StackItemType) (StackItem, error) {
|
|
|
|
if typ == AnyT || !typ.IsValid() {
|
|
|
|
return nil, errInvalidConversion
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
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.
|
2020-04-16 12:28:34 +00:00
|
|
|
func NewBigIntegerItem(value *big.Int) *BigIntegerItem {
|
2020-04-29 14:13:46 +00:00
|
|
|
if value.BitLen() > MaxBigIntegerSizeBits {
|
|
|
|
panic("integer is too big")
|
|
|
|
}
|
2018-04-02 15:04:42 +00:00
|
|
|
return &BigIntegerItem{
|
2020-04-16 12:28:34 +00:00
|
|
|
value: value,
|
2018-04-02 15:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 09:07:08 +00:00
|
|
|
// Bytes converts i to a slice of bytes.
|
|
|
|
func (i *BigIntegerItem) Bytes() []byte {
|
2020-02-03 15:05:13 +00:00
|
|
|
return emit.IntToBytes(i.value)
|
2019-11-05 09:07:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 08:23:58 +00:00
|
|
|
// Bool implements StackItem interface.
|
|
|
|
func (i *BigIntegerItem) Bool() bool {
|
|
|
|
return i.value.Sign() != 0
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:04:28 +00:00
|
|
|
// TryBytes implements StackItem interface.
|
|
|
|
func (i *BigIntegerItem) TryBytes() ([]byte, error) {
|
|
|
|
return i.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:21:56 +00:00
|
|
|
// TryInteger implements StackItem interface.
|
|
|
|
func (i *BigIntegerItem) TryInteger() (*big.Int, error) {
|
|
|
|
return i.value, nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:44:10 +00:00
|
|
|
// Equals implements StackItem interface.
|
|
|
|
func (i *BigIntegerItem) Equals(s StackItem) bool {
|
|
|
|
if i == s {
|
|
|
|
return true
|
|
|
|
} else if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
val, ok := s.(*BigIntegerItem)
|
|
|
|
if ok {
|
|
|
|
return i.value.Cmp(val.value) == 0
|
|
|
|
}
|
|
|
|
bs, err := s.TryBytes()
|
|
|
|
return err == nil && bytes.Equal(i.Bytes(), bs)
|
|
|
|
}
|
|
|
|
|
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)}
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:56:28 +00:00
|
|
|
// ToContractParameter implements StackItem interface.
|
2020-03-03 09:52:56 +00:00
|
|
|
func (i *BigIntegerItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
2020-02-21 14:56:28 +00:00
|
|
|
return smartcontract.Parameter{
|
|
|
|
Type: smartcontract.IntegerType,
|
|
|
|
Value: i.value.Int64(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// Type implements StackItem interface.
|
|
|
|
func (i *BigIntegerItem) Type() StackItemType { return IntegerT }
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
// Convert implements StackItem interface.
|
|
|
|
func (i *BigIntegerItem) Convert(typ StackItemType) (StackItem, error) {
|
|
|
|
return convertPrimitive(i, typ)
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-02-28 15:21:20 +00:00
|
|
|
return "Boolean"
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (i *BoolItem) Dup() StackItem {
|
|
|
|
return &BoolItem{i.value}
|
|
|
|
}
|
|
|
|
|
2020-04-28 08:23:58 +00:00
|
|
|
// Bool implements StackItem interface.
|
|
|
|
func (i *BoolItem) Bool() bool { return i.value }
|
|
|
|
|
2020-03-11 13:04:28 +00:00
|
|
|
// Bytes converts BoolItem to bytes.
|
|
|
|
func (i *BoolItem) Bytes() []byte {
|
|
|
|
if i.value {
|
|
|
|
return []byte{1}
|
|
|
|
}
|
2020-04-15 13:19:42 +00:00
|
|
|
return []byte{0}
|
2020-03-11 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TryBytes implements StackItem interface.
|
|
|
|
func (i *BoolItem) TryBytes() ([]byte, error) {
|
|
|
|
return i.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:21:56 +00:00
|
|
|
// TryInteger implements StackItem interface.
|
|
|
|
func (i *BoolItem) TryInteger() (*big.Int, error) {
|
|
|
|
if i.value {
|
|
|
|
return big.NewInt(1), nil
|
|
|
|
}
|
|
|
|
return big.NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:44:10 +00:00
|
|
|
// Equals implements StackItem interface.
|
|
|
|
func (i *BoolItem) Equals(s StackItem) bool {
|
|
|
|
if i == s {
|
|
|
|
return true
|
|
|
|
} else if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
val, ok := s.(*BoolItem)
|
|
|
|
if ok {
|
|
|
|
return i.value == val.value
|
|
|
|
}
|
|
|
|
bs, err := s.TryBytes()
|
|
|
|
return err == nil && bytes.Equal(i.Bytes(), bs)
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:56:28 +00:00
|
|
|
// ToContractParameter implements StackItem interface.
|
2020-03-03 09:52:56 +00:00
|
|
|
func (i *BoolItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
2020-02-21 14:56:28 +00:00
|
|
|
return smartcontract.Parameter{
|
|
|
|
Type: smartcontract.BoolType,
|
|
|
|
Value: i.value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// Type implements StackItem interface.
|
|
|
|
func (i *BoolItem) Type() StackItemType { return BooleanT }
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
// Convert implements StackItem interface.
|
|
|
|
func (i *BoolItem) Convert(typ StackItemType) (StackItem, error) {
|
|
|
|
return convertPrimitive(i, typ)
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2020-04-28 08:23:58 +00:00
|
|
|
// Bool implements StackItem interface.
|
|
|
|
func (i *ByteArrayItem) Bool() bool {
|
|
|
|
if len(i.value) > MaxBigIntegerSizeBits/8 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, b := range i.value {
|
|
|
|
if b != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:04:28 +00:00
|
|
|
// TryBytes implements StackItem interface.
|
|
|
|
func (i *ByteArrayItem) TryBytes() ([]byte, error) {
|
|
|
|
return i.value, nil
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:21:56 +00:00
|
|
|
// TryInteger implements StackItem interface.
|
|
|
|
func (i *ByteArrayItem) TryInteger() (*big.Int, error) {
|
2020-04-29 14:13:46 +00:00
|
|
|
bi := emit.BytesToInt(i.value)
|
|
|
|
if bi.BitLen() > MaxBigIntegerSizeBits {
|
|
|
|
return nil, errors.New("integer is too big")
|
|
|
|
}
|
|
|
|
return bi, nil
|
2020-03-19 15:21:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:44:10 +00:00
|
|
|
// Equals implements StackItem interface.
|
|
|
|
func (i *ByteArrayItem) Equals(s StackItem) bool {
|
|
|
|
if i == s {
|
|
|
|
return true
|
|
|
|
} else if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
bs, err := s.TryBytes()
|
|
|
|
return err == nil && bytes.Equal(i.value, bs)
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:56:28 +00:00
|
|
|
// ToContractParameter implements StackItem interface.
|
2020-03-03 09:52:56 +00:00
|
|
|
func (i *ByteArrayItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
2020-02-21 14:56:28 +00:00
|
|
|
return smartcontract.Parameter{
|
|
|
|
Type: smartcontract.ByteArrayType,
|
|
|
|
Value: i.value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// Type implements StackItem interface.
|
|
|
|
func (i *ByteArrayItem) Type() StackItemType { return ByteArrayT }
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
// Convert implements StackItem interface.
|
|
|
|
func (i *ByteArrayItem) Convert(typ StackItemType) (StackItem, error) {
|
|
|
|
return convertPrimitive(i, typ)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-04-28 08:23:58 +00:00
|
|
|
// Bool implements StackItem interface.
|
|
|
|
func (i *ArrayItem) Bool() bool { return true }
|
|
|
|
|
2020-03-11 13:04:28 +00:00
|
|
|
// TryBytes implements StackItem interface.
|
|
|
|
func (i *ArrayItem) TryBytes() ([]byte, error) {
|
|
|
|
return nil, errors.New("can't convert Array to ByteArray")
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:21:56 +00:00
|
|
|
// TryInteger implements StackItem interface.
|
|
|
|
func (i *ArrayItem) TryInteger() (*big.Int, error) {
|
|
|
|
return nil, errors.New("can't convert Array to Integer")
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:44:10 +00:00
|
|
|
// Equals implements StackItem interface.
|
|
|
|
func (i *ArrayItem) Equals(s StackItem) bool {
|
|
|
|
return i == s
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (i *ArrayItem) Dup() StackItem {
|
|
|
|
// reference type
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:56:28 +00:00
|
|
|
// ToContractParameter implements StackItem interface.
|
2020-03-03 09:52:56 +00:00
|
|
|
func (i *ArrayItem) ToContractParameter(seen map[StackItem]bool) smartcontract.Parameter {
|
2020-02-21 14:56:28 +00:00
|
|
|
var value []smartcontract.Parameter
|
2020-03-03 09:52:56 +00:00
|
|
|
|
|
|
|
if !seen[i] {
|
|
|
|
seen[i] = true
|
|
|
|
for _, stackItem := range i.value {
|
|
|
|
parameter := stackItem.ToContractParameter(seen)
|
|
|
|
value = append(value, parameter)
|
|
|
|
}
|
2020-02-21 14:56:28 +00:00
|
|
|
}
|
|
|
|
return smartcontract.Parameter{
|
|
|
|
Type: smartcontract.ArrayType,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// Type implements StackItem interface.
|
|
|
|
func (i *ArrayItem) Type() StackItemType { return ArrayT }
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
// Convert implements StackItem interface.
|
|
|
|
func (i *ArrayItem) Convert(typ StackItemType) (StackItem, error) {
|
|
|
|
switch typ {
|
|
|
|
case ArrayT:
|
|
|
|
return i, nil
|
|
|
|
case StructT:
|
|
|
|
return NewStructItem(i.value), nil
|
|
|
|
case BooleanT:
|
|
|
|
return NewBoolItem(i.Bool()), nil
|
|
|
|
default:
|
|
|
|
return nil, errInvalidConversion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 10:30:38 +00:00
|
|
|
// MapElement is a key-value pair of StackItems.
|
|
|
|
type MapElement struct {
|
|
|
|
Key StackItem
|
|
|
|
Value StackItem
|
|
|
|
}
|
|
|
|
|
|
|
|
// MapItem represents Map object. It's ordered, so we use slice representation
|
|
|
|
// which should be fine for maps with less than 32 or so elements. Given that
|
|
|
|
// our VM has quite low limit of overall stack items, it should be good enough,
|
|
|
|
// but it can be extended with a real map for fast random access in the future
|
|
|
|
// if need be.
|
2019-09-24 12:06:23 +00:00
|
|
|
type MapItem struct {
|
2020-03-31 10:30:38 +00:00
|
|
|
value []MapElement
|
2019-09-24 12:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMapItem returns new MapItem object.
|
|
|
|
func NewMapItem() *MapItem {
|
|
|
|
return &MapItem{
|
2020-03-31 10:30:38 +00:00
|
|
|
value: make([]MapElement, 0),
|
2019-09-24 12:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements StackItem interface.
|
|
|
|
func (i *MapItem) Value() interface{} {
|
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
2020-04-28 08:23:58 +00:00
|
|
|
// Bool implements StackItem interface.
|
|
|
|
func (i *MapItem) Bool() bool { return true }
|
|
|
|
|
2020-03-11 13:04:28 +00:00
|
|
|
// TryBytes implements StackItem interface.
|
|
|
|
func (i *MapItem) TryBytes() ([]byte, error) {
|
|
|
|
return nil, errors.New("can't convert Map to ByteArray")
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:21:56 +00:00
|
|
|
// TryInteger implements StackItem interface.
|
|
|
|
func (i *MapItem) TryInteger() (*big.Int, error) {
|
|
|
|
return nil, errors.New("can't convert Map to Integer")
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:44:10 +00:00
|
|
|
// Equals implements StackItem interface.
|
|
|
|
func (i *MapItem) Equals(s StackItem) bool {
|
|
|
|
return i == s
|
|
|
|
}
|
|
|
|
|
2019-09-24 12:06:23 +00:00
|
|
|
func (i *MapItem) String() string {
|
|
|
|
return "Map"
|
|
|
|
}
|
|
|
|
|
2020-03-31 10:30:38 +00:00
|
|
|
// Index returns an index of the key in map.
|
|
|
|
func (i *MapItem) Index(key StackItem) int {
|
|
|
|
for k := range i.value {
|
|
|
|
if i.value[k].Key.Equals(key) {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2019-09-24 12:06:23 +00:00
|
|
|
// Has checks if map has specified key.
|
2020-03-31 10:30:38 +00:00
|
|
|
func (i *MapItem) Has(key StackItem) bool {
|
|
|
|
return i.Index(key) >= 0
|
2019-09-24 12:06:23 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 13:38:42 +00:00
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (i *MapItem) Dup() StackItem {
|
|
|
|
// reference type
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:56:28 +00:00
|
|
|
// ToContractParameter implements StackItem interface.
|
2020-03-03 09:52:56 +00:00
|
|
|
func (i *MapItem) ToContractParameter(seen map[StackItem]bool) smartcontract.Parameter {
|
2020-03-30 15:24:06 +00:00
|
|
|
value := make([]smartcontract.ParameterPair, 0)
|
2020-03-03 09:52:56 +00:00
|
|
|
if !seen[i] {
|
|
|
|
seen[i] = true
|
2020-03-31 10:30:38 +00:00
|
|
|
for k := range i.value {
|
2020-03-30 15:24:06 +00:00
|
|
|
value = append(value, smartcontract.ParameterPair{
|
2020-03-31 10:30:38 +00:00
|
|
|
Key: i.value[k].Key.ToContractParameter(seen),
|
|
|
|
Value: i.value[k].Value.ToContractParameter(seen),
|
2020-03-30 15:24:06 +00:00
|
|
|
})
|
2020-02-21 14:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return smartcontract.Parameter{
|
|
|
|
Type: smartcontract.MapType,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// Type implements StackItem interface.
|
|
|
|
func (i *MapItem) Type() StackItemType { return MapT }
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
// Convert implements StackItem interface.
|
|
|
|
func (i *MapItem) Convert(typ StackItemType) (StackItem, error) {
|
|
|
|
switch typ {
|
|
|
|
case MapT:
|
|
|
|
return i, nil
|
|
|
|
case BooleanT:
|
|
|
|
return NewBoolItem(i.Bool()), nil
|
|
|
|
default:
|
|
|
|
return nil, errInvalidConversion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 12:06:23 +00:00
|
|
|
// Add adds key-value pair to the map.
|
|
|
|
func (i *MapItem) Add(key, value StackItem) {
|
2020-03-31 10:30:38 +00:00
|
|
|
if !isValidMapKey(key) {
|
2019-09-24 12:06:23 +00:00
|
|
|
panic("wrong key type")
|
|
|
|
}
|
2020-03-31 10:30:38 +00:00
|
|
|
index := i.Index(key)
|
|
|
|
if index >= 0 {
|
|
|
|
i.value[index].Value = value
|
|
|
|
} else {
|
|
|
|
i.value = append(i.value, MapElement{key, value})
|
|
|
|
}
|
2019-09-24 12:06:23 +00:00
|
|
|
}
|
2019-10-01 17:11:01 +00:00
|
|
|
|
2020-03-31 10:30:38 +00:00
|
|
|
// Drop removes given index from the map (no bounds check done here).
|
|
|
|
func (i *MapItem) Drop(index int) {
|
|
|
|
copy(i.value[index:], i.value[index+1:])
|
|
|
|
i.value = i.value[:len(i.value)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValidMapKey checks whether it's possible to use given StackItem as a Map
|
|
|
|
// key.
|
|
|
|
func isValidMapKey(key StackItem) bool {
|
|
|
|
switch key.(type) {
|
|
|
|
case *BoolItem, *BigIntegerItem, *ByteArrayItem:
|
|
|
|
return true
|
2020-02-21 14:56:28 +00:00
|
|
|
default:
|
2020-03-31 10:30:38 +00:00
|
|
|
return false
|
2020-02-21 14:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-28 08:23:58 +00:00
|
|
|
// Bool implements StackItem interface.
|
|
|
|
func (i *InteropItem) Bool() bool { return true }
|
|
|
|
|
2020-03-11 13:04:28 +00:00
|
|
|
// TryBytes implements StackItem interface.
|
|
|
|
func (i *InteropItem) TryBytes() ([]byte, error) {
|
|
|
|
return nil, errors.New("can't convert Interop to ByteArray")
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:21:56 +00:00
|
|
|
// TryInteger implements StackItem interface.
|
|
|
|
func (i *InteropItem) TryInteger() (*big.Int, error) {
|
|
|
|
return nil, errors.New("can't convert Interop to Integer")
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:44:10 +00:00
|
|
|
// Equals implements StackItem interface.
|
|
|
|
func (i *InteropItem) Equals(s StackItem) bool {
|
|
|
|
if i == s {
|
|
|
|
return true
|
|
|
|
} else if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
val, ok := s.(*InteropItem)
|
|
|
|
return ok && i.value == val.value
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:56:28 +00:00
|
|
|
// ToContractParameter implements StackItem interface.
|
2020-03-03 09:52:56 +00:00
|
|
|
func (i *InteropItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
2020-02-21 14:56:28 +00:00
|
|
|
return smartcontract.Parameter{
|
|
|
|
Type: smartcontract.InteropInterfaceType,
|
|
|
|
Value: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:46:46 +00:00
|
|
|
// Type implements StackItem interface.
|
|
|
|
func (i *InteropItem) Type() StackItemType { return InteropT }
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
// Convert implements StackItem interface.
|
|
|
|
func (i *InteropItem) Convert(typ StackItemType) (StackItem, error) {
|
|
|
|
switch typ {
|
|
|
|
case InteropT:
|
|
|
|
return i, nil
|
|
|
|
case BooleanT:
|
|
|
|
return NewBoolItem(i.Bool()), nil
|
|
|
|
default:
|
|
|
|
return nil, errInvalidConversion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2020-05-06 13:31:52 +00:00
|
|
|
|
|
|
|
// PointerItem represents VM-level instruction pointer.
|
|
|
|
type PointerItem struct {
|
|
|
|
pos int
|
|
|
|
script []byte
|
|
|
|
hash util.Uint160
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPointerItem returns new pointer on the specified position.
|
|
|
|
func NewPointerItem(pos int, script []byte) *PointerItem {
|
|
|
|
return &PointerItem{
|
|
|
|
pos: pos,
|
|
|
|
script: script,
|
|
|
|
hash: hash.Hash160(script),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements StackItem interface.
|
|
|
|
func (p *PointerItem) String() string {
|
|
|
|
return "Pointer"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements StackItem interface.
|
|
|
|
func (p *PointerItem) Value() interface{} {
|
|
|
|
return p.pos
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dup implements StackItem interface.
|
|
|
|
func (p *PointerItem) Dup() StackItem {
|
|
|
|
return &PointerItem{
|
|
|
|
pos: p.pos,
|
|
|
|
script: p.script,
|
|
|
|
hash: p.hash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bool implements StackItem interface.
|
|
|
|
func (p *PointerItem) Bool() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryBytes implements StackItem interface.
|
|
|
|
func (p *PointerItem) TryBytes() ([]byte, error) {
|
|
|
|
return nil, errors.New("can't convert Pointer to ByteArray")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryInteger implements StackItem interface.
|
|
|
|
func (p *PointerItem) TryInteger() (*big.Int, error) {
|
|
|
|
return nil, errors.New("can't convert Pointer to Integer")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals implements StackItem interface.
|
|
|
|
func (p *PointerItem) Equals(s StackItem) bool {
|
|
|
|
if p == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
ptr, ok := s.(*PointerItem)
|
|
|
|
return ok && p.pos == ptr.pos && p.hash == ptr.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToContractParameter implements StackItem interface.
|
|
|
|
func (p *PointerItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
|
|
|
return smartcontract.NewParameter(smartcontract.AnyType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type implements StackItem interface.
|
|
|
|
func (p *PointerItem) Type() StackItemType {
|
|
|
|
return PointerT
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert implements StackItem interface.
|
|
|
|
func (p *PointerItem) Convert(typ StackItemType) (StackItem, error) {
|
|
|
|
switch typ {
|
|
|
|
case PointerT:
|
|
|
|
return p, nil
|
|
|
|
case BooleanT:
|
|
|
|
return NewBoolItem(p.Bool()), nil
|
|
|
|
default:
|
|
|
|
return nil, errInvalidConversion
|
|
|
|
}
|
|
|
|
}
|