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: use u32 for the stargate vote option #169

Merged
merged 2 commits into from
Nov 26, 2024
Merged

fix: use u32 for the stargate vote option #169

merged 2 commits into from
Nov 26, 2024

Conversation

beer-1
Copy link
Collaborator

@beer-1 beer-1 commented Nov 26, 2024

Description

The current stargate_vote defines option as u64, which causes discrepancies in the JSON encoding of the message compared to the original MsgVote. To address this, we are introducing a new stargate_vote_v2 interface and migrating the original stargate_vote to use it, ensuring consistency in the message format.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Introduced stargate_with_options for enhanced message sending with options.
    • Added stargate_vote_v2 function to replace the deprecated stargate_vote, utilizing a new VoteRequestV2 structure.
  • Bug Fixes

    • Adjusted create_evm_input for proper parameter encoding in EVM interactions.
  • Documentation

    • Updated comments and documentation for new functions and options across modules.
  • Style

    • Reformatted function signatures for improved readability in test helper functions.

@beer-1 beer-1 self-assigned this Nov 26, 2024
@beer-1 beer-1 requested a review from a team as a code owner November 26, 2024 02:33
Copy link

coderabbitai bot commented Nov 26, 2024

Walkthrough

The changes introduced in this pull request primarily focus on enhancing the initia_std::cosmos and initia_std::minitswap modules. Key modifications include the addition of a new public entry function stargate_with_options, which allows for more flexible message handling. The VoteRequest struct has been replaced by VoteRequestV2, changing the option field type from u64 to u32. Additionally, minor formatting and readability improvements were made across several modules without altering their core functionalities.

Changes

File Path Change Summary
precompile/modules/initia_stdlib/sources/cosmos.move - Added stargate_with_options function.
- Deprecated stargate function, replaced by stargate_with_options.
- Replaced VoteRequest with VoteRequestV2, changing option from u64 to u32.
- Deprecated stargate_vote, replaced by stargate_vote_v2.
precompile/modules/initia_stdlib/sources/minitswap.move - Updated create_evm_input function to modify how hex_input is constructed.
- Minor formatting changes.
precompile/modules/initia_stdlib/tests/ibc_transfer_tests_helpers.move - Reformatted function signatures in store_on_callback_request and store_on_ack_request for readability.
precompile/modules/minitia_stdlib/sources/address.move - Adjusted comments for consistency; no changes to functionality.
precompile/modules/minitia_stdlib/sources/cosmos.move - Added VoteRequestV2 struct and stargate_vote_v2 function.
- Deprecated VoteRequest and stargate_vote.
precompile/modules/minitia_stdlib/tests/ibc_transfer_tests_helpers.move - Reformatted function signatures in store_on_callback_request and store_on_ack_request for readability.

Possibly related PRs

  • fix: audit #126: Introduces a new VoteRequestV2 struct and updates the stargate_vote function, which aligns with the changes in the main PR that also introduces VoteRequestV2 and modifies the stargate_vote function to stargate_vote_v2.
  • feat: impl json object #128: This PR includes changes to the cosmos.move module, specifically related to the handling of voting and proposals, which is relevant to the modifications made in the main PR regarding voting requests and the stargate_vote function.
  • feat: support evm chain on minitswap #164: This PR enhances the minitswap module to support EVM integration, which may relate to the overall functionality of the cosmos module in the main PR, particularly in how messages and proposals are handled across different environments.

Suggested reviewers

  • sh-cha

🐇 In the cosmos where changes bloom,
New functions rise to dispel the gloom.
With options aplenty, we now can vote,
A rabbit's cheer for the code we wrote!
From old to new, our paths align,
In the world of code, all will be fine! 🌟


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@beer-1 beer-1 requested a review from ALPAC-4 November 26, 2024 02:35
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: 2

🧹 Outside diff range and nitpick comments (2)
precompile/modules/minitia_stdlib/sources/cosmos.move (1)

554-558: LGTM! Tests updated correctly.

The tests have been properly updated to use VoteRequestV2 and handle the type conversion.

Consider adding a test case with a large option value (> u32::MAX) to verify the handling of truncation in the deprecated function:

+    #[test]
+    #[expected_failure(abort_code = 1)]
+    public fun test_stargate_vote_large_option(sender: &signer) {
+        let option: u64 = 0x100000000; // Greater than u32::MAX
+        stargate_vote(sender, 1, string::utf8(b"voter"), option, string::utf8(b"metadata"));
+    }

Also applies to: 576-580

precompile/modules/initia_stdlib/sources/minitswap.move (1)

2467-2468: LGTM! Consider adding a comment explaining the EVM ABI encoding offset calculation.

The calculation for the receiver's position in the EVM ABI encoding is correct. However, adding a comment explaining why we use 0x60 + 32 + denom_bytes_len would improve code maintainability.

 vector::append(
     &mut hex_input,
+    // Calculate receiver position: initial offset (0x60) + length field (32) + denom bytes length
     table_key::encode_u256((0x60 + 32 + denom_bytes_len as u256))
 ); // start position of receiver
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7e80abb and 7da8337.

