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

test: add ParsePacketsFromEvents #4975

Merged

Conversation

tbruyelle
Copy link
Contributor

Description

Returns all the packets found in the events. Comes in addition with ParsePacketFromEvents (singular version), which only returns the first packet found in the events.

closes: #4287

Commit Message / Changelog Entry

type: commit message

see the guidelines for commit messages. (view raw markdown for examples)


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against correct branch (see CONTRIBUTING.md).
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards and Go style guide.
  • Wrote unit and integration tests.
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/).
  • Added relevant godoc comments.
  • Provide a commit message to be used for the changelog entry in the PR description for review.
  • Re-reviewed Files changed in the Github PR explorer.
  • Review Codecov Report in the comment section below once CI passes.

Copy link
Contributor

@charleenfei charleenfei left a comment

Choose a reason for hiding this comment

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

thanks @tbruyelle 🙏

testing/events.go Outdated Show resolved Hide resolved
@@ -120,6 +120,22 @@ func ParsePacketFromEvents(events []abci.Event) (channeltypes.Packet, error) {
return channeltypes.Packet{}, fmt.Errorf("acknowledgement event attribute not found")
}

// ParsePacketsFromEvents parses events emitted from a MsgRecvPacket and returns
// all the packets found.
func ParsePacketsFromEvents(events []abci.Event) ([]channeltypes.Packet, error) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd actually prefer if the main logic was happening in this function and have ParsePacketFromEvents would call into this and pick the first element from the result. I'm ok either way though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, the implementation might become easier to reason like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, so I added a test and then moved the logic from ParsePacketFromEvents to ParsePacketsFromEvents.

@srdtrk
Copy link
Member

srdtrk commented Oct 27, 2023

Thanks for this :)

Copy link
Contributor

@crodriguezvega crodriguezvega left a comment

Choose a reason for hiding this comment

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

Thank you, @tbruyelle.

