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 GetAppVersion to ICS4Wrapper #1022

Merged
merged 18 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (middleware) [\#1022](https://github.com/cosmos/ibc-go/pull/1022) Add `GetUnwrappedChannelVersion` to the ICS4Wrapper interface. This function should be used by IBC applications to obtain their own version since the version set in the channel structure may be wrapped many times by middleware.
* (testing) [\#942](https://github.com/cosmos/ibc-go/pull/942) `NewTestChain` will create 4 validators in validator set by default. A new constructor function `NewTestChainWithValSet` is provided for test writers who want custom control over the validator set of test chains.
* (testing) [\#904](https://github.com/cosmos/ibc-go/pull/904) Add `ParsePacketFromEvents` function to the testing package. Useful when sending/relaying packets via the testing package.
* (testing) [\#893](https://github.com/cosmos/ibc-go/pull/893) Support custom private keys for testing.
Expand Down
18 changes: 18 additions & 0 deletions docs/ibc/middleware/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Middleware interface {
type ICS4Wrapper interface {
SendPacket(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet exported.Packet) error
WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet exported.Packet, ack []byte) error
GetChannelVersion(ctx sdk.Context, portID, channelID string) (string, bool)
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
}
```

Expand Down Expand Up @@ -239,4 +240,21 @@ func SendPacket(appPacket channeltypes.Packet) {

return ics4Keeper.SendPacket(packet)
}

// middleware must unwrap the channel version so the underlying application
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
// which calls this function receives the version it constructed.
func GetUnwrappedChannelVersion(ctx sdk.Context, portID, channelID string) (string, bool) {
version, found := ics4Keeper.GetChannelVersion(ctx, portID, channelID)
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
if !found {
return "", false
}

// unwrap channel version
metadata, err := Unmarshal(version)
if err != nil {
panic()
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
}

return metadata.AppVersion, true
}
```
10 changes: 10 additions & 0 deletions modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ func (k Keeper) SetChannel(ctx sdk.Context, portID, channelID string, channel ty
store.Set(host.ChannelKey(portID, channelID), bz)
}

// GetUnwrappedChannelVersion gets the version for the specified channel.
func (k Keeper) GetUnwrappedChannelVersion(ctx sdk.Context, portID, channelID string) (string, bool) {
channel, found := k.GetChannel(ctx, portID, channelID)
if !found {
return "", false
}

return channel.Version, true
}

// GetNextChannelSequence gets the next channel sequence from the store.
func (k Keeper) GetNextChannelSequence(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
Expand Down
19 changes: 19 additions & 0 deletions modules/core/04-channel/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
ibcmock "github.com/cosmos/ibc-go/v3/testing/mock"
)

// KeeperTestSuite is a testing suite to test keeper functions.
Expand Down Expand Up @@ -62,6 +63,24 @@ func (suite *KeeperTestSuite) TestSetChannel() {
suite.Equal(expectedCounterparty, storedChannel.Counterparty)
}

func (suite *KeeperTestSuite) TestGetUnwrappedChannelVersion() {
// create client and connections on both chains
path := ibctesting.NewPath(suite.chainA, suite.chainB)
suite.coordinator.SetupConnections(path)

version, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetUnwrappedChannelVersion(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
suite.Require().False(found)
suite.Require().Empty(version)

// init channel
err := path.EndpointA.ChanOpenInit()
suite.NoError(err)

channelVersion, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetUnwrappedChannelVersion(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
suite.Require().True(found)
suite.Require().Equal(ibcmock.Version, channelVersion)
}

// TestGetAllChannels creates multiple channels on chain A through various connections
// and tests their retrieval. 2 channels are on connA0 and 1 channel is on connA1
func (suite KeeperTestSuite) TestGetAllChannels() {
Expand Down