Skip to content

Commit

Permalink
refactor(api!): replace legacy event asserts with current one (#6070)
Browse files Browse the repository at this point in the history
* refactor(modules/core): use asset events instead of assert event legacy

* refactor(modules/apps): use asset events instead of assert event legacy

* refactor: remove asset events legacy and events map

* fix: linter errors

* fix: linter errors

* fix linter errors

* add migration docs

* add changelog

---------

Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
Co-authored-by: Charly <charly@interchain.io>
Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
  • Loading branch information
4 people authored Apr 16, 2024
1 parent ffb1c0a commit 3341773
Show file tree
Hide file tree
Showing 9 changed files with 520 additions and 442 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (core/04-channel) [\#5991](https://github.com/cosmos/ibc-go/pull/5991) The client CLI `QueryLatestConsensusState` has been removed.
* (light-clients/06-solomachine) [\#6037](https://github.com/cosmos/ibc-go/pull/6037) Remove `Initialize` function from `ClientState` and move logic to `Initialize` function of `LightClientModule`.
* (core/02-client) [\#6084](https://github.com/cosmos/ibc-go/pull/6084) Removed `stakingKeeper` as an argument to `NewKeeper` and replaced with a `ConsensusHost` implementation.
* (testing) [\#6070](https://github.com/cosmos/ibc-go/pull/6070) Remove `AssertEventsLegacy` function.
* (core) [\#6138](https://github.com/cosmos/ibc-go/pull/6138) Remove `Router` reference from IBC core keeper and use instead the router on the existing `PortKeeper` reference.

### State Machine Breaking
Expand Down
16 changes: 16 additions & 0 deletions docs/docs/05-migrations/13-v8-to-v9.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ Please use the new functions `path.Setup`, `path.SetupClients`, `path.SetupConne
- The `mock.PV` type has been removed in favour of [`cmttypes.MockPV`](https://github.com/cometbft/cometbft/blob/v0.38.5/types/priv_validator.go#L50) ([#5709](https://github.com/cosmos/ibc-go/pull/5709)).
- Functions `ConstructUpdateTMClientHeader` and `ConstructUpdateTMClientHeaderWithTrustedHeight` of `TestChain` type have been replaced with `IBCClientHeader`. This function will construct a `07-tendermint` header to update the light client on the counterparty chain. The trusted height must be passed in as a non-zero height.
- `GetValsAtHeight` has been renamed to `GetTrustedValidators`
- `AssertEventsLegacy` function of `ibctesting` package (alias for `"github.com/cosmos/ibc-go/v9/testing"`) has been removed and `AssertEvents` function should be used instead (ref: [#6070](https://github.com/cosmos/ibc-go/pull/6070)).

```diff
// testing/events.go
- func AssertEventsLegacy(
- suite *testifysuite.Suite,
- expected EventsMap,
- actual []abci.Event,
- )

func AssertEvents(
suite *testifysuite.Suite,
expected []abci.Event,
actual []abci.Event,
)
```

### IBC core

Expand Down
157 changes: 105 additions & 52 deletions modules/apps/callbacks/types/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,22 @@ package types_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/ibc-go/modules/apps/callbacks/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/v8/testing"
)

func (s *CallbacksTypesTestSuite) TestEvents() {
constEvents := func() ibctesting.EventsMap {
return ibctesting.EventsMap{
types.EventTypeSourceCallback: {
sdk.AttributeKeyModule: types.ModuleName,
types.AttributeKeyCallbackType: string(types.CallbackTypeAcknowledgementPacket),
types.AttributeKeyCallbackAddress: ibctesting.TestAccAddress,
types.AttributeKeyCallbackGasLimit: "100000",
types.AttributeKeyCallbackCommitGasLimit: "200000",
types.AttributeKeyCallbackSourcePortID: ibctesting.MockPort,
types.AttributeKeyCallbackSourceChannelID: ibctesting.FirstChannelID,
types.AttributeKeyCallbackSequence: "1",
types.AttributeKeyCallbackResult: types.AttributeValueCallbackSuccess,
},
types.EventTypeDestinationCallback: {
sdk.AttributeKeyModule: types.ModuleName,
types.AttributeKeyCallbackType: string(types.CallbackTypeReceivePacket),
types.AttributeKeyCallbackAddress: ibctesting.TestAccAddress,
types.AttributeKeyCallbackGasLimit: "100000",
types.AttributeKeyCallbackCommitGasLimit: "200000",
types.AttributeKeyCallbackDestPortID: ibctesting.MockFeePort,
types.AttributeKeyCallbackDestChannelID: ibctesting.InvalidID,
types.AttributeKeyCallbackSequence: "1",
types.AttributeKeyCallbackResult: types.AttributeValueCallbackSuccess,
},
}
}

var expEvents ibctesting.EventsMap
testCases := []struct {
name string
packet channeltypes.Packet
callbackType types.CallbackType
callbackData types.CallbackData
callbackError error
malleate func()
name string
packet channeltypes.Packet
callbackType types.CallbackType
callbackData types.CallbackData
callbackError error
expectedEvents func() []abci.Event
}{
{
"success: ack callback",
Expand All @@ -59,7 +33,22 @@ func (s *CallbacksTypesTestSuite) TestEvents() {
CommitGasLimit: 200_000,
},
nil,
func() {},
func() []abci.Event {
return sdk.Events{
sdk.NewEvent(
types.EventTypeSourceCallback,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyCallbackType, string(types.CallbackTypeAcknowledgementPacket)),
sdk.NewAttribute(types.AttributeKeyCallbackAddress, ibctesting.TestAccAddress),
sdk.NewAttribute(types.AttributeKeyCallbackGasLimit, "100000"),
sdk.NewAttribute(types.AttributeKeyCallbackCommitGasLimit, "200000"),
sdk.NewAttribute(types.AttributeKeyCallbackSourcePortID, ibctesting.MockPort),
sdk.NewAttribute(types.AttributeKeyCallbackSourceChannelID, ibctesting.FirstChannelID),
sdk.NewAttribute(types.AttributeKeyCallbackSequence, "1"),
sdk.NewAttribute(types.AttributeKeyCallbackResult, types.AttributeValueCallbackSuccess),
),
}.ToABCIEvents()
},
},
{
"success: send packet callback",
Expand All @@ -74,8 +63,21 @@ func (s *CallbacksTypesTestSuite) TestEvents() {
CommitGasLimit: 200_000,
},
nil,
func() {
expEvents[types.EventTypeSourceCallback][types.AttributeKeyCallbackType] = string(types.CallbackTypeSendPacket)
func() []abci.Event {
return sdk.Events{
sdk.NewEvent(
types.EventTypeSourceCallback,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyCallbackType, string(types.CallbackTypeSendPacket)),
sdk.NewAttribute(types.AttributeKeyCallbackAddress, ibctesting.TestAccAddress),
sdk.NewAttribute(types.AttributeKeyCallbackGasLimit, "100000"),
sdk.NewAttribute(types.AttributeKeyCallbackCommitGasLimit, "200000"),
sdk.NewAttribute(types.AttributeKeyCallbackSourcePortID, ibctesting.MockPort),
sdk.NewAttribute(types.AttributeKeyCallbackSourceChannelID, ibctesting.FirstChannelID),
sdk.NewAttribute(types.AttributeKeyCallbackSequence, "1"),
sdk.NewAttribute(types.AttributeKeyCallbackResult, types.AttributeValueCallbackSuccess),
),
}.ToABCIEvents()
},
},
{
Expand All @@ -91,8 +93,21 @@ func (s *CallbacksTypesTestSuite) TestEvents() {
CommitGasLimit: 200_000,
},
nil,
func() {
expEvents[types.EventTypeSourceCallback][types.AttributeKeyCallbackType] = string(types.CallbackTypeTimeoutPacket)
func() []abci.Event {
return sdk.Events{
sdk.NewEvent(
types.EventTypeSourceCallback,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyCallbackType, string(types.CallbackTypeTimeoutPacket)),
sdk.NewAttribute(types.AttributeKeyCallbackAddress, ibctesting.TestAccAddress),
sdk.NewAttribute(types.AttributeKeyCallbackGasLimit, "100000"),
sdk.NewAttribute(types.AttributeKeyCallbackCommitGasLimit, "200000"),
sdk.NewAttribute(types.AttributeKeyCallbackSourcePortID, ibctesting.MockPort),
sdk.NewAttribute(types.AttributeKeyCallbackSourceChannelID, ibctesting.FirstChannelID),
sdk.NewAttribute(types.AttributeKeyCallbackSequence, "1"),
sdk.NewAttribute(types.AttributeKeyCallbackResult, types.AttributeValueCallbackSuccess),
),
}.ToABCIEvents()
},
},
{
Expand All @@ -108,7 +123,22 @@ func (s *CallbacksTypesTestSuite) TestEvents() {
CommitGasLimit: 200_000,
},
nil,
func() {},
func() []abci.Event {
return sdk.Events{
sdk.NewEvent(
types.EventTypeDestinationCallback,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyCallbackType, string(types.CallbackTypeReceivePacket)),
sdk.NewAttribute(types.AttributeKeyCallbackAddress, ibctesting.TestAccAddress),
sdk.NewAttribute(types.AttributeKeyCallbackGasLimit, "100000"),
sdk.NewAttribute(types.AttributeKeyCallbackCommitGasLimit, "200000"),
sdk.NewAttribute(types.AttributeKeyCallbackDestPortID, ibctesting.MockFeePort),
sdk.NewAttribute(types.AttributeKeyCallbackDestChannelID, ibctesting.InvalidID),
sdk.NewAttribute(types.AttributeKeyCallbackSequence, "1"),
sdk.NewAttribute(types.AttributeKeyCallbackResult, types.AttributeValueCallbackSuccess),
),
}.ToABCIEvents()
},
},
{
"success: unknown callback",
Expand All @@ -123,8 +153,21 @@ func (s *CallbacksTypesTestSuite) TestEvents() {
CommitGasLimit: 200_000,
},
nil,
func() {
expEvents[types.EventTypeSourceCallback][types.AttributeKeyCallbackType] = "something"
func() []abci.Event {
return sdk.Events{
sdk.NewEvent(
types.EventTypeSourceCallback,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyCallbackType, "something"),
sdk.NewAttribute(types.AttributeKeyCallbackAddress, ibctesting.TestAccAddress),
sdk.NewAttribute(types.AttributeKeyCallbackGasLimit, "100000"),
sdk.NewAttribute(types.AttributeKeyCallbackCommitGasLimit, "200000"),
sdk.NewAttribute(types.AttributeKeyCallbackSourcePortID, ibctesting.MockPort),
sdk.NewAttribute(types.AttributeKeyCallbackSourceChannelID, ibctesting.FirstChannelID),
sdk.NewAttribute(types.AttributeKeyCallbackSequence, "1"),
sdk.NewAttribute(types.AttributeKeyCallbackResult, types.AttributeValueCallbackSuccess),
),
}.ToABCIEvents()
},
},
{
Expand All @@ -140,38 +183,48 @@ func (s *CallbacksTypesTestSuite) TestEvents() {
CommitGasLimit: 200_000,
},
types.ErrNotPacketDataProvider,
func() {
expEvents[types.EventTypeSourceCallback][types.AttributeKeyCallbackResult] = types.AttributeValueCallbackFailure
expEvents[types.EventTypeSourceCallback][types.AttributeKeyCallbackError] = types.ErrNotPacketDataProvider.Error()
func() []abci.Event {
return sdk.Events{
sdk.NewEvent(
types.EventTypeSourceCallback,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyCallbackType, string(types.CallbackTypeAcknowledgementPacket)),
sdk.NewAttribute(types.AttributeKeyCallbackAddress, ibctesting.TestAccAddress),
sdk.NewAttribute(types.AttributeKeyCallbackGasLimit, "100000"),
sdk.NewAttribute(types.AttributeKeyCallbackCommitGasLimit, "200000"),
sdk.NewAttribute(types.AttributeKeyCallbackSourcePortID, ibctesting.MockPort),
sdk.NewAttribute(types.AttributeKeyCallbackSourceChannelID, ibctesting.FirstChannelID),
sdk.NewAttribute(types.AttributeKeyCallbackSequence, "1"),
sdk.NewAttribute(types.AttributeKeyCallbackResult, types.AttributeValueCallbackFailure),
sdk.NewAttribute(types.AttributeKeyCallbackError, types.ErrNotPacketDataProvider.Error()),
),
}.ToABCIEvents()
},
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
expEvents = constEvents()
tc.malleate()
newCtx := sdk.Context{}.WithEventManager(sdk.NewEventManager())

switch tc.callbackType {
case types.CallbackTypeReceivePacket:
delete(expEvents, types.EventTypeSourceCallback)
types.EmitCallbackEvent(
newCtx, tc.packet.GetDestPort(), tc.packet.GetDestChannel(),
tc.packet.GetSequence(), tc.callbackType, tc.callbackData, tc.callbackError,
)

default:
delete(expEvents, types.EventTypeDestinationCallback)
types.EmitCallbackEvent(
newCtx, tc.packet.GetSourcePort(), tc.packet.GetSourceChannel(),
tc.packet.GetSequence(), tc.callbackType, tc.callbackData, tc.callbackError,
)
}

events := newCtx.EventManager().Events().ToABCIEvents()
ibctesting.AssertEventsLegacy(&s.Suite, expEvents, events)
actualEvents := newCtx.EventManager().Events().ToABCIEvents()
expectedEvents := tc.expectedEvents()

ibctesting.AssertEvents(&s.Suite, expectedEvents, actualEvents)
})
}
}
25 changes: 13 additions & 12 deletions modules/apps/transfer/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,27 @@ func (suite *KeeperTestSuite) TestMsgTransfer() {
res, err := suite.chainA.GetSimApp().TransferKeeper.Transfer(ctx, msg)

// Verify events
events := ctx.EventManager().Events().ToABCIEvents()
expEvents := ibctesting.EventsMap{
"ibc_transfer": {
"sender": suite.chainA.SenderAccount.GetAddress().String(),
"receiver": suite.chainB.SenderAccount.GetAddress().String(),
"amount": coin.Amount.String(),
"denom": coin.Denom,
"memo": "memo",
},
}
actualEvents := ctx.EventManager().Events().ToABCIEvents()
expectedEvents := sdk.Events{
sdk.NewEvent(
types.EventTypeTransfer,
sdk.NewAttribute(sdk.AttributeKeySender, suite.chainA.SenderAccount.GetAddress().String()),
sdk.NewAttribute(types.AttributeKeyReceiver, suite.chainB.SenderAccount.GetAddress().String()),
sdk.NewAttribute(types.AttributeKeyAmount, coin.Amount.String()),
sdk.NewAttribute(types.AttributeKeyDenom, coin.Denom),
sdk.NewAttribute(types.AttributeKeyMemo, "memo"),
),
}.ToABCIEvents()

if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().NotEqual(res.Sequence, uint64(0))
ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events)
ibctesting.AssertEvents(&suite.Suite, expectedEvents, actualEvents)
} else {
suite.Require().Error(err)
suite.Require().Nil(res)
suite.Require().Len(events, 0)
suite.Require().Len(actualEvents, 0)
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions modules/core/02-client/keeper/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (suite *KeeperTestSuite) TestMsgCreateClientEvents() {

height, ok := path.EndpointA.Counterparty.Chain.LatestCommittedHeader.GetHeight().(clienttypes.Height)
suite.Require().True(ok)

clientState := ibctm.NewClientState(
path.EndpointA.Counterparty.Chain.ChainID, tmConfig.TrustLevel, tmConfig.TrustingPeriod, tmConfig.UnbondingPeriod, tmConfig.MaxClockDrift,
height, commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath)
Expand Down Expand Up @@ -59,6 +60,7 @@ func (suite *KeeperTestSuite) TestMsgUpdateClientEvents() {

clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)

trustedHeight := clientState.LatestHeight
header, err := suite.chainB.IBCClientHeader(suite.chainB.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)
Expand Down
34 changes: 19 additions & 15 deletions modules/core/04-channel/keeper/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

abci "github.com/cometbft/cometbft/abci/types"

capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
Expand Down Expand Up @@ -866,7 +868,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
name string
malleate func()
expResult func(commitment []byte, err error)
expEvents func(path *ibctesting.Path) map[string]map[string]string
expEvents func(path *ibctesting.Path) []abci.Event
}{
{
name: "success on ordered channel",
Expand Down Expand Up @@ -973,19 +975,21 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {
suite.Require().True(found)
suite.Require().Equal(uint64(1), nextSequenceAck, "sequence incremented for UNORDERED channel")
},
expEvents: func(path *ibctesting.Path) map[string]map[string]string {
return ibctesting.EventsMap{
types.EventTypeChannelFlushComplete: {
types.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID,
types.AttributeKeyChannelID: path.EndpointA.ChannelID,
types.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID,
types.AttributeCounterpartyChannelID: path.EndpointB.ChannelID,
types.AttributeKeyChannelState: path.EndpointA.GetChannel().State.String(),
},
sdk.EventTypeMessage: {
sdk.AttributeKeyModule: types.AttributeValueCategory,
},
}
expEvents: func(path *ibctesting.Path) []abci.Event {
return sdk.Events{
sdk.NewEvent(
types.EventTypeChannelFlushComplete,
sdk.NewAttribute(types.AttributeKeyPortID, path.EndpointA.ChannelConfig.PortID),
sdk.NewAttribute(types.AttributeKeyChannelID, path.EndpointA.ChannelID),
sdk.NewAttribute(types.AttributeCounterpartyPortID, path.EndpointB.ChannelConfig.PortID),
sdk.NewAttribute(types.AttributeCounterpartyChannelID, path.EndpointB.ChannelID),
sdk.NewAttribute(types.AttributeKeyChannelState, path.EndpointA.GetChannel().State.String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
}.ToABCIEvents()
},
},
{
Expand Down Expand Up @@ -1339,7 +1343,7 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() {

expEvents := tc.expEvents(path)

ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events)
ibctesting.AssertEvents(&suite.Suite, expEvents, events)
}
})
}
Expand Down
Loading

0 comments on commit 3341773

Please sign in to comment.