Skip to content

Commit

Permalink
Start returning validators in EndBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Jun 26, 2024
1 parent b0d3ce0 commit 5f53dc3
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
4 changes: 3 additions & 1 deletion app/provider/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/testdata/testpb"
sigtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config"

no_valupdates_staking "github.com/cosmos/interchain-security/v5/x/ccv/no_valupdates_staking"
)

const (
Expand Down Expand Up @@ -552,7 +554,7 @@ func New(
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil, app.GetSubspace(minttypes.ModuleName)),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName), app.interfaceRegistry),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
no_valupdates_staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
upgrade.NewAppModule(&app.UpgradeKeeper, app.AccountKeeper.AddressCodec()),
evidence.NewAppModule(app.EvidenceKeeper),

Expand Down
14 changes: 14 additions & 0 deletions testutil/keeper/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions x/ccv/no_valupdates_staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ type AppModule struct {

// NewAppModule creates a new AppModule object using the native x/staking module
// AppModule constructor.
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, bk types.BankKeeper, subspace exported.Subspace) AppModule {
stakingAppMod := staking.NewAppModule(cdc, &keeper, ak, bk, subspace)
func NewAppModule(
cdc codec.Codec,
keeper *keeper.Keeper,
ak types.AccountKeeper,
bk types.BankKeeper,
ls exported.Subspace,
) AppModule {
stakingAppMod := staking.NewAppModule(cdc, keeper, ak, bk, ls)
return AppModule{
AppModule: stakingAppMod,
keeper: keeper,
keeper: *keeper,
accKeeper: ak,
bankKeeper: bk,
}
Expand Down
66 changes: 66 additions & 0 deletions x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"

abci "github.com/cometbft/cometbft/abci/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"

Expand All @@ -13,6 +14,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
providertypes "github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
ccv "github.com/cosmos/interchain-security/v5/x/ccv/types"
)
Expand Down Expand Up @@ -162,6 +164,70 @@ func (k Keeper) EndBlockVSU(ctx sdk.Context) {
}
}

func (k Keeper) ProviderValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
// get the bonded validators from the staking module
bondedValidators := k.stakingKeeper.GetBondedValidatorsByPower(ctx)

// get the last validator set sent to consensus
currentValidators := k.GetLastProviderConsensusValSet(ctx)

nextValidators := []types.ConsumerValidator{}
maxValidators := k.GetMaxProviderConsensusValidators(ctx)
// avoid out of range errors by bounding the max validators to the number of bonded validators
if maxValidators > int64(len(bondedValidators)) {
maxValidators = int64(len(bondedValidators))
}
for _, val := range bondedValidators[:maxValidators] {
// create the validator from the staking validator
consAddr, err := val.GetConsAddr()
if err != nil {
k.Logger(ctx).Error("getting consensus address",
"validator", val.GetOperator(),
"error", err)
continue
}

pubKey, err := val.TmConsPublicKey()
if err != nil {
k.Logger(ctx).Error("getting consensus public key",
"validator", val.GetOperator(),
"error", err)
continue
}

valAddr, err := sdk.ValAddressFromBech32(val.GetOperator())
if err != nil {
k.Logger(ctx).Error("creating validator address",
"validator", val.GetOperator(),
"error", err)
continue
}

power, err := k.stakingKeeper.GetLastValidatorPower(ctx, valAddr)
if err != nil {
k.Logger(ctx).Error("getting last validator power",
"validator", val.GetOperator(),
"error", err)
continue
}

nextValidator := types.ConsumerValidator{
ProviderConsAddr: consAddr,
ConsumerPublicKey: &pubKey,
Power: power,
}

nextValidators = append(nextValidators, nextValidator)
}

// store the validator set we will send to consensus
k.SetLastProviderConsensusValSet(ctx, nextValidators)

valUpdates := DiffValidators(currentValidators, nextValidators)

return valUpdates
}

// SendVSCPackets iterates over all registered consumers and sends pending
// VSC packets to the chains with established CCV channels.
// If the CCV channel is not established for a consumer chain,
Expand Down
4 changes: 1 addition & 3 deletions x/ccv/provider/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ func (am AppModule) EndBlock(ctx context.Context) error {
// EndBlock logic needed for the Consumer Chain Removal sub-protocol
am.keeper.EndBlockCCR(sdkCtx)
// EndBlock logic needed for the Validator Set Update sub-protocol
am.keeper.EndBlockVSU(sdkCtx)

return nil
return am.keeper.EndBlockVSU(sdkCtx)

Check failure on line 184 in x/ccv/provider/module.go

View workflow job for this annotation

GitHub Actions / test-integration

am.keeper.EndBlockVSU(sdkCtx) (no value) used as value

Check failure on line 184 in x/ccv/provider/module.go

View workflow job for this annotation

GitHub Actions / tests

am.keeper.EndBlockVSU(sdkCtx) (no value) used as value

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call

Check failure on line 184 in x/ccv/provider/module.go

View workflow job for this annotation

GitHub Actions / Analyze

am.keeper.EndBlockVSU(sdkCtx) (no value) used as value
}

// AppModuleSimulation functions
Expand Down
1 change: 1 addition & 0 deletions x/ccv/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type StakingKeeper interface {
GetUnbondingDelegationByUnbondingID(ctx context.Context, id uint64) (stakingtypes.UnbondingDelegation, error)
GetRedelegationByUnbondingID(ctx context.Context, id uint64) (stakingtypes.Redelegation, error)
GetValidatorByUnbondingID(ctx context.Context, id uint64) (stakingtypes.Validator, error)
GetBondedValidatorsByPower(ctx sdk.Context) (validators []stakingtypes.Validator)
}

// SlashingKeeper defines the contract expected to perform ccv slashing
Expand Down

0 comments on commit 5f53dc3

Please sign in to comment.