-
Notifications
You must be signed in to change notification settings - Fork 683
Incorrect gas cost for SSTORE opcode when using fork feature #625
Comments
@nebojsa94 do you have a project that I can recreate this with? I'll give it a shot myself, but my hunch is that I'm not going to be able to reproduce since gas costs are pretty transparent (they're calculated upstream in https://github.com/ethereumjs/ethereumjs-vm) Do you only observe this during forking (you've tested it when not forking and see it provides the expected value)? Remember that SSTORE gas costs also costs 5k if the zeroness remains unchanged; I gotta do some more research on what what bit-level this pertains to, however if it's at the nibble level then changing |
Hey @seesemichaelj, thank you for the quick reply!
You can recreate with an empty truffle project using
I've tested in a nonfork environment and both transactions are spending 15k more gas than in a fork environment (contract creation: 210453 -> 225453, migration saving: 27363-> 42363). Using
This is the case with the newly deployed contract, so every storage slot is empty (0x0000000000000000000000000000000000000000000000000000000000000000), and since the owner of the contract that is set during Migration contract creation is never 0x0000000000000000000000000000000000000000, the |
Awesome, thanks @nebojsa94 for the clarification |
Hey @seesemichaelj, Wanna team-up on resolving this issue? |
Hey @nebojsa94! I'm out of office until Oct 23rd, so plenty of time for you to do some initial investigation :) I definitely would appreciate the help. I may be moving off of forking bugs in the coming weeks |
Hey @nebojsa94, I'm back in the office and wanted to touch base to see if you got anywhere with this? |
Hey @seesemichaelj, I started looking into this during the weekend, hopefully, I will have a proposal by the end of tomorrow. |
awesome, thanks! |
Hey @seesemichaelj, sorry for the delays, It was pretty hard to find the actual issue that led to the incorrect We can use the test in forking.js on line 179 with modified Example.sol where the StateManager is responsible for storage fetching during the execution of the transaction, this line will call ForkedStorageTrie implementation which is going to invoke web3.eth.getStorageAt that is going to return The fix should be pretty straight forward, just make sure |
This is still not fixed. I encountered the issue also on 2.13.2. Could this be reopened @davidmurdoch? |
Hey @sz-piotr, can you provide steps for reproduction? |
This is the contract that I used. pragma solidity ^0.7.0;
contract GasTest {
event GasUsed(uint256 value);
uint256 public x = 0;
function set (uint value) public {
x = value;
}
function setNonZero() public {
uint256 start = gasleft();
set(1337);
uint256 used = start - gasleft();
emit GasUsed(used);
}
} In the tests i forked mainnet from infura and checked the logs for I am very sorry that I cannot provide a full working example for you. |
I can confirm that the issue is still present if the state variable is set to the default value in the declaration statement; in this case, if you remove |
We're currently revamping forking for the upcoming Ganache v7.0.0 release. Adding this to our plate (David's plate) for inclusion there. Thanks David ❤️ And thanks everyone for reporting / getting to the bottom of this! |
Executing transactions that use SSTORE opcode costs less gas when using fork feature.
Expected Behavior
SSTORE that sets the value
v
in the slots
should cost 20k gas when the current value in slots
is0x0000000000000000000000000000000000000000000000000000000000000000
andv
is not0x0000000000000000000000000000000000000000000000000000000000000000
.Current Behavior
SSTORE mentioned above costs 5k gas.
Possible Solution
This is probably an issue with the gas calculator, SSTORE should cost 5k gas when the current value in the slot is not
0x0000000000000000000000000000000000000000000000000000000000000000
Steps to Reproduce (for bugs)
Your Environment
The text was updated successfully, but these errors were encountered: