Skip to content

format#26

Merged
thedavidmeister merged 6 commits intomainfrom
2025-04-07-format
Apr 7, 2025
Merged

format#26
thedavidmeister merged 6 commits intomainfrom
2025-04-07-format

Conversation

@thedavidmeister
Copy link
Copy Markdown
Contributor

@thedavidmeister thedavidmeister commented Apr 7, 2025

Motivation

Solution

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

Summary by CodeRabbit

  • New Features

    • Added enhanced functionality for formatting decimal float values into high-precision strings.
    • Introduced the ability to parse decimal float values directly from string inputs.
    • Integrated a fixed-point arithmetic module to bolster numerical computations.
  • Tests

    • Expanded test coverage to verify consistent round-trip conversions and validate the new formatting and parsing enhancements.
    • Updated performance metrics to ensure robust handling of decimal float operations.
    • Added new test cases to validate the functionality of the decimal formatting and parsing libraries, including additional scenarios for memory and external parsing.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 7, 2025

Walkthrough

The changes add new functionality for handling decimal floats. A new library, LibFormatDecimalFloat, has been created with functions to format decimal floats to strings, while LibParseDecimalFloat now includes a function to parse decimal floats from strings. New test cases in the respective test contracts cover formatting, round-trip conversion, and parsing, with several gas measurements updated. Additionally, a submodule for fixed-point math has been added to the repository along with an updated commit for that module.

Changes

Files Change Summary
.gas-snapshot Updated gas metrics for several test cases in LibParseDecimalFloatTest, with new tests (testFormatMem, testRoundTrip, and testParseMem) added that run 5099 times each along with adjusted average and expected gas values.
.gitmodules
lib/rain.math.fixedpoint
Added a new submodule entry for lib/rain.math.fixedpoint with its URL set to https://github.com/rainlanguage/rain.math.fixedpoint, and a new subproject commit (6f8f7f8c79fd23b00aa5b9bda1171712e200023c) is recorded.
src/lib/format/LibFormatDecimalFloat.sol
src/lib/parse/LibParseDecimalFloat.sol
Introduced a new library LibFormatDecimalFloat with two internal pure functions (toDecimalString) for formatting decimal floats. Added a new function parseDecimalFloat(string memory str) in LibParseDecimalFloat, including inline assembly for processing string inputs and updated import statements.
test/src/lib/format/LibFormatDecimalFloat.t.sol
test/src/lib/parse/LibParseDecimalFloat.t.sol
Created new test contracts: LibFormatDecimalFloatTest with tests for formatting and round-trip conversion, and LibParseDecimalFloatTest with new external parsing functions (parseDecimalFloatExternal, parseDecimalFloatExternalMem) and a test (testParseMem) to validate parsing consistency.

Possibly related PRs

  • float struct #24: Addresses changes involving the Float struct with new test cases in processing decimal float values, similar to the updates in the current PR.

Suggested reviewers

  • hardyjosh

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 13ab1cd and f48e553.

📒 Files selected for processing (1)
  • slither.config.json (1 hunks)
🔇 Additional comments (1)
slither.config.json (1)

3-3: Updated Slither Filter Paths Configuration
The new entry in the filter_paths field — "test/,lib/forge-std,lib/rain.math.fixedpoint,openzeppelin-contracts" — properly expands the scope of analysis to include the newly introduced fixed-point math submodule and OpenZeppelin contracts. This update is aligned with the recent repository changes and will help ensure that vulnerabilities in these additional directories are detected during static analysis.


🪧 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

Copy link
Copy Markdown
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: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 942efc4 and fc96706.

📒 Files selected for processing (7)
  • .gas-snapshot (2 hunks)
  • .gitmodules (1 hunks)
  • lib/rain.math.fixedpoint (1 hunks)
  • src/lib/format/LibFormatDecimalFloat.sol (1 hunks)
  • src/lib/parse/LibParseDecimalFloat.sol (2 hunks)
  • test/src/lib/format/LibFormatDecimalFloat.t.sol (1 hunks)
  • test/src/lib/parse/LibParseDecimalFloat.t.sol (1 hunks)
🔇 Additional comments (15)
lib/rain.math.fixedpoint (1)

1-1: Submodule Commit Update Verification

This update sets the submodule pointer to commit 6f8f7f8c79fd23b00aa5b9bda1171712e200023c. Please verify that this commit includes all the intended fixed-point math enhancements and that the submodule integration is functioning as expected in your overall build and test suite.

.gitmodules (1)

13-15: Integration of fixed-point math library.

The addition of the rain.math.fixedpoint submodule is appropriate and aligns with the new functionality for formatting decimal floats that's being introduced.

src/lib/format/LibFormatDecimalFloat.sol (2)

5-7: Appropriate import structure.

Good organization with separate imports for the internal decimal float library and the external fixed-point formatting library from the newly added submodule.


9-26: Clean implementation of decimal float formatting.

The library provides a well-documented and maintainable approach to formatting decimal floats:

  1. Good documentation explaining the current implementation and future extensibility plans
  2. Smart reuse of the fixed-point library for string formatting
  3. Clean function overloading for convenience

The implementation correctly handles the conversion process by:

  1. Converting to a fixed decimal with 18 decimals precision using toFixedDecimalLossless
  2. Formatting that value as a string using the fixed-point library

