Skip to content

Commit

Permalink
update fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chixiaoxiao committed Mar 13, 2024
1 parent 7519ce8 commit aba3118
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 47 deletions.
10 changes: 6 additions & 4 deletions testutil/mock/types_mock_appmodule.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions testutil/mock/types_module_module.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 16 additions & 12 deletions types/module/core_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (c coreAppModuleAdaptor) ValidateGenesis(bz json.RawMessage) error {
}

// ExportGenesis implements HasGenesis
func (c coreAppModuleAdaptor) ExportGenesis(ctx context.Context) json.RawMessage {
func (c coreAppModuleAdaptor) ExportGenesis(ctx context.Context) (json.RawMessage, error) {
if module, ok := c.module.(appmodule.HasGenesisAuto); ok {
ctx := sdk.UnwrapSDKContext(ctx).WithGasMeter(storetypes.NewInfiniteGasMeter()) // avoid race conditions
target := genesis.RawJSONTarget{}
Expand All @@ -106,39 +106,43 @@ func (c coreAppModuleAdaptor) ExportGenesis(ctx context.Context) json.RawMessage

rawJSON, err := target.JSON()
if err != nil {
panic(err)
return nil, err
}

return rawJSON
return rawJSON, nil
}

if mod, ok := c.module.(HasABCIGenesis); ok {
return mod.ExportGenesis(ctx)
eg, err := mod.ExportGenesis(ctx)
if err != nil {
return nil, err
}
return eg, nil
}

if mod, ok := c.module.(HasGenesis); ok {
eg, err := mod.ExportGenesis(ctx)
if err != nil {
panic(err)
return nil, err
}
return eg
return eg, nil
}

return nil
return nil, nil
}

// InitGenesis implements HasGenesis
func (c coreAppModuleAdaptor) InitGenesis(ctx context.Context, bz json.RawMessage) []abci.ValidatorUpdate {
func (c coreAppModuleAdaptor) InitGenesis(ctx context.Context, bz json.RawMessage) ([]abci.ValidatorUpdate, error) {
if module, ok := c.module.(appmodule.HasGenesisAuto); ok {
// core API genesis
source, err := genesis.SourceFromRawJSON(bz)
if err != nil {
panic(err)
return nil, err
}

err = module.InitGenesis(ctx, source)
if err != nil {
panic(err)
return nil, err
}
}

Expand All @@ -149,11 +153,11 @@ func (c coreAppModuleAdaptor) InitGenesis(ctx context.Context, bz json.RawMessag
if mod, ok := c.module.(HasGenesis); ok {
err := mod.InitGenesis(ctx, bz)
if err != nil {
panic(err)
return nil, err
}

}
return nil
return nil, nil
}

// Name implements HasName
Expand Down
25 changes: 18 additions & 7 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ type HasGenesis = appmodulev2.HasGenesis
// HasABCIGenesis is the extension interface for stateful genesis methods which returns validator updates.
type HasABCIGenesis interface {
HasGenesisBasics
InitGenesis(context.Context, json.RawMessage) []abci.ValidatorUpdate
ExportGenesis(context.Context) json.RawMessage
InitGenesis(context.Context, json.RawMessage) ([]abci.ValidatorUpdate, error)
ExportGenesis(context.Context) (json.RawMessage, error)
}

// HasInvariants is the interface for registering invariants.
Expand Down Expand Up @@ -454,8 +454,10 @@ func (m *Manager) InitGenesis(ctx sdk.Context, _ codec.JSONCodec, genesisData ma
}
} else if module, ok := mod.(HasABCIGenesis); ok {
ctx.Logger().Debug("running initialization for module", "module", moduleName)
moduleValUpdates := module.InitGenesis(ctx, genesisData[moduleName])

moduleValUpdates, err := module.InitGenesis(ctx, genesisData[moduleName])
if err != nil {
return &abci.ResponseInitChain{}, err
}
// use these validator updates if provided, the module manager assumes
// only one module will update the validator set
if len(moduleValUpdates) > 0 {
Expand Down Expand Up @@ -534,8 +536,14 @@ func (m *Manager) ExportGenesisForModules(ctx sdk.Context, modulesToExport []str
} else if module, ok := mod.(HasABCIGenesis); ok {
channels[moduleName] = make(chan genesisResult)
go func(module HasABCIGenesis, ch chan genesisResult) {
ctx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()) // avoid race conditions
ch <- genesisResult{module.ExportGenesis(ctx), nil}
ctx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter())
exportGenesis, err := module.ExportGenesis(ctx)
if err != nil {
ch <- genesisResult{nil, err}
} else {
// avoid race conditions
ch <- genesisResult{exportGenesis, nil}
}
}(module, channels[moduleName])
}
}
Expand Down Expand Up @@ -688,7 +696,10 @@ func (m Manager) RunMigrations(ctx context.Context, cfg Configurator, fromVM Ver
}
}
if module, ok := m.Modules[moduleName].(HasABCIGenesis); ok {
moduleValUpdates := module.InitGenesis(sdkCtx, module.DefaultGenesis())
moduleValUpdates, err := module.InitGenesis(sdkCtx, module.DefaultGenesis())
if err != nil {
return nil, err
}
// The module manager assumes only one module will update the
// validator set, and it can't be a new module.
if len(moduleValUpdates) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion x/authz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#19490](https://github.com/cosmos/cosmos-sdk/pull/19490) `appmodule.Environment` is received on the Keeper to get access to different application services.
* [#18737](https://github.com/cosmos/cosmos-sdk/pull/18737) Update the keeper method `DequeueAndDeleteExpiredGrants` to take a limit argument for the number of grants to prune.
* [#16509](https://github.com/cosmos/cosmos-sdk/pull/16509) `AcceptResponse` has been moved to sdk/types/authz and the `Updated` field is now of the type `sdk.Msg` instead of `authz.Authorization`.
* [#19713](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic.
### Consensus Breaking Changes

* [#19188](https://github.com/cosmos/cosmos-sdk/pull/19188) Remove creation of `BaseAccount` when sending a message to an account that does not exist.
2 changes: 1 addition & 1 deletion x/bank/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#17569](https://github.com/cosmos/cosmos-sdk/pull/17569) `BurnCoins` takes an address instead of a module name
* [#19477](https://github.com/cosmos/cosmos-sdk/pull/19477) `appmodule.Environment` is passed to bank `NewKeeper`
* [#19627](https://github.com/cosmos/cosmos-sdk/pull/19627) The genesis api has been updated to match `appmodule.HasGenesis`.
* [#19713](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic.

### Consensus Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion x/distribution/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* remove `Keeper`: `IterateValidatorCurrentRewards`, `GetValidatorCurrentRewards`, `SetValidatorCurrentRewards`, `DeleteValidatorCurrentRewards`
* [#17657](https://github.com/cosmos/cosmos-sdk/pull/17657) The distribution module keeper now takes a new argument `PoolKeeper` in addition.
* [#17670](https://github.com/cosmos/cosmos-sdk/pull/17670) `AllocateTokens` takes `comet.VoteInfos` instead of `[]abci.VoteInfo`
* [#19713](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic.

### Improvements

Expand Down
10 changes: 5 additions & 5 deletions x/genutil/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ func (am AppModule) ValidateGenesis(bz json.RawMessage) error {
}

// InitGenesis performs genesis initialization for the genutil module.
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) ([]abci.ValidatorUpdate, error) {
var genesisState types.GenesisState
am.cdc.MustUnmarshalJSON(data, &genesisState)
validators, err := InitGenesis(ctx, am.stakingKeeper, am.deliverTx, genesisState, am.txEncodingConfig)
if err != nil {
panic(err)
return nil, err
}
return validators
return validators, nil
}

// ExportGenesis returns the exported genesis state as raw bytes for the genutil module.
func (am AppModule) ExportGenesis(_ context.Context) json.RawMessage {
return am.DefaultGenesis()
func (am AppModule) ExportGenesis(_ context.Context) (json.RawMessage, error) {
return am.DefaultGenesis(), nil
}

// GenTxValidator returns the genutil module's genesis transaction validator.
Expand Down
2 changes: 1 addition & 1 deletion x/gov/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#18532](https://github.com/cosmos/cosmos-sdk/pull/18532) All functions that were taking an expedited bool parameter now take a `ProposalType` parameter instead.
* [#17496](https://github.com/cosmos/cosmos-sdk/pull/17496) in `x/gov/types/v1beta1/vote.go` `NewVote` was removed, constructing the struct is required for this type.
* [#19101](https://github.com/cosmos/cosmos-sdk/pull/19101) Move `QueryProposalVotesParams` and `QueryVoteParams` from the `types/v1` package to `utils` and remove unused `querier.go` file.
* [#19713](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic.

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion x/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#19638](https://github.com/cosmos/cosmos-sdk/pull/19638) Migrate module to use `appmodule.Environment` router service so no `baseapp.MessageRouter` is required is `NewKeeper` anymore.
* [#19489](https://github.com/cosmos/cosmos-sdk/pull/19489) `appmodule.Environment` is received on the Keeper to get access to different application services.
* [#19410](https://github.com/cosmos/cosmos-sdk/pull/19410) Migrate to Store Service.
* [#19713](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic.
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic.
2 changes: 1 addition & 1 deletion x/nft/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* [#19713](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic.

## [v0.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/x/nft/v0.1.0) - 2023-11-07

Expand Down
2 changes: 1 addition & 1 deletion x/slashing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ Ref: https://keepachangelog.com/en/1.0.0/
* remove from `Keeper`: `AddPubkey`
* [#19440](https://github.com/cosmos/cosmos-sdk/pull/19440) Slashing Module creation takes `appmodule.Environment` instead of individual services
* [#19458](https://github.com/cosmos/cosmos-sdk/pull/19458) ValidatorSigningInfo.IndexOffset is deprecated, and no longer used. The index is now derived using just the StartHeight.
* [#19713](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic.

### Bug Fixes
3 changes: 2 additions & 1 deletion x/slashing/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func (s *KeeperTestSuite) TestExportAndInitGenesis() {

s.Require().NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr1, info1))
s.Require().NoError(keeper.ValidatorSigningInfo.Set(ctx, consAddr2, info2))
genesisState := keeper.ExportGenesis(ctx)
genesisState, err := keeper.ExportGenesis(ctx)
require.NoError(err)

require.Equal(genesisState.Params, testutil.TestParams())
require.Len(genesisState.SigningInfos, 2)
Expand Down
2 changes: 1 addition & 1 deletion x/staking/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#18841](https://github.com/cosmos/cosmos-sdk/pull/18841) In a undelegation or redelegation if the shares being left delegated correspond to less than 1 token (in base denom) the entire delegation gets removed.
* [#18142](https://github.com/cosmos/cosmos-sdk/pull/18142) Introduce `key_rotation_fee` param to calculate fees while rotating the keys
* [#17655](https://github.com/cosmos/cosmos-sdk/pull/17655) `HistoricalInfo` was replaced with `HistoricalRecord`, it removes the validator set and comet header and only keep what is needed for IBC.
* [#19713](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic.
12 changes: 6 additions & 6 deletions x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,25 @@ func (am AppModule) ValidateGenesis(bz json.RawMessage) error {
}

// InitGenesis performs genesis initialization for the staking module.
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) ([]abci.ValidatorUpdate, error) {
var genesisState types.GenesisState

am.cdc.MustUnmarshalJSON(data, &genesisState)
res, err := am.keeper.InitGenesis(ctx, &genesisState)
if err != nil {
return nil
return nil, err
}
return res
return res, nil
}

// ExportGenesis returns the exported genesis state as raw bytes for the staking
// module.
func (am AppModule) ExportGenesis(ctx context.Context) json.RawMessage {
func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error) {
genesis, err := am.keeper.ExportGenesis(ctx)
if err != nil {
return nil
return nil, err
}
return am.cdc.MustMarshalJSON(genesis)
return am.cdc.MustMarshalJSON(genesis), nil
}

// ConsensusVersion implements HasConsensusVersion
Expand Down

0 comments on commit aba3118

Please sign in to comment.