2021-02-08 15:00:40 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:37:39 +00:00
|
|
|
// Parameters is just an array of Parameter.
|
|
|
|
type Parameters []Parameter
|
|
|
|
|
2021-02-08 15:00:40 +00:00
|
|
|
// NewParameter returns new parameter of specified name and type.
|
|
|
|
func NewParameter(name string, typ smartcontract.ParamType) Parameter {
|
|
|
|
return Parameter{
|
|
|
|
Name: name,
|
|
|
|
Type: typ,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:50:40 +00:00
|
|
|
// IsValid checks Parameter consistency and correctness.
|
|
|
|
func (p *Parameter) IsValid() error {
|
|
|
|
if p.Name == "" {
|
|
|
|
return errors.New("empty or absent name")
|
|
|
|
}
|
|
|
|
if p.Type == smartcontract.VoidType {
|
|
|
|
return errors.New("void parameter")
|
|
|
|
}
|
|
|
|
_, err := smartcontract.ConvertToParamType(int(p.Type))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:00:40 +00:00
|
|
|
// 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
|
|
|
|
}
|
2021-02-08 15:37:39 +00:00
|
|
|
|
|
|
|
// AreValid checks all parameters for validity and consistency.
|
|
|
|
func (p Parameters) AreValid() error {
|
2021-02-08 15:50:40 +00:00
|
|
|
for i := range p {
|
|
|
|
err := p[i].IsValid()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 15:37:39 +00:00
|
|
|
if len(p) < 2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
names := make([]string, len(p))
|
|
|
|
for i := range p {
|
|
|
|
names[i] = p[i].Name
|
|
|
|
}
|
2021-02-08 18:07:05 +00:00
|
|
|
if stringsHaveDups(names) {
|
|
|
|
return errors.New("duplicate parameter name")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringsHaveDups checks given set of strings for duplicates. It modifies the slice given!
|
|
|
|
func stringsHaveDups(strings []string) bool {
|
|
|
|
sort.Strings(strings)
|
|
|
|
for i := range strings {
|
2021-02-08 15:37:39 +00:00
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-08 18:07:05 +00:00
|
|
|
if strings[i] == strings[i-1] {
|
|
|
|
return true
|
2021-02-08 15:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-08 18:07:05 +00:00
|
|
|
return false
|
2021-02-08 15:37:39 +00:00
|
|
|
}
|