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

refactor: update IncentivzedAck + add channeltype helpers for Ack #749

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ It contains the raw acknowledgement bytes, as well as the forward relayer addres

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `result` | [bytes](#bytes) | | |
| `app_acknowledgement` | [google.protobuf.Any](#google.protobuf.Any) | | |
| `forward_relayer_address` | [string](#string) | | |


Expand Down
17 changes: 14 additions & 3 deletions modules/apps/29-fee/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ func (im IBCModule) OnRecvPacket(
im.keeper.SetForwardRelayerAddress(ctx, channeltypes.NewPacketId(packet.GetSourceChannel(), packet.GetSourcePort(), packet.GetSequence()), forwardRelayer)
}

return types.NewIncentivizedAcknowledgement(forwardRelayer, ack.Acknowledgement())
// pack ack into an Any type
packedAck, err := channeltypes.PackAcknowledgement(ack)
if err != nil {
return nil
}

return types.NewIncentivizedAcknowledgement(forwardRelayer, packedAck)
}

// OnAcknowledgementPacket implements the IBCModule interface
Expand All @@ -225,16 +231,21 @@ func (im IBCModule) OnAcknowledgementPacket(

packetId := channeltypes.NewPacketId(packet.SourceChannel, packet.SourcePort, packet.Sequence)

appAck, err := channeltypes.UnpackAcknowledgement(ack.AppAcknowledgement)
if err != nil {
return err
}

identifiedPacketFee, found := im.keeper.GetFeeInEscrow(ctx, packetId)
if !found {
// return underlying callback if no fee found for given packetID
return im.app.OnAcknowledgementPacket(ctx, packet, ack.Result, relayer)
return im.app.OnAcknowledgementPacket(ctx, packet, appAck.Acknowledgement(), relayer)
}

im.keeper.DistributePacketFees(ctx, identifiedPacketFee.RefundAddress, ack.ForwardRelayerAddress, relayer, identifiedPacketFee)

// call underlying callback
return im.app.OnAcknowledgementPacket(ctx, packet, ack.Result, relayer)
return im.app.OnAcknowledgementPacket(ctx, packet, appAck.Acknowledgement(), relayer)
}

// OnTimeoutPacket implements the IBCModule interface
Expand Down
23 changes: 16 additions & 7 deletions modules/apps/29-fee/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,19 @@ func (suite *FeeTestSuite) TestOnRecvPacket() {
suite.Require().Equal(ack, result)

case tc.forwardRelayer:
packedAck, err := channeltypes.PackAcknowledgement(channeltypes.NewResultAcknowledgement([]byte{1}))
suite.Require().NoError(err)
ack := types.IncentivizedAcknowledgement{
Result: channeltypes.NewResultAcknowledgement([]byte{1}).Acknowledgement(),
AppAcknowledgement: packedAck,
ForwardRelayerAddress: suite.chainB.SenderAccount.GetAddress().String(),
}
suite.Require().Equal(ack, result)

case !tc.forwardRelayer:
packedAck, err := channeltypes.PackAcknowledgement(channeltypes.NewResultAcknowledgement([]byte{1}))
suite.Require().NoError(err)
ack := types.IncentivizedAcknowledgement{
Result: channeltypes.NewResultAcknowledgement([]byte{1}).Acknowledgement(),
AppAcknowledgement: packedAck,
ForwardRelayerAddress: "",
}
suite.Require().Equal(ack, result)
Expand Down Expand Up @@ -527,9 +531,10 @@ func (suite *FeeTestSuite) TestOnAcknowledgementPacket() {
func() {
packetId := channeltypes.NewPacketId(suite.path.EndpointA.ChannelID, suite.path.EndpointA.ChannelConfig.PortID, suite.chainA.SenderAccount.GetSequence())
suite.chainA.GetSimApp().IBCFeeKeeper.DeleteFeeInEscrow(suite.chainA.GetContext(), packetId)

packedAck, err := channeltypes.PackAcknowledgement(channeltypes.NewResultAcknowledgement([]byte{1}))
suite.Require().NoError(err)
ack = types.IncentivizedAcknowledgement{
Result: channeltypes.NewResultAcknowledgement([]byte{1}).Acknowledgement(),
AppAcknowledgement: packedAck,
ForwardRelayerAddress: suite.chainA.SenderAccount.GetAddress().String(),
}.Acknowledgement()

Expand Down Expand Up @@ -560,9 +565,10 @@ func (suite *FeeTestSuite) TestOnAcknowledgementPacket() {
"fail on distribute receive fee (blocked address)",
func() {
blockedAddr := suite.chainA.GetSimApp().AccountKeeper.GetModuleAccount(suite.chainA.GetContext(), transfertypes.ModuleName).GetAddress()

packedAck, err := channeltypes.PackAcknowledgement(channeltypes.NewResultAcknowledgement([]byte{1}))
suite.Require().NoError(err)
ack = types.IncentivizedAcknowledgement{
Result: channeltypes.NewResultAcknowledgement([]byte{1}).Acknowledgement(),
AppAcknowledgement: packedAck,
ForwardRelayerAddress: blockedAddr.String(),
}.Acknowledgement()

Expand Down Expand Up @@ -608,10 +614,13 @@ func (suite *FeeTestSuite) TestOnAcknowledgementPacket() {
suite.Require().NoError(err)

relayerAddr := suite.chainB.SenderAccount.GetAddress()
ack1 := channeltypes.NewResultAcknowledgement([]byte{1})
packedAck, err := channeltypes.PackAcknowledgement(&ack1)
suite.Require().NoError(err)

// must be changed explicitly
ack = types.IncentivizedAcknowledgement{
Result: channeltypes.NewResultAcknowledgement([]byte{1}).Acknowledgement(),
AppAcknowledgement: packedAck,
ForwardRelayerAddress: relayerAddr.String(),
}.Acknowledgement()

Expand Down
11 changes: 8 additions & 3 deletions modules/apps/29-fee/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ func (k Keeper) SendPacket(ctx sdk.Context, chanCap *capabilitytypes.Capability,

// WriteAcknowledgement wraps IBC ChannelKeeper's WriteAcknowledgement function
// ICS29 WriteAcknowledgement is used for asynchronous acknowledgements
func (k Keeper) WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI, acknowledgement []byte) error {
func (k Keeper) WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI, acknowledgement ibcexported.Acknowledgement) error {
// retrieve the forward relayer that was stored in `onRecvPacket`
packetId := channeltypes.NewPacketId(packet.GetSourceChannel(), packet.GetSourcePort(), packet.GetSequence())
relayer, _ := k.GetForwardRelayerAddress(ctx, packetId)

k.DeleteForwardRelayerAddress(ctx, packetId)

ack := types.NewIncentivizedAcknowledgement(relayer, acknowledgement)
packedAck, err := channeltypes.PackAcknowledgement(acknowledgement)
if err != nil {
return err
}

incentivizedAck := types.NewIncentivizedAcknowledgement(relayer, packedAck)

// ics4Wrapper may be core IBC or higher-level middleware
return k.ics4Wrapper.WriteAcknowledgement(ctx, chanCap, packet, ack)
return k.ics4Wrapper.WriteAcknowledgement(ctx, chanCap, packet, incentivizedAck)
}
2 changes: 1 addition & 1 deletion modules/apps/29-fee/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (suite *KeeperTestSuite) TestWriteAcknowledgementAsync() {
timeoutTimestamp,
)

ack := []byte("ack")
ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)})
chanCap := suite.chainB.GetChannelCapability(suite.path.EndpointB.ChannelConfig.PortID, suite.path.EndpointB.ChannelID)

// malleate test case
Expand Down
24 changes: 21 additions & 3 deletions modules/apps/29-fee/types/ack.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
package types

import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"

channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v3/modules/core/exported"
)

var (
_ codectypes.UnpackInterfacesMessage = IncentivizedAcknowledgement{}
)

// NewIncentivizedAcknowledgement creates a new instance of IncentivizedAcknowledgement
func NewIncentivizedAcknowledgement(relayer string, ack []byte) IncentivizedAcknowledgement {
func NewIncentivizedAcknowledgement(relayer string, ack *codectypes.Any) IncentivizedAcknowledgement {
return IncentivizedAcknowledgement{
Result: ack,
AppAcknowledgement: ack,
ForwardRelayerAddress: relayer,
}
}

func (ack IncentivizedAcknowledgement) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var acknowledgement exported.Acknowledgement
return unpacker.UnpackAny(ack.AppAcknowledgement, &acknowledgement)
}

// Success implements the Acknowledgement interface. The acknowledgement is
// considered successful if the forward relayer address is empty. Otherwise it is
// considered a failed acknowledgement.
func (ack IncentivizedAcknowledgement) Success() bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think IncentivizedAcknowledgement needs to implement UnpackInterfaces. It might be best to write a test first to determine if this is true. UnpackInterfaces is used when JSON marshalling/unmarshalling an Any. That's why we have this test case

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 have implemented this but it seems to not be working 🤔 Getting some failed tests for the reason you mention here.

return ack.ForwardRelayerAddress != ""
unpackedAck, err := channeltypes.UnpackAcknowledgement(ack.AppAcknowledgement)
if err != nil {
return false
}

return unpackedAck.Success()
}

// Acknowledgement implements the Acknowledgement interface. It returns the
Expand Down
84 changes: 47 additions & 37 deletions modules/apps/29-fee/types/ack.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading