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

R4R: ParamStore SubModules #1772

Closed
wants to merge 23 commits into from
Closed
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
98 changes: 91 additions & 7 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ type BaseApp struct {
// Volatile
// checkState is set on initialization and reset on Commit.
// deliverState is set in InitChain and BeginBlock and cleared on Commit.
// See methods setCheckState and setDeliverState.
checkState *state // for CheckTx
deliverState *state // for DeliverTx
// See methods app.setCheckState and setDeliverState.
checkState *state // for CheckTx
checkParams sdk.ConsensusParams
deliverState *state // for DeliverTx
deliverParams sdk.ConsensusParams

signedValidators []abci.SigningValidator // absent validators from begin block

// flag for sealing
Expand Down Expand Up @@ -206,6 +209,16 @@ func (app *BaseApp) setCheckState(header abci.Header) {
}
}

func (app *BaseApp) setCheckParams(params *abci.ConsensusParams) {
var sdkparams sdk.ConsensusParams
(&sdkparams).FromABCI(params)
app.checkParams = sdkparams
}

func (app *BaseApp) getCheckContext() sdk.Context {
Copy link
Collaborator

Choose a reason for hiding this comment

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

getCheckStateContext ?

return app.checkState.ctx.WithConsensusParams(app.checkParams)
}

func (app *BaseApp) setDeliverState(header abci.Header) {
ms := app.cms.CacheMultiStore()
app.deliverState = &state{
Expand All @@ -214,6 +227,16 @@ func (app *BaseApp) setDeliverState(header abci.Header) {
}
}

func (app *BaseApp) setDeliverParams(params *abci.ConsensusParams) {
var sdkparams sdk.ConsensusParams
(&sdkparams).FromABCI(params)
app.deliverParams = sdkparams
}

func (app *BaseApp) getDeliverContext() sdk.Context {
return app.deliverState.ctx.WithConsensusParams(app.deliverParams)
}

//______________________________________________________________________________

// ABCI
Expand All @@ -240,12 +263,14 @@ func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOp
func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
// Initialize the deliver state and check state with ChainID and run initChain
app.setDeliverState(abci.Header{ChainID: req.ChainId})
app.setDeliverParams(req.ConsensusParams)
app.setCheckState(abci.Header{ChainID: req.ChainId})
app.setCheckParams(req.ConsensusParams)

if app.initChainer == nil {
return
}
res = app.initChainer(app.deliverState.ctx, req)
res = app.initChainer(app.getDeliverContext(), req)

// NOTE: we don't commit, but BeginBlock for block 1
// starts from this deliverState
Expand Down Expand Up @@ -415,7 +440,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
}

if app.beginBlocker != nil {
res = app.beginBlocker(app.deliverState.ctx, req)
res = app.beginBlocker(app.getDeliverContext(), req)
}

// set the signed validators for addition to context in deliverTx
Expand Down Expand Up @@ -496,12 +521,17 @@ func validateBasicTxMsgs(msgs []sdk.Msg) sdk.Error {
func (app *BaseApp) getContextForAnte(mode runTxMode, txBytes []byte) (ctx sdk.Context) {
// Get the context
if mode == runTxModeCheck || mode == runTxModeSimulate {
ctx = app.checkState.ctx.WithTxBytes(txBytes)
ctx = app.getCheckContext().WithTxBytes(txBytes)
} else {
ctx = app.deliverState.ctx.WithTxBytes(txBytes)
ctx = app.getDeliverContext().WithTxBytes(txBytes)
ctx = ctx.WithSigningValidators(app.signedValidators)
}

params := ctx.ConsensusParams()
if params.TxSize.MaxGas != 0 {
ctx = ctx.WithGasMeter(sdk.NewGasMeter(params.TxSize.MaxGas))
}

return
}

Expand Down Expand Up @@ -634,6 +664,50 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
return
}

