From 089aff838cfbde80e454d7329fe947e85fbcfdd6 Mon Sep 17 00:00:00 2001 From: jaeseung-bae <119839167+jaeseung-bae@users.noreply.github.com> Date: Sun, 17 Mar 2024 12:46:12 +0900 Subject: [PATCH] fix: add init logic of module accounts just in case (#1277) * fix: add init logic of module accounts just in case * chore: update changelog * chore: check if module account successfully created * chore: fix lint * chore: add test * chore: fix test --- CHANGELOG.md | 1 + baseapp/block_gas_test.go | 2 +- x/foundation/keeper/internal/genesis.go | 9 +++ x/foundation/keeper/internal/genesis_test.go | 58 ++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae00fbdc8d..b35b7835a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/auth, x/slashing) [\#1179](https://github.com/Finschia/finschia-sdk/pull/1179) modify missing changes of converting to tendermint * (x/auth) [#1274](https://github.com/Finschia/finschia-sdk/pull/1274) `ModuleAccount.Validate` now reports a nil `.BaseAccount` instead of panicking. * (x/collection) [\#1276](https://github.com/Finschia/finschia-sdk/pull/1276) eliminates potential risk for Insufficient Sanity Check of tokenID in Genesis +* (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 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..b5320033f6 100644 --- a/x/foundation/keeper/internal/genesis.go +++ b/x/foundation/keeper/internal/genesis.go @@ -1,6 +1,8 @@ package internal import ( + "fmt" + sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/foundation" ) @@ -48,6 +50,13 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *foundation.GenesisState) erro k.SetPool(ctx, data.Pool) + // init module accounts just in case + 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 } diff --git a/x/foundation/keeper/internal/genesis_test.go b/x/foundation/keeper/internal/genesis_test.go index 81d34636f3..fb7149c93f 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 module account=" + foundation.ModuleName: { + mockAccKeeper: &stubAccKeeper{nameToFail: foundation.ModuleName}, + }, + "failed to generate module account=" + foundation.TreasuryName: { + mockAccKeeper: &stubAccKeeper{nameToFail: 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 { + nameToFail string +} + +func (s *stubAccKeeper) GetModuleAccount(_ sdk.Context, name string) authtypes.ModuleAccountI { + if s.nameToFail == name { + return nil + } + return authtypes.NewEmptyModuleAccount("dontcare") +}