This approach minimizes code duplication and leverages the capabilities of both libraries.

src/lib/parse/LibParseDecimalFloat.sol (2)

16-16: Appropriate import update.

The addition of Float to the import statement is correct since it's now used as a return type in the new function.


144-154: Effective string-based decimal float parsing implementation.

The new function complements the formatting functionality by providing a convenient way to parse decimal floats from strings. The implementation:

  1. Efficiently uses inline assembly to access the string's memory location
  2. Properly delegates to the existing parsing function for the actual parsing logic
  3. Correctly handles the return values to maintain the same error handling approach

The implementation is minimal and effective, creating a clean API that works well with the new formatting functions.

.gas-snapshot (2)

181-182: New formatting test cases with acceptable gas usage.

The new test cases for the decimal float formatting functionality show reasonable gas consumption, with the round-trip test naturally using more gas due to the more complex operations involved.


188-212: Updated gas measurements for parsing tests.

The small increases in gas usage for some of the parsing tests are reasonable and expected given the additional functionality. The new testParseMem test shows acceptable gas usage for the string-based parsing function.

test/src/lib/parse/LibParseDecimalFloat.t.sol (4)

11-11: Import looks good.

The import statement for the Float struct is correctly added to support the new memory-based parsing functionality.


17-25: Well-implemented external wrapper function.

This function provides a clear external interface to LibParseDecimalFloat.parseDecimalFloat for testing purposes, correctly handling the pointer unwrapping and data passing.


27-33: Good implementation of memory-based parsing wrapper.

This function properly exposes the memory-based version of LibParseDecimalFloat.parseDecimalFloat for testing, with a clean interface returning both the error selector and the parsed Float struct.


35-48: Strong test comparison between stack and memory versions.

This test function effectively verifies that both implementations produce consistent results by:

  1. Checking that error selectors match
  2. Verifying that parsed values (coefficient and exponent) are identical
  3. Properly handling error cases using try-catch and vm.expectRevert

The test ensures the robustness of both parsing implementations.

test/src/lib/format/LibFormatDecimalFloat.t.sol (3)

10-12: Clean contract and library usage.

The contract definition and library usage declaration look good.


13-19: Well-implemented formatting wrapper functions.

Both functions properly expose the LibFormatDecimalFloat.toDecimalString functionality for testing purposes - one using raw parameters and one using the Float struct.


21-30: Strong test comparison between formatting approaches.

This test properly verifies that both the stack-based and memory-based approaches to formatting produce the same result, with appropriate error handling through try-catch.

Comment thread test/src/lib/format/LibFormatDecimalFloat.t.sol
Comment thread test/src/lib/format/LibFormatDecimalFloat.t.sol
Copy link
Copy Markdown
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: 1

♻️ Duplicate comments (1)
test/src/lib/format/LibFormatDecimalFloat.t.sol (1)

1-3: License inconsistency with other files persists.

The license identifier LicenseRef-DCL-1.0 still differs from the CAL license used in other files as noted in a previous review. This inconsistency should be standardized across the codebase.

-// SPDX-License-Identifier: LicenseRef-DCL-1.0
+// SPDX-License-Identifier: CAL
 // SPDX-FileCopyrightText: Copyright (c) 2020 thedavidmeister
 pragma solidity =0.8.25;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fc96706 and 3468862.

📒 Files selected for processing (1)
  • test/src/lib/format/LibFormatDecimalFloat.t.sol (1 hunks)
🔇 Additional comments (4)
test/src/lib/format/LibFormatDecimalFloat.t.sol (4)

13-15: LGTM! External wrapper function for testing stack parameters.

This external function correctly allows for testing the stack version of the toDecimalString function.


17-19: LGTM! External wrapper function for testing memory parameters.

This external function properly enables testing the memory version of the toDecimalString function.


21-30: Well-implemented test for format consistency across parameter types.

The test appropriately verifies that the memory and stack versions of toDecimalString produce identical results, with proper error propagation.


32-42: Well-structured round-trip test with improved documentation.

The test effectively verifies that a value can be formatted to a string and then parsed back to the original value. The comments on lines 34-35 now clearly explain the rationale for the bound, addressing previous feedback.

Consider adding explicit edge case tests:

function testRoundTripEdgeCases() external {
    // Test zero
    testValueRoundTrip(0);
    // Test one
    testValueRoundTrip(1);
    // Test max bound
    testValueRoundTrip(type(uint256).max / 10);
}

function testValueRoundTrip(uint256 value) internal {
    Float memory float = LibDecimalFloat.fromFixedDecimalLosslessMem(value, 18);
    string memory formatted = LibFormatDecimalFloat.toDecimalString(float);
    (bytes4 errorCode, Float memory parsed) = LibParseDecimalFloat.parseDecimalFloat(formatted);
    assertEq(errorCode, 0, "Parse error");
    assertTrue(float.eq(parsed), "Round trip failed");
}

Comment thread test/src/lib/format/LibFormatDecimalFloat.t.sol
thedavidmeister and others added 3 commits April 7, 2025 12:53
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@thedavidmeister thedavidmeister merged commit 8f5b640 into main Apr 7, 2025
4 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Apr 18, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request May 5, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Jun 13, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Sep 1, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Oct 2, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Oct 21, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Dec 1, 2025
4 tasks
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.

1 participant