// Copied from tendermint/tendermint/types/params.go
func updateConsensusParams(params *abci.ConsensusParams, updates *abci.ConsensusParams) (res *abci.ConsensusParams) {
res = new(abci.ConsensusParams)
res.BlockSize = new(abci.BlockSize)
res.TxSize = new(abci.TxSize)
res.BlockGossip = new(abci.BlockGossip)

if updates == nil {
return
}

if updates.BlockSize != nil {
if updates.BlockSize.MaxBytes > 0 {
res.BlockSize.MaxBytes = updates.BlockSize.MaxBytes
} else {
res.BlockSize.MaxBytes = params.BlockSize.MaxBytes
}
if updates.BlockSize.MaxTxs > 0 {
res.BlockSize.MaxTxs = updates.BlockSize.MaxTxs
} else {
res.BlockSize.MaxTxs = params.BlockSize.MaxTxs
}
if updates.BlockSize.MaxGas > 0 {
res.BlockSize.MaxGas = updates.BlockSize.MaxGas
} else {
res.BlockSize.MaxGas = params.BlockSize.MaxGas
}
}
if updates.TxSize != nil {
if updates.TxSize.MaxBytes > 0 {
params.TxSize.MaxBytes = updates.TxSize.MaxBytes
}
if updates.TxSize.MaxGas > 0 {
params.TxSize.MaxGas = updates.TxSize.MaxGas
}
}
if updates.BlockGossip != nil {
if updates.BlockGossip.BlockPartSizeBytes > 0 {
params.BlockGossip.BlockPartSizeBytes = updates.BlockGossip.BlockPartSizeBytes
}
}
return
}

// EndBlock implements the ABCI application interface.
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
if app.deliverState.ms.TracingEnabled() {
Expand All @@ -644,6 +718,16 @@ func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBloc
res = app.endBlocker(app.deliverState.ctx, req)
}

// Use consensus parameter from last block
params := app.getDeliverContext().ConsensusParams().ToABCI()
app.setCheckParams(params)

if res.ConsensusParamUpdates != nil {
// Update consensus parameter
newparams := updateConsensusParams(params, res.ConsensusParamUpdates)
app.setDeliverParams(newparams)
}

return
}

Expand Down
3 changes: 3 additions & 0 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ func TestCheckTx(t *testing.T) {
require.Equal(t, nTxs, storedCounter)

// If a block is committed, CheckTx state should be reset.
app.InitChain(abci.RequestInitChain{})
app.BeginBlock(abci.RequestBeginBlock{})
app.EndBlock(abci.RequestEndBlock{})
app.Commit()
Expand All @@ -492,6 +493,8 @@ func TestDeliverTx(t *testing.T) {
codec := wire.NewCodec()
registerTestCodec(codec)

app.InitChain(abci.RequestInitChain{})

nBlocks := 3
txPerHeight := 5
for blockN := 0; blockN < nBlocks; blockN++ {
Expand Down
15 changes: 10 additions & 5 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
// add handlers
app.coinKeeper = bank.NewKeeper(app.accountMapper)
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.RegisterCodespace(stake.DefaultCodespace))
app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.paramsKeeper.Setter(), app.coinKeeper, app.stakeKeeper, app.RegisterCodespace(gov.DefaultCodespace))
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams, nil)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.paramsKeeper.Subspace(stake.DefaultParamSpace), app.RegisterCodespace(stake.DefaultCodespace))
app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.paramsKeeper, app.paramsKeeper.Subspace(gov.DefaultParamSpace), app.coinKeeper, app.stakeKeeper, app.RegisterCodespace(gov.DefaultCodespace))
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, app.keyFeeCollection)
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Getter(), app.RegisterCodespace(slashing.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamSpace), app.RegisterCodespace(slashing.DefaultCodespace))

// register message routes
app.Router().
Expand Down Expand Up @@ -186,10 +186,15 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
}

// load the address to pubkey map
slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.StakeData)
slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.SlashingData, genesisState.StakeData)

gov.InitGenesis(ctx, app.govKeeper, genesisState.GovData)

err = slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.SlashingData, genesisState.StakeData)
if err != nil {
panic(err)
}

