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

feat: decouple x/capability from simapp #12256

Merged
merged 10 commits into from
Jun 16, 2022
Prev Previous commit
updates
  • Loading branch information
julienrbrt committed Jun 16, 2022
commit 3d5e7736015a87dd70bf7cda7b9c8fba8e9dd73a
2 changes: 1 addition & 1 deletion runtime/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (a *App) Configurator() module.Configurator {
return a.configurator
}

// UnsafeFindStoreKey FindStoreKey fetches a registered StoreKey from the App in linear time.
// UnsafeFindStoreKey fetches a registered StoreKey from the App in linear time.
//
// NOTE: This should only be used in testing.
func (a *App) UnsafeFindStoreKey(storeKey string) storetypes.StoreKey {
Expand Down
40 changes: 37 additions & 3 deletions testutil/sims/app_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,30 @@ var DefaultConsensusParams = &tmproto.ConsensusParams{
// appConfig usually load from a `app.yaml` with `appconfig.LoadYAML`, defines the application configuration.
// extraOutputs defines the extra outputs to be assigned by the dependency injector (depinject).
func Setup(appConfig depinject.Config, extraOutputs ...interface{}) (*runtime.App, error) {
return SetupWithBaseAppOption(appConfig, nil, false, extraOutputs...)
}

// SetupAtGenesis initializes a new runtime.App at genesis. A Nop logger is set in runtime.App.
// appConfig usually load from a `app.yaml` with `appconfig.LoadYAML`, defines the application configuration.
// extraOutputs defines the extra outputs to be assigned by the dependency injector (depinject).
func SetupAtGenesis(appConfig depinject.Config, extraOutputs ...interface{}) (*runtime.App, error) {
return SetupWithBaseAppOption(appConfig, nil, true, extraOutputs...)
}

// SetupWithBaseAppOption initializes a new runtime.App. A Nop logger is set in runtime.App.
// appConfig usually load from a `app.yaml` with `appconfig.LoadYAML`, defines the application configuration.
// baseAppOption defines the additional operations that must be run on baseapp before app start.
// extraOutputs defines the extra outputs to be assigned by the dependency injector (depinject).
// genesis defines if the app started should already have produced block or not.
func SetupWithBaseAppOption(appConfig depinject.Config, baseAppOption runtime.BaseAppOption, genesis bool, extraOutputs ...interface{}) (*runtime.App, error) {
//
// create app
//
var appBuilder *runtime.AppBuilder
var codec codec.Codec
var (
app *runtime.App
appBuilder *runtime.AppBuilder
codec codec.Codec
)

if err := depinject.Inject(
appConfig,
Expand All @@ -68,7 +87,11 @@ func Setup(appConfig depinject.Config, extraOutputs ...interface{}) (*runtime.Ap
return nil, fmt.Errorf("failed to inject dependencies: %w", err)
}

app := appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil)
if baseAppOption != nil {
app = appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil, baseAppOption)
} else {
app = appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil)
}
if err := app.Load(true); err != nil {
return nil, fmt.Errorf("failed to load app: %w", err)
}
Expand Down Expand Up @@ -114,6 +137,17 @@ func Setup(appConfig depinject.Config, extraOutputs ...interface{}) (*runtime.Ap
},
)

// commit genesis changes
if !genesis {
app.Commit()
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{
Height: app.LastBlockHeight() + 1,
AppHash: app.LastCommitID().Hash,
ValidatorsHash: valSet.Hash(),
NextValidatorsHash: valSet.Hash(),
}})
}

return app, nil
}

Expand Down
12 changes: 10 additions & 2 deletions x/capability/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand All @@ -26,10 +27,17 @@ type CapabilityTestSuite struct {
cdc codec.Codec
ctx sdk.Context
keeper *keeper.Keeper
memKey *storetypes.MemoryStoreKey
}

