Skip to content

Commit

Permalink
fix(x/consensus): apply ConsensusParam update rules according to Come…
Browse files Browse the repository at this point in the history
…tBFT spec (forward-port of #20314) (#20347)

Co-authored-by: Marko <marko@baricevic.me>
  • Loading branch information
sergio-mena and tac0turtle committed May 10, 2024
1 parent 6a2618f commit eb7cc12
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 191 deletions.
10 changes: 3 additions & 7 deletions testutil/integration/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

cmtabcitypes "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
cmttypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"

"cosmossdk.io/core/address"
Expand Down Expand Up @@ -93,13 +94,8 @@ func NewIntegrationApp(
bApp.SetParamStore(consensusParamsKeeper.ParamsStore)
consensusparamtypes.RegisterQueryServer(grpcRouter, consensusParamsKeeper)

_, err := consensusParamsKeeper.SetParams(sdkCtx, &consensusparamtypes.ConsensusMsgParams{
Version: simtestutil.DefaultConsensusParams.Version,
Block: simtestutil.DefaultConsensusParams.Block,
Evidence: simtestutil.DefaultConsensusParams.Evidence,
Validator: simtestutil.DefaultConsensusParams.Validator,
Abci: simtestutil.DefaultConsensusParams.Abci,
})
params := cmttypes.ConsensusParamsFromProto(*simtestutil.DefaultConsensusParams) // This fills up missing param sections
err := consensusParamsKeeper.ParamsStore.Set(sdkCtx, params.ToProto())
if err != nil {
panic(fmt.Errorf("failed to set consensus params: %w", err))
}
Expand Down
33 changes: 14 additions & 19 deletions x/consensus/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,33 @@ func (k Keeper) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*
if err != nil {
return nil, err
}
if err := cmttypes.ConsensusParamsFromProto(consensusParams).ValidateBasic(); err != nil {
return nil, err
}

if err := k.ParamsStore.Set(ctx, consensusParams); err != nil {
paramsProto, err := k.ParamsStore.Get(ctx)
if err != nil {
return nil, err
}
params := cmttypes.ConsensusParamsFromProto(paramsProto)

if err := k.EventService.EventManager(ctx).EmitKV(
"update_consensus_params",
event.NewAttribute("authority", msg.Authority),
event.NewAttribute("parameters", consensusParams.String())); err != nil {
nextParams := params.Update(&consensusParams)

if err := nextParams.ValidateBasic(); err != nil {
return nil, err
}

return &types.MsgUpdateParamsResponse{}, nil
}

// SetParams sets the consensus parameters on init of a chain. This is a consensus message. It can only be called by the consensus server
// This is used in the consensus message handler set in module.go.
func (k Keeper) SetParams(ctx context.Context, req *types.ConsensusMsgParams) (*types.ConsensusMsgParamsResponse, error) {
consensusParams, err := req.ToProtoConsensusParams()
if err != nil {
if err := params.ValidateUpdate(&consensusParams, k.HeaderService.HeaderInfo(ctx).Height); err != nil {
return nil, err
}
if err := cmttypes.ConsensusParamsFromProto(consensusParams).ValidateBasic(); err != nil {

if err := k.ParamsStore.Set(ctx, nextParams.ToProto()); err != nil {
return nil, err
}

if err := k.ParamsStore.Set(ctx, consensusParams); err != nil {
if err := k.EventService.EventManager(ctx).EmitKV(
"update_consensus_params",
event.NewAttribute("authority", msg.Authority),
event.NewAttribute("parameters", consensusParams.String())); err != nil {
return nil, err
}

return &types.ConsensusMsgParamsResponse{}, nil
return &types.MsgUpdateParamsResponse{}, nil
}

0 comments on commit eb7cc12

Please sign in to comment.