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: Add handshake logic to ics29 #307

Merged
merged 26 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7b51ebd
do handshake logic, create test file
AdityaSripal Aug 3, 2021
150211d
do cap logic and fix build
AdityaSripal Aug 5, 2021
ae319ea
open handshake implementation and tests
AdityaSripal Sep 2, 2021
b8f2a7a
remove prints
AdityaSripal Sep 2, 2021
fb8522b
Merge branch 'ics29-fee-middleware' into aditya/handshake
AdityaSripal Sep 2, 2021
1f62964
Update modules/apps/29-fee/module.go
AdityaSripal Sep 3, 2021
4711774
debugging progress
AdityaSripal Sep 10, 2021
dac74bc
fee enabled flag
AdityaSripal Sep 16, 2021
d190a19
cleanup handshake logic
AdityaSripal Sep 20, 2021
2f73fd4
fix merge
AdityaSripal Sep 20, 2021
f49f0b4
fix tests
AdityaSripal Sep 20, 2021
4a73249
much cleaner simapp
AdityaSripal Sep 20, 2021
d622916
split module.go file
AdityaSripal Sep 20, 2021
094fef9
cleanup and docs
AdityaSripal Sep 20, 2021
763159a
assert IBC interfaces are fulfilled in middleware
AdityaSripal Sep 20, 2021
f60897b
Update modules/apps/transfer/module.go
AdityaSripal Sep 20, 2021
a167c49
fix merge
AdityaSripal Sep 20, 2021
bc90365
Apply suggestions from code review
AdityaSripal Sep 23, 2021
b9028a2
fix unnecessary crossing hello logic
AdityaSripal Sep 24, 2021
d37a7f7
fix version negotiation bugs and improve tests
AdityaSripal Sep 24, 2021
3deee69
Merge branch 'aditya/handshake' of github.com:cosmos/ibc-go into adit…
AdityaSripal Sep 24, 2021
131e95a
cleanup tests
AdityaSripal Sep 24, 2021
40783bf
Apply suggestions from code review
AdityaSripal Sep 24, 2021
24d2a20
Apply suggestions from code review
AdityaSripal Sep 27, 2021
6b45015
address rest of colin comments
AdityaSripal Sep 27, 2021
56d80fa
Merge branch 'aditya/handshake' of github.com:cosmos/ibc-go into adit…
AdityaSripal Sep 27, 2021
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
2 changes: 1 addition & 1 deletion docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ GenesisState defines the ibc-transfer genesis state

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `port_id` | [string](#string) | | |
| `port_ids` | [string](#string) | repeated | |
| `denom_traces` | [DenomTrace](#ibc.applications.transfer.v1.DenomTrace) | repeated | |
| `params` | [Params](#ibc.applications.transfer.v1.Params) | | |

Expand Down
38 changes: 38 additions & 0 deletions modules/apps/29-fee/fee_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fee_test

import (
"testing"

fee "github.com/cosmos/ibc-go/modules/apps/29-fee"
feekeeper "github.com/cosmos/ibc-go/modules/apps/29-fee/keeper"
"github.com/cosmos/ibc-go/modules/apps/transfer"
ibctesting "github.com/cosmos/ibc-go/testing"
"github.com/stretchr/testify/suite"
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
)

type FeeTestSuite struct {
suite.Suite

coordinator *ibctesting.Coordinator

chainA *ibctesting.TestChain
chainB *ibctesting.TestChain

module fee.AppModule
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
keeper feekeeper.Keeper
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
}

func (suite *FeeTestSuite) SetupTest() {
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2)
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0))
suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1))

suite.keeper = suite.chainA.GetSimApp().IBCFeeKeeper

transferModule := transfer.NewAppModule(suite.chainA.GetSimApp().TransferKeeper)
suite.module = fee.NewAppModule(suite.keeper, suite.chainA.GetSimApp().ScopedIBCFeeKeeper, transferModule)
}

func TestIBCFeeTestSuite(t *testing.T) {
suite.Run(t, new(FeeTestSuite))
}
17 changes: 1 addition & 16 deletions modules/apps/29-fee/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package keeper

/*
import (
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/ibc-go/modules/apps/transfer/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
Expand All @@ -25,7 +23,6 @@ type Keeper struct {
scopedKeeper capabilitykeeper.ScopedKeeper
}


// NewKeeper creates a new 29-fee Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
Expand Down Expand Up @@ -59,7 +56,7 @@ func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
// order to expose it to module's InitGenesis function
func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
cap := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, cap, host.PortPath(portID))
return k.scopedKeeper.ClaimCapability(ctx, cap, host.PortPath(portID))
}

// GetPort returns the portID for the transfer module. Used in ExportGenesis
Expand All @@ -73,15 +70,3 @@ func (k Keeper) SetPort(ctx sdk.Context, portID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.PortKey, []byte(portID))
}

// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
return k.scopedKeeper.AuthenticateCapability(ctx, cap, name)
}

// ClaimCapability allows the transfer module that can claim a capability that IBC module
// passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}
*/
153 changes: 111 additions & 42 deletions modules/apps/29-fee/module.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package fee

