Skip to content

Commit

Permalink
Added rate limits in upgrade (#4340)
Browse files Browse the repository at this point in the history
* added rate limits in upgrade

* added upgrade test

* refactor rate limiting tests and added rate limits pre-upgrade

* lint

* goimports

* experiments for E2E

* post merge lints

* added RL before upgrade

* just get the latest contract

* experiments

* updated wasm file

* remove unnecessary prints

* bad check

* experiments without e2e

* removed unnecessary fmt

* param space initialization for rate limiting (#4360)

* param space initialization for rate limiting

* lint

* better test

* properly initialize empty params

* fix typo

* adding RL to E2E

* removing RL again from E2E

* debug

* genesis

* genesis 2

* add genesis and solve params bug

* uncomment setup rate limiting

* added configurable gov module

* clean up

* clean up and genesis test

* upgrade handler

* genesis clean up

* clean up e2e

* wire querier

* route fix

* fix router

* debug things

* fix init genesis bug

* push fix

* lint

* fix

* clean up upgrades

* testing propper params after upgrade in E2E

* "properly" unmarshaling the parm

* goimports

* allowing for types other than objects in cosmwasm queries

* fix check for param reset

* remove unnecessary prints

* compiled with the proper vesion of workspace optimizer

* Revert "compiled with the proper vesion of workspace optimizer"

This reverts commit ab5cf6c.

* added length check

* params experiment for test with state-export

---------

Co-authored-by: Roman <[email protected]>
Co-authored-by: Roman <[email protected]>
(cherry picked from commit a1e2b3d)

# Conflicts:
#	app/keepers/modules.go
#	app/modules.go
#	app/upgrades/v15/export_test.go
#	app/upgrades/v15/upgrade_test.go
#	app/upgrades/v15/upgrades.go
#	tests/e2e/configurer/chain/commands.go
#	tests/e2e/e2e_test.go
  • Loading branch information
nicolaslara authored and mergify[bot] committed Feb 23, 2023
1 parent 19e3b25 commit 67f633e
Show file tree
Hide file tree
Showing 29 changed files with 1,521 additions and 245 deletions.
4 changes: 1 addition & 3 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,13 @@ func (appKeepers *AppKeepers) WireICS20PreWasmKeeper(
)

// ChannelKeeper wrapper for rate limiting SendPacket(). The wasmKeeper needs to be added after it's created
rateLimitingParams := appKeepers.GetSubspace(ibcratelimittypes.ModuleName)
rateLimitingParams = rateLimitingParams.WithKeyTable(ibcratelimittypes.ParamKeyTable())
rateLimitingICS4Wrapper := ibcratelimit.NewICS4Middleware(
appKeepers.HooksICS4Wrapper,
appKeepers.AccountKeeper,
// wasm keeper we set later.
nil,
appKeepers.BankKeeper,
rateLimitingParams,
appKeepers.GetSubspace(ibcratelimittypes.ModuleName),
)
appKeepers.RateLimitingICS4Wrapper = &rateLimitingICS4Wrapper

Expand Down
7 changes: 6 additions & 1 deletion app/keepers/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
downtimemodule "github.com/osmosis-labs/osmosis/v14/x/downtime-detector/module"
"github.com/osmosis-labs/osmosis/v14/x/epochs"
"github.com/osmosis-labs/osmosis/v14/x/gamm"
ibc_rate_limit "github.com/osmosis-labs/osmosis/v14/x/ibc-rate-limit"
"github.com/osmosis-labs/osmosis/v14/x/ibc-rate-limit/ibcratelimitmodule"
"github.com/osmosis-labs/osmosis/v14/x/incentives"
"github.com/osmosis-labs/osmosis/v14/x/lockup"
"github.com/osmosis-labs/osmosis/v14/x/mint"
Expand Down Expand Up @@ -92,5 +92,10 @@ var AppModuleBasics = []module.AppModuleBasic{
wasm.AppModuleBasic{},
ica.AppModuleBasic{},
ibc_hooks.AppModuleBasic{},
<<<<<<< HEAD
ibc_rate_limit.AppModuleBasic{},
=======
ibcratelimitmodule.AppModuleBasic{},
router.AppModuleBasic{},
>>>>>>> a1e2b3d4 (Added rate limits in upgrade (#4340))
}
12 changes: 12 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ import (
epochstypes "github.com/osmosis-labs/osmosis/v14/x/epochs/types"
"github.com/osmosis-labs/osmosis/v14/x/gamm"
gammtypes "github.com/osmosis-labs/osmosis/v14/x/gamm/types"
"github.com/osmosis-labs/osmosis/v14/x/ibc-rate-limit/ibcratelimitmodule"
ibcratelimittypes "github.com/osmosis-labs/osmosis/v14/x/ibc-rate-limit/types"
"github.com/osmosis-labs/osmosis/v14/x/incentives"
incentivestypes "github.com/osmosis-labs/osmosis/v14/x/incentives/types"
"github.com/osmosis-labs/osmosis/v14/x/lockup"
Expand Down Expand Up @@ -151,6 +153,11 @@ func appModules(
app.EpochsKeeper,
),
tokenfactory.NewAppModule(*app.TokenFactoryKeeper, app.AccountKeeper, app.BankKeeper),
<<<<<<< HEAD
=======
valsetprefmodule.NewAppModule(appCodec, *app.ValidatorSetPreferenceKeeper),
ibcratelimitmodule.NewAppModule(*app.RateLimitingICS4Wrapper),
>>>>>>> a1e2b3d4 (Added rate limits in upgrade (#4340))
ibc_hooks.NewAppModule(app.AccountKeeper),
}
}
Expand Down Expand Up @@ -227,6 +234,11 @@ func OrderInitGenesis(allModuleNames []string) []string {
epochstypes.ModuleName,
lockuptypes.ModuleName,
authz.ModuleName,
<<<<<<< HEAD
=======
concentratedliquiditytypes.ModuleName,
ibcratelimittypes.ModuleName,
>>>>>>> a1e2b3d4 (Added rate limits in upgrade (#4340))
// wasm after ibc transfer
wasm.ModuleName,
// ibc_hooks after auth keeper
Expand Down
30 changes: 30 additions & 0 deletions app/upgrades/v15/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package v15

import (
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
ibcratelimit "github.com/osmosis-labs/osmosis/v14/x/ibc-rate-limit"
icqkeeper "github.com/strangelove-ventures/async-icq/v4/keeper"

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

gammkeeper "github.com/osmosis-labs/osmosis/v14/x/gamm/keeper"
poolmanagerkeeper "github.com/osmosis-labs/osmosis/v14/x/poolmanager"
)

func MigrateNextPoolId(ctx sdk.Context, gammKeeper *gammkeeper.Keeper, poolmanagerKeeper *poolmanagerkeeper.Keeper) {
migrateNextPoolId(ctx, gammKeeper, poolmanagerKeeper)
}

func RegisterOsmoIonMetadata(ctx sdk.Context, bankKeeper bankkeeper.Keeper) {
registerOsmoIonMetadata(ctx, bankKeeper)
}

func SetICQParams(ctx sdk.Context, icqKeeper *icqkeeper.Keeper) {
setICQParams(ctx, icqKeeper)
}

func SetRateLimits(ctx sdk.Context, accountKeeper *authkeeper.AccountKeeper, rateLimitingICS4Wrapper *ibcratelimit.ICS4Wrapper, wasmKeeper *wasmkeeper.Keeper) {
setRateLimits(ctx, accountKeeper, rateLimitingICS4Wrapper, wasmKeeper)
}
161 changes: 161 additions & 0 deletions app/upgrades/v15/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package v15_test

import (
"fmt"
"os"
"reflect"
"testing"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
transfertypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types"

ibcratelimittypes "github.com/osmosis-labs/osmosis/v14/x/ibc-rate-limit/types"

gamm "github.com/osmosis-labs/osmosis/v14/x/gamm/keeper"

"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/osmosis/v14/app/apptesting"
v15 "github.com/osmosis-labs/osmosis/v14/app/upgrades/v15"
)

type UpgradeTestSuite struct {
apptesting.KeeperTestHelper
}

func (suite *UpgradeTestSuite) SetupTest() {
suite.Setup()
}

func TestUpgradeTestSuite(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite))
}

func (suite *UpgradeTestSuite) TestMigrateNextPoolIdAndCreatePool() {
suite.SetupTest() // reset

const (
expectedNextPoolId uint64 = 1
)

var (
gammKeeperType = reflect.TypeOf(&gamm.Keeper{})
)

ctx := suite.Ctx
gammKeeper := suite.App.GAMMKeeper
poolmanagerKeeper := suite.App.PoolManagerKeeper

nextPoolId := gammKeeper.GetNextPoolId(ctx)
suite.Require().Equal(expectedNextPoolId, nextPoolId)

// system under test.
v15.MigrateNextPoolId(ctx, gammKeeper, poolmanagerKeeper)

// validate poolmanager's next pool id.
actualNextPoolId := poolmanagerKeeper.GetNextPoolId(ctx)
suite.Require().Equal(expectedNextPoolId, actualNextPoolId)

// create a pool after migration.
actualCreatedPoolId := suite.PrepareBalancerPool()
suite.Require().Equal(expectedNextPoolId, actualCreatedPoolId)

// validate that module route mapping has been created for each pool id.
for poolId := uint64(1); poolId < expectedNextPoolId; poolId++ {
swapModule, err := poolmanagerKeeper.GetPoolModule(ctx, poolId)
suite.Require().NoError(err)

suite.Require().Equal(gammKeeperType, reflect.TypeOf(swapModule))
}

// validate params
gammPoolCreationFee := gammKeeper.GetParams(ctx).PoolCreationFee
poolmanagerPoolCreationFee := poolmanagerKeeper.GetParams(ctx).PoolCreationFee
suite.Require().Equal(gammPoolCreationFee, poolmanagerPoolCreationFee)
}

func (suite *UpgradeTestSuite) TestRegisterOsmoIonMetadata() {
suite.SetupTest() // reset

expectedUosmodenom := "uosmo"
expectedUiondenom := "uion"

ctx := suite.Ctx
bankKeeper := suite.App.BankKeeper

// meta data should not be found pre-registration of meta data
uosmoMetadata, found := suite.App.BankKeeper.GetDenomMetaData(ctx, "uosmo")
suite.Require().False(found)

uionMetadata, found := suite.App.BankKeeper.GetDenomMetaData(ctx, "uion")
suite.Require().False(found)

// system under test.
v15.RegisterOsmoIonMetadata(ctx, *bankKeeper)

uosmoMetadata, found = suite.App.BankKeeper.GetDenomMetaData(ctx, "uosmo")
suite.Require().True(found)

uionMetadata, found = suite.App.BankKeeper.GetDenomMetaData(ctx, "uion")
suite.Require().True(found)

suite.Require().Equal(expectedUosmodenom, uosmoMetadata.Base)
suite.Require().Equal(expectedUiondenom, uionMetadata.Base)
}

func (suite *UpgradeTestSuite) TestSetICQParams() {
suite.SetupTest() // reset

// system under test.
v15.SetICQParams(suite.Ctx, suite.App.ICQKeeper)

suite.Require().True(suite.App.ICQKeeper.IsHostEnabled(suite.Ctx))
suite.Require().Len(suite.App.ICQKeeper.GetAllowQueries(suite.Ctx), 63)
}

func (suite *UpgradeTestSuite) TestSetRateLimits() {
suite.SetupTest() // reset
accountKeeper := suite.App.AccountKeeper
govModule := accountKeeper.GetModuleAddress(govtypes.ModuleName)

code, err := os.ReadFile("../v13/rate_limiter.wasm")
suite.Require().NoError(err)
contractKeeper := wasmkeeper.NewGovPermissionKeeper(suite.App.WasmKeeper)
instantiateConfig := wasmtypes.AccessConfig{Permission: wasmtypes.AccessTypeOnlyAddress, Address: govModule.String()}
codeID, _, err := contractKeeper.Create(suite.Ctx, govModule, code, &instantiateConfig)
suite.Require().NoError(err)
transferModule := accountKeeper.GetModuleAddress(transfertypes.ModuleName)
initMsgBz := []byte(fmt.Sprintf(`{
"gov_module": "%s",
"ibc_module":"%s",
"paths": []
}`,
govModule, transferModule))

addr, _, err := contractKeeper.Instantiate(suite.Ctx, codeID, govModule, govModule, initMsgBz, "rate limiting contract", nil)
suite.Require().NoError(err)
addrStr, err := sdk.Bech32ifyAddressBytes("osmo", addr)
suite.Require().NoError(err)
params, err := ibcratelimittypes.NewParams(addrStr)
suite.Require().NoError(err)
paramSpace, ok := suite.App.ParamsKeeper.GetSubspace(ibcratelimittypes.ModuleName)
suite.Require().True(ok)
paramSpace.SetParamSet(suite.Ctx, &params)

// system under test.
v15.SetRateLimits(suite.Ctx, accountKeeper, suite.App.RateLimitingICS4Wrapper, suite.App.WasmKeeper)

state, err := suite.App.WasmKeeper.QuerySmart(suite.Ctx, addr, []byte(`{"get_quotas": {"channel_id": "any", "denom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"}}`))
suite.Require().Greaterf(len(state), 0, "state should not be empty")

state, err = suite.App.WasmKeeper.QuerySmart(suite.Ctx, addr, []byte(`{"get_quotas": {"channel_id": "any", "denom": "ibc/D189335C6E4A68B513C10AB227BF1C1D38C746766278BA3EEB4FB14124F1D858"}}`))
suite.Require().Greaterf(len(state), 0, "state should not be empty")

// This is the last one. If the others failed the upgrade would've panicked before adding this one
state, err = suite.App.WasmKeeper.QuerySmart(suite.Ctx, addr, []byte(`{"get_quotas": {"channel_id": "any", "denom": "ibc/E6931F78057F7CC5DA0FD6CEF82FF39373A6E0452BF1FD76910B93292CF356C1"}}`))
suite.Require().Greaterf(len(state), 0, "state should not be empty")

}
Loading

0 comments on commit 67f633e

Please sign in to comment.