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

fix: utils - change way to compute selector hash #250

Merged
merged 2 commits into from
Aug 7, 2024

Conversation

MartianGreed
Copy link
Collaborator

@MartianGreed MartianGreed commented Aug 6, 2024

Closes #247

Introduced changes

Changes way to get component from event hash.

This will not fix the recs issue about Map receiver.

Checklist

  • Linked relevant issue
  • Added relevant tests
  • Performed self-review of the code

Summary by CodeRabbit

  • New Features

    • Introduced enhanced logging and error handling in entity management functions.
    • Added a new test suite using Vitest for utility functions to ensure reliability.
    • Implemented new constants and functions for improved event filtering and component handling.
  • Bug Fixes

    • Refined error handling in component setting logic to prevent issues during execution.
  • Chores

    • Updated the testing framework from Jest to Vitest, enhancing testing capabilities.
    • Added coverage reporting for utility functions to improve quality assurance.

Copy link

coderabbitai bot commented Aug 6, 2024

Warning

Rate limit exceeded

@MartianGreed has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 0 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 8f0d4be and ead2a2d.

Walkthrough

The recent changes enhance the functionality and maintainability of the Dojo.js codebase, focusing on improved logging, error handling, and testing capabilities. Key modifications include refining entity management functions, explicitly defining types for better clarity, and migrating the testing framework to Vitest, which streamlines coverage reporting and strengthens overall reliability.

Changes

Files Change Summary
packages/state/src/recs/index.ts Improved logging and refined error handling in entity functions.
packages/state/src/utils/index.ts Defined return type for convertValues and enhanced code readability.
packages/utils/coverage/.tmp/... Introduced JSON file for test coverage results.
packages/utils/package.json Updated testing framework to Vitest and added coverage reporting scripts.
packages/utils/src/_test_/utils/... New unit tests for utility functions using Vitest.
packages/utils/src/utils/index.ts Added constants and refactored functions for better event handling.

Assessment against linked issues

Objective Addressed Explanation
Components should always be in sync with Torii (##247) Changes improve logging and error handling but do not directly resolve sync issues.

Poem

🐰 In the code where rabbits hop,
Improvements made, we can’t stop!
Logs now sing with every call,
Error handling stands tall.
A testing shift, so bright and new,
Hooray for changes, cheers to you! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Inline review comments failed to post

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 988730c and 7bac4a7.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (6)
  • packages/state/src/recs/index.ts (2 hunks)
  • packages/state/src/utils/index.ts (1 hunks)
  • packages/utils/coverage/.tmp/coverage-0.json (1 hunks)
  • packages/utils/package.json (2 hunks)
  • packages/utils/src/test/utils/index.test.ts (1 hunks)
  • packages/utils/src/utils/index.ts (5 hunks)
Additional context used
Biome
packages/state/src/recs/index.ts

[error] 189-189: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)


[error] 194-194: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)

Additional comments not posted (22)
packages/utils/package.json (2)

11-12: Approved: Update testing framework to Vitest.

Switching to Vitest for testing and adding a coverage script enhances the testing infrastructure.


23-25: Approved: Add Vitest and coverage dependencies.

Adding @vitest/coverage-v8 and vitest is necessary for the new testing framework and coverage functionality.

packages/utils/coverage/.tmp/coverage-0.json (1)

1-38: Approved: Coverage report generated by Vitest.

The coverage report contains valid JSON and relevant coverage information.

packages/state/src/utils/index.ts (3)

1-1: Approved: Import necessary types.

The import statements are correct and necessary for the function.


3-3: Approved: Add explicit return type.

Adding an explicit return type ComponentValue enhances type safety and clarity.


4-82: Approved: Improve readability and maintainability.

The function logic remains consistent with the previous implementation, with improvements in readability and maintainability.

packages/utils/src/_test_/utils/index.test.ts (5)

11-33: Good coverage for getComponentNameFromEvent.

The tests cover multiple scenarios for getComponentNameFromEvent. Ensure that edge cases, such as empty arrays or invalid data, are also tested.


53-71: Good exception handling test for getComponentNameFromEvent.

The test correctly verifies that an exception is thrown when the action is not found. Consider adding more edge cases to ensure robustness.


73-81: Good coverage for splitEventTag.

The tests cover multiple scenarios for splitEventTag. Ensure that edge cases, such as empty strings or strings without separators, are also tested.


83-88: Good coverage for computeByteArrayHash.

The test correctly verifies the hash computation. Ensure that edge cases, such as empty strings or very large strings, are also tested.


90-105: Good coverage for getSelectorFromTag.

The tests cover multiple scenarios for getSelectorFromTag. Ensure that edge cases, such as empty strings or invalid formats, are also tested.

packages/state/src/recs/index.ts (2)

74-74: Approve logging statement in getEntities.

The logging statement enhances traceability. Ensure that no sensitive information is logged.


202-215: Approve error handling in setEntities.

