Skip to content

add isStartBlock and findDeployBlock to locate contract deploy blocks#5

Merged
thedavidmeister merged 2 commits intomainfrom
2026-03-12-start-block
Mar 12, 2026
Merged

add isStartBlock and findDeployBlock to locate contract deploy blocks#5
thedavidmeister merged 2 commits intomainfrom
2026-03-12-start-block

Conversation

@thedavidmeister
Copy link
Contributor

@thedavidmeister thedavidmeister commented Mar 12, 2026

Binary searches fork history via vm.rollFork to find the first block where a contract with the expected code hash appears. isStartBlock validates that a block is the deployment boundary by checking the code hash exists at the block but not at block - 1.

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

    • Introduced enhanced deployment tracking with binary search capabilities to identify exact deployment blocks.
    • Added validation checks to prevent contracts from deploying before specified block numbers.
    • Improved deployment history analysis through forked blockchain inspection.
  • Tests

    • Comprehensive test coverage for deployment detection, validation, and fork state restoration.

Binary searches fork history via vm.rollFork to find the first block
where a contract with the expected code hash appears. isStartBlock
validates that a block is the deployment boundary by checking the
code hash exists at the block but not at block - 1.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@thedavidmeister thedavidmeister self-assigned this Mar 12, 2026
@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

Warning

Rate limit exceeded

@thedavidmeister has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 3 minutes and 46 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: a5fc0431-70c6-45f5-b649-dd020e0138e6

📥 Commits

Reviewing files that changed from the base of the PR and between db66d29 and 702d390.

📒 Files selected for processing (1)
  • test/src/lib/LibRainDeploy.t.sol

Walkthrough

Added two new errors and two internal utility functions to LibRainDeploy for deploy-trace capabilities. The isStartBlock() function detects if a contract's code hash first appears at a given block using forking, while findDeployBlock() implements binary search to locate the first deployment block. Corresponding test coverage validates behavior and fork state restoration.

Changes

Cohort / File(s) Summary
Deploy Trace Library Functions
src/lib/LibRainDeploy.sol
Added public errors NotDeployed and DeployedBeforeStartBlock. Introduced internal isStartBlock() to detect code hash deployment at block boundaries via forking, and internal findDeployBlock() implementing binary search to locate first block where contract attains expected code hash with pre-checks, boundary validation against startBlock, and fork state restoration.
Deploy Trace Tests
test/src/lib/LibRainDeploy.t.sol
Added three external wrapper functions (externalIsStartBlock, externalFindDeployBlock, externalDeployAndBroadcast) exposing library functions for testing. Introduced eight test functions validating behavior across scenarios: code presence, deployment blocks, error conditions, fork restoration, and real-world case (Zoltu Factory).

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Suite
    participant FindDeploy as findDeployBlock()
    participant IsStart as isStartBlock()
    participant Vm as Forge VM
    participant History as Block History

    Test->>FindDeploy: findDeployBlock(target, codeHash, startBlock)
    FindDeploy->>Vm: Check code exists at current block
    Vm-->>FindDeploy: Code present/absent
    FindDeploy->>Vm: Check code hash matches at current block
    Vm-->>FindDeploy: Hash valid/invalid
    
    alt Binary Search Loop
        FindDeploy->>IsStart: isStartBlock(mid block)
        IsStart->>Vm: Fork to mid - 1 block
        IsStart->>Vm: Get code hash at mid - 1
        IsStart->>Vm: Fork to mid block
        IsStart->>Vm: Get code hash at mid
        IsStart->>Vm: Restore original fork
        Vm-->>IsStart: Comparison result
        IsStart-->>FindDeploy: Is start block?
    end
    
    FindDeploy-->>Test: Deploy block number or error
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: two new functions (isStartBlock and findDeployBlock) were added to locate contract deployment blocks, which is the primary objective of the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 2026-03-12-start-block
📝 Coding Plan for PR comments
  • Generate coding plan

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/lib/LibRainDeploy.sol`:
- Around line 91-96: The docstring for the deployment search claims the result
"is validated via `isStartBlock`" but the function `findDeployBlock` doesn't
call `isStartBlock`; update `findDeployBlock` to invoke
`isStartBlock(foundBlock, target)` (or the appropriate signature) after locating
the candidate block and before returning, and revert or return an error if
`isStartBlock` returns false; ensure the fork is still restored to its original
block number before reverting/returning.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 6f49d362-2622-4b2a-9194-7d843fb11a57

📥 Commits

Reviewing files that changed from the base of the PR and between ccfdb37 and db66d29.

📒 Files selected for processing (2)
  • src/lib/LibRainDeploy.sol
  • test/src/lib/LibRainDeploy.t.sol

Comment on lines +91 to +96
/// Finds the block number at which a contract was first deployed by binary
/// searching the fork history. Requires an active fork with archive access
/// back to `startBlock`. The fork is restored to its original block
/// number before returning. The target's code hash is verified against the
/// expected value before searching. The result is validated via
/// `isStartBlock`.
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Documentation states validation that isn't performed.

The docstring says "The result is validated via isStartBlock" but the implementation doesn't call isStartBlock. The tests call it separately for verification (see testFindDeployBlockZoltuFactory). Either add the validation call or update the docstring to clarify that callers should validate the result.

📝 Suggested docstring fix
 /// Finds the block number at which a contract was first deployed by binary
 /// searching the fork history. Requires an active fork with archive access
 /// back to `startBlock`. The fork is restored to its original block
 /// number before returning. The target's code hash is verified against the
-/// expected value before searching. The result is validated via
-/// `isStartBlock`.
+/// expected value before searching. Callers can validate the result via
+/// `isStartBlock` if needed.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/// Finds the block number at which a contract was first deployed by binary
/// searching the fork history. Requires an active fork with archive access
/// back to `startBlock`. The fork is restored to its original block
/// number before returning. The target's code hash is verified against the
/// expected value before searching. The result is validated via
/// `isStartBlock`.
/// Finds the block number at which a contract was first deployed by binary
/// searching the fork history. Requires an active fork with archive access
/// back to `startBlock`. The fork is restored to its original block
/// number before returning. The target's code hash is verified against the
/// expected value before searching. Callers can validate the result via
/// `isStartBlock` if needed.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib/LibRainDeploy.sol` around lines 91 - 96, The docstring for the
deployment search claims the result "is validated via `isStartBlock`" but the
function `findDeployBlock` doesn't call `isStartBlock`; update `findDeployBlock`
to invoke `isStartBlock(foundBlock, target)` (or the appropriate signature)
after locating the candidate block and before returning, and revert or return an
error if `isStartBlock` returns false; ensure the fork is still restored to its
original block number before reverting/returning.

@thedavidmeister thedavidmeister merged commit 43a6ed3 into main Mar 12, 2026
4 checks passed
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