manifest: move Parameter into a file of its own

This commit is contained in:
Roman Khimov 2021-02-08 18:00:40 +03:00
parent 0b2e9312af
commit f3c761e4c2
4 changed files with 94 additions and 75 deletions

View file

@ -7,12 +7,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// Parameter represents smartcontract's parameter's definition.
type Parameter struct {
Name string `json:"name"`
Type smartcontract.ParamType `json:"type"`
}
// Method represents method's metadata.
type Method struct {
Name string `json:"name"`
@ -22,14 +16,6 @@ type Method struct {
Safe bool `json:"safe"`
}
// NewParameter returns new parameter of specified name and type.
func NewParameter(name string, typ smartcontract.ParamType) Parameter {
return Parameter{
Name: name,
Type: typ,
}
}
// ToStackItem converts Method to stackitem.Item.
func (m *Method) ToStackItem() stackitem.Item {
params := make([]stackitem.Item, len(m.Parameters))
@ -91,36 +77,3 @@ func (m *Method) FromStackItem(item stackitem.Item) error {
m.Safe = safe
return nil
}
// ToStackItem converts Parameter to stackitem.Item.
func (p *Parameter) ToStackItem() stackitem.Item {
return stackitem.NewStruct([]stackitem.Item{
stackitem.Make(p.Name),
stackitem.Make(int(p.Type)),
})
}
// FromStackItem converts stackitem.Item to Parameter.
func (p *Parameter) FromStackItem(item stackitem.Item) error {
var err error
if item.Type() != stackitem.StructT {
return errors.New("invalid Parameter stackitem type")
}
param := item.Value().([]stackitem.Item)
if len(param) != 2 {
return errors.New("invalid Parameter stackitem length")
}
p.Name, err = stackitem.ToString(param[0])
if err != nil {
return err
}
typ, err := param[1].TryInteger()
if err != nil {
return err
}
p.Type, err = smartcontract.ConvertToParamType(int(typ.Int64()))
if err != nil {
return err
}
return nil
}

View file

@ -52,34 +52,6 @@ func TestMethod_FromStackItemErrors(t *testing.T) {
}
}
func TestParameter_ToStackItemFromStackItem(t *testing.T) {
p := &Parameter{
Name: "param",
Type: smartcontract.StringType,
}
expected := stackitem.NewStruct([]stackitem.Item{
stackitem.NewByteArray([]byte(p.Name)),
stackitem.NewBigInteger(big.NewInt(int64(p.Type))),
})
CheckToFromStackItem(t, p, expected)
}
func TestParameter_FromStackItemErrors(t *testing.T) {
errCases := map[string]stackitem.Item{
"not a struct": stackitem.NewArray([]stackitem.Item{}),
"invalid length": stackitem.NewStruct([]stackitem.Item{}),
"invalid name type": stackitem.NewStruct([]stackitem.Item{stackitem.NewInterop(nil), stackitem.Null{}}),
"invalid type type": stackitem.NewStruct([]stackitem.Item{stackitem.NewByteArray([]byte{}), stackitem.Null{}}),
"invalid type value": stackitem.NewStruct([]stackitem.Item{stackitem.NewByteArray([]byte{}), stackitem.NewBigInteger(big.NewInt(-100500))}),
}
for name, errCase := range errCases {
t.Run(name, func(t *testing.T) {
p := new(Parameter)
require.Error(t, p.FromStackItem(errCase))
})
}
}
func TestGroup_ToStackItemFromStackItem(t *testing.T) {
pk, _ := keys.NewPrivateKey()
g := &Group{

View file

@ -0,0 +1,56 @@
package manifest
import (
"errors"
"sort"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// Parameter represents smartcontract's parameter's definition.
type Parameter struct {
Name string `json:"name"`
Type smartcontract.ParamType `json:"type"`
}
// NewParameter returns new parameter of specified name and type.
func NewParameter(name string, typ smartcontract.ParamType) Parameter {
return Parameter{
Name: name,
Type: typ,
}
}
// ToStackItem converts Parameter to stackitem.Item.
func (p *Parameter) ToStackItem() stackitem.Item {
return stackitem.NewStruct([]stackitem.Item{
stackitem.Make(p.Name),
stackitem.Make(int(p.Type)),
})
}
// FromStackItem converts stackitem.Item to Parameter.
func (p *Parameter) FromStackItem(item stackitem.Item) error {
var err error
if item.Type() != stackitem.StructT {
return errors.New("invalid Parameter stackitem type")
}
param := item.Value().([]stackitem.Item)
if len(param) != 2 {
return errors.New("invalid Parameter stackitem length")
}
p.Name, err = stackitem.ToString(param[0])
if err != nil {
return err
}
typ, err := param[1].TryInteger()
if err != nil {
return err
}
p.Type, err = smartcontract.ConvertToParamType(int(typ.Int64()))
if err != nil {
return err
}
return nil
}

View file

@ -0,0 +1,38 @@
package manifest
import (
"math/big"
"testing"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
func TestParameter_ToStackItemFromStackItem(t *testing.T) {
p := &Parameter{
Name: "param",
Type: smartcontract.StringType,
}
expected := stackitem.NewStruct([]stackitem.Item{
stackitem.NewByteArray([]byte(p.Name)),
stackitem.NewBigInteger(big.NewInt(int64(p.Type))),
})
CheckToFromStackItem(t, p, expected)
}
func TestParameter_FromStackItemErrors(t *testing.T) {
errCases := map[string]stackitem.Item{
"not a struct": stackitem.NewArray([]stackitem.Item{}),
"invalid length": stackitem.NewStruct([]stackitem.Item{}),
"invalid name type": stackitem.NewStruct([]stackitem.Item{stackitem.NewInterop(nil), stackitem.Null{}}),
"invalid type type": stackitem.NewStruct([]stackitem.Item{stackitem.NewByteArray([]byte{}), stackitem.Null{}}),
"invalid type value": stackitem.NewStruct([]stackitem.Item{stackitem.NewByteArray([]byte{}), stackitem.NewBigInteger(big.NewInt(-100500))}),
}
for name, errCase := range errCases {
t.Run(name, func(t *testing.T) {
p := new(Parameter)
require.Error(t, p.FromStackItem(errCase))
})
}
}