2020-06-03 12:55:06 +00:00
|
|
|
package stackitem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-28 13:09:42 +00:00
|
|
|
"math"
|
2020-06-03 12:55:06 +00:00
|
|
|
"math/big"
|
|
|
|
"reflect"
|
2020-07-29 08:18:51 +00:00
|
|
|
"unicode/utf8"
|
2020-06-03 12:55:06 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-06-04 18:11:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-07-18 13:32:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2020-06-03 12:55:06 +00:00
|
|
|
)
|
|
|
|
|
2020-09-28 13:09:42 +00:00
|
|
|
const (
|
2022-04-20 18:30:09 +00:00
|
|
|
// MaxBigIntegerSizeBits is the maximum size of a BigInt item in bits.
|
2020-09-28 13:09:42 +00:00
|
|
|
MaxBigIntegerSizeBits = 32 * 8
|
|
|
|
// MaxSize is the maximum item size allowed in the VM.
|
2023-11-02 08:44:05 +00:00
|
|
|
MaxSize = math.MaxUint16 * 2
|
2021-07-19 07:07:05 +00:00
|
|
|
// MaxComparableNumOfItems is the maximum number of items that can be compared for structs.
|
2021-07-19 12:39:54 +00:00
|
|
|
MaxComparableNumOfItems = MaxDeserialized
|
|
|
|
// MaxClonableNumOfItems is the maximum number of items that can be cloned in structs.
|
|
|
|
MaxClonableNumOfItems = MaxDeserialized
|
2022-04-20 18:30:09 +00:00
|
|
|
// MaxByteArrayComparableSize is the maximum allowed length of a ByteArray for Equals method.
|
2022-05-06 10:29:00 +00:00
|
|
|
// It is set to be the maximum uint16 value + 1.
|
|
|
|
MaxByteArrayComparableSize = math.MaxUint16 + 1
|
2022-04-20 18:30:09 +00:00
|
|
|
// MaxKeySize is the maximum size of a map key.
|
2020-09-29 07:56:57 +00:00
|
|
|
MaxKeySize = 64
|
2020-09-28 13:09:42 +00:00
|
|
|
)
|
2020-06-11 13:31:31 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// Item represents the "real" value that is pushed on the stack.
|
|
|
|
type Item interface {
|
|
|
|
fmt.Stringer
|
2023-04-03 10:34:24 +00:00
|
|
|
Value() any
|
2020-06-03 12:55:06 +00:00
|
|
|
// Dup duplicates current Item.
|
|
|
|
Dup() Item
|
2020-08-21 17:55:20 +00:00
|
|
|
// TryBool converts Item to a boolean value.
|
|
|
|
TryBool() (bool, error)
|
2020-08-22 20:36:38 +00:00
|
|
|
// TryBytes converts Item to a byte slice. If the underlying type is a
|
|
|
|
// byte slice, it's returned as is without copying.
|
2020-06-03 12:55:06 +00:00
|
|
|
TryBytes() ([]byte, error)
|
|
|
|
// TryInteger converts Item to an integer.
|
|
|
|
TryInteger() (*big.Int, error)
|
|
|
|
// Equals checks if 2 StackItems are equal.
|
|
|
|
Equals(s Item) bool
|
|
|
|
// Type returns stack item type.
|
|
|
|
Type() Type
|
|
|
|
// Convert converts Item to another type.
|
|
|
|
Convert(Type) (Item, error)
|
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
// Convertible is something that can be converted to/from Item.
|
|
|
|
type Convertible interface {
|
|
|
|
ToStackItem() (Item, error)
|
|
|
|
FromStackItem(Item) error
|
|
|
|
}
|
|
|
|
|
2023-06-14 13:42:11 +00:00
|
|
|
// Equatable describes a special value of Interop that can be compared with
|
|
|
|
// value of some other Interop that implements Equatable.
|
|
|
|
type Equatable interface {
|
|
|
|
// Equals checks if two objects are equal.
|
|
|
|
Equals(other Equatable) bool
|
|
|
|
}
|
|
|
|
|
2020-09-28 13:09:42 +00:00
|
|
|
var (
|
2022-04-20 18:30:09 +00:00
|
|
|
// ErrInvalidConversion is returned upon an attempt to make an incorrect
|
2021-07-06 21:18:00 +00:00
|
|
|
// conversion between item types.
|
|
|
|
ErrInvalidConversion = errors.New("invalid conversion")
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ErrTooBig is returned when an item exceeds some size constraints, like
|
|
|
|
// the maximum allowed integer value of the number of elements in an array. It
|
|
|
|
// can also be returned by serialization functions if the resulting
|
2021-07-06 21:18:00 +00:00
|
|
|
// value exceeds MaxSize.
|
|
|
|
ErrTooBig = errors.New("too big")
|
2022-05-30 07:41:00 +00:00
|
|
|
// ErrReadOnly is returned on attempt to modify immutable stack item.
|
|
|
|
ErrReadOnly = errors.New("item is read-only")
|
2021-07-06 21:18:00 +00:00
|
|
|
|
|
|
|
errTooBigComparable = fmt.Errorf("%w: uncomparable", ErrTooBig)
|
|
|
|
errTooBigInteger = fmt.Errorf("%w: integer", ErrTooBig)
|
|
|
|
errTooBigKey = fmt.Errorf("%w: map key", ErrTooBig)
|
|
|
|
errTooBigSize = fmt.Errorf("%w: size", ErrTooBig)
|
2021-07-17 19:21:33 +00:00
|
|
|
errTooBigElements = fmt.Errorf("%w: many elements", ErrTooBig)
|
2020-09-28 13:09:42 +00:00
|
|
|
)
|
2020-06-03 12:55:06 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// mkInvConversion creates a conversion error with additional metadata (from and
|
2021-07-06 21:18:00 +00:00
|
|
|
// to types).
|
|
|
|
func mkInvConversion(from Item, to Type) error {
|
|
|
|
return fmt.Errorf("%w: %s/%s", ErrInvalidConversion, from, to)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Make tries to make an appropriate stack item from the provided value.
|
2020-06-03 12:55:06 +00:00
|
|
|
// It will panic if it's not possible.
|
2023-04-03 10:34:24 +00:00
|
|
|
func Make(v any) Item {
|
2020-06-03 12:55:06 +00:00
|
|
|
switch val := v.(type) {
|
|
|
|
case int:
|
2021-08-04 13:41:13 +00:00
|
|
|
return (*BigInteger)(big.NewInt(int64(val)))
|
2020-06-03 12:55:06 +00:00
|
|
|
case int64:
|
2021-08-04 13:41:13 +00:00
|
|
|
return (*BigInteger)(big.NewInt(val))
|
2020-06-03 12:55:06 +00:00
|
|
|
case uint8:
|
2021-08-04 13:41:13 +00:00
|
|
|
return (*BigInteger)(big.NewInt(int64(val)))
|
2020-06-03 12:55:06 +00:00
|
|
|
case uint16:
|
2021-08-04 13:41:13 +00:00
|
|
|
return (*BigInteger)(big.NewInt(int64(val)))
|
2020-06-03 12:55:06 +00:00
|
|
|
case uint32:
|
2021-08-04 13:41:13 +00:00
|
|
|
return (*BigInteger)(big.NewInt(int64(val)))
|
2020-06-03 12:55:06 +00:00
|
|
|
case uint64:
|
2021-08-04 13:41:13 +00:00
|
|
|
return (*BigInteger)(new(big.Int).SetUint64(val))
|
2020-06-03 12:55:06 +00:00
|
|
|
case []byte:
|
2021-08-13 09:13:10 +00:00
|
|
|
return NewByteArray(val)
|
2020-06-03 12:55:06 +00:00
|
|
|
case string:
|
2021-08-13 09:13:10 +00:00
|
|
|
return NewByteArray([]byte(val))
|
2020-06-03 12:55:06 +00:00
|
|
|
case bool:
|
2021-08-03 16:37:06 +00:00
|
|
|
return Bool(val)
|
2020-06-03 12:55:06 +00:00
|
|
|
case []Item:
|
|
|
|
return &Array{
|
|
|
|
value: val,
|
|
|
|
}
|
|
|
|
case *big.Int:
|
|
|
|
return NewBigInteger(val)
|
|
|
|
case Item:
|
|
|
|
return val
|
|
|
|
case []int:
|
|
|
|
var a []Item
|
|
|
|
for _, i := range val {
|
|
|
|
a = append(a, Make(i))
|
|
|
|
}
|
|
|
|
return Make(a)
|
2023-04-03 10:34:24 +00:00
|
|
|
case []any:
|
2022-10-10 11:00:26 +00:00
|
|
|
res := make([]Item, len(val))
|
|
|
|
for i := range val {
|
|
|
|
res[i] = Make(val[i])
|
|
|
|
}
|
|
|
|
return Make(res)
|
|
|
|
case util.Uint160:
|
|
|
|
return Make(val.BytesBE())
|
|
|
|
case util.Uint256:
|
|
|
|
return Make(val.BytesBE())
|
2023-05-15 15:03:46 +00:00
|
|
|
case *util.Uint160:
|
|
|
|
if val == nil {
|
|
|
|
return Null{}
|
|
|
|
}
|
|
|
|
return Make(*val)
|
|
|
|
case *util.Uint256:
|
|
|
|
if val == nil {
|
|
|
|
return Null{}
|
|
|
|
}
|
|
|
|
return Make(*val)
|
2022-10-10 11:00:26 +00:00
|
|
|
case nil:
|
|
|
|
return Null{}
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
|
|
|
i64T := reflect.TypeOf(int64(0))
|
|
|
|
if reflect.TypeOf(val).ConvertibleTo(i64T) {
|
|
|
|
i64Val := reflect.ValueOf(val).Convert(i64T).Interface()
|
|
|
|
return Make(i64Val)
|
|
|
|
}
|
|
|
|
panic(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"invalid stack item type: %v (%v)",
|
|
|
|
val,
|
|
|
|
reflect.TypeOf(val),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ToString converts an Item to a string if it is a valid UTF-8.
|
2020-07-29 08:18:51 +00:00
|
|
|
func ToString(item Item) (string, error) {
|
|
|
|
bs, err := item.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !utf8.Valid(bs) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return "", fmt.Errorf("%w: not UTF-8", ErrInvalidValue)
|
2020-07-29 08:18:51 +00:00
|
|
|
}
|
|
|
|
return string(bs), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// convertPrimitive converts a primitive item to the specified type.
|
2020-06-03 12:55:06 +00:00
|
|
|
func convertPrimitive(item Item, typ Type) (Item, error) {
|
|
|
|
if item.Type() == typ {
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
switch typ {
|
|
|
|
case IntegerT:
|
|
|
|
bi, err := item.TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewBigInteger(bi), nil
|
|
|
|
case ByteArrayT, BufferT:
|
|
|
|
b, err := item.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if typ == BufferT {
|
2021-07-18 13:32:10 +00:00
|
|
|
return NewBuffer(slice.Copy(b)), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2020-08-22 20:36:38 +00:00
|
|
|
// ByteArray can't really be changed, so it's OK to reuse `b`.
|
2020-06-03 12:55:06 +00:00
|
|
|
return NewByteArray(b), nil
|
|
|
|
case BooleanT:
|
2020-08-21 17:55:20 +00:00
|
|
|
b, err := item.TryBool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewBool(b), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(item, typ)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Struct represents a struct on the stack.
|
|
|
|
type Struct struct {
|
|
|
|
value []Item
|
2021-08-11 12:16:29 +00:00
|
|
|
rc
|
2022-05-30 07:41:00 +00:00
|
|
|
ro
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewStruct returns a new Struct object.
|
2020-06-03 12:55:06 +00:00
|
|
|
func NewStruct(items []Item) *Struct {
|
|
|
|
return &Struct{
|
|
|
|
value: items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value implements the Item interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (i *Struct) Value() any {
|
2020-06-03 12:55:06 +00:00
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Remove removes the element at `pos` index from the Struct value.
|
|
|
|
// It will panic if a bad index given.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) Remove(pos int) {
|
2022-05-30 07:41:00 +00:00
|
|
|
if i.IsReadOnly() {
|
|
|
|
panic(ErrReadOnly)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
i.value = append(i.value[:pos], i.value[pos+1:]...)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Append adds an Item to the end of the Struct value.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) Append(item Item) {
|
2022-05-30 07:41:00 +00:00
|
|
|
if i.IsReadOnly() {
|
|
|
|
panic(ErrReadOnly)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
i.value = append(i.value, item)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Clear removes all elements from the Struct item value.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) Clear() {
|
2022-05-30 07:41:00 +00:00
|
|
|
if i.IsReadOnly() {
|
|
|
|
panic(ErrReadOnly)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
i.value = i.value[:0]
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Len returns the length of the Struct value.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) Len() int {
|
|
|
|
return len(i.value)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// String implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) String() string {
|
|
|
|
return "Struct"
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) Dup() Item {
|
|
|
|
// it's a reference type, so no copying here.
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBool implements the Item interface.
|
2020-08-21 17:55:20 +00:00
|
|
|
func (i *Struct) TryBool() (bool, error) { return true, nil }
|
2020-06-03 12:55:06 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBytes implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) TryBytes() ([]byte, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, ByteArrayT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryInteger implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) TryInteger() (*big.Int, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, IntegerT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Equals implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) Equals(s Item) bool {
|
2021-07-19 07:07:05 +00:00
|
|
|
if s == nil {
|
2020-06-03 12:55:06 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
val, ok := s.(*Struct)
|
2021-07-19 07:07:05 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var limit = MaxComparableNumOfItems - 1 // 1 for current element.
|
|
|
|
return i.equalStruct(val, &limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Struct) equalStruct(s *Struct, limit *int) bool {
|
|
|
|
if i == s {
|
|
|
|
return true
|
|
|
|
} else if len(i.value) != len(s.value) {
|
2020-06-03 12:55:06 +00:00
|
|
|
return false
|
|
|
|
}
|
2022-05-06 07:27:48 +00:00
|
|
|
var maxComparableSize = MaxByteArrayComparableSize
|
2020-06-03 12:55:06 +00:00
|
|
|
for j := range i.value {
|
2021-07-19 07:07:05 +00:00
|
|
|
*limit--
|
|
|
|
if *limit == 0 {
|
|
|
|
panic(errTooBigElements)
|
|
|
|
}
|
2022-05-06 07:27:48 +00:00
|
|
|
arr, ok := i.value[j].(*ByteArray)
|
|
|
|
if ok {
|
|
|
|
if !arr.equalsLimited(s.value[j], &maxComparableSize) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if maxComparableSize == 0 {
|
|
|
|
panic(errTooBigComparable)
|
|
|
|
}
|
|
|
|
maxComparableSize--
|
|
|
|
sa, oka := i.value[j].(*Struct)
|
|
|
|
sb, okb := s.value[j].(*Struct)
|
|
|
|
if oka && okb {
|
|
|
|
if !sa.equalStruct(sb, limit) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if !i.value[j].Equals(s.value[j]) {
|
2021-07-19 07:07:05 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) Type() Type { return StructT }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Convert implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Struct) Convert(typ Type) (Item, error) {
|
|
|
|
switch typ {
|
|
|
|
case StructT:
|
|
|
|
return i, nil
|
|
|
|
case ArrayT:
|
|
|
|
arr := make([]Item, len(i.value))
|
|
|
|
copy(arr, i.value)
|
|
|
|
return NewArray(arr), nil
|
|
|
|
case BooleanT:
|
2020-08-21 17:55:20 +00:00
|
|
|
return NewBool(true), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, typ)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Clone returns a Struct with all Struct fields copied by the value.
|
2020-06-03 12:55:06 +00:00
|
|
|
// Array fields are still copied by reference.
|
2021-07-19 12:39:54 +00:00
|
|
|
func (i *Struct) Clone() (*Struct, error) {
|
|
|
|
var limit = MaxClonableNumOfItems - 1 // For this struct itself.
|
2021-07-11 15:47:50 +00:00
|
|
|
return i.clone(&limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Struct) clone(limit *int) (*Struct, error) {
|
2021-08-11 12:16:29 +00:00
|
|
|
ret := &Struct{value: make([]Item, len(i.value))}
|
2020-06-03 12:55:06 +00:00
|
|
|
for j := range i.value {
|
2021-07-19 12:39:54 +00:00
|
|
|
*limit--
|
|
|
|
if *limit < 0 {
|
|
|
|
return nil, ErrTooBig
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
switch t := i.value[j].(type) {
|
|
|
|
case *Struct:
|
2021-07-11 15:47:50 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
ret.value[j], err = t.clone(limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
|
|
|
ret.value[j] = t
|
|
|
|
}
|
|
|
|
}
|
2021-07-11 15:47:50 +00:00
|
|
|
return ret, nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Null represents null on the stack.
|
|
|
|
type Null struct{}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// String implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i Null) String() string {
|
|
|
|
return "Null"
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value implements the Item interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (i Null) Value() any {
|
2020-06-03 12:55:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup implements the Item interface.
|
|
|
|
// There is no need to perform a real copy here
|
|
|
|
// since Null has no internal state.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i Null) Dup() Item {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBool implements the Item interface.
|
2020-08-21 17:55:20 +00:00
|
|
|
func (i Null) TryBool() (bool, error) { return false, nil }
|
2020-06-03 12:55:06 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBytes implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i Null) TryBytes() ([]byte, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, ByteArrayT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryInteger implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i Null) TryInteger() (*big.Int, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, IntegerT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Equals implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i Null) Equals(s Item) bool {
|
|
|
|
_, ok := s.(Null)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i Null) Type() Type { return AnyT }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Convert implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i Null) Convert(typ Type) (Item, error) {
|
|
|
|
if typ == AnyT || !typ.IsValid() {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, typ)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BigInteger represents a big integer on the stack.
|
2021-08-04 13:41:13 +00:00
|
|
|
type BigInteger big.Int
|
2020-06-03 12:55:06 +00:00
|
|
|
|
|
|
|
// NewBigInteger returns an new BigInteger object.
|
|
|
|
func NewBigInteger(value *big.Int) *BigInteger {
|
2022-02-09 12:30:26 +00:00
|
|
|
if err := CheckIntegerSize(value); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return (*BigInteger)(value)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CheckIntegerSize checks that the value size doesn't exceed the VM limit for Interer.
|
2022-02-09 12:30:26 +00:00
|
|
|
func CheckIntegerSize(value *big.Int) error {
|
2022-04-20 18:30:09 +00:00
|
|
|
// There are 2 cases when `BitLen` differs from the actual size:
|
2022-02-09 12:30:26 +00:00
|
|
|
// 1. Positive integer with the highest bit on byte boundary = 1.
|
|
|
|
// 2. Negative integer with the highest bit on byte boundary = 1
|
2020-10-06 11:47:55 +00:00
|
|
|
// minus some value. (-0x80 -> 0x80, -0x7F -> 0x81, -0x81 -> 0x7FFF).
|
|
|
|
sz := value.BitLen()
|
2022-02-09 12:30:26 +00:00
|
|
|
// This check is not required, just an optimization for the common case.
|
|
|
|
if sz < MaxBigIntegerSizeBits {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-06 11:47:55 +00:00
|
|
|
if sz > MaxBigIntegerSizeBits {
|
2022-02-09 12:30:26 +00:00
|
|
|
return errTooBigInteger
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2022-02-09 12:30:26 +00:00
|
|
|
if value.Sign() == 1 || value.TrailingZeroBits() != MaxBigIntegerSizeBits-1 {
|
|
|
|
return errTooBigInteger
|
|
|
|
}
|
|
|
|
return nil
|
2021-08-04 13:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Big casts i to the big.Int type.
|
|
|
|
func (i *BigInteger) Big() *big.Int {
|
|
|
|
return (*big.Int)(i)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes converts i to a slice of bytes.
|
|
|
|
func (i *BigInteger) Bytes() []byte {
|
2021-08-04 13:41:13 +00:00
|
|
|
return bigint.ToBytes(i.Big())
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBool implements the Item interface.
|
2020-08-21 17:55:20 +00:00
|
|
|
func (i *BigInteger) TryBool() (bool, error) {
|
2021-08-04 13:41:13 +00:00
|
|
|
return i.Big().Sign() != 0, nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBytes implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *BigInteger) TryBytes() ([]byte, error) {
|
|
|
|
return i.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryInteger implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *BigInteger) TryInteger() (*big.Int, error) {
|
2021-08-04 13:41:13 +00:00
|
|
|
return i.Big(), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Equals implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *BigInteger) Equals(s Item) bool {
|
|
|
|
if i == s {
|
|
|
|
return true
|
|
|
|
} else if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
val, ok := s.(*BigInteger)
|
2021-08-04 13:41:13 +00:00
|
|
|
return ok && i.Big().Cmp(val.Big()) == 0
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value implements the Item interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (i *BigInteger) Value() any {
|
2021-08-04 13:41:13 +00:00
|
|
|
return i.Big()
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *BigInteger) String() string {
|
|
|
|
return "BigInteger"
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *BigInteger) Dup() Item {
|
|
|
|
n := new(big.Int)
|
2021-08-04 13:41:13 +00:00
|
|
|
return (*BigInteger)(n.Set(i.Big()))
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *BigInteger) Type() Type { return IntegerT }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Convert implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *BigInteger) Convert(typ Type) (Item, error) {
|
|
|
|
return convertPrimitive(i, typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *BigInteger) MarshalJSON() ([]byte, error) {
|
2021-08-04 13:41:13 +00:00
|
|
|
return json.Marshal(i.Big())
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bool represents a boolean Item.
|
2021-08-03 16:37:06 +00:00
|
|
|
type Bool bool
|
2020-06-03 12:55:06 +00:00
|
|
|
|
|
|
|
// NewBool returns an new Bool object.
|
2021-08-03 16:37:06 +00:00
|
|
|
func NewBool(val bool) Bool {
|
|
|
|
return Bool(val)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value implements the Item interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (i Bool) Value() any {
|
2021-08-03 16:37:06 +00:00
|
|
|
return bool(i)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2021-08-03 16:37:06 +00:00
|
|
|
func (i Bool) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(bool(i))
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 16:37:06 +00:00
|
|
|
func (i Bool) String() string {
|
2020-06-03 12:55:06 +00:00
|
|
|
return "Boolean"
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup implements the Item interface.
|
2021-08-03 16:37:06 +00:00
|
|
|
func (i Bool) Dup() Item {
|
|
|
|
return i
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBool implements the Item interface.
|
2021-08-03 16:37:06 +00:00
|
|
|
func (i Bool) TryBool() (bool, error) { return bool(i), nil }
|
2020-06-03 12:55:06 +00:00
|
|
|
|
|
|
|
// Bytes converts Bool to bytes.
|
2021-08-03 16:37:06 +00:00
|
|
|
func (i Bool) Bytes() []byte {
|
|
|
|
if i {
|
2020-06-03 12:55:06 +00:00
|
|
|
return []byte{1}
|
|
|
|
}
|
|
|
|
return []byte{0}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBytes implements the Item interface.
|
2021-08-03 16:37:06 +00:00
|
|
|
func (i Bool) TryBytes() ([]byte, error) {
|
2020-06-03 12:55:06 +00:00
|
|
|
return i.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryInteger implements the Item interface.
|
2021-08-03 16:37:06 +00:00
|
|
|
func (i Bool) TryInteger() (*big.Int, error) {
|
|
|
|
if i {
|
2020-06-03 12:55:06 +00:00
|
|
|
return big.NewInt(1), nil
|
|
|
|
}
|
|
|
|
return big.NewInt(0), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Equals implements the Item interface.
|
2021-08-03 16:37:06 +00:00
|
|
|
func (i Bool) Equals(s Item) bool {
|
2020-06-03 12:55:06 +00:00
|
|
|
if i == s {
|
|
|
|
return true
|
|
|
|
} else if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-08-03 16:37:06 +00:00
|
|
|
val, ok := s.(Bool)
|
|
|
|
return ok && i == val
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the Item interface.
|
2021-08-03 16:37:06 +00:00
|
|
|
func (i Bool) Type() Type { return BooleanT }
|
2020-06-03 12:55:06 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Convert implements the Item interface.
|
2021-08-03 16:37:06 +00:00
|
|
|
func (i Bool) Convert(typ Type) (Item, error) {
|
2020-06-03 12:55:06 +00:00
|
|
|
return convertPrimitive(i, typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByteArray represents a byte array on the stack.
|
2021-08-13 09:13:10 +00:00
|
|
|
type ByteArray []byte
|
2020-06-03 12:55:06 +00:00
|
|
|
|
|
|
|
// NewByteArray returns an new ByteArray object.
|
|
|
|
func NewByteArray(b []byte) *ByteArray {
|
2021-08-13 09:13:10 +00:00
|
|
|
return (*ByteArray)(&b)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value implements the Item interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (i *ByteArray) Value() any {
|
2021-08-13 09:13:10 +00:00
|
|
|
return []byte(*i)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *ByteArray) MarshalJSON() ([]byte, error) {
|
2021-08-13 09:13:10 +00:00
|
|
|
return json.Marshal(hex.EncodeToString(*i))
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ByteArray) String() string {
|
2020-07-29 09:13:07 +00:00
|
|
|
return "ByteString"
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBool implements the Item interface.
|
2020-08-21 17:55:20 +00:00
|
|
|
func (i *ByteArray) TryBool() (bool, error) {
|
2021-08-13 09:13:10 +00:00
|
|
|
if len(*i) > MaxBigIntegerSizeBits/8 {
|
2021-07-06 21:18:00 +00:00
|
|
|
return false, errTooBigInteger
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2021-08-13 09:13:10 +00:00
|
|
|
for _, b := range *i {
|
2020-06-03 12:55:06 +00:00
|
|
|
if b != 0 {
|
2020-08-21 17:55:20 +00:00
|
|
|
return true, nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-21 17:55:20 +00:00
|
|
|
return false, nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBytes implements the Item interface.
|
2021-08-13 09:13:10 +00:00
|
|
|
func (i ByteArray) TryBytes() ([]byte, error) {
|
|
|
|
return i, nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryInteger implements the Item interface.
|
2021-08-13 09:13:10 +00:00
|
|
|
func (i ByteArray) TryInteger() (*big.Int, error) {
|
|
|
|
if len(i) > MaxBigIntegerSizeBits/8 {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, errTooBigInteger
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2021-08-13 09:13:10 +00:00
|
|
|
return bigint.FromBytes(i), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Equals implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *ByteArray) Equals(s Item) bool {
|
2022-05-06 07:27:48 +00:00
|
|
|
var limit = MaxByteArrayComparableSize
|
|
|
|
return i.equalsLimited(s, &limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// equalsLimited compares ByteArray with provided stackitem using the limit.
|
|
|
|
func (i *ByteArray) equalsLimited(s Item, limit *int) bool {
|
|
|
|
if i == nil {
|
|
|
|
return s == nil
|
|
|
|
}
|
|
|
|
lCurr := len(*i)
|
|
|
|
if lCurr > *limit || *limit == 0 {
|
2021-07-06 21:18:00 +00:00
|
|
|
panic(errTooBigComparable)
|
2020-09-28 13:09:42 +00:00
|
|
|
}
|
2022-05-06 07:27:48 +00:00
|
|
|
|
|
|
|
var comparedSize = 1
|
|
|
|
defer func() { *limit -= comparedSize }()
|
|
|
|
|
|
|
|
if s == nil {
|
2020-06-03 12:55:06 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-07-23 09:53:12 +00:00
|
|
|
val, ok := s.(*ByteArray)
|
2020-09-28 13:09:42 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2022-05-06 07:27:48 +00:00
|
|
|
comparedSize = lCurr
|
|
|
|
lOther := len(*val)
|
|
|
|
if lOther > comparedSize {
|
|
|
|
comparedSize = lOther
|
|
|
|
}
|
|
|
|
if i == val {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if lOther > *limit {
|
2021-07-06 21:18:00 +00:00
|
|
|
panic(errTooBigComparable)
|
2020-09-28 13:09:42 +00:00
|
|
|
}
|
2021-08-13 09:13:10 +00:00
|
|
|
return bytes.Equal(*i, *val)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *ByteArray) Dup() Item {
|
2021-08-13 09:13:10 +00:00
|
|
|
ba := slice.Copy(*i)
|
|
|
|
return (*ByteArray)(&ba)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *ByteArray) Type() Type { return ByteArrayT }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Convert implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *ByteArray) Convert(typ Type) (Item, error) {
|
|
|
|
return convertPrimitive(i, typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Array represents a new Array object.
|
|
|
|
type Array struct {
|
|
|
|
value []Item
|
2021-08-11 12:16:29 +00:00
|
|
|
rc
|
2022-05-30 07:41:00 +00:00
|
|
|
ro
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewArray returns a new Array object.
|
|
|
|
func NewArray(items []Item) *Array {
|
|
|
|
return &Array{
|
|
|
|
value: items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value implements the Item interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (i *Array) Value() any {
|
2020-06-03 12:55:06 +00:00
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Remove removes the element at `pos` index from Array value.
|
2020-06-03 12:55:06 +00:00
|
|
|
// It will panics on bad index.
|
|
|
|
func (i *Array) Remove(pos int) {
|
2022-05-30 07:41:00 +00:00
|
|
|
if i.IsReadOnly() {
|
|
|
|
panic(ErrReadOnly)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
i.value = append(i.value[:pos], i.value[pos+1:]...)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Append adds an Item to the end of the Array value.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Array) Append(item Item) {
|
2022-05-30 07:41:00 +00:00
|
|
|
if i.IsReadOnly() {
|
|
|
|
panic(ErrReadOnly)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
i.value = append(i.value, item)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Clear removes all elements from the Array item value.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Array) Clear() {
|
2022-05-30 07:41:00 +00:00
|
|
|
if i.IsReadOnly() {
|
|
|
|
panic(ErrReadOnly)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
i.value = i.value[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns length of Array value.
|
|
|
|
func (i *Array) Len() int {
|
|
|
|
return len(i.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *Array) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(i.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Array) String() string {
|
|
|
|
return "Array"
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBool implements the Item interface.
|
2020-08-21 17:55:20 +00:00
|
|
|
func (i *Array) TryBool() (bool, error) { return true, nil }
|
2020-06-03 12:55:06 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBytes implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Array) TryBytes() ([]byte, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, ByteArrayT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryInteger implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Array) TryInteger() (*big.Int, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, IntegerT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Equals implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Array) Equals(s Item) bool {
|
|
|
|
return i == s
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Array) Dup() Item {
|
|
|
|
// reference type
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Array) Type() Type { return ArrayT }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Convert implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Array) Convert(typ Type) (Item, error) {
|
|
|
|
switch typ {
|
|
|
|
case ArrayT:
|
|
|
|
return i, nil
|
|
|
|
case StructT:
|
|
|
|
arr := make([]Item, len(i.value))
|
|
|
|
copy(arr, i.value)
|
|
|
|
return NewStruct(arr), nil
|
|
|
|
case BooleanT:
|
2020-08-21 17:55:20 +00:00
|
|
|
return NewBool(true), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, typ)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MapElement is a key-value pair of StackItems.
|
|
|
|
type MapElement struct {
|
|
|
|
Key Item
|
|
|
|
Value Item
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Map represents a Map object. It's ordered, so we use slice representation,
|
2020-06-03 12:55:06 +00:00
|
|
|
// 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
|
2022-04-20 18:30:09 +00:00
|
|
|
// if needed.
|
2020-06-03 12:55:06 +00:00
|
|
|
type Map struct {
|
|
|
|
value []MapElement
|
2021-08-11 12:16:29 +00:00
|
|
|
rc
|
2022-05-30 07:41:00 +00:00
|
|
|
ro
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewMap returns a new Map object.
|
2020-06-03 12:55:06 +00:00
|
|
|
func NewMap() *Map {
|
|
|
|
return &Map{
|
|
|
|
value: make([]MapElement, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewMapWithValue returns a new Map object filled with the specified value.
|
2020-06-03 12:55:06 +00:00
|
|
|
func NewMapWithValue(value []MapElement) *Map {
|
|
|
|
if value != nil {
|
|
|
|
return &Map{
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NewMap()
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value implements the Item interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (i *Map) Value() any {
|
2020-06-03 12:55:06 +00:00
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Clear removes all elements from the Map item value.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) Clear() {
|
2022-05-30 07:41:00 +00:00
|
|
|
if i.IsReadOnly() {
|
|
|
|
panic(ErrReadOnly)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
i.value = i.value[:0]
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Len returns the length of the Map value.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) Len() int {
|
|
|
|
return len(i.value)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBool implements the Item interface.
|
2020-08-21 17:55:20 +00:00
|
|
|
func (i *Map) TryBool() (bool, error) { return true, nil }
|
2020-06-03 12:55:06 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBytes implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) TryBytes() ([]byte, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, ByteArrayT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryInteger implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) TryInteger() (*big.Int, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, IntegerT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Equals implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) Equals(s Item) bool {
|
|
|
|
return i == s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Map) String() string {
|
|
|
|
return "Map"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index returns an index of the key in map.
|
|
|
|
func (i *Map) Index(key Item) int {
|
|
|
|
for k := range i.value {
|
|
|
|
if i.value[k].Key.Equals(key) {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Has checks if the map has the specified key.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) Has(key Item) bool {
|
|
|
|
return i.Index(key) >= 0
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) Dup() Item {
|
|
|
|
// reference type
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) Type() Type { return MapT }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Convert implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) Convert(typ Type) (Item, error) {
|
|
|
|
switch typ {
|
|
|
|
case MapT:
|
|
|
|
return i, nil
|
|
|
|
case BooleanT:
|
2020-08-21 17:55:20 +00:00
|
|
|
return NewBool(true), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, typ)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Add adds a key-value pair to the map.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) Add(key, value Item) {
|
2020-09-29 07:56:57 +00:00
|
|
|
if err := IsValidMapKey(key); err != nil {
|
|
|
|
panic(err)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2022-05-30 07:41:00 +00:00
|
|
|
if i.IsReadOnly() {
|
|
|
|
panic(ErrReadOnly)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
index := i.Index(key)
|
|
|
|
if index >= 0 {
|
|
|
|
i.value[index].Value = value
|
|
|
|
} else {
|
|
|
|
i.value = append(i.value, MapElement{key, value})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Drop removes the given index from the map (no bounds check done here).
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Map) Drop(index int) {
|
2022-05-30 07:41:00 +00:00
|
|
|
if i.IsReadOnly() {
|
|
|
|
panic(ErrReadOnly)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
copy(i.value[index:], i.value[index+1:])
|
|
|
|
i.value = i.value[:len(i.value)-1]
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// IsValidMapKey checks whether it's possible to use the given Item as a Map
|
2020-06-03 12:55:06 +00:00
|
|
|
// key.
|
2020-09-29 07:56:57 +00:00
|
|
|
func IsValidMapKey(key Item) error {
|
2020-06-03 12:55:06 +00:00
|
|
|
switch key.(type) {
|
2021-08-03 16:37:06 +00:00
|
|
|
case Bool, *BigInteger:
|
2020-09-29 07:56:57 +00:00
|
|
|
return nil
|
|
|
|
case *ByteArray:
|
|
|
|
size := len(key.Value().([]byte))
|
|
|
|
if size > MaxKeySize {
|
2021-07-06 21:18:00 +00:00
|
|
|
return errTooBigKey
|
2020-09-29 07:56:57 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
2021-07-06 21:18:00 +00:00
|
|
|
return fmt.Errorf("%w: %s map key", ErrInvalidType, key.Type())
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interop represents interop data on the stack.
|
|
|
|
type Interop struct {
|
2023-04-03 10:34:24 +00:00
|
|
|
value any
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewInterop returns a new Interop object.
|
2023-04-03 10:34:24 +00:00
|
|
|
func NewInterop(value any) *Interop {
|
2020-06-03 12:55:06 +00:00
|
|
|
return &Interop{
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value implements the Item interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (i *Interop) Value() any {
|
2020-06-03 12:55:06 +00:00
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements stringer interface.
|
|
|
|
func (i *Interop) String() string {
|
2022-05-25 10:20:21 +00:00
|
|
|
return "InteropInterface"
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Interop) Dup() Item {
|
|
|
|
// reference type
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBool implements the Item interface.
|
2020-08-21 17:55:20 +00:00
|
|
|
func (i *Interop) TryBool() (bool, error) { return true, nil }
|
2020-06-03 12:55:06 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBytes implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Interop) TryBytes() ([]byte, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, ByteArrayT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryInteger implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Interop) TryInteger() (*big.Int, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, IntegerT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Equals implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Interop) Equals(s Item) bool {
|
|
|
|
if i == s {
|
|
|
|
return true
|
|
|
|
} else if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
val, ok := s.(*Interop)
|
2023-06-14 13:42:11 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
a, okA := i.value.(Equatable)
|
|
|
|
b, okB := val.value.(Equatable)
|
|
|
|
return (okA && okB && a.Equals(b)) || (!okA && !okB && i.value == val.value)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Interop) Type() Type { return InteropT }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Convert implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Interop) Convert(typ Type) (Item, error) {
|
|
|
|
switch typ {
|
|
|
|
case InteropT:
|
|
|
|
return i, nil
|
|
|
|
case BooleanT:
|
2020-08-21 17:55:20 +00:00
|
|
|
return NewBool(true), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, typ)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *Interop) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(i.value)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Pointer represents a VM-level instruction pointer.
|
2020-06-03 12:55:06 +00:00
|
|
|
type Pointer struct {
|
|
|
|
pos int
|
|
|
|
script []byte
|
|
|
|
hash util.Uint160
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewPointer returns a new pointer on the specified position.
|
2020-06-03 12:55:06 +00:00
|
|
|
func NewPointer(pos int, script []byte) *Pointer {
|
|
|
|
return &Pointer{
|
|
|
|
pos: pos,
|
|
|
|
script: script,
|
|
|
|
hash: hash.Hash160(script),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewPointerWithHash returns a new pointer on the specified position of the
|
2020-08-22 19:19:44 +00:00
|
|
|
// specified script. It differs from NewPointer in that the script hash is being
|
2022-04-20 18:30:09 +00:00
|
|
|
// passed explicitly to save on hash calculation. This hash is then being used
|
2020-08-22 19:19:44 +00:00
|
|
|
// for pointer comparisons.
|
|
|
|
func NewPointerWithHash(pos int, script []byte, h util.Uint160) *Pointer {
|
|
|
|
return &Pointer{
|
|
|
|
pos: pos,
|
|
|
|
script: script,
|
|
|
|
hash: h,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// String implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (p *Pointer) String() string {
|
|
|
|
return "Pointer"
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value implements the Item interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (p *Pointer) Value() any {
|
2020-06-03 12:55:06 +00:00
|
|
|
return p.pos
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (p *Pointer) Dup() Item {
|
|
|
|
return &Pointer{
|
|
|
|
pos: p.pos,
|
|
|
|
script: p.script,
|
|
|
|
hash: p.hash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBool implements the Item interface.
|
2020-08-21 17:55:20 +00:00
|
|
|
func (p *Pointer) TryBool() (bool, error) {
|
|
|
|
return true, nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBytes implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (p *Pointer) TryBytes() ([]byte, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(p, ByteArrayT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryInteger implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (p *Pointer) TryInteger() (*big.Int, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(p, IntegerT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Equals implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (p *Pointer) Equals(s Item) bool {
|
|
|
|
if p == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
ptr, ok := s.(*Pointer)
|
|
|
|
return ok && p.pos == ptr.pos && p.hash == ptr.hash
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (p *Pointer) Type() Type {
|
|
|
|
return PointerT
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Convert implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (p *Pointer) Convert(typ Type) (Item, error) {
|
|
|
|
switch typ {
|
|
|
|
case PointerT:
|
|
|
|
return p, nil
|
|
|
|
case BooleanT:
|
2020-08-21 17:55:20 +00:00
|
|
|
return NewBool(true), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(p, typ)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ScriptHash returns the pointer item hash.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (p *Pointer) ScriptHash() util.Uint160 {
|
|
|
|
return p.hash
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Position returns the pointer item position.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (p *Pointer) Position() int {
|
|
|
|
return p.pos
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Buffer represents represents a Buffer stack item.
|
2021-08-13 09:15:57 +00:00
|
|
|
type Buffer []byte
|
2020-06-03 12:55:06 +00:00
|
|
|
|
|
|
|
// NewBuffer returns a new Buffer object.
|
|
|
|
func NewBuffer(b []byte) *Buffer {
|
2021-08-13 09:15:57 +00:00
|
|
|
return (*Buffer)(&b)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value implements the Item interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (i *Buffer) Value() any {
|
2021-08-13 09:15:57 +00:00
|
|
|
return []byte(*i)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// String implements the fmt.Stringer interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Buffer) String() string {
|
|
|
|
return "Buffer"
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBool implements the Item interface.
|
2020-08-21 17:55:20 +00:00
|
|
|
func (i *Buffer) TryBool() (bool, error) {
|
|
|
|
return true, nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryBytes implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Buffer) TryBytes() ([]byte, error) {
|
2021-08-13 09:15:57 +00:00
|
|
|
return *i, nil
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TryInteger implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Buffer) TryInteger() (*big.Int, error) {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, IntegerT)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Equals implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Buffer) Equals(s Item) bool {
|
|
|
|
return i == s
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Buffer) Dup() Item {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (i *Buffer) MarshalJSON() ([]byte, error) {
|
2021-08-13 09:15:57 +00:00
|
|
|
return json.Marshal(hex.EncodeToString(*i))
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Buffer) Type() Type { return BufferT }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Convert implements the Item interface.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Buffer) Convert(typ Type) (Item, error) {
|
|
|
|
switch typ {
|
|
|
|
case BooleanT:
|
2020-08-21 17:55:20 +00:00
|
|
|
return NewBool(true), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
case BufferT:
|
|
|
|
return i, nil
|
|
|
|
case ByteArrayT:
|
2021-08-13 09:15:57 +00:00
|
|
|
return NewByteArray(slice.Copy(*i)), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
case IntegerT:
|
2021-08-13 09:15:57 +00:00
|
|
|
if len(*i) > MaxBigIntegerSizeBits/8 {
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, errTooBigInteger
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2021-08-13 09:15:57 +00:00
|
|
|
return NewBigInteger(bigint.FromBytes(*i)), nil
|
2020-06-03 12:55:06 +00:00
|
|
|
default:
|
2021-07-06 21:18:00 +00:00
|
|
|
return nil, mkInvConversion(i, typ)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Len returns the length of the Buffer value.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (i *Buffer) Len() int {
|
2021-08-13 09:15:57 +00:00
|
|
|
return len(*i)
|
2020-06-03 12:55:06 +00:00
|
|
|
}
|
2020-08-04 07:10:05 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DeepCopy returns a new deep copy of the provided item.
|
2020-08-04 07:10:05 +00:00
|
|
|
// Values of Interop items are not deeply copied.
|
|
|
|
// It does preserve duplicates only for non-primitive types.
|
2022-05-30 07:41:00 +00:00
|
|
|
func DeepCopy(item Item, asImmutable bool) Item {
|
2021-11-30 16:51:52 +00:00
|
|
|
seen := make(map[Item]Item, typicalNumOfItems)
|
2022-05-30 07:41:00 +00:00
|
|
|
return deepCopy(item, seen, asImmutable)
|
2020-08-04 07:10:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 07:41:00 +00:00
|
|
|
func deepCopy(item Item, seen map[Item]Item, asImmutable bool) Item {
|
2020-08-04 07:10:05 +00:00
|
|
|
if it := seen[item]; it != nil {
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
switch it := item.(type) {
|
|
|
|
case Null:
|
|
|
|
return Null{}
|
|
|
|
case *Array:
|
|
|
|
arr := NewArray(make([]Item, len(it.value)))
|
|
|
|
seen[item] = arr
|
|
|
|
for i := range it.value {
|
2022-05-30 07:41:00 +00:00
|
|
|
arr.value[i] = deepCopy(it.value[i], seen, asImmutable)
|
2020-08-04 07:10:05 +00:00
|
|
|
}
|
2022-05-30 07:41:00 +00:00
|
|
|
arr.MarkAsReadOnly()
|
2020-08-04 07:10:05 +00:00
|
|
|
return arr
|
|
|
|
case *Struct:
|
|
|
|
arr := NewStruct(make([]Item, len(it.value)))
|
|
|
|
seen[item] = arr
|
|
|
|
for i := range it.value {
|
2022-05-30 07:41:00 +00:00
|
|
|
arr.value[i] = deepCopy(it.value[i], seen, asImmutable)
|
2020-08-04 07:10:05 +00:00
|
|
|
}
|
2022-05-30 07:41:00 +00:00
|
|
|
arr.MarkAsReadOnly()
|
2020-08-04 07:10:05 +00:00
|
|
|
return arr
|
|
|
|
case *Map:
|
|
|
|
m := NewMap()
|
|
|
|
seen[item] = m
|
|
|
|
for i := range it.value {
|
2022-05-30 07:41:00 +00:00
|
|
|
key := deepCopy(it.value[i].Key, seen,
|
|
|
|
false) // Key is always primitive and not a Buffer.
|
|
|
|
value := deepCopy(it.value[i].Value, seen, asImmutable)
|
2020-08-04 07:10:05 +00:00
|
|
|
m.Add(key, value)
|
|
|
|
}
|
2022-05-30 07:41:00 +00:00
|
|
|
m.MarkAsReadOnly()
|
2020-08-04 07:10:05 +00:00
|
|
|
return m
|
|
|
|
case *BigInteger:
|
2021-08-04 13:41:13 +00:00
|
|
|
bi := new(big.Int).Set(it.Big())
|
|
|
|
return (*BigInteger)(bi)
|
2020-08-04 07:10:05 +00:00
|
|
|
case *ByteArray:
|
2021-08-13 09:13:10 +00:00
|
|
|
return NewByteArray(slice.Copy(*it))
|
2020-08-04 07:10:05 +00:00
|
|
|
case *Buffer:
|
2022-05-30 07:41:00 +00:00
|
|
|
if asImmutable {
|
2022-05-30 08:01:12 +00:00
|
|
|
return NewByteArray(slice.Copy(*it))
|
2022-05-30 07:41:00 +00:00
|
|
|
}
|
2021-08-13 09:15:57 +00:00
|
|
|
return NewBuffer(slice.Copy(*it))
|
2021-08-03 16:37:06 +00:00
|
|
|
case Bool:
|
|
|
|
return it
|
2020-08-04 07:10:05 +00:00
|
|
|
case *Pointer:
|
2020-08-22 19:19:44 +00:00
|
|
|
return NewPointerWithHash(it.pos, it.script, it.hash)
|
2020-08-04 07:10:05 +00:00
|
|
|
case *Interop:
|
|
|
|
return NewInterop(it.value)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|