2020-08-03 12:00:27 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
|
|
|
type candidate struct {
|
|
|
|
Registered bool
|
|
|
|
Votes big.Int
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// FromBytes unmarshals a candidate from the byte array.
|
2020-08-03 12:00:27 +00:00
|
|
|
func (c *candidate) FromBytes(data []byte) *candidate {
|
2021-07-17 15:37:33 +00:00
|
|
|
err := stackitem.DeserializeConvertible(data, c)
|
2021-07-16 13:00:25 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-08-03 12:00:27 +00:00
|
|
|
}
|
2021-07-17 15:37:33 +00:00
|
|
|
return c
|
2020-08-03 12:00:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
// ToStackItem implements stackitem.Convertible. It never returns an error.
|
|
|
|
func (c *candidate) ToStackItem() (stackitem.Item, error) {
|
2020-08-03 12:00:27 +00:00
|
|
|
return stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.NewBool(c.Registered),
|
|
|
|
stackitem.NewBigInteger(&c.Votes),
|
2021-07-17 15:37:33 +00:00
|
|
|
}), nil
|
2020-08-03 12:00:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
// FromStackItem implements stackitem.Convertible.
|
|
|
|
func (c *candidate) FromStackItem(item stackitem.Item) error {
|
2020-08-03 12:00:27 +00:00
|
|
|
arr := item.(*stackitem.Struct).Value().([]stackitem.Item)
|
|
|
|
vs, err := arr[1].TryInteger()
|
|
|
|
if err != nil {
|
2021-07-17 15:37:33 +00:00
|
|
|
return err
|
2020-08-03 12:00:27 +00:00
|
|
|
}
|
2021-07-17 15:37:33 +00:00
|
|
|
reg, err := arr[0].TryBool()
|
2020-08-21 17:55:20 +00:00
|
|
|
if err != nil {
|
2021-07-17 15:37:33 +00:00
|
|
|
return err
|
2020-08-21 17:55:20 +00:00
|
|
|
}
|
2021-07-17 15:37:33 +00:00
|
|
|
c.Registered = reg
|
2020-08-03 12:00:27 +00:00
|
|
|
c.Votes = *vs
|
2021-07-17 15:37:33 +00:00
|
|
|
return nil
|
2020-08-03 12:00:27 +00:00
|
|
|
}
|