Skip to content

feat(ethexe/ethereum): add initial gas benchmarks#5145

Open
StackOverflowExcept1on wants to merge 20 commits intomasterfrom
av/ethexe-contracts-benchmarks
Open

feat(ethexe/ethereum): add initial gas benchmarks#5145
StackOverflowExcept1on wants to merge 20 commits intomasterfrom
av/ethexe-contracts-benchmarks

Conversation

@StackOverflowExcept1on
Copy link
Member

@StackOverflowExcept1on StackOverflowExcept1on commented Feb 12, 2026

Resolves #5176

The basic idea is to introduce SimulationContext, which contains the context for the mock EVM blockchain. It creates revm instance within this context and stores the block_number, block_timestamp, mock smart contract deployer_address (Router, Mirror, WrappedVara, etc.), mock smart contract deployer_nonce, and validator private keys (we're using 4 validators in testnet).

This SimulationContext will then be initialized, and all smart contracts (Router, Mirror, WrappedVara, etc.) and their instrumented versions will be deployed to the mock blockchain. It's also worth noting that about 10 blocks will be created in SimulationContext during initialization. Two codes will also be loaded: one will be validated, and the second will be marked as pending validation. Two programs (Mirror) will also be created. One program will have an uninitialized stateHash and others, and the second will have an initialized stateHash. Basically, all these variations are needed to measure different gas cases (cold slot, hot slot, etc.). The instrumented version for the Router and Mirror smart contacts differs from the regular one in that it uses the event DebugEvent(uint256 indexed topic0) anonymous. Since the event is anonymous, this allows calling opcode log1(offset=0, size=0, topic0=some_debug_const) directly from Solidity, avoiding inline assembly. Next, we use the revm::Inspector API to measure difference between two log1 opcodes and thus calculate the execution cost for a fixed section of code that is bounded by topics.

@StackOverflowExcept1on StackOverflowExcept1on added the A1-inprogress Issue is in progress or PR draft is not ready to be reviewed label Feb 12, 2026
@semanticdiff-com
Copy link

semanticdiff-com bot commented Feb 12, 2026

Review changes with  SemanticDiff

Changed Files
File Status
  ethexe/ethereum/abi/BatchMulticall.json  100% smaller
  ethexe/ethereum/abi/TransparentUpgradeableProxy.json  100% smaller
  ethexe/ethereum/abi/POAMiddleware.json  98% smaller
  ethexe/ethereum/abi/Mirror.json  97% smaller
  ethexe/common/src/db.rs  19% smaller
  .github/workflows/check.yml  0% smaller
  Cargo.lock Unsupported file format
  Cargo.toml Unsupported file format
  Makefile Unsupported file format
  ethexe/common/src/gear.rs  0% smaller
  ethexe/contracts/foundry.toml Unsupported file format
  ethexe/contracts/remappings.txt Unsupported file format
  ethexe/contracts/src/MirrorWithInstrumentation.sol Unsupported file format
  ethexe/contracts/src/RouterWithInstrumentation.sol Unsupported file format
  ethexe/ethereum/Cargo.toml Unsupported file format
  ethexe/ethereum/abi/Middleware.json Unsupported file format
  ethexe/ethereum/abi/MirrorWithInstrumentation.json  0% smaller
  ethexe/ethereum/abi/Router.json Unsupported file format
  ethexe/ethereum/abi/RouterWithInstrumentation.json  0% smaller
  ethexe/ethereum/abi/WrappedVara.json Unsupported file format
  ethexe/ethereum/src/abi/mod.rs  0% smaller
  ethexe/ethereum/src/benchmarking/context.rs  0% smaller
  ethexe/ethereum/src/benchmarking/contracts/mirror_impl.rs  0% smaller
  ethexe/ethereum/src/benchmarking/contracts/mod.rs  0% smaller
  ethexe/ethereum/src/benchmarking/contracts/router.rs  0% smaller
  ethexe/ethereum/src/benchmarking/contracts/router_impl.rs  0% smaller
  ethexe/ethereum/src/benchmarking/contracts/wvara.rs  0% smaller
  ethexe/ethereum/src/benchmarking/extensions.rs  0% smaller
  ethexe/ethereum/src/benchmarking/inspector.rs  0% smaller
  ethexe/ethereum/src/benchmarking/mnemonic.rs  0% smaller
  ethexe/ethereum/src/benchmarking/mod.rs  0% smaller
  ethexe/ethereum/src/lib.rs Unsupported file format
  ethexe/ethereum/src/main.rs  0% smaller
  utils/gear-workspace-hack/Cargo.toml Unsupported file format

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @StackOverflowExcept1on, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces comprehensive gas benchmarking infrastructure within the ethexe/contracts module, specifically targeting the Router.sol and Gear.sol smart contracts. It defines numerous constant markers and integrates debug events and gas cost comments into key functions like commitBatch and its helpers, as well as validateSignaturesAt. This enhancement aims to provide granular insights into gas consumption at various stages of batch commitment and signature validation processes, which is crucial for performance optimization and cost analysis. Additionally, it includes minor dependency path adjustments and a CLI argument fix.

Highlights

  • Gas Benchmarking Infrastructure: Added numerous uint256 constants to Router.sol and Gear.sol to define specific benchmark points within critical functions for gas analysis.
  • Detailed Gas Usage Comments: Integrated DebugEvent emissions and gas usage comments into commitBatch and its helper functions in Router.sol, as well as validateSignaturesAt in Gear.sol, to provide granular gas consumption insights.
  • CLI Argument Fix: Corrected a requires argument in ethexe/cli/src/commands/tx.rs from rpc-url to rpc_url for the injected flag.
  • Dependency Remappings Update: Updated library remappings in ethexe/contracts/remappings.txt to reflect correct paths for ds-test, erc4626-tests, and halmos-cheatcodes.
Changelog
  • ethexe/cli/src/commands/tx.rs
    • Fixed a requires argument from rpc-url to rpc_url for the injected flag.
  • ethexe/contracts/remappings.txt
    • Updated library remappings for ds-test, erc4626-tests, and halmos-cheatcodes to point to correct paths within lib/openzeppelin-contracts/.
  • ethexe/contracts/src/Router.sol
    • Added a large set of uint256 internal constant variables to mark specific 'BEFORE' and 'AFTER' points for gas benchmarking within the commitBatch function and its internal helper functions (_commitChain, _commitCodes, _commitTransitions).
    • Introduced a DebugEvent to emit these benchmark points.
    • Inserted comments detailing estimated gas usage at various stages within commitBatch and its helpers.
  • ethexe/contracts/src/libraries/Gear.sol
    • Added a large set of uint256 internal constant variables to mark specific 'BEFORE' and 'AFTER' points for gas benchmarking within the validateSignaturesAt function.
    • Introduced a DebugEvent to emit these benchmark points.
    • Inserted comments detailing estimated gas usage at various stages within validateSignaturesAt.
Activity
  • No specific human activity (comments, reviews) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces extensive changes to add gas benchmarking capabilities to the smart contracts. This is achieved by adding numerous constants and commented-out DebugEvent emissions at various points in the code to measure gas usage between them. The changes are consistent with the goal of benchmarking.

My review has identified a couple of minor issues in the benchmarking code itself, specifically swapped constant values for BEFORE/AFTER event pairs. I've also noted a TODO comment that suggests some uncertainty in the logic.

For better maintainability and to avoid errors, I would suggest centralizing all the benchmark-related constants into a single, dedicated library file (e.g., BenchmarkEvents.sol). This would make them easier to manage and verify.

@grishasobol grishasobol added the D8-ethexe ethexe-related PR label Feb 21, 2026
fn ensure_types_unchanged() {
const EXPECTED_TYPE_INFO_HASH: &str =
"8a30677f0819eeffd02548e0e91adf940fa39173e509ad24b50cbb92ce61f907";
"1daa6179efabf521b9087edfd0244a64250abc27a9b93255a441e736fb2a6760";
Copy link
Member Author

Choose a reason for hiding this comment

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

Just changing comment in the code changes TypeInfo???

@StackOverflowExcept1on StackOverflowExcept1on marked this pull request as ready for review March 6, 2026 14:18
@StackOverflowExcept1on StackOverflowExcept1on added A0-pleasereview PR is ready to be reviewed by the team and removed A1-inprogress Issue is in progress or PR draft is not ready to be reviewed labels Mar 6, 2026
@StackOverflowExcept1on StackOverflowExcept1on changed the title feat(ethexe/contracts): add gas benchmarks feat(ethexe/contracts): add initial gas benchmarks Mar 6, 2026
@StackOverflowExcept1on StackOverflowExcept1on changed the title feat(ethexe/contracts): add initial gas benchmarks feat(ethexe/ethereum): add initial gas benchmarks Mar 6, 2026
@StackOverflowExcept1on
Copy link
Member Author

/gemini review

@@ -0,0 +1,138 @@
// This file is part of Gear.
Copy link
Member Author

Choose a reason for hiding this comment

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

will be removed, just to demonstrate API

cargo run --package ethexe-ethereum --bin ethexe-ethereum

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a comprehensive gas benchmarking framework for ethexe/ethereum using revm, adding instrumented versions of the Router and Mirror smart contracts for fine-grained gas measurement. It sets up a mock EVM environment with SimulationContext and uses revm::Inspector for gas measurement. A high-severity Denial of Service (DoS) vulnerability was identified in the Mirror contract due to unrestricted gas consumption in external Ether transfers, which could block protocol-wide batch commitments. Additionally, a medium-severity issue exists where the event blacklist for log spoofing protection is incomplete, potentially allowing the spoofing of failure events. There are also suggestions to improve code maintainability by removing dead code and reducing duplication.

Comment on lines +522 to +528
function _transferEther(address destination, uint128 value) private returns (bool) {
if (value != 0) {
(bool success,) = destination.call{value: value}("");
return success;
}
return true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

security-high high

The _transferEther function performs an external Ether transfer using a low-level call without specifying a gas limit. If the destination is a malicious contract that consumes all gas (e.g., via an infinite loop), it will cause the sub-call to consume 63/64 of the remaining gas. When this function is called within loops (such as in _claimValues on line 468) or sequences, the cumulative gas loss can cause the entire commitBatch transaction to revert with an 'Out of Gas' error. Since batches must be committed in order, this allows an attacker to block the entire protocol's progress, leading to a Denial of Service (DoS).

Recommendation: Implement a gas limit for external Ether transfers (e.g., destination.call{value: value, gas: 3000}("")) or adopt a 'pull' pattern where users must withdraw their funds instead of the contract 'pushing' them.

Comment on lines +377 to +383
if (!(topic1 != StateChanged.selector && topic1 != MessageQueueingRequested.selector
&& topic1 != ReplyQueueingRequested.selector && topic1 != ValueClaimingRequested.selector
&& topic1 != OwnedBalanceTopUpRequested.selector
&& topic1 != ExecutableBalanceTopUpRequested.selector && topic1 != Message.selector
&& topic1 != Reply.selector && topic1 != ValueClaimed.selector
&& topic1 != TransferLockedValueToInheritorFailed.selector && topic1 != ReplyTransferFailed.selector
&& topic1 != ValueClaimFailed.selector)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

security-medium medium

The event blacklist in _tryParseAndEmitSailsEvent is incomplete. It fails to include the MessageCallFailed and ReplyCallFailed event selectors. An attacker could potentially exploit this to spoof these events, tricking off-chain components into incorrectly processing message or reply failures.

Recommendation: Add MessageCallFailed.selector and ReplyCallFailed.selector to the blacklist check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A0-pleasereview PR is ready to be reviewed by the team D8-ethexe ethexe-related PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ethexe: implement initial benchamarking of gas consumptions for batch committing

2 participants