From 4cbf253c141e3b395590ec0b52919f2a82f4704a Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 17 May 2018 15:48:47 -0400 Subject: [PATCH] more cwgoes updates --- x/stake/keeper.go | 18 +++++++++++------- x/stake/shares.go | 3 ++- x/stake/store.md | 7 ++++--- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/x/stake/keeper.go b/x/stake/keeper.go index 7289dc8d54..b76f3ddb31 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -167,8 +167,8 @@ func (k Keeper) removeValidator(ctx sdk.Context, address sdk.Address) { store.Delete(GetValidatorKey(address)) store.Delete(GetValidatorsBondedByPowerKey(validator, pool)) - // delete from current and power weighted validator groups if the validator - // exists and add validator with zero power to the validator updates + // delete from the current and power weighted validator groups if the validator + // is bonded - and add validator with zero power to the validator updates if store.Get(GetValidatorsBondedKey(validator.PubKey)) == nil { return } @@ -190,6 +190,11 @@ func (k Keeper) GetValidatorsBonded(ctx sdk.Context) (validators []Validator) { iterator := store.SubspaceIterator(ValidatorsBondedKey) i := 0 for ; iterator.Valid(); iterator.Next() { + + // sanity check + if i > int(maxValidators-1) { + panic("maxValidators is less than the number of records in ValidatorsBonded Store, store should have been updated") + } bz := iterator.Value() var validator Validator k.cdc.MustUnmarshalBinary(bz, &validator) @@ -276,7 +281,7 @@ func (k Keeper) updateBondedValidators(ctx sdk.Context, store sdk.KVStore, pool if found { // remove from ToKickOut group - toKickOut[string(validator.Address)] = nil + delete(toKickOut, string(validator.Address)) } else { // if it wasn't in the toKickOut group it means @@ -299,9 +304,6 @@ func (k Keeper) updateBondedValidators(ctx sdk.Context, store sdk.KVStore, pool // perform the actual kicks for _, value := range toKickOut { - if value == nil { - continue - } var validator Validator k.cdc.MustUnmarshalBinary(value, &validator) k.unbondValidator(ctx, store, validator) @@ -336,6 +338,9 @@ func (k Keeper) unbondValidator(ctx sdk.Context, store sdk.KVStore, validator Va // perform all the store operations for when a validator status becomes bonded func (k Keeper) bondValidator(ctx sdk.Context, store sdk.KVStore, validator Validator, pool Pool) Validator { + // first delete the old record in the pool + store.Delete(GetValidatorsBondedByPowerKey(validator, pool)) + // set the status validator.Status = sdk.Bonded validator, pool = validator.UpdateSharesLocation(pool) @@ -343,7 +348,6 @@ func (k Keeper) bondValidator(ctx sdk.Context, store sdk.KVStore, validator Vali // save the now bonded validator record to the three referened stores bzVal := k.cdc.MustMarshalBinary(validator) - store.Delete(GetValidatorsBondedByPowerKey(validator, pool)) store.Set(GetValidatorKey(validator.Address), bzVal) store.Set(GetValidatorsBondedByPowerKey(validator, pool), bzVal) store.Set(GetValidatorsBondedKey(validator.PubKey), bzVal) diff --git a/x/stake/shares.go b/x/stake/shares.go index ac3fda3f07..425596197f 100644 --- a/x/stake/shares.go +++ b/x/stake/shares.go @@ -134,6 +134,7 @@ func (s PoolShares) Tokens(p Pool) sdk.Rat { return p.unbondedShareExRate().Mul(s.Amount) case ShareUnbonded: return p.unbondedShareExRate().Mul(s.Amount) + default: + panic("unknown share kind") } - return sdk.ZeroRat() } diff --git a/x/stake/store.md b/x/stake/store.md index 6927121f1d..507e0513a6 100644 --- a/x/stake/store.md +++ b/x/stake/store.md @@ -7,14 +7,14 @@ prefixed areas of the staking store which are accessed in `x/stake/keeper.go`. ## Validators - Prefix Key Space: ValidatorsKey - Key/Sort: Validator Owner Address - - Contains: All Validator records whether independent of being bonded or not + - Contains: All Validator records independent of being bonded or not - Used For: Retrieve validator from owner address, general validator retrieval ## Validators By Power - Prefix Key Space: ValidatorsByPowerKey - Key/Sort: Validator Power (equivalent bonded shares) then Block Height then Transaction Order - - Contains: All Validator records whether independent of being bonded or not + - Contains: All Validator records independent of being bonded or not - Used For: Determining who the top validators are whom should be bonded ## Validators Bonded @@ -30,4 +30,5 @@ prefixed areas of the staking store which are accessed in `x/stake/keeper.go`. - Prefix Key Space: TendermintUpdatesKey - Key/Sort: Validator Owner Address - Contains: Validators are queued to affect the consensus validation set in Tendermint - - Used For: Informing Tendermint of the validator set updates + - Used For: Informing Tendermint of the validator set updates, is used only intra-block, as the + updates are applied then cleared on endblock