Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Feb 20, 2024
1 parent b95e7ea commit f183441
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
1 change: 0 additions & 1 deletion x/consensus/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (k Keeper) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*

// 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
Expand Down
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)
}
})
}
}

0 comments on commit f183441

Please sign in to comment.