func ParsePacketsFromEvents(events []abci.Event) ([]channeltypes.Packet, error) {
var packets []channeltypes.Packet
for i, ev := range events {
if ev.Type == channeltypes.EventTypeSendPacket {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could also remove this if statement, because the same one is in ParsePacketFromEvents.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done with the logic moved from one function to the other.

testing/events.go Outdated Show resolved Hide resolved
@@ -120,6 +120,22 @@ func ParsePacketFromEvents(events []abci.Event) (channeltypes.Packet, error) {
return channeltypes.Packet{}, fmt.Errorf("acknowledgement event attribute not found")
}

// ParsePacketsFromEvents parses events emitted from a MsgRecvPacket and returns
// all the packets found.
func ParsePacketsFromEvents(events []abci.Event) ([]channeltypes.Packet, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, the implementation might become easier to reason like that.

@crodriguezvega
Copy link
Contributor

@tbruyelle if you agree with the review comments, would you be able to work on them? Otherwise, if you're busy, we are happy to take over the PR and address the comments. If you give me permissions to push to your branch, I am happy to do that.

@tbruyelle
Copy link
Contributor Author

@tbruyelle if you agree with the review comments, would you be able to work on them? Otherwise, if you're busy, we are happy to take over the PR and address the comments. If you give me permissions to push to your branch, I am happy to do that.

@crodriguezvega oups indeed I was super busy the past weeks, but today is quieter, I'll address the comments, thanks for the ping!

@tbruyelle
Copy link
Contributor Author

@crodriguezvega I addressed all the comments. As requested I moved the logic from one function to the other, and also added a test before that to ensure I didn't break anything during the process.

Copy link
Contributor

@crodriguezvega crodriguezvega left a comment

Choose a reason for hiding this comment

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

Thank you for addressing the comments and adding a test, @tbruyelle. I have left a few more comments now. :)

testing/events.go Show resolved Hide resolved
)

func TestParsePacketsFromEvents(t *testing.T) {
tests := []struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this is the naming we use in the majority of other tests.

Suggested change
tests := []struct {
testCases := []struct {

Comment on lines 199 to 219
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)
assert := assert.New(t)

allPackets, err := ibctesting.ParsePacketsFromEvents(tt.events)

if tt.expectedError != "" {
assert.ErrorContains(err, tt.expectedError)
} else {
require.NoError(err)
assert.Equal(tt.expectedPackets, allPackets)
}

firstPacket, err := ibctesting.ParsePacketFromEvents(tt.events)

if tt.expectedError != "" {
assert.ErrorContains(err, tt.expectedError)
} else {
require.NoError(err)
assert.Equal(tt.expectedPackets[0], firstPacket)
}
})
}
Copy link
Contributor

@crodriguezvega crodriguezvega Nov 12, 2023

Choose a reason for hiding this comment

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

I think this could be written like this (assuming that tests is renamed to testCases):

for _, tc := range testCases {
  t.Run(tc.name, func(t *testing.T) {
    allPackets, err := ibctesting.ParsePacketsFromEvents(tc.events)

    if tc.expectedError == "" {
      require.NoError(t, err)
      require.Equal(t, tc.expectedPackets, allPackets)
    } else {
      require.ErrorContains(t, err, tc.expectedError)
    }

    firstPacket, err := ibctesting.ParsePacketFromEvents(tc.events)

    if tc.expectedError == "" {
      require.NoError(t, err)
      require.Equal(t, tc.expectedPackets[0], firstPacket)
    } else {
      require.ErrorContains(t, err, tc.expectedError)
    }
   })
}

So that:

  • We don't need to import assert.
  • We execute first the checks for the success case.

expectedError: "ibctesting.ParsePacketsFromEvents: strconv.ParseUint: parsing \"x\": invalid syntax",
},
{
name: "ok: events with packets",
Copy link
Contributor

Choose a reason for hiding this comment

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

We usually have the success case the first on of the slice.

Suggested change
name: "ok: events with packets",
name: "success",

{
name: "fail: no events",
events: []abci.Event{},
expectedError: "ibctesting.ParsePacketsFromEvents: acknowledgement event attribute not found",
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are using ErrorContains it could be enough to just have this (same for the other failure test cases):

Suggested change
expectedError: "ibctesting.ParsePacketsFromEvents: acknowledgement event attribute not found",
expectedError: "acknowledgement event attribute not found",

@github-actions github-actions bot added the testing Testing package and unit/integration tests label Nov 14, 2023
@tbruyelle
Copy link
Contributor Author

@crodriguezvega comments addressed here 8e06417

  • rename tests to testCases, tt to tc
  • remove assert package usage
  • revert if tc.expectedError condition
  • rename success case to "success", move it on top
  • remove expectedError common prefix

@codecov-commenter
Copy link

codecov-commenter commented Nov 14, 2023

Codecov Report

Merging #4975 (4974057) into main (523861a) will decrease coverage by 0.03%.
Report is 17 commits behind head on main.
The diff coverage is 20.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4975      +/-   ##
==========================================
- Coverage   79.59%   79.57%   -0.03%     
==========================================
  Files         189      189              
  Lines       13256    13261       +5     
==========================================
+ Hits        10551    10552       +1     
- Misses       2284     2288       +4     
  Partials      421      421              
Files Coverage Δ
modules/core/02-client/keeper/keeper.go 82.31% <100.00%> (+0.06%) ⬆️
modules/core/02-client/types/params.go 100.00% <ø> (ø)
modules/light-clients/06-solomachine/module.go 45.00% <0.00%> (-5.00%) ⬇️
modules/light-clients/07-tendermint/module.go 45.00% <0.00%> (-5.00%) ⬇️

Copy link
Contributor

@crodriguezvega crodriguezvega left a comment

Choose a reason for hiding this comment

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

Nice work, @tbruyelle. Thank you for addressing all the comments!

If you rebase with main, I will try to merge this before your branch gets out of sync again. 🤞

@tbruyelle tbruyelle force-pushed the tbruyelle/test/add-ParsePacketsFromEvents branch from 8e06417 to bc9bc8a Compare November 16, 2023 08:32
@tbruyelle
Copy link
Contributor Author

Nice work, @tbruyelle. Thank you for addressing all the comments!

If you rebase with main, I will try to merge this before your branch gets out of sync again. 🤞

Thank you Carlos, I've just rebased on main!

@chatton
Copy link
Contributor

chatton commented Nov 16, 2023

ah, those grandpa failures can be safely disregarded. This will be resolved with #5092

Closes: cosmos#4287

Returns all the packets found in the events. Comes in addition with
ParsePacketFromEvents (singular version), which only returns the first
packet found in the events.
Because errmod used in clienttypes.ParseHeight() returns a local path.
- rename tests to testCases, tt to tc
- remove assert package usage
- revert `if tc.expectedError` condition
- rename success case to "success", move it on top
- remove `expectedError` common prefix
@tbruyelle tbruyelle force-pushed the tbruyelle/test/add-ParsePacketsFromEvents branch from bc9bc8a to 4974057 Compare November 16, 2023 20:58
@crodriguezvega crodriguezvega merged commit 1f2a9b5 into cosmos:main Nov 17, 2023
56 checks passed
hoangdv2429 pushed a commit to hoangdv2429/ibc-go that referenced this pull request Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing Testing package and unit/integration tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ibctesting: func ParsePacketFromEvents only returns one packet from events
6 participants