Skip to content

Commit

Permalink
systemtests
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvubk committed Nov 14, 2024
1 parent ede3f22 commit 989643f
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 72 deletions.
2 changes: 2 additions & 0 deletions simapp/v2/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject"
lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject"
multisigdepinject "cosmossdk.io/x/accounts/defaults/multisig/depinject"
admindepinject "cosmossdk.io/x/accounts/defaults/admin/depinject"
stakingkeeper "cosmossdk.io/x/staking/keeper"
upgradekeeper "cosmossdk.io/x/upgrade/keeper"

Expand Down Expand Up @@ -55,6 +56,7 @@ func AppConfig() depinject.Config {
multisigdepinject.ProvideAccount,
basedepinject.ProvideAccount,
lockupdepinject.ProvideAllLockupAccounts,
admindepinject.ProvideAccount,

// provide base account options
basedepinject.ProvideSecp256K1PubKey,
Expand Down
2 changes: 1 addition & 1 deletion x/bank/proto/cosmos/bank/v2/authorityMetadata.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ message DenomAuthorityMetadata {
option (gogoproto.equal) = true;

// Can be empty for no admin, or a valid osmosis address
bytes admin = 1;
string admin = 1;
}
16 changes: 0 additions & 16 deletions x/bank/v2/keeper/admins.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,3 @@ func (k Keeper) setAuthorityMetadata(ctx context.Context, denom string, metadata

return k.denomAuthority.Set(ctx, denom, metadata)
}

func (k Keeper) setAdmin(ctx context.Context, denom, admin string) error {
metadata, err := k.GetAuthorityMetadata(ctx, denom)
if err != nil {
return err
}

adminAddr, err := k.addressCodec.StringToBytes(admin)
if err != nil {
return err
}

metadata.Admin = adminAddr

return k.setAuthorityMetadata(ctx, denom, metadata)
}
14 changes: 11 additions & 3 deletions x/bank/v2/keeper/createdenom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"fmt"

"cosmossdk.io/x/bank/v2/types"

admin "cosmossdk.io/x/accounts/defaults/admin"
adminv1 "cosmossdk.io/x/accounts/defaults/admin/v1"
"cosmossdk.io/x/bank/v2/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)
Expand Down Expand Up @@ -62,9 +62,17 @@ func (k Keeper) createDenomAfterValidation(ctx context.Context, creatorAddr, den

// Create denom admin account
_, accountAddr, err := k.accountsKeeper.Init(ctx, admin.Type, creatorAddrBz, msgInitAdmin, sdk.NewCoins())
if err != nil {
return err
}

accountAddrStr, err := k.addressCodec.BytesToString(accountAddr)
if err != nil {
return err
}

authorityMetadata := types.DenomAuthorityMetadata{
Admin: accountAddr,
Admin: accountAddrStr,
}
err = k.setAuthorityMetadata(ctx, denom, authorityMetadata)
if err != nil {
Expand Down
24 changes: 16 additions & 8 deletions x/bank/v2/keeper/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"google.golang.org/grpc/status"

errorsmod "cosmossdk.io/errors"
adminv1 "cosmossdk.io/x/accounts/defaults/admin/v1"
"cosmossdk.io/x/bank/v2/types"

adminv1 "cosmossdk.io/x/accounts/defaults/admin/v1"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -112,15 +112,17 @@ func (h handlers) MsgMint(ctx context.Context, msg *types.MsgMint) (*types.MsgMi
return nil, err
}

adminAccAddr := authorityMetadata.Admin
adminAccAddr, err := h.addressCodec.StringToBytes(authorityMetadata.Admin)
if err != nil {
return nil, err
}

// Query if sender have mint perm

_, err = h.accountsKeeper.Query(ctx, adminAccAddr, &adminv1.QueryMintPerm{
Sender: msg.Authority,
Denom: msg.Amount.Denom,
Denom: msg.Amount.Denom,
})

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -181,15 +183,17 @@ func (h handlers) MsgBurn(ctx context.Context, msg *types.MsgBurn) (*types.MsgBu
return nil, err
}

adminAccAddr := authorityMetadata.Admin
adminAccAddr, err := h.addressCodec.StringToBytes(authorityMetadata.Admin)
if err != nil {
return nil, err
}

// Query if sender have mint perm

_, err = h.accountsKeeper.Query(ctx, adminAccAddr, &adminv1.QueryBurnPerm{
Sender: msg.Authority,
Denom: msg.Amount.Denom,
Denom: msg.Amount.Denom,
})

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -292,7 +296,11 @@ func (h handlers) QueryDenomsFromCreator(ctx context.Context, req *types.QueryDe
denoms := []string{}

err := h.Keeper.denomAuthority.Walk(ctx, nil, func(denom string, authority types.DenomAuthorityMetadata) (stop bool, err error) {
resp, err := h.accountsKeeper.Query(ctx, authority.Admin, &adminv1.QueryOwner{})
adminAccAddr, err := h.addressCodec.StringToBytes(authority.Admin)
if err != nil {
return true, err
}
resp, err := h.accountsKeeper.Query(ctx, adminAccAddr, &adminv1.QueryOwner{})
if err != nil {
return true, err
}
Expand Down
54 changes: 28 additions & 26 deletions x/bank/v2/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
coretesting "cosmossdk.io/core/testing"
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
accountskeeper "cosmossdk.io/x/accounts"
"cosmossdk.io/x/accounts/accountstd"
"cosmossdk.io/x/accounts/defaults/admin"
adminv1 "cosmossdk.io/x/accounts/defaults/admin/v1"
"cosmossdk.io/x/bank/v2/keeper"
banktestutil "cosmossdk.io/x/bank/v2/testutil"
"cosmossdk.io/x/bank/v2/types"

accountskeeper "cosmossdk.io/x/accounts"
"cosmossdk.io/x/accounts/accountstd"
"cosmossdk.io/x/accounts/defaults/admin"
"github.com/cosmos/cosmos-sdk/codec"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -29,7 +30,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
adminv1 "cosmossdk.io/x/accounts/defaults/admin/v1"
)

const (
Expand Down Expand Up @@ -339,7 +339,10 @@ func (s *KeeperTestSuite) TestCreateDenom() {

accountsKeeper := s.bankKeeper.GetAccountsKeeper()

resp, err := accountsKeeper.Query(s.ctx, authority.Admin, &adminv1.QueryOwner{})
adminAccAddr, err := s.addressCodec.StringToBytes(authority.Admin)
require.NoError(err)

resp, err := accountsKeeper.Query(s.ctx, adminAccAddr, &adminv1.QueryOwner{})
require.NoError(err)
v1Resp, ok := resp.(*adminv1.QueryOwnerResponse)
require.True(ok)
Expand Down Expand Up @@ -465,7 +468,7 @@ func (s *KeeperTestSuite) TestMintHandler() {
ToAddress: accAddrs[1].String(),
Amount: sdk.NewCoin(barDenom, math.NewInt(100)),
},
expErr: false,
expErr: false,
adminDisable: false,
},
{
Expand All @@ -475,7 +478,7 @@ func (s *KeeperTestSuite) TestMintHandler() {
ToAddress: accAddrs[1].String(),
Amount: sdk.NewCoin(barDenom, math.NewInt(100)),
},
expErr: true,
expErr: true,
adminDisable: false,
},
{
Expand All @@ -485,7 +488,7 @@ func (s *KeeperTestSuite) TestMintHandler() {
ToAddress: accAddrs[1].String(),
Amount: sdk.NewCoin(newDenom, math.NewInt(100)),
},
expErr: false,
expErr: false,
adminDisable: false,
},
{
Expand All @@ -495,7 +498,7 @@ func (s *KeeperTestSuite) TestMintHandler() {
ToAddress: accAddrs[1].String(),
Amount: sdk.NewCoin(newDenom, math.NewInt(100)),
},
expErr: true,
expErr: true,
adminDisable: false,
},
{
Expand All @@ -505,7 +508,7 @@ func (s *KeeperTestSuite) TestMintHandler() {
ToAddress: accAddrs[1].String(),
Amount: sdk.NewCoin(newDenom, math.NewInt(100)),
},
expErr: false,
expErr: false,
adminDisable: true,
},
{
Expand All @@ -515,7 +518,7 @@ func (s *KeeperTestSuite) TestMintHandler() {
ToAddress: accAddrs[1].String(),
Amount: sdk.NewCoin(newDenom, math.NewInt(100)),
},
expErr: true,
expErr: true,
adminDisable: true,
},
{
Expand All @@ -525,12 +528,11 @@ func (s *KeeperTestSuite) TestMintHandler() {
ToAddress: accAddrs[1].String(),
Amount: sdk.NewCoin(newDenom+"s", math.NewInt(100)),
},
expErr: true,
expErr: true,
adminDisable: true,
},
} {
s.Run(fmt.Sprintf("Case %s", tc.desc), func() {

require.NoError(handler.Keeper.SetParams(s.ctx, types.NewParams(sdk.NewCoins(), 0, tc.adminDisable)))
toAddr, err := s.addressCodec.StringToBytes(tc.msg.ToAddress)
require.NoError(err)
Expand Down Expand Up @@ -575,10 +577,10 @@ func (s *KeeperTestSuite) TestBurnHandler() {
require.NoError(err)

for _, tc := range []struct {
desc string
msg *types.MsgBurn
desc string
msg *types.MsgBurn
adminDisable bool
expErr bool
expErr bool
}{
{
desc: "Burn foo denom, valid",
Expand All @@ -588,7 +590,7 @@ func (s *KeeperTestSuite) TestBurnHandler() {
Amount: sdk.NewCoin(fooDenom, math.NewInt(50)),
},
adminDisable: false,
expErr: false,
expErr: false,
},
{
desc: "Burn foo denom, invalid authority",
Expand All @@ -598,7 +600,7 @@ func (s *KeeperTestSuite) TestBurnHandler() {
Amount: sdk.NewCoin(fooDenom, math.NewInt(50)),
},
adminDisable: false,
expErr: true,
expErr: true,
},
{
desc: "Burn foo denom, insufficient funds",
Expand All @@ -608,7 +610,7 @@ func (s *KeeperTestSuite) TestBurnHandler() {
Amount: sdk.NewCoin(fooDenom, math.NewInt(200)),
},
adminDisable: false,
expErr: true,
expErr: true,
},
{
desc: "Burn bar denom, invalid denom",
Expand All @@ -618,7 +620,7 @@ func (s *KeeperTestSuite) TestBurnHandler() {
Amount: sdk.NewCoin(barDenom, math.NewInt(50)),
},
adminDisable: false,
expErr: true,
expErr: true,
},
{
desc: "Burn tokenfactory denom, admin enable, valid",
Expand All @@ -628,7 +630,7 @@ func (s *KeeperTestSuite) TestBurnHandler() {
Amount: sdk.NewCoin(newDenom, math.NewInt(50)),
},
adminDisable: false,
expErr: false,
expErr: false,
},
{
desc: "Burn tokenfactory denom, admin enable, invalid admin",
Expand All @@ -638,7 +640,7 @@ func (s *KeeperTestSuite) TestBurnHandler() {
Amount: sdk.NewCoin(newDenom, math.NewInt(50)),
},
adminDisable: false,
expErr: true,
expErr: true,
},
{
desc: "Burn tokenfactory denom, admin disable, valid authority",
Expand All @@ -648,7 +650,7 @@ func (s *KeeperTestSuite) TestBurnHandler() {
Amount: sdk.NewCoin(newDenom, math.NewInt(50)),
},
adminDisable: true,
expErr: false,
expErr: false,
},
{
desc: "Burn tokenfactory denom, admin disable, invalid authority",
Expand All @@ -658,7 +660,7 @@ func (s *KeeperTestSuite) TestBurnHandler() {
Amount: sdk.NewCoin(newDenom, math.NewInt(50)),
},
adminDisable: true,
expErr: true,
expErr: true,
},
{
desc: "Burn tokenfactory denom, insufficient funds",
Expand All @@ -668,7 +670,7 @@ func (s *KeeperTestSuite) TestBurnHandler() {
Amount: sdk.NewCoin(newDenom, math.NewInt(150)),
},
adminDisable: false,
expErr: true,
expErr: true,
},
{
desc: "Burn tokenfactory denom, token not exist",
Expand All @@ -678,7 +680,7 @@ func (s *KeeperTestSuite) TestBurnHandler() {
Amount: sdk.NewCoin(newDenom+"s", math.NewInt(50)),
},
adminDisable: false,
expErr: true,
expErr: true,
},
} {
s.Run(fmt.Sprintf("Case %s", tc.desc), func() {
Expand Down
3 changes: 1 addition & 2 deletions x/bank/v2/types/authorityMetadata.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package types

import (
"bytes"
"errors"
)

func (metadata DenomAuthorityMetadata) Validate() error {
if bytes.Equal(metadata.Admin, []byte{}) {
if metadata.Admin == "" {
return errors.New("empty admin")
}
return nil
Expand Down
Loading

0 comments on commit 989643f

Please sign in to comment.