From 9857302cd9ecf8fa002eb50e582665a6fe04baa2 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Tue, 12 Mar 2024 21:10:24 +0900 Subject: [PATCH 1/6] fix: add init logic of module accounts just in case --- baseapp/block_gas_test.go | 2 +- x/foundation/keeper/internal/genesis.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/baseapp/block_gas_test.go b/baseapp/block_gas_test.go index c831fb21ba..933dde385a 100644 --- a/baseapp/block_gas_test.go +++ b/baseapp/block_gas_test.go @@ -100,7 +100,7 @@ func TestBaseApp_BlockGas(t *testing.T) { txBuilder.SetFeeAmount(feeAmount) txBuilder.SetGasLimit(txtypes.MaxGasWanted) // tx validation checks that gasLimit can't be bigger than this - privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{6}, []uint64{0} + privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{8}, []uint64{0} _, txBytes, err := createTestTx(encCfg.TxConfig, txBuilder, privs, accNums, accSeqs, ctx.ChainID()) require.NoError(t, err) diff --git a/x/foundation/keeper/internal/genesis.go b/x/foundation/keeper/internal/genesis.go index a88d6fa6a6..0819dc3cdd 100644 --- a/x/foundation/keeper/internal/genesis.go +++ b/x/foundation/keeper/internal/genesis.go @@ -48,6 +48,9 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *foundation.GenesisState) erro k.SetPool(ctx, data.Pool) + // init module accounts just in case + _ = k.authKeeper.GetModuleAccount(ctx, foundation.ModuleName) + _ = k.authKeeper.GetModuleAccount(ctx, foundation.TreasuryName) return nil } From a29fbc395575d7a7b39c5f7dddde58d1021637b6 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Tue, 12 Mar 2024 21:30:48 +0900 Subject: [PATCH 2/6] chore: update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c5c5569f4..3211152edf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes * chore(deps) [\#1141](https://github.com/Finschia/finschia-sdk/pull/1141) Bump github.com/cosmos/ledger-cosmos-go from 0.12.2 to 0.13.2 to fix ledger signing issue * (x/auth, x/slashing) [\#1179](https://github.com/Finschia/finschia-sdk/pull/1179) modify missing changes of converting to tendermint +* (x/foundation) [\#1277](https://github.com/Finschia/finschia-sdk/pull/1277) add init logic of foundation module accounts to InitGenesis in order to eliminate potential panic + ### Removed From d9118837212b60cc49949d44ca699384474e5ecc Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 15 Mar 2024 18:00:28 +0900 Subject: [PATCH 3/6] chore: check if module account successfully created --- x/foundation/keeper/internal/genesis.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/x/foundation/keeper/internal/genesis.go b/x/foundation/keeper/internal/genesis.go index 0819dc3cdd..13e2f17d87 100644 --- a/x/foundation/keeper/internal/genesis.go +++ b/x/foundation/keeper/internal/genesis.go @@ -1,7 +1,10 @@ package internal import ( + "fmt" + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/x/foundation" ) @@ -49,8 +52,12 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *foundation.GenesisState) erro k.SetPool(ctx, data.Pool) // init module accounts just in case - _ = k.authKeeper.GetModuleAccount(ctx, foundation.ModuleName) - _ = k.authKeeper.GetModuleAccount(ctx, foundation.TreasuryName) + if acc := k.authKeeper.GetModuleAccount(ctx, foundation.ModuleName); acc == nil { + panic(fmt.Sprintf("failed to create module account=%s", foundation.ModuleName)) + } + if acc := k.authKeeper.GetModuleAccount(ctx, foundation.TreasuryName); acc == nil { + panic(fmt.Sprintf("failed to create module account=%s", foundation.TreasuryName)) + } return nil } From ea7112f93f591fc6f224db20d6921a0a5babc687 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 15 Mar 2024 18:24:59 +0900 Subject: [PATCH 4/6] chore: fix lint --- x/foundation/keeper/internal/genesis.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/foundation/keeper/internal/genesis.go b/x/foundation/keeper/internal/genesis.go index 13e2f17d87..b5320033f6 100644 --- a/x/foundation/keeper/internal/genesis.go +++ b/x/foundation/keeper/internal/genesis.go @@ -4,7 +4,6 @@ import ( "fmt" sdk "github.com/Finschia/finschia-sdk/types" - "github.com/Finschia/finschia-sdk/x/foundation" ) From d48e062de3b8c9ab565cdcb8d5b927ad3b28a047 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 15 Mar 2024 19:56:08 +0900 Subject: [PATCH 5/6] chore: add test --- x/foundation/keeper/internal/genesis_test.go | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/x/foundation/keeper/internal/genesis_test.go b/x/foundation/keeper/internal/genesis_test.go index 81d34636f3..66c1822937 100644 --- a/x/foundation/keeper/internal/genesis_test.go +++ b/x/foundation/keeper/internal/genesis_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -11,7 +12,9 @@ import ( "github.com/Finschia/finschia-sdk/simapp" "github.com/Finschia/finschia-sdk/testutil/testdata" sdk "github.com/Finschia/finschia-sdk/types" + authtypes "github.com/Finschia/finschia-sdk/x/auth/types" "github.com/Finschia/finschia-sdk/x/foundation" + "github.com/Finschia/finschia-sdk/x/foundation/keeper/internal" ) func workingPolicy() foundation.DecisionPolicy { @@ -285,3 +288,58 @@ func TestImportExportGenesis(t *testing.T) { require.Equal(t, tc.export, actual, name) } } + +func TestShouldPanicWhenFailToGenerateFoundationModuleAccountInInitGenesis(t *testing.T) { + checkTx := false + app := simapp.Setup(checkTx) + testdata.RegisterInterfaces(app.InterfaceRegistry()) + testdata.RegisterMsgServer(app.MsgServiceRouter(), testdata.MsgServerImpl{}) + gs := &foundation.GenesisState{ + Params: foundation.DefaultParams(), + Foundation: foundation.DefaultFoundation(), + } + ctx := app.BaseApp.NewContext(checkTx, tmproto.Header{}) + + testCases := map[string]struct { + mockAccKeeper *stubAccKeeper + }{ + "failed to generate foundation module account=" + foundation.ModuleName: { + mockAccKeeper: &stubAccKeeper{name: foundation.ModuleName}, + }, + "failed to generate foundation module account=" + foundation.TreasuryName: { + mockAccKeeper: &stubAccKeeper{name: foundation.TreasuryName}, + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + assert.Panics(t, func() { + k := internal.NewKeeper( + app.AppCodec(), + app.GetKey(foundation.ModuleName), + app.MsgServiceRouter(), + tc.mockAccKeeper, + app.BankKeeper, + authtypes.FeeCollectorName, + foundation.DefaultConfig(), + foundation.DefaultAuthority().String(), + app.GetSubspace(foundation.ModuleName), + ) + + _ = k.InitGenesis(ctx, gs) + assert.FailNow(t, "not supposed to reach here, should panic before") + }) + }) + } +} + +type stubAccKeeper struct { + name string +} + +func (s *stubAccKeeper) GetModuleAccount(_ sdk.Context, name string) authtypes.ModuleAccountI { + if s.name == name { + return authtypes.NewEmptyModuleAccount("dontcare") + } + return nil +} From feb9962f650d0bb542c772096c8909ceb98953d5 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Fri, 15 Mar 2024 20:36:50 +0900 Subject: [PATCH 6/6] chore: fix test --- x/foundation/keeper/internal/genesis_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/x/foundation/keeper/internal/genesis_test.go b/x/foundation/keeper/internal/genesis_test.go index 66c1822937..fb7149c93f 100644 --- a/x/foundation/keeper/internal/genesis_test.go +++ b/x/foundation/keeper/internal/genesis_test.go @@ -303,11 +303,11 @@ func TestShouldPanicWhenFailToGenerateFoundationModuleAccountInInitGenesis(t *te testCases := map[string]struct { mockAccKeeper *stubAccKeeper }{ - "failed to generate foundation module account=" + foundation.ModuleName: { - mockAccKeeper: &stubAccKeeper{name: foundation.ModuleName}, + "failed to generate module account=" + foundation.ModuleName: { + mockAccKeeper: &stubAccKeeper{nameToFail: foundation.ModuleName}, }, - "failed to generate foundation module account=" + foundation.TreasuryName: { - mockAccKeeper: &stubAccKeeper{name: foundation.TreasuryName}, + "failed to generate module account=" + foundation.TreasuryName: { + mockAccKeeper: &stubAccKeeper{nameToFail: foundation.TreasuryName}, }, } @@ -334,12 +334,12 @@ func TestShouldPanicWhenFailToGenerateFoundationModuleAccountInInitGenesis(t *te } type stubAccKeeper struct { - name string + nameToFail string } func (s *stubAccKeeper) GetModuleAccount(_ sdk.Context, name string) authtypes.ModuleAccountI { - if s.name == name { - return authtypes.NewEmptyModuleAccount("dontcare") + if s.nameToFail == name { + return nil } - return nil + return authtypes.NewEmptyModuleAccount("dontcare") }