tendermint/types/validator.go

122 lines
3.3 KiB
Go
Raw Normal View History

package types
import (
"bytes"
2014-10-18 01:42:33 -07:00
"fmt"
"io"
2015-07-19 09:40:55 -07:00
acm "github.com/tendermint/tendermint/account"
2015-07-19 16:42:52 -07:00
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/wire"
)
2014-12-23 23:20:49 -08:00
// Persistent (mostly) static data for each Validator
type ValidatorInfo struct {
2015-07-19 09:40:55 -07:00
Address []byte `json:"address"`
PubKey acm.PubKeyEd25519 `json:"pub_key"`
UnbondTo []*TxOutput `json:"unbond_to"`
2015-07-19 09:40:55 -07:00
FirstBondHeight int `json:"first_bond_height"`
FirstBondAmount int64 `json:"first_bond_amount"`
DestroyedHeight int `json:"destroyed_height"` // If destroyed
DestroyedAmount int64 `json:"destroyed_amount"` // If destroyed
ReleasedHeight int `json:"released_height"` // If released
}
func (valInfo *ValidatorInfo) Copy() *ValidatorInfo {
valInfoCopy := *valInfo
return &valInfoCopy
}
func ValidatorInfoEncoder(o interface{}, w io.Writer, n *int64, err *error) {
2015-07-25 15:45:45 -07:00
wire.WriteBinary(o.(*ValidatorInfo), w, n, err)
}
func ValidatorInfoDecoder(r io.Reader, n *int64, err *error) interface{} {
2015-07-25 15:45:45 -07:00
return wire.ReadBinary(&ValidatorInfo{}, r, n, err)
}
2015-07-25 15:45:45 -07:00
var ValidatorInfoCodec = wire.Codec{
Encode: ValidatorInfoEncoder,
Decode: ValidatorInfoDecoder,
}
//-----------------------------------------------------------------------------
// Volatile state for each Validator
// Also persisted with the state, but fields change
// every height|round so they don't go in merkle.Tree
type Validator struct {
2015-07-19 09:40:55 -07:00
Address []byte `json:"address"`
PubKey acm.PubKeyEd25519 `json:"pub_key"`
BondHeight int `json:"bond_height"`
UnbondHeight int `json:"unbond_height"`
LastCommitHeight int `json:"last_commit_height"`
VotingPower int64 `json:"voting_power"`
Accum int64 `json:"accum"`
}
// Creates a new copy of the validator so we can mutate accum.
// Panics if the validator is nil.
func (v *Validator) Copy() *Validator {
vCopy := *v
return &vCopy
}
2014-10-11 21:27:58 -07:00
// Returns the one with higher Accum.
func (v *Validator) CompareAccum(other *Validator) *Validator {
if v == nil {
return other
2014-09-14 15:37:32 -07:00
}
2014-10-11 21:27:58 -07:00
if v.Accum > other.Accum {
return v
} else if v.Accum < other.Accum {
return other
} else {
if bytes.Compare(v.Address, other.Address) < 0 {
2014-10-11 21:27:58 -07:00
return v
} else if bytes.Compare(v.Address, other.Address) > 0 {
2014-10-11 21:27:58 -07:00
return other
} else {
2015-07-19 16:42:52 -07:00
PanicSanity("Cannot compare identical validators")
return nil
}
}
}
2014-10-18 01:42:33 -07:00
func (v *Validator) String() string {
if v == nil {
return "nil-Validator"
}
return fmt.Sprintf("Validator{%X %v %v-%v-%v VP:%v A:%v}",
v.Address,
v.PubKey,
v.BondHeight,
v.LastCommitHeight,
v.UnbondHeight,
v.VotingPower,
v.Accum)
}
func (v *Validator) Hash() []byte {
2015-07-25 15:45:45 -07:00
return wire.BinaryRipemd160(v)
2014-10-18 01:42:33 -07:00
}
2014-10-11 21:27:58 -07:00
//-------------------------------------
2014-09-14 15:37:32 -07:00
2014-10-11 21:27:58 -07:00
var ValidatorCodec = validatorCodec{}
2014-09-14 15:37:32 -07:00
2014-10-11 21:27:58 -07:00
type validatorCodec struct{}
2014-10-11 21:27:58 -07:00
func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
2015-07-25 15:45:45 -07:00
wire.WriteBinary(o.(*Validator), w, n, err)
}
func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
2015-07-25 15:45:45 -07:00
return wire.ReadBinary(&Validator{}, r, n, err)
}
2014-10-11 21:27:58 -07:00
func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
2015-07-19 16:42:52 -07:00
PanicSanity("ValidatorCodec.Compare not implemented")
return 0
2014-10-07 00:43:34 -07:00
}