I'm new to solidity but a question comes to my mind. Say I have this example dummy contract.

contract TestLoop {

    uint8 public blockNumber;
    uint8 public counter= 0;

    event LoopCalled(address addrr, uint8 blockNumber, uint8 counter);

    constructor() public {

    function runLoop() public {
        blockNumber = uint8(block.number);

        for (uint8 i = 0; i < blockNumber; i++) {
            emit LoopCalled(msg.sender, blockNumber, counter);

As you can see, inside the contract I'm using a (pressumable) inmutable variable such as block.number.

How does calling eth_estimateGas on this method (runLoop) work? Shouldn't it always return the same since once mined the block.number stays the same?

How do this edge cases where inside your contract you use a variable that depends on the network work?


As the name suggests, the eth_estimateGas does its best to estimate the gas usage.

It does this by executing the transaction locally, in a local call. This kind of calls do not generate a real transaction to the blockchain, but are only executed in your local node (or the node of your node provider, if you use one). Since these calls are not propagated to the blockchain, they also can't modify the contract state, so they are read-only calls.

What will probably happen is that the estimation checks its current information and acts based on that. As for block.number, I guess it just uses the highest block number currently known to it (since the call is not part of any block). Sometimes the estimator will simply return totally shitty results, and some clients understand to even warn the use about that. Ultimately, it's the user's task to adjust the gas limit accordingly, although that's rather difficult to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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