Skip to content

Commit

Permalink
chore: Improved logging in IBC Core (#2976)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton authored Jan 9, 2023
1 parent 48efd8e commit 6b38711
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
23 changes: 23 additions & 0 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package logging

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// SdkEventsToLogArguments converts a given sdk.Events and returns a slice of strings that provide human
// readable values for the event attributes.
func SdkEventsToLogArguments(events sdk.Events) []string {
logArgs := []string{"events"}
for _, e := range events {
logArgs = append(logArgs, fmt.Sprintf("type=%s", e.Type))
for _, attr := range e.Attributes {
if len(attr.Value) == 0 {
continue
}
logArgs = append(logArgs, fmt.Sprintf("%s=%s", attr.Key, attr.Value))
}
}
return logArgs
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/cosmos/ibc-go/v6/internal/logging"
icatypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v6/modules/core/24-host"
Expand Down Expand Up @@ -72,8 +73,11 @@ func (k Keeper) registerInterchainAccount(ctx sdk.Context, connectionID, portID,
return "", err
}

events := res.GetEvents()
k.Logger(ctx).Debug("emitting interchain account registration events", logging.SdkEventsToLogArguments(events))

// NOTE: The sdk msg handler creates a new EventManager, so events must be correctly propagated back to the current context
ctx.EventManager().EmitEvents(res.GetEvents())
ctx.EventManager().EmitEvents(events)

firstMsgResponse := res.MsgResponses[0]
channelOpenInitResponse, ok := firstMsgResponse.GetCachedValue().(*channeltypes.MsgChannelOpenInitResponse)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
Expand All @@ -24,20 +26,24 @@ func NewMigrator(keeper *Keeper) Migrator {
// are owned by the controller submodule and ibc.
func (m Migrator) AssertChannelCapabilityMigrations(ctx sdk.Context) error {
if m.keeper != nil {
logger := m.keeper.Logger(ctx)
filteredChannels := m.keeper.channelKeeper.GetAllChannelsWithPortPrefix(ctx, icatypes.ControllerPortPrefix)
for _, ch := range filteredChannels {
name := host.ChannelCapabilityPath(ch.PortId, ch.ChannelId)
capability, found := m.keeper.scopedKeeper.GetCapability(ctx, name)
if !found {
logger.Error(fmt.Sprintf("failed to find capability: %s", name))
return sdkerrors.Wrapf(capabilitytypes.ErrCapabilityNotFound, "failed to find capability: %s", name)
}

isAuthenticated := m.keeper.scopedKeeper.AuthenticateCapability(ctx, capability, name)
if !isAuthenticated {
logger.Error(fmt.Sprintf("expected capability owner: %s", controllertypes.SubModuleName))
return sdkerrors.Wrapf(capabilitytypes.ErrCapabilityNotOwned, "expected capability owner: %s", controllertypes.SubModuleName)
}

m.keeper.SetMiddlewareEnabled(ctx, ch.PortId, ch.ConnectionHops[0])
logger.Info("successfully migrated channel capability", "name", name)
}
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ func (s msgServer) RegisterInterchainAccount(goCtx context.Context, msg *types.M

channelID, err := s.registerInterchainAccount(ctx, msg.ConnectionId, portID, msg.Version)
if err != nil {
s.Logger(ctx).Error("error registering interchain account", "error", err.Error())
return nil, err
}

s.Logger(ctx).Info("successfully registered interchain account", "channel-id", channelID)

return &types.MsgRegisterInterchainAccountResponse{
ChannelId: channelID,
}, nil
Expand Down
2 changes: 2 additions & 0 deletions modules/apps/27-interchain-accounts/host/keeper/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (k Keeper) OnChanOpenTry(
interchainAccAddr, found := k.GetInterchainAccountAddress(ctx, metadata.HostConnectionId, counterparty.PortId)
if found {
// reopening an interchain account
k.Logger(ctx).Info("reopening existing interchain account", "address", interchainAccAddr)
accAddress = sdk.MustAccAddressFromBech32(interchainAccAddr)
if _, ok := k.accountKeeper.GetAccount(ctx, accAddress).(*icatypes.InterchainAccount); !ok {
return "", sdkerrors.Wrapf(icatypes.ErrInvalidAccountReopening, "existing account address %s, does not have interchain account type", accAddress)
Expand All @@ -88,6 +89,7 @@ func (k Keeper) OnChanOpenTry(
if err != nil {
return "", err
}
k.Logger(ctx).Info("successfully created new interchain account", "host-connection-id", metadata.HostConnectionId, "port-id", counterparty.PortId, "address", accAddress)
}

metadata.Address = accAddress.String()
Expand Down
Loading

0 comments on commit 6b38711

Please sign in to comment.