3

The two transactions:

a) https://etherscan.io/tx/0x9227f7b00bf2c98a7e16eb5ab90c04df5b49698297ee7f52c765d5218af395ad
b) https://etherscan.io/tx/0x1af76244391e06c375e8511f4140191fb874362b238aa9b6fdacc072e35cd778

are exactly the same but as you can see TX (a) failed as it used up all its available gas. The limit was set to 53,356. The second TX (b) is exactly the same but gas limit was set to 160,000 and the transaction succeeded. Although it used only 38,368 gas which is less than the limit of TX (a). But before I get answers like "some instructions take a different amount of gas depending on XYZ" I checked the execution of the contracts:

a) https://etherscan.io/vmtrace?txhash=0x9227f7b00bf2c98a7e16eb5ab90c04df5b49698297ee7f52c765d5218af395ad
b) https://etherscan.io/vmtrace?txhash=0x1af76244391e06c375e8511f4140191fb874362b238aa9b6fdacc072e35cd778

They seem to be doing the exact same thing with the exact same amounts of gas used. So why did the first transaction fail? And why does is say there's only 31700 gas to start with on TX (a)?

5
  • 1
    The cost also depends on any state variable being changed during the transaction. For example, changing a uint slot from zero to non-zero costs 20K, changing it from non-zero to non-zero costs 10K, and changing it from non-zero to zero actually refunds you 5K (conditioned to the fact that the refund does not exceed half of your transaction cost). Sep 18, 2020 at 17:31
  • From the gas limit of 53356 you have to pay 21000 for the transaction plus some extra for the input data. Another thing to consider is gas refunds, thery are accounted at the end of the transaction.
    – Ismael
    Sep 18, 2020 at 18:21
  • @Ismael why does the extra 21k not show in etherscan?
    – Majster
    Sep 24, 2020 at 6:23
  • @goodvibration but looking at instructions in the execution of the contract everything looks exactly the same.
    – Majster
    Sep 24, 2020 at 6:24
  • @Majster I don't know, perhaps Etherscan doesn't know how to display it. If you substract gas limit minus starting gas available, you get 21656, which is close to 21k (the rest should be input data cost).
    – Ismael
    Sep 24, 2020 at 12:57

1 Answer 1

1

I dont know the answer but I can provide some clues.

Both transactions are combined of the same steps, but the total gas is different.

You can see steps in:

fail: https://etherscan.io/vmtrace?txhash=0x9227f7b00bf2c98a7e16eb5ab90c04df5b49698297ee7f52c765d5218af395ad

success: https://etherscan.io/vmtrace?txhash=0x1af76244391e06c375e8511f4140191fb874362b238aa9b6fdacc072e35cd778

And I made a google sheet to show the total gas consume step by step:

https://docs.google.com/spreadsheets/d/1QHRrWwf7ITzeuwBTfedqe3-NpjU6fLr-JcoTqeAg8e4/edit#gid=0

At step 436, both costs 31700. The success one finally cost 31712 at step 440. Then the total of success one should be 31712 + (160000 - 138344) = 53368. But actually it only cost 38368. The diff is 15000.

Maybe you can learn something from What is storage_location in terms of SSTORE?

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.