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

feat: delete forwarded packet when it is not needed anymore #6621

Merged
merged 40 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
013f883
Create ontimeoutpacket test for forwarding
bznein Jun 13, 2024
83f4582
Propagate ack on A
bznein Jun 13, 2024
59b93ed
Refactoring
bznein Jun 13, 2024
ff35ada
Minor changes
bznein Jun 13, 2024
926d8da
Added comments
bznein Jun 13, 2024
8a3dd36
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6384/ontimeo…
bznein Jun 13, 2024
3f5dc6c
Fix type name.
bznein Jun 14, 2024
10592e1
Gofumpt
bznein Jun 14, 2024
907de8e
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6384/ontimeo…
bznein Jun 14, 2024
6787c74
Update modules/apps/transfer/keeper/relay_forwarding_test.go
bznein Jun 14, 2024
37375ba
Update modules/apps/transfer/keeper/relay_forwarding_test.go
bznein Jun 14, 2024
65a0d0a
Update modules/apps/transfer/keeper/relay_forwarding_test.go
bznein Jun 14, 2024
ca59adf
Update modules/apps/transfer/keeper/relay_forwarding_test.go
bznein Jun 14, 2024
1492378
Add godoc to test.
bznein Jun 14, 2024
288f5f6
Changed trace construction
bznein Jun 14, 2024
d4e9b59
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6384/ontimeo…
bznein Jun 17, 2024
a70e56d
Update modules/apps/transfer/keeper/relay_forwarding_test.go
bznein Jun 17, 2024
6a33a57
remove error msg parameter from helper function
bznein Jun 14, 2024
7fb5569
Add test for forwarded packet
bznein Jun 17, 2024
774d971
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6384/ontimeo…
bznein Jun 17, 2024
d1736dc
Delete packet when not needed anymore.
bznein Jun 17, 2024
8c8feeb
Move deletion of packet in a single place.
bznein Jun 17, 2024
24f82d6
Reintroduce newline
bznein Jun 17, 2024
6528515
Do not ignore error.
bznein Jun 17, 2024
caa3902
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6546/deletep…
bznein Jun 17, 2024
bec616f
PR feedback.
bznein Jun 18, 2024
ad4e6f6
Construct packet for B ack check.
bznein Jun 18, 2024
3acd732
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6384/ontimeo…
bznein Jun 18, 2024
61884f0
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6546/deletep…
bznein Jun 18, 2024
139facf
PR feedback
bznein Jun 18, 2024
f9c1e70
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6546/deletep…
gjermundgaraba Jun 19, 2024
fdd769f
Pass packet to acknowledgeforwardedpacket
bznein Jun 20, 2024
d532007
revert unwanted change
bznein Jun 20, 2024
db52296
Another unwanted change
bznein Jun 20, 2024
8d06bc3
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6546/deletep…
bznein Jun 20, 2024
b6684da
Better signature and fix source/dest
bznein Jun 20, 2024
a5c8729
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6546/deletep…
crodriguezvega Jun 21, 2024
432fd1f
Added one more test.
bznein Jun 21, 2024
265de48
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6546/deletep…
bznein Jun 21, 2024
fc5410e
Merge branch 'feat/ics20-v2-path-forwarding' into bznein/6546/deletep…
bznein Jun 21, 2024
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
21 changes: 13 additions & 8 deletions modules/apps/transfer/keeper/forwarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func (k Keeper) forwardPacket(ctx sdk.Context, data types.FungibleTokenPacketDat
}

