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>
This commit is contained in:
Jack Zampolin 2020-04-22 10:49:09 -07:00 committed by GitHub
parent b737e7f7f9
commit f82bc19b99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 14 deletions

View File

@ -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

View File

@ -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"
@ -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 }
@ -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.

View File

@ -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}
}