This post is about the “REVERT, “a savior opcode introduced in EIP-140 and became a part of @solidity_lang in 0.4.10.
The REVERT opcode is a built-in function in the EVM with a bytecode denotation of "0xFD" and is a system operations opcode. The purpose of this opcode is to allow a contract to stop execution and revert any state changes made during the current call.
That means this opcode works by halting the execution of a contract and returning the contract's state to how it was before the current call was made. This includes reversing any Ether or token transfers that may have occurred during the call and returning the unused gas.
It expects two stack items; the top item is the "memory_offset" followed by "memory_length," which means it takes a memory location in the current execution context and the length of memory and returns memory[offset: offset + length].
The sequence of bytes between memory[offset: offset + length] is called an “error message.” The error message will be available to the caller in the returndata buffer. It will also be copied to the output area, i.e., handled the same way as the regular return data.
REVERT is different from the STOP opcode in that it also reverses state changes. STOP simply stops execution without undoing any changes.
REVERT can be used in various situations, such as handling exceptions thrown by other contracts, implementing security measures to prevent unauthorized access, or ensuring that funds are not lost in the event of an error.
It's important to note that REVERT does not generate an error message or log. This means that it's up to the contract developer to provide appropriate error handling and messaging for the user.
But there are scenarios when REVERT occurs with no message.
An external function call that targets a contract that contains no code.
If your contract receives Ether via a public function without a payable modifier.
If not enough, calldata is supplied for a function call.
Overall, the REVERT opcode is a useful tool for maintaining the integrity of a contract and ensuring that it functions as intended. It's an essential part of the EVM and should be used appropriately in contract development.
Solidity also supports revert opcode in the inline assembly in v0.4.10. Here is the snippet:
Solidity does provide "require()" and "revert()" functions to use the REVERT opcode in your smart contract, but that's the topic for another post!