2

According to this spec, setting a state-variable to the same value consumes 200 gas.

However, I conducted a test over ganache-core v2.10.2, and the result was closer to 800 gas:

Solidity Contract:

pragma solidity 0.6.12;

contract MyContract {
    uint256 public gasUsed;
    uint256 public storageSlot;
    function func(uint256 x) public {
        storageSlot = x;
        uint256 gasLeft = gasleft();
        storageSlot = x;
        gasUsed = gasLeft - gasleft();
    }
}

Truffle 5.x Test:

const MyContract = artifacts.require("MyContract");

contract("MyContract", () => {
    it("test", async () => {
        const myContract = await MyContract.new();
        for (let x = 0; x < 10; x++) {
            await myContract.func(x);
            const gasUsed = await myContract.gasUsed();
            console.log(gasUsed.toString());
        }
    });
});

The printout is 816 for every iteration, and assuming that the gasleft() operation in the last line of the contract function costs 16 gas, storing the same value appears to cost 800 gas.

I suspect that gas cost in the spec applies to older EVM versions (prior Istanbul or something like that), where SLOAD used to cost 200 gas.

In the current EVM version, SLOAD has changed to 800 gas, so I believe that the case of SSTORE of the same value may have changed to 800 gas as well.

Does anyone have any idea about this discrepancy?

2

This cost of a non-changing SSTORE was changed to 800 gas as proposed by EIP-2200 and EIP-1884 (which I can't find anywhere for some reason). I suppose the 200 value is there for backwards compatibility. If you search in your spec you'll find the SloadGasEIP1884 and SloadGasEIP1884 values. When a new node syncs and verifies all transactions it also has to be able to understand old consensus rules which is likely why it's included there. Personally it might also just be old non-refactored code, I'm not too familiar with the geth code base.

For the future I'd recommend the remix web IDE for checking and debugging low level details such as gas cost. Remix has the best debugger I know of, allowing you to step through the execution of every op code individually.

7
  • This answer by "@eth" (which also refers to the accepted answer on the same post) suggests otherwise. I'm still waiting for @eth response in order to determine what is the correct answer here (i.e., whether it is 200 or 800 or 200 on mainnet and 800 on ganache). Thanks for now. – goodvibration Nov 21 '20 at 9:28
  • 1
    I tested myself, it's 800 – MrClottom Nov 21 '20 at 9:30
  • Yes, but I'm guessing - just like me - you tested it on ganache, right? And perhaps ganache doesn't implement the standard correctly in this specific aspect. – goodvibration Nov 21 '20 at 9:31
  • 1
    On ropsten the cost is also 800 – MrClottom Nov 21 '20 at 11:23
  • 1
    Sorry for the delay on this. Thank you both for your corrections! (I notice I'm still the only one who has upvoted the question.) – eth Dec 13 '20 at 8:06

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.