WIP: Track validator height (closes #582)

This commit is contained in:
Christopher Goes 2018-04-06 13:11:26 +02:00
parent 1b9afdab48
commit b1c83f2edb
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
3 changed files with 26 additions and 18 deletions

View File

@ -101,9 +101,9 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
// update the list ordered by voting power // update the list ordered by voting power
if oldFound { if oldFound {
store.Delete(GetValidatorKey(address, oldCandidate.Assets, k.cdc)) store.Delete(GetValidatorKey(address, oldCandidate.Assets, oldCandidate.ValidatorHeight, k.cdc))
} }
store.Set(GetValidatorKey(address, validator.Power, k.cdc), bz) store.Set(GetValidatorKey(address, validator.Power, validator.Height, k.cdc), bz)
// add to the validators to update list if is already a validator // add to the validators to update list if is already a validator
// or is a new validator // or is a new validator
@ -136,7 +136,7 @@ func (k Keeper) removeCandidate(ctx sdk.Context, address sdk.Address) {
// delete the old candidate record // delete the old candidate record
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
store.Delete(GetCandidateKey(address)) store.Delete(GetCandidateKey(address))
store.Delete(GetValidatorKey(address, candidate.Assets, k.cdc)) store.Delete(GetValidatorKey(address, candidate.Assets, candidate.ValidatorHeight, k.cdc))
// delete from recent and power weighted validator groups if the validator // delete from recent and power weighted validator groups if the validator
// exists and add validator with zero power to the validator updates // exists and add validator with zero power to the validator updates

View File

@ -1,6 +1,8 @@
package stake package stake
import ( import (
"encoding/binary"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
) )
@ -30,9 +32,11 @@ func GetCandidateKey(addr sdk.Address) []byte {
} }
// get the key for the validator used in the power-store // get the key for the validator used in the power-store
func GetValidatorKey(addr sdk.Address, power sdk.Rat, cdc *wire.Codec) []byte { func GetValidatorKey(addr sdk.Address, power sdk.Rat, height uint64, cdc *wire.Codec) []byte {
powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount)) powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first)
return append(ValidatorsKey, append(powerBytes, addr.Bytes()...)...) heightBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(heightBytes, height) // height little-endian (older validators first)
return append(ValidatorsKey, append(powerBytes, append(heightBytes, addr.Bytes()...)...)...)
} }
// get the key for the accumulated update validators // get the key for the accumulated update validators

View File

@ -59,12 +59,13 @@ const (
// exchange rate. Voting power can be calculated as total bonds multiplied by // exchange rate. Voting power can be calculated as total bonds multiplied by
// exchange rate. // exchange rate.
type Candidate struct { type Candidate struct {
Status CandidateStatus `json:"status"` // Bonded status Status CandidateStatus `json:"status"` // Bonded status
Address sdk.Address `json:"owner"` // Sender of BondTx - UnbondTx returns here Address sdk.Address `json:"owner"` // Sender of BondTx - UnbondTx returns here
PubKey crypto.PubKey `json:"pub_key"` // Pubkey of candidate PubKey crypto.PubKey `json:"pub_key"` // Pubkey of candidate
Assets sdk.Rat `json:"assets"` // total shares of a global hold pools Assets sdk.Rat `json:"assets"` // total shares of a global hold pools
Liabilities sdk.Rat `json:"liabilities"` // total shares issued to a candidate's delegators Liabilities sdk.Rat `json:"liabilities"` // total shares issued to a candidate's delegators
Description Description `json:"description"` // Description terms for the candidate Description Description `json:"description"` // Description terms for the candidate
ValidatorHeight uint64 `json:"validator_height"` // If considered a validator, height when first considered a validator, else 0
} }
// Candidates - list of Candidates // Candidates - list of Candidates
@ -73,12 +74,13 @@ type Candidates []Candidate
// NewCandidate - initialize a new candidate // NewCandidate - initialize a new candidate
func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Description) Candidate { func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Description) Candidate {
return Candidate{ return Candidate{
Status: Unbonded, Status: Unbonded,
Address: address, Address: address,
PubKey: pubKey, PubKey: pubKey,
Assets: sdk.ZeroRat, Assets: sdk.ZeroRat,
Liabilities: sdk.ZeroRat, Liabilities: sdk.ZeroRat,
Description: description, Description: description,
ValidatorHeight: uint64(0),
} }
} }
@ -114,6 +116,7 @@ func (c Candidate) validator() Validator {
Address: c.Address, Address: c.Address,
PubKey: c.PubKey, PubKey: c.PubKey,
Power: c.Assets, Power: c.Assets,
Height: c.ValidatorHeight,
} }
} }
@ -127,6 +130,7 @@ type Validator struct {
Address sdk.Address `json:"address"` Address sdk.Address `json:"address"`
PubKey crypto.PubKey `json:"pub_key"` PubKey crypto.PubKey `json:"pub_key"`
Power sdk.Rat `json:"voting_power"` Power sdk.Rat `json:"voting_power"`
Height uint64 `json:"height"` // If considered a validator, height when first considered a validator, else 0
} }
// abci validator from stake validator type // abci validator from stake validator type