Skip to content

Commit

Permalink
feat!: add upgrade handler for initializing ICS epochs (backport #3079)…
Browse files Browse the repository at this point in the history
… (#3080)

* feat!: add upgrade handler for initializing ICS epochs (#3079)

* add upgrade handler for initializing ICS epochs

* adding changelog entries

* fix test

(cherry picked from commit 5c5447c)

# Conflicts:
#	.changelog/v16.0.0/dependencies/3079-init-ics-epochs.md
#	.changelog/v16.0.0/features/3079-init-ics-epochs.md

* fixing changelog

---------

Co-authored-by: Marius Poke <[email protected]>
  • Loading branch information
mergify[bot] and mpoke authored Apr 23, 2024
1 parent fb5269f commit 4378fb9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .changelog/v16.0.0/features/3079-init-ics-epochs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Initialize ICS epochs by adding a consumer validator set for every existing consumer chain.
([\#3079](https://github.com/cosmos/gaia/pull/3079))
2 changes: 2 additions & 0 deletions .changelog/v16.0.0/state-breaking/3079-init-ics-epochs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Initialize ICS epochs by adding a consumer validator set for every existing consumer chain.
([\#3079](https://github.com/cosmos/gaia/pull/3079))
2 changes: 1 addition & 1 deletion .changelog/v16.0.0/summary.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*17th April, 2024*
*23rd April, 2024*
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## v16.0.0

*17th April, 2024*
*23rd April, 2024*

### DEPENDENCIES

Expand Down Expand Up @@ -34,6 +34,8 @@
- Add rate limits to IBC transfer channels cf.
https://www.mintscan.io/cosmos/proposals/890.
([\#3042](https://github.com/cosmos/gaia/pull/3042))
- Initialize ICS epochs by adding a consumer validator set for every existing consumer chain.
([\#3079](https://github.com/cosmos/gaia/pull/3079))

### STATE BREAKING

Expand All @@ -53,6 +55,8 @@
- Bump [ICS](https://github.com/cosmos/interchain-security) to
[v4.1.0-lsm](https://github.com/cosmos/interchain-security/releases/tag/v4.1.0-lsm)
([\#3062](https://github.com/cosmos/gaia/pull/3062))
- Initialize ICS epochs by adding a consumer validator set for every existing consumer chain.
([\#3079](https://github.com/cosmos/gaia/pull/3079))

## Previous Versions

Expand Down
29 changes: 29 additions & 0 deletions app/upgrades/v16/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
ratelimittypes "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/types"

icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
providerkeeper "github.com/cosmos/interchain-security/v4/x/ccv/provider/keeper"
providertypes "github.com/cosmos/interchain-security/v4/x/ccv/provider/types"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/cosmos/gaia/v16/app/keepers"
Expand Down Expand Up @@ -87,6 +89,11 @@ func CreateUpgradeHandler(
} else if addErr != nil {
return vm, errorsmod.Wrapf(addErr, "unable to add rate limits")
}

if err := InitICSEpochs(ctx, keepers.ProviderKeeper, *keepers.StakingKeeper); err != nil {
return vm, errorsmod.Wrapf(err, "failed initializing ICS epochs")
}

ctx.Logger().Info("Upgrade complete")
return vm, err
}
Expand Down Expand Up @@ -155,3 +162,25 @@ func AddRateLimits(ctx sdk.Context, k ratelimitkeeper.Keeper) error {
ctx.Logger().Info("Finished adding rate limits")
return nil
}

func InitICSEpochs(ctx sdk.Context, pk providerkeeper.Keeper, sk stakingkeeper.Keeper) error {
ctx.Logger().Info("Initializing ICS epochs...")

// get the bonded validators from the staking module
bondedValidators := sk.GetLastValidators(ctx)

for _, chain := range pk.GetAllConsumerChains(ctx) {
chainID := chain.ChainId
valset := pk.GetConsumerValSet(ctx, chainID)
if len(valset) > 0 {
ctx.Logger().Info("consumer chain `%s` already has the valset initialized", chainID)
} else {
// init valset for consumer with chainID
nextValidators := pk.ComputeNextEpochConsumerValSet(ctx, chainID, bondedValidators)
pk.SetConsumerValSet(ctx, chainID, nextValidators)
}
}

ctx.Logger().Info("Finished initializing ICS epochs")
return nil
}
34 changes: 34 additions & 0 deletions app/upgrades/v16/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,37 @@ func TestAddRateLimits(t *testing.T) {
require.Equal(t, expectedRateLimit, rateLimit)
}
}

func TestInitICSEpochs(t *testing.T) {
gaiaApp := helpers.Setup(t)
ctx := gaiaApp.NewUncachedContext(true, tmproto.Header{})

providerKeeper := gaiaApp.ProviderKeeper
stakingKeeper := gaiaApp.StakingKeeper

// the setup has only one validator that is bonded
expBondedVals := stakingKeeper.GetAllValidators(ctx)
require.Equal(t, 1, len(expBondedVals))
expVal := expBondedVals[0]
expPower := expVal.ConsensusPower(stakingKeeper.PowerReduction(ctx))
expConsAddr, err := expVal.GetConsAddr()
require.NoError(t, err)
expConsumerPublicKey, err := expVal.TmConsPublicKey()
require.NoError(t, err)

providerKeeper.SetConsumerClientId(ctx, "chainID-0", "clientID-0")
providerKeeper.SetConsumerClientId(ctx, "chainID-1", "clientID-1")

err = v16.InitICSEpochs(ctx, providerKeeper, *stakingKeeper)
require.NoError(t, err)

for _, chain := range providerKeeper.GetAllConsumerChains(ctx) {
chainID := chain.ChainId
valset := providerKeeper.GetConsumerValSet(ctx, chainID)
require.Equal(t, 1, len(valset))
val := valset[0]
require.Equal(t, expPower, val.Power)
require.Equal(t, expConsAddr.Bytes(), val.ProviderConsAddr)
require.Equal(t, expConsumerPublicKey, *val.ConsumerPublicKey)
}
}

0 comments on commit 4378fb9

Please sign in to comment.