mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-28 23:27:36 +00:00
aab18c3083
We have a lot of native contract types that are converted to stack items before serialization, then deserialized as stack items and converted back to regular structures. stackitem.Convertible allows to remove a lot of repetitive io.Serializable code. This also introduces to/from converter in testserdes which unfortunately required to change util tests to avoid circular references.
45 lines
973 B
Go
45 lines
973 B
Go
package native
|
|
|
|
import (
|
|
"math/big"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
)
|
|
|
|
type candidate struct {
|
|
Registered bool
|
|
Votes big.Int
|
|
}
|
|
|
|
// FromBytes unmarshals candidate from byte array.
|
|
func (c *candidate) FromBytes(data []byte) *candidate {
|
|
err := stackitem.DeserializeConvertible(data, c)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return c
|
|
}
|
|
|
|
// ToStackItem implements stackitem.Convertible. It never returns an error.
|
|
func (c *candidate) ToStackItem() (stackitem.Item, error) {
|
|
return stackitem.NewStruct([]stackitem.Item{
|
|
stackitem.NewBool(c.Registered),
|
|
stackitem.NewBigInteger(&c.Votes),
|
|
}), nil
|
|
}
|
|
|
|
// FromStackItem implements stackitem.Convertible.
|
|
func (c *candidate) FromStackItem(item stackitem.Item) error {
|
|
arr := item.(*stackitem.Struct).Value().([]stackitem.Item)
|
|
vs, err := arr[1].TryInteger()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
reg, err := arr[0].TryBool()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.Registered = reg
|
|
c.Votes = *vs
|
|
return nil
|
|
}
|