foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.31k stars 1.75k forks source link

gas report reports 0 size and gas for factory deployed contracts #9300

Closed sakulstra closed 12 hours ago

sakulstra commented 1 day ago

Component

Forge

Have you ensured that all of these are up to date?

What version of Foundry are you on?

forge 0.2.0 (d2ed15d 2024-11-04T00:27:08.261921000Z)

What command(s) is the bug in?

No response

Operating System

macOS (Apple Silicon)

Describe the bug

When a contract is deployed by another contract it's method gas cost will show up, but size is reported as zero.

Reproduction: Parent deploying child and test calling child.w().

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import 'forge-std/Test.sol';

contract Child {
  function w() external {}
}

contract Parent {
  Child public immutable child;

  constructor() {
    child = new Child();
  }
}

contract NestedDeploy is Test {
  function test_gas() external {
    Parent p = new Parent();
    p.child().w();
  }
}

Yields:

| test/NestedDeploy.t.sol:Child contract |                 |       |        |       |         |
|----------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost                        | Deployment Size |       |        |       |         |
| 0                                      | 0               |       |        |       |         |
| Function Name                          | min             | avg   | median | max   | # calls |
| w                                      | 21161           | 21161 | 21161  | 21161 | 1       |

| test/NestedDeploy.t.sol:Parent contract |                 |     |        |     |         |
|-----------------------------------------|-----------------|-----|--------|-----|---------|
| Deployment Cost                         | Deployment Size |     |        |     |         |
| 143072                                  | 385             |     |        |     |         |
| Function Name                           | min             | avg | median | max | # calls |
| child                                   | 181             | 181 | 181    | 181 | 1       |

I would expect deployment size & cost to be reported correctly.

grandizzy commented 1 day ago

should probably record contract_info gas and size before returning here

https://github.com/foundry-rs/foundry/blob/4817280d96e0e33a2e96cf169770da60514d1764/crates/forge/src/gas_report.rs#L97-L106

grandizzy commented 17 hours ago

@sakulstra pls check comment here https://github.com/foundry-rs/foundry/pull/9301#issuecomment-2472784914 For accurate gas reports you should create dedicated test for nested contracts, we could improve docs to reflect this. Does this make sense? thanks!