Skip to content

Commit

Permalink
refactor: reduce channel lookup for timeout flow (#7149)
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner authored Aug 13, 2024
1 parent 2a07e6e commit 0743c09
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 26 deletions.
4 changes: 2 additions & 2 deletions modules/core/04-channel/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func (k *Keeper) SetRecvStartSequence(ctx sdk.Context, portID, channelID string,
}

// TimeoutExecuted is a wrapper around timeoutExecuted to allow the function to be directly called in tests.
func (k *Keeper) TimeoutExecuted(ctx sdk.Context, capability *capabilitytypes.Capability, packet types.Packet) error {
return k.timeoutExecuted(ctx, capability, packet)
func (k *Keeper) TimeoutExecuted(ctx sdk.Context, channel types.Channel, capability *capabilitytypes.Capability, packet types.Packet) error {
return k.timeoutExecuted(ctx, channel, capability, packet)
}
10 changes: 3 additions & 7 deletions modules/core/04-channel/keeper/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (k *Keeper) TimeoutPacket(
return "", err
}

if err = k.timeoutExecuted(ctx, chanCap, packet); err != nil {
if err = k.timeoutExecuted(ctx, channel, chanCap, packet); err != nil {
return "", err
}

Expand All @@ -136,14 +136,10 @@ func (k *Keeper) TimeoutPacket(
// CONTRACT: this function must be called in the IBC handler
func (k *Keeper) timeoutExecuted(
ctx sdk.Context,
channel types.Channel,
chanCap *capabilitytypes.Capability,
packet types.Packet,
) error {
channel, found := k.GetChannel(ctx, packet.GetSourcePort(), packet.GetSourceChannel())
if !found {
return errorsmod.Wrapf(types.ErrChannelNotFound, "port ID (%s) channel ID (%s)", packet.GetSourcePort(), packet.GetSourceChannel())
}

capName := host.ChannelCapabilityPath(packet.GetSourcePort(), packet.GetSourceChannel())
if !k.scopedKeeper.AuthenticateCapability(ctx, chanCap, capName) {
return errorsmod.Wrapf(
Expand Down Expand Up @@ -302,7 +298,7 @@ func (k *Keeper) TimeoutOnClose(
return "", err
}

if err = k.timeoutExecuted(ctx, chanCap, packet); err != nil {
if err = k.timeoutExecuted(ctx, channel, chanCap, packet); err != nil {
return "", err
}

Expand Down
18 changes: 1 addition & 17 deletions modules/core/04-channel/keeper/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,22 +283,6 @@ func (suite *KeeperTestSuite) TestTimeoutExecuted() {
},
nil,
},
{
"channel not found",
func() {
// use wrong channel naming
path.Setup()
packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
},
func(packetCommitment []byte, err error) {
suite.Require().Error(err)
suite.Require().ErrorIs(err, types.ErrChannelNotFound)

// packet never sent.
suite.Require().Nil(packetCommitment)
},
nil,
},
{
"incorrect capability ORDERED",
func() {
Expand Down Expand Up @@ -528,7 +512,7 @@ func (suite *KeeperTestSuite) TestTimeoutExecuted() {

tc.malleate()

err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutExecuted(ctx, chanCap, packet)
err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutExecuted(ctx, path.EndpointA.GetChannel(), chanCap, packet)
pc := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())

tc.expResult(pc, err)
Expand Down

0 comments on commit 0743c09

Please sign in to comment.