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

refactor: migrate consensus to collections #15553

Merged
merged 16 commits into from
Mar 28, 2023
Merged
50 changes: 25 additions & 25 deletions api/cosmos/consensus/v1/query.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions api/cosmos/consensus/v1/query_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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 {
err := app.StoreConsensusParams(app.deliverState.ctx, req.ConsensusParams)
err := app.StoreConsensusParams(app.deliverState.ctx, *req.ConsensusParams)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -238,9 +238,8 @@ func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBloc
res.Events = sdk.MarkEventsToIndex(res.Events, app.indexEvents)
}

if cp := app.GetConsensusParams(app.deliverState.ctx); cp != nil {
res.ConsensusParamUpdates = cp
}
cp := app.GetConsensusParams(app.deliverState.ctx)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call
res.ConsensusParamUpdates = &cp

// call the streaming service hook with the EndBlock messages
for _, abciListener := range app.streamingManager.ABCIListeners {
Expand Down Expand Up @@ -864,7 +863,7 @@ func (app *BaseApp) GetBlockRetentionHeight(commitHeight int64) int64 {
// on the unbonding period and block commitment time as the two should be
// equivalent.
cp := app.GetConsensusParams(app.deliverState.ctx)
if cp != nil && cp.Evidence != nil && cp.Evidence.MaxAgeNumBlocks > 0 {
if cp.Evidence != nil && cp.Evidence.MaxAgeNumBlocks > 0 {
retentionHeight = commitHeight - cp.Evidence.MaxAgeNumBlocks
}

Expand Down
12 changes: 4 additions & 8 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ func (app *BaseApp) setState(mode runTxMode, header cmtproto.Header) {

// GetConsensusParams returns the current consensus parameters from the BaseApp's
// ParamStore. If the BaseApp has no ParamStore defined, nil is returned.
func (app *BaseApp) GetConsensusParams(ctx sdk.Context) *cmtproto.ConsensusParams {
func (app *BaseApp) GetConsensusParams(ctx sdk.Context) cmtproto.ConsensusParams {
if app.paramStore == nil {
return nil
panic("param store not set")
Fixed Show fixed Hide fixed
}

cp, err := app.paramStore.Get(ctx)
Expand All @@ -448,15 +448,11 @@ func (app *BaseApp) GetConsensusParams(ctx sdk.Context) *cmtproto.ConsensusParam
}

// StoreConsensusParams sets the consensus parameters to the baseapp's param store.
func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *cmtproto.ConsensusParams) error {
func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp cmtproto.ConsensusParams) error {
if app.paramStore == nil {
panic("cannot store consensus params with no params store set")
}

if cp == nil {
return nil
}

return app.paramStore.Set(ctx, cp)
// We're explicitly not storing the CometBFT app_version in the param store. It's
// stored instead in the x/upgrade store, with its own bump logic.
Expand All @@ -474,7 +470,7 @@ func (app *BaseApp) AddRunTxRecoveryHandler(handlers ...RecoveryHandler) {
// one.
func (app *BaseApp) GetMaximumBlockGas(ctx sdk.Context) uint64 {
cp := app.GetConsensusParams(ctx)
if cp == nil || cp.Block == nil {
if cp.Block == nil {
return 0
}

Expand Down
10 changes: 5 additions & 5 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
app.SetInterfaceRegistry(cdc.InterfaceRegistry())
app.MsgServiceRouter().SetInterfaceRegistry(cdc.InterfaceRegistry())
app.MountStores(capKey1, capKey2)
app.SetParamStore(&paramStore{db: dbm.NewMemDB()})
app.SetParamStore(paramStore{db: dbm.NewMemDB()})
app.SetTxDecoder(txConfig.TxDecoder())
app.SetTxEncoder(txConfig.TxEncoder())

Expand Down Expand Up @@ -587,16 +587,16 @@ func TestGetMaximumBlockGas(t *testing.T) {
suite.baseApp.InitChain(abci.RequestInitChain{})
ctx := suite.baseApp.NewContext(true, cmtproto.Header{})

suite.baseApp.StoreConsensusParams(ctx, &cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 0}})
suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 0}})
require.Equal(t, uint64(0), suite.baseApp.GetMaximumBlockGas(ctx))

suite.baseApp.StoreConsensusParams(ctx, &cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: -1}})
suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: -1}})
require.Equal(t, uint64(0), suite.baseApp.GetMaximumBlockGas(ctx))

suite.baseApp.StoreConsensusParams(ctx, &cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 5000000}})
suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 5000000}})
require.Equal(t, uint64(5000000), suite.baseApp.GetMaximumBlockGas(ctx))

suite.baseApp.StoreConsensusParams(ctx, &cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: -5000000}})
suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: -5000000}})
require.Panics(t, func() { suite.baseApp.GetMaximumBlockGas(ctx) })
}

Expand Down
4 changes: 2 additions & 2 deletions baseapp/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// ParamStore defines the interface the parameter store used by the BaseApp must
// fulfill.
type ParamStore interface {
Get(ctx context.Context) (*cmtproto.ConsensusParams, error)
Get(ctx context.Context) (cmtproto.ConsensusParams, error)
Has(ctx context.Context) (bool, error)
Set(ctx context.Context, cp *cmtproto.ConsensusParams) error
Set(ctx context.Context, cp cmtproto.ConsensusParams) error
}
2 changes: 1 addition & 1 deletion baseapp/params_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func GetConsensusParams(ctx sdk.Context, paramStore LegacyParamStore) *cmtproto.

