-
Notifications
You must be signed in to change notification settings - Fork 751
Callbacks Eureka #7934
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
Callbacks Eureka #7934
Changes from 1 commit
be8cdee
a9c28f5
bcc4b36
afa88ab
e3eab32
7d6f7dd
bb73c5f
bfea28d
bc83db1
ec65620
0dcc001
9403192
1d22ea9
0c27be8
c067db3
684fe73
b0e0aaf
e93d5e1
348fe72
170ccfe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| errorsmod "cosmossdk.io/errors" | ||
| storetypes "cosmossdk.io/store/types" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
|
||
| "github.com/cosmos/ibc-go/modules/apps/callbacks/types" | ||
| ) | ||
|
|
||
| // ProcessCallback executes the callbackExecutor and reverts contract changes if the callbackExecutor fails. | ||
| // | ||
| // Error Precedence and Returns: | ||
| // - oogErr: Takes the highest precedence. If the callback runs out of gas, an error wrapped with types.ErrCallbackOutOfGas is returned. | ||
| // - panicErr: Takes the second-highest precedence. If a panic occurs and it is not propagated, an error wrapped with types.ErrCallbackPanic is returned. | ||
| // - callbackErr: If the callbackExecutor returns an error, it is returned as-is. | ||
| // | ||
| // panics if | ||
| // - the contractExecutor panics for any reason, and the callbackType is SendPacket, or | ||
| // - the contractExecutor runs out of gas and the relayer has not reserved gas grater than or equal to | ||
| // CommitGasLimit. | ||
| func ProcessCallback( | ||
| ctx sdk.Context, callbackType types.CallbackType, | ||
| callbackData types.CallbackData, callbackExecutor func(sdk.Context) error, | ||
| ) (err error) { | ||
| cachedCtx, writeFn := ctx.CacheContext() | ||
| cachedCtx = cachedCtx.WithGasMeter(storetypes.NewGasMeter(callbackData.ExecutionGasLimit)) | ||
|
|
||
| defer func() { | ||
| // consume the minimum of g.consumed and g.limit | ||
| ctx.GasMeter().ConsumeGas(cachedCtx.GasMeter().GasConsumedToLimit(), fmt.Sprintf("ibc %s callback", callbackType)) | ||
|
|
||
| // recover from all panics except during SendPacket callbacks | ||
| if r := recover(); r != nil { | ||
| if callbackType == types.CallbackTypeSendPacket { | ||
| panic(r) | ||
| } | ||
| err = errorsmod.Wrapf(types.ErrCallbackPanic, "ibc %s callback panicked with: %v", callbackType, r) | ||
| } | ||
|
|
||
| // if the callback ran out of gas and the relayer has not reserved enough gas, then revert the state | ||
| if cachedCtx.GasMeter().IsPastLimit() { | ||
| if callbackData.AllowRetry() { | ||
| panic(storetypes.ErrorOutOfGas{Descriptor: fmt.Sprintf("ibc %s callback out of gas; commitGasLimit: %d", callbackType, callbackData.CommitGasLimit)}) | ||
| } | ||
| err = errorsmod.Wrapf(types.ErrCallbackOutOfGas, "ibc %s callback out of gas", callbackType) | ||
| } | ||
|
|
||
| // allow the transaction to be committed, continuing the packet lifecycle | ||
| }() | ||
|
|
||
| err = callbackExecutor(cachedCtx) | ||
| if err == nil { | ||
| writeFn() | ||
| } | ||
|
|
||
| return err | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import ( | |
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
|
||
| "github.com/cosmos/ibc-go/modules/apps/callbacks/internal" | ||
| "github.com/cosmos/ibc-go/modules/apps/callbacks/types" | ||
| clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" | ||
| channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" | ||
|
|
@@ -49,6 +50,14 @@ func NewIBCMiddleware( | |
| panic(errors.New("contract keeper cannot be nil")) | ||
| } | ||
|
|
||
| if writeAckWrapper == nil { | ||
| panic(errors.New("write acknowledgement wrapper cannot be nil")) | ||
| } | ||
|
|
||
| if chanKeeperV2 == nil { | ||
| panic(errors.New("channel keeper v2 cannot be nil")) | ||
| } | ||
|
|
||
| if maxCallbackGas == 0 { | ||
| panic(errors.New("maxCallbackGas cannot be zero")) | ||
| } | ||
|
|
@@ -111,7 +120,7 @@ func (im IBCMiddleware) OnSendPacket( | |
| ) | ||
| } | ||
|
|
||
| err = types.ProcessCallback(sdkCtx, types.CallbackTypeSendPacket, cbData, callbackExecutor) | ||
| err = internal.ProcessCallback(sdkCtx, types.CallbackTypeSendPacket, cbData, callbackExecutor) | ||
| // contract keeper is allowed to reject the packet send. | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -172,17 +181,13 @@ func (im IBCMiddleware) OnRecvPacket( | |
| TimeoutTimestamp: 0, | ||
|
Comment on lines
+180
to
+181
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. timeout values zeroed out everywhere. So we preserve interface, but contracts should not rely on these values for v2 packets |
||
| } | ||
| // wrap the individual acknowledgement into the channeltypesv2.Acknowledgement since it implements the exported.Acknowledgement interface | ||
srdtrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var ack channeltypesv2.Acknowledgement | ||
| if recvResult.Status == channeltypesv2.PacketStatus_Failure { | ||
| ack = channeltypesv2.NewAcknowledgement(channeltypesv2.ErrorAcknowledgement[:]) | ||
| } else { | ||
| ack = channeltypesv2.NewAcknowledgement(recvResult.Acknowledgement) | ||
| } | ||
| // since we return early on failure, we are guaranteed that the ack is a successful acknowledgement | ||
| ack := channeltypesv2.NewAcknowledgement(recvResult.Acknowledgement) | ||
| return im.contractKeeper.IBCReceivePacketCallback(cachedCtx, packetv1, ack, cbData.CallbackAddress, payload.Version) | ||
| } | ||
|
|
||
| // callback execution errors are not allowed to block the packet lifecycle, they are only used in event emissions | ||
| err = types.ProcessCallback(sdkCtx, types.CallbackTypeReceivePacket, cbData, callbackExecutor) | ||
| err = internal.ProcessCallback(sdkCtx, types.CallbackTypeReceivePacket, cbData, callbackExecutor) | ||
| types.EmitCallbackEvent( | ||
| sdkCtx, payload.DestinationPort, destinationClient, sequence, | ||
| types.CallbackTypeReceivePacket, cbData, err, | ||
|
|
@@ -239,13 +244,18 @@ func (im IBCMiddleware) OnAcknowledgementPacket( | |
| TimeoutHeight: clienttypes.Height{}, | ||
| TimeoutTimestamp: 0, | ||
| } | ||
| // NOTE: The callback is receiving the acknowledgement that the application received for its particular payload. | ||
| // In the case of a successful acknowledgement, this will be the acknowledgement sent by the counterparty application for the given payload | ||
| // In the case of an error acknowledgement, this will be the sentinel error acknowledgement bytes defined by IBC v2 protocol. | ||
| // Thus, the contract must be aware that the sentinel error acknowledgement signals a failed receive | ||
| // and the contract must handle this error case and the corresponding success case (ie ack != ErrorAcknowledgement) accordingly. | ||
| return im.contractKeeper.IBCOnAcknowledgementPacketCallback( | ||
| cachedCtx, packetv1, acknowledgement, relayer, cbData.CallbackAddress, cbData.SenderAddress, payload.Version, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, here acknowledgement callback just expects []byte
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is bad because old apps won't recognize the new error ack we have, we should consider if this is ok or we need to do something about this such as passing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, discussed and the plan is to document and keep as-is for now. We will check with confio team on what they would prefer |
||
| ) | ||
| } | ||
|
|
||
| // callback execution errors are not allowed to block the packet lifecycle, they are only used in event emissions | ||
| err = types.ProcessCallback(sdkCtx, types.CallbackTypeAcknowledgementPacket, cbData, callbackExecutor) | ||
| err = internal.ProcessCallback(sdkCtx, types.CallbackTypeAcknowledgementPacket, cbData, callbackExecutor) | ||
| types.EmitCallbackEvent( | ||
| sdkCtx, payload.SourcePort, sourceClient, sequence, | ||
| types.CallbackTypeAcknowledgementPacket, cbData, err, | ||
|
|
@@ -306,7 +316,7 @@ func (im IBCMiddleware) OnTimeoutPacket( | |
| } | ||
|
|
||
| // callback execution errors are not allowed to block the packet lifecycle, they are only used in event emissions | ||
| err = types.ProcessCallback(sdkCtx, types.CallbackTypeTimeoutPacket, cbData, callbackExecutor) | ||
| err = internal.ProcessCallback(sdkCtx, types.CallbackTypeTimeoutPacket, cbData, callbackExecutor) | ||
| types.EmitCallbackEvent( | ||
| sdkCtx, payload.SourcePort, sourceClient, sequence, | ||
| types.CallbackTypeTimeoutPacket, cbData, err, | ||
|
|
@@ -331,21 +341,17 @@ func (im IBCMiddleware) WriteAcknowledgement( | |
| return errorsmod.Wrapf(channeltypesv2.ErrInvalidAcknowledgement, "async packet not found for clientID (%s) and sequence (%d)", clientID, sequence) | ||
| } | ||
|
|
||
| // if WriteAckWrapper is wired, use the wrapper to write the acknowledgement | ||
| // otherwise, use the channel keeper v2 to write the acknowledgement directly | ||
| var writeAckWrapper api.WriteAcknowledgementWrapper | ||
| if im.writeAckWrapper != nil { | ||
| writeAckWrapper = im.writeAckWrapper | ||
| } else { | ||
| writeAckWrapper = im.chanKeeperV2 | ||
| } | ||
| err := writeAckWrapper.WriteAcknowledgement(ctx, clientID, sequence, ack) | ||
| err := im.writeAckWrapper.WriteAcknowledgement(ctx, clientID, sequence, ack) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // NOTE: use first payload as the payload that is being handled by callbacks middleware | ||
| // must reconsider if multipacket data gets supported with async packets | ||
| // TRACKING ISSUE: https://github.com/cosmos/ibc-go/issues/7950 | ||
| if len(packet.Payloads) != 1 { | ||
| return errorsmod.Wrapf(channeltypesv2.ErrInvalidAcknowledgement, "async packet has multiple payloads") | ||
| } | ||
| payload := packet.Payloads[0] | ||
|
|
||
|
Comment on lines
+347
to
+356
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. somewhere before this we should assert
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good shout |
||
| packetData, err := im.app.UnmarshalPacketData(payload) | ||
|
|
@@ -393,7 +399,7 @@ func (im IBCMiddleware) WriteAcknowledgement( | |
| } | ||
|
|
||
| // callback execution errors are not allowed to block the packet lifecycle, they are only used in event emissions | ||
| err = types.ProcessCallback(sdkCtx, types.CallbackTypeReceivePacket, cbData, callbackExecutor) | ||
| err = internal.ProcessCallback(sdkCtx, types.CallbackTypeReceivePacket, cbData, callbackExecutor) | ||
| types.EmitCallbackEvent( | ||
| sdkCtx, payload.DestinationPort, clientID, sequence, | ||
| types.CallbackTypeReceivePacket, cbData, err, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.