tendermint/state/validator.go

120 lines
2.7 KiB
Go
Raw Normal View History

package state
import (
"bytes"
2014-10-18 01:42:33 -07:00
"fmt"
"io"
2015-03-30 22:50:27 -07:00
"github.com/tendermint/tendermint2/account"
"github.com/tendermint/tendermint2/binary"
"github.com/tendermint/tendermint2/types"
)
2014-12-23 23:20:49 -08:00
// Persistent (mostly) static data for each Validator
type ValidatorInfo struct {
Address []byte
PubKey account.PubKeyEd25519
UnbondTo []*types.TxOutput
FirstBondHeight uint
2014-12-17 01:37:13 -08:00
FirstBondAmount uint64
// If destroyed:
DestroyedHeight uint
DestroyedAmount uint64
// If released:
ReleasedHeight uint
}
func (valInfo *ValidatorInfo) Copy() *ValidatorInfo {
valInfoCopy := *valInfo
return &valInfoCopy
}
func ValidatorInfoEncoder(o interface{}, w io.Writer, n *int64, err *error) {
binary.WriteBinary(o.(*ValidatorInfo), w, n, err)
}
func ValidatorInfoDecoder(r io.Reader, n *int64, err *error) interface{} {
return binary.ReadBinary(&ValidatorInfo{}, r, n, err)
}
var ValidatorInfoCodec = binary.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 {
Address []byte
PubKey account.PubKeyEd25519
BondHeight uint
UnbondHeight uint
LastCommitHeight uint
2014-10-12 17:57:23 -07:00
VotingPower uint64
Accum int64
}
// Creates a new copy of the validator so we can mutate accum.
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 {
panic("Cannot compare identical validators")
}
}
}
2014-10-18 01:42:33 -07:00
func (v *Validator) String() string {
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 {
return binary.BinarySha256(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) {
binary.WriteBinary(o.(*Validator), w, n, err)
}
func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
return binary.ReadBinary(&Validator{}, r, n, err)
}
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
}