8000 chore(evm): adjust extra decimals on stateDB methods by GAtom22 · Pull Request #2897 · evmos/evmos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore(evm): adjust extra decimals on stateDB methods #2897

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

Merged
merged 2 commits into from
Sep 30, 2024
Merged

Conversation

GAtom22
Copy link
Contributor
@GAtom22 GAtom22 commented Sep 30, 2024

Description

Closes: #XXXX


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...

  • tackled an existing issue or discussed with a team member
  • left instructions on how to review the changes
  • targeted the correct branch (see PR Targeting)

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...

  • added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • confirmed all author checklist items have been addressed
  • confirmed that this PR does not change production code
  • reviewed content
  • tested instructions (if applicable)
  • confirmed all CI checks have passed

Summary by CodeRabbit

  • New Features

    • Introduced new governance precompiles and IBC rate-limit middleware.
    • Added functionality for adjusting balance amounts with extra decimal precision.
  • Improvements

    • Enhanced performance of RPC calls and updated the EVM interface.
    • Improved documentation and testing processes.
  • Bug Fixes

    • Resolved issues with delegated amounts and gas estimation.
    • Fixed parsing issues to maintain backward compatibility.
  • Refactor

    • Updated method signatures for better clarity and consistency across the codebase.

Copy link
Contributor
coderabbitai bot commented Sep 30, 2024

Walkthrough

The pull request introduces significant updates to the Evmos project, including enhancements to the state object management, balance adjustments, and improved decimal handling in wrapper functions. Key changes involve the addition of methods for balance manipulation, modifications to existing methods for better tracking of state changes, and updates to function visibility. The changelog reflects ongoing efforts to improve functionality, compatibility, and performance across the ecosystem.

Changes

File Path Change Summary
CHANGELOG.md Updates across multiple versions, including breaking changes, improvements, and bug fixes related to state machine, performance, and documentation enhancements.
x/evm/statedb/state_object.go Added AddBalance and SubBalance methods; modified SetBalance and SetCode to include journal entries for state changes.
x/evm/wrappers/bank.go Updated method visibility for coin conversion functions, changing them from lowercase to uppercase.
x/evm/wrappers/utils.go Renamed functions for consistency and added AdjustExtraDecimalsBigInt for handling decimal adjustments in big integers.
x/evm/wrappers/utils_test.go Renamed package to wrappers_test and added test cases for coin conversion functions and decimal adjustments.

Possibly related PRs

Suggested labels

types, precompile

Suggested reviewers

  • hanchon
  • 0xstepit
  • ramacarlucho
  • fedekunze

🐇 In the world of code, we hop and play,
With balances adjusted, we brighten the day.
Methods added, functions refined,
In the realm of Evmos, new features aligned.
So let’s celebrate with a joyful cheer,
For every change brings us closer, my dear! 🥕


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.

@GAtom22 GAtom22 marked this pull request as ready for review September 30, 2024 15:02
@GAtom22 GAtom22 requested a review from a team as a code owner September 30, 2024 15:02
@GAtom22 GAtom22 requested review from hanchon and ramacarlucho and removed request for a team September 30, 2024 15:02
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

🧹 Outside diff range and nitpick comments (8)
x/evm/wrappers/bank.go (1)

102-102: Approve change and summarize overall impact

The change from convertCoinsFrom18Decimals to ConvertCoinsFrom18Decimals in the SendCoinsFromModuleToAccount method is consistent with all previous changes in this file.

These changes collectively represent a refactoring to make all conversion functions public (exported). While the functionality remains the same, the increased visibility of these functions may have implications for the broader codebase:

  1. It allows for more flexible usage of these functions in other packages.
  2. It potentially exposes these functions as part of the public API, which may have maintenance and backwards compatibility considerations.

Ensure that this change aligns with the overall architecture and design principles of the project.

Consider documenting these newly exported functions and updating any relevant API documentation to reflect these changes.

x/evm/statedb/state_object.go (5)

Line range hint 104-108: LGTM: Adjustment for extra decimals in AddBalance.

The addition of wrappers.AdjustExtraDecimalsBigInt(amount) aligns with the PR objective of handling extra decimals in stateDB methods. This change ensures that the balance is adjusted correctly before being added.

Consider adding a comment explaining the purpose of the AdjustExtraDecimalsBigInt call for better code readability:

// Adjust the amount for extra decimals before adding to balance
amount = wrappers.AdjustExtraDecimalsBigInt(amount)

Line range hint 114-118: LGTM: Adjustment for extra decimals in SubBalance.

The addition of wrappers.AdjustExtraDecimalsBigInt(amount) is consistent with the changes in AddBalance and aligns with the PR objective. This ensures that the balance is adjusted correctly before being subtracted.