The error handling around setComponent is robust and provides useful feedback for developers.

packages/utils/src/utils/index.ts (9)

Line range hint 13-27: Approve getEvents function.

The filtering logic is straightforward and correct. Ensure that the criteria cover all relevant scenarios.


Line range hint 30-35: Approve setComponentsFromEvents function.

The iteration and update logic is correct. Ensure that the function handles edge cases and errors gracefully.


52-92: Approve setComponentFromEvent function.

The update logic and error handling are robust. Ensure that the function handles edge cases and errors gracefully.


95-102: Approve getComponentNames function.

The extraction logic is correct. Ensure that the function handles edge cases and errors gracefully.


Line range hint 103-115: Approve parseComponentValue function.

The parsing logic is correct. Ensure that the function handles edge cases and errors gracefully.


Line range hint 116-122: Approve decodeComponent function.

The decoding logic is correct. Ensure that the function handles edge cases and errors gracefully.


185-191: Approve hexToAscii function.

The conversion logic is correct. Ensure that the function handles edge cases and errors gracefully.


196-215: Approve getComponentNameFromEvent function.

The retrieval logic is correct. Ensure that the function handles edge cases and errors gracefully.


224-230: Approve getSelectorFromTag function.

The computation logic is correct. Ensure that the function handles edge cases and errors gracefully.

Comments failed to post (2)
packages/state/src/recs/index.ts

194-195: Replace hasOwnProperty with Object.hasOwn().

It's recommended to use Object.hasOwn() instead of hasOwnProperty() to avoid potential issues with prototype chain.

