Skip to content

Commit

Permalink
Fix capabilities issue (cosmos#6057)
Browse files Browse the repository at this point in the history
* Fix capabilities issue

* Update x/capability/module.go

quick doc fix

* Address PR comments

Co-authored-by: Aditya <adityasripal@gmail.com>
  • Loading branch information
jackzampolin and AdityaSripal authored Apr 22, 2020
1 parent b737e7f commit f82bc19
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
12 changes: 4 additions & 8 deletions x/capability/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,12 @@ func (k *Keeper) InitializeAndSeal(ctx sdk.Context) {
k.sealed = true
}

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

// GetLatestIndex returns the latest index of the CapabilityKeeper
Expand Down
19 changes: 13 additions & 6 deletions x/capability/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/capability/types"

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

// DefaultGenesis returns the capability module's default genesis state.
func (AppModuleBasic) DefaultGenesis(_ codec.JSONMarshaler) json.RawMessage { return []byte("{}") }
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesis())
}

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

// InitGenesis performs the capability module's genesis initialization It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, _ codec.JSONMarshaler, _ json.RawMessage) []abci.ValidatorUpdate {
// Initialize global index to 1
am.keeper.InitializeIndex(ctx)
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, gs json.RawMessage) []abci.ValidatorUpdate {
var genState types.GenesisState
// Initialize global index to index in genesis state
cdc.MustUnmarshalJSON(gs, &genState)

am.keeper.SetIndex(ctx, genState.Index)

return []abci.ValidatorUpdate{}
}

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

// BeginBlock executes all ABCI BeginBlock logic respective to the capability module.
Expand Down
11 changes: 11 additions & 0 deletions x/capability/types/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package types

// GenesisState represents the Capability module genesis state
type GenesisState struct {
Index uint64 `json:"index" yaml:"index"`
}

// DefaultGenesis returns the default Capability genesis state
func DefaultGenesis() GenesisState {
return GenesisState{Index: 1}
}

0 comments on commit f82bc19

Please sign in to comment.