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: fun token state marshalling #1947

Merged
merged 8 commits into from
Jun 28, 2024
Merged

Conversation

k-yang
Copy link
Member

@k-yang k-yang commented Jun 28, 2024

Purpose / Abstract

  • Fixes FunToken marshalling
  • adds FunTokenState unit tests
  • refactor to use core geth types instead of type aliases

Summary by CodeRabbit

  • New Features

    • Added tests for inserting, getting, collecting, and deleting FunTokens in the EVM module.
  • Refactor

    • Updated various Ethereum address and hash type declarations to improve compatibility and code clarity.
    • Refactored FunToken ID generation and validation logic for better error handling.
  • Bug Fixes

    • Fixed FunToken state marshalling in the EVM module.
  • Tests

    • Added new test functions to ensure correct address-to-string conversions and FunToken operations.

Copy link
Contributor

coderabbitai bot commented Jun 28, 2024

Walkthrough

The updates focus on refining the handling of Ethereum addresses within the EVM module by replacing the EthAddr and EthHash aliases with gethcommon.Address and gethcommon.Hash. The changes improve ID generation and error handling for FunToken, ensuring more consistent and simplified address management while adding new test functions to ensure reliable conversions and functionality.

Changes

File Change Summary
CHANGELOG.md Fixed FunToken state marshalling. No alterations to exported/public entities.
eth/hex.go Updated NewHexAddr and ToAddr signatures to use gethcommon.Address. Modified MarshalTo.
eth/hex_test.go Added TestHexAddrToString() to validate hex address conversion methods.
eth/state_encoder.go Replaced EthAddr/EthHash with gethcommon.Address/gethcommon.Hash. Updated encoders.
eth/state_encoder_test.go Changed given parameter to gethcommon.Address in TestEncoderEthAddr().
x/evm/evm.go Refactored NewFunTokenID and error handling in FunToken. Removed newFunTokenIDFromStr.
x/evm/keeper/evm_state.go Updated SetAccState and GetState to use gethcommon.Address and gethcommon.Hash.
x/evm/keeper/funtoken_state.go Modified FunTokenState structure to use []byte and evm.FunToken for indexing and encoding.
x/evm/keeper/funtoken_state_test.go Added tests for FunToken operations: TestInsertAndGet, TestCollect, and TestDelete.

Poem

Soft whispers through the code repository,
Updating types with thoughtful clarity.
Ethereum addresses now more clean,
FunTokens validated, ensured to glean.
Tests to guide, ensuring all abide,
In this enhanced ether's tide.
🐇✨


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.

@k-yang k-yang marked this pull request as ready for review June 28, 2024 16:53
@k-yang k-yang requested a review from a team as a code owner June 28, 2024 16:53
Copy link

codecov bot commented Jun 28, 2024

Codecov Report

Attention: Patch coverage is 70.83333% with 7 lines in your changes missing coverage. Please review.

