Skip to content

Commit

Permalink
fix: change check to disallow optimistic sends (#3009)
Browse files Browse the repository at this point in the history
* change check to disallow optimistic sends

* update test
  • Loading branch information
charleenfei authored Jan 18, 2023
1 parent 8b03197 commit c56f789
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 39 deletions.
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
6 changes: 5 additions & 1 deletion docs/migrations/v6-to-v7.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ ModuleBasics = module.NewBasicManager(

It may be useful to reference the [PR](https://github.com/cosmos/ibc-go/pull/2826) which added the `AppModuleBasic` for the solo machine client.

### Testing package API

The `SetChannelClosed` utility method in `testing/endpoint.go` has been updated to `SetChannelState`, which will take a `channeltypes.State` argument so that the `ChannelState` can be set to any of the possible channel states.

## IBC Apps

- No relevant changes were made in this release.
Expand Down Expand Up @@ -286,4 +290,4 @@ import (

- host.RouterKey
+ ibcexported.RouterKey
```
```
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 {
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 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 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() {
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 {
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

0 comments on commit c56f789

Please sign in to comment.