Search code examples
eventsethereumsolidity

Solidity event triggering order


I've been looking at the Ownable contract from OpenZeppelin, and came accross the following method:

/**
 * @dev Allows the current owner to transfer control of the contract to a wOwner.
 * @param newOwner The address to transfer ownership to.
 */
function transferOwnership(address newOwner) public onlyOwner {
  require(newOwner != address(0));
  OwnershipTransferred(owner, newOwner);
  owner = newOwner;
}

Here, the event OwnershipTransferred gets triggered before the actual owner gets set to the new owner. Is method execution in the EVM atomic, in the sense that the event will only be triggered after the execution after the method was done executing? Otherwise I see the possibility that some event listener will act upon this event while the EVM isn't yet in the correct state. I'm not a 100% convinced this is safe and sound.


Solution

  • Functions which mutate state are called as part of transactions, which are just that. Either the whole transaction succeeds or the whole transaction fails. From an outside observer's standpoint (e.g. something watching for an event), yes, the transaction is atomic.