From ed14ec03b63773cf41c9e9d72ff5c3d7cb122364 Mon Sep 17 00:00:00 2001 From: Marko Date: Wed, 11 Oct 2023 12:20:18 +0200 Subject: [PATCH] chore: check for nil params (#18041) Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com> --- CHANGELOG.md | 1 + x/consensus/keeper/keeper.go | 5 ++++- x/consensus/keeper/keeper_test.go | 33 +++++++++++++++++++++++++++++++ x/consensus/types/msgs.go | 10 ++++++++-- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1eabfb228..0c1b3dd8c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -161,6 +161,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (types) [#17885](https://github.com/cosmos/cosmos-sdk/pull/17885) `InitGenesis` & `ExportGenesis` now take `context.Context` instead of `sdk.Context` * (x/auth) [#17985](https://github.com/cosmos/cosmos-sdk/pull/17985) Remove `StdTxConfig` * Remove depreacted `MakeTestingEncodingParams` from `simapp/params` +* (x/consensus) [#18041](https://github.com/cosmos/cosmos-sdk/pull/18041) `ToProtoConsensusParams()` returns an error ### CLI Breaking Changes diff --git a/x/consensus/keeper/keeper.go b/x/consensus/keeper/keeper.go index 6c429f9db7..31315de405 100644 --- a/x/consensus/keeper/keeper.go +++ b/x/consensus/keeper/keeper.go @@ -67,7 +67,10 @@ func (k Keeper) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (* return nil, fmt.Errorf("invalid authority; expected %s, got %s", k.GetAuthority(), msg.Authority) } - consensusParams := msg.ToProtoConsensusParams() + consensusParams, err := msg.ToProtoConsensusParams() + if err != nil { + return nil, err + } if err := cmttypes.ConsensusParamsFromProto(consensusParams).ValidateBasic(); err != nil { return nil, err } diff --git a/x/consensus/keeper/keeper_test.go b/x/consensus/keeper/keeper_test.go index 2d39d1ca25..d4e8e63619 100644 --- a/x/consensus/keeper/keeper_test.go +++ b/x/consensus/keeper/keeper_test.go @@ -175,6 +175,39 @@ func (s *KeeperTestSuite) TestUpdateParams() { expErr: true, expErrMsg: "invalid authority", }, + { + name: "nil evidence params", + input: &types.MsgUpdateParams{ + Authority: s.consensusParamsKeeper.GetAuthority(), + Block: defaultConsensusParams.Block, + Validator: defaultConsensusParams.Validator, + Evidence: nil, + }, + expErr: true, + expErrMsg: "all parameters must be present", + }, + { + name: "nil block params", + input: &types.MsgUpdateParams{ + Authority: s.consensusParamsKeeper.GetAuthority(), + Block: nil, + Validator: defaultConsensusParams.Validator, + Evidence: defaultConsensusParams.Evidence, + }, + expErr: true, + expErrMsg: "all parameters must be present", + }, + { + name: "nil validator params", + input: &types.MsgUpdateParams{ + Authority: s.consensusParamsKeeper.GetAuthority(), + Block: defaultConsensusParams.Block, + Validator: nil, + Evidence: defaultConsensusParams.Evidence, + }, + expErr: true, + expErrMsg: "all parameters must be present", + }, } for _, tc := range testCases { diff --git a/x/consensus/types/msgs.go b/x/consensus/types/msgs.go index 64307f4c3c..b62c0052f7 100644 --- a/x/consensus/types/msgs.go +++ b/x/consensus/types/msgs.go @@ -1,6 +1,8 @@ package types import ( + "errors" + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" cmttypes "github.com/cometbft/cometbft/types" @@ -9,7 +11,11 @@ import ( var _ sdk.Msg = &MsgUpdateParams{} -func (msg MsgUpdateParams) ToProtoConsensusParams() cmtproto.ConsensusParams { +func (msg MsgUpdateParams) ToProtoConsensusParams() (cmtproto.ConsensusParams, error) { + if msg.Evidence == nil || msg.Block == nil || msg.Validator == nil { + return cmtproto.ConsensusParams{}, errors.New("all parameters must be present") + } + cp := cmtproto.ConsensusParams{ Block: &cmtproto.BlockParams{ MaxBytes: msg.Block.MaxBytes, @@ -32,5 +38,5 @@ func (msg MsgUpdateParams) ToProtoConsensusParams() cmtproto.ConsensusParams { } } - return cp + return cp, nil }