Skip to content

linear growth#377

Merged
thedavidmeister merged 3 commits into
mainfrom
2025-07-22-linera
Jul 22, 2025
Merged

linear growth#377
thedavidmeister merged 3 commits into
mainfrom
2025-07-22-linera

Conversation

@thedavidmeister
Copy link
Copy Markdown
Contributor

@thedavidmeister thedavidmeister commented Jul 22, 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

    • Re-enabled the linear growth operation with enhanced floating-point calculation support for improved precision.
  • Bug Fixes

    • Updated the linear growth operation to use floating-point arithmetic, increasing accuracy and reliability.
  • Tests

    • Refactored and expanded tests to validate floating-point inputs and outputs, ensuring correctness and robustness of the linear growth calculation.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 22, 2025

"""

Walkthrough

The changes re-enable and update the LibOpLinearGrowth opcode in the Rain Interpreter. This includes integrating it back into the standard opcode library, refactoring its implementation to use a new float type (Float) and decimal float math, updating function signatures, and rewriting tests to handle floating-point inputs and outputs.

Changes

File(s) Change Summary
src/lib/op/LibAllStandardOps.sol Re-enabled LibOpLinearGrowth opcode: uncommented import, incremented opcode count, added metadata, and restored function pointers in handler arrays.
src/lib/op/math/growth/LibOpLinearGrowth.sol Refactored to use Float type and LibDecimalFloat for arithmetic; updated function signatures to use OperandV2; added new referenceFn for float-based reference calculation.
test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol Fully rewritten tests: switched from fixed-point to floating-point input/output, updated to use OperandV2 and StackItem, expanded evaluation cases, and improved input validation.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Interpreter
    participant LibOpLinearGrowth
    participant LibDecimalFloat

    Caller->>Interpreter: Execute linear-growth opcode
    Interpreter->>LibOpLinearGrowth: run(state, operandV2, stackTop)
    LibOpLinearGrowth->>LibDecimalFloat: Unpack and compute base, rate, t as Float
    LibOpLinearGrowth->>LibDecimalFloat: result = base + rate * t
    LibDecimalFloat-->>LibOpLinearGrowth: Computed Float result
    LibOpLinearGrowth-->>Interpreter: Push result to stack
    Interpreter-->>Caller: Return result
Loading

Estimated code review effort

3 (~45 minutes)

Possibly related PRs

  • 2025 07 12 mul #355: Re-enables and updates the LibOpMul opcode with floating-point arithmetic and operand changes, sharing the same integration and implementation approach.
  • 2025 07 09 div #351: Re-enables and refactors the LibOpDiv opcode with floating-point arithmetic and updated operand handling, modifying the same standard ops integration.
  • inv op #374: Re-enables and updates the LibOpInv opcode with floating-point arithmetic and updated operand types, following a similar pattern of opcode reactivation and modernization.
    """

📜 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 6a4a775 and 77925ac.

📒 Files selected for processing (1)
  • test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (1 hunks)
