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

optimization constant time #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

deepface19
Copy link

@deepface19 deepface19 commented Mar 21, 2024

optimization constant time.

make public constant time.

Summary by CodeRabbit

  • New Features
    • Introduced a new constant to improve the computation of timeoutTimestamp in specific functions, enhancing the efficiency of operations.

Copy link
Contributor

coderabbitai bot commented Mar 21, 2024

Walkthrough

The update introduces a new constant CONST_TIME with a value of 1e9 in both XCounter.sol and XCounterUC.sol contracts. This constant is now used to calculate the timeoutTimestamp within the sendPacket and sendCounter functions, streamlining the process by replacing previous dynamic multiplications with this fixed value.

Changes

Files Changes
contracts/XCounter.sol,
contracts/XCounterUC.sol
Introduced CONST_TIME (uint64) with value 1e9 for timeoutTimestamp calculation in sendPacket and sendCounter.

🐇🎉
To the code we hop and make our mark,
With CONST_TIME shining, bright and stark.
No more dynamic woes, we're setting the pace,
In XCounter lands, with elegance and grace.
So here's to changes, both big and small,
May our code run swift - bugs, none at all!
🥕🚀

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-tests 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 tests 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 tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 704dcc3 and af43315.
Files selected for processing (2)
  • contracts/XCounter.sol (2 hunks)
  • contracts/XCounterUC.sol (2 hunks)
Additional comments: 2
contracts/XCounter.sol (1)
  • 12-12: The introduction of CONST_TIME as an immutable uint64 is a good practice for gas optimization. The choice of type and the immutable keyword are appropriate for constants that do not change.
contracts/XCounterUC.sol (1)
  • 12-12: The introduction of CONST_TIME as an immutable uint64 is a good practice for gas optimization. The choice of type and the immutable keyword are appropriate for constants that do not change.

@@ -37,7 +38,7 @@ contract XCounter is CustomChanIbcApp {
bytes memory payload = abi.encode(msg.sender);

// setting the timeout timestamp at 10h from now
uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * 1000000000);
uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * CONST_TIME);
Copy link
Contributor

Choose a reason for hiding this comment

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

The calculation of timeoutTimestamp seems to misunderstand the purpose of CONST_TIME. Multiplying block.timestamp + timeoutSeconds by CONST_TIME does not align with the typical use of a timeout timestamp. If CONST_TIME is meant to represent a constant duration, it should likely be added to, not multiplied by, the current timestamp and timeout period.

- uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * CONST_TIME);
+ uint64 timeoutTimestamp = uint64(block.timestamp + (timeoutSeconds * CONST_TIME));

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.

Suggested change
uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * CONST_TIME);
uint64 timeoutTimestamp = uint64(block.timestamp + (timeoutSeconds * CONST_TIME));

@@ -32,7 +34,7 @@ contract XCounterUC is UniversalChanIbcApp {
increment();
bytes memory payload = abi.encode(msg.sender, counter);

uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * 1000000000);
uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * CONST_TIME);
Copy link
Contributor

Choose a reason for hiding this comment

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

The calculation of timeoutTimestamp seems to misunderstand the purpose of CONST_TIME. Multiplying block.timestamp + timeoutSeconds by CONST_TIME does not align with the typical use of a timeout timestamp. If CONST_TIME is meant to represent a constant duration, it should likely be added to, not multiplied by, the current timestamp and timeout period.

- uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * CONST_TIME);
+ uint64 timeoutTimestamp = uint64(block.timestamp + (timeoutSeconds * CONST_TIME));

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.

Suggested change
uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * CONST_TIME);
uint64 timeoutTimestamp = uint64(block.timestamp + (timeoutSeconds * CONST_TIME));

Copy link
Contributor

@tmsdkeys tmsdkeys left a comment

Choose a reason for hiding this comment

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

Hey @deepface19 thanks for your contribution. I understand the idea to set some constant for conversion. However, if we were to optimize I wonder if this is the place to do it.

I'm thinking it could be better instead to add a helper function to convert timeout in seconds to its nanoseconds value in the vibc-core-smart-contracts repo here.

I'll discuss with the team

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