Skip to content

Commit

Permalink
Merge PR cosmos#5952: Refactor Evidence Age Util + Governable Consens…
Browse files Browse the repository at this point in the history
…us Params
  • Loading branch information
alexanderbez authored Apr 16, 2020
1 parent 9e97770 commit 1270adc
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 24 deletions.
22 changes: 12 additions & 10 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
tmos "github.com/tendermint/tendermint/libs/os"
dbm "github.com/tendermint/tm-db"

bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codecstd "github.com/cosmos/cosmos-sdk/codec/std"
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
Expand Down Expand Up @@ -95,7 +96,7 @@ var _ App = (*SimApp)(nil)
// They are exported for convenience in creating helper functions, as object
// capabilities aren't needed for testing.
type SimApp struct {
*bam.BaseApp
*baseapp.BaseApp
cdc *codec.Codec

invCheckPeriod uint
Expand Down Expand Up @@ -138,19 +139,19 @@ type SimApp struct {
// NewSimApp returns a reference to an initialized SimApp.
func NewSimApp(
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, skipUpgradeHeights map[int64]bool,
homePath string, invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp),
homePath string, invCheckPeriod uint, baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {

// TODO: Remove cdc in favor of appCodec once all modules are migrated.
cdc := codecstd.MakeCodec(ModuleBasics)
appCodec := codecstd.NewAppCodec(cdc)

bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
bApp := baseapp.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetAppVersion(version.Version)

keys := sdk.NewKVStoreKeys(
bam.MainStoreKey, auth.StoreKey, bank.StoreKey, staking.StoreKey,
auth.StoreKey, bank.StoreKey, staking.StoreKey,
supply.StoreKey, mint.StoreKey, distr.StoreKey, slashing.StoreKey,
gov.StoreKey, params.StoreKey, ibc.StoreKey, upgrade.StoreKey,
evidence.StoreKey, transfer.StoreKey, capability.StoreKey,
Expand All @@ -176,7 +177,9 @@ func NewSimApp(
app.subspaces[slashing.ModuleName] = app.ParamsKeeper.Subspace(slashing.DefaultParamspace)
app.subspaces[gov.ModuleName] = app.ParamsKeeper.Subspace(gov.DefaultParamspace).WithKeyTable(gov.ParamKeyTable())
app.subspaces[crisis.ModuleName] = app.ParamsKeeper.Subspace(crisis.DefaultParamspace)
app.subspaces[evidence.ModuleName] = app.ParamsKeeper.Subspace(evidence.DefaultParamspace)

// set the BaseApp's parameter store
bApp.SetParamStore(app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(std.ConsensusParamsKeyTable()))

// add capability keeper and ScopeToModule for ibc module
app.CapabilityKeeper = capability.NewKeeper(appCodec, keys[capability.StoreKey])
Expand Down Expand Up @@ -250,7 +253,7 @@ func NewSimApp(

// create evidence keeper with router
evidenceKeeper := evidence.NewKeeper(
appCodec, keys[evidence.StoreKey], app.subspaces[evidence.ModuleName], &app.StakingKeeper, app.SlashingKeeper,
appCodec, keys[evidence.StoreKey], &app.StakingKeeper, app.SlashingKeeper,
)
evidenceRouter := evidence.NewRouter().
AddRoute(ibcclient.RouterKey, ibcclient.HandlerClientMisbehaviour(app.IBCKeeper.ClientKeeper))
Expand Down Expand Up @@ -330,8 +333,7 @@ func NewSimApp(
app.SetEndBlocker(app.EndBlocker)

if loadLatest {
err := app.LoadLatestVersion(app.keys[bam.MainStoreKey])
if err != nil {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
}
}
Expand Down Expand Up @@ -370,7 +372,7 @@ func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.

// LoadHeight loads a particular height
func (app *SimApp) LoadHeight(height int64) error {
return app.LoadVersion(height, app.keys[bam.MainStoreKey])
return app.LoadVersion(height)
}

// ModuleAccountAddrs returns all the app's module account addresses.
Expand Down
2 changes: 1 addition & 1 deletion app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestSimAppExport(t *testing.T) {

// Making a new app object with the db, so that initchain hasn't been called
app2 := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0)
_, _, err = app2.ExportAppStateAndValidators(false, []string{})
_, _, _, err = app2.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}

Expand Down
6 changes: 3 additions & 3 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// file.
func (app *SimApp) ExportAppStateAndValidators(
forZeroHeight bool, jailWhiteList []string,
) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
) (appState json.RawMessage, validators []tmtypes.GenesisValidator, cp *abci.ConsensusParams, err error) {

// as if they could withdraw from the start of the next block
ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()})
Expand All @@ -30,11 +30,11 @@ func (app *SimApp) ExportAppStateAndValidators(
genState := app.mm.ExportGenesis(ctx, app.cdc)
appState, err = codec.MarshalJSONIndent(app.cdc, genState)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

validators = staking.WriteValidators(ctx, app.StakingKeeper)
return appState, validators, nil
return appState, validators, app.BaseApp.GetConsensusParams(ctx), nil
}

// prepare for fresh start at zero height
Expand Down
5 changes: 2 additions & 3 deletions sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestAppImportExport(t *testing.T) {

fmt.Printf("exporting genesis...\n")

appState, _, err := app.ExportAppStateAndValidators(false, []string{})
appState, _, _, err := app.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err)

fmt.Printf("importing genesis...\n")
Expand All @@ -143,7 +143,6 @@ func TestAppImportExport(t *testing.T) {
fmt.Printf("comparing stores...\n")

storeKeysPrefixes := []StoreKeysPrefixes{
{app.keys[baseapp.MainStoreKey], newApp.keys[baseapp.MainStoreKey], [][]byte{}},
{app.keys[auth.StoreKey], newApp.keys[auth.StoreKey], [][]byte{}},
{app.keys[staking.StoreKey], newApp.keys[staking.StoreKey],
[][]byte{
Expand Down Expand Up @@ -208,7 +207,7 @@ func TestAppSimulationAfterImport(t *testing.T) {

fmt.Printf("exporting genesis...\n")

appState, _, err := app.ExportAppStateAndValidators(true, []string{})
appState, _, _, err := app.ExportAppStateAndValidators(true, []string{})
require.NoError(t, err)

fmt.Printf("importing genesis...\n")
Expand Down
32 changes: 27 additions & 5 deletions test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"

bam "github.com/cosmos/cosmos-sdk/baseapp"
Expand All @@ -24,10 +25,29 @@ import (
"github.com/cosmos/cosmos-sdk/x/supply"
)

// DefaultConsensusParams defines the default Tendermint consensus params used in
// SimApp testing.
var DefaultConsensusParams = &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxBytes: 200000,
MaxGas: 2000000,
},
Evidence: &abci.EvidenceParams{
MaxAgeNumBlocks: 302400,
MaxAgeDuration: 1814400,
},
Validator: &abci.ValidatorParams{
PubKeyTypes: []string{
tmtypes.ABCIPubKeyTypeEd25519,
tmtypes.ABCIPubKeyTypeSecp256k1,
},
},
}

// Setup initializes a new SimApp. A Nop logger is set in SimApp.
func Setup(isCheckTx bool) *SimApp {
db := dbm.NewMemDB()
app := NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0)
app := NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5)
if !isCheckTx {
// init chain must be called to stop deliverState from being nil
genesisState := NewDefaultGenesisState()
Expand All @@ -39,8 +59,9 @@ func Setup(isCheckTx bool) *SimApp {
// Initialize the chain
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
}
Expand Down Expand Up @@ -70,8 +91,9 @@ func SetupWithGenesisAccounts(genAccs []authexported.GenesisAccount, balances ..

app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)

Expand Down
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type App interface {
// Exports the state of the application for a genesis file.
ExportAppStateAndValidators(
forZeroHeight bool, jailWhiteList []string,
) (json.RawMessage, []tmtypes.GenesisValidator, error)
) (json.RawMessage, []tmtypes.GenesisValidator, *abci.ConsensusParams, error)

// All the registered module account addreses.
ModuleAccountAddrs() map[string]bool
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func CheckExportSimulation(
) error {
if config.ExportStatePath != "" {
fmt.Println("exporting app state...")
appState, _, err := app.ExportAppStateAndValidators(false, nil)
appState, _, _, err := app.ExportAppStateAndValidators(false, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit 1270adc

Please sign in to comment.