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

ica: genesis state implementation #481

Merged
merged 12 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
40 changes: 38 additions & 2 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
- [InterchainAccount](#ibc.applications.interchain_accounts.v1.InterchainAccount)

- [ibc/applications/interchain_accounts/v1/genesis.proto](#ibc/applications/interchain_accounts/v1/genesis.proto)
- [ActiveChannel](#ibc.applications.interchain_accounts.v1.ActiveChannel)
- [GenesisState](#ibc.applications.interchain_accounts.v1.GenesisState)
- [RegisteredInterchainAccount](#ibc.applications.interchain_accounts.v1.RegisteredInterchainAccount)

- [ibc/applications/interchain_accounts/v1/query.proto](#ibc/applications/interchain_accounts/v1/query.proto)
- [QueryInterchainAccountAddressRequest](#ibc.applications.interchain_accounts.v1.QueryInterchainAccountAddressRequest)
Expand Down Expand Up @@ -311,15 +313,49 @@ An InterchainAccount is defined as a BaseAccount & the address of the account ow



<a name="ibc.applications.interchain_accounts.v1.ActiveChannel"></a>

### ActiveChannel
ActiveChannel contains a pairing of port ID and channel ID for an active interchain accounts channel


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `port_id` | [string](#string) | | |
| `channel_id` | [string](#string) | | |






<a name="ibc.applications.interchain_accounts.v1.GenesisState"></a>

### GenesisState
GenesisState defines the interchain_account genesis state
GenesisState defines the interchain accounts genesis state


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `active_channels` | [ActiveChannel](#ibc.applications.interchain_accounts.v1.ActiveChannel) | repeated | |
| `interchain_accounts` | [RegisteredInterchainAccount](#ibc.applications.interchain_accounts.v1.RegisteredInterchainAccount) | repeated | |
| `ports` | [string](#string) | repeated | |






<a name="ibc.applications.interchain_accounts.v1.RegisteredInterchainAccount"></a>

### RegisteredInterchainAccount
RegisteredInterchainAccount contains a pairing of controller port ID and associated interchain account address


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `port_id` | [string](#string) | | |
| `account_address` | [string](#string) | | |



Expand Down Expand Up @@ -416,7 +452,7 @@ Body of a tx for an ics27 IBC packet
<a name="ibc.applications.interchain_accounts.v1.InterchainAccountPacketData"></a>

### InterchainAccountPacketData
InterchainAccountPacketData is comprised of araw transaction,type of transaction and optional memo field.
InterchainAccountPacketData is comprised of a raw transaction, type of transaction and optional memo field.


| Field | Type | Label | Description |
Expand Down
48 changes: 40 additions & 8 deletions modules/apps/27-interchain-accounts/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,51 @@ import (

// InitGenesis initializes the interchain accounts application state from a provided genesis state
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, state types.GenesisState) {
if !keeper.IsBound(ctx, state.PortId) {
cap := keeper.BindPort(ctx, state.PortId)
if err := keeper.ClaimCapability(ctx, cap, host.PortPath(state.PortId)); err != nil {
panic(fmt.Sprintf("could not claim port capability: %v", err))
for _, portID := range state.Ports {
if !keeper.IsBound(ctx, portID) {
cap := keeper.BindPort(ctx, portID)
if err := keeper.ClaimCapability(ctx, cap, host.PortPath(portID)); err != nil {
panic(fmt.Sprintf("could not claim port capability: %v", err))
}
}
}

for _, ch := range state.ActiveChannels {
keeper.SetActiveChannel(ctx, ch.PortId, ch.ChannelId)
}

for _, acc := range state.InterchainAccounts {
keeper.SetInterchainAccountAddress(ctx, acc.PortId, acc.AccountAddress)
}
}

// ExportGenesis exports transfer module's portID into its geneis state
// ExportGenesis returns the interchain accounts exported genesis
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
portID := keeper.GetPort(ctx)
var (
activeChannels []*types.ActiveChannel
interchainAccounts []*types.RegisteredInterchainAccount
)

ports := keeper.GetAllPorts(ctx)
for _, portID := range ports {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
if channelID, found := keeper.GetActiveChannel(ctx, portID); found {
activeChan := &types.ActiveChannel{
PortId: portID,
ChannelId: channelID,
}

return &types.GenesisState{
PortId: portID,
activeChannels = append(activeChannels, activeChan)
}

if accountAddr, found := keeper.GetInterchainAccountAddress(ctx, portID); found {
interchainAcc := &types.RegisteredInterchainAccount{
PortId: portID,
AccountAddress: accountAddr,
}

interchainAccounts = append(interchainAccounts, interchainAcc)
}
}

return types.NewGenesisState(ports, activeChannels, interchainAccounts)
}
55 changes: 55 additions & 0 deletions modules/apps/27-interchain-accounts/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package interchain_accounts_test

import (
ica "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts"
"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"
)

func (suite *InterchainAccountsTestSuite) TestInitGenesis() {
suite.SetupTest()

genesisState := types.GenesisState{
Ports: []string{types.PortID, TestPortID},
ActiveChannels: []*types.ActiveChannel{
{
PortId: TestPortID,
ChannelId: "channel-0",
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
},
},
InterchainAccounts: []*types.RegisteredInterchainAccount{
{
PortId: TestPortID,
AccountAddress: TestAccAddress.String(),
},
},
}

ica.InitGenesis(suite.chainA.GetContext(), suite.chainA.GetSimApp().ICAKeeper, genesisState)

channelID, found := suite.chainA.GetSimApp().ICAKeeper.GetActiveChannel(suite.chainA.GetContext(), TestPortID)
suite.Require().True(found)
suite.Require().Equal("channel-0", channelID)

accountAdrr, found := suite.chainA.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), TestPortID)
suite.Require().True(found)
suite.Require().Equal(TestAccAddress.String(), accountAdrr)
}

func (suite *InterchainAccountsTestSuite) TestExportGenesis() {
suite.SetupTest()
path := NewICAPath(suite.chainA, suite.chainB)
suite.coordinator.SetupConnections(path)

err := SetupICAPath(path, TestOwnerAddress)
suite.Require().NoError(err)

genesisState := ica.ExportGenesis(suite.chainA.GetContext(), suite.chainA.GetSimApp().ICAKeeper)

suite.Require().Equal([]string{types.PortID, TestPortID}, genesisState.GetPorts())

suite.Require().Equal(path.EndpointA.ChannelID, genesisState.ActiveChannels[0].ChannelId)
suite.Require().Equal(path.EndpointA.ChannelConfig.PortID, genesisState.ActiveChannels[0].PortId)

suite.Require().Equal(TestAccAddress.String(), genesisState.InterchainAccounts[0].AccountAddress)
suite.Require().Equal(path.EndpointA.ChannelConfig.PortID, genesisState.InterchainAccounts[0].PortId)
}
29 changes: 23 additions & 6 deletions modules/apps/27-interchain-accounts/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
"strings"

baseapp "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -86,16 +87,32 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", host.ModuleName, types.ModuleName))
}

// GetPort returns the portID for the interchain accounts module. Used in ExportGenesis
func (k Keeper) GetPort(ctx sdk.Context) string {
// GetAllPorts returns all bound ports for the interchain accounts module. Used in ExportGenesis
func (k Keeper) GetAllPorts(ctx sdk.Context) []string {
store := ctx.KVStore(k.storeKey)
return string(store.Get([]byte(types.PortKey)))
iterator := sdk.KVStorePrefixIterator(store, []byte(types.PortKeyPrefix))
defer iterator.Close()

var ports []string
for ; iterator.Valid(); iterator.Next() {
keySplit := strings.Split(string(iterator.Key()), "/")

ports = append(ports, keySplit[1])
}

return ports
}

// HasPort returns true if the provided portID is found in state, otherwise false
func (k Keeper) HasPort(ctx sdk.Context, portID string) bool {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
store := ctx.KVStore(k.storeKey)
return store.Has(types.KeyPort(portID))
}

// BindPort stores the provided portID and binds to it, returning the associated capability
func (k Keeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability {
store := ctx.KVStore(k.storeKey)
store.Set([]byte(types.PortKey), []byte(portID))
store.Set(types.KeyPort(portID), []byte{0x01})

return k.portKeeper.BindPort(ctx, portID)
}
Expand All @@ -117,9 +134,9 @@ func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability
}

// GetActiveChannel retrieves the active channelID from the store keyed by the provided portID
func (k Keeper) GetActiveChannel(ctx sdk.Context, portId string) (string, bool) {
func (k Keeper) GetActiveChannel(ctx sdk.Context, portID string) (string, bool) {
store := ctx.KVStore(k.storeKey)
key := types.KeyActiveChannel(portId)
key := types.KeyActiveChannel(portID)

if !store.Has(key) {
return "", false
Expand Down
14 changes: 11 additions & 3 deletions modules/apps/27-interchain-accounts/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,17 @@ func (suite *KeeperTestSuite) TestIsBound() {
suite.Require().True(isBound)
}

func (suite *KeeperTestSuite) TestGetPort() {
port := suite.chainA.GetSimApp().ICAKeeper.GetPort(suite.chainA.GetContext())
suite.Require().Equal(types.PortID, port)
func (suite *KeeperTestSuite) TestGetAllPorts() {
suite.SetupTest()
path := NewICAPath(suite.chainA, suite.chainB)
suite.coordinator.SetupConnections(path)

err := SetupICAPath(path, TestOwnerAddress)
suite.Require().NoError(err)

ports := suite.chainA.GetSimApp().ICAKeeper.GetAllPorts(suite.chainA.GetContext())
suite.Require().Contains(ports, types.PortID)
suite.Require().Contains(ports, TestPortID)
}

func (suite *KeeperTestSuite) TestGetInterchainAccountAddress() {
Expand Down
13 changes: 12 additions & 1 deletion modules/apps/27-interchain-accounts/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package types

// DefaultGenesis creates and returns the default interchain accounts GenesisState
// The default GenesisState includes the standard port identifier to which all host chains must bind
func DefaultGenesis() *GenesisState {
return &GenesisState{
PortId: PortID,
Ports: []string{PortID},
}
}

// NewGenesisState creates a returns a new GenesisState instance
func NewGenesisState(ports []string, channels []*ActiveChannel, accounts []*RegisteredInterchainAccount) *GenesisState {
return &GenesisState{
ActiveChannels: channels,
InterchainAccounts: accounts,
Ports: ports,
}
}
Loading