Skip to content

Commit

Permalink
add tx channel-upgrade cancel command
Browse files Browse the repository at this point in the history
Signed-off-by: Masanori Yoshida <[email protected]>
  • Loading branch information
siburu committed Jul 8, 2024
1 parent 0066f09 commit af452bf
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
43 changes: 41 additions & 2 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func channelUpgradeCmd(ctx *config.Context) *cobra.Command {
cmd.AddCommand(
channelUpgradeInitCmd(ctx),
channelUpgradeExecuteCmd(ctx),
//channelUpgradeCancel(ctx),
channelUpgradeCancelCmd(ctx),
)

return cmd
Expand All @@ -226,7 +226,7 @@ func channelUpgradeInitCmd(ctx *config.Context) *cobra.Command {

cmd := cobra.Command{
Use: "init [path-name] [chain-id]",
Short: "execute chanOpenInit",
Short: "execute chanUpgradeInit",
Long: "This command is meant to be used to initialize an IBC channel upgrade on a configured chain",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -321,6 +321,45 @@ func channelUpgradeExecuteCmd(ctx *config.Context) *cobra.Command {
return &cmd
}

func channelUpgradeCancelCmd(ctx *config.Context) *cobra.Command {
return &cobra.Command{
Use: "cancel [path-name] [chain-id]",
Short: "execute chanUpgradeCancel",
Long: "This command is meant to be used to cancel an IBC channel upgrade on a configured chain",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
pathName := args[0]
chainID := args[1]

_, srcChainID, dstChainID, err := ctx.Config.ChainsFromPath(pathName)
if err != nil {
return err
}

var cpChainID string
switch {
case chainID == srcChainID:
cpChainID = dstChainID
case chainID == dstChainID:
cpChainID = srcChainID
default:
return fmt.Errorf("invalid chain ID: %s or %s was expected, but %s was given", srcChainID, dstChainID, chainID)
}

chain, err := ctx.Config.GetChain(chainID)
if err != nil {
return err
}
cp, err := ctx.Config.GetChain(cpChainID)
if err != nil {
return err
}

return core.CancelChannelUpgrade(chain, cp)
},
}
}

func relayMsgsCmd(ctx *config.Context) *cobra.Command {
const (
flagDoRefresh = "do-refresh"
Expand Down
65 changes: 64 additions & 1 deletion core/channel-upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func InitChannelUpgrade(chain *ProvableChain, upgradeFields chantypes.UpgradeFie
} else if len(msgIDs) != 1 {
panic(fmt.Sprintf("len(msgIDs) == %d", len(msgIDs)))
} else if result, err := GetFinalizedMsgResult(*chain, msgIDs[0]); err != nil {
logger.Error("failed to the finalized result of MsgChannelUpgradeInit", err)
logger.Error("failed to get the finalized result of MsgChannelUpgradeInit", err)
return err
} else if ok, desc := result.Status(); !ok {
err := fmt.Errorf("failed to initialize channel upgrade: %s", desc)
Expand Down Expand Up @@ -88,6 +88,69 @@ func ExecuteChannelUpgrade(src, dst *ProvableChain, interval time.Duration) erro
}
}

// CancelChannelUpgrade executes chanUpgradeCancel on `chain`.
func CancelChannelUpgrade(chain, cp *ProvableChain) error {
logger := GetChannelPairLogger(chain, cp)
defer logger.TimeTrack(time.Now(), "CancelChannelUpgrade")

addr, err := chain.GetAddress()
if err != nil {
logger.Error("failed to get address", err)
return err
}

var ctx, cpCtx QueryContext
if sh, err := NewSyncHeaders(chain, cp); err != nil {
logger.Error("failed to create a SyncHeaders", err)
return err
} else {
ctx = sh.GetQueryContext(chain.ChainID())
cpCtx = sh.GetQueryContext(cp.ChainID())
}

chann, err := chain.QueryChannel(ctx)
if err != nil {
logger.Error("failed to get the channel state", err)
return err
}

var errReceiptSequence uint64 = 0
if chann.Channel.State == chantypes.FLUSHCOMPLETE {
errReceiptSequence = chann.Channel.UpgradeSequence
}

var msg sdk.Msg
if upgErr, err := cp.QueryChannelUpgradeError(cpCtx, errReceiptSequence); err != nil {
logger.Error("failed to query the channel upgrade error receipt", err)
return err
} else if upgErr == nil {
// NOTE: Even if an error receipt is not found, anyway try to execute ChanUpgradeCancel.
// If the sender is authority and the channel state is anything other than FLUSHCOMPLETE,
// the cancellation will be successful.
msg = chain.Path().ChanUpgradeCancel(&chantypes.QueryUpgradeErrorResponse{}, addr)
} else {
msg = chain.Path().ChanUpgradeCancel(upgErr, addr)
}

msgIDs, err := chain.SendMsgs([]sdk.Msg{msg})
if err != nil {
logger.Error("failed to send MsgChannelUpgradeCancel", err)
return err
} else if len(msgIDs) != 1 {
panic(fmt.Sprintf("len(msgIDs) == %d", len(msgIDs)))
} else if result, err := GetFinalizedMsgResult(*chain, msgIDs[0]); err != nil {
logger.Error("failed to get the finalized result of MsgChannelUpgradeCancel", err)
return err
} else if ok, desc := result.Status(); !ok {
err := fmt.Errorf("failed to cancel the channel upgrade: %s", desc)
logger.Error(err.Error(), err)
} else {
logger.Info("successfully cancelled the channel upgrade")
}

return nil
}

func upgradeChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
logger := GetChannelPairLogger(src, dst)

Expand Down

0 comments on commit af452bf

Please sign in to comment.