1

contract A { 
      
    uint x = 10;
    
    function test2() public {
        x = 0;
    }
          
}

I can see that:

  • Transaction cost is 13204
  • Execution cost is 5136

How can this be ? I calculate differently.

Those 3 are taken from white paper.

  • Paid for every transaction - 21000
  • Paid for an SSTORE operation when the storage value’s zeroness remains unchanged or is set to zero. - 5000
  • Refund given (added into refund counter) when the storage value is set to zero from non-zero - 15000

So, 21000 + 5000 - 15000 = 11000. How did we get 13204 ?

1 Answer 1

1

There's a cap to the maximum refundable gas. Only half of the total used gas can be refunded. (*)

In the example at most you can refund (21000 + 5000) / 2 = 13000 gas.

So it will be at 21000 + 5000 - min(13000, 15000) = 26000 - 13000 = 13000. Very close to the measured amount.

(*) The exact details are in Ethereum's Yellow Paper Section 6.2 Execution.

3
  • Hi Ismael @Ismael, I wanted to hear your opinion on this. Let's say the gas transaction needed is 21000+3+3+5000 = 26006, but the refund of the operation is 15,000. Now, we can't get 15,000, since it exceeds half the amount, so we get only 26003/2 = 13003 as refund. I know that this is because if we give 15,000 refund in this case, then miners will not be incentivized, but it really doesn't make sense. If we make the refund as 15,000, miner still gts 26006-15000 = 11006 , So, he still gets money. why not incentivized ? Jan 29, 2021 at 21:02
  • @NikaKurashvili I don't know why GasRefund <= GasUsed/2 is used. It seems easy to calculate though, but I don't know if there was another reason. Certainly allowing to refund GasUsed opens the possibility of DoS attacks.
    – Ismael
    Jan 30, 2021 at 1:21
  • 1
    The actual reason is that because if you dont , then miners are not incentivized. I couldnt understand though how it is related Jan 30, 2021 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.