Skip to content

Commit

Permalink
Merge branch 'main' into charly/issue#5522-remove-the-channel-type-=-…
Browse files Browse the repository at this point in the history
…ordered-checks-from-both-host-and-controller
  • Loading branch information
charleenfei committed Jan 11, 2024
2 parents ee6bc18 + 8624ecd commit bf47cbb
Show file tree
Hide file tree
Showing 19 changed files with 1,110 additions and 372 deletions.
232 changes: 228 additions & 4 deletions docs/docs/01-ibc/06-channel-upgrades.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,26 @@ Each handshake step will be documented below in greater detail.

## Initializing a Channel Upgrade

A channel upgrade is initialised by submitting the `ChanUpgradeInit` message, which can be submitted only by the chain itself upon governance authorization. This message should specify an appropriate timeout window for the upgrade. It is possible to upgrade the channel ordering, the channel connection hops, and the channel version.
A channel upgrade is initialised by submitting the `MsgChannelUpgradeInit` message, which can be submitted only by the chain itself upon governance authorization. It is possible to upgrade the channel ordering, the channel connection hops, and the channel version, which can be found in the `UpgradeFields`.

As part of the handling of the `ChanUpgradeInit` message, the application's callbacks `OnChanUpgradeInit` will be triggered as well.
```go
type MsgChannelUpgradeInit struct {
PortId string
ChannelId string
Fields UpgradeFields
Signer string
}
```

As part of the handling of the `MsgChannelUpgradeInit` message, the application's `OnChanUpgradeInit` callback will be triggered as well.

After this message is handled successfully, the channel's upgrade sequence will be incremented. This upgrade sequence will serve as a nonce for the upgrade process to provide replay protection.

### Governance gating on `ChanUpgradeInit`

The message signer for `MsgChanUpgradeInit` must be the address which has been designated as the `authority` of the `IBCKeeper`. If this proposal passes, the counterparty's channel will upgrade by default.
The message signer for `MsgChannelUpgradeInit` must be the address which has been designated as the `authority` of the `IBCKeeper`. If this proposal passes, the counterparty's channel will upgrade by default.

