Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: message interceptor #29

Merged
merged 2 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/ibc/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ connection handhake.

The IBC module also has
[`BeginBlock`](https://github.com/cosmos/ibc-go/blob/main/modules/core/02-client/abci.go) logic as
well. This is optional as it is only required if your application uses the [localhost
client](https://github.com/cosmos/ibc/blob/master/spec/client/ics-009-loopback-client) to connect two
well. This is optional as it is only required if your application uses the localhost
client to connect two
different modules from the same chain.

::: tip
Expand Down
24 changes: 0 additions & 24 deletions modules/core/02-client/keeper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import (
func (k Keeper) CreateClient(
ctx sdk.Context, clientState exported.ClientState, consensusState exported.ConsensusState,
) (string, error) {
if k.clientHooks != nil {
err := k.clientHooks.OnCreateClient(ctx, clientState, consensusState)
if err != nil {
return "", err
}
}
params := k.GetParams(ctx)
if !params.IsAllowedClient(clientState.ClientType()) {
return "", sdkerrors.Wrapf(
Expand Down Expand Up @@ -64,12 +58,6 @@ func (k Keeper) CreateClient(

// UpdateClient updates the consensus state and the state root from a provided header.
func (k Keeper) UpdateClient(ctx sdk.Context, clientID string, header exported.Header) error {
if k.clientHooks != nil {
err := k.clientHooks.OnUpdateClient(ctx, clientID, header)
if err != nil {
return err
}
}
clientState, found := k.GetClientState(ctx, clientID)
if !found {
return sdkerrors.Wrapf(types.ErrClientNotFound, "cannot update client with ID %s", clientID)
Expand Down Expand Up @@ -159,12 +147,6 @@ func (k Keeper) UpdateClient(ctx sdk.Context, clientID string, header exported.H
// by the old client at the specified upgrade height
func (k Keeper) UpgradeClient(ctx sdk.Context, clientID string, upgradedClient exported.ClientState, upgradedConsState exported.ConsensusState,
proofUpgradeClient, proofUpgradeConsState []byte) error {
if k.clientHooks != nil {
err := k.clientHooks.OnUpgradeClient(ctx, clientID, upgradedClient, upgradedConsState, proofUpgradeClient, proofUpgradeConsState)
if err != nil {
return err
}
}
clientState, found := k.GetClientState(ctx, clientID)
if !found {
return sdkerrors.Wrapf(types.ErrClientNotFound, "cannot update client with ID %s", clientID)
Expand Down Expand Up @@ -207,12 +189,6 @@ func (k Keeper) UpgradeClient(ctx sdk.Context, clientID string, upgradedClient e
// CheckMisbehaviourAndUpdateState checks for client misbehaviour and freezes the
// client if so.
func (k Keeper) CheckMisbehaviourAndUpdateState(ctx sdk.Context, misbehaviour exported.Misbehaviour) error {
if k.clientHooks != nil {
err := k.clientHooks.OnCheckMisbehaviourAndUpdateState(ctx, misbehaviour)
if err != nil {
return err
}
}
clientState, found := k.GetClientState(ctx, misbehaviour.GetClientID())
if !found {
return sdkerrors.Wrapf(types.ErrClientNotFound, "cannot check misbehaviour for client with ID %s", misbehaviour.GetClientID())
Expand Down
5 changes: 1 addition & 4 deletions modules/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type Keeper struct {
stakingKeeper types.StakingKeeper
upgradeKeeper types.UpgradeKeeper
selfClient exported.SelfClient
clientHooks exported.ClientHooks
}

// NewKeeper creates a new NewKeeper instance
Expand All @@ -36,8 +35,7 @@ func NewKeeper(
paramSpace paramtypes.Subspace,
sk types.StakingKeeper,
uk types.UpgradeKeeper,
selfClient exported.SelfClient,
clientHooks exported.ClientHooks) Keeper {
selfClient exported.SelfClient) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
Expand All @@ -50,7 +48,6 @@ func NewKeeper(
stakingKeeper: sk,
upgradeKeeper: uk,
selfClient: selfClient,
clientHooks: clientHooks,
}
}

Expand Down
32 changes: 0 additions & 32 deletions modules/core/exported/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,38 +261,6 @@ type SelfClient interface {
) (ConsensusState, error)
}

// ClientHooks defines an interface that implements callbacks
// for the client module methods as specified in ICS-02.
// First the callback is called and if no error, proceed to
// the method functionality
type ClientHooks interface {
OnCreateClient(
ctx sdk.Context,
clientState ClientState,
consensusState ConsensusState,
) error

OnUpdateClient(
ctx sdk.Context,
clientID string,
header Header,
) error

OnUpgradeClient(
ctx sdk.Context,
clientID string,
upgradedClient ClientState,
upgradedConsState ConsensusState,
proofUpgradeClient,
proofUpgradeConsState []byte,
) error

OnCheckMisbehaviourAndUpdateState(
ctx sdk.Context,
misbehaviour Misbehaviour,
) error
}

// String returns the string representation of a client status.
func (s Status) String() string {
return string(s)
Expand Down
77 changes: 77 additions & 0 deletions modules/core/ibc_msg_interceptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ibc

import (
"context"

clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v3/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
)

type IBCMsgI interface {
/////////////////////////////////////////////////////////////////////////////
// Keeper
/////////////////////////////////////////////////////////////////////////////

// CreateClient defines a rpc handler method for MsgCreateClient.
CreateClient(goCtx context.Context, msg *clienttypes.MsgCreateClient) (*clienttypes.MsgCreateClientResponse, error)

// UpdateClient defines a rpc handler method for MsgUpdateClient.
UpdateClient(goCtx context.Context, msg *clienttypes.MsgUpdateClient) (*clienttypes.MsgUpdateClientResponse, error)

// UpgradeClient defines a rpc handler method for MsgUpgradeClient.
UpgradeClient(goCtx context.Context, msg *clienttypes.MsgUpgradeClient) (*clienttypes.MsgUpgradeClientResponse, error)

// SubmitMisbehaviour defines a rpc handler method for MsgSubmitMisbehaviour.
SubmitMisbehaviour(goCtx context.Context, msg *clienttypes.MsgSubmitMisbehaviour) (*clienttypes.MsgSubmitMisbehaviourResponse, error)

// ConnectionOpenInit defines a rpc handler method for MsgConnectionOpenInit.
ConnectionOpenInit(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenInit) (*connectiontypes.MsgConnectionOpenInitResponse, error)

// ConnectionOpenTry defines a rpc handler method for MsgConnectionOpenTry.
ConnectionOpenTry(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenTry) (*connectiontypes.MsgConnectionOpenTryResponse, error)

// ConnectionOpenAck defines a rpc handler method for MsgConnectionOpenAck.
ConnectionOpenAck(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenAck) (*connectiontypes.MsgConnectionOpenAckResponse, error)

// ConnectionOpenConfirm defines a rpc handler method for MsgConnectionOpenConfirm.
ConnectionOpenConfirm(goCtx context.Context, msg *connectiontypes.MsgConnectionOpenConfirm) (*connectiontypes.MsgConnectionOpenConfirmResponse, error)

// ChannelOpenInit defines a rpc handler method for MsgChannelOpenInit.
// ChannelOpenInit will perform 04-channel checks, route to the application
// callback, and write an OpenInit channel into state upon successful execution.
ChannelOpenInit(goCtx context.Context, msg *channeltypes.MsgChannelOpenInit) (*channeltypes.MsgChannelOpenInitResponse, error)

// ChannelOpenTry defines a rpc handler method for MsgChannelOpenTry.
// ChannelOpenTry will perform 04-channel checks, route to the application
// callback, and write an OpenTry channel into state upon successful execution.
ChannelOpenTry(goCtx context.Context, msg *channeltypes.MsgChannelOpenTry) (*channeltypes.MsgChannelOpenTryResponse, error)

// ChannelOpenAck defines a rpc handler method for MsgChannelOpenAck.
// ChannelOpenAck will perform 04-channel checks, route to the application
// callback, and write an OpenAck channel into state upon successful execution.
ChannelOpenAck(goCtx context.Context, msg *channeltypes.MsgChannelOpenAck) (*channeltypes.MsgChannelOpenAckResponse, error)

// ChannelOpenConfirm defines a rpc handler method for MsgChannelOpenConfirm.
// ChannelOpenConfirm will perform 04-channel checks, route to the application
// callback, and write an OpenConfirm channel into state upon successful execution.
ChannelOpenConfirm(goCtx context.Context, msg *channeltypes.MsgChannelOpenConfirm) (*channeltypes.MsgChannelOpenConfirmResponse, error)

// ChannelCloseInit defines a rpc handler method for MsgChannelCloseInit.
ChannelCloseInit(goCtx context.Context, msg *channeltypes.MsgChannelCloseInit) (*channeltypes.MsgChannelCloseInitResponse, error)

// ChannelCloseConfirm defines a rpc handler method for MsgChannelCloseConfirm.
ChannelCloseConfirm(goCtx context.Context, msg *channeltypes.MsgChannelCloseConfirm) (*channeltypes.MsgChannelCloseConfirmResponse, error)

// RecvPacket defines a rpc handler method for MsgRecvPacket.
RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPacket) (*channeltypes.MsgRecvPacketResponse, error)

// Timeout defines a rpc handler method for MsgTimeout.
Timeout(goCtx context.Context, msg *channeltypes.MsgTimeout) (*channeltypes.MsgTimeoutResponse, error)

// TimeoutOnClose defines a rpc handler method for MsgTimeoutOnClose.
TimeoutOnClose(goCtx context.Context, msg *channeltypes.MsgTimeoutOnClose) (*channeltypes.MsgTimeoutOnCloseResponse, error)

// Acknowledgement defines a rpc handler method for MsgAcknowledgement.
Acknowledgement(goCtx context.Context, msg *channeltypes.MsgAcknowledgement) (*channeltypes.MsgAcknowledgementResponse, error)
}
10 changes: 5 additions & 5 deletions modules/core/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ type Keeper struct {
Router *porttypes.Router
}

//IBCKeeper initialized in wasm code (in wasmd@v0.28.0/x/wasm/keeper/test_common.go:354)
//We need to maintain backward comptabile interface
// IBCKeeper initialized in wasm code (in wasmd@v0.28.0/x/wasm/keeper/test_common.go:354)
// We need to maintain backward comptabile interface
func NewKeeper(
cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
stakingKeeper clienttypes.StakingKeeper, upgradeKeeper clienttypes.UpgradeKeeper,
scopedKeeper capabilitykeeper.ScopedKeeper,
) *Keeper {
//Using dymint and nil as default. we can panic instead
return NewKeeperWithSelfClient(cdc, key, paramSpace, stakingKeeper, upgradeKeeper, scopedKeeper, ibcdmtypes.NewSelfClient(), nil)
return NewKeeperWithSelfClient(cdc, key, paramSpace, stakingKeeper, upgradeKeeper, scopedKeeper, ibcdmtypes.NewSelfClient())
}

// NewKeeper creates a new ibc Keeper
func NewKeeperWithSelfClient(
cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
stakingKeeper clienttypes.StakingKeeper, upgradeKeeper clienttypes.UpgradeKeeper,
scopedKeeper capabilitykeeper.ScopedKeeper, selfClient exported.SelfClient, clientHooks exported.ClientHooks,
scopedKeeper capabilitykeeper.ScopedKeeper, selfClient exported.SelfClient,
) *Keeper {
// register paramSpace at top level keeper
// set KeyTable if it has not already been set
Expand All @@ -76,7 +76,7 @@ func NewKeeperWithSelfClient(
panic(fmt.Errorf("cannot initialize IBC keeper: empty scoped keeper"))
}

clientKeeper := clientkeeper.NewKeeper(cdc, key, paramSpace, stakingKeeper, upgradeKeeper, selfClient, clientHooks)
clientKeeper := clientkeeper.NewKeeper(cdc, key, paramSpace, stakingKeeper, upgradeKeeper, selfClient)
connectionKeeper := connectionkeeper.NewKeeper(cdc, key, paramSpace, clientKeeper)
portKeeper := portkeeper.NewKeeper(scopedKeeper)
channelKeeper := channelkeeper.NewKeeper(cdc, key, clientKeeper, connectionKeeper, portKeeper, scopedKeeper)
Expand Down
1 change: 0 additions & 1 deletion modules/core/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
upgradeKeeper,
scopedKeeper,
ibctmtypes.NewSelfClient(),
nil,
)
}
)
Expand Down
22 changes: 17 additions & 5 deletions modules/core/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,27 @@ func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry)
// AppModule implements an application module for the ibc module.
type AppModule struct {
AppModuleBasic
keeper *keeper.Keeper
keeper *keeper.Keeper
msgSrvInterceptor IBCMsgI

// create localhost by default
createLocalhost bool
}

// NewAppModuleWithMsgInterceptor creates a new AppModule object
// with customized message server for IBC messages
func NewAppModuleWithMsgInterceptor(k *keeper.Keeper, msgInterceptor IBCMsgI) AppModule {
return AppModule{
keeper: k,
msgSrvInterceptor: msgInterceptor,
}
}

// NewAppModule creates a new AppModule object
func NewAppModule(k *keeper.Keeper) AppModule {
return AppModule{
keeper: k,
keeper: k,
msgSrvInterceptor: k,
}
}

Expand Down Expand Up @@ -132,9 +143,10 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
clienttypes.RegisterMsgServer(cfg.MsgServer(), am.keeper)
connectiontypes.RegisterMsgServer(cfg.MsgServer(), am.keeper)
channeltypes.RegisterMsgServer(cfg.MsgServer(), am.keeper)
clienttypes.RegisterMsgServer(cfg.MsgServer(), am.msgSrvInterceptor)
connectiontypes.RegisterMsgServer(cfg.MsgServer(), am.msgSrvInterceptor)
channeltypes.RegisterMsgServer(cfg.MsgServer(), am.msgSrvInterceptor)

types.RegisterQueryService(cfg.QueryServer(), am.keeper)

m := clientkeeper.NewMigrator(am.keeper.ClientKeeper)
Expand Down
2 changes: 1 addition & 1 deletion testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func NewSimAppWithConsensusType(
}
// Create IBC Keeper
app.IBCKeeper = ibckeeper.NewKeeperWithSelfClient(
appCodec, keys[ibchost.StoreKey], app.GetSubspace(ibchost.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper, selfClient, nil,
appCodec, keys[ibchost.StoreKey], app.GetSubspace(ibchost.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper, selfClient,
)

app.AuthzKeeper = authzkeeper.NewKeeper(keys[authzkeeper.StoreKey], appCodec, app.BaseApp.MsgServiceRouter())
Expand Down