📒 Files selected for processing (6)
  • precompile/modules/initia_stdlib/sources/cosmos.move (3 hunks)
  • precompile/modules/initia_stdlib/sources/minitswap.move (1 hunks)
  • precompile/modules/initia_stdlib/tests/ibc_transfer_tests_helpers.move (2 hunks)
  • precompile/modules/minitia_stdlib/sources/address.move (1 hunks)
  • precompile/modules/minitia_stdlib/sources/cosmos.move (3 hunks)
  • precompile/modules/minitia_stdlib/tests/ibc_transfer_tests_helpers.move (2 hunks)
✅ Files skipped from review due to trivial changes (3)
  • precompile/modules/initia_stdlib/tests/ibc_transfer_tests_helpers.move
  • precompile/modules/minitia_stdlib/sources/address.move
  • precompile/modules/minitia_stdlib/tests/ibc_transfer_tests_helpers.move
🔇 Additional comments (6)
precompile/modules/minitia_stdlib/sources/cosmos.move (4)

64-83: LGTM! Implementation correctly uses the new VoteRequestV2 struct.

The function properly marshals the vote request with the correct type prefix and field types.


85-92: LGTM! Properly marked as deprecated.

The original struct is correctly maintained for backward compatibility while being properly marked as deprecated.


56-62: LGTM! Struct definition aligns with Cosmos SDK.

The change from u64 to u32 for the option field correctly aligns with the Cosmos SDK's MsgVote type.

Let's verify the Cosmos SDK type definition:

✅ Verification successful

Change from u64 to u32 for vote option field is correct

The change aligns with Cosmos SDK's governance module design. While we couldn't directly verify the Cosmos SDK proto definition, the change is justified because:

  1. The documentation shows the original VoteRequest struct used u64 for the option field, and the new VoteRequestV2 changes it to u32.
  2. The change is part of a systematic update to match Cosmos SDK's types, as evidenced by the PR objectives.
  3. The test file shows the vote functionality is being maintained and updated accordingly.

The change is safe and maintains compatibility with the Cosmos SDK's governance module while fixing the type inconsistency.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that Cosmos SDK's MsgVote uses uint32 for option field
rg -A 5 "message MsgVote" | rg "option"

Length of output: 39


Script:

#!/bin/bash
# Let's try searching for proto files that might contain MsgVote definition
fd -e proto | xargs rg -l "MsgVote"

# Also search for any documentation or comments about vote options
rg -i "vote.*option.*uint32|vote.*option.*u32"

Length of output: 389


Script:

#!/bin/bash
# Let's check the documentation files for any mention of vote options type
cat precompile/modules/minitia_stdlib/doc/cosmos.md

# Also check if there are any tests that might show the expected behavior
rg -A 3 "test.*vote" precompile/modules/minitia_stdlib/sources/cosmos.move

Length of output: 23168


94-109: Verify the safety of u64 to u32 conversion.

While the implementation correctly forwards to the new v2 function, the type cast from u64 to u32 could potentially truncate values. However, this should be safe as Cosmos vote options are typically small values (0-4).

Let's verify the valid vote option values:

precompile/modules/initia_stdlib/sources/cosmos.move (2)

22-24: LGTM

The stargate function correctly calls stargate_internal with the disallow_failure() option.


46-51: Verify that changing option from u64 to u32 will not cause issues

The option field in VoteRequestV2 has been changed from u64 to u32. Ensure that all possible option values fit within the u32 range to prevent potential overflows or data truncation.

Copy link
Contributor

@ALPAC-4 ALPAC-4 left a comment

Choose a reason for hiding this comment

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

LGTM

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 (2)
precompile/modules/initia_stdlib/sources/cosmos.move (2)

75-81: Consider adding deprecation notice in documentation

While the code is correctly marked with #[deprecated], it would be helpful to add documentation comments explaining:

  1. Why these entities are deprecated
  2. When they will be removed
  3. How users should migrate to the new VoteRequestV2 and stargate_vote_v2

Also applies to: 84-98


Line range hint 554-582: Consider adding edge case tests

While the current tests verify the basic functionality, consider adding test cases for:

  1. Maximum valid option value (u32::MAX)
  2. Values exceeding u32::MAX to verify error handling
#[test]
#[expected_failure(abort_code = 0x10001)] // EINVALID_OPTION_VALUE
public fun test_stargate_vote_overflow(sender: &signer) {
    let option: u64 = 0x100000000; // Exceeds u32::MAX
    stargate_vote(sender, 1, string::utf8(b"voter"), option, string::utf8(b"metadata"));
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7da8337 and f6d66cb.

📒 Files selected for processing (1)
  • precompile/modules/initia_stdlib/sources/cosmos.move (4 hunks)
🔇 Additional comments (2)
precompile/modules/initia_stdlib/sources/cosmos.move (2)

26-39: Great documentation for the stargate_with_options function!

The documentation clearly explains the available options and provides helpful examples of callback function signatures, making it easy for users to understand how to use this function.


Line range hint 46-71: Implementation aligns with the PR objective

The new VoteRequestV2 struct correctly uses u32 for the option field, ensuring consistency with the original MsgVote message format. The implementation properly marshals the request with the correct type prefix.

@beer-1 beer-1 merged commit e5294e7 into main Nov 26, 2024
3 checks passed
@beer-1 beer-1 deleted the fix/stargate-vote branch November 26, 2024 03:11
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