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(api!): replace legacy event asserts with current one #6070

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c424ec6
refactor(modules/core): use asset events instead of assert event legacy
akaladarshi Mar 25, 2024
d487038
refactor(modules/apps): use asset events instead of assert event legacy
akaladarshi Mar 27, 2024
f29b72f
refactor: remove asset events legacy and events map
akaladarshi Mar 27, 2024
80d6242
Merge branch 'main' into akaldarshi/event-testing-cleanup
akaladarshi Mar 30, 2024
dd04e90
fix: linter errors
akaladarshi Mar 31, 2024
7c79e60
Merge branch 'main' into akaldarshi/event-testing-cleanup
DimitrisJim Apr 2, 2024
76894d0
Merge branch 'main' into akaldarshi/event-testing-cleanup
charleenfei Apr 3, 2024
5181f1b
fix: linter errors
akaladarshi Apr 3, 2024
4283f84
Merge branch 'main' into akaldarshi/event-testing-cleanup
DimitrisJim Apr 3, 2024
4fca1ec
Merge branch 'main' into akaldarshi/event-testing-cleanup
akaladarshi Apr 4, 2024
da84fc0
Merge branch 'main' into akaldarshi/event-testing-cleanup
charleenfei Apr 4, 2024
5dc0694
Merge branch 'main' into akaldarshi/event-testing-cleanup
akaladarshi Apr 4, 2024
7f823f7
Merge branch 'main' into akaldarshi/event-testing-cleanup
crodriguezvega Apr 9, 2024
11cd59f
Merge branch 'main' into akaldarshi/event-testing-cleanup
crodriguezvega Apr 10, 2024
f474442
fix linter errors
crodriguezvega Apr 10, 2024
36d3599
add migration docs
crodriguezvega Apr 11, 2024
7facb47
add changelog
crodriguezvega Apr 11, 2024
a549678
Merge branch 'main' into akaldarshi/event-testing-cleanup
akaladarshi Apr 11, 2024
fc9a30b
Merge branch 'main' into akaldarshi/event-testing-cleanup
crodriguezvega Apr 14, 2024
223d0d1
Merge branch 'main' into akaldarshi/event-testing-cleanup
crodriguezvega Apr 14, 2024
5bc2d44
Merge branch 'main' into akaldarshi/event-testing-cleanup
DimitrisJim Apr 15, 2024
4cc9f13
Merge branch 'main' into akaldarshi/event-testing-cleanup
akaladarshi Apr 16, 2024
e848956
Merge branch 'main' into akaldarshi/event-testing-cleanup
crodriguezvega Apr 16, 2024
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
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()
},
},
{
Comment on lines 975 to 995
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [851-972]

The expEvents function has been modified to return []abci.Event instead of the previous map[string]map[string]string type. This change is part of the refactor to use abci.Event for event creation and comparison, which is a more direct and type-safe approach to handling events in tests. The conversion of sdk.Events to abci.Events using .ToABCIEvents() at the end of the function is a necessary step to match the expected return type.

However, there's a minor issue with the usage of sdk.Events within the expEvents function. The sdk package is not explicitly imported in the provided code snippet, which could lead to a compilation error if it's not already imported elsewhere in the file. It's important to ensure that all necessary packages are imported to avoid compilation issues.

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

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
Loading