neoneo-go/pkg/core/native/native_neo_candidate.go
Elizaveta Chichindaeva 28908aa3cf [] English Check
Signed-off-by: Elizaveta Chichindaeva <elizaveta@nspcc.ru>
2022-05-04 19:48:27 +03:00

45 lines
979 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 a candidate from the 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
}