7

Consider the following contrived contract function:

function submitOffer(uint256 foo) public payable {
    emit MyEvent(foo);
    // then later...
    require(foo > 10, "Foo must be greater than 10");
}

This function emits an event on the first line, then later checks some require condition. If the condition fails, any state changes are reverted. Will this cause the event to be "reverted" (or never actually emitted) as well? I assume the answer is yes because the transaction will never actually get included in a block, but is there something I'm not considering?

2 Answers 2

8

Well, yes.

If a transaction reverts, from the blockchain's state of view, it's like it never happened. No changes are stored. But the transaction is still visible off-chain, as a reverted transaction - but still no changes are stored, and this includes also event emittance.

A reverted transaction is included in a block, so in that sense it's a regular transaction. So it does leave a permanent trace.

4
  • 1
    So the record of the failed transaction will exist on the blockchain, but the event will not appear in the logs for the contract, and anyone observing the event with web3 will not be notified that the event has fired, correct?
    – dalton_c
    Commented Apr 4, 2021 at 19:52
  • 3
    correct. The transaction is processed as "all or nothing". If it reverts, everything reverts. Commented Apr 4, 2021 at 21:06
  • 1
    @LauriPeltonen What is the purpose of the record remaining on the blockchain if the transaction is "atomic"? Who is it for?
    – YulePale
    Commented May 27, 2021 at 14:59
  • 1
    Well, a reverted transaction is also a valid transaction. It just has a (probably) undesired result. Furthermore, blockchains are immutable Commented May 27, 2021 at 16:12
3

Yes. Think of it, not as the smart contract operating "live" on the real/live blockchain and then later "rolling back the changes." This can also be confusing because, yes, what happens to the events?

Instead: Think of it like this, which is more accurate: When the smart contract runs, a local EVM is set up. Nothing has to do with the live blockchain; nothing is changed in the live blockchain.

ONLY when the local EVM completes correctly will the changes be applied (so always after the fact) to the actual blockchain. And hence, since the tx reverted, no events are emitted since no part of the code will be "rolled out" to the live blockchain.

So:

  1. Run in a sandbox.
  2. If not reverted: Apply to "the real blockchain."
  3. If reverted: Just simply don't apply any changes (and simply don't submit any events as a corollary).

Do note, however that a failed transaction - even though it never ran on the real blockchain - will show up in the "Receipts" part of the block (specifically in the Receipts Trie) as a failed transaction.

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.