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

feat(consensus): add consensus message #19483

Merged
merged 13 commits into from
Feb 23, 2024
1,366 changes: 1,366 additions & 0 deletions api/cosmos/consensus/v1/consensus.pulsar.go

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions proto/cosmos/consensus/v1/consensus.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Since: cosmos-sdk 0.51
syntax = "proto3";
package cosmos.consensus.v1;

import "tendermint/types/params.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/consensus/types";

// ConMsgParams is the Msg/Params request type. This is a consensus message that is sent from cometbft.
message ConMsgParams {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Is there a reason to abbreviate Consensus?

Copy link
Member Author

Choose a reason for hiding this comment

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

smaller footprint since larger names impact overall size

// params defines the x/consensus parameters to be passed from comet.
//
// NOTE: All parameters must be supplied.
tendermint.types.VersionParams version = 1;
tendermint.types.BlockParams block = 2;
tendermint.types.EvidenceParams evidence = 3;
tendermint.types.ValidatorParams validator = 4;
tendermint.types.ABCIParams abci = 5;
}

// ConMsgParamsResponse defines the response structure for executing a
// ConMsgParams message.
message ConMsgParamsResponse {}
17 changes: 17 additions & 0 deletions x/consensus/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,20 @@ func (k Keeper) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*

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
func (k Keeper) SetParams(ctx context.Context, req *types.ConMsgParams) (*types.ConMsgParamsResponse, error) {
consensusParams, err := req.ToProtoConsensusParams()
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 {
return nil, err
}

return &types.ConMsgParamsResponse{}, nil
}
98 changes: 98 additions & 0 deletions x/consensus/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,101 @@ func (s *KeeperTestSuite) TestUpdateParams() {
})
}
}
func (s *KeeperTestSuite) TestSetParams() {
defaultConsensusParams := cmttypes.DefaultConsensusParams().ToProto()
testCases := []struct {
name string
input *types.ConMsgParams
expErr bool
expErrMsg string
}{
{
name: "valid params",
input: &types.ConMsgParams{
Version: &cmtproto.VersionParams{App: 1},
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
},
expErr: false,
expErrMsg: "",
},
{
name: "invalid params",
input: &types.ConMsgParams{
Version: &cmtproto.VersionParams{App: 1},
Block: &cmtproto.BlockParams{MaxGas: -10, MaxBytes: -10},
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
},
expErr: true,
expErrMsg: "block.MaxBytes must be -1 or greater than 0. Got -10",
},
{
name: "nil version params",
input: &types.ConMsgParams{
Version: nil,
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
},
expErr: true,
expErrMsg: "all parameters must be present",
},
{
name: "nil evidence params",
input: &types.ConMsgParams{
Version: &cmtproto.VersionParams{App: 1},
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: nil,
},
expErr: true,
expErrMsg: "all parameters must be present",
},
{
name: "nil block params",
input: &types.ConMsgParams{
Version: &cmtproto.VersionParams{App: 1},
Block: nil,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
},
expErr: true,
expErrMsg: "all parameters must be present",
},
{
name: "nil validator params",
input: &types.ConMsgParams{
Version: &cmtproto.VersionParams{App: 1},
Block: defaultConsensusParams.Block,
Validator: nil,
Evidence: defaultConsensusParams.Evidence,
},
expErr: true,
expErrMsg: "all parameters must be present",
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
s.SetupTest()
_, err := s.consensusParamsKeeper.SetParams(s.ctx, tc.input)
if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
s.Require().NoError(err)

res, err := s.consensusParamsKeeper.Params(s.ctx, &types.QueryParamsRequest{})
s.Require().NoError(err)

s.Require().Equal(tc.input.Abci, res.Params.Abci)
s.Require().Equal(tc.input.Block, res.Params.Block)
s.Require().Equal(tc.input.Evidence, res.Params.Evidence)
s.Require().Equal(tc.input.Validator, res.Params.Validator)
}
})
}
}
5 changes: 5 additions & 0 deletions x/consensus/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,8 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule {

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// RegisterConsensusMessages registers the consensus module's messages.
func (am AppModule) RegisterConsensusMessages(builder any) {
// std.RegisterConsensusHandler(builder ,am.keeper.SetParams) // TODO uncomment when api is available
}
42 changes: 42 additions & 0 deletions x/consensus/types/consensus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package types

import (
"errors"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmttypes "github.com/cometbft/cometbft/types"
)

func (msg ConMsgParams) ToProtoConsensusParams() (cmtproto.ConsensusParams, error) {
if msg.Evidence == nil || msg.Block == nil || msg.Validator == nil || msg.Version == nil {
return cmtproto.ConsensusParams{}, errors.New("all parameters must be present")
}

cp := cmtproto.ConsensusParams{
Block: &cmtproto.BlockParams{
MaxBytes: msg.Block.MaxBytes,
MaxGas: msg.Block.MaxGas,
},
Evidence: &cmtproto.EvidenceParams{
MaxAgeNumBlocks: msg.Evidence.MaxAgeNumBlocks,
MaxAgeDuration: msg.Evidence.MaxAgeDuration,
MaxBytes: msg.Evidence.MaxBytes,
},
Validator: &cmtproto.ValidatorParams{
PubKeyTypes: msg.Validator.PubKeyTypes,
},
Version: cmttypes.DefaultConsensusParams().ToProto().Version, // Version is stored in x/upgrade
}

if msg.Abci != nil {
cp.Abci = &cmtproto.ABCIParams{
VoteExtensionsEnableHeight: msg.Abci.VoteExtensionsEnableHeight,
}
}

if msg.Version != nil {
cp.Version.App = msg.Version.App
}

return cp, nil
}
Loading
Loading