Skip to content

Commit

Permalink
circuit breaker params (#444)
Browse files Browse the repository at this point in the history
* changes

* Update params.go
  • Loading branch information
shaspitz authored Nov 15, 2022
1 parent df53566 commit b10e132
Show file tree
Hide file tree
Showing 10 changed files with 444 additions and 111 deletions.
12 changes: 12 additions & 0 deletions proto/interchain_security/ccv/provider/v1/provider.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ message Params {
// to timeout VSC packets even when a consumer chain is not live.
google.protobuf.Duration vsc_timeout_period = 5
[(gogoproto.nullable) = false, (gogoproto.stdduration) = true];

// The period for which the slash meter is replenished
google.protobuf.Duration slash_meter_replenish_period = 6
[(gogoproto.nullable) = false, (gogoproto.stdduration) = true];

// The fraction of total voting power that is replenished to the slash meter every replenish period.
// This param also serves as a maximum fraction of total voting power that the slash meter can hold.
string slash_meter_replenish_fraction = 7;

// The maximum amount of pending slash packets that can be queued for a consumer
// before the provider chain halts.
int64 max_pending_slash_packets = 8;
}

message HandshakeMetadata {
Expand Down
24 changes: 2 additions & 22 deletions x/ccv/consumer/types/params.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package types

import (
fmt "fmt"
time "time"

sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ccvtypes "github.com/cosmos/interchain-security/x/ccv/types"
Expand Down Expand Up @@ -110,7 +108,7 @@ func (p Params) Validate() error {
if err := ccvtypes.ValidateDuration(p.TransferTimeoutPeriod); err != nil {
return err
}
if err := validateConsumerRedistributionFraction(p.ConsumerRedistributionFraction); err != nil {
if err := ccvtypes.ValidateStringFraction(p.ConsumerRedistributionFraction); err != nil {
return err
}
if err := ccvtypes.ValidatePositiveInt64(p.HistoricalEntries); err != nil {
Expand All @@ -137,7 +135,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
paramtypes.NewParamSetPair(KeyTransferTimeoutPeriod,
p.TransferTimeoutPeriod, ccvtypes.ValidateDuration),
paramtypes.NewParamSetPair(KeyConsumerRedistributionFrac,
p.ConsumerRedistributionFraction, validateConsumerRedistributionFraction),
p.ConsumerRedistributionFraction, ccvtypes.ValidateStringFraction),
paramtypes.NewParamSetPair(KeyHistoricalEntries,
p.HistoricalEntries, ccvtypes.ValidatePositiveInt64),
paramtypes.NewParamSetPair(KeyConsumerUnbondingPeriod,
Expand All @@ -162,21 +160,3 @@ func validateProviderFeePoolAddrStr(i interface{}) error {
// Otherwise validate as usual for a bech32 address
return ccvtypes.ValidateBech32(i)
}

func validateConsumerRedistributionFraction(i interface{}) error {
str, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
dec, err := sdk.NewDecFromStr(str)
if err != nil {
return err
}
if dec.IsNegative() {
return fmt.Errorf("consumer redistribution fraction is negative")
}
if dec.Sub(sdk.NewDec(1)).IsPositive() {
return fmt.Errorf("consumer redistribution fraction cannot be above 1.0")
}
return nil
}
27 changes: 27 additions & 0 deletions x/ccv/provider/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ func (k Keeper) SetVscTimeoutPeriod(ctx sdk.Context, period time.Duration) {
k.paramSpace.Set(ctx, types.KeyVscTimeoutPeriod, period)
}

// GetSlashMeterReplenishPeriod returns the period for which the slash gas meter is replenished.
func (k Keeper) GetSlashMeterReplenishPeriod(ctx sdk.Context) time.Duration {
var p time.Duration
k.paramSpace.Get(ctx, types.KeySlashMeterReplenishPeriod, &p)
return p
}

// GetSlashMeterReplenishFraction returns the string fraction of total voting power that is replenished
// to the slash meter every replenish period. This param also serves as a maximum fraction of total
// voting power that the slash meter can hold.
func (k Keeper) GetSlashMeterReplenishFraction(ctx sdk.Context) string {
var f string
k.paramSpace.Get(ctx, types.KeySlashMeterReplenishFraction, &f)
return f
}

// GetMaxPendingSlashingPackets returns the maximum number of pending slash packets that can be queued for a consumer
// before the provider chain halts.
func (k Keeper) GetMaxPendingSlashingPackets(ctx sdk.Context) int64 {
var p int64
k.paramSpace.Get(ctx, types.KeyMaxPendingSlashPackets, &p)
return p
}

// GetParams returns the paramset for the provider module
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(
Expand All @@ -60,6 +84,9 @@ func (k Keeper) GetParams(ctx sdk.Context) types.Params {
k.GetCCVTimeoutPeriod(ctx),
k.GetInitTimeoutPeriod(ctx),
k.GetVscTimeoutPeriod(ctx),
k.GetSlashMeterReplenishPeriod(ctx),
k.GetSlashMeterReplenishFraction(ctx),
k.GetMaxPendingSlashingPackets(ctx),
)
}

Expand Down
29 changes: 23 additions & 6 deletions x/ccv/provider/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types"
ibctmtypes "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types"
testkeeper "github.com/cosmos/interchain-security/testutil/keeper"
"github.com/cosmos/interchain-security/x/ccv/provider/types"
ccvtypes "github.com/cosmos/interchain-security/x/ccv/types"
providertypes "github.com/cosmos/interchain-security/x/ccv/provider/types"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,14 +20,32 @@ func TestParams(t *testing.T) {
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, keeperParams)
defer ctrl.Finish()

defaultParams := types.DefaultParams()
defaultParams := providertypes.DefaultParams()
providerKeeper.SetParams(ctx, defaultParams)
params := providerKeeper.GetParams(ctx)
require.Equal(t, defaultParams, params)

newParams := types.NewParams(ibctmtypes.NewClientState("", ibctmtypes.DefaultTrustLevel, 0, 0,
time.Second*40, clienttypes.Height{}, commitmenttypes.GetSDKSpecs(), []string{"ibc", "upgradedIBCState"}, true, false),
types.DefaultTrustingPeriodFraction, ccvtypes.DefaultCCVTimeoutPeriod, types.DefaultInitTimeoutPeriod, types.DefaultVscTimeoutPeriod)
newParams := providertypes.NewParams(
ibctmtypes.NewClientState(
"",
ibctmtypes.DefaultTrustLevel,
0,
0,
time.Second*40,
clienttypes.Height{},
commitmenttypes.GetSDKSpecs(),
[]string{"ibc", "upgradedIBCState"},
true,
false,
),
4,
7*24*time.Hour,
5*time.Hour,
10*time.Minute,
time.Hour,
"0.4",
100,
)
providerKeeper.SetParams(ctx, newParams)
params = providerKeeper.GetParams(ctx)
require.Equal(t, newParams, params)
Expand Down
11 changes: 7 additions & 4 deletions x/ccv/provider/keeper/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,13 @@ func TestMakeConsumerGenesis(t *testing.T) {
},
// Note these are unused provider parameters for this test, and not actually asserted against
// They must be populated with reasonable values to satisfy SetParams though.
TrustingPeriodFraction: providertypes.DefaultTrustingPeriodFraction,
CcvTimeoutPeriod: ccvtypes.DefaultCCVTimeoutPeriod,
InitTimeoutPeriod: types.DefaultInitTimeoutPeriod,
VscTimeoutPeriod: types.DefaultVscTimeoutPeriod,
TrustingPeriodFraction: providertypes.DefaultTrustingPeriodFraction,
CcvTimeoutPeriod: ccvtypes.DefaultCCVTimeoutPeriod,
InitTimeoutPeriod: types.DefaultInitTimeoutPeriod,
VscTimeoutPeriod: types.DefaultVscTimeoutPeriod,
SlashMeterReplenishPeriod: types.DefaultSlashMeterReplenishPeriod,
SlashMeterReplenishFraction: types.DefaultSlashMeterReplenishFraction,
MaxPendingSlashPackets: types.DefaultMaxPendingSlashPackets,
}
providerKeeper.SetParams(ctx, moduleParams)
defer ctrl.Finish()
Expand Down
96 changes: 90 additions & 6 deletions x/ccv/provider/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestValidateGenesisState(t *testing.T) {
nil,
types.NewParams(ibctmtypes.NewClientState("", ibctmtypes.DefaultTrustLevel, 0, 0,
time.Second*40, clienttypes.Height{}, commitmenttypes.GetSDKSpecs(), []string{"ibc", "upgradedIBCState"}, true, false),
3, time.Hour, time.Hour, time.Hour),
3, time.Hour, time.Hour, 30*time.Minute, time.Hour, "0.1", 400),
),
true,
},
Expand All @@ -95,7 +95,13 @@ func TestValidateGenesisState(t *testing.T) {
nil,
types.NewParams(ibctmtypes.NewClientState("", ibctmtypes.DefaultTrustLevel, 0, 0,
0, clienttypes.Height{}, nil, []string{"ibc", "upgradedIBCState"}, true, false),
types.DefaultTrustingPeriodFraction, ccv.DefaultCCVTimeoutPeriod, types.DefaultInitTimeoutPeriod, types.DefaultVscTimeoutPeriod),
types.DefaultTrustingPeriodFraction,
ccv.DefaultCCVTimeoutPeriod,
types.DefaultInitTimeoutPeriod,
types.DefaultVscTimeoutPeriod,
types.DefaultSlashMeterReplenishPeriod,
types.DefaultSlashMeterReplenishFraction,
types.DefaultMaxPendingSlashPackets),
),
false,
},
Expand All @@ -114,7 +120,10 @@ func TestValidateGenesisState(t *testing.T) {
0, // 0 trusting period fraction here
ccv.DefaultCCVTimeoutPeriod,
types.DefaultInitTimeoutPeriod,
types.DefaultVscTimeoutPeriod),
types.DefaultVscTimeoutPeriod,
types.DefaultSlashMeterReplenishPeriod,
types.DefaultSlashMeterReplenishFraction,
types.DefaultMaxPendingSlashPackets),
),
false,
},
Expand All @@ -133,7 +142,10 @@ func TestValidateGenesisState(t *testing.T) {
types.DefaultTrustingPeriodFraction,
0, // 0 ccv timeout here
types.DefaultInitTimeoutPeriod,
types.DefaultVscTimeoutPeriod),
types.DefaultVscTimeoutPeriod,
types.DefaultSlashMeterReplenishPeriod,
types.DefaultSlashMeterReplenishFraction,
types.DefaultMaxPendingSlashPackets),
),
false,
},
Expand All @@ -152,7 +164,10 @@ func TestValidateGenesisState(t *testing.T) {
types.DefaultTrustingPeriodFraction,
ccv.DefaultCCVTimeoutPeriod,
0, // 0 init timeout here
types.DefaultVscTimeoutPeriod),
types.DefaultVscTimeoutPeriod,
types.DefaultSlashMeterReplenishPeriod,
types.DefaultSlashMeterReplenishFraction,
types.DefaultMaxPendingSlashPackets),
),
false,
},
Expand All @@ -171,7 +186,76 @@ func TestValidateGenesisState(t *testing.T) {
types.DefaultTrustingPeriodFraction,
ccv.DefaultCCVTimeoutPeriod,
types.DefaultInitTimeoutPeriod,
0), // 0 vsc timeout here
0, // 0 vsc timeout here
types.DefaultSlashMeterReplenishPeriod,
types.DefaultSlashMeterReplenishFraction,
types.DefaultMaxPendingSlashPackets),
),
false,
},
{
"invalid params, zero slash meter replenish period",
types.NewGenesisState(
0,
nil,
[]types.ConsumerState{{ChainId: "chainid-1", ChannelId: "channelid"}},
nil,
nil,
nil,
nil,
types.NewParams(ibctmtypes.NewClientState("", ibctmtypes.DefaultTrustLevel, 0, 0,
time.Second*40, clienttypes.Height{}, commitmenttypes.GetSDKSpecs(), []string{"ibc", "upgradedIBCState"}, true, false),
types.DefaultTrustingPeriodFraction,
ccv.DefaultCCVTimeoutPeriod,
types.DefaultInitTimeoutPeriod,
types.DefaultVscTimeoutPeriod,
0, // 0 slash meter replenish period here
types.DefaultSlashMeterReplenishFraction,
types.DefaultMaxPendingSlashPackets),
),
false,
},
{
"invalid params, invalid slash meter replenish fraction",
types.NewGenesisState(
0,
nil,
[]types.ConsumerState{{ChainId: "chainid-1", ChannelId: "channelid"}},
nil,
nil,
nil,
nil,
types.NewParams(ibctmtypes.NewClientState("", ibctmtypes.DefaultTrustLevel, 0, 0,
time.Second*40, clienttypes.Height{}, commitmenttypes.GetSDKSpecs(), []string{"ibc", "upgradedIBCState"}, true, false),
types.DefaultTrustingPeriodFraction,
ccv.DefaultCCVTimeoutPeriod,
types.DefaultInitTimeoutPeriod,
types.DefaultVscTimeoutPeriod,
types.DefaultSlashMeterReplenishPeriod,
"1.15",
types.DefaultMaxPendingSlashPackets),
),
false,
},
{
"invalid params, invalid max pending slash packets",
types.NewGenesisState(
0,
nil,
[]types.ConsumerState{{ChainId: "chainid-1", ChannelId: "channelid"}},
nil,
nil,
nil,
nil,
types.NewParams(ibctmtypes.NewClientState("", ibctmtypes.DefaultTrustLevel, 0, 0,
time.Second*40, clienttypes.Height{}, commitmenttypes.GetSDKSpecs(), []string{"ibc", "upgradedIBCState"}, true, false),
types.DefaultTrustingPeriodFraction,
ccv.DefaultCCVTimeoutPeriod,
types.DefaultInitTimeoutPeriod,
types.DefaultVscTimeoutPeriod,
types.DefaultSlashMeterReplenishPeriod,
"1.15",
-1),
),
false,
},
Expand Down
Loading

0 comments on commit b10e132

Please sign in to comment.