From 905a9eefb9b6113a5675bc7e095ef51d9bb1b797 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Mon, 9 Apr 2018 12:17:45 +0200 Subject: [PATCH] Cleanup implementation --- x/stake/keeper.go | 7 ++++++- x/stake/keeper_keys.go | 4 ++-- x/stake/types.go | 6 +++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/x/stake/keeper.go b/x/stake/keeper.go index 2dcfd3caf..a51877b3c 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -80,6 +80,9 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) { // retreive the old candidate record oldCandidate, oldFound := k.GetCandidate(ctx, address) + // update the validator block height (will only get written if stake has changed) + candidate.ValidatorHeight = ctx.BlockHeight() + // marshal the candidate record and add to the state bz, err := k.cdc.MarshalBinary(candidate) if err != nil { @@ -87,7 +90,7 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) { } store.Set(GetCandidateKey(candidate.Address), bz) - // mashal the new validator record + // marshal the new validator record validator := candidate.validator() bz, err = k.cdc.MarshalBinary(validator) if err != nil { @@ -121,7 +124,9 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) { panic(err) } store.Set(GetAccUpdateValidatorKey(validator.Address), bz) + } + return } diff --git a/x/stake/keeper_keys.go b/x/stake/keeper_keys.go index ae8f9d733..a8adc2a12 100644 --- a/x/stake/keeper_keys.go +++ b/x/stake/keeper_keys.go @@ -32,10 +32,10 @@ func GetCandidateKey(addr sdk.Address) []byte { } // get the key for the validator used in the power-store -func GetValidatorKey(addr sdk.Address, power sdk.Rat, height uint64, cdc *wire.Codec) []byte { +func GetValidatorKey(addr sdk.Address, power sdk.Rat, height int64, cdc *wire.Codec) []byte { powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first) heightBytes := make([]byte, 8) - binary.LittleEndian.PutUint64(heightBytes, height) // height little-endian (older validators first) + binary.LittleEndian.PutUint64(heightBytes, uint64(height)) // height little-endian (older validators first) return append(ValidatorsKey, append(powerBytes, append(heightBytes, addr.Bytes()...)...)...) } diff --git a/x/stake/types.go b/x/stake/types.go index 7757566bf..052a50fa2 100644 --- a/x/stake/types.go +++ b/x/stake/types.go @@ -65,7 +65,7 @@ type Candidate struct { 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 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 + ValidatorHeight int64 `json:"validator_height"` // Earliest height at current voting power } // Candidates - list of Candidates @@ -80,7 +80,7 @@ func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Descrip Assets: sdk.ZeroRat, Liabilities: sdk.ZeroRat, Description: description, - ValidatorHeight: uint64(0), + ValidatorHeight: int64(0), } } @@ -130,7 +130,7 @@ type Validator struct { Address sdk.Address `json:"address"` PubKey crypto.PubKey `json:"pub_key"` Power sdk.Rat `json:"voting_power"` - Height uint64 `json:"height"` // If considered a validator, height when first considered a validator, else 0 + Height int64 `json:"height"` // Earliest height at current voting power } // abci validator from stake validator type