Skip to content

Commit

Permalink
Merge pull request #640 from neutron-org/feat/icq-params-gov-gated
Browse files Browse the repository at this point in the history
feat: icq MaxKvQueryKeysCount and MaxTransactionsFilters gov gated #NTRN-364
  • Loading branch information
pr0n00gler authored Sep 2, 2024
2 parents 5629cc5 + e94558f commit b84352c
Show file tree
Hide file tree
Showing 15 changed files with 320 additions and 89 deletions.
8 changes: 7 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,13 @@ func New(

icaHostIBCModule := icahost.NewIBCModule(app.ICAHostKeeper)

interchainQueriesModule := interchainqueries.NewAppModule(appCodec, app.InterchainQueriesKeeper, app.AccountKeeper, app.BankKeeper)
interchainQueriesModule := interchainqueries.NewAppModule(
appCodec,
keys[interchainqueriesmoduletypes.StoreKey],
app.InterchainQueriesKeeper,
app.AccountKeeper,
app.BankKeeper,
)
interchainTxsModule := interchaintxs.NewAppModule(appCodec, app.InterchainTxsKeeper, app.AccountKeeper, app.BankKeeper)
contractManagerModule := contractmanager.NewAppModule(appCodec, app.ContractManagerKeeper)
ibcHooksModule := ibchooks.NewAppModule(app.AccountKeeper)
Expand Down
6 changes: 6 additions & 0 deletions proto/neutron/interchainqueries/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ message Params {
// balance between network cleaning speed and EndBlock duration. A zero value
// means no limit.
uint64 tx_query_removal_limit = 3;

// Maximum amount of keys in a registered key value query
uint64 max_kv_query_keys_count = 4;

// max_transactions_filters defines maximum allowed amount of tx filters in msgRegisterInterchainQuery
uint64 max_transactions_filters = 5;
}
25 changes: 25 additions & 0 deletions x/interchainqueries/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package keeper

import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

v3 "github.com/neutron-org/neutron/v4/x/interchainqueries/migrations/v3"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
}

// NewMigrator returns a new Migrator.
func NewMigrator(cdc codec.BinaryCodec, storeKey storetypes.StoreKey) Migrator {
return Migrator{storeKey: storeKey, cdc: cdc}
}

// Migrate2to3 migrates from version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v3.MigrateParams(ctx, m.cdc, m.storeKey)
}
19 changes: 9 additions & 10 deletions x/interchainqueries/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {

func (m msgServer) RegisterInterchainQuery(goCtx context.Context, msg *types.MsgRegisterInterchainQuery) (*types.MsgRegisterInterchainQueryResponse, error) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), LabelRegisterInterchainQuery)
ctx := sdk.UnwrapSDKContext(goCtx)
ctx.Logger().Debug("RegisterInterchainQuery", "msg", msg)
params := m.GetParams(ctx)

