Skip to content

Commit

Permalink
update denom tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dssei committed Sep 12, 2024
1 parent 4a91cc3 commit 4d143d1
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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 @@ -55,6 +56,9 @@ func (t TestAppOpts) Get(s string) interface{} {
if s == FlagSCEnable {
return t.useSc
}
if s == tokenfactorykeeper.FlagDenomAllowListMaxSize {
return 3
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion x/tokenfactory/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func NewUpdateDenomCmd() *cobra.Command {
msg := types.NewMsgUpdateDenom(
clientCtx.GetFromAddress().String(),
args[0],
allowList,
&allowList,
)

Check warning on line 141 in x/tokenfactory/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/tokenfactory/client/cli/tx.go#L136-L141

Added lines #L136 - L141 were not covered by tests
return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
Expand Down
4 changes: 2 additions & 2 deletions x/tokenfactory/keeper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ var DefaultConfig = Config{
}

const (
flagDenomAllowListMaxSize = "tokenfactory.denom_allow_list_max_size"
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 v := opts.Get(FlagDenomAllowListMaxSize); v != nil {
if cfg.DenomAllowListMaxSize, err = cast.ToIntE(v); err != nil {
return cfg, err
}

Check warning on line 26 in x/tokenfactory/keeper/config.go

View check run for this annotation

Codecov / codecov/patch

x/tokenfactory/keeper/config.go#L25-L26

Added lines #L25 - L26 were not covered by tests
Expand Down
4 changes: 4 additions & 0 deletions x/tokenfactory/keeper/createdenom.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (k Keeper) validateUpdateDenom(ctx sdk.Context, msg *types.MsgUpdateDenom)
}

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

if len(allowList.Addresses) > k.config.DenomAllowListMaxSize {
return types.ErrAllowListTooLarge
}
Expand Down
156 changes: 154 additions & 2 deletions x/tokenfactory/keeper/createdenom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (suite *KeeperTestSuite) TestCreateDenom() {
desc: "valid allow list",
subdenom: "withallowlist",
allowList: &banktypes.AllowList{
Addresses: []string{suite.TestAccs[0].String(), suite.TestAccs[1].String()},
Addresses: []string{suite.TestAccs[0].String(), suite.TestAccs[1].String(), suite.TestAccs[2].String()},
},
valid: true,
},
Expand All @@ -98,6 +98,18 @@ 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,
},
} {
suite.Run(fmt.Sprintf("Case %s", tc.desc), func() {
if tc.setup != nil {
Expand Down Expand Up @@ -136,10 +148,150 @@ func (suite *KeeperTestSuite) TestCreateDenom() {
Denom: res.GetNewTokenDenom(),
})
suite.Require().NoError(err)
suite.Require().Equal(tc.allowList, allowListRes.AllowList)
suite.Require().Equal(tc.allowList, &allowListRes.AllowList)
}
} else {
suite.Require().Error(err)
}
})
}
}

