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

(OraklNode) refactor websocketchainreader init code #1717

Merged
merged 2 commits into from
Jul 3, 2024

Conversation

nick-bisonai
Copy link
Collaborator

Description

Use functional options pattern for initialization
return error only when both eth websocket url and kaia websocket url are not provided

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist before requesting a review

  • I have performed a self-review of my code.
  • If it is a core feature, I have added thorough tests.

Deployment

  • Should publish npm package
  • Should publish Docker image

@nick-bisonai nick-bisonai self-assigned this Jul 1, 2024
@nick-bisonai nick-bisonai requested a review from a team as a code owner July 1, 2024 09:02
Copy link
Contributor

coderabbitai bot commented Jul 1, 2024

Walkthrough

Walkthrough

The recent changes introduce a ChainReaderConfig struct and ChainReaderOption methods to the WebSocket chain reader. This enhances flexibility in configuring the ChainReader, allowing options like KaiaWebsocketUrl, EthWebsocketUrl, and RetryInterval to be provided as arguments. The changes also refactor the New function for easier initialization of the ChainReader with these options.

Changes

File(s) Change Summary
node/pkg/chain/websocketchainreader/type.go Introduced ChainReaderConfig struct and ChainReaderOption functions to set KaiaWebsocketUrl, EthWebsocketUrl, and RetryInterval.
node/pkg/chain/websocketchainreader/websocketreader.go Refactored New function to accept variadic ChainReaderOption arguments instead of explicit websocket URLs.
node/pkg/error/sentinel.go Removed ErrChainKaiaWebsocketUrlNotFound and ErrChainEthWebsocketUrlNotFound, added ErrChainWebsocketUrlNotProvided.
node/pkg/websocketfetcher/app.go Modified App struct's initializeDex method to initialize chainReader using ChainReaderOption for setting EthWebsocketUrl and KaiaWebsocketUrl.
node/script/test_websocketchainreader/main.go Changed initialization of chainReader in main function to use builder pattern, passing websocket URLs as options.

Poem

In fields of code, new methods bloom,
With webs of options, they remove the gloom.
Configs are set with graceful care,
Flexibility now dances in the air.
Blocks and chains, they now entwine,
A Rabbit's code, by pure design. 🐇✨


Tip

Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI gpt-4o model for code reviews and chat: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.
  • OSS projects are currently opted into early access features by default.

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 Configration 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
Contributor

@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 67fb2bd and 146fec2.

Files selected for processing (6)
  • node/pkg/chain/websocketchainreader/type.go (1 hunks)
  • node/pkg/chain/websocketchainreader/websocketreader.go (1 hunks)
  • node/pkg/error/sentinel.go (1 hunks)
  • node/pkg/websocketfetcher/app.go (1 hunks)
  • node/script/test_websocketDexFetcher/main.go (1 hunks)
  • node/script/test_websocketchainreader/main.go (1 hunks)
Files not summarized due to errors (3)
  • node/pkg/chain/websocketchainreader/type.go: Error: Server error. Please try again later.
  • node/script/test_websocketchainreader/main.go: Error: Server error. Please try again later.
  • node/script/test_websocketDexFetcher/main.go: Error: Server error. Please try again later.
Additional comments not posted (7)
node/pkg/chain/websocketchainreader/type.go (2)

18-22: New ChainReaderConfig struct definition is well-designed.

The ChainReaderConfig struct is clear and concise, providing a straightforward way to configure the ChainReader. It includes URLs for both Ethereum and Kaia chains, as well as a retry interval which is a good practice for network-related operations.


24-42: Functional options pattern implemented effectively.

The functional options pattern is a good choice for configuration flexibility. Each option function (WithKaiaWebsocketUrl, WithEthWebsocketUrl, WithRetryInterval) is well-implemented, correctly modifying the ChainReaderConfig struct.

node/script/test_websocketchainreader/main.go (1)

30-33: Correct implementation of ChainReader initialization using functional options.

The initialization of ChainReader using WithEthWebsocketUrl and WithKaiaWebsocketUrl demonstrates the flexibility of the functional options pattern. The error handling following the initialization is also correctly implemented, ensuring that the script terminates if the ChainReader cannot be created.

node/script/test_websocketDexFetcher/main.go (1)

107-110: Proper use of ChainReader initialization in a data-fetching context.

The script correctly initializes the ChainReader with both Ethereum and Kaia WebSocket URLs using the functional options pattern. The subsequent data fetching logic is dependent on the correct initialization, which is handled well here.

node/pkg/chain/websocketchainreader/websocketreader.go (1)

18-62: Refactored New function correctly implements error handling and client initialization.

The refactored New function now correctly initializes Ethereum and Kaia clients based on the provided URLs and handles errors appropriately. The use of a consolidated error for missing URLs (ErrChainWebsocketUrlNotProvided) simplifies the error handling logic.

node/pkg/websocketfetcher/app.go (1)

197-199: Refactored initialization using functional options is clean and modular.

The updated initialization of chainReader using the functional options pattern (WithEthWebsocketUrl and WithKaiaWebsocketUrl) is a good use of modern Go practices for configuration. This approach enhances readability and flexibility in setting up the chainReader.

However, ensure that the environment variables KAIA_WEBSOCKET_URL and ETH_WEBSOCKET_URL are always set before deployment as the application will fail to start without them.

node/pkg/error/sentinel.go (1)

124-124: Consolidated error for WebSocket URL enhances maintainability.

Replacing specific errors for missing WebSocket URLs with a generic ErrChainWebsocketUrlNotProvided error simplifies the error handling logic and reduces potential redundancy. This change is consistent with the PR's objective to streamline error handling.

Copy link
Contributor

@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 67fb2bd and 146fec2.

Files selected for processing (6)
  • node/pkg/chain/websocketchainreader/type.go (1 hunks)
  • node/pkg/chain/websocketchainreader/websocketreader.go (1 hunks)
  • node/pkg/error/sentinel.go (1 hunks)
  • node/pkg/websocketfetcher/app.go (1 hunks)
  • node/script/test_websocketDexFetcher/main.go (1 hunks)
  • node/script/test_websocketchainreader/main.go (1 hunks)
Files not summarized due to errors (1)
  • node/script/test_websocketDexFetcher/main.go: Error: Server error. Please try again later.
Additional comments not posted (9)
node/pkg/chain/websocketchainreader/type.go (2)

18-22: Configuration struct is well-defined.

The ChainReaderConfig struct is properly defined and aligns with the PR objectives to allow flexible initialization of ChainReader. This struct now contains fields for both websocket URLs and a retry interval, providing clear configuration points for the ChainReader.


26-30: Functional options are correctly implemented.

The functional options WithKaiaWebsocketUrl, WithEthWebsocketUrl, and WithRetryInterval are correctly implemented. They modify the ChainReaderConfig struct as expected, allowing for flexible and dynamic configuration of the ChainReader.

Also applies to: 32-36, 38-42

node/script/test_websocketchainreader/main.go (1)

30-33: Correct usage of functional options in ChainReader initialization.

The ChainReader is initialized using the functional options pattern correctly, which matches the changes made in the type.go file. This ensures that the ChainReader can be flexibly configured based on environment variables.

node/script/test_websocketDexFetcher/main.go (1)

107-110: Functional options are correctly used for ChainReader initialization.

The initialization of ChainReader using the functional options pattern is correctly implemented here as well. This allows the script to dynamically configure the ChainReader based on provided environment variables, aligning with the changes in the type.go file.

node/pkg/chain/websocketchainreader/websocketreader.go (2)

18-62: Refactored New function correctly implements functional options and handles errors appropriately.

The New function has been refactored to use functional options, which is correctly implemented. The error handling is robust, checking for the absence of both websocket URLs and returning a specific error if both are missing, which aligns with the PR description.


Line range hint 65-148: Subscription handling and error management are well-implemented.

The Subscribe method and related subscription handling logic are correctly implemented. The method uses a configuration struct and functional options pattern for flexible subscription setup. Error handling is thorough, with specific errors returned for missing configuration details, and retry logic is in place for handling failures.

node/pkg/websocketfetcher/app.go (2)

197-199: Ensure both websocket URLs are provided.

The current implementation checks for both kaiaWebsocketUrl and ethWebsocketUrl to be set. This is correct as per the new error handling logic.


199-199: LGTM!

The use of functional options pattern for websocketchainreader.New is correct and improves the code flexibility.

node/pkg/error/sentinel.go (1)

124-124: LGTM!

The new error ErrChainWebsocketUrlNotProvided is correctly added to handle the case where both websocket URLs are not provided.

node/pkg/websocketfetcher/app.go Show resolved Hide resolved
Copy link
Contributor

@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 146fec2 and 64d2964.

Files selected for processing (1)
  • node/pkg/chain/websocketchainreader/websocketreader.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • node/pkg/chain/websocketchainreader/websocketreader.go

Copy link
Contributor

@Intizar-T Intizar-T left a comment

Choose a reason for hiding this comment

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

lgtm!

@nick-bisonai nick-bisonai merged commit 4285929 into master Jul 3, 2024
1 check passed
@nick-bisonai nick-bisonai deleted the feat/refactor-websocketchainreader branch July 3, 2024 04:30
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.

2 participants