0

I'm trying to understand various aspects of the micro-process of a transaction such as how msg.sender's balance is changed prior to and after a transaction is mined. Is the balance updated after the block is mined or after a transaction is processed? If two transactions in the same block request for the balance of one of the two senders, would they get the same number? To be more specific, suppose TX 1: A->B, TX 2: A->C, and in both TX 1&2, the balance of A is read as some input parameter. Thanks.

2 Answers 2

2

The balance is changed after a transaction is processed on a miner's side, so the miner can work on other transactions (even from the same user) with the balance of all entities involved in the transaction updated. Otherwise, it wouldn't be possible to make two transactions in one block, and even more - it will be an obstacle for two users to interact with the same smart contract in one block.

And only after the block is mined and the new block is spread out on the network, the balance is changed in the whole network and all users as the result of their transactions.

2
  • That said, the balance of A is somehow unpredictable in the eyes of B and C, since it is at the will of miners. If TX 1 has a higher gas, B would read the balance of A first? Is it deterministic or mutable subject to other factors?
    – Maxareo
    Jul 14, 2021 at 15:47
  • Yes, you're right. Then B would read the balance of A first. But there are other criterias apart from gas price. MEV is now probably the most important problem of the Ethereum, because miners can place their transactions (or transactions of people who would buy the places on MEV auctions) before or after others in a way that would make them profit (and often a loss for the ordinary users). So the order only depends on the miner. If you have a friend miner then you could ask him to pick your transaction first, for example. Jul 14, 2021 at 19:46
2

The transactions are processed sequentially in a block. The process look like this for each transaction:

  • Check that the balance of the sender has enough funds to pay for transaction and send the Value
  • Buy gas
  • Call corresponding contract or make a transfer. If it is a contract, then sequentially each balance is modified according to the control flow
  • Refund any gas if present
  • Pay the miner the transaction fee (this goes last because of possible gas refunds)

then another transaction begins. So, there is no chance for a transaction to somehow mess up the balances

4
  • Does that mean in the same block, it is guaranteed that the balance of A would be different to B and C?
    – Maxareo
    Jul 14, 2021 at 15:51
  • we dont know would they be different or not , that will depend on the values sent and the code of the contract
    – Nulik
    Jul 14, 2021 at 15:56
  • Let's simplify the question by assuming both TX 1 & 2 have significant gas costs. That means, after either transaction of the two, balance of A would be changed for sure. Then, seeing from one level higher, balance of A would always be read as different values by B and C, correct?
    – Maxareo
    Jul 14, 2021 at 16:00
  • like I said, values will depend on the input. And as for seeing, everybody will see the transactions when block is finished and published, not during block making process.
    – Nulik
    Jul 14, 2021 at 16:39

Your Answer

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

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