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: ics 29 packet callbacks #357

Merged
merged 43 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
7b51ebd
do handshake logic, create test file
AdityaSripal Aug 3, 2021
150211d
do cap logic and fix build
AdityaSripal Aug 5, 2021
412f3b6
Merge remote-tracking branch 'upstream/aditya/handshake' into feat/ic…
charleenfei Aug 27, 2021
734ead7
initial scaffolding
charleenfei Aug 12, 2021
1e894fe
added ics4 callbacks
charleenfei Aug 27, 2021
f735e57
Merge branch 'ics29-fee-middleware' into feat/ics-29-packetcallbacks
charleenfei Oct 6, 2021
435a2fa
Merge branch 'feat/ics-29-packetcallbacks' of github.com:cosmos/ibc-g…
charleenfei Nov 10, 2021
8d0c194
Merge branch 'ics29-fee-middleware' into feat/ics-29-packetcallbacks
charleenfei Nov 10, 2021
bfa8795
wip
charleenfei Nov 16, 2021
09985ce
update fee proto to include refund acc in storage of IdentifiedPacketFee
charleenfei Nov 16, 2021
ff7db8a
update yaml
charleenfei Nov 16, 2021
8c6d8ec
Merge branch 'update/fee_proto' into feat/ics-29-packetcallbacks
charleenfei Nov 16, 2021
9d9bcae
finish logic in packet callbacks
charleenfei Nov 16, 2021
d8e4429
wip
charleenfei Nov 17, 2021
e514f1a
store refund address in IdentifiedPacketFee
AdityaSripal Nov 17, 2021
f561cc0
Merge branch 'store_refund_address' into feat/ics-29-packetcallbacks
charleenfei Nov 18, 2021
97cc863
wip
charleenfei Nov 24, 2021
0a0022d
Merge branch 'ics29-fee-middleware' into feat/ics-29-packetcallbacks
charleenfei Nov 24, 2021
8e83ba2
proto file
charleenfei Nov 26, 2021
40951eb
incentivized ack proto
charleenfei Nov 26, 2021
e8b2ddc
Merge branch 'feat/incentivised_ack_proto' into feat/ics-29-packetcal…
charleenfei Nov 26, 2021
04763b6
wip
charleenfei Nov 30, 2021
e574499
Merge branch 'ics29-fee-middleware' of github.com:cosmos/ibc-go into …
charleenfei Nov 30, 2021
e7297a8
Merge branch 'ics29-fee-middleware' into feat/ics-29-packetcallbacks
charleenfei Nov 30, 2021
db93ac9
packet callbacks w ack
charleenfei Nov 30, 2021
2870993
testing wip
charleenfei Dec 1, 2021
64f77b9
Merge branch 'ics29-fee-middleware' of github.com:cosmos/ibc-go into …
charleenfei Dec 1, 2021
721a6c7
Merge branch 'ics29-fee-middleware' into feat/ics-29-packetcallbacks
charleenfei Dec 1, 2021
9f00d29
finished testing
charleenfei Dec 6, 2021
0844485
app changes
charleenfei Dec 7, 2021
e374cb2
initial pr review comments
charleenfei Dec 7, 2021
232e906
initial comments
charleenfei Dec 8, 2021
a8c69db
add tests
charleenfei Dec 8, 2021
6c85913
semantics
charleenfei Dec 8, 2021
9d84834
logic changes re: comments
charleenfei Dec 10, 2021
141d281
add cacheCtx
charleenfei Dec 10, 2021
76b7de9
tests
charleenfei Dec 10, 2021
7ced819
comments
charleenfei Dec 10, 2021
dc33d66
diff channel, test
charleenfei Dec 20, 2021
903b9d5
fix escrow bug
charleenfei Dec 20, 2021
8fc3aac
fix test
charleenfei Dec 20, 2021
2a061e4
sdk.AccAddress casting
charleenfei Dec 21, 2021
aae78ce
comments
charleenfei Dec 21, 2021
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
24 changes: 24 additions & 0 deletions modules/apps/29-fee/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (

"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
colin-axner marked this conversation as resolved.
Show resolved Hide resolved

"github.com/cosmos/ibc-go/modules/apps/29-fee/types"
transfertypes "github.com/cosmos/ibc-go/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/testing"
)
Expand Down Expand Up @@ -39,3 +42,24 @@ func (suite *FeeTestSuite) SetupTest() {
func TestIBCFeeTestSuite(t *testing.T) {
suite.Run(t, new(FeeTestSuite))
}

func (suite *FeeTestSuite) CreateICS20Packet(coin sdk.Coin) channeltypes.Packet {

fungibleTokenPacket := transfertypes.NewFungibleTokenPacketData(
coin.Denom,
sdk.NewInt(100).Uint64(),
suite.chainA.SenderAccount.GetAddress().String(),
suite.chainB.SenderAccount.GetAddress().String(),
)

return channeltypes.NewPacket(
fungibleTokenPacket.GetBytes(),
suite.chainA.SenderAccount.GetSequence(),
suite.path.EndpointA.ChannelConfig.PortID,
suite.path.EndpointA.ChannelID,
suite.path.EndpointB.ChannelConfig.PortID,
suite.path.EndpointB.ChannelID,
clienttypes.NewHeight(0, 100),
0,
)
}
87 changes: 80 additions & 7 deletions modules/apps/29-fee/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cosmos/ibc-go/modules/apps/29-fee/types"
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/modules/core/exported"
"github.com/cosmos/ibc-go/modules/core/exported"
)

