Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switched to open sourced rate limit module #1123

Merged
merged 20 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
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
Loading