Skip to content

test: Fork Hyperliquid behind its latest block in the simulator tests - #1624

Merged
pxrl merged 2 commits into
masterfrom
pxrl/hyperliquidForkRetry
Dec 3, 2026
Merged

pxrl merged 2 commits into
masterfrom
pxrl/hyperliquidForkRetry

Conversation

@pxrl

@pxrl pxrl commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Stops a flaky failure in "EVM Tests" that has hit unrelated pull requests twice in one day (#1622 and #1623).

The failure. Three suites (HyperCoreFlowExecutorTest, CCTPDirectFlowTest, SponsoredCCTPDstPeripheryTest) share a setUp that forks the public Hyperliquid RPC at its latest block. Now and then the fork fails:

vm.createSelectFork: ... error code -32603: invalid block height: 52914819

The RPC is served by more than one node. A fork asks for the latest block number and then for that block, and the node that got the second request had not yet received it. The block rejected on #1623 carries the timestamp of the second the tests ran in.

The fix. setUp asks for the latest block number and forks ten blocks before it. Blocks come about a second apart, so every node has a block that old.

  • One file: test/evm/foundry/local/external/hyper-evm-lib/test/BaseSimulatorTest.sol. It is vendored, so this diverges from upstream.
  • The tests still fail if the RPC is down.
  • The failure could not be reproduced on demand, so this is reasoned from the error and the block's timestamp, not measured.

🤖 Generated with Claude Code

Three suites fork the public Hyperliquid RPC at its latest block in
setUp. Now and then the fork fails with "invalid block height": the
RPC is served by more than one node, and the one asked for the latest
block had not yet received it. The block rejected on #1623 carries the
timestamp of the second the tests ran in. That has failed EVM Tests on
unrelated pull requests, twice in one day.

Ask for the latest block number and fork ten blocks before it. Blocks
come about a second apart, so every node has a block that old. The
tests still fail if the RPC is down.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Paul <108695806+pxrl@users.noreply.github.com>
@pxrl
pxrl requested review from droplet-rl and removed request for bmzig, mrice32 and nicholaspai December 3, 2026 13:45
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-03T14:33:38.403147Z aaf4cdd Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

droplet-rl
droplet-rl previously approved these changes Dec 3, 2026

@droplet-rl droplet-rl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. The fix matches the failure. When createSelectFork(url) is given no block, it calls eth_blockNumber and then eth_getBlockByNumber, and those two calls can reach different nodes. Pinning the fork ten blocks back closes that gap, and it doesn't add an RPC round-trip: it's still one eth_blockNumber plus one block fetch per setUp.

What I checked:

  • Decoding on the pinned Foundry (1.7.1, .foundry-version). vm.rpc takes the hex string, left-pads odd-length values with 0, and returns it as raw bytes. Lengths 20 and 32 become Address/FixedBytes and then go through convert_to_bytes, so they come back as raw bytes too. The shift handles every length up to 32. An empty result would underflow and fail loudly, which is fine.
  • Historical state on the public RPC. rpc.hyperliquid.xyz/evm returned eth_getStorageAt and eth_getBalance at latest - 10. A pinned fork needs that, and it works.
  • RPC disk cache. It doesn't change. In 1.7.1, cheatcode forks cache by the resolved block number whether or not a block is pinned.
  • Simulator reads. PrecompileSim and RealL1Read still eth_call HyperCore precompiles at "latest", so they now run about 10 s ahead of the forked EVM state. That skew already grew over a test run, and nothing in the simulator depends on wall-clock alignment, so I don't see a problem.
  • Tests. forge test on the three suites (HyperCoreFlowExecutor, SponsoredDirectFlow, SponsorredCCTPDstPeriphery) with Foundry 1.7.1: 53 passed, 0 failed. I didn't try to reproduce the original flake. CI's EVM Tests was still pending when I submitted this.

One optional readability nit is inline.

Comment on lines +55 to +57
// The result is the block number as the bytes of its hex digits: 0x3276a83 comes back as 03276a83.
bytes memory latest = vm.rpc(hyperliquidRpc, "eth_blockNumber", "[]");
uint256 latestBlock = uint256(bytes32(latest)) >> (8 * (32 - latest.length));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit, optional: vm.parseUint(vm.toString(latest)) gives the same number, and you could drop the shift and its comment. I compared the two against the live RPC on Foundry 1.7.1 and both returned 52916136.

uint256 latestBlock = vm.parseUint(vm.toString(vm.rpc(hyperliquidRpc, "eth_blockNumber", "[]")));

The current code is correct, so this is only about readability.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Taken in aaf4cdd: the block number is parsed with vm.parseUint(vm.toString(...)), and the shift and its comment are gone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Paul <108695806+pxrl@users.noreply.github.com>
@pxrl

pxrl commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: aaf4cdde54

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@droplet-rl droplet-rl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-approving after aaf4cdd. That commit only replaces the byte-shift parse with vm.parseUint(vm.toString(vm.rpc(...))), which addresses my earlier nit. Nothing else changed since my first approval.

I reran the three forked suites on the new head with the pinned Foundry 1.7.1: 53 passed, 0 failed. If the RPC returns an empty or malformed result, the test still fails loudly, either in parseUint or on the - FORK_BLOCKS_BEHIND underflow. Everything else from my first review still holds: vm.rpc decoding, the public RPC serving state at latest - 10, and the precompile "latest" reads.

I haven't checked:

  • CI EVM Tests on aaf4cdd, which was still pending when I submitted this.
  • The original intermittent failure, which I didn't reproduce.

As the description says, this is vendored code, so it will need reapplying if hyper-evm-lib is re-synced from upstream.

@pxrl
pxrl merged commit 063d72d into master Dec 3, 2026
9 checks passed
@pxrl
pxrl deleted the pxrl/hyperliquidForkRetry branch December 3, 2026 14:37
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