-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Non deterministic gas usage of the same function on truffle development server #1012
Comments
@boorac Just for clarification, are you connecting to ganache-cli? If so which version? I believe there's been a known problem in the ethereumjs-vm with deleting storage (e.g. setting it to zero) - refunds haven't been handled gracefully. Might have been fixed recently though. @seesemichaelj Does this issue ring any bells for you? |
@boorac What is likely happening here is you're receiving a refund from the EVM. There are two reasons for a refund (below are excerpts from the EVM Yellow Paper):
It looks like Your transaction is going to take X gas to execute, at the end of the executing the transaction, if you qualify for refunds, the account is refunded the gas. Unfortunately, it's reported as "spending 24721 gas" rather than reported as "consumed 39721 gas and received a refund of 15000 gas, netting your account 24721 gas used." I would hypothesize that if you set your gas limit to 39721, it would succeed. I'm not sure if anything is actually being done to handle this more gracefully. Hope this helps clear things up. |
With all that said, I'm not sure if/where the issue is. Some questions to ponder:
|
@cgewecke I'm using the command @seesemichaelj I understand the issue better now, thank you. Regarding Metamask and eth_estimateGas, I suspect that Metamask uses some kind of heuristic since every time I attempt to call a function which will be reverted the gasLimit in the transaction is automatically set to 0. I will try sniffing out the traffic using wireshark and report back. Also, if Metamask does indeed use eth_estimateGas, I'm wondering why is it making the wrong estimation, that is, chooses an incorrect gas limit. Is eth_estimateGas deterministic in nature? |
@boorac Yes It looks like MetaMask runs estimateGas and adds a gas buffer of 50% or runs the tx at 90% of the block gas limit if the buffer is too high. |
Per @cgewecke's comment, this means that trufflesuite/ganache#26 is causing the issue you're seeing @boorac |
@boorac I'm going close here and we'll track this over at ganache-core 26 |
Environment
Issue
I'm calling the
claimRefund
function on my smart contract using metamask. When calling the function with 37081 gas limit at 20 gwei gas price, the transaction spends 36924 gas and gets reverted with EVM Revert.If I change params to 58080 gas limit at 20 gwei gas price, the transaction passes spending 24721 gas.
The code is shown below:
Question
How is it possible that the same call results in a different gas spent amount? Also, how can a function fail after spending more gas and pass after spending less gas?
The text was updated successfully, but these errors were encountered: