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

[Code Health] refactor: simplify protocol.CountHashDifficultyBits() #656

Merged
merged 23 commits into from
Jul 6, 2024

Conversation

bryanchriswhite
Copy link
Contributor

@bryanchriswhite bryanchriswhite commented Jul 4, 2024

Summary

Simplifies protocol.CountHashDifficultyBits() and eliminates MustCountHashDifficultyBits().

Issue

Type of change

Select one or more:

  • New feature, functionality or library
  • Bug fix
  • Code health or cleanup
  • Documentation
  • Other (specify)

Testing

Documentation changes (only if making doc changes)

  • make docusaurus_start; only needed if you make doc changes

Local Testing (only if making code changes)

  • Unit Tests: make go_develop_and_test
  • LocalNet E2E Tests: make test_e2e
  • See quickstart guide for instructions

PR Testing (only if making code changes)

  • DevNet E2E Tests: Add the devnet-test-e2e label to the PR.
    • THIS IS VERY EXPENSIVE, so only do it after all the reviews are complete.
    • Optionally run make trigger_ci if you want to re-trigger tests without any code changes
    • If tests fail, try re-running failed tests only using the GitHub UI as shown here

Sanity Checklist

  • I have tested my changes using the available tooling
  • I have commented my code
  • I have performed a self-review of my own code; both comments & source code
  • I create and reference any new tickets, if applicable
  • I have left TODOs throughout the codebase, if applicable

Summary by CodeRabbit

  • New Features

    • Introduced CountHashDifficultyBits function to calculate leading zero bits in a hash.
  • Refactor

    • Updated various components to use the new CountHashDifficultyBits for difficulty calculations instead of the previous method.
    • Changed import paths for protocol to use the new subpackage crypto/protocol.
  • Tests

    • Added test cases for the CountHashDifficultyBits function to ensure accuracy in difficulty bit counting.

@bryanchriswhite bryanchriswhite added the code health Cleans up some code label Jul 4, 2024
@bryanchriswhite bryanchriswhite self-assigned this Jul 4, 2024
Copy link

coderabbitai bot commented Jul 4, 2024

Walkthrough

The updates introduce a new function CountHashDifficultyBits in the protocol package to calculate leading zero bits in a byte slice. Several files are adjusted to utilize this function by modifying import paths and replacing previous implementations. Errors are streamlined, reducing redundancy and enhancing maintainability. Test coverage is added to verify the new function's accuracy.

Changes

Files/Paths Change Summary
pkg/crypto/protocol/difficulty.go Introduced CountHashDifficultyBits to calculate the number of leading zero bits in a byte slice.
pkg/crypto/protocol/difficulty_test.go Added TestCountDifficultyBits to test the new CountHashDifficultyBits function.
pkg/crypto/protocol/errors.go Introduced codespace global variable initialized to "crypto/protocol".
pkg/relayer/miner/gen/gen_fixtures.go, pkg/relayer/miner/miner.go Updated import paths and usage to replace MustCountDifficultyBits with CountHashDifficultyBits.
x/proof/keeper/msg_server_submit_proof.go, .../msg_server_submit_proof_test.go Modified import paths and direct assignment of relay difficulty bits; updated test functions accordingly.

Sequence Diagram(s)

Sequence diagrams are not included as the changes are straightforward and function-focused without significant modifications to the control flow.

Poem

In the land where bytes align,
A function counts the zeros fine.
Imports shifted, paths refined,
In crypto's realm, new code designed.
Tests ensure precision's twine,
Mining's easier, spirits shine.
🐇✨


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.

@bryanchriswhite bryanchriswhite added push-image CI related - pushes images to ghcr.io devnet-test-e2e labels Jul 4, 2024
@bryanchriswhite bryanchriswhite marked this pull request as ready for review July 4, 2024 10:57
Copy link

github-actions bot commented Jul 4, 2024

The image is going to be pushed after the next commit.

You can use make trigger_ci to push an empty commit.

If you also want to run E2E tests, please add devnet-test-e2e label.

Copy link

github-actions bot commented Jul 4, 2024

The CI will now also run the e2e tests on devnet, which increases the time it takes to complete all CI checks.

You may need to run make trigger_ci to submit an empty commit that'll trigger the tests.

GCP workloads (requires changing the namespace to 656)
Grafana network dashboard for devnet-issue-{issue-id}

@github-actions github-actions bot added the devnet label Jul 4, 2024
@bryanchriswhite bryanchriswhite changed the title refactor: simplify protocol.CountHashDifficultyBits() [Code Health] refactor: simplify protocol.CountHashDifficultyBits() Jul 4, 2024
@bryanchriswhite bryanchriswhite changed the title [Code Health] refactor: simplify protocol.CountHashDifficultyBits() [Code Health] refactor: simplify protocol.CountHashDifficultyBits() Jul 4, 2024

