-
Notifications
You must be signed in to change notification settings - Fork 656
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
Add testing for client events #5686
Merged
Merged
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
452167b
test: adding TestMsgConnectionOpenInitEvents
chatton b885208
test: adding TestMsgConnectionOpenTryEvents
chatton 27d266e
test: adding TestMsgConnectionOpenAckEvents
chatton c2adf1b
test: adding TestMsgConnectionOpenConfirmEvents
chatton 1aa723e
test: adding TestMsgCreateClientEvent
chatton 3e038fd
test: adding TestMsgUpdateClientEvents
chatton 8f8258e
test: adding TestMsgUpgradeClientEvents
chatton 4db951d
test: adding event test for recover client
chatton c848868
test: refactor upgrade client test to use existing test
chatton cbd165e
test: add test for ibc software upgrade
chatton a1a8acb
chore: linting
chatton d2c70bc
Merge branch 'main' into cian/issue#2823-client-events
chatton e836ae2
Merge branch 'main' into cian/issue#2823-client-events
chatton 1b20be9
Merge branch 'main' into cian/issue#2823-client-events
chatton 785dd7a
Merge branch 'main' into cian/issue#2823-client-events
chatton 93f406f
Merge branch 'main' into cian/issue#2823-client-events
chatton 32aede9
Merge branch 'main' into cian/issue#2823-client-events
chatton fd9d9b1
Merge branch 'main' into cian/issue#2823-client-events
chatton 6b27961
Merge branch 'main' into cian/issue#2823-client-events
chatton 58bfa91
Merge branch 'main' into cian/issue#2823-client-events
chatton 6e93f77
Merge branch 'main' into cian/issue#2823-client-events
damiannolan ab454ce
Merge branch 'main' into cian/issue#2823-client-events
chatton 6aef5e0
chore: addressing PR feedback
chatton e22c6fc
Merge branch 'main' into cian/issue#2823-client-events
chatton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package keeper_test | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" | ||
commitmenttypes "github.com/cosmos/ibc-go/v8/modules/core/23-commitment/types" | ||
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint" | ||
ibctesting "github.com/cosmos/ibc-go/v8/testing" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestMsgCreateClientEvents() { | ||
suite.SetupTest() | ||
path := ibctesting.NewPath(suite.chainA, suite.chainB) | ||
|
||
path.EndpointA.Counterparty.Chain.NextBlock() | ||
|
||
tmConfig, ok := path.EndpointA.ClientConfig.(*ibctesting.TendermintConfig) | ||
suite.Require().True(ok) | ||
|
||
height := path.EndpointA.Counterparty.Chain.LatestCommittedHeader.GetHeight().(clienttypes.Height) | ||
clientState := ibctm.NewClientState( | ||
path.EndpointA.Counterparty.Chain.ChainID, tmConfig.TrustLevel, tmConfig.TrustingPeriod, tmConfig.UnbondingPeriod, tmConfig.MaxClockDrift, | ||
height, commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath) | ||
consensusState := path.EndpointA.Counterparty.Chain.LatestCommittedHeader.ConsensusState() | ||
|
||
msg, err := clienttypes.NewMsgCreateClient( | ||
clientState, consensusState, path.EndpointA.Chain.SenderAccount.GetAddress().String(), | ||
) | ||
suite.Require().NoError(err) | ||
|
||
res, err := suite.chainA.SendMsgs(msg) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
|
||
events := res.Events | ||
expectedEvents := sdk.Events{ | ||
sdk.NewEvent( | ||
clienttypes.EventTypeCreateClient, | ||
sdk.NewAttribute(clienttypes.AttributeKeyClientID, ibctesting.FirstClientID), | ||
sdk.NewAttribute(clienttypes.AttributeKeyClientType, clientState.ClientType()), | ||
sdk.NewAttribute(clienttypes.AttributeKeyConsensusHeight, clientState.GetLatestHeight().String()), | ||
), | ||
}.ToABCIEvents() | ||
|
||
var indexSet map[string]struct{} | ||
expectedEvents = sdk.MarkEventsToIndex(expectedEvents, indexSet) | ||
ibctesting.AssertEvents(&suite.Suite, expectedEvents, events) | ||
} | ||
|
||
func (suite *KeeperTestSuite) TestMsgUpdateClientEvents() { | ||
suite.SetupTest() | ||
path := ibctesting.NewPath(suite.chainA, suite.chainB) | ||
|
||
suite.Require().NoError(path.EndpointA.CreateClient()) | ||
|
||
suite.chainB.Coordinator.CommitBlock(suite.chainB) | ||
|
||
header, err := suite.chainA.ConstructUpdateTMClientHeader(suite.chainB, ibctesting.FirstClientID) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(header) | ||
|
||
msg, err := clienttypes.NewMsgUpdateClient( | ||
ibctesting.FirstClientID, header, | ||
path.EndpointA.Chain.SenderAccount.GetAddress().String(), | ||
) | ||
|
||
suite.Require().NoError(err) | ||
|
||
res, err := suite.chainA.SendMsgs(msg) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
|
||
events := res.Events | ||
expectedEvents := sdk.Events{ | ||
sdk.NewEvent( | ||
clienttypes.EventTypeUpdateClient, | ||
sdk.NewAttribute(clienttypes.AttributeKeyClientID, ibctesting.FirstClientID), | ||
sdk.NewAttribute(clienttypes.AttributeKeyClientType, path.EndpointA.GetClientState().ClientType()), | ||
sdk.NewAttribute(clienttypes.AttributeKeyConsensusHeight, path.EndpointA.GetClientState().GetLatestHeight().String()), | ||
sdk.NewAttribute(clienttypes.AttributeKeyConsensusHeights, path.EndpointA.GetClientState().GetLatestHeight().String()), | ||
), | ||
}.ToABCIEvents() | ||
|
||
var indexSet map[string]struct{} | ||
expectedEvents = sdk.MarkEventsToIndex(expectedEvents, indexSet) | ||
ibctesting.AssertEvents(&suite.Suite, expectedEvents, events) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package ibctesting | |
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"strconv" | ||
|
||
testifysuite "github.com/stretchr/testify/suite" | ||
|
@@ -207,17 +206,12 @@ func AssertEvents( | |
|
||
for i, expectedEvent := range expected { | ||
for _, actualEvent := range actual { | ||
// the actual event will have an extra attribute added automatically | ||
// by Cosmos SDK since v0.50, that's why we subtract 1 when comparing | ||
// with the number of attributes in the expected event. | ||
if expectedEvent.Type == actualEvent.Type && (len(expectedEvent.Attributes) == len(actualEvent.Attributes)-1) { | ||
// multiple events with the same type may be emitted, only mark the expected event as found | ||
// if all of the attributes match | ||
if shouldProcessEvent(expectedEvent, actualEvent) { | ||
attributeMatch := true | ||
for _, expectedAttr := range expectedEvent.Attributes { | ||
// any expected attributes that are not contained in the actual events will cause this event | ||
// not to match | ||
attributeMatch = attributeMatch && slices.Contains(actualEvent.Attributes, expectedAttr) | ||
attributeMatch = attributeMatch && containsAttribute(actualEvent.Attributes, expectedAttr.Key, expectedAttr.Value) | ||
} | ||
|
||
if attributeMatch { | ||
|
@@ -231,3 +225,39 @@ func AssertEvents( | |
suite.Require().True(foundEvents[i], "event: %s was not found in events", expectedEvent.Type) | ||
} | ||
} | ||
|
||
// shouldProcessEvent returns true if the given expected event should be processed based on event type. | ||
func shouldProcessEvent(expectedEvent abci.Event, actualEvent abci.Event) bool { | ||
if expectedEvent.Type != actualEvent.Type { | ||
return false | ||
} | ||
// the actual event will have an extra attribute added automatically | ||
// by Cosmos SDK since v0.50, that's why we subtract 1 when comparing | ||
// with the number of attributes in the expected event. | ||
if containsAttributeKey(actualEvent.Attributes, "msg_index") { | ||
return len(expectedEvent.Attributes) == len(actualEvent.Attributes)-1 | ||
} | ||
|
||
return len(expectedEvent.Attributes) == len(actualEvent.Attributes) | ||
} | ||
|
||
// containsAttribute returns true if the given key/value pair is contained in the given attributes. | ||
// NOTE: this ignores the indexed field, which can be set or unset depending on how the events are retrieved. | ||
func containsAttribute(attrs []abci.EventAttribute, key, value string) bool { | ||
for _, attr := range attrs { | ||
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. a slices.ContainsFn() can be used on these? 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. nice suggestion we can use return slices.ContainsFunc(attrs, func(attr abci.EventAttribute) bool {
return attr.Key == key && attr.Value == value
}) |
||
if attr.Key == key && attr.Value == value { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// containsAttributeKey returns true if the given key is contained in the given attributes. | ||
func containsAttributeKey(attrs []abci.EventAttribute, key string) bool { | ||
for _, attr := range attrs { | ||
if attr.Key == key { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line might not be necessary anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like the test fails without this.