Skip to content

Commit

Permalink
added upgrade handler for ics infraction params
Browse files Browse the repository at this point in the history
  • Loading branch information
stana-miric committed Nov 28, 2024
1 parent 6159a47 commit a38bd3d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
15 changes: 15 additions & 0 deletions app/upgrades/v22/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v22

import (
"github.com/cosmos/gaia/v21/app/upgrades"
)

const (
// UpgradeName defines the on-chain upgrade name.
UpgradeName = "v22"
)

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
}
65 changes: 65 additions & 0 deletions app/upgrades/v22/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package v22

import (
"context"
"time"

"cosmossdk.io/math"
upgradetypes "cosmossdk.io/x/upgrade/types"
providerkeeper "github.com/cosmos/interchain-security/v6/x/ccv/provider/keeper"
providertypes "github.com/cosmos/interchain-security/v6/x/ccv/provider/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

"github.com/cosmos/gaia/v21/app/keepers"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
keepers *keepers.AppKeepers,
) upgradetypes.UpgradeHandler {
return func(c context.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx.Logger().Info("Starting module migrations...")

vm, err := mm.RunMigrations(ctx, configurator, vm)
if err != nil {
return vm, err
}

if err := SetConsumerInfractionParams(ctx, keepers.ProviderKeeper); err != nil {
return vm, err
}

ctx.Logger().Info("Upgrade v22 complete")
return vm, nil
}
}

func SetConsumerInfractionParams(ctx sdk.Context, pk providerkeeper.Keeper) error {
infractionParameters := DefaultInfractionParams()

activeConsumerIds := pk.GetAllActiveConsumerIds(ctx)

Check failure on line 44 in app/upgrades/v22/upgrades.go

View workflow job for this annotation

GitHub Actions / Analyze

var-naming: var activeConsumerIds should be activeConsumerIDs (revive)

Check failure on line 44 in app/upgrades/v22/upgrades.go

View workflow job for this annotation

GitHub Actions / golangci-lint

var-naming: var activeConsumerIds should be activeConsumerIDs (revive)
for _, consumerId := range activeConsumerIds {

Check failure on line 45 in app/upgrades/v22/upgrades.go

View workflow job for this annotation

GitHub Actions / Analyze

var-naming: range var consumerId should be consumerID (revive)

Check failure on line 45 in app/upgrades/v22/upgrades.go

View workflow job for this annotation

GitHub Actions / golangci-lint

var-naming: range var consumerId should be consumerID (revive)
if err := pk.SetInfractionParameters(ctx, consumerId, infractionParameters); err != nil {
return err
}
}

return nil
}

func DefaultInfractionParams() providertypes.InfractionParameters {
return providertypes.InfractionParameters{
DoubleSign: &providertypes.SlashJailParameters{
JailDuration: time.Duration(1<<63 - 1), // the largest value a time.Duration can hold 9223372036854775807 (approximately 292 years)
SlashFraction: math.LegacyNewDecWithPrec(5, 2), // 0.05
},
Downtime: &providertypes.SlashJailParameters{
JailDuration: 600 * time.Second,
SlashFraction: math.LegacyNewDec(0), // no slashing for downtime on the consumer
},
}
}
47 changes: 47 additions & 0 deletions app/upgrades/v22/upgrades_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package v22_test

import (
"testing"

"github.com/stretchr/testify/require"

v22 "github.com/cosmos/gaia/v21/app/upgrades/v22"
testutil "github.com/cosmos/interchain-security/v6/testutil/keeper"
providertypes "github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
)