🧠 Learnings (2)
📓 Common learnings
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:0-0
Timestamp: 2025-07-15T11:31:28.010Z
Learning: In the rainlanguage/rain.interpreter project, forge (Foundry's formatting tool) handles code formatting automatically, so formatting-related suggestions are not actionable.
test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (1)

Learnt from: thedavidmeister
PR: #368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:0-0
Timestamp: 2025-07-15T11:31:28.010Z
Learning: In the rainlanguage/rain.interpreter project, forge (Foundry's formatting tool) handles code formatting automatically, so formatting-related suggestions are not actionable.
test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (1)

Learnt from: thedavidmeister
PR: #368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (macos-latest, rainix-rs-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: rainix (macos-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, test-wasm-build)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-static)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-artifacts)
  • GitHub Check: git-clean
🔇 Additional comments (4)
test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (4)

11-17: Integrity test looks good!

The test correctly validates that the linear growth operation always requires 3 inputs and produces 1 output.


51-74: Comprehensive evaluation test coverage!

The test cases effectively cover various scenarios including zero values, fractional inputs, negative coefficients, and different time values. The expected Float values are correctly constructed using LibDecimalFloat.packLossless.


76-98: Input/output validation tests are thorough!

The tests properly validate that the operation fails when provided with incorrect numbers of inputs (expecting exactly 3) or outputs (expecting exactly 1).


100-103: Operand restriction test is correct!

The test properly validates that the linear growth operation doesn't accept operands, throwing UnexpectedOperand when one is provided.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this 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.

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 0d32b1c and 1147cfa.

⛔ Files ignored due to path filters (3)
  • src/generated/Rainterpreter.pointers.sol is excluded by !**/generated/**
  • src/generated/RainterpreterExpressionDeployer.pointers.sol is excluded by !**/generated/**
  • src/generated/RainterpreterParser.pointers.sol is excluded by !**/generated/**
📒 Files selected for processing (3)
  • src/lib/op/LibAllStandardOps.sol (6 hunks)
  • src/lib/op/math/growth/LibOpLinearGrowth.sol (1 hunks)
  • test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (1 hunks)
🧠 Learnings (3)
📓 Common learnings
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:0-0
Timestamp: 2025-07-15T11:31:28.010Z
Learning: In the rainlanguage/rain.interpreter project, forge (Foundry's formatting tool) handles code formatting automatically, so formatting-related suggestions are not actionable.
src/lib/op/LibAllStandardOps.sol (1)

Learnt from: thedavidmeister
PR: #368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (1)

Learnt from: thedavidmeister
PR: #368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:0-0
Timestamp: 2025-07-15T11:31:28.010Z
Learning: In the rainlanguage/rain.interpreter project, forge (Foundry's formatting tool) handles code formatting automatically, so formatting-related suggestions are not actionable.
src/lib/op/LibAllStandardOps.sol (1)

Learnt from: thedavidmeister
PR: #368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (1)

Learnt from: thedavidmeister
PR: #368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: rainix (ubuntu-latest, test-wasm-build)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: rainix (macos-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-static)
  • GitHub Check: rainix (macos-latest, rainix-rs-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-test)
  • GitHub Check: git-clean
🔇 Additional comments (14)
src/lib/op/LibAllStandardOps.sol (5)

63-63: LGTM! Import statement correctly re-enabled.

The import for LibOpLinearGrowth has been properly uncommented to re-enable the linear growth opcode.


243-246: LGTM! Linear growth metadata properly added.

The authoring metadata for the linear growth opcode correctly describes its functionality and parameter order.


440-441: LGTM! Operand handler correctly configured.

The linear growth opcode uses handleOperandDisallowed, consistent with other math operations that don't require special operand handling.


579-579: LGTM! Integrity function pointer correctly added.

The integrity check function pointer for LibOpLinearGrowth is properly positioned in the array.


687-687: LGTM! Runtime function pointer correctly added.

The runtime execution function pointer for LibOpLinearGrowth is properly positioned in the array.

src/lib/op/math/growth/LibOpLinearGrowth.sol (4)

1-10: LGTM! Imports and dependencies updated correctly.

The imports have been properly updated to use the new Float type system and OperandV2, aligning with the interpreter's updated architecture.


11-18: LGTM! Integrity check correctly specifies 3 inputs and 1 output.

The integrity function properly enforces that linear growth requires exactly 3 inputs (base, rate, time) and produces 1 output.


20-38: LGTM! Linear growth formula correctly implemented.

The run function properly:

  1. Loads three Float values from the stack (base, rate, time)
  2. Implements the linear growth formula: base + (rate * t)
  3. Correctly manages the stack pointer to consume 3 inputs and produce 1 output

The assembly implementation is efficient and follows the expected stack manipulation pattern.


40-53: LGTM! Reference implementation matches the optimized version.

The referenceFn correctly implements the same linear growth formula as the optimized run function, providing a clear reference for testing. The use of StackItem wrapping/unwrapping is appropriate for the testing interface.

test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (5)

1-9: LGTM! Test imports updated correctly.

The test file imports have been properly updated to use the new Float type system and testing infrastructure.


11-17: LGTM! Integrity test validates correct input/output counts.

The integrity test correctly verifies that linear growth always requires 3 inputs and produces 1 output.


19-49: LGTM! Runtime test provides comprehensive fuzz testing.

The runtime test properly:

  1. Bounds exponents to prevent overflow
  2. Creates Float values using packLossless
  3. Tests the reference implementation against the optimized version
  4. Uses appropriate fuzzing parameters for thorough testing

74-96: LGTM! Input/output validation tests are comprehensive.

The tests properly validate that:

  • Linear growth requires exactly 3 inputs
  • Linear growth produces exactly 1 output
  • Various incorrect input/output combinations are rejected

98-102: LGTM! Operand disallowance correctly tested.

The test verifies that operands are not allowed for the linear growth opcode, consistent with its configuration in LibAllStandardOps.

Comment thread src/lib/op/LibAllStandardOps.sol
Comment thread test/src/lib/op/math/growth/LibOpLinearGrowth.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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1147cfa and 6a4a775.

📒 Files selected for processing (1)
  • test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (1 hunks)
🧠 Learnings (2)
📓 Common learnings
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:0-0
Timestamp: 2025-07-15T11:31:28.010Z
Learning: In the rainlanguage/rain.interpreter project, forge (Foundry's formatting tool) handles code formatting automatically, so formatting-related suggestions are not actionable.
test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (2)

Learnt from: thedavidmeister
PR: #368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

Learnt from: 0xgleb
PR: #334
File: crates/eval/src/trace.rs:92-118
Timestamp: 2025-06-17T18:01:06.316Z
Learning: User 0xgleb considers refactoring to remove a single duplicate as premature optimization in crates/eval/src/trace.rs when dealing with trace-filtering logic.

🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:0-0
Timestamp: 2025-07-15T11:31:28.010Z
Learning: In the rainlanguage/rain.interpreter project, forge (Foundry's formatting tool) handles code formatting automatically, so formatting-related suggestions are not actionable.
test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (2)

Learnt from: thedavidmeister
PR: #368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

Learnt from: 0xgleb
PR: #334
File: crates/eval/src/trace.rs:92-118
Timestamp: 2025-06-17T18:01:06.316Z
Learning: User 0xgleb considers refactoring to remove a single duplicate as premature optimization in crates/eval/src/trace.rs when dealing with trace-filtering logic.

🔇 Additional comments (5)
test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol (5)

11-17: Well-structured integrity test.

The integrity test correctly verifies that the linear growth operation requires exactly 3 inputs and produces 1 output.


51-72: Excellent test coverage for linear growth calculations.

The evaluation tests thoroughly cover various scenarios including zero values, fractional inputs, negative growth rates, and negative initial values. The expected results correctly implement the linear growth formula a + r * t.


74-88: Comprehensive input validation tests.

The tests properly verify that the operation rejects invalid input counts (0, 1, 2, and 4) when exactly 3 inputs are required.


90-96: Proper output validation tests.

The tests correctly verify that the operation rejects invalid output counts (0 and 2) when exactly 1 output is required.


98-102: Important operand validation test.

The test correctly verifies that the linear growth operation doesn't accept operands, throwing an UnexpectedOperand error when one is provided.

Comment thread test/src/lib/op/math/growth/LibOpLinearGrowth.t.sol
@thedavidmeister thedavidmeister merged commit e2b6f72 into main Jul 22, 2025
11 checks passed
This was referenced Jul 28, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Aug 6, 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