Skip to content

Commit

Permalink
- limit allow list size
Browse files Browse the repository at this point in the history
- update denom checks
  • Loading branch information
dssei committed Sep 12, 2024
1 parent bdf110d commit 4f191f7
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 5 deletions.
6 changes: 6 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,19 @@ 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
29 changes: 29 additions & 0 deletions x/tokenfactory/keeper/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package keeper

import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/spf13/cast"
)

type Config struct {
DenomAllowListMaxSize int `mapstructure:"denom_allow_list_max_size"`
}

var DefaultConfig = Config{
DenomAllowListMaxSize: 2000,
}

const (
flagDenomAllowListMaxSize = "tokenfactory.denom_allow_list_max_size"
)

func ReadConfig(opts servertypes.AppOptions) (Config, error) {
cfg := DefaultConfig // copy
var err error
if v := opts.Get(flagDenomAllowListMaxSize); v != nil {
if cfg.DenomAllowListMaxSize, err = cast.ToIntE(v); err != nil {
return cfg, err
}
}
return cfg, nil
}
16 changes: 14 additions & 2 deletions x/tokenfactory/keeper/createdenom.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func (k Keeper) validateCreateDenom(ctx sdk.Context, creatorAddr string, subdeno
return denom, nil
}

func (k Keeper) validateUpdateDenom(ctx sdk.Context, creatorAddr string, subdenom string) (tokenDenom string, err error) {
denom, err := types.GetTokenDenom(creatorAddr, subdenom)
func (k Keeper) validateUpdateDenom(ctx sdk.Context, msg *types.MsgUpdateDenom) (tokenDenom string, err error) {
denom, err := types.GetTokenDenom(msg.GetSender(), msg.GetSubdenom())
if err != nil {
return "", err
}
Expand All @@ -80,5 +80,17 @@ func (k Keeper) validateUpdateDenom(ctx sdk.Context, creatorAddr string, subdeno
return "", types.ErrDenomDoesNotExist.Wrapf("denom: %s", denom)
}

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

return denom, nil
}

func (k Keeper) validateAllowListSize(allowList banktypes.AllowList) error {
if len(allowList.Addresses) > k.config.DenomAllowListMaxSize {
return types.ErrAllowListTooLarge
}
return nil
}
5 changes: 5 additions & 0 deletions x/tokenfactory/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type (
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistrKeeper

config Config
}
)

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

config: config,
}
}

Expand Down
4 changes: 2 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)
newKeeper := NewKeeper(cdc, storeKey, paramsSubspace, nil, bankkeeper.NewBaseKeeper(cdc, bankstorekey, nil, paramsSubspace, nil), nil, Config{DenomAllowListMaxSize: 100})
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)
keeper := NewKeeper(nil, nil, typesparams.Subspace{}, nil, nil, nil, Config{DenomAllowListMaxSize: 100})
m := NewMigrator(keeper)
m.SetMetadata(&metadata)
require.Equal(t, sdk.DefaultBondDenom, metadata.Display)
Expand Down
15 changes: 14 additions & 1 deletion x/tokenfactory/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (server msgServer) CreateDenom(goCtx context.Context, msg *types.MsgCreateD
)

if msg.AllowList != nil {
err = server.validateAllowListSize(*msg.AllowList)
if err != nil {
return nil, err
}
server.bankKeeper.SetDenomAllowList(ctx, denom, *msg.AllowList)
createDenomEvent = createDenomEvent.AppendAttributes(
sdk.NewAttribute(types.AttributeAllowList, strings.Join(msg.AllowList.Addresses, ",")),
Expand All @@ -53,11 +57,20 @@ func (server msgServer) CreateDenom(goCtx context.Context, msg *types.MsgCreateD
func (server msgServer) UpdateDenom(goCtx context.Context, msg *types.MsgUpdateDenom) (*types.MsgUpdateDenomResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

denom, err := server.Keeper.validateUpdateDenom(ctx, msg.Sender, msg.GetSubdenom())
denom, err := server.Keeper.validateUpdateDenom(ctx, msg)
if err != nil {
return nil, err
}

authorityMetadata, err := server.Keeper.GetAuthorityMetadata(ctx, denom)
if err != nil {
return nil, err
}

if msg.Sender != authorityMetadata.GetAdmin() {
return nil, types.ErrUnauthorized
}

updateDenomEvent := sdk.NewEvent(
types.TypeMsgUpdateDenom,
sdk.NewAttribute(types.AttributeCreator, msg.Sender),
Expand Down
1 change: 1 addition & 0 deletions x/tokenfactory/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ var (
ErrEncodingDenomAuthorityMetadata = sdkerrors.Register(ModuleName, 18, "Error encoding denom authority metadata as JSON")
ErrEncodingDenomsFromCreator = sdkerrors.Register(ModuleName, 19, "Error encoding denoms from creator as JSON")
ErrUnknownSeiTokenFactoryQuery = sdkerrors.Register(ModuleName, 23, "Error unknown sei token factory query")
ErrAllowListTooLarge = sdkerrors.Register(ModuleName, 24, "allowlist too large")
)

0 comments on commit 4f191f7

Please sign in to comment.