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 9 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
38 changes: 37 additions & 1 deletion 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
31 changes: 20 additions & 11 deletions modules/apps/27-interchain-accounts/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,29 @@ 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))
}
}
}
}

// ExportGenesis exports transfer module's portID into its geneis state
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
// TODO: Using a range query with KVStorePrefixIterator export all port IDs
// See https://github.com/cosmos/ibc-go/issues/448
for _, ch := range state.ActiveChannels {
keeper.SetActiveChannel(ctx, ch.PortId, ch.ChannelId)
}

return &types.GenesisState{
PortId: types.PortID,
for _, acc := range state.InterchainAccounts {
keeper.SetInterchainAccountAddress(ctx, acc.PortId, acc.AccountAddress)
}
}

// ExportGenesis returns the interchain accounts exported genesis
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
return types.NewGenesisState(
keeper.GetAllPorts(ctx),
keeper.GetAllActiveChannels(ctx),
keeper.GetAllInterchainAccounts(ctx),
)
}
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)
}
45 changes: 43 additions & 2 deletions modules/apps/27-interchain-accounts/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,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 All @@ -137,6 +137,27 @@ func (k Keeper) GetActiveChannel(ctx sdk.Context, portId string) (string, bool)
return string(store.Get(key)), true
}

// GetAllActiveChannels returns a list of all active interchain accounts channels and their associated port identifiers
func (k Keeper) GetAllActiveChannels(ctx sdk.Context) []*types.ActiveChannel {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.ActiveChannelKeyPrefix))
defer iterator.Close()

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

ch := &types.ActiveChannel{
PortId: keySplit[1],
ChannelId: string(iterator.Value()),
}

activeChannels = append(activeChannels, ch)
}

return activeChannels
}

// SetActiveChannel stores the active channelID, keyed by the provided portID
func (k Keeper) SetActiveChannel(ctx sdk.Context, portID, channelID string) {
store := ctx.KVStore(k.storeKey)
Expand Down Expand Up @@ -167,6 +188,26 @@ func (k Keeper) GetInterchainAccountAddress(ctx sdk.Context, portID string) (str
return string(store.Get(key)), true
}

// GetAllInterchainAccounts returns a list of all registered interchain account addresses and their associated controller port identifiers
func (k Keeper) GetAllInterchainAccounts(ctx sdk.Context) []*types.RegisteredInterchainAccount {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.OwnerKeyPrefix))

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

acc := &types.RegisteredInterchainAccount{
PortId: keySplit[1],
AccountAddress: string(iterator.Value()),
}

interchainAccounts = append(interchainAccounts, acc)
}

return interchainAccounts
}

// SetInterchainAccountAddress stores the InterchainAccount address, keyed by the associated portID
func (k Keeper) SetInterchainAccountAddress(ctx sdk.Context, portID string, address string) {
store := ctx.KVStore(k.storeKey)
Expand Down
58 changes: 56 additions & 2 deletions modules/apps/27-interchain-accounts/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ func (suite *KeeperTestSuite) TestGetAllPorts() {
err := SetupICAPath(path, TestOwnerAddress)
suite.Require().NoError(err)

expectedPorts := []string{types.PortID, TestPortID}

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

func (suite *KeeperTestSuite) TestGetInterchainAccountAddress() {
Expand All @@ -141,6 +143,58 @@ func (suite *KeeperTestSuite) TestGetInterchainAccountAddress() {
suite.Require().Empty(retrievedAddr)
}

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

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

suite.chainA.GetSimApp().ICAKeeper.SetActiveChannel(suite.chainA.GetContext(), "testing-port", "testing-channel")
damiannolan marked this conversation as resolved.
Show resolved Hide resolved

expectedChannels := []*types.ActiveChannel{
{
PortId: TestPortID,
ChannelId: path.EndpointA.ChannelID,
},
{
PortId: "testing-port",
ChannelId: "testing-channel",
},
}

activeChannels := suite.chainA.GetSimApp().ICAKeeper.GetAllActiveChannels(suite.chainA.GetContext())
suite.Require().Len(activeChannels, len(expectedChannels))
suite.Require().Equal(expectedChannels, activeChannels)
}

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

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

suite.chainA.GetSimApp().ICAKeeper.SetInterchainAccountAddress(suite.chainA.GetContext(), "testing-port", "testing-acc-addr")

expectedAccounts := []*types.RegisteredInterchainAccount{
{
PortId: TestPortID,
AccountAddress: TestAccAddress.String(),
},
{
PortId: "testing-port",
AccountAddress: "testing-acc-addr",
},
}

interchainAccounts := suite.chainA.GetSimApp().ICAKeeper.GetAllInterchainAccounts(suite.chainA.GetContext())
suite.Require().Len(interchainAccounts, len(expectedAccounts))
suite.Require().Equal(expectedAccounts, interchainAccounts)
}

func (suite *KeeperTestSuite) TestIsActiveChannel() {
suite.SetupTest() // reset
path := NewICAPath(suite.chainA, suite.chainB)
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