if err := msg.Validate(); err != nil {
if err := msg.Validate(params); err != nil {
return nil, errors.Wrap(err, "failed to validate MsgRegisterInterchainQuery")
}

ctx := sdk.UnwrapSDKContext(goCtx)
ctx.Logger().Debug("RegisterInterchainQuery", "msg", msg)

senderAddr, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
m.Logger(ctx).Debug("RegisterInterchainQuery: failed to parse sender address", "sender_address", msg.Sender)
Expand All @@ -62,8 +62,6 @@ func (m msgServer) RegisterInterchainQuery(goCtx context.Context, msg *types.Msg
lastID := m.GetLastRegisteredQueryKey(ctx)
lastID++

params := m.GetParams(ctx)

registeredQuery := &types.RegisteredQuery{
Id: lastID,
Owner: msg.Sender,
Expand Down Expand Up @@ -122,12 +120,13 @@ func (m msgServer) RemoveInterchainQuery(goCtx context.Context, msg *types.MsgRe
}

func (m msgServer) UpdateInterchainQuery(goCtx context.Context, msg *types.MsgUpdateInterchainQueryRequest) (*types.MsgUpdateInterchainQueryResponse, error) {
if err := msg.Validate(); err != nil {
return nil, errors.Wrap(err, "failed to validate MsgUpdateInterchainQueryRequest")
}

ctx := sdk.UnwrapSDKContext(goCtx)
ctx.Logger().Debug("UpdateInterchainQuery", "msg", msg)
params := m.GetParams(ctx)

if err := msg.Validate(params); err != nil {
return nil, errors.Wrap(err, "failed to validate MsgUpdateInterchainQueryRequest")
}

query, err := m.GetQueryByID(ctx, msg.GetQueryId())
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions x/interchainqueries/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestMsgRegisterInterchainQueryValidate(t *testing.T) {
"too many keys",
types.MsgRegisterInterchainQuery{
QueryType: string(types.InterchainQueryTypeKV),
Keys: make([]*types.KVKey, types.MaxKVQueryKeysCount+1),
Keys: make([]*types.KVKey, types.DefaultMaxKvQueryKeysCount+1),
TransactionsFilter: "[]",
ConnectionId: "connection-0",
UpdatePeriod: 1,
Expand Down Expand Up @@ -394,7 +394,7 @@ func TestMsgUpdateInterchainQueryRequestValidate(t *testing.T) {
"too many keys",
types.MsgUpdateInterchainQueryRequest{
QueryId: 1,
NewKeys: make([]*types.KVKey, types.MaxKVQueryKeysCount+1),
NewKeys: make([]*types.KVKey, types.DefaultMaxKvQueryKeysCount+1),
NewUpdatePeriod: 0,
Sender: testutil.TestOwnerAddress,
},
Expand Down
28 changes: 28 additions & 0 deletions x/interchainqueries/migrations/v3/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package v3

import (
"fmt"

store "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/neutron-org/neutron/v4/x/interchainqueries/types"
)

func MigrateParams(ctx sdk.Context, cdc codec.BinaryCodec, storeKey store.StoreKey) error {
var params types.Params
st := ctx.KVStore(storeKey)
bz := st.Get(types.ParamsKey)
if bz == nil {
return fmt.Errorf("no params stored in %s", types.ParamsKey)
}

cdc.MustUnmarshal(bz, &params)
params.MaxTransactionsFilters = types.DefaultMaxTransactionsFilters
params.MaxKvQueryKeysCount = types.DefaultMaxKvQueryKeysCount
bz = cdc.MustMarshal(&params)
st.Set(types.ParamsKey, bz)
return nil
}
83 changes: 83 additions & 0 deletions x/interchainqueries/migrations/v3/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package v3_test

import (
"testing"

github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
"gopkg.in/yaml.v2"

"github.com/neutron-org/neutron/v4/testutil"
v3 "github.com/neutron-org/neutron/v4/x/interchainqueries/migrations/v3"
"github.com/neutron-org/neutron/v4/x/interchainqueries/types"
)

type V3ICQMigrationTestSuite struct {
testutil.IBCConnectionTestSuite
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(V3ICQMigrationTestSuite))
}

// ParamsV2 defines the parameters for the module v2.
type ParamsV2 struct {
// Defines amount of blocks required before query becomes available for
// removal by anybody
QuerySubmitTimeout uint64 `protobuf:"varint,1,opt,name=query_submit_timeout,json=querySubmitTimeout,proto3" json:"query_submit_timeout,omitempty"`
// Amount of coins deposited for the query.
QueryDeposit github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,2,rep,name=query_deposit,json=queryDeposit,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coins" json:"query_deposit"`
// Amount of tx hashes to be removed during a single EndBlock. Can vary to
// balance between network cleaning speed and EndBlock duration. A zero value
// means no limit.
TxQueryRemovalLimit uint64 `protobuf:"varint,3,opt,name=tx_query_removal_limit,json=txQueryRemovalLimit,proto3" json:"tx_query_removal_limit,omitempty"`
}

func (p *ParamsV2) Reset() { *p = ParamsV2{} }
func (p *ParamsV2) ProtoMessage() {}

// String implements the Stringer interface.
func (p ParamsV2) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}

func (suite *V3ICQMigrationTestSuite) TestParamsMigration() {
var (
app = suite.GetNeutronZoneApp(suite.ChainA)
storeKey = app.GetKey(types.StoreKey)
ctx = suite.ChainA.GetContext()
cdc = app.AppCodec()
)

// preinitialize v2 params
p := ParamsV2{
QuerySubmitTimeout: types.DefaultQuerySubmitTimeout,
QueryDeposit: types.DefaultQueryDeposit,
TxQueryRemovalLimit: types.DefaultTxQueryRemovalLimit,
}
store := ctx.KVStore(storeKey)
bz, err := cdc.Marshal(&p)
suite.Require().NoError(err)
store.Set(types.ParamsKey, bz)

paramsOld := app.InterchainQueriesKeeper.GetParams(ctx)
suite.Require().Equal(paramsOld.TxQueryRemovalLimit, p.TxQueryRemovalLimit)
suite.Require().Equal(paramsOld.QuerySubmitTimeout, p.QuerySubmitTimeout)
suite.Require().Equal(paramsOld.QueryDeposit, p.QueryDeposit)
suite.Require().Equal(paramsOld.MaxTransactionsFilters, uint64(0))
suite.Require().Equal(paramsOld.MaxKvQueryKeysCount, uint64(0))

err = v3.MigrateParams(ctx, cdc, storeKey)
suite.Require().NoError(err)

paramsNew := app.InterchainQueriesKeeper.GetParams(ctx)
params := types.Params{
QuerySubmitTimeout: types.DefaultQuerySubmitTimeout,
QueryDeposit: types.DefaultQueryDeposit,
TxQueryRemovalLimit: types.DefaultTxQueryRemovalLimit,
MaxKvQueryKeysCount: types.DefaultMaxKvQueryKeysCount,
MaxTransactionsFilters: types.DefaultMaxTransactionsFilters,
}
suite.Require().Equal(params, paramsNew)
}
11 changes: 11 additions & 0 deletions x/interchainqueries/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"

storetypes "cosmossdk.io/store/types"

"cosmossdk.io/core/appmodule"

abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -103,19 +105,22 @@ var _ appmodule.AppModule = AppModule{}
type AppModule struct {
AppModuleBasic

storeKey storetypes.StoreKey
keeper keeper.Keeper
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
}

func NewAppModule(
cdc codec.Codec,
storeKey storetypes.StoreKey,
keeper keeper.Keeper,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
) AppModule {
return AppModule{
AppModuleBasic: NewAppModuleBasic(cdc),
storeKey: storeKey,
keeper: keeper,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
Expand All @@ -141,6 +146,12 @@ func (AppModule) QuerierRoute() string { return types.QuerierRoute }
// RegisterServices registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
m := keeper.NewMigrator(am.cdc, am.storeKey)

if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil {
panic(fmt.Sprintf("failed to migrate x/interchainqueries from version 2 to 3: %v", err))
}

types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
}
Expand Down
2 changes: 1 addition & 1 deletion x/interchainqueries/types/constants.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package types

const ConsensusVersion = 2
const ConsensusVersion = 3
4 changes: 2 additions & 2 deletions x/interchainqueries/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func (gs GenesisState) Validate() error {

switch val.QueryType {
case string(InterchainQueryTypeTX):
if err := ValidateTransactionsFilter(val.TransactionsFilter); err != nil {
if err := ValidateTransactionsFilter(val.TransactionsFilter, gs.Params.MaxTransactionsFilters); err != nil {
return errors.Wrap(ErrInvalidTransactionsFilter, err.Error())
}
case string(InterchainQueryTypeKV):
if len(val.Keys) == 0 {
return errors.Wrap(ErrEmptyKeys, "keys cannot be empty")
}
if err := validateKeys(val.GetKeys()); err != nil {
if err := validateKeys(val.GetKeys(), gs.Params.MaxKvQueryKeysCount); err != nil {
return err
}
default:
Expand Down
26 changes: 15 additions & 11 deletions x/interchainqueries/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (
var _ paramtypes.ParamSet = (*Params)(nil)

var (
KeyQuerySubmitTimeout = []byte("QuerySubmitTimeout")
DefaultQuerySubmitTimeout = uint64(1036800) // One month, with block_time = 2.5s
KeyQueryDeposit = []byte("QueryDeposit")
DefaultQueryDeposit = sdk.NewCoins(sdk.NewCoin(params.DefaultDenom, math.NewInt(int64(1_000_000))))
KeyTxQueryRemovalLimit = []byte("TxQueryRemovalLimit")
DefaultTxQueryRemovalLimit = uint64(10_000)
KeyQuerySubmitTimeout = []byte("QuerySubmitTimeout")
DefaultQuerySubmitTimeout = uint64(1036800) // One month, with block_time = 2.5s
KeyQueryDeposit = []byte("QueryDeposit")
DefaultQueryDeposit = sdk.NewCoins(sdk.NewCoin(params.DefaultDenom, math.NewInt(int64(1_000_000))))
KeyTxQueryRemovalLimit = []byte("TxQueryRemovalLimit")
DefaultTxQueryRemovalLimit = uint64(10_000)
DefaultMaxKvQueryKeysCount = uint64(32)
DefaultMaxTransactionsFilters = uint64(32)
)

// ParamKeyTable the param key table for launch module
Expand All @@ -32,17 +34,19 @@ func ParamKeyTable() paramtypes.KeyTable {
}

// NewParams creates a new Params instance
func NewParams(querySubmitTimeout uint64, queryDeposit sdk.Coins, txQueryRemovalLimit uint64) Params {
func NewParams(querySubmitTimeout uint64, queryDeposit sdk.Coins, txQueryRemovalLimit, maxKvQueryKeysCount, maxTransactionsFilters uint64) Params {
return Params{
QuerySubmitTimeout: querySubmitTimeout,
QueryDeposit: queryDeposit,
TxQueryRemovalLimit: txQueryRemovalLimit,
QuerySubmitTimeout: querySubmitTimeout,
QueryDeposit: queryDeposit,
TxQueryRemovalLimit: txQueryRemovalLimit,
MaxKvQueryKeysCount: maxKvQueryKeysCount,
MaxTransactionsFilters: maxTransactionsFilters,
}
}

// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(DefaultQuerySubmitTimeout, DefaultQueryDeposit, DefaultTxQueryRemovalLimit)
return NewParams(DefaultQuerySubmitTimeout, DefaultQueryDeposit, DefaultTxQueryRemovalLimit, DefaultMaxKvQueryKeysCount, DefaultMaxTransactionsFilters)
}

// ParamSetPairs get the params.ParamSet
Expand Down
Loading

0 comments on commit b84352c

Please sign in to comment.