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-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/account"
|
|
|
|
"github.com/tendermint/tendermint/binary"
|
|
|
|
"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 {
|
2015-05-01 17:26:49 -07:00
|
|
|
Address []byte `json:"address"`
|
|
|
|
PubKey account.PubKeyEd25519 `json:"pub_key"`
|
|
|
|
UnbondTo []*types.TxOutput `json:"unbond_to"`
|
2015-06-25 20:28:34 -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
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-05-01 17:26:49 -07:00
|
|
|
Address []byte `json:"address"`
|
|
|
|
PubKey account.PubKeyEd25519 `json:"pub_key"`
|
2015-06-25 20:28:34 -07:00
|
|
|
BondHeight int `json:"bond_height"`
|
|
|
|
UnbondHeight int `json:"unbond_height"`
|
|
|
|
LastCommitHeight int `json:"last_commit_height"`
|
|
|
|
VotingPower int64 `json:"voting_power"`
|
2015-05-01 17:26:49 -07:00
|
|
|
Accum int64 `json:"accum"`
|
2014-09-03 19:21:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new copy of the validator so we can mutate accum.
|
2015-07-10 09:55:23 -07:00
|
|
|
// Panics if the validator is nil.
|
2014-09-03 19:21:19 -07:00
|
|
|
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 {
|
2015-06-16 21:16:58 -07:00
|
|
|
// SANITY CHECK
|
2014-10-11 21:27:58 -07:00
|
|
|
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 {
|
2015-07-10 09:55:23 -07:00
|
|
|
if v == nil {
|
|
|
|
return "nil-Validator"
|
|
|
|
}
|
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
|
|
|
}
|