If chains want to initiate the upgrade of many channels, they will need to submit a governance proposal with multiple `MsgChanUpgradeInit` messages, one for each channel they would like to upgrade, again with message signer as the designated `authority` of the `IBCKeeper`
If chains want to initiate the upgrade of many channels, they will need to submit a governance proposal with multiple `MsgChannelUpgradeInit` messages, one for each channel they would like to upgrade, again with message signer as the designated `authority` of the `IBCKeeper`. The `upgrade-channels` CLI command can be used to submit a proposal that initiates the upgrade of multiple channels; see section [Upgrading channels with the CLI](#upgrading-channels-with-the-cli) below for more information.

## Channel State and Packet Flushing

Expand Down Expand Up @@ -147,6 +156,89 @@ The application's `OnChanUpgradeRestore` callback method will be invoked.

It will then be possible to re-initiate an upgrade by sending a `MsgChannelOpenInit` message.

## Timing Out a Channel Upgrade

Timing out an outstanding channel upgrade may be necessary during the flushing packet stage of the channel upgrade process. As stated above, with `ChanUpgradeTry` or `ChanUpgradeAck`, the channel state has been changed from `OPEN` to `FLUSHING`, so no new packets will be allowed to be sent over this channel while flushing. If upgrades cannot be performed in a timely manner (due to unforeseen flushing issues), upgrade timeouts allow the channel to avoid blocking packet sends indefinitely. If flushing exceeds the time limit set in the `UpgradeTimeout` channel `Params`, the upgrade process will need to be timed out to abort the upgrade attempt and resume normal channel processing.

Channel upgrades require setting a valid timeout value in the channel `Params` before submitting a `MsgChannelUpgradeTry` or `MsgChannelUpgradeAck` message (by default, 10 minutes):

```go
type Params struct {
UpgradeTimeout Timeout
}
```

A valid Timeout contains either one or both of a timestamp and block height (sequence).

```go
type Timeout struct {
// block height after which the packet or upgrade times out
Height types.Height
// block timestamp (in nanoseconds) after which the packet or upgrade times out
Timestamp uint64
}
```

This timeout will then be set as a field on the `Upgrade` struct itself when flushing is started.

```go
type Upgrade struct {
Fields UpgradeFields
Timeout Timeout
NextSequenceSend uint64
}
```

If the timeout has been exceeded during flushing, a chain can then submit the `MsgChannelUpgradeTimeout` to timeout the channel upgrade process:

```go
type MsgChannelUpgradeTimeout struct {
PortId string
ChannelId string
CounterpartyChannel Channel
ProofChannel []byte
ProofHeight types.Height
Signer string
}
```

An `ErrorReceipt` will be written with the channel's current upgrade sequence, and the channel will move back to `OPEN` state keeping its original parameters.

The application's `OnChanUpgradeRestore` callback method will also be invoked.

Note that timing out a channel upgrade will end the upgrade process, and a new `MsgChannelUpgradeInit` will have to be submitted via governance in order to restart the upgrade process.

## Pruning Acknowledgements

Acknowledgements can be pruned by broadcasting the `MsgPruneAcknowledgements` message.

> Note: It is only possible to prune acknowledgements after a channel has been upgraded, so pruning will fail
> if the channel has not yet been upgraded.
```protobuf
// MsgPruneAcknowledgements defines the request type for the PruneAcknowledgements rpc.
message MsgPruneAcknowledgements {
option (cosmos.msg.v1.signer) = "signer";
option (gogoproto.goproto_getters) = false;
string port_id = 1;
string channel_id = 2;
uint64 limit = 3;
string signer = 4;
}
```

The `port_id` and `channel_id` specify the port and channel to act on, and the `limit` specifies the upper bound for the number
of acknowledgements and packet receipts to prune.

### CLI Usage

Acknowledgements can be pruned via the cli with the `prune-acknowledgements` command.

```bash
simd tx ibc channel prune-acknowledgements [port] [channel] [limit]
```

## IBC App Recommendations

IBC application callbacks should be primarily used to validate data fields and do compatibility checks.
Expand All @@ -164,3 +256,135 @@ IBC application callbacks should be primarily used to validate data fields and d
> IBC applications should not attempt to process any packet data under the new conditions until after `OnChanUpgradeOpen`
> has been executed, as up until this point it is still possible for the upgrade handshake to fail and for the channel
> to remain in the pre-upgraded state.
## Upgrade an existing transfer application stack to use 29-fee middleware

### Wire up the transfer stack and middleware in app.go

In app.go, the existing transfer stack must be wrapped with the fee middleware.

```golang

import (
// ...
ibcfee "github.com/cosmos/ibc-go/v8/modules/apps/29-fee"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
transfer "github.com/cosmos/ibc-go/v8/modules/apps/transfer"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
// ...
)

type App struct {
// ...
TransferKeeper ibctransferkeeper.Keeper
IBCFeeKeeper ibcfeekeeper.Keeper
// ..
}

// ...

app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
appCodec, keys[ibcfeetypes.StoreKey],
app.IBCKeeper.ChannelKeeper, // may be replaced with IBC middleware
app.IBCKeeper.ChannelKeeper,
app.IBCKeeper.PortKeeper, app.AccountKeeper, app.BankKeeper,
)

// Create Transfer Keeper and pass IBCFeeKeeper as expected Channel and PortKeeper
// since fee middleware will wrap the IBCKeeper for underlying application.
app.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec, keys[ibctransfertypes.StoreKey], app.GetSubspace(ibctransfertypes.ModuleName),
app.IBCFeeKeeper, // ISC4 Wrapper: fee IBC middleware
app.IBCKeeper.ChannelKeeper, app.IBCKeeper.PortKeeper,
app.AccountKeeper, app.BankKeeper, scopedTransferKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)


ibcRouter := porttypes.NewRouter()

// create IBC module from bottom to top of stack
var transferStack porttypes.IBCModule
transferStack = transfer.NewIBCModule(app.TransferKeeper)
transferStack = ibcfee.NewIBCMiddleware(transferStack, app.IBCFeeKeeper)

// Add transfer stack to IBC Router
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack)
```

### Submit a governance proposal to execute a MsgChannelUpgradeInit message

> This process can be performed with the new CLI that has been added
> outlined [here](#upgrading-channels-with-the-cli).
Only the configured authority for the ibc module is able to initiate a channel upgrade by submitting a `MsgChannelUpgradeInit` message.

Execute a governance proposal specifying the relevant fields to perform a channel upgrade.

Update the following json sample, and copy the contents into `proposal.json`.

```json
{
"title": "Channel upgrade init",
"summary": "Channel upgrade init",
"messages": [
{
"@type": "/ibc.core.channel.v1.MsgChannelUpgradeInit",
"signer": "<gov-address>",
"port_id": "transfer",
"channel_id": "channel-...",
"fields": {
"ordering": "ORDER_UNORDERED",
"connection_hops": ["connection-0"],
"version": "{\"fee_version\":\"ics29-1\",\"app_version\":\"ics20-1\"}"
}
}
],
"metadata": "<metadata>",
"deposit": "10stake"
}
```

> Note: ensure the correct fields.version is specified. This is the new version that the channels will be upgraded to.
### Submit the proposal

```shell
simd tx submit-proposal proposal.json --from <key_or_address>
```

## Upgrading channels with the CLI

A new cli has been added which enables either
- submitting a governance proposal which contains a `MsgChannelUpgradeInit` for every channel to be upgraded.
- generating a `proposal.json` file which contains the proposal contents to be edited/submitted at a later date.

The following example, would submit a governance proposal with the specified deposit, title and summary which would
contain a `MsgChannelUpgradeInit` for all `OPEN` channels whose port matches the regular expression `transfer`.

> Note: by adding the `--json` flag, the command would instead output the contents of the proposal which could be
> stored in a `proposal.json` file to be edited and submitted at a later date.
```bash
simd tx ibc channel upgrade-channels "{\"fee_version\":\"ics29-1\",\"app_version\":\"ics20-1\"}" \
--deposit "10stake" \
--title "Channel Upgrades Governance Proposal" \
--summary "Upgrade all transfer channels to be fee enabled" \
--port-pattern "transfer"
```

It is also possible to explicitly list a comma separated string of channel IDs. It is important to note that the
regular expression matching specified by `--port-pattern` (which defaults to `transfer`) still applies.

For example the following command would generate the contents of a `proposal.json` file which would attempt to upgrade
channels with a port ID of `transfer` and a channelID of `channel-0`, `channel-1` or `channel-2`.

```bash
simd tx ibc channel upgrade-channels "{\"fee_version\":\"ics29-1\",\"app_version\":\"ics20-1\"}" \
--deposit "10stake" \
--title "Channel Upgrades Governance Proposal" \
--summary "Upgrade all transfer channels to be fee enabled" \
--port-pattern "transfer" \
--channel-ids "channel-0,channel-1,channel-2" \
--json
```
2 changes: 1 addition & 1 deletion e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require (
github.com/ChainSafe/go-schnorrkel v1.1.0 // indirect
github.com/ChainSafe/go-schnorrkel/1 v0.0.0-00010101000000-000000000000 // indirect
github.com/ComposableFi/go-subkey/v2 v2.0.0-tm03420 // indirect
github.com/CosmWasm/wasmvm v1.5.0 // indirect
github.com/CosmWasm/wasmvm v1.5.1 // indirect
github.com/DataDog/datadog-go v4.8.3+incompatible // indirect
github.com/DataDog/zstd v1.5.5 // indirect
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e // indirect
Expand Down
4 changes: 2 additions & 2 deletions e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ github.com/ChainSafe/go-schnorrkel v1.0.0 h1:3aDA67lAykLaG1y3AOjs88dMxC88PgUuHRr
github.com/ChainSafe/go-schnorrkel v1.0.0/go.mod h1:dpzHYVxLZcp8pjlV+O+UR8K0Hp/z7vcchBSbMBEhCw4=
github.com/ComposableFi/go-subkey/v2 v2.0.0-tm03420 h1:oknQF/iIhf5lVjbwjsVDzDByupRhga8nhA3NAmwyHDA=
github.com/ComposableFi/go-subkey/v2 v2.0.0-tm03420/go.mod h1:KYkiMX5AbOlXXYfxkrYPrRPV6EbVUALTQh5ptUOJzu8=
github.com/CosmWasm/wasmvm v1.5.0 h1:3hKeT9SfwfLhxTGKH3vXaKFzBz1yuvP8SlfwfQXbQfw=
github.com/CosmWasm/wasmvm v1.5.0/go.mod h1:fXB+m2gyh4v9839zlIXdMZGeLAxqUdYdFQqYsTha2hc=
github.com/CosmWasm/wasmvm v1.5.1 h1:2MHN9uFyHP6pxfvpBJ0JW6ujvAIBk9kQk283zyri0Ro=
github.com/CosmWasm/wasmvm v1.5.1/go.mod h1:fXB+m2gyh4v9839zlIXdMZGeLAxqUdYdFQqYsTha2hc=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/datadog-go v4.8.3+incompatible h1:fNGaYSuObuQb5nzeTQqowRAd9bpDIRRV4/gUtIBjh8Q=
github.com/DataDog/datadog-go v4.8.3+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
Expand Down
2 changes: 1 addition & 1 deletion modules/apps/27-interchain-accounts/host/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (im IBCModule) OnRecvPacket(
ack = channeltypes.NewErrorAcknowledgement(err)
logger.Error(fmt.Sprintf("%s sequence %d", err.Error(), packet.Sequence))
} else {
logger.Info("successfully handled packet sequence: %d", packet.Sequence)
logger.Info("successfully handled packet", "sequence", packet.Sequence)
}

// Emit an event indicating a successful or failed acknowledgement.
Expand Down
48 changes: 40 additions & 8 deletions modules/apps/27-interchain-accounts/host/keeper/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,45 @@ func (Keeper) OnChanCloseConfirm(
}

// OnChanUpgradeTry performs the upgrade try step of the channel upgrade handshake.
// The upgrade try callback must verify the proposed changes to the order, connectionHops, and version.
// Within the version we have the tx type, encoding, interchain account address, host/controller connectionID's
// and the ICS27 protocol version.
//
// The following may be changed:
// - tx type (must be supported)
// - encoding (must be supported)
//
// The following may not be changed:
// - order
// - connectionHops (and subsequently host/controller connectionIDs)
// - interchain account address
// - ICS27 protocol version
func (k Keeper) OnChanUpgradeTry(ctx sdk.Context, portID, channelID string, order channeltypes.Order, connectionHops []string, counterpartyVersion string) (string, error) {
// verify order has not changed
// support for unordered ICA channels is not implemented yet
if order != channeltypes.ORDERED {
return "", errorsmod.Wrapf(channeltypes.ErrInvalidChannelOrdering, "expected %s channel, got %s", channeltypes.ORDERED, order)
}

if portID != icatypes.HostPortID {
return "", errorsmod.Wrapf(porttypes.ErrInvalidPort, "expected %s, got %s", icatypes.HostPortID, portID)
}

if strings.TrimSpace(counterpartyVersion) == "" {
return "", errorsmod.Wrap(channeltypes.ErrInvalidChannelVersion, "counterparty version cannot be empty")
// verify connection hops has not changed
connectionID, err := k.getConnectionID(ctx, portID, channelID)
if err != nil {
return "", err
}

// support for unordered ICA channels is not implemented yet
if order != channeltypes.ORDERED {
return "", errorsmod.Wrapf(channeltypes.ErrInvalidChannelOrdering, "expected %s channel, got %s", channeltypes.ORDERED, order)
if len(connectionHops) != 1 || connectionHops[0] != connectionID {
return "", errorsmod.Wrapf(channeltypes.ErrInvalidUpgrade, "expected connection hops %s, got %s", []string{connectionID}, connectionHops)
}

metadata, err := icatypes.MetadataFromVersion(counterpartyVersion)
if strings.TrimSpace(counterpartyVersion) == "" {
return "", errorsmod.Wrap(channeltypes.ErrInvalidChannelVersion, "counterparty version cannot be empty")
}

proposedCounterpartyMetadata, err := icatypes.MetadataFromVersion(counterpartyVersion)
if err != nil {
return "", err
}
Expand All @@ -152,16 +176,24 @@ func (k Keeper) OnChanUpgradeTry(ctx sdk.Context, portID, channelID string, orde
return "", err
}

if err := icatypes.ValidateHostMetadata(ctx, k.channelKeeper, connectionHops, metadata); err != nil {
// ValidateHostMetadata will ensure the ICS27 protocol version has not changed and that the
// tx type and encoding are supported. It also validates the connection params against the counterparty metadata.
if err := icatypes.ValidateHostMetadata(ctx, k.channelKeeper, connectionHops, proposedCounterpartyMetadata); err != nil {
return "", errorsmod.Wrap(err, "invalid metadata")
}

// the interchain account address on the host chain
// must remain the same after the upgrade.
if currentMetadata.Address != metadata.Address {
if currentMetadata.Address != proposedCounterpartyMetadata.Address {
return "", errorsmod.Wrap(icatypes.ErrInvalidAccountAddress, "interchain account address cannot be changed")
}

// these explicit checks on the controller connection identifier should be unreachable
if currentMetadata.ControllerConnectionId != proposedCounterpartyMetadata.ControllerConnectionId {
return "", errorsmod.Wrap(connectiontypes.ErrInvalidConnection, "proposed controller connection ID must not change")
}

// these explicit checks on the host connection identifier should be unreachable
if currentMetadata.HostConnectionId != connectionHops[0] {
return "", errorsmod.Wrap(connectiontypes.ErrInvalidConnectionIdentifier, "proposed connection hop must not change")
}
Expand Down
Loading

0 comments on commit bf47cbb

Please sign in to comment.