// CountHashDifficultyBits returns the number of leading zero bits in the given byte
// slice. It returns an error if the byte slice is all zero bits.
func CountHashDifficultyBits(bz [32]byte) int {
Copy link
Member

Choose a reason for hiding this comment

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

cc @red-0ne who I believe has similar changes elsewhere.

pkg/crypto/protocol/difficulty.go Show resolved Hide resolved
@bryanchriswhite bryanchriswhite changed the base branch from issues/584/refactor/miner-query-params to main July 5, 2024 21:27
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

Outside diff range and nitpick comments (1)
pkg/crypto/protocol/difficulty.go (1)

8-8: Improve documentation clarity.

The documentation is clear, but it can be improved by explaining why BigEndian is chosen for bit/byte ordering.

- // CountHashDifficultyBits returns the number of leading zero bits in the given byte slice.
+ // CountHashDifficultyBits returns the number of leading zero bits in the given byte slice.
+ // BigEndian is chosen for bit/byte ordering to ensure that leading zeros accumulate across adjacent bytes.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e119984 and e6dfda7.

Files selected for processing (7)
  • pkg/crypto/protocol/difficulty.go (1 hunks)
  • pkg/crypto/protocol/difficulty_test.go (1 hunks)
  • pkg/crypto/protocol/errors.go (1 hunks)
  • pkg/relayer/miner/gen/gen_fixtures.go (2 hunks)
  • pkg/relayer/miner/miner.go (2 hunks)
  • x/proof/keeper/msg_server_submit_proof.go (2 hunks)
  • x/proof/keeper/msg_server_submit_proof_test.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • pkg/crypto/protocol/errors.go
Additional comments not posted (7)
pkg/crypto/protocol/difficulty.go (1)

9-15: LGTM!

The function implementation is correct and efficient.

pkg/crypto/protocol/difficulty_test.go (1)

12-50: LGTM!

The test cases are comprehensive and cover various scenarios for the CountHashDifficultyBits function.

pkg/relayer/miner/miner.go (1)

115-126: LGTM!

The changes are correct and consistent. The function mapMineRelay uses CountHashDifficultyBits correctly.

pkg/relayer/miner/gen/gen_fixtures.go (2)

203-203: LGTM!

The changes are correct and consistent. The function difficultyGTE uses CountHashDifficultyBits correctly.


209-209: LGTM!

The changes are correct and consistent. The function difficultyLT uses CountHashDifficultyBits correctly.

x/proof/keeper/msg_server_submit_proof.go (1)

479-479: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to validateMiningDifficulty match the new implementation.

Verification successful

Function usage verified.

The function validateMiningDifficulty is consistently used with the new implementation in x/proof/keeper/msg_server_submit_proof.go.

  • x/proof/keeper/msg_server_submit_proof.go: Verified usage of validateMiningDifficulty at lines 479 and 483.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `validateMiningDifficulty` match the new implementation.

# Test: Search for the function usage. Expect: Only occurrences of the new implementation.
rg --type go $'validateMiningDifficulty'

Length of output: 551


Script:

#!/bin/bash
# Extract the definition and usage instances of `validateMiningDifficulty` in `x/proof/keeper/msg_server_submit_proof.go`

# Extract the function definition
echo "Function Definition:"
rg --type go --context 10 'func validateMiningDifficulty' x/proof/keeper/msg_server_submit_proof.go

# Extract the function usage instances
echo "Function Usage Instances:"
rg --type go --context 5 'validateMiningDifficulty(relayBz, params.MinRelayDifficultyBits)' x/proof/keeper/msg_server_submit_proof.go

Length of output: 1247


Script:

#!/bin/bash
# Extract the function usage instances of `validateMiningDifficulty` in `x/proof/keeper/msg_server_submit_proof.go`

# Extract the function usage instances
rg --type go --context 5 'validateMiningDifficulty' x/proof/keeper/msg_server_submit_proof.go

Length of output: 1592

x/proof/keeper/msg_server_submit_proof_test.go (1)

1397-1397: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to getClosestRelayDifficultyBits match the new implementation.

Verification successful

Verified: The function getClosestRelayDifficultyBits is only used within the test file x/proof/keeper/msg_server_submit_proof_test.go and matches the new implementation.

  • x/proof/keeper/msg_server_submit_proof_test.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `getClosestRelayDifficultyBits` match the new implementation.

# Test: Search for the function usage. Expect: Only occurrences of the new implementation.
rg --type go $'getClosestRelayDifficultyBits'

Length of output: 411

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 e6dfda7 and 00ab72b.

Files selected for processing (1)
  • pkg/crypto/protocol/difficulty.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pkg/crypto/protocol/difficulty.go

@bryanchriswhite
Copy link
Contributor Author

E2E tests passing locally but failed to run in CI:
image

@bryanchriswhite bryanchriswhite merged commit bdbc365 into main Jul 6, 2024
9 of 10 checks passed
okdas pushed a commit that referenced this pull request Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code health Cleans up some code devnet devnet-test-e2e push-image CI related - pushes images to ghcr.io
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

2 participants