Skip to content

Commit 9c6750b

Browse files
committed
add checking authority and module account
1 parent 7f6ba1b commit 9c6750b

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

x/bandtss/genesis.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bandtss
22

33
import (
4+
"fmt"
5+
46
sdk "github.com/cosmos/cosmos-sdk/types"
57

68
"github.com/bandprotocol/chain/v2/x/bandtss/keeper"
@@ -9,6 +11,17 @@ import (
911

1012
// InitGenesis performs genesis initialization for this module.
1113
func InitGenesis(ctx sdk.Context, k *keeper.Keeper, data *types.GenesisState) {
14+
// check if the module account exists
15+
moduleAcc := k.GetBandtssAccount(ctx)
16+
if moduleAcc == nil {
17+
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
18+
}
19+
20+
// Set module account if its balance is zero
21+
if balance := k.GetModuleBalance(ctx); balance.IsZero() {
22+
k.SetModuleAccount(ctx, moduleAcc)
23+
}
24+
1225
if err := k.SetParams(ctx, data.Params); err != nil {
1326
panic(err)
1427
}

x/bandtss/genesis_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package bandtss_test
33
import (
44
"testing"
55

6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
68
"github.com/stretchr/testify/require"
9+
"go.uber.org/mock/gomock"
710

811
"github.com/bandprotocol/chain/v2/pkg/tss"
912
bandtesting "github.com/bandprotocol/chain/v2/testing"
@@ -16,6 +19,13 @@ func TestExportGenesis(t *testing.T) {
1619
s := testutil.NewTestSuite(t)
1720
ctx, k := s.Ctx, s.Keeper
1821

22+
s.MockAccountKeeper.EXPECT().GetModuleAccount(ctx, gomock.Any()).Return(authtypes.AccountI(&authtypes.ModuleAccount{
23+
BaseAccount: &authtypes.BaseAccount{Address: "test"},
24+
})).AnyTimes()
25+
s.MockAccountKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(sdk.AccAddress{}).AnyTimes()
26+
s.MockAccountKeeper.EXPECT().SetModuleAccount(ctx, gomock.Any()).AnyTimes()
27+
s.MockBankKeeper.EXPECT().GetAllBalances(ctx, gomock.Any()).Return(sdk.Coins{}).AnyTimes()
28+
1929
data := types.GenesisState{
2030
Params: types.DefaultParams(),
2131
Members: []types.Member{

x/bandtss/keeper/keeper.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func NewKeeper(
4545
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
4646
}
4747

48+
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
49+
panic(fmt.Errorf("invalid bandtss authority address: %w", err))
50+
}
51+
4852
return &Keeper{
4953
cdc: cdc,
5054
storeKey: storeKey,
@@ -64,6 +68,16 @@ func (k Keeper) GetBandtssAccount(ctx sdk.Context) authtypes.ModuleAccountI {
6468
return k.authKeeper.GetModuleAccount(ctx, types.ModuleName)
6569
}
6670

71+
// GetModuleBalance returns the balance of the bandtss ModuleAccount
72+
func (k Keeper) GetModuleBalance(ctx sdk.Context) sdk.Coins {
73+
return k.bankKeeper.GetAllBalances(ctx, k.GetBandtssAccount(ctx).GetAddress())
74+
}
75+
76+
// SetModuleAccount sets a module account in the account keeper.
77+
func (k Keeper) SetModuleAccount(ctx sdk.Context, acc authtypes.ModuleAccountI) {
78+
k.authKeeper.SetModuleAccount(ctx, acc)
79+
}
80+
6781
// Logger gets logger object.
6882
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
6983
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))

x/bandtss/testutil/mock_expected_keepers.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x/bandtss/types/expected_keepers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"time"
55

66
sdk "github.com/cosmos/cosmos-sdk/types"
7-
"github.com/cosmos/cosmos-sdk/x/auth/types"
7+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
88
"github.com/cosmos/cosmos-sdk/x/authz"
99
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
1010
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
@@ -33,7 +33,8 @@ type AuthzKeeper interface {
3333
// AccountKeeper defines the expected account keeper (noalias)
3434
type AccountKeeper interface {
3535
GetModuleAddress(name string) sdk.AccAddress
36-
GetModuleAccount(ctx sdk.Context, name string) types.ModuleAccountI
36+
GetModuleAccount(ctx sdk.Context, name string) authtypes.ModuleAccountI
37+
SetModuleAccount(sdk.Context, authtypes.ModuleAccountI)
3738
}
3839

3940
// BankKeeper defines the expected interface needed to retrieve account balances.

x/tss/keeper/keeper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func NewKeeper(
3131
rtr *types.Router,
3232
authority string,
3333
) *Keeper {
34+
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
35+
panic(fmt.Errorf("invalid tss authority address: %w", err))
36+
}
37+
3438
return &Keeper{
3539
cdc: cdc,
3640
storeKey: storeKey,

0 commit comments

Comments
 (0)