Skip to content

Commit

Permalink
fix Chain::QueryChannelUpgradeError to return the latest error receip…
Browse files Browse the repository at this point in the history
…t if 0 is given as upgrade sequence parameter

Signed-off-by: Masanori Yoshida <[email protected]>
  • Loading branch information
siburu committed Jul 5, 2024
1 parent db6cd9c commit d531f88
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
26 changes: 16 additions & 10 deletions chains/tendermint/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,19 +403,25 @@ func (c *Chain) QueryChannelUpgradeError(ctx core.QueryContext, upgradeSequence
return c.queryChannelUpgradeError(int64(ctx.Height().GetRevisionHeight()), upgradeSequence, false)
}

func (c *Chain) queryChannelUpgradeError(maxHeight int64, upgradeSequence uint64, prove bool) (chanRes *chantypes.QueryUpgradeErrorResponse, err error) {
txs, err := c.QueryTxs(maxHeight, 1, 2, channelUpgradeErrorQuery(c.Path().ChannelID, upgradeSequence))
switch {
case err != nil:
return nil, err
case len(txs) == 0:
return nil, fmt.Errorf("no transactions returned with query")
case len(txs) > 1:
return nil, fmt.Errorf("more than one transaction returned with query")
func (c *Chain) queryChannelUpgradeError(height int64, upgradeSequence uint64, prove bool) (chanRes *chantypes.QueryUpgradeErrorResponse, err error) {
var errReceiptHeight int64
if upgradeSequence == 0 {
errReceiptHeight = height
} else {
txs, err := c.QueryTxs(height, 1, 2, channelUpgradeErrorQuery(c.Path().ChannelID, upgradeSequence))
switch {
case err != nil:
return nil, err
case len(txs) == 0:
return nil, fmt.Errorf("no transactions returned with query")
case len(txs) > 1:
return nil, fmt.Errorf("more than one transaction returned with query")
}
errReceiptHeight = txs[0].Height
}

if res, err := chanutils.QueryUpgradeError(
c.CLIContext(txs[0].Height),
c.CLIContext(errReceiptHeight),
c.PathEnd.PortID,
c.PathEnd.ChannelID,
prove,
Expand Down
3 changes: 2 additions & 1 deletion core/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ type ICS04Querier interface {
// QueryChannelUpgrade returns the channel upgrade associated with a channelID
QueryChannelUpgrade(ctx QueryContext) (*chantypes.QueryUpgradeResponse, error)

// QueryChannelUpgradeError iterates through chain events in reverse chronological order and returns the error receipt that matches `upgradeSequence`.
// QueryChannelUpgradeError iterates through chain events in reverse chronological order from the height of `ctx` and returns the error receipt that matches `upgradeSequence`.
// If zero is specified as `upgradeSequence`, this function simply returns the error receipt stored at the height of `ctx`.
QueryChannelUpgradeError(ctx QueryContext, upgradeSequence uint64) (*chantypes.QueryUpgradeErrorResponse, error)

// QueryCanTransitionToFlushComplete returns the channel can transition to FLUSHCOMPLETE state.
Expand Down
20 changes: 10 additions & 10 deletions core/channel-upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func upgradeChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
return nil, errors.New("channel upgrade is not initialized")
case srcState == UPGRADEINIT && dstState == UPGRADEUNINIT:
if dstChan.Channel.UpgradeSequence >= srcChan.Channel.UpgradeSequence {
if out.Src, err = doCancel(src, dstCtxFinalized, dst, dstUpdateHeaders, dstChan.Channel.UpgradeSequence); err != nil {
if out.Src, err = doCancel(src, dstCtxFinalized, dst, dstUpdateHeaders, 0); err != nil {
return nil, err
}
out.Last = true
Expand All @@ -266,7 +266,7 @@ func upgradeChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
}
case srcState == UPGRADEUNINIT && dstState == UPGRADEINIT:
if srcChan.Channel.UpgradeSequence >= dstChan.Channel.UpgradeSequence {
if out.Dst, err = doCancel(dst, srcCtxFinalized, src, srcUpdateHeaders, srcChan.Channel.UpgradeSequence); err != nil {
if out.Dst, err = doCancel(dst, srcCtxFinalized, src, srcUpdateHeaders, 0); err != nil {
return nil, err
}
out.Last = true
Expand All @@ -276,12 +276,12 @@ func upgradeChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
}
}
case srcState == UPGRADEUNINIT && dstState == FLUSHING:
if out.Dst, err = doCancel(dst, srcCtxFinalized, src, srcUpdateHeaders, srcChan.Channel.UpgradeSequence); err != nil {
if out.Dst, err = doCancel(dst, srcCtxFinalized, src, srcUpdateHeaders, 0); err != nil {
return nil, err
}
out.Last = true
case srcState == FLUSHING && dstState == UPGRADEUNINIT:
if out.Src, err = doCancel(src, dstCtxFinalized, dst, dstUpdateHeaders, dstChan.Channel.UpgradeSequence); err != nil {
if out.Src, err = doCancel(src, dstCtxFinalized, dst, dstUpdateHeaders, 0); err != nil {
return nil, err
}
out.Last = true
Expand All @@ -295,7 +295,7 @@ func upgradeChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
} else if timedout {
out.Dst = doTimeout(dst, srcUpdateHeaders, srcChan)
} else {
if out.Dst, err = doCancel(dst, srcCtxFinalized, src, srcUpdateHeaders, srcChan.Channel.UpgradeSequence); err != nil {
if out.Dst, err = doCancel(dst, srcCtxFinalized, src, srcUpdateHeaders, dstChan.Channel.UpgradeSequence); err != nil {
return nil, err
}
}
Expand All @@ -310,7 +310,7 @@ func upgradeChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
} else if timedout {
out.Src = doTimeout(src, dstUpdateHeaders, dstChan)
} else {
if out.Src, err = doCancel(src, dstCtxFinalized, dst, dstUpdateHeaders, dstChan.Channel.UpgradeSequence); err != nil {
if out.Src, err = doCancel(src, dstCtxFinalized, dst, dstUpdateHeaders, srcChan.Channel.UpgradeSequence); err != nil {
return nil, err
}
}
Expand All @@ -330,7 +330,7 @@ func upgradeChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
}
case srcState == UPGRADEINIT && dstState == FLUSHING:
if srcChan.Channel.UpgradeSequence != dstChan.Channel.UpgradeSequence {
if out.Dst, err = doCancel(dst, srcCtxFinalized, src, srcUpdateHeaders, srcChan.Channel.UpgradeSequence-1); err != nil {
if out.Dst, err = doCancel(dst, srcCtxFinalized, src, srcUpdateHeaders, 0); err != nil {
return nil, err
}
} else {
Expand All @@ -341,7 +341,7 @@ func upgradeChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
}
case srcState == FLUSHING && dstState == UPGRADEINIT:
if srcChan.Channel.UpgradeSequence != dstChan.Channel.UpgradeSequence {
if out.Src, err = doCancel(src, dstCtxFinalized, dst, dstUpdateHeaders, dstChan.Channel.UpgradeSequence-1); err != nil {
if out.Src, err = doCancel(src, dstCtxFinalized, dst, dstUpdateHeaders, 0); err != nil {
return nil, err
}
} else {
Expand All @@ -360,7 +360,7 @@ func upgradeChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
} else if timedout {
out.Dst = doTimeout(dst, srcUpdateHeaders, srcChan)
} else {
if out.Dst, err = doCancel(dst, srcCtxFinalized, src, srcUpdateHeaders, srcChan.Channel.UpgradeSequence-1); err != nil {
if out.Dst, err = doCancel(dst, srcCtxFinalized, src, srcUpdateHeaders, dstChan.Channel.UpgradeSequence); err != nil {
return nil, err
}
}
Expand All @@ -374,7 +374,7 @@ func upgradeChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
} else if timedout {
out.Src = doTimeout(src, dstUpdateHeaders, dstChan)
} else {
if out.Src, err = doCancel(src, dstCtxFinalized, dst, dstUpdateHeaders, dstChan.Channel.UpgradeSequence-1); err != nil {
if out.Src, err = doCancel(src, dstCtxFinalized, dst, dstUpdateHeaders, srcChan.Channel.UpgradeSequence); err != nil {
return nil, err
}
}
Expand Down

0 comments on commit d531f88

Please sign in to comment.