Skip to content

Commit

Permalink
Remove token factory config (sei-protocol#1971)
Browse files Browse the repository at this point in the history
* remove token factory config

* move DenomAllowListMaxSize to params

* formatting

* add more tests

* formatting

* address review comments
  • Loading branch information
dssei authored Dec 5, 2024
1 parent 7c8e452 commit 15333e5
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 90 deletions.
6 changes: 0 additions & 6 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,19 +534,13 @@ func New(
).SetHooks(epochmoduletypes.NewMultiEpochHooks(
app.MintKeeper.Hooks()))

tokenFactoryConfig, err := tokenfactorykeeper.ReadConfig(appOpts)
if err != nil {
panic(fmt.Sprintf("error reading token factory config due to %s", err))
}

app.TokenFactoryKeeper = tokenfactorykeeper.NewKeeper(
appCodec,
app.keys[tokenfactorytypes.StoreKey],
app.GetSubspace(tokenfactorytypes.ModuleName),
app.AccountKeeper,
app.BankKeeper.(bankkeeper.BaseKeeper).WithMintCoinsRestriction(tokenfactorytypes.NewTokenFactoryDenomMintCoinsRestriction()),
app.DistrKeeper,
tokenFactoryConfig,
)

// The last arguments can contain custom message handlers, and custom query handlers,
Expand Down
4 changes: 0 additions & 4 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
tokenfactorykeeper "github.com/sei-protocol/sei-chain/x/tokenfactory/keeper"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/config"
Expand Down Expand Up @@ -56,9 +55,6 @@ func (t TestAppOpts) Get(s string) interface{} {
if s == FlagSCEnable {
return t.useSc
}
if s == tokenfactorykeeper.FlagDenomAllowListMaxSize {
return 3
}
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion proto/tokenfactory/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ package seiprotocol.seichain.tokenfactory;
option go_package = "github.com/sei-protocol/sei-chain/x/tokenfactory/types";

// Params defines the parameters for the tokenfactory module.
message Params {}
message Params {
uint32 denom_allowlist_max_size = 1;
}
29 changes: 0 additions & 29 deletions x/tokenfactory/keeper/config.go

This file was deleted.

10 changes: 5 additions & 5 deletions x/tokenfactory/keeper/createdenom.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,27 @@ func (k Keeper) validateUpdateDenom(ctx sdk.Context, msg *types.MsgUpdateDenom)
return "", types.ErrDenomDoesNotExist.Wrapf("denom: %s", msg.GetDenom())
}

err = k.validateAllowList(msg.AllowList)
err = k.validateAllowList(ctx, msg.AllowList)
if err != nil {
return "", err
}

return msg.GetDenom(), nil
}

