Encode-Club-Solidity-Foundations / Lesson-01

21 stars 14 forks source link

A partially successful transaction? #45

Open stpinkie opened 1 year ago

stpinkie commented 1 year ago

Yesterday during the lesson it was mentioned that transactions can either completely succeed of completely fail and there is nothing inbetween. However some time ago I noticed a transaction that reverted but still executed successfully, which sorta contradicts this.

The transaction in question: https://etherscan.io/tx/0xb41630b2578da2a4c09957d8669bb483f6d3eeb019ab4516e7ff7851993a73eb

It seems that this is an exception rather than a norm but how is this possible where a transaction can be both successful and reverted?

MatheusDaros commented 1 year ago

Hello @stpinkie

Thanks for your question.

transactions can either completely succeed of completely fail and there is nothing inbetween

Exactly. That transaction did complete entirely. Some internal transactions failed, indeed, but since solidity 0.6.x it is possible to try/catch for errors in calls like this, and thus you can write logic to proceed the transaction execution even if you happen to encounter an error. Internal transactions are just some operations that may happen inside a transaction. It is impossible to run those internal transactions of one transaction in parallel with the ones of other transactions, so they are all executed in sequence one after the other inside the same transaction. The transaction itself you mentioned completed entirely and caused all the state changes in a single transaction, even though you had small steps to get there, some failing and other succeeding.

Also, notice that controlling execution flow through exception throwing and catching is not the best way to implement things, but I am certain that the creators of that smart contract had a reason to do so.