// IBCModule implements the ICS26 callbacks for the fee middleware given the fee keeper and the underlying application.
Expand Down Expand Up @@ -171,32 +171,105 @@ func (im IBCModule) OnChanCloseConfirm(
}

// OnRecvPacket implements the IBCModule interface.
// If fees are not enabled, this callback will default to the ibc-core packet callback
func (im IBCModule) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) ibcexported.Acknowledgement {
// TODO: Implement fee specific logic if fee is enabled for the given channel
return im.app.OnRecvPacket(ctx, packet, relayer)
) exported.Acknowledgement {
if !im.keeper.IsFeeEnabled(ctx, packet.DestinationPort, packet.DestinationChannel) {
return im.app.OnRecvPacket(ctx, packet, relayer)
}

ack := im.app.OnRecvPacket(ctx, packet, relayer)

forwardRelayer, found := im.keeper.GetCounterpartyAddress(ctx, relayer.String())
if !found {
forwardRelayer = ""
}

return types.IncentivizedAcknowledgement{
Result: ack.Acknowledgement(),
ForwardRelayerAddress: forwardRelayer,
}
}

// OnAcknowledgementPacket implements the IBCModule interface
// If fees are not enabled, this callback will default to the ibc-core packet callback
func (im IBCModule) OnAcknowledgementPacket(
ctx sdk.Context,
packet channeltypes.Packet,
acknowledgement []byte,
relayer sdk.AccAddress,
) error {
// TODO: Implement fee specific logic if fee is enabled for the given channel
return im.app.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer)
if !im.keeper.IsFeeEnabled(ctx, packet.SourcePort, packet.SourceChannel) {
return im.app.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer)
}

ack := new(types.IncentivizedAcknowledgement)
if err := types.ModuleCdc.UnmarshalJSON(acknowledgement, ack); err != nil {
return sdkerrors.Wrapf(err, "cannot unmarshal ICS-29 incentivized packet acknowledgement: %v", ack)
}

packetId := channeltypes.NewPacketId(packet.SourceChannel, packet.SourcePort, packet.Sequence)
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)
}

// cache context before trying to distribute the fee
cacheCtx, writeFn := ctx.CacheContext()

forwardRelayer, _ := sdk.AccAddressFromBech32(ack.ForwardRelayerAddress)
Copy link
Contributor

Choose a reason for hiding this comment

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

we should probably check the error

Copy link
Contributor Author

@charleenfei charleenfei Dec 21, 2021

Choose a reason for hiding this comment

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

yes...but then should we forego distribution entirely if this error exists? in the case only this address is incorrect or doesnt exist, then it seems extreme

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 we handle this here already implicitly -> https://github.com/cosmos/ibc-go/blob/feat/ics-29-packetcallbacks/modules/apps/29-fee/keeper/escrow.go#L56

So no need to return an error here

refundAcc, _ := sdk.AccAddressFromBech32(identifiedPacketFee.RefundAddress)

err := im.keeper.DistributeFee(cacheCtx, refundAcc, forwardRelayer, relayer, packetId)

if err == nil {
// write the cache and then call underlying callback
writeFn()
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
}
// otherwise discard cache and call underlying callback
return im.app.OnAcknowledgementPacket(ctx, packet, ack.Result, relayer)
}

// OnTimeoutPacket implements the IBCModule interface
// If fees are not enabled, this callback will default to the ibc-core packet callback
func (im IBCModule) OnTimeoutPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) error {
// TODO: Implement fee specific logic if fee is enabled for the given channel
if !im.keeper.IsFeeEnabled(ctx, packet.SourcePort, packet.SourceChannel) {
return im.app.OnTimeoutPacket(ctx, packet, relayer)
}

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

identifiedPacketFee, found := im.keeper.GetFeeInEscrow(ctx, packetId)

if !found {
// return underlying callback if fee not found for given packetID
return im.app.OnTimeoutPacket(ctx, packet, relayer)
}

// cache context before trying to distribute the fee
cacheCtx, writeFn := ctx.CacheContext()

refundAcc, _ := sdk.AccAddressFromBech32(identifiedPacketFee.RefundAddress)
err := im.keeper.DistributeFeeTimeout(cacheCtx, refundAcc, relayer, packetId)

if err == nil {
// write the cache and then call underlying callback
writeFn()
// NOTE: The context returned by CacheContext() refers to a new EventManager, so it needs to explicitly set events to the original context.
ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events())
}

// otherwise discard cache and call underlying callback
return im.app.OnTimeoutPacket(ctx, packet, relayer)
}
Loading