|
| 1 | +package keeper_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/cosmos/cosmos-sdk/codec" |
| 8 | + codectypes "github.com/cosmos/cosmos-sdk/codec/types" |
| 9 | + "github.com/cosmos/cosmos-sdk/store" |
| 10 | + storetypes "github.com/cosmos/cosmos-sdk/store/types" |
| 11 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 12 | + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" |
| 13 | + types2 "github.com/cosmos/ibc-go/v4/modules/light-clients/07-tendermint/types" |
| 14 | + providerkeeper "github.com/cosmos/interchain-security/x/ccv/provider/keeper" |
| 15 | + providertypes "github.com/cosmos/interchain-security/x/ccv/provider/types" |
| 16 | + ccvtypes "github.com/cosmos/interchain-security/x/ccv/types" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + "github.com/tendermint/tendermint/libs/log" |
| 19 | + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" |
| 20 | + tmdb "github.com/tendermint/tm-db" |
| 21 | +) |
| 22 | + |
| 23 | +func TestMigrateParamsv1p0To1p3(t *testing.T) { |
| 24 | + // Setup raw store |
| 25 | + db := tmdb.NewMemDB() |
| 26 | + stateStore := store.NewCommitMultiStore(db) |
| 27 | + storeKey := sdk.NewKVStoreKey(paramtypes.StoreKey) |
| 28 | + memStoreKey := storetypes.NewMemoryStoreKey("mem_key") |
| 29 | + stateStore.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db) |
| 30 | + stateStore.MountStoreWithDB(memStoreKey, sdk.StoreTypeMemory, nil) |
| 31 | + require.NoError(t, stateStore.LoadLatestVersion()) |
| 32 | + registry := codectypes.NewInterfaceRegistry() |
| 33 | + cdc := codec.NewProtoCodec(registry) |
| 34 | + ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) |
| 35 | + require.NoError(t, stateStore.LoadLatestVersion()) |
| 36 | + |
| 37 | + // Create new empty subspace |
| 38 | + subspace := paramtypes.NewSubspace(cdc, |
| 39 | + codec.NewLegacyAmino(), |
| 40 | + storeKey, |
| 41 | + memStoreKey, |
| 42 | + paramtypes.ModuleName, |
| 43 | + ) |
| 44 | + |
| 45 | + // Note that new param key table is set in keeper constructor |
| 46 | + subspace = subspace.WithKeyTable(v1p0p0KeyTable()) |
| 47 | + |
| 48 | + // Set 8 params from v1.0.0 |
| 49 | + subspace.Set(ctx, providertypes.KeyTemplateClient, providertypes.DefaultParams().TemplateClient) |
| 50 | + subspace.Set(ctx, providertypes.KeyTrustingPeriodFraction, "0.75") |
| 51 | + subspace.Set(ctx, ccvtypes.KeyCCVTimeoutPeriod, time.Hour) |
| 52 | + subspace.Set(ctx, providertypes.KeyInitTimeoutPeriod, time.Hour) |
| 53 | + subspace.Set(ctx, providertypes.KeyVscTimeoutPeriod, time.Hour) |
| 54 | + subspace.Set(ctx, providertypes.KeySlashMeterReplenishPeriod, time.Hour) |
| 55 | + subspace.Set(ctx, providertypes.KeySlashMeterReplenishFraction, "0.5") |
| 56 | + subspace.Set(ctx, providertypes.KeyMaxThrottledPackets, int64(10)) |
| 57 | + |
| 58 | + // Confirm new param cannot be set with old key table |
| 59 | + require.Panics(t, func() { |
| 60 | + subspace.Set(ctx, providertypes.KeyConsumerRewardDenomRegistrationFee, sdk.NewInt64Coin("uatom", 100)) |
| 61 | + }) |
| 62 | + |
| 63 | + // Now create new subspace, mocking an upgrade where app initialization happens again |
| 64 | + subspace = paramtypes.NewSubspace(cdc, |
| 65 | + codec.NewLegacyAmino(), |
| 66 | + storeKey, |
| 67 | + memStoreKey, |
| 68 | + paramtypes.ModuleName, |
| 69 | + ).WithKeyTable(providertypes.ParamKeyTable()) // Use new key table, this would be set in keeper constructor |
| 70 | + |
| 71 | + // Run migration |
| 72 | + providerkeeper.MigrateParamsv1p0To1p3(ctx, subspace, sdk.NewCoin("uatom", sdk.NewInt(100000))) |
| 73 | + |
| 74 | + // Use keeper to confirm params are set correctly |
| 75 | + keeper := providerkeeper.Keeper{} |
| 76 | + keeper.SetParamSpace(ctx, subspace) |
| 77 | + |
| 78 | + params := keeper.GetParams(ctx) |
| 79 | + require.Equal(t, providertypes.DefaultParams().TemplateClient, params.TemplateClient) |
| 80 | + require.Equal(t, "0.75", params.TrustingPeriodFraction) |
| 81 | + require.Equal(t, time.Hour, params.CcvTimeoutPeriod) |
| 82 | + require.Equal(t, time.Hour, params.InitTimeoutPeriod) |
| 83 | + require.Equal(t, time.Hour, params.VscTimeoutPeriod) |
| 84 | + require.Equal(t, time.Hour, params.SlashMeterReplenishPeriod) |
| 85 | + require.Equal(t, "0.5", params.SlashMeterReplenishFraction) |
| 86 | + require.Equal(t, int64(10), params.MaxThrottledPackets) |
| 87 | + // New param should be set |
| 88 | + require.Equal(t, sdk.NewCoin("uatom", sdk.NewInt(100000)), params.ConsumerRewardDenomRegistrationFee) |
| 89 | + |
| 90 | + // Set new params to other values |
| 91 | + params.ConsumerRewardDenomRegistrationFee = sdk.NewCoin("uatom", sdk.NewInt(1000000000)) |
| 92 | + keeper.SetParams(ctx, params) |
| 93 | + require.Equal(t, sdk.NewCoin("uatom", sdk.NewInt(1000000000)), keeper.GetParams(ctx).ConsumerRewardDenomRegistrationFee) |
| 94 | +} |
| 95 | + |
| 96 | +// |
| 97 | +// Note: the following methods and struct could be removed if v1.3.0 is actually defined as v2.0.0 |
| 98 | +// and we bump the go.mod package name accordingly |
| 99 | +// |
| 100 | + |
| 101 | +// v1p0p0Params is a copy of the ParamKeyTable method from v1.0.0 |
| 102 | +func v1p0p0KeyTable() paramtypes.KeyTable { |
| 103 | + return paramtypes.NewKeyTable().RegisterParamSet(&v1p0p0Params{}) |
| 104 | +} |
| 105 | + |
| 106 | +// ParamSetPairs implements params.ParamSet for v1p0p0Params |
| 107 | +func (p *v1p0p0Params) ParamSetPairs() paramtypes.ParamSetPairs { |
| 108 | + return paramtypes.ParamSetPairs{ |
| 109 | + paramtypes.NewParamSetPair(providertypes.KeyTemplateClient, p.TemplateClient, providertypes.ValidateTemplateClient), |
| 110 | + paramtypes.NewParamSetPair(providertypes.KeyTrustingPeriodFraction, p.TrustingPeriodFraction, ccvtypes.ValidateStringFraction), |
| 111 | + paramtypes.NewParamSetPair(ccvtypes.KeyCCVTimeoutPeriod, p.CcvTimeoutPeriod, ccvtypes.ValidateDuration), |
| 112 | + paramtypes.NewParamSetPair(providertypes.KeyInitTimeoutPeriod, p.InitTimeoutPeriod, ccvtypes.ValidateDuration), |
| 113 | + paramtypes.NewParamSetPair(providertypes.KeyVscTimeoutPeriod, p.VscTimeoutPeriod, ccvtypes.ValidateDuration), |
| 114 | + paramtypes.NewParamSetPair(providertypes.KeySlashMeterReplenishPeriod, p.SlashMeterReplenishPeriod, ccvtypes.ValidateDuration), |
| 115 | + paramtypes.NewParamSetPair(providertypes.KeySlashMeterReplenishFraction, p.SlashMeterReplenishFraction, ccvtypes.ValidateStringFraction), |
| 116 | + paramtypes.NewParamSetPair(providertypes.KeyMaxThrottledPackets, p.MaxThrottledPackets, ccvtypes.ValidatePositiveInt64), |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// v1p0p0Params is a copy of the Params struct from v1.0.0 |
| 121 | +type v1p0p0Params struct { |
| 122 | + TemplateClient *types2.ClientState `protobuf:"bytes,1,opt,name=template_client,json=templateClient,proto3" json:"template_client,omitempty"` |
| 123 | + TrustingPeriodFraction string `protobuf:"bytes,2,opt,name=trusting_period_fraction,json=trustingPeriodFraction,proto3" json:"trusting_period_fraction,omitempty"` |
| 124 | + CcvTimeoutPeriod time.Duration `protobuf:"bytes,3,opt,name=ccv_timeout_period,json=ccvTimeoutPeriod,proto3,stdduration" json:"ccv_timeout_period"` |
| 125 | + InitTimeoutPeriod time.Duration `protobuf:"bytes,4,opt,name=init_timeout_period,json=initTimeoutPeriod,proto3,stdduration" json:"init_timeout_period"` |
| 126 | + VscTimeoutPeriod time.Duration `protobuf:"bytes,5,opt,name=vsc_timeout_period,json=vscTimeoutPeriod,proto3,stdduration" json:"vsc_timeout_period"` |
| 127 | + SlashMeterReplenishPeriod time.Duration `protobuf:"bytes,6,opt,name=slash_meter_replenish_period,json=slashMeterReplenishPeriod,proto3,stdduration" json:"slash_meter_replenish_period"` |
| 128 | + SlashMeterReplenishFraction string `protobuf:"bytes,7,opt,name=slash_meter_replenish_fraction,json=slashMeterReplenishFraction,proto3" json:"slash_meter_replenish_fraction,omitempty"` |
| 129 | + MaxThrottledPackets int64 `protobuf:"varint,8,opt,name=max_throttled_packets,json=maxThrottledPackets,proto3" json:"max_throttled_packets,omitempty"` |
| 130 | +} |
0 commit comments