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

fix(x/consensus): apply ConsensusParam update rules according to CometBFT spec #20314

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
18 changes: 16 additions & 2 deletions x/consensus/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"cosmossdk.io/errors"

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

Check failure on line 17 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / dependency-review

"github.com/cosmos/cosmos-sdk/types" imported as sdk and not used

Check failure on line 17 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (02)

"github.com/cosmos/cosmos-sdk/types" imported as sdk and not used

Check failure on line 17 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (03)

"github.com/cosmos/cosmos-sdk/types" imported as sdk and not used

Check failure on line 17 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (03)

"github.com/cosmos/cosmos-sdk/types" imported as sdk and not used

Check failure on line 17 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (01)

"github.com/cosmos/cosmos-sdk/types" imported as sdk and not used

Check failure on line 17 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (01)

"github.com/cosmos/cosmos-sdk/types" imported as sdk and not used

Check failure on line 17 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

"github.com/cosmos/cosmos-sdk/types" imported as sdk and not used

Check failure on line 17 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

"github.com/cosmos/cosmos-sdk/types" imported as sdk and not used

Check failure on line 17 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

"github.com/cosmos/cosmos-sdk/types" imported as sdk and not used

Check failure on line 17 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

"github.com/cosmos/cosmos-sdk/types" imported as sdk and not used
"github.com/cosmos/cosmos-sdk/x/consensus/exported"
"github.com/cosmos/cosmos-sdk/x/consensus/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
Expand Down Expand Up @@ -72,11 +73,24 @@
if err != nil {
return nil, err
}
if err := cmttypes.ConsensusParamsFromProto(consensusParams).ValidateBasic(); err != nil {

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

nextParams := params.Update(&consensusParams)

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

if err := params.ValidateUpdate(&consensusParams, k.HeaderService.HeaderInfo(ctx).Height); err != nil {

Check failure on line 89 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / dependency-review

k.HeaderService undefined (type Keeper has no field or method HeaderService)

Check failure on line 89 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (02)

k.HeaderService undefined (type Keeper has no field or method HeaderService)

Check failure on line 89 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (03)

k.HeaderService undefined (type Keeper has no field or method HeaderService)

Check failure on line 89 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (03)

k.HeaderService undefined (type Keeper has no field or method HeaderService)

Check failure on line 89 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (01)

k.HeaderService undefined (type Keeper has no field or method HeaderService)

Check failure on line 89 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (01)

k.HeaderService undefined (type Keeper has no field or method HeaderService)

Check failure on line 89 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

k.HeaderService undefined (type Keeper has no field or method HeaderService)

Check failure on line 89 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

k.HeaderService undefined (type Keeper has no field or method HeaderService)

Check failure on line 89 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

k.HeaderService undefined (type Keeper has no field or method HeaderService)

Check failure on line 89 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

k.HeaderService undefined (type Keeper has no field or method HeaderService)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

k.HeaderService doesn't seem to work on release/v0.50.x. Consider reverting it

return nil, err
}

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

Expand Down
125 changes: 106 additions & 19 deletions x/consensus/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type KeeperTestSuite struct {
func (s *KeeperTestSuite) SetupTest() {
key := storetypes.NewKVStoreKey(consensusparamkeeper.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{})
header := cmtproto.Header{Height: 5}
ctx := testCtx.Ctx.WithBlockHeader(header)
encCfg := moduletestutil.MakeTestEncodingConfig()
storeService := runtime.NewKVStoreService(key)

Expand All @@ -43,14 +44,23 @@ func (s *KeeperTestSuite) SetupTest() {
queryHelper := baseapp.NewQueryServerTestHelper(ctx, encCfg.InterfaceRegistry)
types.RegisterQueryServer(queryHelper, keeper)
s.queryClient = types.NewQueryClient(queryHelper)
err := s.consensusParamsKeeper.ParamsStore.Set(ctx, cmttypes.DefaultConsensusParams().ToProto())
s.Require().NoError(err)
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func (s *KeeperTestSuite) TestGRPCQueryConsensusParams() {
defaultConsensusParams := cmttypes.DefaultConsensusParams().ToProto()
// Create ConsensusParams with modified fields
modifiedConsensusParams := cmttypes.DefaultConsensusParams().ToProto()
modifiedConsensusParams.Block.MaxBytes++
modifiedConsensusParams.Block.MaxGas = 100
modifiedConsensusParams.Evidence.MaxAgeDuration++
modifiedConsensusParams.Evidence.MaxAgeNumBlocks++
modifiedConsensusParams.Evidence.MaxBytes++
modifiedConsensusParams.Validator.PubKeyTypes = []string{cmttypes.ABCIPubKeyTypeSecp256k1}

testCases := []struct {
msg string
Expand All @@ -65,18 +75,22 @@ func (s *KeeperTestSuite) TestGRPCQueryConsensusParams() {
func() {
input := &types.MsgUpdateParams{
Authority: s.consensusParamsKeeper.GetAuthority(),
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
Block: modifiedConsensusParams.Block,
Validator: modifiedConsensusParams.Validator,
Evidence: modifiedConsensusParams.Evidence,
}
s.consensusParamsKeeper.UpdateParams(s.ctx, input)
_, err := s.consensusParamsKeeper.UpdateParams(s.ctx, input)
s.Require().NoError(err)
},
types.QueryParamsResponse{
Params: &cmtproto.ConsensusParams{
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
Version: defaultConsensusParams.Version,
Block: modifiedConsensusParams.Block,
Validator: modifiedConsensusParams.Validator,
Evidence: modifiedConsensusParams.Evidence,
Version: modifiedConsensusParams.Version,
Abci: &cmtproto.ABCIParams{
VoteExtensionsEnableHeight: 0,
},
},
},
true,
Expand All @@ -87,21 +101,22 @@ func (s *KeeperTestSuite) TestGRPCQueryConsensusParams() {
func() {
input := &types.MsgUpdateParams{
Authority: s.consensusParamsKeeper.GetAuthority(),
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
Block: modifiedConsensusParams.Block,
Validator: modifiedConsensusParams.Validator,
Evidence: modifiedConsensusParams.Evidence,
Abci: &cmtproto.ABCIParams{
VoteExtensionsEnableHeight: 1234,
},
}
s.consensusParamsKeeper.UpdateParams(s.ctx, input)
_, err := s.consensusParamsKeeper.UpdateParams(s.ctx, input)
s.Require().NoError(err)
},
types.QueryParamsResponse{
Params: &cmtproto.ConsensusParams{
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
Version: defaultConsensusParams.Version,
Block: modifiedConsensusParams.Block,
Validator: modifiedConsensusParams.Validator,
Evidence: modifiedConsensusParams.Evidence,
Version: modifiedConsensusParams.Version,
Abci: &cmtproto.ABCIParams{
VoteExtensionsEnableHeight: 1234,
},
Expand Down Expand Up @@ -204,6 +219,76 @@ func (s *KeeperTestSuite) TestUpdateParams() {
expErr: true,
expErrMsg: "all parameters must be present",
},
{
name: "valid ABCI update",
input: &types.MsgUpdateParams{
Authority: s.consensusParamsKeeper.GetAuthority(),
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
Abci: &cmtproto.ABCIParams{
VoteExtensionsEnableHeight: 1235,
},
},
expErr: false,
expErrMsg: "",
},
{
name: "noop ABCI update",
input: &types.MsgUpdateParams{
Authority: s.consensusParamsKeeper.GetAuthority(),
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
Abci: &cmtproto.ABCIParams{
VoteExtensionsEnableHeight: 1235,
},
},
expErr: false,
expErrMsg: "",
},
{
name: "valid ABCI clear",
input: &types.MsgUpdateParams{
Authority: s.consensusParamsKeeper.GetAuthority(),
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
Abci: &cmtproto.ABCIParams{
VoteExtensionsEnableHeight: 0,
},
},
expErr: false,
expErrMsg: "",
},
{
name: "invalid ABCI update - current height",
input: &types.MsgUpdateParams{
Authority: s.consensusParamsKeeper.GetAuthority(),
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
Abci: &cmtproto.ABCIParams{
VoteExtensionsEnableHeight: 5,
},
},
expErr: true,
expErrMsg: "vote extensions cannot be updated to a past or current height",
},
{
name: "invalid ABCI update - past height",
input: &types.MsgUpdateParams{
Authority: s.consensusParamsKeeper.GetAuthority(),
Block: defaultConsensusParams.Block,
Validator: defaultConsensusParams.Validator,
Evidence: defaultConsensusParams.Evidence,
Abci: &cmtproto.ABCIParams{
VoteExtensionsEnableHeight: 4,
},
},
expErr: true,
expErrMsg: "vote extensions cannot be updated to a past or current height",
},
}

for _, tc := range testCases {
Expand All @@ -220,7 +305,9 @@ func (s *KeeperTestSuite) TestUpdateParams() {
res, err := s.consensusParamsKeeper.Params(s.ctx, &types.QueryParamsRequest{})
s.Require().NoError(err)

s.Require().Equal(tc.input.Abci, res.Params.Abci)
if tc.input.Abci != nil {
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)
Expand Down
Loading