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

Add chain-based event filtering #1920

Merged
merged 12 commits into from
Sep 13, 2024
Merged

Conversation

hectorgomezv
Copy link
Member

@hectorgomezv hectorgomezv commented Sep 12, 2024

Summary

This PR adds a check to the onEvent callback of HooksRepositoryWithNotifications/HooksRepository to check the chainId in the event being processed is related to a known Chain. If it isn't, a warning log is emitted, instead of letting the service execute the event handler (and fail).

Note: Ideally the unsupported events should be grouped to reduce the amount of potential logs. A forthcoming PR will take care of the grouping.

Changes

  • Adds isSupportedChain to IChainsRepository (and its memorization wrapper isSupportedChainMemoto HooksRepositoryWithNotifications/HooksRepository). This function checks the validity of the chain in the event being processed.
  • Adds the related tests.

@hectorgomezv hectorgomezv requested a review from a team as a code owner September 12, 2024 17:52
@hectorgomezv hectorgomezv linked an issue Sep 12, 2024 that may be closed by this pull request
@hectorgomezv hectorgomezv self-assigned this Sep 12, 2024
@hectorgomezv hectorgomezv changed the title Chain based event filtering Add chain-based event filtering Sep 12, 2024
@coveralls
Copy link

coveralls commented Sep 12, 2024

Pull Request Test Coverage Report for Build 10847874852

Details

  • 20 of 20 (100.0%) changed or added relevant lines in 2 files are covered.
  • 15 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.2%) to 90.633%

Files with Coverage Reduction New Missed Lines %
src/domain/hooks/helpers/event-notifications.helper.ts 1 92.5%
src/routes/transactions/mappers/common/transaction-info.mapper.ts 1 53.72%
src/domain/hooks/hooks.repository.ts 13 89.34%
Totals Coverage Status
Change from base Build 10833545297: -0.2%
Covered Lines: 8352
Relevant Lines: 8839

💛 - Coveralls

Copy link
Member

@iamacook iamacook left a comment

Choose a reason for hiding this comment

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

Out of scope but I think we should consider a way to reduce the duplication across the (non-)notification hook repositories. What do you think?

});
} else {
this.loggingService.warn({
type: 'unsupported_event_chain',
Copy link
Member

Choose a reason for hiding this comment

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

If this follows a naming convention we can leave it as is, but unsupported_chain_event reads better (also would need changing in the other service).

Copy link
Member Author

Choose a reason for hiding this comment

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

Totally agree! Changed in 789cee5

}
}

private async isSupportedChain(chainId: string): Promise<boolean> {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think this should "live" in the chains repository and we then expose a clearSupportedChain method for consistency. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved in 789cee5

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't move the memoized function to the ChainsRepository because I think it could add more complexity (other classes might not need to call a memoized function I think). So I've left the clearing of the memoized function cache responsibility in the HooksRepository for now. If memoizing makes sense in other high-load places we could think on memoizing inside the ChainsRepository (or even on other repositories), but I tend to think it is out of the scope of the PR, wdyt?

src/routes/hooks/hooks-cache.controller.spec.ts Outdated Show resolved Hide resolved
src/routes/hooks/hooks-cache.controller.spec.ts Outdated Show resolved Hide resolved
src/routes/hooks/hooks-cache.controller.spec.ts Outdated Show resolved Hide resolved
hectorgomezv and others added 3 commits September 13, 2024 11:21
Co-authored-by: Aaron Cook <aaron@safe.global>
Co-authored-by: Aaron Cook <aaron@safe.global>
Co-authored-by: Aaron Cook <aaron@safe.global>
@@ -328,6 +348,10 @@ export class HooksRepositoryWithNotifications implements IHooksRepository {
);
break;
case ConfigEventType.CHAIN_UPDATE:
// As the chains have been updated, we need to clear the memoized function cache.
if (this.isSupportedChainMemo.cache.clear) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: What do you think about creating a private method to handle this condition?
The current method is nearly 300 lines long, so splitting it could improve readability. It’s fine for now, but it seems like the entire method could benefit from being refactored.

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely! Good point, I think we could move all the code within each case to private methods to improve the overall readability. Are you OK if I do that on a separate PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

@hectorgomezv Absolutely, that's a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've created an issue to keep track of it 👍

#1926

@hectorgomezv
Copy link
Member Author

Out of scope but I think we should consider a way to reduce the duplication across the (non-)notification hook repositories. What do you think?

Absolutely, I think this suggestion from @PooyaRaki #1920 (comment) would also help, as both repositories could reference the same private functions if I'm not wrong.

@hectorgomezv hectorgomezv merged commit 6221291 into main Sep 13, 2024
18 checks passed
@hectorgomezv hectorgomezv deleted the chain-based-event-filtering branch September 13, 2024 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement a chain-based event filtering
4 participants