/*
import (
"context"
"encoding/json"
"fmt"
"math/rand"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -20,12 +18,16 @@ import (
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/ibc-go/modules/apps/transfer/client/cli"
"github.com/cosmos/ibc-go/modules/apps/transfer/keeper"
"github.com/cosmos/ibc-go/modules/apps/transfer/simulation"
"github.com/cosmos/ibc-go/modules/apps/transfer/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
"github.com/cosmos/ibc-go/modules/apps/29-fee/client/cli"
"github.com/cosmos/ibc-go/modules/apps/29-fee/keeper"
"github.com/cosmos/ibc-go/modules/apps/29-fee/types"

// "github.com/cosmos/ibc-go/modules/apps/29-fee/client/cli"
// "github.com/cosmos/ibc-go/modules/apps/29-fee/simulation"
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/modules/core/exported"
)

Expand All @@ -48,30 +50,32 @@ func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}

// RegisterInterfaces registers module concrete types into protobuf Any.
func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
types.RegisterInterfaces(registry)
// types.RegisterInterfaces(registry)
}

// DefaultGenesis returns default genesis state as raw bytes for the ibc
// transfer module.
// 29-fee module.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesisState())
// return cdc.MustMarshalJSON(types.DefaultGenesisState())
return nil
}

// ValidateGenesis performs genesis state validation for the 29-fee module.
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
var gs types.GenesisState
if err := cdc.UnmarshalJSON(bz, &gs); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}
// var gs types.GenesisState
// if err := cdc.UnmarshalJSON(bz, &gs); err != nil {
// return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
// }

return gs.Validate()
// return gs.Validate()
return nil
}

// RegisterRESTRoutes implements AppModuleBasic interface
func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) {
}

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the ibc-transfer module.
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the ibc-29-fee module.
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))
}
Expand All @@ -89,13 +93,17 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command {
// AppModule represents the AppModule for this module
type AppModule struct {
AppModuleBasic
keeper keeper.Keeper
keeper keeper.Keeper
scopedKeeper capabilitykeeper.ScopedKeeper
app porttypes.IBCModule
}

// NewAppModule creates a new 29-fee module
func NewAppModule(k keeper.Keeper) AppModule {
func NewAppModule(k keeper.Keeper, scopedKeeper capabilitykeeper.ScopedKeeper, app porttypes.IBCModule) AppModule {
return AppModule{
keeper: k,
keeper: k,
scopedKeeper: scopedKeeper,
app: app,
}
}

Expand All @@ -121,24 +129,25 @@ func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
// types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
// types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

// InitGenesis performs genesis initialization for the ibc-transfer module. It returns
// InitGenesis performs genesis initialization for the ibc-29-fee module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
am.keeper.InitGenesis(ctx, genesisState)
// var genesisState types.GenesisState
// cdc.MustUnmarshalJSON(data, &genesisState)
// am.keeper.InitGenesis(ctx, genesisState)
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the exported genesis state as raw bytes for the ibc-transfer
// ExportGenesis returns the exported genesis state as raw bytes for the ibc-29-fee
// module.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
gs := am.keeper.ExportGenesis(ctx)
return cdc.MustMarshalJSON(gs)
// gs := am.keeper.ExportGenesis(ctx)
// return cdc.MustMarshalJSON(gs)
return nil
}

// ConsensusVersion implements AppModule/ConsensusVersion.
Expand All @@ -155,27 +164,28 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V

// AppModuleSimulation functions

// GenerateGenesisState creates a randomized GenState of the transfer module.
// GenerateGenesisState creates a randomized GenState of the 29-fee module.
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
// simulation.RandomizedGenState(simState)
}

// ProposalContents doesn't return any content functions for governance proposals.
func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent {
return nil
}

// RandomizedParams creates randomized ibc-transfer param changes for the simulator.
// RandomizedParams creates randomized ibc-29-fee param changes for the simulator.
func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
return simulation.ParamChanges(r)
// return simulation.ParamChanges(r)
return nil
}

// RegisterStoreDecoder registers a decoder for transfer module's types
// RegisterStoreDecoder registers a decoder for 29-fee module's types
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore(am.keeper)
// sdr[types.StoreKey] = simulation.NewDecodeStore(am.keeper)
}

// WeightedOperations returns the all the transfer module operations with their respective weights.
// WeightedOperations returns the all the 29-fee module operations with their respective weights.
func (am AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation {
return nil
}
Expand All @@ -191,7 +201,23 @@ func (am AppModule) OnChanOpenInit(
counterparty channeltypes.Counterparty,
version string,
) error {
return nil
feeVersion, appVersion := channeltypes.SplitChannelVersion(version)
if feeVersion != types.Version {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "expected: %s, got: %s", types.Version, feeVersion)
}
// Claim channel capability passed back by IBC module
if err := am.scopedKeeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
return err
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
}

appCap, err := am.scopedKeeper.NewCapability(ctx, types.AppCapabilityName(portID, channelID))
if err != nil {
return sdkerrors.Wrap(err, "could not create capability for underlying application")
}

// call underlying app's OnChanOpenInit callback with the appVersion
return am.app.OnChanOpenInit(ctx, order, connectionHops, portID, channelID,
appCap, counterparty, appVersion)
}

// OnChanOpenTry implements the IBCModule interface
Expand All @@ -206,7 +232,43 @@ func (am AppModule) OnChanOpenTry(
version,
counterpartyVersion string,
) error {
return nil
feeVersion, appVersion := channeltypes.SplitChannelVersion(version)
cpFeeVersion, cpAppVersion := channeltypes.SplitChannelVersion(counterpartyVersion)

if feeVersion != types.Version {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "expected version: %s, got: %s", types.Version, feeVersion)
}
if cpFeeVersion != feeVersion {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "expected counterparty version: %s, got: %s", types.Version, cpFeeVersion)
}
var (
appCap *capabilitytypes.Capability
err error
ok bool
)
// Module may have already claimed capability in OnChanOpenInit in the case of crossing hellos
// (ie chainA and chainB both call ChanOpenInit before one of them calls ChanOpenTry)
// If module can already authenticate the capability then module already owns it so we don't need to claim
// Otherwise, module does not have channel capability and we must claim it from IBC
if !am.scopedKeeper.AuthenticateCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)) {
// Only claim channel capability passed back by IBC module if we do not already own it
if err := am.scopedKeeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
return err
}
appCap, err = am.scopedKeeper.NewCapability(ctx, types.AppCapabilityName(portID, channelID))
if err != nil {
return sdkerrors.Wrap(err, "could not create capability for underlying app")
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
appCap, ok = am.scopedKeeper.GetCapability(ctx, types.AppCapabilityName(portID, channelID))
if !ok {
return sdkerrors.Wrap(capabilitytypes.ErrCapabilityNotFound,
"could not find app capability on OnChanOpenTry even after OnChanOpenInit called on this chain first (crossing hellos)")
}
}
// call underlying app's OnChanOpenTry callback with the app versions
return am.app.OnChanOpenTry(ctx, order, connectionHops, portID, channelID,
appCap, counterparty, appVersion, cpAppVersion)
}

// OnChanOpenAck implements the IBCModule interface
Expand All @@ -216,7 +278,13 @@ func (am AppModule) OnChanOpenAck(
channelID string,
counterpartyVersion string,
) error {
return nil
cpFeeVersion, cpAppVersion := channeltypes.SplitChannelVersion(counterpartyVersion)

if cpFeeVersion != types.Version {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "expected counterparty version: %s, got: %s", types.Version, cpFeeVersion)
}
// call underlying app's OnChanOpenAck callback with the counterparty app version.
return am.app.OnChanOpenAck(ctx, portID, channelID, cpAppVersion)
}

// OnChanOpenConfirm implements the IBCModule interface
Expand All @@ -225,7 +293,8 @@ func (am AppModule) OnChanOpenConfirm(
portID,
channelID string,
) error {
return nil
// call underlying app's OnChanOpenConfirm callback.
return am.app.OnChanOpenConfirm(ctx, portID, channelID)
}

// OnChanCloseInit implements the IBCModule interface
Expand All @@ -234,8 +303,8 @@ func (am AppModule) OnChanCloseInit(
portID,
channelID string,
) error {
// Disallow user-initiated channel closing for 29-fee channels
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel")
// TODO: Unescrow all remaining funds for unprocessed packets
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
return am.app.OnChanCloseInit(ctx, portID, channelID)
}

// OnChanCloseConfirm implements the IBCModule interface
Expand All @@ -244,7 +313,8 @@ func (am AppModule) OnChanCloseConfirm(
portID,
channelID string,
) error {
return nil
// TODO: Unescrow all remaining funds for unprocessed packets
return am.app.OnChanCloseConfirm(ctx, portID, channelID)
}

// OnRecvPacket implements the IBCModule interface.
Expand Down Expand Up @@ -274,4 +344,3 @@ func (am AppModule) OnTimeoutPacket(
) error {
return nil
}
*/
Loading