[#14] Use stackitems in morph/client converters
In neo-go v0.91.0 testinvoke returns stackitems.Item interface, so converters should work with this type. Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
46cd3d19fc
commit
0c06eafc60
2 changed files with 170 additions and 0 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/binary"
|
||||
|
||||
sc "github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
|
@ -130,3 +131,70 @@ func StringFromStackParameter(param sc.Parameter) (string, error) {
|
|||
return "", errors.Errorf("chain/client: %s is not a string type", param.Type)
|
||||
}
|
||||
}
|
||||
|
||||
// BoolFromStackItem receives boolean value from the value of a smart contract parameter.
|
||||
func BoolFromStackItem(param stackitem.Item) (bool, error) {
|
||||
switch param.Type() {
|
||||
case stackitem.BooleanT, stackitem.IntegerT, stackitem.ByteArrayT:
|
||||
return param.Bool(), nil
|
||||
default:
|
||||
return false, errors.Errorf("chain/client: %s is not a bool type", param.Type())
|
||||
}
|
||||
}
|
||||
|
||||
// IntFromStackItem receives numerical value from the value of a smart contract parameter.
|
||||
func IntFromStackItem(param stackitem.Item) (int64, error) {
|
||||
switch param.Type() {
|
||||
case stackitem.IntegerT, stackitem.ByteArrayT:
|
||||
i, err := param.TryInteger()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return i.Int64(), nil
|
||||
default:
|
||||
return 0, errors.Errorf("chain/client: %s is not an integer type", param.Type())
|
||||
}
|
||||
}
|
||||
|
||||
// BytesFromStackItem receives binary value from the value of a smart contract parameter.
|
||||
func BytesFromStackItem(param stackitem.Item) ([]byte, error) {
|
||||
if param.Type() != stackitem.ByteArrayT {
|
||||
if param.Type() == stackitem.AnyT && param.Value() == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil, errors.Errorf("chain/client: %s is not a byte array type", param.Type())
|
||||
}
|
||||
|
||||
return param.TryBytes()
|
||||
}
|
||||
|
||||
// ArrayFromStackItem returns the slice contract parameters from passed parameter.
|
||||
//
|
||||
// If passed parameter carries boolean false value, (nil, nil) returns.
|
||||
func ArrayFromStackItem(param stackitem.Item) ([]stackitem.Item, error) {
|
||||
// if param.Type()
|
||||
switch param.Type() {
|
||||
case stackitem.AnyT:
|
||||
return nil, nil
|
||||
case stackitem.ArrayT:
|
||||
items, ok := param.Value().([]stackitem.Item)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("chain/client: can't convert %T to parameter slice", param.Value())
|
||||
}
|
||||
|
||||
return items, nil
|
||||
default:
|
||||
return nil, errors.Errorf("chain/client: %s is not an array type", param.Type())
|
||||
}
|
||||
}
|
||||
|
||||
// StringFromStackItem receives string value from the value of a smart contract parameter.
|
||||
func StringFromStackItem(param stackitem.Item) (string, error) {
|
||||
if param.Type() != stackitem.ByteArrayT {
|
||||
return "", errors.Errorf("chain/client: %s is not an integer type", param.Type())
|
||||
}
|
||||
|
||||
return stackitem.ToString(param)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
sc "github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
|
@ -47,6 +49,15 @@ var (
|
|||
Type: sc.ArrayType,
|
||||
Value: []sc.Parameter{intParam, byteArrayParam},
|
||||
}
|
||||
|
||||
stringByteItem = stackitem.NewByteArray([]byte("Hello World"))
|
||||
intItem = stackitem.NewBigInteger(new(big.Int).SetInt64(1))
|
||||
byteWithIntItem = stackitem.NewByteArray([]byte{0x0a})
|
||||
emptyByteArrayItem = stackitem.NewByteArray([]byte{})
|
||||
trueBoolItem = stackitem.NewBool(true)
|
||||
falseBoolItem = stackitem.NewBool(false)
|
||||
arrayItem = stackitem.NewArray([]stackitem.Item{intItem, stringByteItem})
|
||||
anyTypeItem = stackitem.Null{}
|
||||
)
|
||||
|
||||
func TestBoolFromStackParameter(t *testing.T) {
|
||||
|
@ -143,3 +154,94 @@ func TestStringFromStackParameter(t *testing.T) {
|
|||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBoolFromStackItem(t *testing.T) {
|
||||
t.Run("true assert", func(t *testing.T) {
|
||||
val, err := BoolFromStackItem(trueBoolItem)
|
||||
require.NoError(t, err)
|
||||
require.True(t, val)
|
||||
|
||||
val, err = BoolFromStackItem(intItem)
|
||||
require.NoError(t, err)
|
||||
require.True(t, val)
|
||||
})
|
||||
|
||||
t.Run("false assert", func(t *testing.T) {
|
||||
val, err := BoolFromStackItem(falseBoolItem)
|
||||
require.NoError(t, err)
|
||||
require.False(t, val)
|
||||
|
||||
val, err = BoolFromStackItem(emptyByteArrayItem)
|
||||
require.NoError(t, err)
|
||||
require.False(t, val)
|
||||
})
|
||||
|
||||
t.Run("incorrect assert", func(t *testing.T) {
|
||||
_, err := BoolFromStackItem(arrayItem)
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArrayFromStackItem(t *testing.T) {
|
||||
t.Run("correct assert", func(t *testing.T) {
|
||||
val, err := ArrayFromStackItem(arrayItem)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, val, len(arrayItem.Value().([]stackitem.Item)))
|
||||
})
|
||||
t.Run("incorrect assert", func(t *testing.T) {
|
||||
_, err := ArrayFromStackItem(stringByteItem)
|
||||
require.Error(t, err)
|
||||
})
|
||||
t.Run("nil array case", func(t *testing.T) {
|
||||
val, err := ArrayFromStackItem(anyTypeItem)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, val)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBytesFromStackItem(t *testing.T) {
|
||||
t.Run("correct assert", func(t *testing.T) {
|
||||
val, err := BytesFromStackItem(stringByteItem)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, stringByteItem.Value().([]byte), val)
|
||||
})
|
||||
|
||||
t.Run("incorrect assert", func(t *testing.T) {
|
||||
_, err := BytesFromStackItem(arrayItem)
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntFromStackItem(t *testing.T) {
|
||||
t.Run("correct assert", func(t *testing.T) {
|
||||
val, err := IntFromStackItem(intItem)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, intItem.Value().(*big.Int).Int64(), val)
|
||||
|
||||
val, err = IntFromStackItem(byteWithIntItem)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(0x0a), val)
|
||||
|
||||
val, err = IntFromStackItem(emptyByteArrayItem)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(0), val)
|
||||
})
|
||||
|
||||
t.Run("incorrect assert", func(t *testing.T) {
|
||||
_, err := IntFromStackItem(arrayItem)
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStringFromStackItem(t *testing.T) {
|
||||
t.Run("correct assert", func(t *testing.T) {
|
||||
val, err := StringFromStackItem(stringByteItem)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, string(stringByteItem.Value().([]byte)), val)
|
||||
})
|
||||
|
||||
t.Run("incorrect assert", func(t *testing.T) {
|
||||
_, err := StringFromStackItem(intItem)
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue