Skip to content

Commit

Permalink
fix(x/circuit): msg_url is skipped when publishing events (backport #…
Browse files Browse the repository at this point in the history
…17105) (#17108)

Co-authored-by: Yaroslav Savchuk <savchukyarpolk@gmail.com>
  • Loading branch information
mergify[bot] and ysv authored Jul 24, 2023
1 parent d68a123 commit 31c4fcd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
10 changes: 2 additions & 8 deletions x/circuit/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
77 changes: 76 additions & 1 deletion x/circuit/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package keeper_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -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]
}

0 comments on commit 31c4fcd

Please sign in to comment.