Skip to content

Commit

Permalink
Fix bonded/active validator distinction
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Jul 19, 2024
1 parent 5422f92 commit e506aa9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 37 deletions.
24 changes: 2 additions & 22 deletions tests/e2e/steps_inactive_vals.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,32 +606,12 @@ func stepsInactiveValsWithTopN() []Step {
State: State{
ChainID("consu"): ChainState{
ValPowers: &map[ValidatorID]uint{
ValidatorID("alice"): 100,
ValidatorID("bob"): 200,
ValidatorID("alice"): 0, // alice and bob are not in the top N, so aren't in the validator set
ValidatorID("bob"): 0,
ValidatorID("carol"): 300,
},
},
},
},
{
Action: AddIbcConnectionAction{
ChainA: ChainID("consu"),
ChainB: ChainID("provi"),
ClientA: 0,
ClientB: 0,
},
State: State{},
},
{
Action: AddIbcChannelAction{
ChainA: ChainID("consu"),
ChainB: ChainID("provi"),
ConnectionA: 0,
PortA: "consumer",
PortB: "provider",
Order: "ordered",
},
State: State{},
},
}
}
6 changes: 5 additions & 1 deletion x/ccv/consumer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,5 +716,9 @@ func (k Keeper) IsPrevStandaloneChain(ctx sdk.Context) bool {
// GetLastBondedValidators iterates the last validator powers in the staking module
// and returns the first MaxValidators many validators with the largest powers.
func (k Keeper) GetLastBondedValidators(ctx sdk.Context) ([]stakingtypes.Validator, error) {
return ccv.GetLastBondedValidatorsUtil(ctx, k.standaloneStakingKeeper, k.Logger(ctx))
maxVals, err := k.standaloneStakingKeeper.MaxValidators(ctx)
if err != nil {
return nil, err
}
return ccv.GetLastBondedValidatorsUtil(ctx, k.standaloneStakingKeeper, k.Logger(ctx), maxVals)
}
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/legacy_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ func (k Keeper) HandleLegacyConsumerModificationProposal(ctx sdk.Context, p *typ
if p.Top_N != oldTopN {
if p.Top_N > 0 {
// if the chain receives a non-zero top N value, store the minimum power in the top N
bondedValidators, err := k.GetLastBondedValidators(ctx)
activeValidators, err := k.GetLastActiveBondedValidators(ctx)
if err != nil {
return err
}
minPower, err := k.ComputeMinPowerInTopN(ctx, bondedValidators, p.Top_N)
minPower, err := k.ComputeMinPowerInTopN(ctx, activeValidators, p.Top_N)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions x/ccv/provider/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,20 @@ func (k Keeper) MakeConsumerGenesis(
}

if prop.Top_N > 0 {
// get the consensus active validators
activeValidators, err := k.GetLastActiveBondedValidators(ctx)
if err != nil {
return gen, nil, errorsmod.Wrapf(stakingtypes.ErrNoValidatorFound, "error getting last active bonded validators: %s", err)
}
// in a Top-N chain, we automatically opt in all validators that belong to the top N
minPower, err := k.ComputeMinPowerInTopN(ctx, bondedValidators, prop.Top_N)
minPower, err := k.ComputeMinPowerInTopN(ctx, activeValidators, prop.Top_N)
if err != nil {
return gen, nil, err
}
k.OptInTopNValidators(ctx, chainID, bondedValidators, minPower)
k.OptInTopNValidators(ctx, chainID, activeValidators, minPower)
k.SetMinimumPowerInTopN(ctx, chainID, minPower)
}
// need to use the bondedValidators, not activeValidators, here since the chain might be opt-in and allow inactive vals
nextValidators := k.ComputeNextValidators(ctx, chainID, bondedValidators)

k.SetConsumerValSet(ctx, chainID, nextValidators)
Expand Down
10 changes: 8 additions & 2 deletions x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,18 @@ func (k Keeper) QueueVSCPackets(ctx sdk.Context) {

if topN > 0 {
// in a Top-N chain, we automatically opt in all validators that belong to the top N
minPower, err := k.ComputeMinPowerInTopN(ctx, bondedValidators, topN)
// of the active validators
activeValidators, err := k.GetLastActiveBondedValidators(ctx)
if err != nil {
// we just log here and do not panic because panic-ing would halt the provider chain
k.Logger(ctx).Error("failed to get active validators", "error", err)
}
minPower, err := k.ComputeMinPowerInTopN(ctx, activeValidators, topN)
if err == nil {
// set the minimal power of validators in the top N in the store
k.SetMinimumPowerInTopN(ctx, chainID, minPower)

k.OptInTopNValidators(ctx, chainID, bondedValidators, minPower)
k.OptInTopNValidators(ctx, chainID, activeValidators, minPower)
} else {
// we just log here and do not panic because panic-ing would halt the provider chain
k.Logger(ctx).Error("failed to compute min power to opt in for chain", "chain", chainID, "error", err)
Expand Down
11 changes: 10 additions & 1 deletion x/ccv/provider/keeper/validator_set_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,14 @@ func (k Keeper) FilterValidators(
// GetLastBondedValidators iterates the last validator powers in the staking module
// and returns the first MaxValidators many validators with the largest powers.
func (k Keeper) GetLastBondedValidators(ctx sdk.Context) ([]stakingtypes.Validator, error) {
return ccv.GetLastBondedValidatorsUtil(ctx, k.stakingKeeper, k.Logger(ctx))
maxVals, err := k.stakingKeeper.MaxValidators(ctx)
if err != nil {
return nil, err
}
return ccv.GetLastBondedValidatorsUtil(ctx, k.stakingKeeper, k.Logger(ctx), maxVals)
}

func (k Keeper) GetLastActiveBondedValidators(ctx sdk.Context) ([]stakingtypes.Validator, error) {
maxVals := k.GetMaxProviderConsensusValidators(ctx)
return ccv.GetLastBondedValidatorsUtil(ctx, k.stakingKeeper, k.Logger(ctx), uint32(maxVals))
}
11 changes: 4 additions & 7 deletions x/ccv/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,13 @@ func GetConsAddrFromBech32(bech32str string) (sdk.ConsAddress, error) {
return sdk.ConsAddress(addr), nil
}

func GetLastBondedValidatorsUtil(ctx sdk.Context, stakingKeeper StakingKeeper, logger log.Logger) ([]stakingtypes.Validator, error) {
maxVals, err := stakingKeeper.MaxValidators(ctx)
if err != nil {
return nil, err
}

// GetLastBondedValidatorsUtil iterates the last validator powers in the staking module
// and returns the first MaxValidators many validators with the largest powers.
func GetLastBondedValidatorsUtil(ctx sdk.Context, stakingKeeper StakingKeeper, logger log.Logger, maxVals uint32) ([]stakingtypes.Validator, error) {
lastPowers := make([]stakingtypes.LastValidatorPower, maxVals)

i := 0
err = stakingKeeper.IterateLastValidatorPowers(ctx, func(addr sdk.ValAddress, power int64) (stop bool) {
err := stakingKeeper.IterateLastValidatorPowers(ctx, func(addr sdk.ValAddress, power int64) (stop bool) {
lastPowers[i] = stakingtypes.LastValidatorPower{Address: addr.String(), Power: power}
i++
return i >= int(maxVals) // stop iteration if true
Expand Down

0 comments on commit e506aa9

Please sign in to comment.