github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/neorpc/result/validator.go (about)

     1  package result
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
     7  )
     8  
     9  // Validator is used for the representation of consensus node data in the JSON-RPC
    10  // protocol.
    11  type Validator struct {
    12  	PublicKey keys.PublicKey `json:"publickey"`
    13  	Votes     int64          `json:"votes"`
    14  }
    15  
    16  // Candidate represents a node participating in the governance elections, it's
    17  // active when it's a validator (consensus node).
    18  type Candidate struct {
    19  	PublicKey keys.PublicKey `json:"publickey"`
    20  	Votes     int64          `json:"votes,string"`
    21  	Active    bool           `json:"active"`
    22  }
    23  
    24  type newValidator struct {
    25  	PublicKey keys.PublicKey `json:"publickey"`
    26  	Votes     int64          `json:"votes"`
    27  }
    28  
    29  type oldValidator struct {
    30  	PublicKey keys.PublicKey `json:"publickey"`
    31  	Votes     int64          `json:"votes,string"`
    32  }
    33  
    34  // UnmarshalJSON implements the json.Unmarshaler interface.
    35  func (v *Validator) UnmarshalJSON(data []byte) error {
    36  	var nv = new(newValidator)
    37  	err := json.Unmarshal(data, nv)
    38  	if err != nil {
    39  		var ov = new(oldValidator)
    40  		err := json.Unmarshal(data, ov)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		v.PublicKey = ov.PublicKey
    45  		v.Votes = ov.Votes
    46  		return nil
    47  	}
    48  	v.PublicKey = nv.PublicKey
    49  	v.Votes = nv.Votes
    50  	return nil
    51  }