10

I've reviewed several related SE questions on this topic and I haven't seen an answer. In Remix, I've created two separate .sol files. The files are names file1.sol and file2.sol. The code for the two files looks like this:

file1.sol

pragma solidity ^0.4.0;

contract MyFirstContract {

}

file2.sol

pragma solidity ^0.4.0;

contract MyOtherContract {

}

When I review the transaction and execution costs, I'm slightly confused. The part that cons

          | transaction cost | execution cost |
file1.sol | 68926 gas        | 10862 gas      |
file2.sol | 68990 gas        | 10862 gas      |

Why are the transaction costs different when the contracts are virtually the same? At first, I suspected there was an added space or something else in the contract. However, I verified that wasn't the case. Why are there different transaction costs across these two contracts?

  • I wasn't able to reproduce such behavior. Current version in remix.ethereum.org always states tx cost: 68990 gas, execution cost: 10862 gas. Independently of file name or contract name. – Ismael Sep 17 '17 at 18:51
  • like @Ismael i can't also reproduce your results for the first file, i get 68990 for both. Check if you haven't changed the compiler while you were trying. i propose that you past the opcodes instructions generated by the compiler(debug mode). – Badr Bellaj Sep 17 '17 at 19:24
  • I just tried again and I was able to confirm the values that I entered. I'm still seeing the differing gas costs. – Some User Sep 18 '17 at 13:09
7
+50

I can reproduce this with compiler version 0.4.12+commit.194ff033.Emscripten.clang (but, in my case, the other way round - file1.sol has a deployment cost of 68990, file2.sol has a deployment cost of 68926.)

There is a 64 gas difference in the cost between the two deployments. The reason for this is that, although the deployment code is exactly the same length (82 bytes), one of the sets of bytecodes has a zero byte in a place where the other has a non-zero byte.

When transmitting a transaction to the blockchain, the gas cost per byte of calldata is 68 for non-zero bytes, and 4 for zero bytes (Appendix G of the Yellow paper, G_txdatanonzero vs. G_txdatazero). Hence the 68 - 4 = 64 gas difference between your codes.

The different byte actually occurs in the appended contract metadata section, not in the deployment code or the deployed code itself which are identical for both your examples as you would expect. The following are the metadata sections for the contracts - the metadata is generated automatically by the compiler and comprises a Swarm hash of the contents of the source file plus some other bits and pieces:

a165627a7a72305820176535c2c0a4a3ac4a26111bf3f9aa4ab0fa5db14d592f8574bf15f1f42f46420029
a165627a7a723058204e0a27613ab151031db65041c0e2ff84bf88842fd7b2cc00163bc9af9acd05230029
                          zero/non-zero byte difference is here ^^

So, with two near identical source files you have some chance of there being a difference in deployment cost due to this, and it is compiler version dependent since the compiler version is embedded in the data used to create the Swarm hash.

  • Thank you for your response. Can you tell me, is transaction cost related to the size of the contract text (i.e. the code)? Or is the transaction cost determined by the size of the compiled contract? I assume the latter. However, I'm not 100% certain. – Some User Sep 18 '17 at 13:13
  • In this case, the size of the compiled contract (the bytecode). Your findings above relate to the deployment of the contract, i.e. the transaction that sends the contract bytecode to the blockchain and stores it there. Subsequent transactions that call methods on the contract are unrelated to the contract's size. – benjaminion Sep 18 '17 at 13:38

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.