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?