diff --git a/x/circuit/keeper/msg_server.go b/x/circuit/keeper/msg_server.go index 262e21ec6356..b4976ec4318c 100644 --- a/x/circuit/keeper/msg_server.go +++ b/x/circuit/keeper/msg_server.go @@ -129,10 +129,7 @@ func (srv msgServer) TripCircuitBreaker(ctx context.Context, msg *types.MsgTripC return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "account does not have permission to trip circuit breaker") } - var urls string - if len(msg.GetMsgTypeUrls()) > 1 { - urls = strings.Join(msg.GetMsgTypeUrls(), ",") - } + urls := strings.Join(msg.GetMsgTypeUrls(), ",") sdkCtx := sdk.UnwrapSDKContext(ctx) sdkCtx.EventManager().EmitEvents(sdk.Events{ @@ -183,10 +180,7 @@ func (srv msgServer) ResetCircuitBreaker(ctx context.Context, msg *types.MsgRese return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "account does not have permission to reset circuit breaker") } - var urls string - if len(msg.GetMsgTypeUrls()) > 1 { - urls = strings.Join(msg.GetMsgTypeUrls(), ",") - } + urls := strings.Join(msg.GetMsgTypeUrls(), ",") sdkCtx := sdk.UnwrapSDKContext(ctx) sdkCtx.EventManager().EmitEvents(sdk.Events{ diff --git a/x/circuit/keeper/msg_server_test.go b/x/circuit/keeper/msg_server_test.go index 4c567cdf0569..a3a91b370823 100644 --- a/x/circuit/keeper/msg_server_test.go +++ b/x/circuit/keeper/msg_server_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "context" "testing" "github.com/stretchr/testify/require" @@ -8,6 +9,8 @@ import ( "cosmossdk.io/collections" "cosmossdk.io/x/circuit/keeper" "cosmossdk.io/x/circuit/types" + + sdk "github.com/cosmos/cosmos-sdk/types" ) const msgSend = "cosmos.bank.v1beta1.MsgSend" @@ -38,6 +41,16 @@ func TestAuthorizeCircuitBreaker(t *testing.T) { msg = &types.MsgAuthorizeCircuitBreaker{Granter: authority, Grantee: addresses[2], Permissions: &allmsgs} _, err = srv.AuthorizeCircuitBreaker(ft.ctx, msg) require.NoError(t, err) + require.Equal( + t, + sdk.NewEvent( + "authorize_circuit_breaker", + sdk.NewAttribute("granter", authority), + sdk.NewAttribute("grantee", addresses[2]), + sdk.NewAttribute("permission", allmsgs.String()), + ), + lastEvent(ft.ctx), + ) add2, err := ft.ac.StringToBytes(addresses[2]) require.NoError(t, err) @@ -64,6 +77,16 @@ func TestAuthorizeCircuitBreaker(t *testing.T) { msg = &types.MsgAuthorizeCircuitBreaker{Granter: authority, Grantee: addresses[3], Permissions: &somemsgs} _, err = srv.AuthorizeCircuitBreaker(ft.ctx, msg) require.NoError(t, err) + require.Equal( + t, + sdk.NewEvent( + "authorize_circuit_breaker", + sdk.NewAttribute("granter", authority), + sdk.NewAttribute("grantee", addresses[3]), + sdk.NewAttribute("permission", somemsgs.String()), + ), + lastEvent(ft.ctx), + ) add3, err := ft.ac.StringToBytes(addresses[3]) require.NoError(t, err) @@ -101,6 +124,15 @@ func TestTripCircuitBreaker(t *testing.T) { admintrip := &types.MsgTripCircuitBreaker{Authority: authority, MsgTypeUrls: []string{url}} _, err = srv.TripCircuitBreaker(ft.ctx, admintrip) require.NoError(t, err) + require.Equal( + t, + sdk.NewEvent( + "trip_circuit_breaker", + sdk.NewAttribute("authority", authority), + sdk.NewAttribute("msg_url", url), + ), + lastEvent(ft.ctx), + ) allowed, err := ft.keeper.IsAllowed(ft.ctx, url) require.NoError(t, err) @@ -118,6 +150,15 @@ func TestTripCircuitBreaker(t *testing.T) { superTrip := &types.MsgTripCircuitBreaker{Authority: addresses[1], MsgTypeUrls: []string{url2}} _, err = srv.TripCircuitBreaker(ft.ctx, superTrip) require.NoError(t, err) + require.Equal( + t, + sdk.NewEvent( + "trip_circuit_breaker", + sdk.NewAttribute("authority", addresses[1]), + sdk.NewAttribute("msg_url", url2), + ), + lastEvent(ft.ctx), + ) allowed, err = ft.keeper.IsAllowed(ft.ctx, url2) require.NoError(t, err) @@ -128,7 +169,7 @@ func TestTripCircuitBreaker(t *testing.T) { _, err = srv.TripCircuitBreaker(ft.ctx, unknownTrip) require.Error(t, err) - // user has permission to trip circuit breaker for two messages but only has permission for one + // user tries to trip circuit breaker for two messages but only has permission for one url, url2 = "cosmos.staking.v1beta1.MsgCreateValidator", "cosmos.staking.v1beta1.MsgEditValidator" somemsgs := &types.Permissions{Level: types.Permissions_LEVEL_SOME_MSGS, LimitTypeUrls: []string{url}} msg = &types.MsgAuthorizeCircuitBreaker{Granter: authority, Grantee: addresses[2], Permissions: somemsgs} @@ -168,6 +209,15 @@ func TestResetCircuitBreaker(t *testing.T) { adminReset := &types.MsgResetCircuitBreaker{Authority: authority, MsgTypeUrls: []string{url}} _, err = srv.ResetCircuitBreaker(ft.ctx, adminReset) require.NoError(t, err) + require.Equal( + t, + sdk.NewEvent( + "reset_circuit_breaker", + sdk.NewAttribute("authority", authority), + sdk.NewAttribute("msg_url", url), + ), + lastEvent(ft.ctx), + ) allowed, err = ft.keeper.IsAllowed(ft.ctx, url) require.NoError(t, err) @@ -206,6 +256,15 @@ func TestResetCircuitBreaker(t *testing.T) { allMsgsReset := &types.MsgResetCircuitBreaker{Authority: addresses[1], MsgTypeUrls: []string{url}} _, err = srv.ResetCircuitBreaker(ft.ctx, allMsgsReset) require.NoError(t, err) + require.Equal( + t, + sdk.NewEvent( + "reset_circuit_breaker", + sdk.NewAttribute("authority", addresses[1]), + sdk.NewAttribute("msg_url", url), + ), + lastEvent(ft.ctx), + ) // user tries to reset a message they dont have permission to reset url = "cosmos.staking.v1beta1.MsgCreateValidator" @@ -223,9 +282,25 @@ func TestResetCircuitBreaker(t *testing.T) { someMsgsReset := &types.MsgResetCircuitBreaker{Authority: addresses[2], MsgTypeUrls: []string{url}} _, err = srv.ResetCircuitBreaker(ft.ctx, someMsgsReset) require.NoError(t, err) + require.Equal( + t, + sdk.NewEvent( + "reset_circuit_breaker", + sdk.NewAttribute("authority", addresses[2]), + sdk.NewAttribute("msg_url", url), + ), + lastEvent(ft.ctx), + ) // user tries to reset an already reset circuit breaker admintrip = &types.MsgTripCircuitBreaker{Authority: addresses[1], MsgTypeUrls: []string{url2}} _, err = srv.TripCircuitBreaker(ft.ctx, admintrip) require.Error(t, err) } + +func lastEvent(ctx context.Context) sdk.Event { + sdkCtx := sdk.UnwrapSDKContext(ctx) + events := sdkCtx.EventManager().Events() + + return events[len(events)-1] +}