From 27087bb06454cf1d7ab4882a0bdba7590c346526 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 5 Apr 2018 18:34:25 -0400 Subject: [PATCH] cwgoes comments, validator.VotingPower -> validator.Power --- x/stake/keeper.go | 12 +++++++----- x/stake/keeper_test.go | 22 +++++++++++----------- x/stake/types.go | 14 +++++++------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/x/stake/keeper.go b/x/stake/keeper.go index 4415c92e1..0f29b177b 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -111,7 +111,7 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) { if oldFound { store.Delete(GetValidatorKey(address, oldCandidate.Assets, k.cdc)) } - store.Set(GetValidatorKey(address, validator.VotingPower, k.cdc), bz) + store.Set(GetValidatorKey(address, validator.Power, k.cdc), bz) // add to the validators to update list if is already a validator // or is a new validator @@ -161,10 +161,12 @@ func (k Keeper) removeCandidate(ctx sdk.Context, address sdk.Address) { //___________________________________________________________________________ -// get the most recent updated validator set from the Candidates. These bonds -// are already sorted by Assets from the UpdateVotingPower function which -// is the only function which is to modify the Assets -// this function also updaates the most recent validators saved in store +// Get the validator set from the candidates. The correct subset is retrieved +// by iterating through an index of the candidates sorted by power, stored +// using the ValidatorsKey. Simultaniously the most recent the validator +// records are updated in store with the RecentValidatorsKey. This store is +// used to determine if a candidate is a validator without needing to iterate +// over the subspace as we do in GetValidators func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) { store := ctx.KVStore(k.storeKey) diff --git a/x/stake/keeper_test.go b/x/stake/keeper_test.go index 2b165fc79..088bc5e30 100644 --- a/x/stake/keeper_test.go +++ b/x/stake/keeper_test.go @@ -218,11 +218,11 @@ func TestGetValidators(t *testing.T) { // first make sure everything as normal is ordered validators := keeper.GetValidators(ctx) require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(400), validators[0].VotingPower, "%v", validators) - assert.Equal(t, sdk.NewRat(200), validators[1].VotingPower, "%v", validators) - assert.Equal(t, sdk.NewRat(100), validators[2].VotingPower, "%v", validators) - assert.Equal(t, sdk.NewRat(1), validators[3].VotingPower, "%v", validators) - assert.Equal(t, sdk.NewRat(0), validators[4].VotingPower, "%v", validators) + assert.Equal(t, sdk.NewRat(400), validators[0].Power, "%v", validators) + assert.Equal(t, sdk.NewRat(200), validators[1].Power, "%v", validators) + assert.Equal(t, sdk.NewRat(100), validators[2].Power, "%v", validators) + assert.Equal(t, sdk.NewRat(1), validators[3].Power, "%v", validators) + assert.Equal(t, sdk.NewRat(0), validators[4].Power, "%v", validators) assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) assert.Equal(t, candidates[1].Address, validators[2].Address, "%v", validators) @@ -234,7 +234,7 @@ func TestGetValidators(t *testing.T) { keeper.setCandidate(ctx, candidates[3]) validators = keeper.GetValidators(ctx) require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(500), validators[0].VotingPower, "%v", validators) + assert.Equal(t, sdk.NewRat(500), validators[0].Power, "%v", validators) assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) // test a decrease in voting power @@ -242,7 +242,7 @@ func TestGetValidators(t *testing.T) { keeper.setCandidate(ctx, candidates[3]) validators = keeper.GetValidators(ctx) require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(300), validators[0].VotingPower, "%v", validators) + assert.Equal(t, sdk.NewRat(300), validators[0].Power, "%v", validators) assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) // test a swap in voting power @@ -250,9 +250,9 @@ func TestGetValidators(t *testing.T) { keeper.setCandidate(ctx, candidates[0]) validators = keeper.GetValidators(ctx) require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(600), validators[0].VotingPower, "%v", validators) + assert.Equal(t, sdk.NewRat(600), validators[0].Power, "%v", validators) assert.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - assert.Equal(t, sdk.NewRat(300), validators[1].VotingPower, "%v", validators) + assert.Equal(t, sdk.NewRat(300), validators[1].Power, "%v", validators) assert.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) // test the max validators term @@ -262,9 +262,9 @@ func TestGetValidators(t *testing.T) { keeper.setParams(ctx, params) validators = keeper.GetValidators(ctx) require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(600), validators[0].VotingPower, "%v", validators) + assert.Equal(t, sdk.NewRat(600), validators[0].Power, "%v", validators) assert.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - assert.Equal(t, sdk.NewRat(300), validators[1].VotingPower, "%v", validators) + assert.Equal(t, sdk.NewRat(300), validators[1].Power, "%v", validators) assert.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) } diff --git a/x/stake/types.go b/x/stake/types.go index c483a1896..1154f7962 100644 --- a/x/stake/types.go +++ b/x/stake/types.go @@ -106,9 +106,9 @@ func (c Candidate) delegatorShareExRate() sdk.Rat { // Should only be called when the Candidate qualifies as a validator. func (c Candidate) validator() Validator { return Validator{ - Address: c.Address, - PubKey: c.PubKey, - VotingPower: c.Assets, + Address: c.Address, + PubKey: c.PubKey, + Power: c.Assets, } } @@ -119,9 +119,9 @@ func (c Candidate) validator() Validator { // Validator is one of the top Candidates type Validator struct { - Address sdk.Address `json:"address"` - PubKey crypto.PubKey `json:"PubKey"` - VotingPower sdk.Rat `json:"voting_power"` + Address sdk.Address `json:"address"` + PubKey crypto.PubKey `json:"pub_key"` + Power sdk.Rat `json:"voting_power"` } // abci validator from stake validator type @@ -132,7 +132,7 @@ func (v Validator) abciValidator(cdc *wire.Codec) abci.Validator { } return abci.Validator{ PubKey: pkBytes, - Power: v.VotingPower.Evaluate(), + Power: v.Power.Evaluate(), } }