func (k Keeper) validateAllowListSize(allowList *banktypes.AllowList) error {
func (k Keeper) validateAllowListSize(ctx sdk.Context, allowList *banktypes.AllowList) error {
if allowList == nil {
return types.ErrAllowListUndefined
}

if len(allowList.Addresses) > k.config.DenomAllowListMaxSize {
if len(allowList.Addresses) > int(k.GetDenomAllowListMaxSize(ctx)) {
return types.ErrAllowListTooLarge
}
return nil
}

func (k Keeper) validateAllowList(allowList *banktypes.AllowList) error {
err := k.validateAllowListSize(allowList)
func (k Keeper) validateAllowList(ctx sdk.Context, allowList *banktypes.AllowList) error {
err := k.validateAllowListSize(ctx, allowList)
if err != nil {
return err
}
Expand Down
36 changes: 14 additions & 22 deletions x/tokenfactory/keeper/createdenom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper_test

import (
"fmt"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -51,6 +50,9 @@ func (suite *KeeperTestSuite) TestMsgCreateDenom() {
}

func (suite *KeeperTestSuite) TestCreateDenom() {
params, _ := suite.queryClient.Params(suite.Ctx.Context(), &types.QueryParamsRequest{})
allowListSize := params.Params.DenomAllowlistMaxSize
largeAllowList := make([]string, allowListSize+1)
for _, tc := range []struct {
desc string
setup func()
Expand Down Expand Up @@ -99,16 +101,10 @@ func (suite *KeeperTestSuite) TestCreateDenom() {
valid: false,
},
{
desc: "list is too large",
subdenom: "test",
allowList: &banktypes.AllowList{
Addresses: []string{
suite.TestAccs[0].String(),
suite.TestAccs[1].String(),
suite.TestAccs[2].String(),
suite.TestAccs[2].String()},
},
valid: false,
desc: "list is too large",
subdenom: "test",
allowList: &banktypes.AllowList{Addresses: largeAllowList},
valid: false,
},
} {
suite.Run(fmt.Sprintf("Case %s", tc.desc), func() {
Expand Down Expand Up @@ -158,6 +154,9 @@ func (suite *KeeperTestSuite) TestCreateDenom() {
}

func (suite *KeeperTestSuite) TestUpdateDenom() {
params, _ := suite.queryClient.Params(suite.Ctx.Context(), &types.QueryParamsRequest{})
allowListSize := params.Params.DenomAllowlistMaxSize
largeAllowList := make([]string, allowListSize+1)
for _, tc := range []struct {
desc string
setup func()
Expand Down Expand Up @@ -222,17 +221,10 @@ func (suite *KeeperTestSuite) TestUpdateDenom() {
types.NewMsgCreateDenom(suite.TestAccs[0].String(), "TLRG"))
suite.Require().NoError(err)
},
denom: fmt.Sprintf("factory/%s/TLRG", suite.TestAccs[0].String()),
allowList: &banktypes.AllowList{
Addresses: []string{
suite.TestAccs[0].String(),
suite.TestAccs[1].String(),
suite.TestAccs[2].String(),
suite.TestAccs[2].String(),
},
},
valid: false,
errMsg: "allowlist too large",
denom: fmt.Sprintf("factory/%s/TLRG", suite.TestAccs[0].String()),
allowList: &banktypes.AllowList{Addresses: largeAllowList},
valid: false,
errMsg: "allowlist too large",
},
{
desc: "denom having invalid characters",
Expand Down
11 changes: 6 additions & 5 deletions x/tokenfactory/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type (
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistrKeeper

config Config
}
)

Expand All @@ -37,7 +35,6 @@ func NewKeeper(
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
distrKeeper types.DistrKeeper,
config Config,
) Keeper {
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
Expand All @@ -50,8 +47,6 @@ func NewKeeper(
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,

config: config,
}
}

Expand Down Expand Up @@ -86,3 +81,9 @@ func (k Keeper) CreateModuleAccount(ctx sdk.Context) {
moduleAcc := authtypes.NewEmptyModuleAccount(types.ModuleName, authtypes.Minter, authtypes.Burner)
k.accountKeeper.SetModuleAccount(ctx, moduleAcc)
}

func (k Keeper) GetDenomAllowListMaxSize(ctx sdk.Context) uint32 {
var denomAllowListMaxSize uint32
k.paramSpace.Get(ctx, types.DenomAllowListMaxSizeKey, &denomAllowListMaxSize)
return denomAllowListMaxSize
}
9 changes: 8 additions & 1 deletion x/tokenfactory/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewMigrator(keeper Keeper) Migrator {
// Migrate2to3 migrates from version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
// Reset params after removing the denom creation fee param
defaultParams := types.DefaultParams()
defaultParams := types.Params{}
m.keeper.paramSpace.SetParamSet(ctx, &defaultParams)

// We remove the denom creation fee whitelist in this migration
Expand Down Expand Up @@ -58,6 +58,13 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return nil
}

func (m Migrator) Migrate4to5(ctx sdk.Context) error {
// Add new params and set all to defaults
defaultParams := types.DefaultParams()
m.keeper.SetParams(ctx, defaultParams)
return nil
}

func (m Migrator) SetMetadata(denomMetadata *banktypes.Metadata) {
if len(denomMetadata.Base) == 0 {
panic(fmt.Errorf("no base exists for denom %v", denomMetadata))
Expand Down
39 changes: 37 additions & 2 deletions x/tokenfactory/keeper/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestMigrate2to3(t *testing.T) {
store.Set(oldCreatorSpecificPrefix, []byte("garbage value whitelist creator"))
require.True(t, store.Has(oldCreateDenomFeeWhitelistPrefix))
require.True(t, store.Has(oldCreatorSpecificPrefix))
newKeeper := NewKeeper(cdc, storeKey, paramsSubspace, nil, bankkeeper.NewBaseKeeper(cdc, bankstorekey, nil, paramsSubspace, nil), nil, Config{DenomAllowListMaxSize: 100})
newKeeper := NewKeeper(cdc, storeKey, paramsSubspace, nil, bankkeeper.NewBaseKeeper(cdc, bankstorekey, nil, paramsSubspace, nil), nil)
m := NewMigrator(newKeeper)
err := m.Migrate2to3(ctx)
require.Nil(t, err)
Expand All @@ -80,7 +80,7 @@ func TestMigrate2to3(t *testing.T) {
func TestMigrate3To4(t *testing.T) {
// Test migration with all metadata denom
metadata := banktypes.Metadata{Description: sdk.DefaultBondDenom, Base: sdk.DefaultBondDenom, Display: sdk.DefaultBondDenom, Name: sdk.DefaultBondDenom, Symbol: sdk.DefaultBondDenom}
keeper := NewKeeper(nil, nil, typesparams.Subspace{}, nil, nil, nil, Config{DenomAllowListMaxSize: 100})
_, keeper := getStoreAndKeeper(t)
m := NewMigrator(keeper)
m.SetMetadata(&metadata)
require.Equal(t, sdk.DefaultBondDenom, metadata.Display)
Expand All @@ -94,3 +94,38 @@ func TestMigrate3To4(t *testing.T) {
require.Equal(t, testDenom, metadata.Name)
require.Equal(t, testDenom, metadata.Symbol)
}

func TestMigrate4To5(t *testing.T) {
stateStore, keeper := getStoreAndKeeper(t)
m := NewMigrator(keeper)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
err := m.Migrate4to5(ctx)
require.NoError(t, err)
require.NotPanics(t, func() { m.keeper.GetParams(ctx) })
params := m.keeper.GetParams(ctx)
require.Equal(t, types.DefaultParams(), params)
}

func getStoreAndKeeper(t *testing.T) (store.CommitMultiStore, Keeper) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
bankStoreKey := sdk.NewKVStoreKey(banktypes.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(bankStoreKey, sdk.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, sdk.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

paramsSubspace := typesparams.NewSubspace(cdc,
codec.NewLegacyAmino(),
storeKey,
memStoreKey,
"TokenfactoryParams",
)
return stateStore, NewKeeper(nil, nil, paramsSubspace, nil, nil, nil)
}
2 changes: 1 addition & 1 deletion x/tokenfactory/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (server msgServer) CreateDenom(goCtx context.Context, msg *types.MsgCreateD
)

if msg.AllowList != nil {
err = server.validateAllowList(msg.AllowList)
err = server.validateAllowList(ctx, msg.AllowList)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion x/tokenfactory/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
_ = cfg.RegisterMigration(types.ModuleName, 1, func(ctx sdk.Context) error { return nil })
_ = cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3)
_ = cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4)
_ = cfg.RegisterMigration(types.ModuleName, 4, m.Migrate4to5)
}

// RegisterInvariants registers the x/tokenfactory module's invariants.
Expand Down Expand Up @@ -194,7 +195,7 @@ func (am AppModuleBasic) ValidateGenesisStream(cdc codec.JSONCodec, config clien
}

// ConsensusVersion implements ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 4 }
func (AppModule) ConsensusVersion() uint64 { return 5 }

// BeginBlock executes all ABCI BeginBlock logic respective to the tokenfactory module.
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down
1 change: 1 addition & 0 deletions x/tokenfactory/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
CreatorPrefixKey = "creator"
AdminPrefixKey = "admin"
CreateDenomFeeWhitelistKey = "createdenomfeewhitelist"
DenomAllowListMaxSizeKey = []byte("allowlistmaxsize")
)

// GetDenomPrefixStore returns the store prefix where all the data associated with a specific denom
Expand Down
33 changes: 27 additions & 6 deletions x/tokenfactory/types/params.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
package types

import (
"fmt"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

// ParamTable for tokenfactory module.
// DefaultDenomAllowListMaxSize default denom allowlist max size and can be overridden by governance proposal.
const DefaultDenomAllowListMaxSize = 2000

// ParamKeyTable ParamTable for tokenfactory module.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

// default tokenfactory module parameters.
// DefaultParams default tokenfactory module parameters.
func DefaultParams() Params {
return Params{}
return Params{
DenomAllowlistMaxSize: DefaultDenomAllowListMaxSize,
}
}

// validate params.
// Validate validate params.
func (p Params) Validate() error {
if err := validateDenomAllowListMaxSize(p.DenomAllowlistMaxSize); err != nil {
return err
}
return nil
}

// Implements params.ParamSet.
// ParamSetPairs Implements params.ParamSet.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{}
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(DenomAllowListMaxSizeKey, &p.DenomAllowlistMaxSize, validateDenomAllowListMaxSize),
}
}

// validateDenomAllowListMaxSize validates a parameter value is within a valid range.
func validateDenomAllowListMaxSize(i interface{}) error {
_, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
Loading

0 comments on commit 15333e5

Please sign in to comment.