Skip to content

Commit

Permalink
Jonathan/5988 sim params consensus 2 (cosmos#6051)
Browse files Browse the repository at this point in the history
* temporal commit

* add random consensus params

* make format

* make random pass

* remove comment

* revert change

* update ub key types

* extract Evidence params from state

* extract the random parameters from state

* clean the code

* update imports!

* mispelled back

* mispelled back

* add misspelled command

* update changelog

* remove useless linter to avoid misspell

* remove function

* use tendermint constants

* update import test

* remove debug comment

* Update baseapp/baseapp.go

Co-Authored-By: Federico Kunze <[email protected]>

Co-authored-by: Alexander Bezobchuk <[email protected]>
Co-authored-by: Federico Kunze <[email protected]>
  • Loading branch information
3 people authored Apr 22, 2020
1 parent f82bc19 commit 550c3e9
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ functionality that requires an online connection.
* (types/rest) [\#5900](https://github.com/cosmos/cosmos-sdk/pull/5900) Add Check*Error function family to spare developers from replicating tons of boilerplate code.
* (x/evidence) [\#5952](https://github.com/cosmos/cosmos-sdk/pull/5952) Tendermint Consensus parameters can now be changed via parameter change proposals through `x/gov`.
* (x/auth/ante) [\#6040](https://github.com/cosmos/cosmos-sdk/pull/6040) `AccountKeeper` interface used for `NewAnteHandler` and handler's decorators to add support of using custom `AccountKeeper` implementations.
* (simulation) [\#6002](https://github.com/cosmos/cosmos-sdk/pull/6002) Add randomized consensus params into simulation.

## [v0.38.3] - 2020-04-09

Expand Down
2 changes: 1 addition & 1 deletion baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
// done after the deliver state and context have been set as it's persisted
// to state.
if req.ConsensusParams != nil {
app.storeConsensusParams(app.deliverState.ctx, req.ConsensusParams)
app.StoreConsensusParams(app.deliverState.ctx, req.ConsensusParams)
}

if app.initChainer == nil {
Expand Down
3 changes: 2 additions & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ func (app *BaseApp) GetConsensusParams(ctx sdk.Context) *abci.ConsensusParams {
return cp
}

func (app *BaseApp) storeConsensusParams(ctx sdk.Context, cp *abci.ConsensusParams) {
// StoreConsensusParams sets the consensus parameters to the baseapp's param store.
func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *abci.ConsensusParams) {
if app.paramStore == nil {
panic("cannot store consensus params with no params store set")
}
Expand Down
8 changes: 4 additions & 4 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1501,16 +1501,16 @@ func TestGetMaximumBlockGas(t *testing.T) {
app.InitChain(abci.RequestInitChain{})
ctx := app.NewContext(true, abci.Header{})

app.storeConsensusParams(ctx, &abci.ConsensusParams{Block: &abci.BlockParams{MaxGas: 0}})
app.StoreConsensusParams(ctx, &abci.ConsensusParams{Block: &abci.BlockParams{MaxGas: 0}})
require.Equal(t, uint64(0), app.getMaximumBlockGas(ctx))

app.storeConsensusParams(ctx, &abci.ConsensusParams{Block: &abci.BlockParams{MaxGas: -1}})
app.StoreConsensusParams(ctx, &abci.ConsensusParams{Block: &abci.BlockParams{MaxGas: -1}})
require.Equal(t, uint64(0), app.getMaximumBlockGas(ctx))

app.storeConsensusParams(ctx, &abci.ConsensusParams{Block: &abci.BlockParams{MaxGas: 5000000}})
app.StoreConsensusParams(ctx, &abci.ConsensusParams{Block: &abci.BlockParams{MaxGas: 5000000}})
require.Equal(t, uint64(5000000), app.getMaximumBlockGas(ctx))

app.storeConsensusParams(ctx, &abci.ConsensusParams{Block: &abci.BlockParams{MaxGas: -5000000}})
app.StoreConsensusParams(ctx, &abci.ConsensusParams{Block: &abci.BlockParams{MaxGas: -5000000}})
require.Panics(t, func() { app.getMaximumBlockGas(ctx) })
}

Expand Down
8 changes: 4 additions & 4 deletions client/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func TestValidateCmd(t *testing.T) {
args []string
wantErr bool
}{
{"misspelled command", []string{"commission"}, true}, // nolint: misspell
{"misspelled command", []string{"COMMISSION"}, true},
{"no command provided", []string{}, false},
{"help flag", []string{"commission", "--help"}, false}, // nolint: misspell
{"shorthand help flag", []string{"commission", "-h"}, false}, // nolint: misspell
{"help flag", []string{"COMMISSION", "--help"}, false},
{"shorthand help flag", []string{"COMMISSION", "-h"}, false},
{"flag only, no command provided", []string{"--gas", "1000atom"}, false},
{"flag and misspelled command", []string{"--gas", "1000atom", "comission"}, true}, // nolint: misspell
{"flag and misspelled command", []string{"--gas", "1000atom", "COMMISSION"}, true},
}

for _, tt := range tests {
Expand Down
3 changes: 2 additions & 1 deletion simapp/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, _, consensusParams, err := app.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err)

fmt.Printf("importing genesis...\n")
Expand All @@ -139,6 +139,7 @@ func TestAppImportExport(t *testing.T) {
ctxA := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()})
ctxB := newApp.NewContext(true, abci.Header{Height: app.LastBlockHeight()})
newApp.mm.InitGenesis(ctxB, app.Codec(), genesisState)
newApp.StoreConsensusParams(ctxB, consensusParams)

fmt.Printf("comparing stores...\n")

Expand Down
3 changes: 2 additions & 1 deletion x/ibc/09-localhost/types/localhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (

"github.com/stretchr/testify/suite"

dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/dbadapter"
commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types"
dbm "github.com/tendermint/tm-db"
)

const (
Expand Down
38 changes: 38 additions & 0 deletions x/simulation/params.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package simulation

import (
"encoding/json"
"fmt"
"math/rand"

"github.com/tendermint/tendermint/types"

"github.com/tendermint/go-amino"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/simulation"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

const (
Expand Down Expand Up @@ -142,3 +150,33 @@ func (w WeightedProposalContent) DefaultWeight() int {
func (w WeightedProposalContent) ContentSimulatorFn() simulation.ContentSimulatorFn {
return w.contentSimulatorFn
}

//-----------------------------------------------------------------------------
// Param change proposals

// RandomParams returns random simulation consensus parameters, it extracts the Evidence from the Staking genesis state.
func RandomConsensusParams(r *rand.Rand, appState json.RawMessage) *abci.ConsensusParams {
cdc := amino.NewCodec()

var genesisState map[string]json.RawMessage
cdc.UnmarshalJSON(appState, &genesisState)

stakingGenesisState := stakingtypes.GetGenesisStateFromAppState(cdc, genesisState)

consensusParams := &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxBytes: int64(simulation.RandIntBetween(r, 20000000, 30000000)),
MaxGas: -1,
},
Validator: &abci.ValidatorParams{
PubKeyTypes: []string{types.ABCIPubKeyTypeSecp256k1, types.ABCIPubKeyTypeEd25519},
},
Evidence: &abci.EvidenceParams{
MaxAgeNumBlocks: int64(stakingGenesisState.Params.UnbondingTime / AverageBlockTime),
MaxAgeDuration: stakingGenesisState.Params.UnbondingTime,
},
}
fmt.Printf("Selected randomly generated consensus parameters:\n%s\n", codec.MustMarshalJSONIndent(cdc, consensusParams))

return consensusParams
}
11 changes: 8 additions & 3 deletions x/simulation/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/cosmos/cosmos-sdk/types/simulation"
)

const AverageBlockTime = 6 * time.Second

// initialize the chain for the simulation
func initChain(
r *rand.Rand, params Params, accounts []simulation.Account, app *baseapp.BaseApp,
Expand All @@ -25,9 +27,12 @@ func initChain(

appState, accounts, chainID, genesisTimestamp := appStateFn(r, accounts, config)

consensusParams := RandomConsensusParams(r, appState)

req := abci.RequestInitChain{
AppStateBytes: appState,
ChainId: chainID,
AppStateBytes: appState,
ChainId: chainID,
ConsensusParams: consensusParams,
}
res := app.InitChain(req)
validators := newMockValidators(r, res.Validators, params)
Expand Down Expand Up @@ -108,7 +113,7 @@ func SimulateFromSeed(

// These are operations which have been queued by previous operations
operationQueue := NewOperationQueue()
timeOperationQueue := []simulation.FutureOperation{}
var timeOperationQueue []simulation.FutureOperation

logWriter := NewLogWriter(testingMode)

Expand Down
14 changes: 14 additions & 0 deletions x/staking/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -37,3 +40,14 @@ func DefaultGenesisState() GenesisState {
Params: DefaultParams(),
}
}

// GetGenesisStateFromAppState returns x/staking GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc *codec.Codec, appState map[string]json.RawMessage) GenesisState {
var genesisState GenesisState
if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}

return genesisState
}

0 comments on commit 550c3e9

Please sign in to comment.