func MigrateParams(ctx sdk.Context, lps LegacyParamStore, ps ParamStore) {
if cp := GetConsensusParams(ctx, lps); cp != nil {
ps.Set(ctx, cp)
ps.Set(ctx, *cp)
Fixed Show fixed Hide fixed
} else {
ctx.Logger().Info("warning: consensus parameters are undefined; skipping migration")
}
Expand Down
14 changes: 7 additions & 7 deletions baseapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ type paramStore struct {

var _ baseapp.ParamStore = (*paramStore)(nil)

func (ps *paramStore) Set(_ context.Context, value *cmtproto.ConsensusParams) error {
func (ps paramStore) Set(_ context.Context, value cmtproto.ConsensusParams) error {
bz, err := json.Marshal(value)
if err != nil {
return err
Expand All @@ -250,26 +250,26 @@ func (ps *paramStore) Set(_ context.Context, value *cmtproto.ConsensusParams) er
return ps.db.Set(ParamStoreKey, bz)
}

func (ps *paramStore) Has(_ context.Context) (bool, error) {
func (ps paramStore) Has(_ context.Context) (bool, error) {
return ps.db.Has(ParamStoreKey)
}

func (ps paramStore) Get(ctx context.Context) (*cmtproto.ConsensusParams, error) {
func (ps paramStore) Get(_ context.Context) (cmtproto.ConsensusParams, error) {
bz, err := ps.db.Get(ParamStoreKey)
if err != nil {
return nil, err
return cmtproto.ConsensusParams{}, err
}

if len(bz) == 0 {
return nil, errors.New("params not found")
return cmtproto.ConsensusParams{}, errors.New("params not found")
}

var params cmtproto.ConsensusParams
if err := json.Unmarshal(bz, &params); err != nil {
return nil, err
return cmtproto.ConsensusParams{}, err
}

return &params, nil
return params, nil
}

func setTxSignature(t *testing.T, builder client.TxBuilder, nonce uint64) {
Expand Down
2 changes: 1 addition & 1 deletion proto/cosmos/consensus/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ option go_package = "github.com/cosmos/cosmos-sdk/x/consensus/types";
// Query defines the gRPC querier service.
service Query {
// Params queries the parameters of x/consensus_param module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
rpc GetParams(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/cosmos/consensus/v1/params";
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func isZeroExportedApp(a types.ExportedApp) bool {
return a.AppState == nil &&
len(a.Validators) == 0 &&
a.Height == 0 &&
a.ConsensusParams == nil
a.ConsensusParams == cmtproto.ConsensusParams{}
}

// mockExporter provides an Export method matching server/types.AppExporter,
Expand All @@ -127,7 +127,7 @@ type mockExporter struct {
// when e.Export is called.
func (e *mockExporter) SetDefaultExportApp() {
e.ExportApp = types.ExportedApp{
ConsensusParams: &cmtproto.ConsensusParams{
ConsensusParams: cmtproto.ConsensusParams{
Block: &cmtproto.BlockParams{
MaxBytes: 5 * 1024 * 1024,
MaxGas: -1,
Expand Down
2 changes: 1 addition & 1 deletion server/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type (
// Height is the app's latest block height.
Height int64
// ConsensusParams are the exported consensus params for ABCI.
ConsensusParams *cmtproto.ConsensusParams
ConsensusParams cmtproto.ConsensusParams
}

// AppExporter is a function that dumps all app state to
Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func NewSimApp(

// set the BaseApp's parameter store
app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]), authtypes.NewModuleAddress(govtypes.ModuleName).String())
bApp.SetParamStore(&app.ConsensusParamsKeeper)
bApp.SetParamStore(app.ConsensusParamsKeeper.Params)

// add keepers
app.AccountKeeper = authkeeper.NewAccountKeeper(appCodec, keys[authtypes.StoreKey], authtypes.ProtoBaseAccount, maccPerms, sdk.Bech32MainPrefix, authtypes.NewModuleAddress(govtypes.ModuleName).String())
Expand Down
2 changes: 1 addition & 1 deletion simapp/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (app SimApp) RegisterUpgradeHandlers() {
UpgradeName,
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// Migrate CometBFT consensus parameters from x/params module to a dedicated x/consensus module.
baseapp.MigrateParams(ctx, baseAppLegacySS, &app.ConsensusParamsKeeper)
baseapp.MigrateParams(ctx, baseAppLegacySS, app.ConsensusParamsKeeper.Params)

// Note: this migration is optional,
// You can include x/gov proposal migration documented in [UPGRADING.md](https://github.com/cosmos/cosmos-sdk/blob/main/UPGRADING.md)
Expand Down
8 changes: 4 additions & 4 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Context struct {
checkTx bool
recheckTx bool // if recheckTx == true, then checkTx must also be true
minGasPrice DecCoins
consParams *cmtproto.ConsensusParams
consParams cmtproto.ConsensusParams
eventManager EventManagerI
priority int64 // The tx priority, only relevant in CheckTx
kvGasConfig storetypes.GasConfig
Expand Down Expand Up @@ -79,8 +79,8 @@ func (c Context) HeaderHash() []byte {
return hash
}

func (c Context) ConsensusParams() *cmtproto.ConsensusParams {
return proto.Clone(c.consParams).(*cmtproto.ConsensusParams)
func (c Context) ConsensusParams() cmtproto.ConsensusParams {
return c.consParams
}

func (c Context) Deadline() (deadline time.Time, ok bool) {
Expand Down Expand Up @@ -239,7 +239,7 @@ func (c Context) WithMinGasPrices(gasPrices DecCoins) Context {
}

// WithConsensusParams returns a Context with an updated consensus params
func (c Context) WithConsensusParams(params *cmtproto.ConsensusParams) Context {
func (c Context) WithConsensusParams(params cmtproto.ConsensusParams) Context {
c.consParams = params
return c
}
Expand Down
Loading