Skip to content

Commit f82bc19

Browse files
Fix capabilities issue (#6057)
* Fix capabilities issue * Update x/capability/module.go quick doc fix * Address PR comments Co-authored-by: Aditya <adityasripal@gmail.com>
1 parent b737e7f commit f82bc19

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

x/capability/keeper/keeper.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,12 @@ func (k *Keeper) InitializeAndSeal(ctx sdk.Context) {
130130
k.sealed = true
131131
}
132132

133-
// InitializeIndex sets the index to one in InitChain
133+
// SetIndex sets the index to one in InitChain
134134
// Since it is an exported function, we check that index is indeed unset, before initializing
135-
func (k Keeper) InitializeIndex(ctx sdk.Context) {
136-
// set the global index to start at 1 if it is unset
137-
index := k.GetLatestIndex(ctx)
138-
if index != 0 {
139-
return
140-
}
135+
func (k Keeper) SetIndex(ctx sdk.Context, index uint64) {
136+
// set the global index to the passed index
141137
store := ctx.KVStore(k.storeKey)
142-
store.Set(types.KeyIndex, types.IndexToKey(1))
138+
store.Set(types.KeyIndex, types.IndexToKey(index))
143139
}
144140

145141
// GetLatestIndex returns the latest index of the CapabilityKeeper

x/capability/module.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/cosmos/cosmos-sdk/codec"
88
sdk "github.com/cosmos/cosmos-sdk/types"
99
"github.com/cosmos/cosmos-sdk/types/module"
10+
"github.com/cosmos/cosmos-sdk/x/capability/types"
1011

1112
"github.com/gorilla/mux"
1213
"github.com/spf13/cobra"
@@ -44,7 +45,9 @@ func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
4445
}
4546

4647
// DefaultGenesis returns the capability module's default genesis state.
47-
func (AppModuleBasic) DefaultGenesis(_ codec.JSONMarshaler) json.RawMessage { return []byte("{}") }
48+
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage {
49+
return cdc.MustMarshalJSON(types.DefaultGenesis())
50+
}
4851

4952
// ValidateGenesis performs genesis state validation for the capability module.
5053
func (AppModuleBasic) ValidateGenesis(_ codec.JSONMarshaler, _ json.RawMessage) error { return nil }
@@ -98,16 +101,20 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
98101

99102
// InitGenesis performs the capability module's genesis initialization It returns
100103
// no validator updates.
101-
func (am AppModule) InitGenesis(ctx sdk.Context, _ codec.JSONMarshaler, _ json.RawMessage) []abci.ValidatorUpdate {
102-
// Initialize global index to 1
103-
am.keeper.InitializeIndex(ctx)
104+
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, gs json.RawMessage) []abci.ValidatorUpdate {
105+
var genState types.GenesisState
106+
// Initialize global index to index in genesis state
107+
cdc.MustUnmarshalJSON(gs, &genState)
108+
109+
am.keeper.SetIndex(ctx, genState.Index)
104110

105111
return []abci.ValidatorUpdate{}
106112
}
107113

108114
// ExportGenesis returns the capability module's exported genesis state as raw JSON bytes.
109-
func (am AppModule) ExportGenesis(_ sdk.Context, cdc codec.JSONMarshaler) json.RawMessage {
110-
return am.DefaultGenesis(cdc)
115+
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json.RawMessage {
116+
index := am.keeper.GetLatestIndex(ctx)
117+
return cdc.MustMarshalJSON(types.GenesisState{index})
111118
}
112119

113120
// BeginBlock executes all ABCI BeginBlock logic respective to the capability module.

x/capability/types/genesis.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package types
2+
3+
// GenesisState represents the Capability module genesis state
4+
type GenesisState struct {
5+
Index uint64 `json:"index" yaml:"index"`
6+
}
7+
8+
// DefaultGenesis returns the default Capability genesis state
9+
func DefaultGenesis() GenesisState {
10+
return GenesisState{Index: 1}
11+
}

0 commit comments

Comments
 (0)