Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas for Execution Cost doesn't add up #163

Closed
tomw1808 opened this issue Dec 26, 2017 · 5 comments
Closed

Gas for Execution Cost doesn't add up #163

tomw1808 opened this issue Dec 26, 2017 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@tomw1808
Copy link

Considering the following contract:

pragma solidity ^0.4.19;
contract A {
    
    function testme() public{
        
    }
}

It uses gas for input (transaction cost): 4*68 = 272 + 21000 = 21272 - this is correct in Remix. But I cannot verify the execution cost:

It uses the following instructions according to the debugger:

Gas Instruction
3 000 PUSH1 60
3 002 PUSH1 40
3 004 MSTORE
3 005 PUSH1 04
2 007 CALLDATASIZE
3 008 LT
3 009 PUSH1 3f
10 011 JUMPI
3 012 PUSH1 00
3 014 CALLDATALOAD
3 015 PUSH29 0100000000000000000000000000000000000000000000000000000000
3 045 SWAP1
5 046 DIV
3 047 PUSH4 ffffffff
3 052 AND
3 053 DUP1
3 054 PUSH4 7fdcf8c4
3 059 EQ
3 060 PUSH1 44
10 062 JUMPI
1 068 JUMPDEST
2 069 CALLVALUE
3 070 ISZERO
3 071 PUSH1 4e
10 073 JUMPI
1 078 JUMPDEST
3 079 PUSH1 54
3 081 PUSH1 56
8 083 JUMP
1 086 JUMPDEST
8 087 JUMP
1 084 JUMPDEST
0 085 STOP

Which is in sum 119 gas, but the instruction details in the Remix logger shows 128.

status 0x1 Transaction mined and execution succeed
from 0xca35b7d915458ef540ade6068dfe2f44e8fa733c
to A.testme() 0x692a70d2e424a56d2c6c27aa97d1a86395877b3a
gas 3000000 gas
transaction cost 21400 gas
execution cost 128 gas
hash 0xccca4e36cc65d4856f556ad56414848b82d6eccd9ea7a66dfa76e194042e555c
input 0x7fdcf8c4
decoded input {}
decoded output {}
logs []
value 0 wei

The instruction cost is the same as shown in Appendix G in the Yellowpaper, this is something I could verify. But I cannot verify where the 9 gas are coming from.

@tomw1808
Copy link
Author

tomw1808 commented Dec 29, 2017

I found what causes the difference between the values shown and the values calculated: MSTORE is marked as "W_verylow" in the Yellowpaper (3 gas), shows 3 gas in the remix debugger, but takes up 12 gas on chain. I have released a similar contract on rinkeby, its's the third instruction and pretty obvious: http://rinkeby.etherscan.io/remix?txhash=0xb837a742727706e9b8f57f33477d3b5dfd3959f705f15f041c1ed6357178917b

I'd be very happy if someone can explain it to me before closing the issue. Thank you.

@yann300
Copy link
Contributor

yann300 commented Jan 2, 2018

That's a bug in Remix:
The cost of MSTORE is 3 gas, but the general cost if the step can be higher if the memory needs to be extended.

@yann300 yann300 self-assigned this Jan 11, 2018
@seandotau
Copy link
Contributor

Thanks for this @yann300 . Could you elaborate under what situation would the memory need to be extended? Is it a linear increase as well? Just curious.

@LianaHus LianaHus transferred this issue from ethereum/remix-ide Jul 28, 2020
@LianaHus LianaHus added the bug Something isn't working label Jul 28, 2020
@yann300
Copy link
Contributor

yann300 commented Feb 9, 2023

this is has been fixed for sometimes.
Sorry for not responding here.
you'll find the information here: https://github.com/wolflo/evm-opcodes/blob/main/gas.md#a0-1-memory-expansion
The following opcodes incur a memory expansion cost in addition to their otherwise static gas cost: RETURN, REVERT, MLOAD, MSTORE, MSTORE8.

@yann300 yann300 closed this as completed Feb 9, 2023
v0.31.0 - Feature Freeze 15/02 - Release 27/02 automation moved this from To do to Done Feb 9, 2023
@tomw1808
Copy link
Author

tomw1808 commented Feb 9, 2023

@yann300 appreciate it. I already forgot about the issue, been 5 years. Again, really appreciate getting back to it and referencing more information here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Development

No branches or pull requests

4 participants