Skip to content

Commit b40dbc6

Browse files
authored
Consolidate usage of NewErrorAcknowledgement (#1565)
1 parent 41282c7 commit b40dbc6

18 files changed

Lines changed: 173 additions & 175 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Ref: https://keepachangelog.com/en/1.0.0/
4747
* (modules/29-fee)[\#1338](https://github.com/cosmos/ibc-go/pull/1338) Renaming `Result` field in `IncentivizedAcknowledgement` to `AppAcknowledgement`.
4848
* (modules/29-fee)[\#1343](https://github.com/cosmos/ibc-go/pull/1343) Renaming `KeyForwardRelayerAddress` to `KeyRelayerAddressForAsyncAck`, and `ParseKeyForwardRelayerAddress` to `ParseKeyRelayerAddressForAsyncAck`.
4949
* (apps/27-interchain-accounts)[\#1432](https://github.com/cosmos/ibc-go/pull/1432) Updating `RegisterInterchainAccount` to include an additional `version` argument, supporting ICS29 fee middleware functionality in ICS27 interchain accounts.
50+
* (apps/27-interchain-accounts)[\#1565](https://github.com/cosmos/ibc-go/pull/1565) Removing `NewErrorAcknowledgement` in favour of `channeltypes.NewErrorAcknowledgement`.
51+
* (transfer)[\#1565](https://github.com/cosmos/ibc-go/pull/1565) Removing `NewErrorAcknowledgement` in favour of `channeltypes.NewErrorAcknowledgement`.
52+
* (channel)[\#1565](https://github.com/cosmos/ibc-go/pull/1565) Updating `NewErrorAcknowledgement` to accept an error instead of a string and removing the possibility of non-deterministic writes to application state.
5053

5154
### State Machine Breaking
5255

docs/migrations/v3-to-v4.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ This is an API breaking change and as such IBC application developers will have
2626
The `OnChanOpenInit` application callback has been modified.
2727
The return signature now includes the application version as detailed in the latest IBC [spec changes](https://github.com/cosmos/ibc/pull/629).
2828

29+
The `NewErrorAcknowledgement` method signature has changed.
30+
It now accepts an `error` rather than a `string`. This was done in order to prevent accidental state changes.
31+
All error acknowledgements now contain a deterministic ABCI code and error message. It is the responsibility of the application developer to emit error details in events.
32+
2933
### ICS27 - Interchain Accounts
3034

3135
The `RegisterInterchainAccount` API has been modified to include an additional `version` argument. This change has been made in order to support ICS29 fee middleware, for relayer incentivization of ICS27 packets.

modules/apps/27-interchain-accounts/controller/ibc_middleware.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ func (im IBCMiddleware) OnRecvPacket(
138138
packet channeltypes.Packet,
139139
_ sdk.AccAddress,
140140
) ibcexported.Acknowledgement {
141-
return channeltypes.NewErrorAcknowledgement("cannot receive packet on controller chain")
141+
err := sdkerrors.Wrapf(icatypes.ErrInvalidChannelFlow, "cannot receive packet on controller chain")
142+
ack := channeltypes.NewErrorAcknowledgement(err)
143+
keeper.EmitAcknowledgementEvent(ctx, packet, ack, err)
144+
return ack
142145
}
143146

144147
// OnAcknowledgementPacket implements the IBCMiddleware interface
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package keeper
2+
3+
import (
4+
"fmt"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
8+
icatypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"
9+
"github.com/cosmos/ibc-go/v3/modules/core/exported"
10+
)
11+
12+
// EmitAcknowledgementEvent emits an event signalling a successful or failed acknowledgement and including the error
13+
// details if any.
14+
func EmitAcknowledgementEvent(ctx sdk.Context, packet exported.PacketI, ack exported.Acknowledgement, err error) {
15+
attributes := []sdk.Attribute{
16+
sdk.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
17+
sdk.NewAttribute(icatypes.AttributeKeyControllerChannelID, packet.GetDestChannel()),
18+
sdk.NewAttribute(icatypes.AttributeKeyAckSuccess, fmt.Sprintf("%t", ack.Success())),
19+
}
20+
21+
if err != nil {
22+
attributes = append(attributes, sdk.NewAttribute(icatypes.AttributeKeyAckError, err.Error()))
23+
}
24+
25+
ctx.EventManager().EmitEvent(
26+
sdk.NewEvent(
27+
icatypes.EventTypePacket,
28+
attributes...,
29+
),
30+
)
31+
}

modules/apps/27-interchain-accounts/host/ibc_module.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ func (im IBCModule) OnRecvPacket(
106106
_ sdk.AccAddress,
107107
) ibcexported.Acknowledgement {
108108
if !im.keeper.IsHostEnabled(ctx) {
109-
return types.NewErrorAcknowledgement(types.ErrHostSubModuleDisabled)
109+
return channeltypes.NewErrorAcknowledgement(types.ErrHostSubModuleDisabled)
110110
}
111111

112112
txResponse, err := im.keeper.OnRecvPacket(ctx, packet)
113113
ack := channeltypes.NewResultAcknowledgement(txResponse)
114114
if err != nil {
115-
ack = types.NewErrorAcknowledgement(err)
115+
ack = channeltypes.NewErrorAcknowledgement(err)
116116
}
117117

118118
// Emit an event indicating a successful or failed acknowledgement.

modules/apps/27-interchain-accounts/host/ibc_module_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() {
403403
suite.chainB.GetSimApp().ICAAuthModule.IBCApp.OnRecvPacket = func(
404404
ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress,
405405
) exported.Acknowledgement {
406-
return channeltypes.NewErrorAcknowledgement("failed OnRecvPacket mock callback")
406+
return channeltypes.NewErrorAcknowledgement(fmt.Errorf("failed OnRecvPacket mock callback"))
407407
}
408408
}, true,
409409
},

modules/apps/27-interchain-accounts/host/keeper/events.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ import (
1010
// EmitAcknowledgementEvent emits an event signalling a successful or failed acknowledgement and including the error
1111
// details if any.
1212
func EmitAcknowledgementEvent(ctx sdk.Context, packet exported.PacketI, ack exported.Acknowledgement, err error) {
13-
var errorMsg string
13+
attributes := []sdk.Attribute{
14+
sdk.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
15+
sdk.NewAttribute(icatypes.AttributeKeyHostChannelID, packet.GetDestChannel()),
16+
sdk.NewAttribute(icatypes.AttributeKeyAckSuccess, fmt.Sprintf("%t", ack.Success())),
17+
}
18+
1419
if err != nil {
15-
errorMsg = err.Error()
20+
attributes = append(attributes, sdk.NewAttribute(icatypes.AttributeKeyAckError, err.Error()))
1621
}
1722

1823
ctx.EventManager().EmitEvent(
1924
sdk.NewEvent(
2025
icatypes.EventTypePacket,
21-
sdk.NewAttribute(sdk.AttributeKeyModule, icatypes.ModuleName),
22-
sdk.NewAttribute(icatypes.AttributeKeyAckError, errorMsg),
23-
sdk.NewAttribute(icatypes.AttributeKeyHostChannelID, packet.GetDestChannel()),
24-
sdk.NewAttribute(icatypes.AttributeKeyAckSuccess, fmt.Sprintf("%t", ack.Success())),
26+
attributes...,
2527
),
2628
)
2729
}

modules/apps/27-interchain-accounts/host/types/ack.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

modules/apps/27-interchain-accounts/host/types/ack_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
tmprotostate "github.com/tendermint/tendermint/proto/tendermint/state"
1010
tmstate "github.com/tendermint/tendermint/state"
1111

12-
"github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host/types"
1312
ibctesting "github.com/cosmos/ibc-go/v3/testing"
1413
)
1514

@@ -80,21 +79,3 @@ func (suite *TypesTestSuite) TestABCICodeDeterminism() {
8079
suite.Require().Equal(hash, hashSameABCICode)
8180
suite.Require().NotEqual(hash, hashDifferentABCICode)
8281
}
83-
84-
// TestAcknowledgementError will verify that only a constant string and
85-
// ABCI error code are used in constructing the acknowledgement error string
86-
func (suite *TypesTestSuite) TestAcknowledgementError() {
87-
// same ABCI error code used
88-
err := sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "error string 1")
89-
errSameABCICode := sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "error string 2")
90-
91-
// different ABCI error code used
92-
errDifferentABCICode := sdkerrors.ErrNotFound
93-
94-
ack := types.NewErrorAcknowledgement(err)
95-
ackSameABCICode := types.NewErrorAcknowledgement(errSameABCICode)
96-
ackDifferentABCICode := types.NewErrorAcknowledgement(errDifferentABCICode)
97-
98-
suite.Require().Equal(ack, ackSameABCICode)
99-
suite.Require().NotEqual(ack, ackDifferentABCICode)
100-
}

modules/apps/27-interchain-accounts/types/events.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ package types
44
const (
55
EventTypePacket = "ics27_packet"
66

7-
AttributeKeyAckError = "error"
8-
AttributeKeyHostChannelID = "host_channel_id"
9-
AttributeKeyAckSuccess = "success"
7+
AttributeKeyAckError = "error"
8+
AttributeKeyHostChannelID = "host_channel_id"
9+
AttributeKeyControllerChannelID = "controller_channel_id"
10+
AttributeKeyAckSuccess = "success"
1011
)

0 commit comments

Comments
 (0)