1.14 Transaction Reverts & Data

Reverts

A transaction can revert for different exceptional conditions:

  • The transaction could run out of Gas depending on how much was supplied as part of it and what that transaction actually needs when it is executing.

  • The transaction could also revert because of invalid instructions that are encountered as part of executing the smart contract.

When the transaction gets reverted, all the state changes made in the context of the EVM so far from all the previous instructions in the contract are discarded, and the original state before the transaction started executing is restored. It is as if the transaction never executed from the perspective of the EVM state.

Data

Recall that the data field within a transaction is relevant when the recipient of said transaction is a contract account. In that case, the transaction data contains two components:

  • It has to specify the function of that contract that is being invoked and...

  • If that function requires any arguments, then it needs to specify those as well

All this is encoded according to the Application Binary Interface (ABI): it's the contract's interface that's specified in a standard way, so that contracts can interact with each other. This is critical for contracts to interact both from outside the blockchain (when a transaction is triggered targeting a destination contract) but also for messages that are sent between two or more contracts within the EVM context.

These interface functions that are specified as part of the ABI are strongly typed, are known at compilation time and they are static: the types of the function parameters are well known at compile time and they cannot change, because if they did, then what is specified as part of the contract call during execution will not reflect to what the destination contract requires in terms of the function encoding, or in terms of the arguments that are supplied.

  • So, how does a callee contract specify the function to be invoked on the destination contract?\

    It does that through the function selector. The way that it is specified is by taking the function signature (of the function that needs to be invoked), running that through a Keccak-256 hash and taking the first four bytes of the output hash.

  • How is this function signature calculated from the function declaration?\

    The function name is taken and appended with the parenthesized list of the parameter types that it accepts. These parameter types are specified one after another, with the comma being the delimiter.\

    Note that there are no spaces used (this is something that is enforced as part of the ABI and it's a standard, because if different contracts use different notations for function signatures, then you can imagine that when a transaction triggers a contract and sends the function selector, the receiving contract will not know which function to execute).\

    So everyone has to know what the format is. This allows the EVM to function in a very deterministic manner.\

    Besides the function selector we have the function arguments that are also part of the transaction data (like we just discussed). These are encoded as well immediately following the four bytes of function selector: they span from the fifth byte onwards and go on depending on the number of arguments that the particular function needs.

Last updated