Similar to the suggestion for AddBalance, consider adding a comment for clarity:

// Adjust the amount for extra decimals before subtracting from balance
amount = wrappers.AdjustExtraDecimalsBigInt(amount)

Line range hint 122-126: LGTM: Added journal entry for balance changes in SetBalance.

The addition of a journal entry for balance changes improves the tracking and potential reverting of state changes. This enhancement aligns with best practices for maintaining state in blockchain systems.

Consider adding a comment explaining the purpose of the journal entry:

// Append a journal entry to track the balance change
s.db.journal.append(balanceChange{
    account: &s.address,
    prev:    new(big.Int).Set(s.account.Balance),
})

Line range hint 183-188: LGTM: Added journal entry for code changes in SetCode.

The addition of a journal entry for code changes improves the tracking and potential reverting of contract code modifications. This enhancement aligns with best practices for maintaining state in blockchain systems.

Consider adding a comment explaining the purpose of the journal entry:

// Append a journal entry to track the code change
s.db.journal.append(codeChange{
    account:  &s.address,
    prevhash: s.CodeHash(),
    prevcode: prevcode,
})

Line range hint 1-270: Overall assessment: Changes are well-implemented and align with PR objectives.

The modifications to AddBalance, SubBalance, SetBalance, and SetCode methods improve the handling of extra decimals and enhance state tracking through journal entries. These changes are consistent with the PR objectives and follow good practices for blockchain state management.

Consider documenting the rationale behind the extra decimal adjustments in the package or file-level comments. This would help future developers understand the purpose and importance of these modifications in the context of the EVM state management.

x/evm/wrappers/utils_test.go (1)

210-263: LGTM: Comprehensive test function for AdjustExtraDecimalsBigInt.

The new TestZeroExtraDecimalsBigInt function is well-structured and covers a good range of scenarios, including edge cases. The use of table-driven tests and testing both 6 and 18 decimal configurations is commendable.

Consider adding a test case for a very large number (e.g., close to math.MaxInt64) to ensure the function handles large inputs correctly.

CHANGELOG.md (1)

67-67: Consider wrapping this line to improve readability.

The line exceeds the recommended length of 120 characters. Consider wrapping it to improve readability and consistency with markdown best practices.

Here's a suggested way to wrap the line:

- (evm) [#2897](https://github.com/evmos/evmos/pull/2897) Adjust extra decimals in `AddBalance` and `SubBalance`
  methods on `stateObject` in state DB.
🧰 Tools
🪛 Markdownlint

67-67: Expected: 120; Actual: 150
Line length

(MD013, line-length)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 1b155e3 and 53d67af.

📒 Files selected for processing (5)
  • CHANGELOG.md (1 hunks)
  • x/evm/statedb/state_object.go (3 hunks)
  • x/evm/wrappers/bank.go (4 hunks)
  • x/evm/wrappers/utils.go (4 hunks)
  • x/evm/wrappers/utils_test.go (4 hunks)
🧰 Additional context used
🪛 Markdownlint
CHANGELOG.md

67-67: Expected: 120; Actual: 150
Line length

(MD013, line-length)

🔇 Additional comments (15)
x/evm/wrappers/utils.go (4)

Line range hint 18-28: LGTM: Function renamed to follow Go conventions

The function has been correctly renamed from mustConvertEvmCoinTo18Decimals to MustConvertEvmCoinTo18Decimals, following Go's naming convention for exported functions. The logic remains unchanged and appropriate for its purpose.


Line range hint 31-41: LGTM: Function renamed to follow Go conventions

The function has been correctly renamed from convertEvmCoinFrom18Decimals to ConvertEvmCoinFrom18Decimals, following Go's naming convention for exported functions. The logic and error handling remain appropriate and unchanged.


Line range hint 44-59: LGTM: Function renamed to follow Go conventions

The function has been correctly renamed from convertCoinsFrom18Decimals to ConvertCoinsFrom18Decimals, following Go's naming convention for exported functions. The logic remains unchanged and correctly handles the conversion of multiple coins, only modifying the EVM coin while leaving others untouched.


61-73: Verify the behavior of the new AdjustExtraDecimalsBigInt function

The new function AdjustExtraDecimalsBigInt has been added to handle cases where the decimal configuration differs from 18 decimals. While the implementation looks correct, please consider the following points:

  1. Verify that this function behaves correctly for all possible decimal configurations.
  2. There might be a potential precision loss when scaling down and then up again. Consider if this is acceptable for your use case or if additional measures need to be taken to minimize precision loss.

To verify the function's behavior, please run the following test cases:

func TestAdjustExtraDecimalsBigInt(t *testing.T) {
	testCases := []struct {
		name     string
		input    *big.Int
		decimals int64
		expected *big.Int
	}{
		{"18 decimals", big.NewInt(1e18), 18, big.NewInt(1e18)},
		{"6 decimals", big.NewInt(1e18), 6, big.NewInt(1e6)},
		{"zero amount", big.NewInt(0), 6, big.NewInt(0)},
		{"large number", new(big.Int).Exp(big.NewInt(10), big.NewInt(30), nil), 6, new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)},
	}

	for _, 
8000
tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			// Set the EVM coin decimals for the test
			config.SetEVMCoinDecimals(sdk.NewInt(tc.decimals))
			
			result := AdjustExtraDecimalsBigInt(tc.input)
			if result.Cmp(tc.expected) != 0 {
				t.Errorf("Expected %s, got %s", tc.expected.String(), result.String())
			}
		})
	}
}

