2014-09-03 19:21:19 -07:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2014-12-09 18:49:04 -08:00
|
|
|
"bytes"
|
2014-10-18 01:42:33 -07:00
|
|
|
"fmt"
|
2014-09-03 19:21:19 -07:00
|
|
|
"io"
|
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
"github.com/tendermint/tendermint/account"
|
|
|
|
"github.com/tendermint/tendermint/binary"
|
2015-03-22 19:00:08 -07:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2014-09-03 19:21:19 -07:00
|
|
|
)
|
|
|
|
|
2014-12-23 23:20:49 -08:00
|
|
|
// Persistent (mostly) static data for each Validator
|
2014-12-09 18:49:04 -08:00
|
|
|
type ValidatorInfo struct {
|
|
|
|
Address []byte
|
2015-01-14 20:34:53 -08:00
|
|
|
PubKey account.PubKeyEd25519
|
2015-03-22 19:00:08 -07:00
|
|
|
UnbondTo []*types.TxOutput
|
2014-12-09 18:49:04 -08:00
|
|
|
FirstBondHeight uint
|
2014-12-17 01:37:13 -08:00
|
|
|
FirstBondAmount uint64
|
2014-12-09 18:49:04 -08:00
|
|
|
|
|
|
|
// 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) {
|
2015-01-14 20:34:53 -08:00
|
|
|
binary.WriteBinary(o.(*ValidatorInfo), w, n, err)
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
func ValidatorInfoDecoder(r io.Reader, n *int64, err *error) interface{} {
|
|
|
|
return binary.ReadBinary(&ValidatorInfo{}, r, n, err)
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
var ValidatorInfoCodec = binary.Codec{
|
2014-12-09 18:49:04 -08:00
|
|
|
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
|
2014-09-03 19:21:19 -07:00
|
|
|
type Validator struct {
|
2014-12-09 18:49:04 -08:00
|
|
|
Address []byte
|
2015-01-14 20:34:53 -08:00
|
|
|
PubKey account.PubKeyEd25519
|
2014-12-09 18:49:04 -08:00
|
|
|
BondHeight uint
|
|
|
|
UnbondHeight uint
|
|
|
|
LastCommitHeight uint
|
2014-10-12 17:57:23 -07:00
|
|
|
VotingPower uint64
|
|
|
|
Accum int64
|
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 {
|
2014-11-05 03:08:29 -08:00
|
|
|
vCopy := *v
|
|
|
|
return &vCopy
|
2014-09-03 19:21:19 -07:00
|
|
|
}
|
|
|
|
|
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 {
|
2014-12-09 18:49:04 -08:00
|
|
|
if bytes.Compare(v.Address, other.Address) < 0 {
|
2014-10-11 21:27:58 -07:00
|
|
|
return v
|
2014-12-09 18:49:04 -08:00
|
|
|
} 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-09-03 19:21:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-18 01:42:33 -07:00
|
|
|
func (v *Validator) String() string {
|
2014-12-09 18:49:04 -08:00
|
|
|
return fmt.Sprintf("Validator{%X %v %v-%v-%v VP:%v A:%v}",
|
|
|
|
v.Address,
|
|
|
|
v.PubKey,
|
2014-11-05 03:08:29 -08:00
|
|
|
v.BondHeight,
|
|
|
|
v.LastCommitHeight,
|
|
|
|
v.UnbondHeight,
|
|
|
|
v.VotingPower,
|
|
|
|
v.Accum)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validator) Hash() []byte {
|
2015-01-14 20:34:53 -08:00
|
|
|
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-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) {
|
2015-01-14 20:34:53 -08:00
|
|
|
binary.WriteBinary(o.(*Validator), w, n, err)
|
2014-09-03 19:21:19 -07:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
|
|
|
|
return binary.ReadBinary(&Validator{}, 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
|
|
|
}
|