func (suite *KeeperTestSuite) TestUpdateDenom() {
for _, tc := range []struct {
desc string
setup func()
sender string
subdenom string
allowList *banktypes.AllowList // Ensure this is the correct type for your allow list
valid bool
errMsg string
}{
{
desc: "subdenom too long",
subdenom: "assadsadsadasdasdsadsadsadsadsadsadsklkadaskkkdasdasedskhanhassyeunganassfnlksdflksafjlkasd",
valid: false,
errMsg: "subdenom too long, max length is 44 bytes",
},
{
desc: "denom does not exist",
subdenom: "nonexistent",
valid: false,
errMsg: fmt.Sprintf("denom: factory/%s/nonexistent: denom does not exist",
suite.TestAccs[0].String()),
},
{
desc: "denom allow list can be updated",
setup: func() {
_, err := suite.msgServer.CreateDenom(sdk.WrapSDKContext(suite.Ctx),
types.NewMsgCreateDenom(suite.TestAccs[0].String(), "UPD"))
suite.Require().NoError(err)
},
subdenom: "UPD",
allowList: &banktypes.AllowList{
Addresses: []string{suite.TestAccs[0].String(), suite.TestAccs[1].String()},
},
valid: true,
},
{
desc: "denom allow list can be updated with empty list",
setup: func() {
_, err := suite.msgServer.CreateDenom(sdk.WrapSDKContext(suite.Ctx),
types.NewMsgCreateDenom(suite.TestAccs[0].String(), "EMPT"))
suite.Require().NoError(err)
},
subdenom: "EMPT",
allowList: &banktypes.AllowList{},
valid: true,
},
{
desc: "error if allow list is undefined",
setup: func() {
_, err := suite.msgServer.CreateDenom(sdk.WrapSDKContext(suite.Ctx),
types.NewMsgCreateDenom(suite.TestAccs[0].String(), "UND"))
suite.Require().NoError(err)
},
subdenom: "UND",
allowList: nil,
valid: false,
errMsg: "allowlist undefined",
},
{
desc: "error if allow list is too large",
setup: func() {
_, err := suite.msgServer.CreateDenom(sdk.WrapSDKContext(suite.Ctx),
types.NewMsgCreateDenom(suite.TestAccs[0].String(), "TLRG"))
suite.Require().NoError(err)
},
subdenom: "TLRG",
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",
},
{
desc: "subdenom having invalid characters",
subdenom: "bit/***///&&&/coin",
valid: false,
errMsg: fmt.Sprintf("invalid denom: factory/%s/bit/***///&&&/coin", suite.TestAccs[0].String()),
},
{
desc: "invalid allow list with invalid address",
setup: func() {
_, err := suite.msgServer.CreateDenom(sdk.WrapSDKContext(suite.Ctx), types.NewMsgCreateDenom(suite.TestAccs[0].String(), "invalidallowlist"))
suite.Require().NoError(err)
},
subdenom: "invalidallowlist",
allowList: &banktypes.AllowList{
Addresses: []string{"invalid_address"},
},
valid: false,
errMsg: "invalid address invalid_address: decoding bech32 failed: invalid separator index -1",
},
{
desc: "sender is not the admin",
setup: func() {
_, err := suite.msgServer.CreateDenom(sdk.WrapSDKContext(suite.Ctx), types.NewMsgCreateDenom(suite.TestAccs[0].String(), "SND"))
suite.Require().NoError(err)
},
subdenom: "SND",
sender: suite.TestAccs[1].String(),
allowList: &banktypes.AllowList{},
valid: false,
errMsg: fmt.Sprintf("denom: factory/%s/SND: denom does not exist", suite.TestAccs[1].String()),
},
} {
suite.Run(fmt.Sprintf("Case %s", tc.desc), func() {
if tc.setup != nil {
tc.setup()
}
if tc.sender == "" {
tc.sender = suite.TestAccs[0].String()
}
msg := types.NewMsgUpdateDenom(tc.sender, tc.subdenom, tc.allowList)

// Update a denom
_, err := suite.msgServer.UpdateDenom(sdk.WrapSDKContext(suite.Ctx), msg)
if tc.valid {
suite.Require().NoError(err)

// Verify the allow list if provided
if tc.allowList != nil {
allowListRes, err := suite.queryClient.DenomAllowList(suite.Ctx.Context(), &types.QueryDenomAllowListRequest{
Denom: fmt.Sprintf("factory/%s/%s", suite.TestAccs[0].String(), tc.subdenom),
})
suite.Require().NoError(err)
suite.Require().Equal(tc.allowList, &allowListRes.AllowList)
}
} else {
suite.Require().Error(err)
suite.Require().Equal(tc.errMsg, err.Error())
}
})
}
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 @@ -30,4 +30,5 @@ var (
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")
ErrAllowListUndefined = sdkerrors.Register(ModuleName, 25, "allowlist undefined")
)
4 changes: 2 additions & 2 deletions x/tokenfactory/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func (m MsgCreateDenom) GetSigners() []sdk.AccAddress {
var _ sdk.Msg = &MsgUpdateDenom{}

// NewMsgUpdateDenom creates a msg to update denom
func NewMsgUpdateDenom(sender, subdenom string, allowList banktypes.AllowList) *MsgUpdateDenom {
func NewMsgUpdateDenom(sender, subdenom string, allowList *banktypes.AllowList) *MsgUpdateDenom {

Check failure on line 57 in x/tokenfactory/types/msgs.go

View workflow job for this annotation

GitHub Actions / forward-compatibility

undefined: banktypes.AllowList
return &MsgUpdateDenom{
Sender: sender,
Subdenom: subdenom,
AllowList: &allowList,
AllowList: allowList,
}

Check warning on line 62 in x/tokenfactory/types/msgs.go

View check run for this annotation

Codecov / codecov/patch

x/tokenfactory/types/msgs.go#L57-L62

Added lines #L57 - L62 were not covered by tests
}

Expand Down

0 comments on commit 4d143d1

Please sign in to comment.