1

Suppose we have a contract with the following defined function:

   function send(address receiver, uint amount) public {
        if (balances[msg.sender] < amount) return;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        emit Sent(msg.sender, receiver, amount);
    }

and suppose that sender runs out of gas just after the following line:

balances[msg.sender] -= amount;

What happened with the state variables? Are incomplete tx included in the block or not?

Shane Fontaine
  • 2,431
  • 3
  • 13
  • 23
Federico Caccia
  • 1,817
  • 1
  • 13
  • 33
  • What do you mean by "runs out of gas"? Is that a Solidity thing or are you using a turn of phrase? – Makoto Nov 28 '18 at 20:12

2 Answers2

2

If you run out of gas in the middle of a transaction it will fail. You will only pay for the computation used, meaning that all the gas used until it failed will not be returned, but the rest will.

You can read more about gas in this chapter of the Ethereum Book

1

A transaction that runs out of gas will fail and none of the state variables will be updated. Failed transactions are still included in a block, as you can see in this out of gas example.

In your example, balances[msg.sender] -= result will not be executed, and balances[msg.sender] will remain the exact same as before the transaction.

The sender of the transaction will still pay a fee to the miner for including the transaction in the block.

This post does a good job of walking through various failure scenarios.

Shane Fontaine
  • 2,431
  • 3
  • 13
  • 23