2

I have this contract

contract Contract {
    function a() external {
        assembly {
            return(0, 1000000)
        }
    }
}

I am calculating the gas cost for a call to a. Following https://github.com/wolflo/evm-opcodes/blob/main/gas.md#a0-1-memory-expansion, I have roughly:

gas = (1e6/32)^2/512 + (1e6/32)*3 ~ 2M

Running the transaction via hardhat returns a gas of ~4M. Why is it double? I also changed the size in the return, and confirmed the quadratic behavior, but there is this factor 2 I can't understand.

1 Answer 1

2

I tried to deploy an instance of the contract and the calculation is correct, i.e. gas used is ~2M.

The 4M is explained in this way: I was testing the contract via HardHat and deploying it via hardhat-upgrades, so with a Transparent Proxy... in this case there's a factor 2 involved as the result is copied into the proxy context after the delegatecall!

Refs: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/4e7e6e54daedf091d91f2f2df024cbb8f253e2ef/contracts/proxy/Proxy.sol#L34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.