neoneo-go/pkg/rpc/response/result/validator.go
Roman Khimov 0da0bb21ee rpc: make getnextvalidators behavior compliant with C# node
Turns out, our getnextvalidators implementation already works the way
getcandidates is supposed to work, but original getnextvalidators works a bit
differently. It only returns validators, it doesn't return Active flag (all
of them are active) and it represents votes as a number. So for the maximum
compatibility:
 * drop non-validator keys from getnextvalidators server-side
 * drop Active flag client-side (sorry, it doesn't exist)
 * allow unmarshalling old answers along with the new one

This technically breaks `query candidates` CLI command, but it'll be fixed
when getcandidates are to be introduced.
2022-07-01 14:52:02 +03:00

43 lines
961 B
Go

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
}