Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start global capability index at 1 #6047

Merged
merged 3 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,11 @@ func NewSimApp(

// NOTE: The genutils moodule must occur after staking so that pools are
// properly initialized with tokens from genesis accounts.
// NOTE: Capability module must occur first so that it can initialize any capabilities
// so that other modules that want to create or claim capabilities afterwards in InitChain
// can do so safely.
app.mm.SetOrderInitGenesis(
auth.ModuleName, distr.ModuleName, staking.ModuleName, bank.ModuleName,
capability.ModuleName, auth.ModuleName, distr.ModuleName, staking.ModuleName, bank.ModuleName,
slashing.ModuleName, gov.ModuleName, mint.ModuleName, crisis.ModuleName,
ibc.ModuleName, genutil.ModuleName, evidence.ModuleName, transfer.ModuleName,
)
Expand Down Expand Up @@ -338,6 +341,8 @@ func NewSimApp(
// Initialize and seal the capability keeper so all persistent capabilities
// are loaded in-memory and prevent any further modules from creating scoped
// sub-keepers.
// This must be done during creation of baseapp rather than in InitChain so
// that in-memory capabilities get regenerated on app restart
ctx := app.BaseApp.NewContext(true, abci.Header{})
app.CapabilityKeeper.InitializeAndSeal(ctx)

Expand Down
12 changes: 12 additions & 0 deletions x/capability/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ func (k *Keeper) InitializeAndSeal(ctx sdk.Context) {
k.sealed = true
}

// InitializeIndex 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
}
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyIndex, types.IndexToKey(1))
}

// GetLatestIndex returns the latest index of the CapabilityKeeper
func (k Keeper) GetLatestIndex(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
Expand Down
12 changes: 12 additions & 0 deletions x/capability/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type KeeperTestSuite struct {
suite.Suite

ctx sdk.Context
app *simapp.SimApp
keeper *keeper.Keeper
}

Expand All @@ -32,6 +33,7 @@ func (suite *KeeperTestSuite) SetupTest() {
// create new keeper so we can define custom scoping before init and seal
keeper := keeper.NewKeeper(cdc, app.GetKey(capability.StoreKey), app.GetMemKey(capability.MemStoreKey))

suite.app = app
suite.ctx = app.BaseApp.NewContext(checkTx, abci.Header{Height: 1})
suite.keeper = keeper
}
Expand Down Expand Up @@ -93,6 +95,16 @@ func (suite *KeeperTestSuite) TestNewCapability() {
suite.Require().Nil(cap)
}

func (suite *KeeperTestSuite) TestOriginalCapabilityKeeper() {
got, ok := suite.app.ScopedIBCKeeper.GetCapability(suite.ctx, "invalid")
suite.Require().False(ok)
suite.Require().Nil(got)

port, ok := suite.app.ScopedIBCKeeper.GetCapability(suite.ctx, "ports/transfer")
suite.Require().True(ok)
suite.Require().NotNil(port)
}

func (suite *KeeperTestSuite) TestAuthenticateCapability() {
sk1 := suite.keeper.ScopeToModule(bank.ModuleName)
sk2 := suite.keeper.ScopeToModule(staking.ModuleName)
Expand Down
5 changes: 4 additions & 1 deletion x/capability/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// InitGenesis performs the capability module's genesis initialization It returns
// no validator updates.
func (am AppModule) InitGenesis(_ sdk.Context, _ codec.JSONMarshaler, _ json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx sdk.Context, _ codec.JSONMarshaler, _ json.RawMessage) []abci.ValidatorUpdate {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this be run after port binding in app.go? That seems problematic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... never mind - however, somehow, this is not being called

// Initialize global index to 1
am.keeper.InitializeIndex(ctx)

return []abci.ValidatorUpdate{}
}

Expand Down