return abci.ResponseInitChain{
Validators: validators,
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/gaia/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/db"
Expand All @@ -21,8 +22,9 @@ func setGenesis(gapp *GaiaApp, accs ...*auth.BaseAccount) error {
}

genesisState := GenesisState{
Accounts: genaccs,
StakeData: stake.DefaultGenesisState(),
Accounts: genaccs,
StakeData: stake.DefaultGenesisState(),
SlashingData: slashing.HubDefaultGenesisState(),
}

stateBytes, err := wire.MarshalJSONIndent(gapp.cdc, genesisState)
Expand Down
17 changes: 11 additions & 6 deletions cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"

"github.com/spf13/pflag"
Expand All @@ -31,9 +32,10 @@ var (

// State to Unmarshal
type GenesisState struct {
Accounts []GenesisAccount `json:"accounts"`
StakeData stake.GenesisState `json:"stake"`
GovData gov.GenesisState `json:"gov"`
Accounts []GenesisAccount `json:"accounts"`
StakeData stake.GenesisState `json:"stake"`
GovData gov.GenesisState `json:"gov"`
SlashingData slashing.GenesisState `json:"slashing"`
}

// GenesisAccount doesn't need pubkey or sequence
Expand Down Expand Up @@ -169,6 +171,8 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState
// start with the default staking genesis state
stakeData := stake.DefaultGenesisState()

slashingData := slashing.HubDefaultGenesisState()

// get genesis flag account information
genaccs := make([]GenesisAccount, len(appGenTxs))
for i, appGenTx := range appGenTxs {
Expand Down Expand Up @@ -216,9 +220,10 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState

// create the final app state
genesisState = GenesisState{
Accounts: genaccs,
StakeData: stakeData,
GovData: gov.DefaultGenesisState(),
Accounts: genaccs,
StakeData: stakeData,
GovData: gov.DefaultGenesisState(),
SlashingData: slashingData,
}
return
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/gaia/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
banksim "github.com/cosmos/cosmos-sdk/x/bank/simulation"
govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation"
"github.com/cosmos/cosmos-sdk/x/mock/simulation"
"github.com/cosmos/cosmos-sdk/x/slashing"
slashingsim "github.com/cosmos/cosmos-sdk/x/slashing/simulation"
stake "github.com/cosmos/cosmos-sdk/x/stake"
stakesim "github.com/cosmos/cosmos-sdk/x/stake/simulation"
Expand Down Expand Up @@ -72,8 +73,9 @@ func appStateFn(r *rand.Rand, keys []crypto.PrivKey, accs []sdk.AccAddress) json
stakeGenesis.Params.InflationMax = sdk.NewDec(0)
stakeGenesis.Params.InflationMin = sdk.NewDec(0)
genesis := GenesisState{
Accounts: genesisAccounts,
StakeData: stakeGenesis,
Accounts: genesisAccounts,
StakeData: stakeGenesis,
SlashingData: slashing.HubDefaultGenesisState(),
}

// Marshal genesis
Expand Down
16 changes: 12 additions & 4 deletions cmd/gaia/cmd/gaiadebug/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type GaiaApp struct {
keyStake *sdk.KVStoreKey
keySlashing *sdk.KVStoreKey
keyParams *sdk.KVStoreKey
tkeyParams *sdk.TransientStoreKey

// Manage getting and setting accounts
accountMapper auth.AccountMapper
Expand Down Expand Up @@ -163,6 +164,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
keyStake: sdk.NewKVStoreKey("stake"),
keySlashing: sdk.NewKVStoreKey("slashing"),
keyParams: sdk.NewKVStoreKey("params"),
tkeyParams: sdk.NewTransientStoreKey("params"),
}

// define the accountMapper
Expand All @@ -175,9 +177,9 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
// add handlers
app.coinKeeper = bank.NewKeeper(app.accountMapper)
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.RegisterCodespace(stake.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Getter(), app.RegisterCodespace(slashing.DefaultCodespace))
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams, nil)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.paramsKeeper.Subspace(stake.DefaultParamSpace), app.RegisterCodespace(stake.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamSpace), app.RegisterCodespace(slashing.DefaultCodespace))

// register message routes
app.Router().
Expand All @@ -190,7 +192,8 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake, app.keySlashing)
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake, app.keySlashing, app.keyParams)
app.MountStore(app.tkeyParams, sdk.StoreTypeTransient)
err := app.LoadLatestVersion(app.keyMain)
if err != nil {
cmn.Exit(err.Error())
Expand Down Expand Up @@ -257,6 +260,11 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468 // return sdk.ErrGenesisParse("").TraceCause(err, "")
}

err = slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.SlashingData, genesisState.StakeData)
if err != nil {
panic(err)
}

return abci.ResponseInitChain{
Validators: validators,
}
Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/cool/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func getMockApp(t *testing.T) *mock.App {

mapp.SetInitChainer(getInitChainer(mapp, keeper, "ice-cold"))

require.NoError(t, mapp.CompleteSetup([]*sdk.KVStoreKey{keyCool}))
require.NoError(t, mapp.CompleteSetup(keyCool))
return mapp
}

Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/pow/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func getMockApp(t *testing.T) *mock.App {

mapp.SetInitChainer(getInitChainer(mapp, keeper))

require.NoError(t, mapp.CompleteSetup([]*sdk.KVStoreKey{keyPOW}))
require.NoError(t, mapp.CompleteSetup(keyPOW))

mapp.Seal()

Expand Down
Loading