Skip to content

Commit

Permalink
fix: add init logic of module accounts just in case (#1277)
Browse files Browse the repository at this point in the history
* 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

(cherry picked from commit 089aff8)

# Conflicts:
#	CHANGELOG.md
#	x/foundation/keeper/internal/genesis_test.go
  • Loading branch information
jaeseung-bae authored and mergify[bot] committed Mar 17, 2024
1 parent 647075f commit 0129943
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

### Bug Fixes
<<<<<<< HEAD
* (x/auth) [#1281](https://github.com/Finschia/finschia-sdk/pull/1281) `ModuleAccount.Validate` now reports a nil `.BaseAccount` instead of panicking. (backport #1274)
=======
* 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/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
>>>>>>> 089aff838 (fix: add init logic of module accounts just in case (#1277))
### Removed

Expand Down
2 changes: 1 addition & 1 deletion baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,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)

Expand Down
9 changes: 9 additions & 0 deletions x/foundation/keeper/internal/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package internal

import (
"fmt"

sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/foundation"
)
Expand Down Expand Up @@ -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
}

Expand Down
61 changes: 61 additions & 0 deletions x/foundation/keeper/internal/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/Finschia/finschia-sdk/crypto/keys/secp256k1"
"github.com/Finschia/finschia-sdk/simapp"
"github.com/Finschia/finschia-sdk/testutil/testdata"
sdk "github.com/Finschia/finschia-sdk/types"
<<<<<<< HEAD

Check failure on line 15 in x/foundation/keeper/internal/genesis_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

missing import path (typecheck)

Check failure on line 15 in x/foundation/keeper/internal/genesis_test.go

View workflow job for this annotation

GitHub Actions / build (amd64, gcc)

missing import path

Check failure on line 15 in x/foundation/keeper/internal/genesis_test.go

View workflow job for this annotation

GitHub Actions / build (amd64, gcc)

missing import path

=======
authtypes "github.com/Finschia/finschia-sdk/x/auth/types"
>>>>>>> 089aff838 (fix: add init logic of module accounts just in case (#1277))
"github.com/Finschia/finschia-sdk/x/foundation"
"github.com/Finschia/finschia-sdk/x/foundation/keeper/internal"
)

func workingPolicy() foundation.DecisionPolicy {
Expand Down Expand Up @@ -286,3 +292,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")
}

0 comments on commit 0129943

Please sign in to comment.