func (suite *CapabilityTestSuite) SetupTest() {
app, err := simtestutil.Setup(testutil.AppConfig,
suite.memKey = storetypes.NewMemoryStoreKey("testingkey")

app, err := simtestutil.SetupWithBaseAppOption(testutil.AppConfig,
func(ba *baseapp.BaseApp) {
ba.MountStores(suite.memKey)
},
false,
&suite.cdc,
&suite.keeper,
)
Expand All @@ -49,7 +57,7 @@ func (suite *CapabilityTestSuite) TestInitializeMemStore() {
suite.Require().NotNil(cap1)

// mock statesync by creating new keeper that shares persistent state but loses in-memory map
newKeeper := keeper.NewKeeper(suite.cdc, suite.app.UnsafeFindStoreKey(types.StoreKey).(*storetypes.KVStoreKey), suite.app.UnsafeFindStoreKey(types.MemStoreKey).(*storetypes.MemoryStoreKey))
newKeeper := keeper.NewKeeper(suite.cdc, suite.app.UnsafeFindStoreKey(types.StoreKey).(*storetypes.KVStoreKey), suite.memKey)
newSk1 := newKeeper.ScopeToModule(banktypes.ModuleName)

// Mock App startup
Expand Down
2 changes: 1 addition & 1 deletion x/capability/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (suite *CapabilityTestSuite) TestGenesis() {
// create new app that does not share persistent or in-memory state
// and initialize app from exported genesis state above.
var newKeeper *keeper.Keeper
newApp, err := simtestutil.Setup(testutil.AppConfig, &newKeeper)
newApp, err := simtestutil.SetupAtGenesis(testutil.AppConfig, &newKeeper)
suite.Require().NoError(err)

newSk1 := newKeeper.ScopeToModule(banktypes.ModuleName)
Expand Down
6 changes: 0 additions & 6 deletions x/capability/spec/01_concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,3 @@ not own.

* MemStore
* KeyStore

## App Wiring

The minimal app-wiring configuration for `x/capability` is as follows:

+++ https://github.com/cosmos/cosmos-sdk/blob/main/x/capability/testutil/app.yaml
5 changes: 5 additions & 0 deletions x/capability/spec/03_app_wiring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# App Wiring

The minimal app-wiring configuration for `x/capability` is as follows:

+++ https://github.com/cosmos/cosmos-sdk/blob/main/x/capability/testutil/app.yaml
1 change: 1 addition & 0 deletions x/capability/spec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ func NewApp(...) *App {

1. **[Concepts](01_concepts.md)**
1. **[State](02_state.md)**
1. **[App Wiring](03_app_wiring.md)**
2 changes: 1 addition & 1 deletion x/capability/testutil/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ modules:
- name: capability
config:
"@type": cosmos.capability.module.v1.Module
seal_keeper: false
seal_keeper: true
15 changes: 8 additions & 7 deletions x/nft/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ func (suite *SimTestSuite) SetupTest() {

suite.app = app
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{})

suite.app.BeginBlock(abci.RequestBeginBlock{
Header: tmproto.Header{
Height: suite.app.LastBlockHeight() + 1,
AppHash: suite.app.LastCommitID().Hash,
},
})
}

func (suite *SimTestSuite) TestWeightedOperations() {
Expand Down Expand Up @@ -122,6 +115,14 @@ func (suite *SimTestSuite) TestSimulateMsgSend() {
blockTime := time.Now().UTC()
ctx := suite.ctx.WithBlockTime(blockTime)

// begin new block
suite.app.BeginBlock(abci.RequestBeginBlock{
Header: tmproto.Header{
Height: suite.app.LastBlockHeight() + 1,
AppHash: suite.app.LastCommitID().Hash,
},
})

// execute operation
registry := suite.interfaceRegistry
op := simulation.SimulateMsgSend(codec.NewProtoCodec(registry), suite.accountKeeper, suite.bankKeeper, suite.nftKeeper)
Expand Down