This test will help ensure the function behaves correctly for various inputs and decimal configurations.

x/evm/wrappers/bank.go (4)

63-63: Approve consistent change

The change from convertEvmCoinFrom18Decimals to ConvertEvmCoinFrom18Decimals in the BurnAmountFromAccount method is consistent with the previous change in MintAmountToAccount. This maintains the pattern of making the conversion functions public.


86-86: Approve change and note "Must" prefix implication

The change from mustConvertEvmCoinTo18Decimals to MustConvertEvmCoinTo18Decimals is consistent with the pattern of making conversion functions public.

Note that the "Must" prefix in the function name implies that this function will panic if it fails. This is a common Go pattern for functions that shouldn't fail under normal circumstances. Ensure that the function's implementation matches this expectation.


93-93: Approve consistent change

The change from convertCoinsFrom18Decimals to ConvertCoinsFrom18Decimals in the SendCoinsFromAccountToModule method is consistent with the previous changes. This maintains the pattern of making the conversion functions public.


45-45: Approve change and verify impact on codebase

The change from convertEvmCoinFrom18Decimals to ConvertEvmCoinFrom18Decimals is part of making the conversion functions public. While this change is correct, it's important to ensure that all other parts of the codebase using this function have been updated accordingly.

To verify the impact and consistency of this change across the codebase, run the following script:

✅ Verification successful

Verified function name update and consistent usage

All instances of convertEvmCoinFrom18Decimals have been replaced with ConvertEvmCoinFrom18Decimals across the codebase, ensuring consistency and proper function visibility.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining usage of the old function name and verify the new function name is used consistently.

# Test 1: Search for any remaining usage of the old function name
echo "Searching for any remaining usage of convertEvmCoinFrom18Decimals:"
rg --type go 'convertEvmCoinFrom18Decimals'

# Test 2: Verify the new function name is used consistently
echo "Verifying usage of ConvertEvmCoinFrom18Decimals:"
rg --type go 'ConvertEvmCoinFrom18Decimals'

Length of output: 883

x/evm/statedb/state_object.go (1)

14-14: LGTM: New import for wrappers package.

The addition of the wrappers package import is necessary for the new functionality in AddBalance and SubBalance methods.

x/evm/wrappers/utils_test.go (5)

4-4: LGTM: Package name change aligns with Go testing conventions.

The change from package wrappers to package wrappers_test is appropriate. This allows the tests to import and use the wrappers package as an external package, which is a good practice for testing exported functionality.


7-8: LGTM: Import changes are consistent with new test function and package structure.

The addition of the fmt import and the wrappers package import are appropriate given the package name change and the new test function.

Also applies to: 15-15


82-82: LGTM: Function call updated to use wrappers package.

The change to wrappers.MustConvertEvmCoinTo18Decimals(tc.coin) is consistent with the package restructuring and allows proper testing of the exported functionality.


146-146: LGTM: Function call updated to use wrappers package.

The change to wrappers.ConvertEvmCoinFrom18Decimals(tc.coin) is consistent with the package restructuring and allows proper testing of the exported functionality.


Line range hint 1-263: Summary: Improved test structure and coverage.

The changes in this file successfully:

  1. Align the package structure with Go testing conventions.
  2. Update existing tests to work with the new package structure.
  3. Add comprehensive testing for the AdjustExtraDecimalsBigInt function.

These improvements enhance the overall test coverage and maintainability of the codebase. Great job!

CHANGELOG.md (1)

Line range hint 1-66: LGTM: Changelog entries are well-documented and follow a consistent format.

The changes in the "Unreleased" section of the CHANGELOG.md file are well-documented and follow a consistent format. The entries provide clear information about improvements and bug fixes, which is helpful for users and developers to understand the changes in the upcoming release.

🧰 Tools
🪛 Markdownlint

64-64: Expected: 120; Actual: 139
Line length

(MD013, line-length)


67-67: Expected: 120; Actual: 150
Line length

(MD013, line-length)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0