Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change check to disallow optimistic sends #3009

Merged
merged 9 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ linters:
- unconvert
- unused
- misspell
- nolintlint

issues:
exclude-rules:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -887,9 +887,9 @@ func (suite *InterchainAccountsTestSuite) TestClosedChannelReopensWithMsgServer(
suite.Require().NoError(err)

// set the channel state to closed
err = path.EndpointA.SetChannelClosed()
err = path.EndpointA.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)

// reset endpoint channel ids
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func (suite *KeeperTestSuite) TestOnChanOpenInit() {
err := SetupICAPath(path, TestOwnerAddress)
suite.Require().NoError(err)

err = path.EndpointA.SetChannelClosed()
err = path.EndpointA.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)

err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)

path.EndpointA.ChannelID = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (suite *KeeperTestSuite) TestSendTx() {
{
"sendPacket fails - channel closed",
func() {
err := path.EndpointA.SetChannelClosed()
err := path.EndpointA.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
},
false,
Expand Down Expand Up @@ -151,6 +151,7 @@ func (suite *KeeperTestSuite) TestSendTx() {

tc.malleate() // malleate mutates test data

//nolint: staticcheck // SA1019: ibctesting.FirstConnectionID is deprecated: use path.EndpointA.ConnectionID instead. (staticcheck)
_, err = suite.chainA.GetSimApp().ICAControllerKeeper.SendTx(suite.chainA.GetContext(), nil, ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID, packetData, timeoutTimestamp)

if tc.expPass {
Expand Down
6 changes: 4 additions & 2 deletions modules/apps/27-interchain-accounts/host/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ func (suite *InterchainAccountsTestSuite) TestControlAccountAfterChannelClose()
params := types.NewParams(true, []string{sdk.MsgTypeURL(msg)})
suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params)

//nolint: staticcheck // SA1019: ibctesting.FirstConnectionID is deprecated: use path.EndpointA.ConnectionID instead. (staticcheck)
_, err = suite.chainA.GetSimApp().ICAControllerKeeper.SendTx(suite.chainA.GetContext(), nil, ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID, icaPacketData, ^uint64(0))
suite.Require().NoError(err)
err = path.EndpointB.UpdateClient()
Expand All @@ -684,16 +685,17 @@ func (suite *InterchainAccountsTestSuite) TestControlAccountAfterChannelClose()
suite.assertBalance(icaAddr, expBalAfterFirstSend)

// close the channel
err = path.EndpointA.SetChannelClosed()
err = path.EndpointA.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)

// open a new channel on the same port
path.EndpointA.ChannelID = ""
path.EndpointB.ChannelID = ""
suite.coordinator.CreateChannels(path)

//nolint: staticcheck // SA1019: ibctesting.FirstConnectionID is deprecated: use path.EndpointA.ConnectionID instead. (staticcheck)
_, err = suite.chainA.GetSimApp().ICAControllerKeeper.SendTx(suite.chainA.GetContext(), nil, ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID, icaPacketData, ^uint64(0))
suite.Require().NoError(err)
err = path.EndpointB.UpdateClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func (suite *KeeperTestSuite) openAndCloseChannel(path *ibctesting.Path) {
err = path.EndpointB.ChanOpenConfirm()
suite.Require().NoError(err)

err = path.EndpointA.SetChannelClosed()
err = path.EndpointA.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)

err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)

path.EndpointA.ChannelID = ""
Expand Down
10 changes: 5 additions & 5 deletions modules/core/04-channel/keeper/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ func (suite *KeeperTestSuite) TestChanCloseInit() {
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)

// close channel
err := path.EndpointA.SetChannelClosed()
err := path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
}, false},
{"connection not found", func() {
Expand Down Expand Up @@ -693,7 +693,7 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() {
suite.coordinator.Setup(path)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

err := path.EndpointA.SetChannelClosed()
err := path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
}, true},
{"channel doesn't exist", func() {
Expand All @@ -709,7 +709,7 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() {
suite.coordinator.Setup(path)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

err := path.EndpointB.SetChannelClosed()
err := path.EndpointB.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
}, false},
{"connection not found", func() {
Expand Down Expand Up @@ -740,7 +740,7 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() {
suite.coordinator.Setup(path)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

err := path.EndpointA.SetChannelClosed()
err := path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)

heightDiff = 3
Expand All @@ -754,7 +754,7 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() {
suite.coordinator.Setup(path)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

err := path.EndpointA.SetChannelClosed()
err := path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)

channelCap = capabilitytypes.NewCapability(3)
Expand Down
4 changes: 2 additions & 2 deletions modules/core/04-channel/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ func (k Keeper) SendPacket(
return 0, sdkerrors.Wrap(types.ErrChannelNotFound, sourceChannel)
}

if channel.State == types.CLOSED {
if channel.State != types.OPEN {
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
return 0, sdkerrors.Wrapf(
types.ErrInvalidChannelState,
"channel is CLOSED (got %s)", channel.State.String(),
"channel is not OPEN (got %s)", channel.State.String(),
)
}

Expand Down
24 changes: 19 additions & 5 deletions modules/core/04-channel/keeper/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,25 @@ func (suite *KeeperTestSuite) TestSendPacket() {
sourceChannel = ibctesting.InvalidID
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
}, false},
{"channel closed", func() {
{"channel is in CLOSED stage", func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{"channel is in CLOSED stage", func() {
{"channel is in CLOSED state", func() {

suite.coordinator.Setup(path)
sourceChannel = path.EndpointA.ChannelID

err := path.EndpointA.SetChannelClosed()
err := path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
}, false},
{"channel is in INIT stage", func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{"channel is in INIT stage", func() {
{"channel is in INIT state", func() {

suite.coordinator.Setup(path)
sourceChannel = path.EndpointA.ChannelID

err := path.EndpointA.SetChannelState(types.INIT)
suite.Require().NoError(err)
}, false},
{"channel is in TRYOPEN stage", func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{"channel is in TRYOPEN stage", func() {
{"channel is in TRYOPEN state", func() {

suite.coordinator.Setup(path)
sourceChannel = path.EndpointA.ChannelID

err := path.EndpointA.SetChannelState(types.TRYOPEN)
suite.Require().NoError(err)
}, false},
{"connection not found", func() {
Expand Down Expand Up @@ -337,7 +351,7 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
suite.coordinator.Setup(path)
packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)

err := path.EndpointB.SetChannelClosed()
err := path.EndpointB.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
}, false},
Expand Down Expand Up @@ -535,7 +549,7 @@ func (suite *KeeperTestSuite) TestWriteAcknowledgement() {
packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
ack = ibcmock.MockAcknowledgement

err := path.EndpointB.SetChannelClosed()
err := path.EndpointB.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
}, false},
Expand Down Expand Up @@ -695,7 +709,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
suite.coordinator.Setup(path)
packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)

err := path.EndpointA.SetChannelClosed()
err := path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
}, false},
Expand Down
14 changes: 7 additions & 7 deletions modules/core/04-channel/keeper/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() {
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)

err = path.EndpointA.SetChannelClosed()
err = path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
}, false},
{"packet destination port ≠ channel counterparty port", func() {
Expand Down Expand Up @@ -332,7 +332,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() {

sequence, err := path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
// need to update chainA's client representing chainB to prove missing ack
err = path.EndpointA.UpdateClient()
Expand All @@ -349,7 +349,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() {

sequence, err := path.EndpointA.SendPacket(timeoutHeight, disabledTimeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
// need to update chainA's client representing chainB to prove missing ack
err = path.EndpointA.UpdateClient()
Expand Down Expand Up @@ -406,7 +406,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() {

sequence, err := path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
// need to update chainA's client representing chainB to prove missing ack
err = path.EndpointA.UpdateClient()
Expand Down Expand Up @@ -439,7 +439,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() {

sequence, err := path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
Expand All @@ -456,7 +456,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() {

sequence, err := path.EndpointA.SendPacket(timeoutHeight, disabledTimeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
Expand All @@ -474,7 +474,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() {

sequence, err := path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
// need to update chainA's client representing chainB to prove missing ack
err = path.EndpointA.UpdateClient()
Expand Down
2 changes: 1 addition & 1 deletion modules/core/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (suite *AnteTestSuite) createTimeoutOnCloseMessage(isRedundant bool) sdk.Ms

sequence, err := suite.path.EndpointB.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
err = suite.path.EndpointA.SetChannelClosed()
err = suite.path.EndpointA.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)

packet := channeltypes.NewPacket(ibctesting.MockPacketData, sequence,
Expand Down
10 changes: 5 additions & 5 deletions modules/core/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() {
packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel())

// close counterparty channel
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
}, true},
{"success: UNORDERED", func() {
Expand All @@ -538,7 +538,7 @@ func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() {
packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())

// close counterparty channel
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
}, true},
{"success: UNORDERED timeout out of order packet", func() {
Expand All @@ -561,7 +561,7 @@ func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() {
packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())

// close counterparty channel
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
}, true},
{"success: ORDERED timeout out of order packet", func() {
Expand All @@ -584,7 +584,7 @@ func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() {
packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel())

// close counterparty channel
err = path.EndpointB.SetChannelClosed()
err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
}, true},
{"channel does not exist", func() {
Expand All @@ -599,7 +599,7 @@ func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() {
packetKey = host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())

// close counterparty channel
err := path.EndpointB.SetChannelClosed()
err := path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
}, true},
{"ORDERED: channel not closed", func() {
Expand Down
6 changes: 3 additions & 3 deletions testing/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,11 +566,11 @@ func (endpoint *Endpoint) TimeoutOnClose(packet channeltypes.Packet) error {
return endpoint.Chain.sendMsgs(timeoutOnCloseMsg)
}

// SetChannelClosed sets a channel state to CLOSED.
func (endpoint *Endpoint) SetChannelClosed() error {
// SetChannelState sets a channel state
func (endpoint *Endpoint) SetChannelState(state channeltypes.State) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! We could avoid the api breaking change here in the testing package by just adding a new function SetChannelState(state) and keep SetChannelClosed as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this refactoring. Should we anyway break the API and document this in the migration docs for v7?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @crodriguezvega that breaking the API and updating migration docs in this case is cleaner...do you have a strong opinion about not breaking it @damiannolan ?

channel := endpoint.GetChannel()

channel.State = channeltypes.CLOSED
channel.State = state
endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.SetChannel(endpoint.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID, channel)

endpoint.Chain.Coordinator.CommitBlock(endpoint.Chain)
Expand Down