Fix Optimistic Channel Handshake bugs (#7678)

* fix optimistic handshake bugs and add crossing hello test

* fix tests

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
colin axnér 2020-10-27 15:34:25 +01:00 committed by GitHub
parent 7ccd2675d7
commit 3d46c672f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 7 deletions

View File

@ -187,16 +187,33 @@ func (k Keeper) ChanOpenTry(
return nil, err
}
k.SetChannel(ctx, portID, desiredChannelID, channel)
var (
capKey *capabilitytypes.Capability
err error
)
capKey, err := k.scopedKeeper.NewCapability(ctx, host.ChannelCapabilityPath(portID, desiredChannelID))
if err != nil {
return nil, sdkerrors.Wrapf(err, "could not create channel capability for port ID %s and channel ID %s", portID, desiredChannelID)
// Only create a capability and set the sequences if the previous channel does not exist
_, found = k.GetChannel(ctx, portID, desiredChannelID)
if !found {
capKey, err = k.scopedKeeper.NewCapability(ctx, host.ChannelCapabilityPath(portID, desiredChannelID))
if err != nil {
return nil, sdkerrors.Wrapf(err, "could not create channel capability for port ID %s and channel ID %s", portID, desiredChannelID)
}
k.SetNextSequenceSend(ctx, portID, desiredChannelID, 1)
k.SetNextSequenceRecv(ctx, portID, desiredChannelID, 1)
k.SetNextSequenceAck(ctx, portID, desiredChannelID, 1)
} else {
// capability initialized in ChanOpenInit
capKey, found = k.scopedKeeper.GetCapability(ctx, host.ChannelCapabilityPath(portID, desiredChannelID))
if !found {
return nil, sdkerrors.Wrapf(types.ErrChannelCapabilityNotFound,
"capability not found for existing channel, portID (%s) channelID (%s)", portID, desiredChannelID,
)
}
}
k.SetNextSequenceSend(ctx, portID, desiredChannelID, 1)
k.SetNextSequenceRecv(ctx, portID, desiredChannelID, 1)
k.SetNextSequenceAck(ctx, portID, desiredChannelID, 1)
k.SetChannel(ctx, portID, desiredChannelID, channel)
k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", desiredChannelID, "previous-state", previousChannel.State.String(), "new-state", "TRYOPEN")

View File

@ -153,6 +153,12 @@ func (suite *KeeperTestSuite) TestChanOpenTry() {
suite.chainB.CreatePortCapability(connB.NextTestChannel(ibctesting.MockPort).PortID)
portCap = suite.chainB.GetPortCapability(connB.NextTestChannel(ibctesting.MockPort).PortID)
}, true},
{"success with crossing hello", func() {
_, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, ibctesting.Tendermint)
suite.coordinator.ChanOpenInitOnBothChains(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED)
portCap = suite.chainB.GetPortCapability(connB.NextTestChannel(ibctesting.MockPort).PortID)
}, true},
{"success with empty counterparty chosen channel id", func() {
var clientA, clientB string
clientA, clientB, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, ibctesting.Tendermint)

View File

@ -524,6 +524,54 @@ func (coord *Coordinator) ChanOpenInit(
return sourceChannel, counterpartyChannel, nil
}
// ChanOpenInitOnBothChains initializes a channel on the source chain and counterparty chain
// with the state INIT using the OpenInit handshake call.
func (coord *Coordinator) ChanOpenInitOnBothChains(
source, counterparty *TestChain,
connection, counterpartyConnection *TestConnection,
sourcePortID, counterpartyPortID string,
order channeltypes.Order,
) (TestChannel, TestChannel, error) {
sourceChannel := connection.AddTestChannel(sourcePortID)
counterpartyChannel := counterpartyConnection.AddTestChannel(counterpartyPortID)
// NOTE: only creation of a capability for a transfer or mock port is supported
// Other applications must bind to the port in InitGenesis or modify this code.
source.CreatePortCapability(sourceChannel.PortID)
counterparty.CreatePortCapability(counterpartyChannel.PortID)
coord.IncrementTime()
// initialize channel on source
if err := source.ChanOpenInit(sourceChannel, counterpartyChannel, order, connection.ID); err != nil {
return sourceChannel, counterpartyChannel, err
}
coord.IncrementTime()
// initialize channel on counterparty
if err := counterparty.ChanOpenInit(counterpartyChannel, sourceChannel, order, counterpartyConnection.ID); err != nil {
return sourceChannel, counterpartyChannel, err
}
coord.IncrementTime()
// update counterparty client on source connection
if err := coord.UpdateClient(
source, counterparty,
connection.ClientID, Tendermint,
); err != nil {
return sourceChannel, counterpartyChannel, err
}
// update source client on counterparty connection
if err := coord.UpdateClient(
counterparty, source,
counterpartyConnection.ClientID, Tendermint,
); err != nil {
return sourceChannel, counterpartyChannel, err
}
return sourceChannel, counterpartyChannel, nil
}
// ChanOpenTry initializes a channel on the source chain with the state TRYOPEN
// using the OpenTry handshake call.
func (coord *Coordinator) ChanOpenTry(