Skip to content

Commit

Permalink
Merge branch '04-channel-upgrades' into charly/#3448-modify-upgrade-try
Browse files Browse the repository at this point in the history
  • Loading branch information
charleenfei committed May 30, 2023
2 parents 134c459 + 6c5e414 commit 9b42618
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 29 deletions.
19 changes: 19 additions & 0 deletions modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"encoding/binary"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -514,6 +515,24 @@ func (k Keeper) GetUpgrade(ctx sdk.Context, portID, channelID string) (types.Upg
return upgrade, true
}

// GetCounterpartyLastPacketSequence gets the counterparty last packet sequence send from the store.
func (k Keeper) GetCounterpartyLastPacketSequence(ctx sdk.Context, portID, channelID string) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.ChannelCounterpartyLastPacketSequenceKey(portID, channelID))
if bz == nil {
return 0, false
}

return binary.BigEndian.Uint64(bz), true
}

// SetCounterpartyLastPacketSequence sets the counterparty last packet sequence in the store.
func (k Keeper) SetCounterpartyLastPacketSequence(ctx sdk.Context, portID, channelID string, sequence uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(sequence)
store.Set(host.ChannelCounterpartyLastPacketSequenceKey(portID, channelID), bz)
}

// SetUpgrade sets the proposed upgrade using the provided port and channel identifiers.
func (k Keeper) SetUpgrade(ctx sdk.Context, portID, channelID string, upgrade types.Upgrade) {
store := ctx.KVStore(k.storeKey)
Expand Down
7 changes: 6 additions & 1 deletion modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ func (k Keeper) ChanUpgradeInit(

// WriteUpgradeInitChannel writes a channel which has successfully passed the UpgradeInit handshake step.
// An event is emitted for the handshake step.
func (k Keeper) WriteUpgradeInitChannel(ctx sdk.Context, portID, channelID string, currentChannel types.Channel, upgrade types.Upgrade) {
func (k Keeper) WriteUpgradeInitChannel(ctx sdk.Context, portID, channelID string, upgrade types.Upgrade) {
defer telemetry.IncrCounter(1, "ibc", "channel", "upgrade-init")

currentChannel, found := k.GetChannel(ctx, portID, channelID)
if !found {
panic(fmt.Sprintf("could not find existing channel when updating channel state in successful ChanUpgradeInit step, channelID: %s, portID: %s", channelID, portID))
}

currentChannel.State = types.INITUPGRADE

k.SetChannel(ctx, portID, channelID, currentChannel)
Expand Down
2 changes: 1 addition & 1 deletion modules/core/04-channel/keeper/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (suite *KeeperTestSuite) TestChanUpgradeInit() {
)

if tc.expPass {
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.WriteUpgradeInitChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointA.GetChannel(), proposedUpgrade)
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.WriteUpgradeInitChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, proposedUpgrade)
channel := path.EndpointA.GetChannel()

suite.Require().NoError(err)
Expand Down
18 changes: 0 additions & 18 deletions modules/core/04-channel/types/channel.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"fmt"

errorsmod "cosmossdk.io/errors"

host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
Expand All @@ -14,12 +12,6 @@ var (
_ exported.CounterpartyChannelI = (*Counterparty)(nil)
)

const (
// restoreErrorString defines a string constant included in error receipts.
// NOTE: Changing this const is state machine breaking as it is written into state.
restoreErrorString = "restored channel to pre-upgrade state"
)

// NewChannel creates a new Channel instance
func NewChannel(
state State, ordering Order, counterparty Counterparty,
Expand Down Expand Up @@ -136,13 +128,3 @@ func (ic IdentifiedChannel) ValidateBasic() error {
channel := NewChannel(ic.State, ic.Ordering, ic.Counterparty, ic.ConnectionHops, ic.Version)
return channel.ValidateBasic()
}

// NewErrorReceipt returns an error receipt with the code from the provided error type stripped
// out to ensure changes of the error message don't cause state machine breaking changes.
func NewErrorReceipt(upgradeSequence uint64, err error) ErrorReceipt {
_, code, _ := errorsmod.ABCIInfo(err, false) // discard non-determinstic codespace and log values
return ErrorReceipt{
Sequence: upgradeSequence,
Error: fmt.Sprintf("ABCI code: %d: %s", code, restoreErrorString),
}
}
17 changes: 17 additions & 0 deletions modules/core/04-channel/types/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"fmt"
"strings"
"time"

Expand All @@ -12,6 +13,12 @@ import (
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
)

const (
// restoreErrorString defines a string constant included in error receipts.
// NOTE: Changing this const is state machine breaking as it is written into state.
restoreErrorString = "restored channel to pre-upgrade state"
)

// NewUpgrade creates a new Upgrade instance.
func NewUpgrade(upgradeFields UpgradeFields, timeout Timeout, latestPacketSent uint64) Upgrade {
return Upgrade{
Expand Down Expand Up @@ -92,3 +99,13 @@ func (ut Timeout) HasPassed(ctx sdk.Context) (bool, error) {

return false, nil
}

// NewErrorReceipt returns an error receipt with the code from the provided error type stripped
// out to ensure changes of the error message don't cause state machine breaking changes.
func NewErrorReceipt(upgradeSequence uint64, err error) ErrorReceipt {
_, code, _ := errorsmod.ABCIInfo(err, false) // discard non-determinstic codespace and log values
return ErrorReceipt{
Sequence: upgradeSequence,
Error: fmt.Sprintf("ABCI code: %d: %s", code, restoreErrorString),
}
}
28 changes: 20 additions & 8 deletions modules/core/24-host/channel_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package host
import "fmt"

const (
KeyChannelEndPrefix = "channelEnds"
KeyChannelPrefix = "channels"
KeyChannelUpgradePrefix = "channelUpgrades"
KeyChannelRestorePrefix = "restore"
KeyUpgradeTimeoutPrefix = "upgradeTimeout"
KeyUpgradeSequencePrefix = "upgradeSequence"
KeyUpgradeErrorPrefix = "upgradeError"
KeyChannelEndPrefix = "channelEnds"
KeyChannelPrefix = "channels"
KeyChannelUpgradePrefix = "channelUpgrades"
KeyChannelRestorePrefix = "restore"
KeyUpgradePrefix = "upgrades"
KeyUpgradeTimeoutPrefix = "upgradeTimeout"
KeyUpgradeSequencePrefix = "upgradeSequence"
KeyUpgradeErrorPrefix = "upgradeError"
KeyCounterpartyLastPacketSequence = "counterpartyLastPacketSequence"
)

// ICS04
Expand Down Expand Up @@ -53,14 +55,24 @@ func ChannelRestoreKey(portID, channelID string) []byte {

// ChannelUpgradePath defines the path which stores the information related to an upgrade attempt
func ChannelUpgradePath(portID, channelID string) string {
return fmt.Sprintf("%s/%s", KeyChannelUpgradePrefix, channelPath(portID, channelID))
return fmt.Sprintf("%s/%s/%s", KeyChannelUpgradePrefix, KeyUpgradePrefix, channelPath(portID, channelID))
}

// ChannelUpgradeKey returns the store key for a particular channel upgrade attempt
func ChannelUpgradeKey(portID, channelID string) []byte {
return []byte(ChannelUpgradePath(portID, channelID))
}

// ChannelCounterpartyLastPacketSequenceKey returns the store key for the last packet sequence sent on the counterparty channel.
func ChannelCounterpartyLastPacketSequenceKey(portID, channelID string) []byte {
return []byte(ChannelCounterpartyLastPacketSequencePath(portID, channelID))
}

// ChannelCounterpartyLastPacketSequencePath defines the path under which the last packet sequence sent on the counterparty channel is stored.
func ChannelCounterpartyLastPacketSequencePath(portID, channelID string) string {
return fmt.Sprintf("%s/%s/%s", KeyChannelUpgradePrefix, KeyCounterpartyLastPacketSequence, channelPath(portID, channelID))
}

func channelPath(portID, channelID string) string {
return fmt.Sprintf("%s/%s/%s/%s", KeyPortPrefix, portID, KeyChannelPrefix, channelID)
}
2 changes: 1 addition & 1 deletion modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ func (k Keeper) ChannelUpgradeInit(goCtx context.Context, msg *channeltypes.MsgC
}

proposedUpgrade.Fields.Version = proposedVersion
k.ChannelKeeper.WriteUpgradeInitChannel(ctx, msg.PortId, msg.ChannelId, channel, proposedUpgrade)
k.ChannelKeeper.WriteUpgradeInitChannel(ctx, msg.PortId, msg.ChannelId, proposedUpgrade)

ctx.Logger().Info("channel upgrade init callback succeeded", "channel-id", msg.ChannelId, "version", proposedVersion)

Expand Down

0 comments on commit 9b42618

Please sign in to comment.