2014-09-03 19:21:19 -07:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
. "github.com/tendermint/tendermint/binary"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Holds state for a Validator at a given height+round.
|
|
|
|
// Meant to be discarded every round of the consensus protocol.
|
|
|
|
// TODO consider moving this to another common types package.
|
|
|
|
type Validator struct {
|
|
|
|
Account
|
2014-10-04 19:16:49 -07:00
|
|
|
BondHeight uint32 // TODO: is this needed?
|
2014-09-03 19:21:19 -07:00
|
|
|
VotingPower uint64
|
|
|
|
Accum int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to persist the state of ConsensusStateControl.
|
2014-09-03 20:41:57 -07:00
|
|
|
func ReadValidator(r io.Reader, n *int64, err *error) *Validator {
|
2014-09-03 19:21:19 -07:00
|
|
|
return &Validator{
|
2014-10-04 19:16:49 -07:00
|
|
|
Account: ReadAccount(r, n, err),
|
2014-09-03 20:41:57 -07:00
|
|
|
BondHeight: ReadUInt32(r, n, err),
|
|
|
|
VotingPower: ReadUInt64(r, n, err),
|
|
|
|
Accum: ReadInt64(r, n, err),
|
2014-09-03 19:21:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new copy of the validator so we can mutate accum.
|
|
|
|
func (v *Validator) Copy() *Validator {
|
|
|
|
return &Validator{
|
|
|
|
Account: v.Account,
|
|
|
|
BondHeight: v.BondHeight,
|
|
|
|
VotingPower: v.VotingPower,
|
|
|
|
Accum: v.Accum,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to persist the state of ConsensusStateControl.
|
|
|
|
func (v *Validator) WriteTo(w io.Writer) (n int64, err error) {
|
2014-10-04 19:16:49 -07:00
|
|
|
WriteBinary(w, v.Account, &n, &err)
|
2014-09-03 20:41:57 -07:00
|
|
|
WriteUInt32(w, v.BondHeight, &n, &err)
|
|
|
|
WriteUInt64(w, v.VotingPower, &n, &err)
|
|
|
|
WriteInt64(w, v.Accum, &n, &err)
|
2014-09-03 19:21:19 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 v.Id < other.Id {
|
|
|
|
return v
|
|
|
|
} else if v.Id > other.Id {
|
|
|
|
return other
|
|
|
|
} else {
|
|
|
|
panic("Cannot compare identical validators")
|
2014-09-03 19:21:19 -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-09-03 19:21:19 -07:00
|
|
|
|
2014-10-11 21:27:58 -07:00
|
|
|
func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
|
|
|
|
WriteBinary(w, o.(*Validator), n, err)
|
2014-09-03 19:21:19 -07:00
|
|
|
}
|
|
|
|
|
2014-10-11 21:27:58 -07:00
|
|
|
func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
|
|
|
|
return ReadValidator(r, n, err)
|
2014-09-03 19:21:19 -07:00
|
|
|
}
|
|
|
|
|
2014-10-11 21:27:58 -07:00
|
|
|
func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
|
|
|
|
panic("ValidatorCodec.Compare not implemented")
|
2014-10-07 00:43:34 -07:00
|
|
|
}
|