Skip to content

Commit

Permalink
x/ibc: client consensus_height event (cosmos#6381)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze authored Jun 10, 2020
1 parent 6851c84 commit 55796fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 9 additions & 1 deletion x/ibc/02-client/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/evidence"
Expand All @@ -15,7 +17,10 @@ import (
func HandleMsgCreateClient(ctx sdk.Context, k Keeper, msg exported.MsgCreateClient) (*sdk.Result, error) {
clientType := exported.ClientTypeFromString(msg.GetClientType())

var clientState exported.ClientState
var (
clientState exported.ClientState
consensusHeight uint64
)

switch clientType {
case exported.Tendermint:
Expand All @@ -29,9 +34,11 @@ func HandleMsgCreateClient(ctx sdk.Context, k Keeper, msg exported.MsgCreateClie
if err != nil {
return nil, err
}
consensusHeight = msg.GetConsensusState().GetHeight()
case exported.Localhost:
// msg client id is always "localhost"
clientState = localhosttypes.NewClientState(ctx.ChainID(), ctx.BlockHeight())
consensusHeight = uint64(ctx.BlockHeight())
default:
return nil, sdkerrors.Wrap(ErrInvalidClientType, msg.GetClientType())
}
Expand All @@ -48,6 +55,7 @@ func HandleMsgCreateClient(ctx sdk.Context, k Keeper, msg exported.MsgCreateClie
EventTypeCreateClient,
sdk.NewAttribute(AttributeKeyClientID, msg.GetClientID()),
sdk.NewAttribute(AttributeKeyClientType, msg.GetClientType()),
sdk.NewAttribute(types.AttributeKeyConsensusHeight, fmt.Sprintf("%d", consensusHeight)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
Expand Down
9 changes: 7 additions & 2 deletions x/ibc/02-client/keeper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ func (k Keeper) UpdateClient(ctx sdk.Context, clientID string, header exported.H
}

var (
consensusState exported.ConsensusState
err error
consensusState exported.ConsensusState
consensusHeight uint64
err error
)

switch clientType {
Expand All @@ -80,6 +81,7 @@ func (k Keeper) UpdateClient(ctx sdk.Context, clientID string, header exported.H
ctx.ChainID(), // use the chain ID from context since the client is from the running chain (i.e self).
ctx.BlockHeight(),
)
consensusHeight = uint64(ctx.BlockHeight())
default:
err = types.ErrInvalidClientType
}
Expand All @@ -93,6 +95,7 @@ func (k Keeper) UpdateClient(ctx sdk.Context, clientID string, header exported.H
// we don't set consensus state for localhost client
if header != nil && clientType != exported.Localhost {
k.SetClientConsensusState(ctx, clientID, header.GetHeight(), consensusState)
consensusHeight = consensusState.GetHeight()
}

k.Logger(ctx).Info(fmt.Sprintf("client %s updated to height %d", clientID, clientState.GetLatestHeight()))
Expand All @@ -103,6 +106,7 @@ func (k Keeper) UpdateClient(ctx sdk.Context, clientID string, header exported.H
types.EventTypeUpdateClient,
sdk.NewAttribute(types.AttributeKeyClientID, clientID),
sdk.NewAttribute(types.AttributeKeyClientType, clientType.String()),
sdk.NewAttribute(types.AttributeKeyConsensusHeight, fmt.Sprintf("%d", consensusHeight)),
),
)

Expand Down Expand Up @@ -155,6 +159,7 @@ func (k Keeper) CheckMisbehaviourAndUpdateState(ctx sdk.Context, misbehaviour ex
types.EventTypeSubmitMisbehaviour,
sdk.NewAttribute(types.AttributeKeyClientID, misbehaviour.GetClientID()),
sdk.NewAttribute(types.AttributeKeyClientType, misbehaviour.ClientType().String()),
sdk.NewAttribute(types.AttributeKeyConsensusHeight, fmt.Sprintf("%d", consensusState.GetHeight())),
),
)

Expand Down
5 changes: 3 additions & 2 deletions x/ibc/02-client/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

// IBC client events
const (
AttributeKeyClientID = "client_id"
AttributeKeyClientType = "client_type"
AttributeKeyClientID = "client_id"
AttributeKeyClientType = "client_type"
AttributeKeyConsensusHeight = "consensus_height"
)

// IBC client events vars
Expand Down

0 comments on commit 55796fb

Please sign in to comment.