// ackForwardPacketSuccess writes a successful async acknowledgement for the prevPacket
func (k Keeper) ackForwardPacketSuccess(ctx sdk.Context, prevPacket channeltypes.Packet) error {
func (k Keeper) ackForwardPacketSuccess(ctx sdk.Context, prevPacket, forwardedPacket channeltypes.Packet) error {
forwardAck := channeltypes.NewResultAcknowledgement([]byte{byte(1)})
return k.acknowledgeForwardedPacket(ctx, prevPacket, forwardAck)
return k.acknowledgeForwardedPacket(ctx, prevPacket, forwardedPacket, forwardAck)
}

// ackForwardPacketError reverts the receive packet logic that occurs in the middle chain and writes the async ack for the prevPacket
func (k Keeper) ackForwardPacketError(ctx sdk.Context, prevPacket channeltypes.Packet, failedPacketData types.FungibleTokenPacketDataV2) error {
func (k Keeper) ackForwardPacketError(ctx sdk.Context, prevPacket, forwardedPacket channeltypes.Packet, failedPacketData types.FungibleTokenPacketDataV2) error {
// the forwarded packet has failed, thus the funds have been refunded to the intermediate address.
// we must revert the changes that came from successfully receiving the tokens on our chain
// before propagating the error acknowledgement back to original sender chain
Expand All @@ -59,27 +59,32 @@ func (k Keeper) ackForwardPacketError(ctx sdk.Context, prevPacket channeltypes.P
}

forwardAck := channeltypes.NewErrorAcknowledgement(types.ErrForwardedPacketFailed)
return k.acknowledgeForwardedPacket(ctx, prevPacket, forwardAck)
return k.acknowledgeForwardedPacket(ctx, prevPacket, forwardedPacket, forwardAck)
}

// ackForwardPacketTimeout reverts the receive packet logic that occurs in the middle chain and writes a failed async ack for the prevPacket
func (k Keeper) ackForwardPacketTimeout(ctx sdk.Context, prevPacket channeltypes.Packet, timeoutPacketData types.FungibleTokenPacketDataV2) error {
func (k Keeper) ackForwardPacketTimeout(ctx sdk.Context, prevPacket, forwardedPacket channeltypes.Packet, timeoutPacketData types.FungibleTokenPacketDataV2) error {
if err := k.revertForwardedPacket(ctx, prevPacket, timeoutPacketData); err != nil {
return err
}

forwardAck := channeltypes.NewErrorAcknowledgement(types.ErrForwardedPacketTimedOut)
return k.acknowledgeForwardedPacket(ctx, prevPacket, forwardAck)
return k.acknowledgeForwardedPacket(ctx, prevPacket, forwardedPacket, forwardAck)
}

// acknowledgeForwardedPacket writes the async acknowledgement for packet
func (k Keeper) acknowledgeForwardedPacket(ctx sdk.Context, packet channeltypes.Packet, ack channeltypes.Acknowledgement) error {
func (k Keeper) acknowledgeForwardedPacket(ctx sdk.Context, packet, forwardedPacket channeltypes.Packet, ack channeltypes.Acknowledgement) error {
capability, ok := k.scopedKeeper.GetCapability(ctx, host.ChannelCapabilityPath(packet.DestinationPort, packet.DestinationChannel))
if !ok {
return errorsmod.Wrap(channeltypes.ErrChannelCapabilityNotFound, "module does not own channel capability")
}

return k.ics4Wrapper.WriteAcknowledgement(ctx, capability, packet, ack)
if err := k.ics4Wrapper.WriteAcknowledgement(ctx, capability, packet, ack); err != nil {
return err
}

k.deleteForwardedPacket(ctx, forwardedPacket.SourcePort, forwardedPacket.SourceChannel, forwardedPacket.Sequence)
return nil
}

// revertForwardedPacket reverts the logic of receive packet that occurs in the middle chains during a packet forwarding.
Expand Down
8 changes: 8 additions & 0 deletions modules/apps/transfer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,11 @@ func (k Keeper) GetForwardedPacket(ctx sdk.Context, portID, channelID string, se

return storedPacket, true
}

// deleteForwardedPacket deletes the forwarded packet from the store.
func (k Keeper) deleteForwardedPacket(ctx sdk.Context, portID, channelID string, sequence uint64) {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is some improvement we can do with the naming since "forwarded packet" here in the name of the function refers to the packet that was received by the middle chain, but in the places where this function is called "forwarded packet" refers to the packet that was sent from the middle chain. Anyway, no need to change it for now, we can make a proposal for other naming in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I agree. I chose a name which is consistent with what we use in the rest of the file (which in my understanding refer to the same concept), but I'm happy to find a better name.

store := ctx.KVStore(k.storeKey)
packetKey := types.PacketForwardKey(portID, channelID, sequence)

store.Delete(packetKey)
}
6 changes: 3 additions & 3 deletions modules/apps/transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Pac
switch ack.Response.(type) {
case *channeltypes.Acknowledgement_Result:
if isForwarded {
return k.ackForwardPacketSuccess(ctx, prevPacket)
return k.ackForwardPacketSuccess(ctx, prevPacket, packet)
}

// the acknowledgement succeeded on the receiving chain so nothing
Expand All @@ -297,7 +297,7 @@ func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Pac
return err
}
if isForwarded {
return k.ackForwardPacketError(ctx, prevPacket, data)
return k.ackForwardPacketError(ctx, prevPacket, packet, data)
}

return nil
Expand All @@ -316,7 +316,7 @@ func (k Keeper) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, dat

prevPacket, isForwarded := k.GetForwardedPacket(ctx, packet.SourcePort, packet.SourceChannel, packet.Sequence)
if isForwarded {
return k.ackForwardPacketTimeout(ctx, prevPacket, data)
return k.ackForwardPacketTimeout(ctx, prevPacket, packet, data)
}

return nil
Expand Down
8 changes: 8 additions & 0 deletions modules/apps/transfer/keeper/relay_forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ func (suite *KeeperTestSuite) TestSimplifiedHappyPathForwarding() {
err = path2.EndpointB.UpdateClient()
suite.Require().NoError(err)

// B should now have deleted the forwarded packet.
_, found := suite.chainB.GetSimApp().TransferKeeper.GetForwardedPacket(suite.chainB.GetContext(), packetFromAtoB.DestinationPort, packetFromAtoB.DestinationChannel, packetFromAtoB.Sequence)
suite.Require().False(found, "Chain B should have deleted its forwarded packet")

result, err = path2.EndpointB.RecvPacketWithResult(packetFromBtoC)
suite.Require().NoError(err)
suite.Require().NotNil(result)
Expand Down Expand Up @@ -562,6 +566,10 @@ func (suite *KeeperTestSuite) TestAcknowledgementFailureScenario5Forwarding() {
err = path1.EndpointB.AcknowledgePacket(packetFromBtoA, errorAckOnA.Acknowledgement())
suite.Require().NoError(err)

// Check that B deleted the forwarded packet.
Copy link
Contributor

Choose a reason for hiding this comment

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

We can probably add this check to some other tests as well (like the happy path).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, done!

_, found = suite.chainB.GetSimApp().TransferKeeper.GetForwardedPacket(suite.chainB.GetContext(), forwardedPacket.SourcePort, forwardedPacket.SourceChannel, forwardedPacket.Sequence)
suite.Require().False(found, "chain B should have deleted the forwarded packet mapping")

// Check that Escrow B has been refunded amount
coin = sdk.NewCoin(denomAB.IBCDenom(), amount)
totalEscrowChainB = suite.chainB.GetSimApp().TransferKeeper.GetTotalEscrowForDenom(suite.chainB.GetContext(), coin.GetDenom())
Expand Down
Loading