-  if (!entities[key].hasOwnProperty(componentName)) {
+  if (!Object.hasOwn(entities[key], componentName)) {
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

            if (!Object.hasOwn(entities[key], componentName)) {
                continue;
Tools
Biome

[error] 194-194: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)


189-190: Replace hasOwnProperty with Object.hasOwn().

It's recommended to use Object.hasOwn() instead of hasOwnProperty() to avoid potential issues with prototype chain.

-  if (!entities.hasOwnProperty(key)) {
+  if (!Object.hasOwn(entities, key)) {
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

        if (!Object.hasOwn(entities, key)) {
            continue;
Tools
Biome

[error] 189-189: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7bac4a7 and 84d936d.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (6)
  • packages/state/src/recs/index.ts (2 hunks)
  • packages/state/src/utils/index.ts (1 hunks)
  • packages/utils/coverage/.tmp/coverage-0.json (1 hunks)
  • packages/utils/package.json (2 hunks)
  • packages/utils/src/test/utils/index.test.ts (1 hunks)
  • packages/utils/src/utils/index.ts (5 hunks)
Files skipped from review due to trivial changes (1)
  • packages/utils/coverage/.tmp/coverage-0.json
Files skipped from review as they are similar to previous changes (3)
  • packages/state/src/utils/index.ts
  • packages/utils/package.json
  • packages/utils/src/test/utils/index.test.ts
Additional context used
Biome
packages/state/src/recs/index.ts

[error] 189-189: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)


[error] 194-194: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)

Additional comments not posted (9)
packages/state/src/recs/index.ts (4)

74-74: Logging added for fetched entities and components.

The added logging statement enhances traceability and aids in debugging. Ensure that the logged data does not contain sensitive information.


189-191: Refined logic for handling entity properties.

The inverted check for property existence improves readability.

Tools
Biome

[error] 189-189: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)


194-196: Refined logic for handling component properties.

The inverted check for property existence improves readability.

Tools
Biome

[error] 194-194: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)


201-215: Enhanced error handling around setComponent.

The added try-catch block improves robustness by preventing the entire function from failing due to a single error and provides useful feedback for developers.

packages/utils/src/utils/index.ts (5)

13-14: Use of constant STORE_SET_RECORD_EVENT_NAME.

The use of a constant improves maintainability by avoiding hardcoded strings.

Also applies to: 27-27


52-92: Refactored logic for better error handling and clarity.

The refactored logic and the try-catch block around getComponentNameFromEvent improve robustness and readability.


185-191: New function hexToAscii added.

The function converts a hexadecimal string to an ASCII string. Ensure that the function handles edge cases and invalid input gracefully.


196-216: New function getComponentNameFromEvent added.

The function retrieves the component name from an event. The use of a map for action-felt pairs improves efficiency. Ensure that the function handles cases where the action is not found.


224-231: New function getSelectorFromTag added.

The function computes a selector from a namespace and an event name. The use of poseidonHashMany for hashing improves security. Ensure that the function handles invalid input gracefully.

Comment on lines 189 to 194
if (!entities.hasOwnProperty(key)) {
continue;
}

for (let componentName in entities[key]) {
if (!entities[key].hasOwnProperty(componentName)) {
Copy link

Choose a reason for hiding this comment

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

Use Object.hasOwn() instead of Object.prototype.hasOwnProperty.

It's recommended to use Object.hasOwn() for checking property existence.

- if (!entities.hasOwnProperty(key)) {
+ if (!Object.hasOwn(entities, key)) {
- if (!entities[key].hasOwnProperty(componentName)) {
+ if (!Object.hasOwn(entities[key], componentName)) {
Tools
Biome

[error] 189-189: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)


[error] 194-194: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)

@MartianGreed MartianGreed changed the title fix(utils): change way to compute selector hash fix: utils - change way to compute selector hash Aug 7, 2024
@@ -71,6 +71,7 @@ export const getEntities = async <S extends Schema>(

while (continueFetching) {
const entities = await client.getAllEntities(limit, cursor);
console.log(entities, components);
Copy link
Contributor

Choose a reason for hiding this comment

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

we should remove this

@ponderingdemocritus
Copy link
Contributor

Looks good other than that, nice iteration

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 84d936d and 8f0d4be.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (6)
  • packages/state/src/recs/index.ts (1 hunks)
  • packages/state/src/utils/index.ts (1 hunks)
  • packages/utils/coverage/.tmp/coverage-0.json (1 hunks)
  • packages/utils/package.json (2 hunks)
  • packages/utils/src/test/utils/index.test.ts (1 hunks)
  • packages/utils/src/utils/index.ts (5 hunks)
Files skipped from review due to trivial changes (1)
  • packages/utils/coverage/.tmp/coverage-0.json
Files skipped from review as they are similar to previous changes (2)
  • packages/state/src/utils/index.ts
  • packages/utils/src/test/utils/index.test.ts
Additional comments not posted (17)
packages/utils/package.json (3)

11-12: Transition to Vitest for testing.

The test script has been updated to use Vitest instead of Jest. This change is correct and aligns with the objective of improving testing capabilities.


12-12: Addition of coverage reporting script.

A new script for coverage reporting using Vitest has been added. This enhances the project's testing capabilities.


24-25: Addition of Vitest dependencies.

New dependencies for Vitest and coverage reporting have been added. This is necessary for the transition to the new testing framework.

packages/state/src/recs/index.ts (3)

188-189: Use Object.hasOwn() instead of Object.prototype.hasOwnProperty.

It's recommended to use Object.hasOwn() for checking property existence. This change improves code readability and reduces potential errors.


193-194: Use Object.hasOwn() instead of Object.prototype.hasOwnProperty.

It's recommended to use Object.hasOwn() for checking property existence. This change improves code readability and reduces potential errors.


200-214: Improved error handling in setComponent call.

The try-catch block around the setComponent call ensures that errors are caught and logged, preventing the entire function from failing due to a single issue. This change improves the robustness and maintainability of the function.

packages/utils/src/utils/index.ts (11)

13-14: Addition of STORE_SET_RECORD_EVENT_NAME constant.

The addition of this constant improves code maintainability by replacing a hardcoded string.


27-27: Use of STORE_SET_RECORD_EVENT_NAME in getEvents function.

The use of the constant STORE_SET_RECORD_EVENT_NAME in the getEvents function enhances clarity and reduces potential errors from string literals.


52-93: Refactored setComponentFromEvent function.

The logic in the setComponentFromEvent function has been refactored for better error handling and clarity. The retrieval of the component name is now encapsulated in a new function, getComponentNameFromEvent, which modularizes the code and adds robustness by throwing an error if the action is not found.


95-102: Addition of getComponentNames helper function.

This helper function extracts component names from components, improving code readability and modularity.


185-186: Addition of hexToAscii function.

This utility function converts a hexadecimal string to an ASCII string, adding useful functionality for handling hex strings.


193-216: Addition of getComponentNameFromEvent function.

This function retrieves the component name from an event, improving code modularity and error handling by throwing an error if the action is not found.


218-221: Addition of toHexString helper function.

This helper function encodes a big number to a formatted hex string, adding useful functionality for handling big numbers.


223-231: Addition of getSelectorFromTag function.

This function computes a dojo selector from a namespace and event name, adding useful functionality for generating selectors.


233-242: Addition of serializeByteArray function.

This function serializes a ByteArray to a bigint array, adding useful functionality for handling byte arrays.


245-248: Addition of computeByteArrayHash function.

This function computes the Poseidon hash of a string represented as a ByteArray, adding useful functionality for generating hashes.


250-253: Addition of splitEventTag function.

This function splits a selector name into a namespace and event name, adding useful functionality for handling event tags.

@ponderingdemocritus ponderingdemocritus merged commit 5f81899 into dojoengine:main Aug 7, 2024
3 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Sep 16, 2024
4 tasks
This was referenced Oct 3, 2024
@coderabbitai coderabbitai bot mentioned this pull request Oct 12, 2024
@coderabbitai coderabbitai bot mentioned this pull request Nov 1, 2024
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.

[Bug]: Client not in sync with torii grpc
2 participants