neo-go/pkg/rpc/response/result/validator.go

44 lines
961 B
Go
Raw Normal View History

package result
import (
"encoding/json"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
)
// Validator is used for the representation of consensus node data in the JSON-RPC
// protocol.
type Validator struct {
PublicKey keys.PublicKey `json:"publickey"`
Votes int64 `json:"votes"`
}
type newValidator struct {
PublicKey keys.PublicKey `json:"publickey"`
Votes int64 `json:"votes"`
}
type oldValidator struct {
PublicKey keys.PublicKey `json:"publickey"`
Votes int64 `json:"votes,string"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (v *Validator) UnmarshalJSON(data []byte) error {
var nv = new(newValidator)
err := json.Unmarshal(data, nv)
if err != nil {
var ov = new(oldValidator)
err := json.Unmarshal(data, ov)
if err != nil {
return err
}
v.PublicKey = ov.PublicKey
v.Votes = ov.Votes
return nil
}
v.PublicKey = nv.PublicKey
v.Votes = nv.Votes
return nil
}