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

x/lockup: ensure events are emitted and tests added #2391

Closed
wants to merge 3 commits into from

Conversation

stackman27
Copy link
Contributor

Closes: #2270

What is the purpose of the change

Currently, our tests are missing assertions about events. Moreover, it seems that some of our events are missing based on
1939

The lockup module emits the following events:

There are 4 types of events that exist in lockup module:

  • types.TypeEvtLockTokens - "lock_tokens"
  • types.TypeEvtAddTokensToLock - "add_tokens_to_lock"
  • types.TypeEvtBeginUnlockAll - "begin_unlock_all"
  • types.TypeEvtBeginUnlock - "begin_unlock"

Testing and Verifying

  1. Added events test in msg_server_test
  2. Added emit.test to test out if events emit correct events

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes? (no)
  • Is a relevant changelog entry added to the Unreleased section in CHANGELOG.md? (no)
  • How is the feature or change documented? (not applicable / specification (x/<module>/spec/) / Osmosis docs repo / not documented)

@stackman27 stackman27 requested a review from a team August 12, 2022 22:47
}

// Question: Any way we can make this private method?
func BeginUnlockEvent(lock *types.PeriodLock) sdk.Event {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We use this Event to append here

It would be ideal to make this a private method, but i dont want to repeat code

)
}

// EmitLockToken returns a new event when user lock tokens.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// EmitLockToken returns a new event when user lock tokens.
// EmitExtendLockToken returns a new event when user locks tokens.

sdk.NewAttribute(types.AttributePeriodLockAmount, msg.Coins.String()),
),
})
events.EmitAddTokenToLock(ctx, lockID, msg.Owner, msg.Coins.String())
Copy link
Member

Choose a reason for hiding this comment

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

I think we should move this inside keeper so that if another module ends up calling AddToExistingLock, the event does not get missed

),
})

events.EmitLockToken(ctx, &lock)
Copy link
Member

Choose a reason for hiding this comment

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

ditto for moving this inside the keeper's CreateLock

@p0mvn
Copy link
Member

p0mvn commented Aug 13, 2022

Can we also add the event info to docs (README), please?

@stackman27
Copy link
Contributor Author

stackman27 commented Aug 13, 2022

Can we also add the event info to docs (README), please?

I didn't add README.md following @alexanderbez comment here

Didn't know what the final verdict was

@p0mvn
Copy link
Member

p0mvn commented Aug 13, 2022

Can we also add the event info to docs (README), please?

I didn't add README.md following @alexanderbez comment here

Didn't know what the final verdict was

I think Bez is right that these tend to get out of sync and are difficult to maintain. However, these docs get auto-uploaded to our docs.osmosis.zone website. Currently, there is no information about events on the docs site.

I think that this information could be useful for integrators. So, in my opinion, we should still have it

@stackman27
Copy link
Contributor Author

Can we also add the event info to docs (README), please?

I didn't add README.md following @alexanderbez comment here
Didn't know what the final verdict was

I think Bez is right that these tend to get out of sync and are difficult to maintain. However, these docs get auto-uploaded to our docs.osmosis.zone website. Currently, there is no information about events on the docs site.

I think that this information could be useful for integrators. So, in my opinion, we should still have it

Gotchu i'll definitely add it then

@@ -0,0 +1,264 @@
package events_test
Copy link
Member

@ValarDragon ValarDragon Aug 13, 2022

Choose a reason for hiding this comment

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

This entire test file seems like useless boilerplate, its questionable that we even needed these different EmitEvent functions, we definitely don't need tests ensuring that the call to the emit function works correctly.

What tests are useful for is ensuring events are emitted after the actual.execution of relevant messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes sense, the entire boilerplate tests definitely seems like an overkill. I will remove this so that we can test messages that emit these events directly in msg_server_test.go

Copy link
Member

@p0mvn p0mvn Aug 15, 2022

Choose a reason for hiding this comment

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

In my eyes, the goal at this level of abstraction is to test that attributes are set correctly. This is achieved by the tests here.

Then, it is possible to simply focus that a certain event kind is emitted at the keeper/message server layer. Given that we have already tested the attributes here in emit_test.go, we can reduce the boilerplate in tests for keeper and simply call for testing the fact that an event has been emitted:

