Skip to content

Commit

Permalink
refactor!: use injected encoding params in simapp (#12717)
Browse files Browse the repository at this point in the history
* refactor!: use injected encoding params in simapp

* add upgrading.md entry
  • Loading branch information
julienrbrt authored Jul 27, 2022
1 parent a1777a8 commit 9c51d9a
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 90 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* [#12717](https://github.com/cosmos/cosmos-sdk/pull/12717) Use injected encoding params in simapp.
* [#12702](https://github.com/cosmos/cosmos-sdk/pull/12702) Linting and tidiness, fixed two minor security warnings.
* [#12634](https://github.com/cosmos/cosmos-sdk/pull/12634) Move `sdk.Dec` to math package.
* [#12596](https://github.com/cosmos/cosmos-sdk/pull/12596) Remove all imports of the non-existent gogo/protobuf v1.3.3 to ease downstream use and go workspaces.
Expand Down
14 changes: 13 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@

This guide provides instructions for upgrading to specific versions of Cosmos SDK.

## v0.46
## [Unreleased]

### SimApp

SimApp's `app.go` is using App Wiring, the dependency injection framework of the Cosmos SDK.
This means that modules are injected directly into SimApp thanks to a [configuration file](https://github.com/cosmos/cosmos-sdk/blob/main/simapp/app_config.go).
The old behavior is preserved and still can be used, without the dependency injection framework, as shows [`app_legacy.go`](https://github.com/cosmos/cosmos-sdk/blob/main/simapp/app_legacy.go).

The constructor, `NewSimApp` has been simplified:
- `NewSimApp` does not take encoding parameters (`encodingConfig`) as input, instead the encoding parameters are injected (when using app wiring), or directly created in the constructor. Instead, we can instantiate `SimApp` for getting the encoding configuration.
- `NewSimApp` now uses `AppOptions` for getting the home path (`homePath`) and the invariant checks period (`invCheckPeriod`). These were unnecessary given as arguments as they were already present in the `AppOptions`.

## [v0.46.x](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0)

### Client Changes

Expand Down
1 change: 0 additions & 1 deletion depinject/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ func NewSimApp(
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
encodingConfig simappparams.EncodingConfig,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {
Expand Down
7 changes: 6 additions & 1 deletion docs/architecture/adr-038-state-listening.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,12 @@ e.g. in `NewSimApp`:

```go
func NewSimApp(
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, encodingConfig simappparams.EncodingConfig, appOpts servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp),
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {

...
Expand Down
18 changes: 14 additions & 4 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/store/streaming"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata_pulsar"
Expand Down Expand Up @@ -148,6 +147,7 @@ type SimApp struct {
*runtime.App
legacyAmino *codec.LegacyAmino
appCodec codec.Codec
txConfig client.TxConfig
interfaceRegistry codectypes.InterfaceRegistry

// keys to access the substores
Expand Down Expand Up @@ -186,8 +186,12 @@ func init() {

// NewSimApp returns a reference to an initialized SimApp.
func NewSimApp(
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, encodingConfig simappparams.EncodingConfig,
appOpts servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp),
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {
var (
app = &SimApp{}
Expand Down Expand Up @@ -215,6 +219,7 @@ func NewSimApp(
&appBuilder,
&app.appCodec,
&app.legacyAmino,
&app.txConfig,
&app.interfaceRegistry,
&app.AccountKeeper,
&app.BankKeeper,
Expand Down Expand Up @@ -269,7 +274,7 @@ func NewSimApp(
// app.ModuleManager.SetOrderMigrations(custom order)

app.ModuleManager.RegisterInvariants(app.CrisisKeeper)
app.ModuleManager.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino)
app.ModuleManager.RegisterRoutes(app.Router(), app.QueryRouter(), app.legacyAmino)

// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
// Make sure it's called after `app.ModuleManager` and `app.configurator` are set.
Expand Down Expand Up @@ -337,6 +342,11 @@ func (app *SimApp) InterfaceRegistry() codectypes.InterfaceRegistry {
return app.interfaceRegistry
}

// TxConfig returns SimApp's TxConfig
func (app *SimApp) TxConfig() client.TxConfig {
return app.txConfig
}

// GetKey returns the KVStoreKey for the provided store key.
//
// NOTE: This is solely to be used for testing purposes.
Expand Down
21 changes: 17 additions & 4 deletions simapp/app_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/store/streaming"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata_pulsar"
Expand Down Expand Up @@ -158,6 +157,7 @@ type SimApp struct {
*baseapp.BaseApp
legacyAmino *codec.LegacyAmino
appCodec codec.Codec
txConfig client.TxConfig
interfaceRegistry types.InterfaceRegistry

// keys to access the substores
Expand Down Expand Up @@ -204,14 +204,21 @@ func init() {

// NewSimApp returns a reference to an initialized SimApp.
func NewSimApp(
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, encodingConfig simappparams.EncodingConfig,
appOpts servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp),
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {
encodingConfig := MakeTestEncodingConfig()

appCodec := encodingConfig.Codec
legacyAmino := encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry
txConfig := encodingConfig.TxConfig

bApp := baseapp.NewBaseApp(appName, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
bApp := baseapp.NewBaseApp(appName, logger, db, txConfig.TxDecoder(), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
bApp.SetInterfaceRegistry(interfaceRegistry)
Expand All @@ -238,6 +245,7 @@ func NewSimApp(
BaseApp: bApp,
legacyAmino: legacyAmino,
appCodec: appCodec,
txConfig: txConfig,
interfaceRegistry: interfaceRegistry,
keys: keys,
tkeys: tkeys,
Expand Down Expand Up @@ -569,6 +577,11 @@ func (app *SimApp) InterfaceRegistry() types.InterfaceRegistry {
return app.interfaceRegistry
}

// TxConfig returns SimApp's TxConfig
func (app *SimApp) TxConfig() client.TxConfig {
return app.txConfig
}

// GetKey returns the KVStoreKey for the provided store key.
//
// NOTE: This is solely to be used for testing purposes.
Expand Down
28 changes: 11 additions & 17 deletions simapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ import (
)

func TestSimAppExportAndBlockedAddrs(t *testing.T) {
encCfg := MakeTestEncodingConfig()
db := dbm.NewMemDB()
logger, _ := log.NewDefaultLogger("plain", "info", false)
app := NewSimappWithCustomOptions(t, false, SetupOptions{
Logger: logger,
DB: db,
EncConfig: encCfg,
AppOpts: simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome),
Logger: logger,
DB: db,
AppOpts: simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome),
})

for acc := range maccPerms {
Expand All @@ -59,7 +57,7 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) {

logger2, _ := log.NewDefaultLogger("plain", "info", false)
// Making a new app object with the db, so that initchain hasn't been called
app2 := NewSimApp(logger2, db, nil, true, encCfg, simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome))
app2 := NewSimApp(logger2, db, nil, true, simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome))
_, err := app2.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}
Expand All @@ -71,14 +69,13 @@ func TestGetMaccPerms(t *testing.T) {

func TestRunMigrations(t *testing.T) {
db := dbm.NewMemDB()
encCfg := MakeTestEncodingConfig()
logger, _ := log.NewDefaultLogger("plain", "info", false)
app := NewSimApp(logger, db, nil, true, encCfg, simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome))
app := NewSimApp(logger, db, nil, true, simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome))

// Create a new baseapp and configurator for the purpose of this test.
bApp := baseapp.NewBaseApp(app.Name(), logger, db, encCfg.TxConfig.TxDecoder())
bApp := baseapp.NewBaseApp(app.Name(), logger, db, app.TxConfig().TxDecoder())
bApp.SetCommitMultiStoreTracer(nil)
bApp.SetInterfaceRegistry(encCfg.InterfaceRegistry)
bApp.SetInterfaceRegistry(app.InterfaceRegistry())
app.BaseApp = bApp
configurator := module.NewConfigurator(app.appCodec, bApp.MsgServiceRouter(), app.GRPCQueryRouter())

Expand Down Expand Up @@ -204,9 +201,8 @@ func TestRunMigrations(t *testing.T) {

func TestInitGenesisOnMigration(t *testing.T) {
db := dbm.NewMemDB()
encCfg := MakeTestEncodingConfig()
logger, _ := log.NewDefaultLogger("plain", "info", false)
app := NewSimApp(logger, db, nil, true, encCfg, simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome))
app := NewSimApp(logger, db, nil, true, simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome))
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})

// Create a mock module. This module will serve as the new module we're
Expand Down Expand Up @@ -247,14 +243,12 @@ func TestInitGenesisOnMigration(t *testing.T) {
}

func TestUpgradeStateOnGenesis(t *testing.T) {
encCfg := MakeTestEncodingConfig()
db := dbm.NewMemDB()
logger, _ := log.NewDefaultLogger("plain", "info", false)
app := NewSimappWithCustomOptions(t, false, SetupOptions{
Logger: logger,
DB: db,
EncConfig: encCfg,
AppOpts: simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome),
Logger: logger,
DB: db,
AppOpts: simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome),
})

// make sure the upgrade keeper has version map in state
Expand Down
4 changes: 2 additions & 2 deletions simapp/sim_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func BenchmarkFullAppSimulation(b *testing.B) {
appOptions[flags.FlagHome] = DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = FlagPeriodValue

app := NewSimApp(logger, db, nil, true, MakeTestEncodingConfig(), appOptions, interBlockCacheOpt())
app := NewSimApp(logger, db, nil, true, appOptions, interBlockCacheOpt())

// run randomized simulation
_, simParams, simErr := simulation.SimulateFromSeed(
Expand Down Expand Up @@ -88,7 +88,7 @@ func BenchmarkInvariants(b *testing.B) {
appOptions[flags.FlagHome] = DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = FlagPeriodValue

app := NewSimApp(logger, db, nil, true, MakeTestEncodingConfig(), appOptions, interBlockCacheOpt())
app := NewSimApp(logger, db, nil, true, appOptions, interBlockCacheOpt())

// run randomized simulation
_, simParams, simErr := simulation.SimulateFromSeed(
Expand Down
12 changes: 6 additions & 6 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestFullAppSimulation(t *testing.T) {
appOptions[flags.FlagHome] = DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = FlagPeriodValue

app := NewSimApp(logger, db, nil, true, MakeTestEncodingConfig(), appOptions, fauxMerkleModeOpt)
app := NewSimApp(logger, db, nil, true, appOptions, fauxMerkleModeOpt)
require.Equal(t, "SimApp", app.Name())

// run randomized simulation
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestAppImportExport(t *testing.T) {
appOptions[flags.FlagHome] = DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = FlagPeriodValue

app := NewSimApp(logger, db, nil, true, MakeTestEncodingConfig(), appOptions, fauxMerkleModeOpt)
app := NewSimApp(logger, db, nil, true, appOptions, fauxMerkleModeOpt)
require.Equal(t, "SimApp", app.Name())

// Run randomized simulation
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestAppImportExport(t *testing.T) {
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := NewSimApp(log.NewNopLogger(), newDB, nil, true, MakeTestEncodingConfig(), appOptions, fauxMerkleModeOpt)
newApp := NewSimApp(log.NewNopLogger(), newDB, nil, true, appOptions, fauxMerkleModeOpt)
require.Equal(t, "SimApp", newApp.Name())

var genesisState GenesisState
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
appOptions[flags.FlagHome] = DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = FlagPeriodValue

app := NewSimApp(logger, db, nil, true, MakeTestEncodingConfig(), appOptions, fauxMerkleModeOpt)
app := NewSimApp(logger, db, nil, true, appOptions, fauxMerkleModeOpt)
require.Equal(t, "SimApp", app.Name())

// Run randomized simulation
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := NewSimApp(log.NewNopLogger(), newDB, nil, true, MakeTestEncodingConfig(), appOptions, fauxMerkleModeOpt)
newApp := NewSimApp(log.NewNopLogger(), newDB, nil, true, appOptions, fauxMerkleModeOpt)
require.Equal(t, "SimApp", newApp.Name())

newApp.InitChain(abci.RequestInitChain{
Expand Down Expand Up @@ -332,7 +332,7 @@ func TestAppStateDeterminism(t *testing.T) {
}

db := dbm.NewMemDB()
app := NewSimApp(logger, db, nil, true, MakeTestEncodingConfig(), appOptions, interBlockCacheOpt())
app := NewSimApp(logger, db, nil, true, appOptions, interBlockCacheOpt())

fmt.Printf(
"running non-determinism simulation; seed %d: %d/%d, attempt: %d/%d\n",
Expand Down
5 changes: 2 additions & 3 deletions simapp/simd/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestInitCmd(t *testing.T) {
rootCmd, _ := cmd.NewRootCmd()
rootCmd := cmd.NewRootCmd()
rootCmd.SetArgs([]string{
"init", // Test the init cmd
"simapp-test", // Moniker
Expand All @@ -27,8 +27,7 @@ func TestInitCmd(t *testing.T) {
func TestHomeFlagRegistration(t *testing.T) {
homeDir := "/tmp/foo"

rootCmd, _ := cmd.NewRootCmd()

rootCmd := cmd.NewRootCmd()
rootCmd.SetArgs([]string{
"query",
fmt.Sprintf("--%s", flags.FlagHome),
Expand Down
Loading

0 comments on commit 9c51d9a

Please sign in to comment.