From 9bab52f7142ef2a9b21b9913e5c200d1982b7c13 Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Fri, 22 Apr 2022 07:28:16 -0400 Subject: [PATCH] test(monitoringp): increase code coverage (#736) Co-authored-by: Lucas Bertrand --- testutil/keeper/ibc_mock.go | 3 +- x/monitoringp/keeper/begin_block_test.go | 108 ++++++++++++++++++ .../keeper/grpc_connection_channel_id_test.go | 18 ++- .../keeper/grpc_consumer_client_id_test.go | 18 ++- .../keeper/grpc_monitoring_info_test.go | 18 ++- x/monitoringp/keeper/handshake_test.go | 9 ++ 6 files changed, 161 insertions(+), 13 deletions(-) diff --git a/testutil/keeper/ibc_mock.go b/testutil/keeper/ibc_mock.go index d69b89395..a6b9566d1 100644 --- a/testutil/keeper/ibc_mock.go +++ b/testutil/keeper/ibc_mock.go @@ -61,8 +61,9 @@ func (c ChannelMock) GetChannel(_ sdk.Context, _, channelID string) (channel cha } // GetNextSequenceSend implements ChannelKeeper +// returns true for all cases func (c ChannelMock) GetNextSequenceSend(_ sdk.Context, _, _ string) (uint64, bool) { - return 0, false + return 0, true } // SendPacket implements ChannelKeeper diff --git a/x/monitoringp/keeper/begin_block_test.go b/x/monitoringp/keeper/begin_block_test.go index 213a18e70..8aa7bea24 100644 --- a/x/monitoringp/keeper/begin_block_test.go +++ b/x/monitoringp/keeper/begin_block_test.go @@ -235,3 +235,111 @@ func TestKeeper_ReportBlockSignatures(t *testing.T) { }) } } + +func TestKeeper_TransmitSignatures(t *testing.T) { + ctx, tk, _ := monitoringpKeeperWithFooClient(t) + valFoo, valBar, valBaz, valFred, valQux := sample.Validator(t, r), + sample.Validator(t, r), + sample.Validator(t, r), + sample.Validator(t, r), + sample.Validator(t, r) + + // initialize staking validator set + tk.StakingKeeper.SetValidator(ctx, valFoo) + tk.StakingKeeper.SetValidator(ctx, valBar) + tk.StakingKeeper.SetValidator(ctx, valBaz) + tk.StakingKeeper.SetValidator(ctx, valFred) + tk.StakingKeeper.SetValidator(ctx, valQux) + err := tk.StakingKeeper.SetValidatorByConsAddr(ctx, valFoo) + require.NoError(t, err) + err = tk.StakingKeeper.SetValidatorByConsAddr(ctx, valBar) + require.NoError(t, err) + err = tk.StakingKeeper.SetValidatorByConsAddr(ctx, valBaz) + require.NoError(t, err) + err = tk.StakingKeeper.SetValidatorByConsAddr(ctx, valFred) + require.NoError(t, err) + err = tk.StakingKeeper.SetValidatorByConsAddr(ctx, valQux) + require.NoError(t, err) + + tests := []struct { + name string + monitoringInfoExist bool + inputMonitoringInfo types.MonitoringInfo + lastBlockHeight int64 + currentBlockHeight int64 + channelIDExist bool + channelID types.ConnectionChannelID + expectedMonitoringInfoFound bool + expectedMonitoringInfo types.MonitoringInfo + wantErr bool + }{ + { + name: "currentBlockHeight < lastBlockHeight returns nil", + monitoringInfoExist: false, + lastBlockHeight: 11, + currentBlockHeight: 10, + channelIDExist: false, + expectedMonitoringInfoFound: false, + }, + { + name: "lastBlockHeight no channel ID set returns nil", + monitoringInfoExist: false, + lastBlockHeight: 10, + currentBlockHeight: 11, + channelIDExist: false, + expectedMonitoringInfoFound: false, + }, + { + name: "no monitoring info found", + monitoringInfoExist: false, + lastBlockHeight: 10, + currentBlockHeight: 11, + channelIDExist: true, + channelID: types.ConnectionChannelID{ChannelID: "channelID"}, + expectedMonitoringInfoFound: false, + }, + { + name: "monitoring info found with channel not found", + monitoringInfoExist: true, + inputMonitoringInfo: tc.MonitoringInfo(1, + tc.SignatureCount(t, + valFoo.OperatorAddress, + "1")), + lastBlockHeight: 10, + currentBlockHeight: 11, + channelIDExist: true, + channelID: types.ConnectionChannelID{ChannelID: "channelID"}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // set keeper values + params := tk.MonitoringProviderKeeper.GetParams(ctx) + params.LastBlockHeight = tt.lastBlockHeight + tk.MonitoringProviderKeeper.SetParams(ctx, params) + if tt.monitoringInfoExist { + tk.MonitoringProviderKeeper.SetMonitoringInfo(ctx, tt.inputMonitoringInfo) + } else { + tk.MonitoringProviderKeeper.RemoveMonitoringInfo(ctx) + } + + if tt.channelIDExist { + tk.MonitoringProviderKeeper.SetConnectionChannelID(ctx, tt.channelID) + } + + // report + err := tk.MonitoringProviderKeeper.TransmitSignatures(ctx, tt.currentBlockHeight) + if tt.wantErr { + require.Error(t, err) + return + } + require.NoError(t, err) + + // check saved values + monitoringInfo, found := tk.MonitoringProviderKeeper.GetMonitoringInfo(ctx) + require.EqualValues(t, tt.expectedMonitoringInfoFound, found) + require.EqualValues(t, tt.expectedMonitoringInfo, monitoringInfo) + }) + } +} diff --git a/x/monitoringp/keeper/grpc_connection_channel_id_test.go b/x/monitoringp/keeper/grpc_connection_channel_id_test.go index 864886dee..879836d0b 100644 --- a/x/monitoringp/keeper/grpc_connection_channel_id_test.go +++ b/x/monitoringp/keeper/grpc_connection_channel_id_test.go @@ -16,17 +16,23 @@ import ( func TestConnectionChannelIDQuery(t *testing.T) { ctx, tk, _ := testkeeper.NewTestSetupWithMonitoringp(t) wctx := sdk.WrapSDKContext(ctx) - item := createTestConnectionChannelID(ctx, tk.MonitoringProviderKeeper) for _, tc := range []struct { desc string + setItem bool request *types.QueryGetConnectionChannelIDRequest response *types.QueryGetConnectionChannelIDResponse err error }{ { - desc: "Valid Request", - request: &types.QueryGetConnectionChannelIDRequest{}, - response: &types.QueryGetConnectionChannelIDResponse{ConnectionChannelID: item}, + desc: "object does not exist", + setItem: false, + request: &types.QueryGetConnectionChannelIDRequest{}, + err: status.Error(codes.NotFound, "not found"), + }, + { + desc: "object exists", + setItem: true, + request: &types.QueryGetConnectionChannelIDRequest{}, }, { desc: "Invalid Request", @@ -34,6 +40,10 @@ func TestConnectionChannelIDQuery(t *testing.T) { }, } { t.Run(tc.desc, func(t *testing.T) { + if tc.setItem { + item := createTestConnectionChannelID(ctx, tk.MonitoringProviderKeeper) + tc.response = &types.QueryGetConnectionChannelIDResponse{ConnectionChannelID: item} + } response, err := tk.MonitoringProviderKeeper.ConnectionChannelID(wctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) diff --git a/x/monitoringp/keeper/grpc_consumer_client_id_test.go b/x/monitoringp/keeper/grpc_consumer_client_id_test.go index dade92407..efe75cfcc 100644 --- a/x/monitoringp/keeper/grpc_consumer_client_id_test.go +++ b/x/monitoringp/keeper/grpc_consumer_client_id_test.go @@ -16,17 +16,23 @@ import ( func TestConsumerClientIDQuery(t *testing.T) { ctx, tk, _ := testkeeper.NewTestSetupWithMonitoringp(t) wctx := sdk.WrapSDKContext(ctx) - item := createTestConsumerClientID(ctx, tk.MonitoringProviderKeeper) for _, tc := range []struct { desc string + setItem bool request *types.QueryGetConsumerClientIDRequest response *types.QueryGetConsumerClientIDResponse err error }{ { - desc: "Valid Request", - request: &types.QueryGetConsumerClientIDRequest{}, - response: &types.QueryGetConsumerClientIDResponse{ConsumerClientID: item}, + desc: "object does not exist", + setItem: false, + request: &types.QueryGetConsumerClientIDRequest{}, + err: status.Error(codes.NotFound, "not found"), + }, + { + desc: "object exists", + setItem: true, + request: &types.QueryGetConsumerClientIDRequest{}, }, { desc: "Invalid Request", @@ -34,6 +40,10 @@ func TestConsumerClientIDQuery(t *testing.T) { }, } { t.Run(tc.desc, func(t *testing.T) { + if tc.setItem { + item := createTestConsumerClientID(ctx, tk.MonitoringProviderKeeper) + tc.response = &types.QueryGetConsumerClientIDResponse{ConsumerClientID: item} + } response, err := tk.MonitoringProviderKeeper.ConsumerClientID(wctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) diff --git a/x/monitoringp/keeper/grpc_monitoring_info_test.go b/x/monitoringp/keeper/grpc_monitoring_info_test.go index ec4ce8b8b..cb4995516 100644 --- a/x/monitoringp/keeper/grpc_monitoring_info_test.go +++ b/x/monitoringp/keeper/grpc_monitoring_info_test.go @@ -16,17 +16,23 @@ import ( func TestMonitoringInfoQuery(t *testing.T) { ctx, tk, _ := testkeeper.NewTestSetupWithMonitoringp(t) wctx := sdk.WrapSDKContext(ctx) - item := createTestMonitoringInfo(ctx, tk.MonitoringProviderKeeper) for _, tc := range []struct { desc string + setItem bool request *types.QueryGetMonitoringInfoRequest response *types.QueryGetMonitoringInfoResponse err error }{ { - desc: "Valid Request", - request: &types.QueryGetMonitoringInfoRequest{}, - response: &types.QueryGetMonitoringInfoResponse{MonitoringInfo: item}, + desc: "object does not exist", + setItem: false, + request: &types.QueryGetMonitoringInfoRequest{}, + err: status.Error(codes.NotFound, "not found"), + }, + { + desc: "object exists", + setItem: true, + request: &types.QueryGetMonitoringInfoRequest{}, }, { desc: "Invalid Request", @@ -34,6 +40,10 @@ func TestMonitoringInfoQuery(t *testing.T) { }, } { t.Run(tc.desc, func(t *testing.T) { + if tc.setItem { + item := createTestMonitoringInfo(ctx, tk.MonitoringProviderKeeper) + tc.response = &types.QueryGetMonitoringInfoResponse{MonitoringInfo: item} + } response, err := tk.MonitoringProviderKeeper.MonitoringInfo(wctx, tc.request) if tc.err != nil { require.ErrorIs(t, err, tc.err) diff --git a/x/monitoringp/keeper/handshake_test.go b/x/monitoringp/keeper/handshake_test.go index 01693676c..f68809577 100644 --- a/x/monitoringp/keeper/handshake_test.go +++ b/x/monitoringp/keeper/handshake_test.go @@ -46,6 +46,15 @@ func TestKeeper_VerifyClientIDFromChannelID(t *testing.T) { require.NoError(t, err) }) + t.Run("should fail if channelClientID is not equal to consumerClientID", func(t *testing.T) { + ctx, tk, _ := monitoringpKeeperWithFooClient(t) + tk.MonitoringProviderKeeper.SetConsumerClientID(ctx, types.ConsumerClientID{ + ClientID: "notequal", + }) + err := tk.MonitoringProviderKeeper.VerifyClientIDFromChannelID(ctx, "foo") + require.ErrorIs(t, err, types.ErrInvalidClient) + }) + t.Run("should fail if channel doesn't exist", func(t *testing.T) { ctx, tk, _ := monitoringpKeeperWithFooClient(t) tk.MonitoringProviderKeeper.SetConsumerClientID(ctx, types.ConsumerClientID{