func (s *KeeperTestHelper) AssertEventEmitted(ctx sdk.Context, eventTypeExpected string, numEventsExpected int) {

Otherwise, we would have to validate the attributes in keeper and message server tests which goes against the separation of concerns

@ValarDragon
Copy link
Member

ValarDragon commented Aug 13, 2022

I don't really like this entire direction.

Every event here seems like we could derive it automatically from the message response, and these tests aren't actually helping check that we are emitting things correctly post message execution. At most extracting building an event to its own function makes sense to me. ctx.EventManager().EmitEvent(constructAddTokensToLockEvent(lock)), but again seems like short term status

The message_server_test changes are great, we should amend to test the real constructed event as wekk

Comment on lines 33 to 35
if ctx.EventManager() == nil {
return
}
Copy link
Member

Choose a reason for hiding this comment

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

Why are we doing all these nil guards? Seems fine to me if it panics with NPE, this is misconfiguration that should get caught in testing, not be graceful no-op

Copy link
Member

Choose a reason for hiding this comment

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

While I agree that no-op is probably not the way to go, I think it is worth having a check for nil event manager somewhere since ctx is injected into Osmosis's message server by its dependency - cosmos-sdk. I don't think we should be spending time on this right now though so it should be fine if we just remove the check

@stackman27
Copy link
Contributor Author

I don't really like this entire direction.

Every event here seems like we could derive it automatically from the message response, and these tests aren't actually helping check that we are emitting things correctly post message execution. At most extracting building an event to its own function makes sense to me. ctx.EventManager().EmitEvent(constructAddTokensToLockEvent(lock)), but again seems like short term status

The message_server_test changes are great, we should amend to test the real constructed event as wekk

We should definitely make a decision on how we should structure events + test events as we've followed this structure in x/gamm and x/superfluid EPIC 2195

@p0mvn what do you think?

@github-actions github-actions bot added the C:app-wiring Changes to the app folder label Aug 14, 2022
actualEvents = append(actualEvents, event)
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

line [413-421] will be moved to app/apptesting/events.go once i receive approach ack

}

if tc.expectPass {
suite.Require().Equal(expectedEvents[0], actualEvents[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests the events type as well as its attributes

@p0mvn
Copy link
Member

p0mvn commented Aug 16, 2022

I don't really like this entire direction.

Every event here seems like we could derive it automatically from the message response, and these tests aren't actually helping check that we are emitting things correctly post message execution. At most extracting building an event to its own function makes sense to me. ctx.EventManager().EmitEvent(constructAddTokensToLockEvent(lock)), but again seems like short term status

The message_server_test changes are great, we should amend to test the real constructed event as week

Every event here seems like we could derive it automatically from the message response

How can events be derived from the message response?

I don't think it is possible in the example here.

First, some messages emit events based on conditions. For example, LockTokens emits TypeEvtAddTokensToLock if lock exists, TypeEvtLockTokens if it doesn't.

Second, these should really not be in the message server but in keeper. Other modules can call CreateLock bypassing lockup message server. As a result, if we have messages emitted here, they would be missed when CreateLock is called from another module.

At most extracting building an event to its own function makes sense to me

That's the goal of the internal/events package inside the module to standardize the event creation for consistency. I agree that duplicating Emit... function for every event is unnecessary and was planning to refactor that after these PRs are merged in. However, I think that we should keep the constructors.

again seems like short term status

That's fine because we can do incremental refactorings. I think changing the direction now when there is a relevant PR merged and 2 more are in progress, leading to inconsistent designs would be difficult to manage.

I also don't see how most of these events can be generated right now due to the reasons mentioned above.

The message_server_test changes are great, we should amend to test the real constructed event as week

I mentioned the original thinking for the emit_test.go to be used for validating attributes in this comment. Basically, if we test event construction at a lower level of abstraction, here we can only test the fact of a specific kind of event being emitted, without validating the attributes. The construction and attributes are already tested at a lower level of abstraction.

To summarize my thinking:

  • I still think that we should construct the events from the internal/events package and test the attributes there
    • This construction pattern across modules makes it more visible and consistent where and when the events are emitted.
  • We should test the event construction / attributes in the events package
  • We can remove the duplicate Emit... functions and the event manager check
  • I think validating the event construction at the message server layer is useful and I support extracting this helper to be called in the message server tests. However, I think it might not be necessary given that we test the attributes/construction in the internal package
  • Some of these events should be moved to the keeper methods as per my earlier comments:

@ValarDragon @stackman27 please let me know what you think

@p0mvn
Copy link
Member

p0mvn commented Aug 29, 2022

Hi @stackman27

Just wanted to follow up here as well. Dev and I discussed this offline.

Let's move the events to keeper please but not use the internal/events logic. Please feel free to add tests like you were doing before.

We’ll not pursue the events QA further unless asked for by integrators and wait for improvements from the SDK

Sorry for having this blocked for quite some time

@stackman27
Copy link
Contributor Author

Hi @stackman27

Just wanted to follow up here as well. Dev and I discussed this offline.

Let's move the events to keeper please but not use the internal/events logic. Please feel free to add tests like you were doing before.

We’ll not pursue the events QA further unless asked for by integrators and wait for improvements from the SDK

Sorry for having this blocked for quite some time

Gotchu! Thank you for sorting this out. I will continue adding new tests

@github-actions
Copy link
Contributor

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Thank you!

@github-actions github-actions bot added the Stale label Sep 18, 2022
@github-actions github-actions bot closed this Sep 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:app-wiring Changes to the app folder C:x/lockup Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

x/lockup: Ensure events are emitted and tests added
3 participants