Project coverage is 64.80%. Comparing base (422c676) to head (f3ac2f0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1947      +/-   ##
==========================================
+ Coverage   64.71%   64.80%   +0.08%     
==========================================
  Files         253      253              
  Lines       16053    16050       -3     
==========================================
+ Hits        10389    10401      +12     
+ Misses       4907     4892      -15     
  Partials      757      757              
Files Coverage Δ
x/evm/keeper/evm_state.go 85.36% <100.00%> (ø)
x/evm/keeper/funtoken_state.go 92.00% <100.00%> (+48.00%) ⬆️
eth/hex.go 74.07% <66.66%> (ø)
eth/state_encoder.go 80.95% <70.00%> (ø)
x/evm/evm.go 47.05% <57.14%> (+7.05%) ⬆️

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: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 422c676 and f3ac2f0.

Files selected for processing (9)
  • CHANGELOG.md (1 hunks)
  • eth/hex.go (3 hunks)
  • eth/hex_test.go (1 hunks)
  • eth/state_encoder.go (3 hunks)
  • eth/state_encoder_test.go (2 hunks)
  • x/evm/evm.go (1 hunks)
  • x/evm/keeper/evm_state.go (2 hunks)
  • x/evm/keeper/funtoken_state.go (3 hunks)
  • x/evm/keeper/funtoken_state_test.go (1 hunks)
Additional context used
GitHub Check: codecov/patch
x/evm/evm.go

[warning] 18-18: x/evm/evm.go#L18
Added line #L18 was not covered by tests


[warning] 21-22: x/evm/evm.go#L21-L22
Added lines #L21 - L22 were not covered by tests

eth/state_encoder.go

[warning] 69-70: eth/state_encoder.go#L69-L70
Added lines #L69 - L70 were not covered by tests


[warning] 73-73: eth/state_encoder.go#L73
Added line #L73 was not covered by tests

eth/hex.go

[warning] 91-91: eth/hex.go#L91
Added line #L91 was not covered by tests

Additional comments not posted (15)
x/evm/evm.go (2)

25-35: Good refactor on error handling in Validate method.

Using funTokenValidationError centralizes error formatting and enhances maintainability.


21-22: Add test coverage for NewFunTokenID.

This function is critical for ensuring unique token IDs but lacks test coverage.

Verification successful

Test coverage for NewFunTokenID is confirmed.

The NewFunTokenID function is covered by test cases in the x/evm/keeper/funtoken_state_test.go file.

  • x/evm/keeper/funtoken_state_test.go: Lines containing NewFunTokenID
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the test coverage for `NewFunTokenID` function in `x/evm/evm.go`.

# Test: Search for test cases covering the `NewFunTokenID` function. Expect: At least one test case.
rg --type python $'test.*NewFunTokenID' x/evm/evm_test.go

Length of output: 89



Script:

#!/bin/bash
# Description: Verify the test coverage for `NewFunTokenID` function in `x/evm/evm.go`.

# Test: Search for test cases covering the `NewFunTokenID` function. Expect: At least one test case.
rg --type go 'NewFunTokenID' --glob '*_test.go'

Length of output: 451

Tools
GitHub Check: codecov/patch

[warning] 21-22: x/evm/evm.go#L21-L22
Added lines #L21 - L22 were not covered by tests

x/evm/keeper/funtoken_state.go (2)

19-19: Improvement on type usage in FunTokenState.

Using []byte and evm.FunToken directly in the IndexedMap simplifies and potentially enhances performance.


69-72: Proper validation before insertion in SafeInsert.

Ensuring data integrity by validating FunToken before insertion is a best practice.

x/evm/keeper/funtoken_state_test.go (3)

11-29: Comprehensive testing for insertion and retrieval in TestInsertAndGet.

The test effectively verifies the critical functionality of FunTokenState by checking insertion and retrieval operations.


34-60: Effective verification of collect functionality in TestCollect.

The test ensures that FunToken instances are correctly retrieved by both bank denomination and ERC20 address, confirming the indexing and collection logic.


63-82: Thorough testing of delete functionality in TestDelete.

The test ensures that FunToken instances can be successfully removed and are not retrievable post-deletion, confirming the delete logic.

eth/state_encoder_test.go (1)

Line range hint 67-80: Effective testing of Ethereum address encoder in TestEncoderEthAddr.

The test ensures bijectivity, verifying that encoding, decoding, and stringification are consistent and correct.

eth/state_encoder.go (2)

21-23: Updated encoders for Ethereum address and hash.

The changes align with the PR's objectives to use core Geth types, improving type safety and clarity. However, ensure that these encoders are covered by tests.

Also applies to: 26-26


69-73: Implementation of keEthHash encoder.

The encoder correctly handles gethcommon.Hash, converting to bytes and back, and providing a string representation. Ensure this encoder is covered by tests.

Tools
GitHub Check: codecov/patch

[warning] 69-70: eth/state_encoder.go#L69-L70
Added lines #L69 - L70 were not covered by tests


[warning] 73-73: eth/state_encoder.go#L73
Added line #L73 was not covered by tests

eth/hex.go (2)

19-19: Refactored NewHexAddr to use gethcommon.Address.

The change aligns with the PR's objective to use core Geth types. This should ensure better type safety and compatibility with the Ethereum library.


61-61: Refactored ToAddr to return gethcommon.Address.

This change is consistent with the updated NewHexAddr function, maintaining consistency across the module.

x/evm/keeper/evm_state.go (2)

133-133: Updated SetAccState to use gethcommon.Address and gethcommon.Hash.

This change is part of the refactoring to use core Geth types, which should improve the consistency and reliability of the EVM state management.


143-143: Updated GetState to use gethcommon.Address and gethcommon.Hash.

The consistent use of Geth types across these critical functions helps ensure that the Ethereum state is managed accurately within the Cosmos SDK environment.

eth/hex_test.go (1)

274-281: New test function TestHexAddrToString added.

This test ensures that the String method of HexAddr returns the correct string representation of an Ethereum address. It's a good addition to the test suite as it verifies the integrity of address conversions.

@@ -77,6 +77,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1917](https://github.com/NibiruChain/nibiru/pull/1917) - test(e2e-evm): TypeScript support. Type generation from compiled contracts. Formatter for TS code.
- [#1922](https://github.com/NibiruChain/nibiru/pull/1922) - feat(evm): tracer option is read from the config.
- [#1936](https://github.com/NibiruChain/nibiru/pull/1936) - feat(evm): EVM fungible token protobufs and encoding tests
- [#1947](https://github.com/NibiruChain/nibiru/pull/1947) - fix(evm): fix FunToken state marshalling
Copy link
Contributor

Choose a reason for hiding this comment

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

Enhance the changelog entry for better clarity.

The current changelog entry for PR 1947 is a bit vague. It would be beneficial for users and developers if the entry could provide a bit more context about what exactly was fixed with the FunToken state marshalling and why it was significant.

- [#1947](https://github.com/NibiruChain/nibiru/pull/1947) - fix(evm): Address an issue with `FunToken` state marshalling that previously led to incorrect state transitions under specific conditions.

@@ -88,7 +88,7 @@
// Implements the gogo proto custom type interface.
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md
func (h *HexAddr) MarshalTo(data []byte) (n int, err error) {
bz := []byte{}
bz := []byte(*h)
Copy link
Contributor

Choose a reason for hiding this comment

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

Potential issue in MarshalTo method.

The MarshalTo method seems to unnecessarily recreate the HexAddr from a string. This could be simplified and optimized.

-	hexAddr, err := NewHexAddrFromStr(string(bz))
-	*h = hexAddr
+	// Removed unnecessary conversion
+	err = nil

Additionally, this line was flagged by codecov as not covered by tests.

Would you like me to help add a test case for this method?

Committable suggestion was skipped due to low confidence.

Tools
GitHub Check: codecov/patch

[warning] 91-91: eth/hex.go#L91
Added line #L91 was not covered by tests

@@ -15,29 +15,24 @@
// tokens that are valid ERC20s with the same address.
// https://github.com/NibiruChain/nibiru/issues/1933
func (fun FunToken) ID() []byte {
return newFunTokenIDFromStr(fun.Erc20Addr.String(), fun.BankDenom)
return NewFunTokenID(fun.Erc20Addr, fun.BankDenom)
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Add test coverage for the ID method.

The method is crucial for generating unique identifiers but lacks test coverage. Ensure to include test cases that validate the correctness of the ID method in x/evm/evm.go.

  • Method: ID in x/evm/evm.go
Analysis chain

Add test coverage for the ID method.

The method is crucial for generating unique identifiers but lacks test coverage.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the test coverage for `ID` method in `x/evm/evm.go`.

# Test: Search for test cases covering the `ID` method. Expect: At least one test case.
rg --type python $'test.*ID' x/evm/evm_test.go

Length of output: 78



Script:

#!/bin/bash
# Description: Verify the test coverage for `ID` method in `x/evm/evm.go`.

# Test: Search for test cases covering the `ID` method. Expect: At least one test case.
rg --type go 'ID' x/evm/evm_test.go

Length of output: 35



Script:

#!/bin/bash
# Description: Verify the test coverage for `ID` method in `x/evm/evm.go`.

# Test: Search for test cases covering the `ID` method across all Go test files. Expect: At least one test case.
rg --type go 'ID' --glob '*.go'

Length of output: 57772

Tools
GitHub Check: codecov/patch

[warning] 18-18: x/evm/evm.go#L18
Added line #L18 was not covered by tests

Copy link
Member

@Unique-Divine Unique-Divine left a comment

Choose a reason for hiding this comment

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

LGTM w/ small change comment

)

type (
funtokenPrimaryKeyType = []byte
Copy link
Member

Choose a reason for hiding this comment

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

I think the primary key type is easier to follow when reading the collections below than using []byte

@Unique-Divine Unique-Divine merged commit 2c1198d into main Jun 28, 2024
18 checks passed
@Unique-Divine Unique-Divine deleted the test/evm/fun-token-state branch June 28, 2024 17:16
@k-yang k-yang restored the test/evm/fun-token-state branch January 3, 2025 16:09
@k-yang k-yang deleted the test/evm/fun-token-state branch January 3, 2025 16:15
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