func TestSetDefaultConsumerInfractionParams(t *testing.T) {
t.Helper()
inMemParams := testutil.NewInMemKeeperParams(t)
pk, ctx, ctrl, _ := testutil.GetProviderKeeperAndCtx(t, inMemParams)
defer ctrl.Finish()

// Add consumer chains
initConsumerID := pk.FetchAndIncrementConsumerId(ctx)
pk.SetConsumerChainId(ctx, initConsumerID, "init-1")
pk.SetConsumerPhase(ctx, initConsumerID, providertypes.CONSUMER_PHASE_INITIALIZED)
launchedConsumerID := pk.FetchAndIncrementConsumerId(ctx)
pk.SetConsumerChainId(ctx, launchedConsumerID, "launched-1")
pk.SetConsumerPhase(ctx, launchedConsumerID, providertypes.CONSUMER_PHASE_LAUNCHED)
stoppedConsumerID := pk.FetchAndIncrementConsumerId(ctx)
pk.SetConsumerChainId(ctx, stoppedConsumerID, "stopped-1")
pk.SetConsumerPhase(ctx, stoppedConsumerID, providertypes.CONSUMER_PHASE_STOPPED)

activeConsumerIds := pk.GetAllActiveConsumerIds(ctx)

Check failure on line 30 in app/upgrades/v22/upgrades_test.go

View workflow job for this annotation

GitHub Actions / Analyze

var-naming: var activeConsumerIds should be activeConsumerIDs (revive)

Check failure on line 30 in app/upgrades/v22/upgrades_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

var-naming: var activeConsumerIds should be activeConsumerIDs (revive)
require.Equal(t, 2, len(activeConsumerIds))

for _, consumerId := range activeConsumerIds {

Check failure on line 33 in app/upgrades/v22/upgrades_test.go

View workflow job for this annotation

GitHub Actions / Analyze

var-naming: range var consumerId should be consumerID (revive)

Check failure on line 33 in app/upgrades/v22/upgrades_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

var-naming: range var consumerId should be consumerID (revive)
_, err := pk.GetInfractionParameters(ctx, consumerId)
require.Error(t, err)
}

err := v22.SetConsumerInfractionParams(ctx, pk)
require.NoError(t, err)

defaultInfractionParams := v22.DefaultInfractionParams()
for _, consumerId := range activeConsumerIds {

Check failure on line 42 in app/upgrades/v22/upgrades_test.go

View workflow job for this annotation

GitHub Actions / Analyze

var-naming: range var consumerId should be consumerID (revive)

Check failure on line 42 in app/upgrades/v22/upgrades_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

var-naming: range var consumerId should be consumerID (revive)
infractionParams, err := pk.GetInfractionParameters(ctx, consumerId)
require.NoError(t, err)
require.Equal(t, defaultInfractionParams, infractionParams)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ replace (
// Use special SDK v0.50.x release with support for both ICS and LSM
github.com/cosmos/cosmos-sdk => github.com/cosmos/cosmos-sdk v0.50.10-lsm

// todo:remove this after ics version is updated
github.com/cosmos/interchain-security/v6 => github.com/cosmos/interchain-security/v6 v6.0.0-20241128093443-3f7df3506d1a

// TODO: remove it: https://github.com/cosmos/cosmos-sdk/issues/13134
github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ github.com/cosmos/ibc-go/v8 v8.5.2 h1:27s9oeD2AxLQF3e9BQsYt9doONyZ7FwZi/qkBv6Sdk
github.com/cosmos/ibc-go/v8 v8.5.2/go.mod h1:P5hkAvq0Qbg0h18uLxDVA9q1kOJ0l36htMsskiNwXbo=
github.com/cosmos/ics23/go v0.11.0 h1:jk5skjT0TqX5e5QJbEnwXIS2yI2vnmLOgpQPeM5RtnU=
github.com/cosmos/ics23/go v0.11.0/go.mod h1:A8OjxPE67hHST4Icw94hOxxFEJMBG031xIGF/JHNIY0=
github.com/cosmos/interchain-security/v6 v6.3.0 h1:AIsfxLUDtUGVfaqJ1WPwnYIOT5AxoSO58469iw9vNH4=
github.com/cosmos/interchain-security/v6 v6.3.0/go.mod h1:6DSiV2w+DuPkxP1KGFtaxpiwf8Xt2iusj8O53KCx96Q=
github.com/cosmos/interchain-security/v6 v6.0.0-20241128093443-3f7df3506d1a h1:JQfqLMR0XBF40kRkMzqISVDH86frYyZ/X6xAzRwvDbw=
github.com/cosmos/interchain-security/v6 v6.0.0-20241128093443-3f7df3506d1a/go.mod h1:6DSiV2w+DuPkxP1KGFtaxpiwf8Xt2iusj8O53KCx96Q=
github.com/cosmos/keyring v1.2.0 h1:8C1lBP9xhImmIabyXW4c3vFjjLiBdGCmfLUfeZlV1Yo=
github.com/cosmos/keyring v1.2.0/go.mod h1:fc+wB5KTk9wQ9sDx0kFXB3A0MaeGHM9AwRStKOQ5vOA=
github.com/cosmos/ledger-cosmos-go v0.13.3 h1:7ehuBGuyIytsXbd4MP43mLeoN2LTOEnk5nvue4rK+yM=
Expand Down

0 comments on commit a38bd3d

Please sign in to comment.