Skip to content

Commit

Permalink
switched to open sourced rate limit module (#1123)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs authored Mar 6, 2024
1 parent c483e50 commit 48239b6
Show file tree
Hide file tree
Showing 84 changed files with 278 additions and 11,494 deletions.
40 changes: 17 additions & 23 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/Stride-Labs/ibc-rate-limiting/ratelimit"
ratelimitkeeper "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/keeper"
ratelimittypes "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/types"
dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
tmjson "github.com/cometbft/cometbft/libs/json"
Expand Down Expand Up @@ -139,10 +142,6 @@ import (
"github.com/Stride-Labs/stride/v18/x/mint"
mintkeeper "github.com/Stride-Labs/stride/v18/x/mint/keeper"
minttypes "github.com/Stride-Labs/stride/v18/x/mint/types"
ratelimitmodule "github.com/Stride-Labs/stride/v18/x/ratelimit"
ratelimitclient "github.com/Stride-Labs/stride/v18/x/ratelimit/client"
ratelimitmodulekeeper "github.com/Stride-Labs/stride/v18/x/ratelimit/keeper"
ratelimitmoduletypes "github.com/Stride-Labs/stride/v18/x/ratelimit/types"
recordsmodule "github.com/Stride-Labs/stride/v18/x/records"
recordsmodulekeeper "github.com/Stride-Labs/stride/v18/x/records/keeper"
recordsmoduletypes "github.com/Stride-Labs/stride/v18/x/records/types"
Expand Down Expand Up @@ -171,10 +170,6 @@ func getGovProposalHandlers() []govclient.ProposalHandler {
ibcclientclient.UpgradeProposalHandler,
stakeibcclient.AddValidatorsProposalHandler,
stakeibcclient.ToggleLSMProposalHandler,
ratelimitclient.AddRateLimitProposalHandler,
ratelimitclient.UpdateRateLimitProposalHandler,
ratelimitclient.RemoveRateLimitProposalHandler,
ratelimitclient.ResetRateLimitProposalHandler,
evmosvestingclient.RegisterClawbackProposalHandler,
)

Expand Down Expand Up @@ -213,7 +208,7 @@ var (
interchainquery.AppModuleBasic{},
ica.AppModuleBasic{},
recordsmodule.AppModuleBasic{},
ratelimitmodule.AppModuleBasic{},
ratelimit.AppModuleBasic{},
icacallbacksmodule.AppModuleBasic{},
claim.AppModuleBasic{},
ccvconsumer.AppModuleBasic{},
Expand Down Expand Up @@ -322,7 +317,7 @@ type StrideApp struct {
RecordsKeeper recordsmodulekeeper.Keeper
IcacallbacksKeeper icacallbacksmodulekeeper.Keeper
ScopedratelimitKeeper capabilitykeeper.ScopedKeeper
RatelimitKeeper ratelimitmodulekeeper.Keeper
RatelimitKeeper ratelimitkeeper.Keeper
ClaimKeeper claimkeeper.Keeper
ICAOracleKeeper icaoraclekeeper.Keeper
StaketiaKeeper staketiakeeper.Keeper
Expand Down Expand Up @@ -367,7 +362,7 @@ func NewStrideApp(
interchainquerytypes.StoreKey,
icacontrollertypes.StoreKey, icahosttypes.StoreKey,
recordsmoduletypes.StoreKey,
ratelimitmoduletypes.StoreKey,
ratelimittypes.StoreKey,
icacallbacksmoduletypes.StoreKey,
claimtypes.StoreKey,
icaoracletypes.StoreKey,
Expand Down Expand Up @@ -473,17 +468,18 @@ func NewStrideApp(
)

// Create Ratelimit Keeper
scopedratelimitKeeper := app.CapabilityKeeper.ScopeToModule(ratelimitmoduletypes.ModuleName)
scopedratelimitKeeper := app.CapabilityKeeper.ScopeToModule(ratelimittypes.ModuleName)
app.ScopedratelimitKeeper = scopedratelimitKeeper
app.RatelimitKeeper = *ratelimitmodulekeeper.NewKeeper(
app.RatelimitKeeper = *ratelimitkeeper.NewKeeper(
appCodec,
keys[ratelimitmoduletypes.StoreKey],
app.GetSubspace(ratelimitmoduletypes.ModuleName),
keys[ratelimittypes.StoreKey],
app.GetSubspace(ratelimittypes.ModuleName),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
app.BankKeeper,
app.IBCKeeper.ChannelKeeper,
app.IBCKeeper.ChannelKeeper, // ICS4Wrapper
)
ratelimitModule := ratelimitmodule.NewAppModule(appCodec, app.RatelimitKeeper)
ratelimitModule := ratelimit.NewAppModule(appCodec, app.RatelimitKeeper)

// Initialize the packet forward middleware Keeper
// It's important to note that the PFM Keeper must be initialized before the Transfer Keeper
Expand Down Expand Up @@ -684,7 +680,6 @@ func NewStrideApp(
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)).
AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper)).
AddRoute(stakeibcmoduletypes.RouterKey, stakeibcmodule.NewStakeibcProposalHandler(app.StakeibcKeeper)).
AddRoute(ratelimitmoduletypes.RouterKey, ratelimitmodule.NewRateLimitProposalHandler(app.RatelimitKeeper, app.IBCKeeper.ChannelKeeper)).
AddRoute(evmosvestingtypes.RouterKey, evmosvesting.NewVestingProposalHandler(&app.VestingKeeper))

govKeeper := govkeeper.NewKeeper(
Expand All @@ -705,7 +700,6 @@ func NewStrideApp(
app.StakeibcKeeper.Hooks(),
app.MintKeeper.Hooks(),
app.ClaimKeeper.Hooks(),
app.RatelimitKeeper.Hooks(),
app.StaketiaKeeper.Hooks(),
),
)
Expand Down Expand Up @@ -778,7 +772,7 @@ func NewStrideApp(
packetforwardkeeper.DefaultForwardTransferPacketTimeoutTimestamp, // forward timeout
packetforwardkeeper.DefaultRefundTransferPacketTimeoutTimestamp, // refund timeout
)
transferStack = ratelimitmodule.NewIBCMiddleware(app.RatelimitKeeper, transferStack)
transferStack = ratelimit.NewIBCMiddleware(app.RatelimitKeeper, transferStack)
transferStack = staketia.NewIBCMiddleware(app.StaketiaKeeper, transferStack)
transferStack = recordsmodule.NewIBCModule(app.RecordsKeeper, transferStack)
transferStack = autopilot.NewIBCModule(app.AutopilotKeeper, transferStack)
Expand Down Expand Up @@ -877,7 +871,7 @@ func NewStrideApp(
epochsmoduletypes.ModuleName,
interchainquerytypes.ModuleName,
recordsmoduletypes.ModuleName,
ratelimitmoduletypes.ModuleName,
ratelimittypes.ModuleName,
icacallbacksmoduletypes.ModuleName,
claimtypes.ModuleName,
ccvconsumertypes.ModuleName,
Expand Down Expand Up @@ -914,7 +908,7 @@ func NewStrideApp(
epochsmoduletypes.ModuleName,
interchainquerytypes.ModuleName,
recordsmoduletypes.ModuleName,
ratelimitmoduletypes.ModuleName,
ratelimittypes.ModuleName,
icacallbacksmoduletypes.ModuleName,
claimtypes.ModuleName,
ccvconsumertypes.ModuleName,
Expand Down Expand Up @@ -956,7 +950,7 @@ func NewStrideApp(
epochsmoduletypes.ModuleName,
interchainquerytypes.ModuleName,
recordsmoduletypes.ModuleName,
ratelimitmoduletypes.ModuleName,
ratelimittypes.ModuleName,
icacallbacksmoduletypes.ModuleName,
claimtypes.ModuleName,
ccvconsumertypes.ModuleName,
Expand Down Expand Up @@ -1256,7 +1250,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(icacontrollertypes.SubModuleName)
paramsKeeper.Subspace(icahosttypes.SubModuleName)
paramsKeeper.Subspace(recordsmoduletypes.ModuleName)
paramsKeeper.Subspace(ratelimitmoduletypes.ModuleName)
paramsKeeper.Subspace(ratelimittypes.ModuleName)
paramsKeeper.Subspace(icacallbacksmoduletypes.ModuleName)
paramsKeeper.Subspace(ccvconsumertypes.ModuleName)
paramsKeeper.Subspace(autopilottypes.ModuleName)
Expand Down
4 changes: 3 additions & 1 deletion app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
consumertypes "github.com/cosmos/interchain-security/v4/x/ccv/consumer/types"
evmosvestingtypes "github.com/evmos/vesting/x/vesting/types"

ratelimittypes "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/types"

v10 "github.com/Stride-Labs/stride/v18/app/upgrades/v10"
v11 "github.com/Stride-Labs/stride/v18/app/upgrades/v11"
v12 "github.com/Stride-Labs/stride/v18/app/upgrades/v12"
Expand All @@ -37,7 +39,6 @@ import (
claimtypes "github.com/Stride-Labs/stride/v18/x/claim/types"
icacallbacktypes "github.com/Stride-Labs/stride/v18/x/icacallbacks/types"
icaoracletypes "github.com/Stride-Labs/stride/v18/x/icaoracle/types"
ratelimittypes "github.com/Stride-Labs/stride/v18/x/ratelimit/types"
recordtypes "github.com/Stride-Labs/stride/v18/x/records/types"
stakeibctypes "github.com/Stride-Labs/stride/v18/x/stakeibc/types"
staketiatypes "github.com/Stride-Labs/stride/v18/x/staketia/types"
Expand Down Expand Up @@ -257,6 +258,7 @@ func (app *StrideApp) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
app.configurator,
app.appCodec,
app.ConsumerKeeper,
app.RatelimitKeeper,
app.WasmKeeper,
),
)
Expand Down
10 changes: 5 additions & 5 deletions app/upgrades/v10/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (
"github.com/cosmos/ibc-go/v7/modules/core/exported"
ibctmmigrations "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint/migrations"

ratelimitkeeper "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/keeper"
ratelimittypes "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/types"

claimkeeper "github.com/Stride-Labs/stride/v18/x/claim/keeper"
claimtypes "github.com/Stride-Labs/stride/v18/x/claim/types"
icacallbackskeeper "github.com/Stride-Labs/stride/v18/x/icacallbacks/keeper"
mintkeeper "github.com/Stride-Labs/stride/v18/x/mint/keeper"
minttypes "github.com/Stride-Labs/stride/v18/x/mint/types"
ratelimitkeeper "github.com/Stride-Labs/stride/v18/x/ratelimit/keeper"
ratelimitgov "github.com/Stride-Labs/stride/v18/x/ratelimit/keeper/gov"
ratelimittypes "github.com/Stride-Labs/stride/v18/x/ratelimit/types"
recordskeeper "github.com/Stride-Labs/stride/v18/x/records/keeper"
recordstypes "github.com/Stride-Labs/stride/v18/x/records/types"
stakeibckeeper "github.com/Stride-Labs/stride/v18/x/stakeibc/keeper"
Expand Down Expand Up @@ -331,15 +331,15 @@ func EnableRateLimits(
denom := stakeibctypes.StAssetDenomFromHostZoneDenom(hostZone.HostDenom)
channelId := hostZone.TransferChannelId

addRateLimit := &ratelimittypes.AddRateLimitProposal{
addRateLimitMsg := &ratelimittypes.MsgAddRateLimit{
Denom: denom,
ChannelId: channelId,
MaxPercentSend: threshold,
MaxPercentRecv: threshold,
DurationHours: RateLimitDurationHours,
}

if err := ratelimitgov.AddRateLimit(ctx, ratelimitKeeper, channelKeeper, addRateLimit); err != nil {
if err := ratelimitKeeper.AddRateLimit(ctx, addRateLimitMsg); err != nil {
return errorsmod.Wrapf(err, "unable to add rate limit for %s", denom)
}

Expand Down
3 changes: 2 additions & 1 deletion app/upgrades/v10/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
v10 "github.com/Stride-Labs/stride/v18/app/upgrades/v10"
"github.com/Stride-Labs/stride/v18/utils"

ratelimittypes "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/types"

icacallbackstypes "github.com/Stride-Labs/stride/v18/x/icacallbacks/types"
ratelimittypes "github.com/Stride-Labs/stride/v18/x/ratelimit/types"
recordskeeper "github.com/Stride-Labs/stride/v18/x/records/keeper"
recordstypes "github.com/Stride-Labs/stride/v18/x/records/types"
stakeibckeeper "github.com/Stride-Labs/stride/v18/x/stakeibc/keeper"
Expand Down
3 changes: 2 additions & 1 deletion app/upgrades/v16/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

ratelimitkeeper "github.com/Stride-Labs/stride/v18/x/ratelimit/keeper"
ratelimitkeeper "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/keeper"

stakeibckeeper "github.com/Stride-Labs/stride/v18/x/stakeibc/keeper"
stakeibctypes "github.com/Stride-Labs/stride/v18/x/stakeibc/types"
)
Expand Down
6 changes: 4 additions & 2 deletions app/upgrades/v17/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import (

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

ratelimittypes "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/types"

ratelimitkeeper "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/keeper"

"github.com/Stride-Labs/stride/v18/utils"
icqkeeper "github.com/Stride-Labs/stride/v18/x/interchainquery/keeper"
ratelimitkeeper "github.com/Stride-Labs/stride/v18/x/ratelimit/keeper"
ratelimittypes "github.com/Stride-Labs/stride/v18/x/ratelimit/types"
recordtypes "github.com/Stride-Labs/stride/v18/x/records/types"
stakeibckeeper "github.com/Stride-Labs/stride/v18/x/stakeibc/keeper"
stakeibctypes "github.com/Stride-Labs/stride/v18/x/stakeibc/types"
Expand Down
3 changes: 2 additions & 1 deletion app/upgrades/v17/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/stretchr/testify/suite"

ratelimittypes "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/types"

icqtypes "github.com/Stride-Labs/stride/v18/x/interchainquery/types"
ratelimittypes "github.com/Stride-Labs/stride/v18/x/ratelimit/types"
recordtypes "github.com/Stride-Labs/stride/v18/x/records/types"

"github.com/Stride-Labs/stride/v18/app/apptesting"
Expand Down
1 change: 1 addition & 0 deletions app/upgrades/v19/legacyratelimit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The legacy ratelimit types aren't required for this migration (since the types are the same), but they're used as a sanity check in the unit tests.
File renamed without changes.
25 changes: 25 additions & 0 deletions app/upgrades/v19/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package v19

import (
"time"

errorsmod "cosmossdk.io/errors"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
ratelimitkeeper "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/keeper"
ratelimittypes "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -23,6 +27,7 @@ func CreateUpgradeHandler(
configurator module.Configurator,
cdc codec.Codec,
consumerKeeper ccvconsumerkeeper.Keeper,
ratelimitKeeper ratelimitkeeper.Keeper,
wasmKeeper wasmkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
Expand All @@ -40,6 +45,10 @@ func CreateUpgradeHandler(
return newVm, errorsmod.Wrapf(err, "unable to migrate ICS to v14")
}

// Migrate to open sourced rate limiter
MigrateRateLimitModule(ctx, ratelimitKeeper)

// Update wasm upload permissions
if err := SetWasmPermissions(ctx, wasmKeeper); err != nil {
return newVm, errorsmod.Wrapf(err, "unable to set wasm permissions")
}
Expand All @@ -66,6 +75,22 @@ func MigrateICSOutstandingDowntime(ctx sdk.Context, ck ccvconsumerkeeper.Keeper)
return nil
}

// Migrate the rate limit module to the open sourced version
// The module has the same store key so all the rate limit types
// can remain unchanged
// The only required change is to create the new epoch type
// that's used instead of the epochs module
func MigrateRateLimitModule(ctx sdk.Context, k ratelimitkeeper.Keeper) {
// Initialize the hour epoch so that the epoch number matches
// the current hour and the start time is precisely on the hour
genesisState := ratelimittypes.DefaultGenesis()
hourEpoch := genesisState.HourEpoch
hourEpoch.EpochNumber = uint64(ctx.BlockTime().Hour())
hourEpoch.EpochStartTime = ctx.BlockTime().Truncate(time.Hour)
hourEpoch.EpochStartHeight = ctx.BlockHeight()
k.SetHourEpoch(ctx, hourEpoch)
}

// Update wasm params so that contracts can only be uploaded through governance
func SetWasmPermissions(ctx sdk.Context, wk wasmkeeper.Keeper) error {
wasmParams := wk.GetParams(ctx)
Expand Down
Loading

0 comments on commit 48239b6

Please sign in to comment.