package keeper import ( "bytes" "fmt" "sort" gogotypes "github.com/gogo/protobuf/types" abci "github.com/tendermint/tendermint/abci/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/staking/types" ) // Calculate the ValidatorUpdates for the current block // Called in each EndBlock func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate { // Calculate validator set changes. // // NOTE: ApplyAndReturnValidatorSetUpdates has to come before // UnbondAllMatureValidatorQueue. // This fixes a bug when the unbonding period is instant (is the case in // some of the tests). The test expected the validator to be completely // unbonded after the Endblocker (go from Bonded -> Unbonding during // ApplyAndReturnValidatorSetUpdates and then Unbonding -> Unbonded during // UnbondAllMatureValidatorQueue). validatorUpdates := k.ApplyAndReturnValidatorSetUpdates(ctx) // Unbond all mature validators from the unbonding queue. k.UnbondAllMatureValidatorQueue(ctx) // Remove all mature unbonding delegations from the ubd queue. matureUnbonds := k.DequeueAllMatureUBDQueue(ctx, ctx.BlockHeader().Time) for _, dvPair := range matureUnbonds { balances, err := k.CompleteUnbondingWithAmount(ctx, dvPair.DelegatorAddress, dvPair.ValidatorAddress) if err != nil { continue } ctx.EventManager().EmitEvent( sdk.NewEvent( types.EventTypeCompleteUnbonding, sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()), sdk.NewAttribute(types.AttributeKeyValidator, dvPair.ValidatorAddress.String()), sdk.NewAttribute(types.AttributeKeyDelegator, dvPair.DelegatorAddress.String()), ), ) } // Remove all mature redelegations from the red queue. matureRedelegations := k.DequeueAllMatureRedelegationQueue(ctx, ctx.BlockHeader().Time) for _, dvvTriplet := range matureRedelegations { balances, err := k.CompleteRedelegationWithAmount( ctx, dvvTriplet.DelegatorAddress, dvvTriplet.ValidatorSrcAddress, dvvTriplet.ValidatorDstAddress, ) if err != nil { continue } ctx.EventManager().EmitEvent( sdk.NewEvent( types.EventTypeCompleteRedelegation, sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()), sdk.NewAttribute(types.AttributeKeyDelegator, dvvTriplet.DelegatorAddress.String()), sdk.NewAttribute(types.AttributeKeySrcValidator, dvvTriplet.ValidatorSrcAddress.String()), sdk.NewAttribute(types.AttributeKeyDstValidator, dvvTriplet.ValidatorDstAddress.String()), ), ) } return validatorUpdates } // Apply and return accumulated updates to the bonded validator set. Also, // * Updates the active valset as keyed by LastValidatorPowerKey. // * Updates the total power as keyed by LastTotalPowerKey. // * Updates validator status' according to updated powers. // * Updates the fee pool bonded vs not-bonded tokens. // * Updates relevant indices. // It gets called once after genesis, another time maybe after genesis transactions, // then once at every EndBlock. // // CONTRACT: Only validators with non-zero power or zero-power that were bonded // at the previous block height or were removed from the validator set entirely // are returned to Tendermint. func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []abci.ValidatorUpdate) { maxValidators := k.GetParams(ctx).MaxValidators totalPower := sdk.ZeroInt() amtFromBondedToNotBonded, amtFromNotBondedToBonded := sdk.ZeroInt(), sdk.ZeroInt() // Retrieve the last validator set. // The persistent set is updated later in this function. // (see LastValidatorPowerKey). last := k.getLastValidatorsByAddr(ctx) // Iterate over validators, highest power to lowest. iterator := k.ValidatorsPowerStoreIterator(ctx) defer iterator.Close() for count := 0; iterator.Valid() && count < int(maxValidators); iterator.Next() { // everything that is iterated in this loop is becoming or already a // part of the bonded validator set valAddr := sdk.ValAddress(iterator.Value()) validator := k.mustGetValidator(ctx, valAddr) if validator.Jailed { panic("should never retrieve a jailed validator from the power store") } // if we get to a zero-power validator (which we don't bond), // there are no more possible bonded validators if validator.PotentialConsensusPower() == 0 { break } // apply the appropriate state change if necessary switch { case validator.IsUnbonded(): validator = k.unbondedToBonded(ctx, validator) amtFromNotBondedToBonded = amtFromNotBondedToBonded.Add(validator.GetTokens()) case validator.IsUnbonding(): validator = k.unbondingToBonded(ctx, validator) amtFromNotBondedToBonded = amtFromNotBondedToBonded.Add(validator.GetTokens()) case validator.IsBonded(): // no state change default: panic("unexpected validator status") } // fetch the old power bytes var valAddrBytes [sdk.AddrLen]byte copy(valAddrBytes[:], valAddr[:]) oldPowerBytes, found := last[valAddrBytes] newPower := validator.ConsensusPower() newPowerBytes := k.cdc.MustMarshalBinaryLengthPrefixed(&gogotypes.Int64Value{Value: newPower}) // update the validator set if power has changed if !found || !bytes.Equal(oldPowerBytes, newPowerBytes) { updates = append(updates, validator.ABCIValidatorUpdate()) k.SetLastValidatorPower(ctx, valAddr, newPower) } delete(last, valAddrBytes) count++ totalPower = totalPower.Add(sdk.NewInt(newPower)) } noLongerBonded := sortNoLongerBonded(last) for _, valAddrBytes := range noLongerBonded { validator := k.mustGetValidator(ctx, sdk.ValAddress(valAddrBytes)) validator = k.bondedToUnbonding(ctx, validator) amtFromBondedToNotBonded = amtFromBondedToNotBonded.Add(validator.GetTokens()) k.DeleteLastValidatorPower(ctx, validator.GetOperator()) updates = append(updates, validator.ABCIValidatorUpdateZero()) } // Update the pools based on the recent updates in the validator set: // - The tokens from the non-bonded candidates that enter the new validator set need to be transferred // to the Bonded pool. // - The tokens from the bonded validators that are being kicked out from the validator set // need to be transferred to the NotBonded pool. switch { // Compare and subtract the respective amounts to only perform one transfer. // This is done in order to avoid doing multiple updates inside each iterator/loop. case amtFromNotBondedToBonded.GT(amtFromBondedToNotBonded): k.notBondedTokensToBonded(ctx, amtFromNotBondedToBonded.Sub(amtFromBondedToNotBonded)) case amtFromNotBondedToBonded.LT(amtFromBondedToNotBonded): k.bondedTokensToNotBonded(ctx, amtFromBondedToNotBonded.Sub(amtFromNotBondedToBonded)) default: // equal amounts of tokens; no update required } // set total power on lookup index if there are any updates if len(updates) > 0 { k.SetLastTotalPower(ctx, totalPower) } return updates } // Validator state transitions func (k Keeper) bondedToUnbonding(ctx sdk.Context, validator types.Validator) types.Validator { if !validator.IsBonded() { panic(fmt.Sprintf("bad state transition bondedToUnbonding, validator: %v\n", validator)) } return k.beginUnbondingValidator(ctx, validator) } func (k Keeper) unbondingToBonded(ctx sdk.Context, validator types.Validator) types.Validator { if !validator.IsUnbonding() { panic(fmt.Sprintf("bad state transition unbondingToBonded, validator: %v\n", validator)) } return k.bondValidator(ctx, validator) } func (k Keeper) unbondedToBonded(ctx sdk.Context, validator types.Validator) types.Validator { if !validator.IsUnbonded() { panic(fmt.Sprintf("bad state transition unbondedToBonded, validator: %v\n", validator)) } return k.bondValidator(ctx, validator) } // switches a validator from unbonding state to unbonded state func (k Keeper) UnbondingToUnbonded(ctx sdk.Context, validator types.Validator) types.Validator { if !validator.IsUnbonding() { panic(fmt.Sprintf("bad state transition unbondingToBonded, validator: %v\n", validator)) } return k.completeUnbondingValidator(ctx, validator) } // send a validator to jail func (k Keeper) jailValidator(ctx sdk.Context, validator types.Validator) { if validator.Jailed { panic(fmt.Sprintf("cannot jail already jailed validator, validator: %v\n", validator)) } validator.Jailed = true k.SetValidator(ctx, validator) k.DeleteValidatorByPowerIndex(ctx, validator) } // remove a validator from jail func (k Keeper) unjailValidator(ctx sdk.Context, validator types.Validator) { if !validator.Jailed { panic(fmt.Sprintf("cannot unjail already unjailed validator, validator: %v\n", validator)) } validator.Jailed = false k.SetValidator(ctx, validator) k.SetValidatorByPowerIndex(ctx, validator) } // perform all the store operations for when a validator status becomes bonded func (k Keeper) bondValidator(ctx sdk.Context, validator types.Validator) types.Validator { // delete the validator by power index, as the key will change k.DeleteValidatorByPowerIndex(ctx, validator) validator = validator.UpdateStatus(sdk.Bonded) // save the now bonded validator record to the two referenced stores k.SetValidator(ctx, validator) k.SetValidatorByPowerIndex(ctx, validator) // delete from queue if present k.DeleteValidatorQueue(ctx, validator) // trigger hook k.AfterValidatorBonded(ctx, validator.GetConsAddr(), validator.OperatorAddress) return validator } // perform all the store operations for when a validator begins unbonding func (k Keeper) beginUnbondingValidator(ctx sdk.Context, validator types.Validator) types.Validator { params := k.GetParams(ctx) // delete the validator by power index, as the key will change k.DeleteValidatorByPowerIndex(ctx, validator) // sanity check if validator.Status != sdk.Bonded { panic(fmt.Sprintf("should not already be unbonded or unbonding, validator: %v\n", validator)) } validator = validator.UpdateStatus(sdk.Unbonding) // set the unbonding completion time and completion height appropriately validator.UnbondingTime = ctx.BlockHeader().Time.Add(params.UnbondingTime) validator.UnbondingHeight = ctx.BlockHeader().Height // save the now unbonded validator record and power index k.SetValidator(ctx, validator) k.SetValidatorByPowerIndex(ctx, validator) // Adds to unbonding validator queue k.InsertValidatorQueue(ctx, validator) // trigger hook k.AfterValidatorBeginUnbonding(ctx, validator.GetConsAddr(), validator.OperatorAddress) return validator } // perform all the store operations for when a validator status becomes unbonded func (k Keeper) completeUnbondingValidator(ctx sdk.Context, validator types.Validator) types.Validator { validator = validator.UpdateStatus(sdk.Unbonded) k.SetValidator(ctx, validator) return validator } // map of operator addresses to serialized power type validatorsByAddr map[[sdk.AddrLen]byte][]byte // get the last validator set func (k Keeper) getLastValidatorsByAddr(ctx sdk.Context) validatorsByAddr { last := make(validatorsByAddr) iterator := k.LastValidatorsIterator(ctx) defer iterator.Close() for ; iterator.Valid(); iterator.Next() { var valAddr [sdk.AddrLen]byte // extract the validator address from the key (prefix is 1-byte) copy(valAddr[:], iterator.Key()[1:]) powerBytes := iterator.Value() last[valAddr] = make([]byte, len(powerBytes)) copy(last[valAddr], powerBytes) } return last } // given a map of remaining validators to previous bonded power // returns the list of validators to be unbonded, sorted by operator address func sortNoLongerBonded(last validatorsByAddr) [][]byte { // sort the map keys for determinism noLongerBonded := make([][]byte, len(last)) index := 0 for valAddrBytes := range last { valAddr := make([]byte, sdk.AddrLen) copy(valAddr, valAddrBytes[:]) noLongerBonded[index] = valAddr index++ } // sorted by address - order doesn't matter sort.SliceStable(noLongerBonded, func(i, j int) bool { // -1 means strictly less than return bytes.Compare(noLongerBonded[i], noLongerBonded[j]) == -1 }) return noLongerBonded }