Skip to content

Commit

Permalink
imp: add UpdateChannel method to Endpoint (cosmos#5725)
Browse files Browse the repository at this point in the history
  • Loading branch information
charleenfei authored Feb 8, 2024
1 parent 2c9ad16 commit 71fa5c9
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 324 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1210,10 +1210,8 @@ func (suite *InterchainAccountsTestSuite) TestClosedChannelReopensWithMsgServer(
suite.Require().NoError(err)

// set the channel state to closed
err = path.EndpointA.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })
path.EndpointB.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })

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

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

err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })
path.EndpointB.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })

path.EndpointA.ChannelID = ""
path.EndpointB.ChannelID = ""
Expand Down Expand Up @@ -579,9 +576,7 @@ func (suite *KeeperTestSuite) TestOnChanUpgradeInit() {
{
name: "failure: cannot decode self version string",
malleate: func() {
ch := path.EndpointA.GetChannel()
ch.Version = invalidVersion
path.EndpointA.SetChannel(ch)
path.EndpointA.UpdateChannel(func(channel *channeltypes.Channel) { channel.Version = invalidVersion })
},
expError: icatypes.ErrUnknownDataType,
},
Expand Down Expand Up @@ -739,9 +734,7 @@ func (suite *KeeperTestSuite) TestOnChanUpgradeAck() {
{
name: "failure: cannot decode self version string",
malleate: func() {
channel := path.EndpointA.GetChannel()
channel.Version = invalidVersion
path.EndpointA.SetChannel(channel)
path.EndpointA.UpdateChannel(func(channel *channeltypes.Channel) { channel.Version = invalidVersion })
},
expError: icatypes.ErrUnknownDataType,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,14 @@ func (suite *KeeperTestSuite) TestSendTx() {
{
"channel in INIT state - optimistic packet sends fail",
func() {
channel, found := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
suite.Require().True(found)

channel.State = channeltypes.INIT
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel)
path.EndpointA.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.INIT })
},
false,
},
{
"sendPacket fails - channel closed",
func() {
err := path.EndpointA.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })
},
false,
},
Expand Down
6 changes: 2 additions & 4 deletions modules/apps/27-interchain-accounts/host/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,10 +842,8 @@ func (suite *InterchainAccountsTestSuite) TestControlAccountAfterChannelClose()
suite.assertBalance(icaAddr, expBalAfterFirstSend)

// close the channel
err = path.EndpointA.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })
path.EndpointB.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })

// open a new channel on the same port
path.EndpointA.ChannelID = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ func (suite *KeeperTestSuite) openAndCloseChannel(path *ibctesting.Path) {
err = path.EndpointB.ChanOpenConfirm()
suite.Require().NoError(err)

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

err = path.EndpointB.SetChannelState(channeltypes.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })
path.EndpointB.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })

path.EndpointA.ChannelID = ""
err = RegisterInterchainAccount(path.EndpointA, TestOwnerAddress)
Expand Down Expand Up @@ -503,9 +500,7 @@ func (suite *KeeperTestSuite) TestOnChanUpgradeTry() {
{
name: "failure: cannot decode version string from channel",
malleate: func() {
channel := path.EndpointB.GetChannel()
channel.Version = "invalid-metadata-string"
path.EndpointB.SetChannel(channel)
path.EndpointB.UpdateChannel(func(channel *channeltypes.Channel) { channel.Version = "invalid-metadata-string" })
},
expError: icatypes.ErrUnknownDataType,
},
Expand Down
4 changes: 1 addition & 3 deletions modules/core/03-connection/keeper/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,7 @@ func (suite *KeeperTestSuite) TestVerifyChannelState() {
heightDiff = 5
}, false},
{"verification failed - changed channel state", func() {
channel := path.EndpointA.GetChannel()
channel.State = channeltypes.TRYOPEN
path.EndpointA.SetChannel(channel)
path.EndpointA.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.TRYOPEN })
}, false},
{"client status is not active - client is expired", func() {
clientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
Expand Down
18 changes: 6 additions & 12 deletions modules/core/04-channel/keeper/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,7 @@ func (suite *KeeperTestSuite) TestChanCloseInit() {
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)

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

err := path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.CLOSED })
}, true},
{"success with upgrade info", func() {
path.Setup()
Expand Down Expand Up @@ -748,8 +746,7 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() {
path.Setup()
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

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

err := path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.CLOSED })

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

err := path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.CLOSED })

channelCap = capabilitytypes.NewCapability(3)
}, false},
Expand All @@ -809,8 +804,7 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() {
err := path.EndpointB.ChanUpgradeInit()
suite.Require().NoError(err)

err = path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.CLOSED })

channelCap = capabilitytypes.NewCapability(3)
},
Expand Down
71 changes: 19 additions & 52 deletions modules/core/04-channel/keeper/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,31 +98,26 @@ func (suite *KeeperTestSuite) TestSendPacket() {
path.Setup()
sourceChannel = path.EndpointA.ChannelID

err := path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.CLOSED })
}, false},
{"channel is in INIT state", func() {
path.Setup()
sourceChannel = path.EndpointA.ChannelID

err := path.EndpointA.SetChannelState(types.INIT)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.INIT })
}, false},
{"channel is in TRYOPEN stage", func() {
path.Setup()
sourceChannel = path.EndpointA.ChannelID

err := path.EndpointA.SetChannelState(types.TRYOPEN)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.TRYOPEN })
}, false},
{"connection not found", func() {
// pass channel check
path.Setup()
sourceChannel = path.EndpointA.ChannelID

channel := path.EndpointA.GetChannel()
channel.ConnectionHops[0] = "invalid-connection"
path.EndpointA.SetChannel(channel)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.ConnectionHops[0] = "invalid-connection" })

channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
}, false},
Expand Down Expand Up @@ -221,9 +216,7 @@ func (suite *KeeperTestSuite) TestSendPacket() {
path.Setup()
sourceChannel = path.EndpointA.ChannelID

channel := path.EndpointA.GetChannel()
channel.State = types.FLUSHCOMPLETE
path.EndpointA.SetChannel(channel)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHCOMPLETE })
},
false,
},
Expand Down Expand Up @@ -324,9 +317,7 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
func() {
// setup uses an UNORDERED channel
path.Setup()
channel := path.EndpointB.GetChannel()
channel.State = types.FLUSHING
path.EndpointB.SetChannel(channel)
path.EndpointB.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHING })

sequence, err := path.EndpointA.SendPacket(defaultTimeoutHeight, disabledTimeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
Expand All @@ -340,9 +331,7 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
func() {
// setup uses an UNORDERED channel
path.Setup()
channel := path.EndpointB.GetChannel()
channel.State = types.FLUSHCOMPLETE
path.EndpointB.SetChannel(channel)
path.EndpointB.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHCOMPLETE })

sequence, err := path.EndpointA.SendPacket(defaultTimeoutHeight, disabledTimeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
Expand Down Expand Up @@ -376,9 +365,7 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

channel := path.EndpointB.GetChannel()
channel.State = types.FLUSHING
path.EndpointB.SetChannel(channel)
path.EndpointB.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHING })

// set upgrade next sequence send to sequence + 1
counterpartyUpgrade := types.Upgrade{NextSequenceSend: sequence + 1}
Expand All @@ -395,9 +382,7 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

channel := path.EndpointB.GetChannel()
channel.State = types.FLUSHING
path.EndpointB.SetChannel(channel)
path.EndpointB.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHING })
},
nil,
},
Expand All @@ -413,9 +398,7 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

channel := path.EndpointB.GetChannel()
channel.State = types.FLUSHING
path.EndpointB.SetChannel(channel)
path.EndpointB.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHING })

// set upgrade next sequence send to sequence - 1
counterpartyUpgrade := types.Upgrade{NextSequenceSend: sequence - 1}
Expand Down Expand Up @@ -488,8 +471,7 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
path.Setup()
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.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
path.EndpointB.UpdateChannel(func(channel *types.Channel) { channel.State = types.CLOSED })
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
},
types.ErrInvalidChannelState,
Expand Down Expand Up @@ -722,8 +704,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.SetChannelState(types.FLUSHING)
suite.Require().NoError(err)
path.EndpointB.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHING })
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
},
true,
Expand All @@ -735,8 +716,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.SetChannelState(types.FLUSHCOMPLETE)
suite.Require().NoError(err)
path.EndpointB.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHCOMPLETE })
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
},
true,
Expand All @@ -753,8 +733,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.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
path.EndpointB.UpdateChannel(func(channel *types.Channel) { channel.State = types.CLOSED })
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
}, false},
{
Expand Down Expand Up @@ -915,9 +894,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)

channel := path.EndpointA.GetChannel()
channel.State = types.FLUSHING
path.EndpointA.SetChannel(channel)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHING })
},
expResult: func(commitment []byte, err error) {
suite.Require().NoError(err)
Expand Down Expand Up @@ -948,10 +925,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)

channel := path.EndpointA.GetChannel()
channel.State = types.FLUSHING

path.EndpointA.SetChannel(channel)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHING })

counterpartyUpgrade := types.Upgrade{
Timeout: types.NewTimeout(suite.chainB.GetTimeoutHeight(), 0),
Expand Down Expand Up @@ -1002,10 +976,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)

channel := path.EndpointA.GetChannel()
channel.State = types.FLUSHING

path.EndpointA.SetChannel(channel)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHING })

upgrade := types.Upgrade{
Fields: types.NewUpgradeFields(types.UNORDERED, []string{ibctesting.FirstConnectionID}, ibcmock.UpgradeVersion),
Expand Down Expand Up @@ -1105,8 +1076,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)

err = path.EndpointA.SetChannelState(types.CLOSED)
suite.Require().NoError(err)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.CLOSED })
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
},
expResult: assertErr(types.ErrInvalidChannelState),
Expand All @@ -1118,10 +1088,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)

channel := path.EndpointA.GetChannel()
channel.State = types.FLUSHCOMPLETE

path.EndpointA.SetChannel(channel)
path.EndpointA.UpdateChannel(func(channel *types.Channel) { channel.State = types.FLUSHCOMPLETE })
},
expResult: func(commitment []byte, err error) {
suite.Require().Error(err)
Expand Down
Loading

0 comments on commit 71fa5c9

Please sign in to comment.