4. Subtract 10 more ether from the sender's account, and add it to the contract's
account.
5. Run the code. In this case, this is simple: it checks if the contract's storage at index 2
is used, notices that it is not, and so it sets the storage at index 2 to the value
CHARLIE . Suppose this takes 187 gas, so the remaining amount of gas is 1150 - 187 =
963
6. Add 963 * 0.001 = 0.963 ether back to the sender's account, and return the resulting
state.
If there was no contract at the receiving end of the transaction, then the total transaction
fee would simply be equal to the provided GASPRICE multiplied by the length of the
transaction in bytes, and the data sent alongside the transaction would be irrelevant.
Note that messages work equivalently to transactions in terms of reverts: if a message
execution runs out of gas, then that message's execution, and all other executions
triggered by that execution, revert, but parent executions do not need to revert. This
means that it is "safe" for a contract to call another contract, as if A calls B with G gas
then A's execution is guaranteed to lose at most G gas. Finally, note that there is an
opcode, CREATE , that creates a contract; its execution mechanics are generally similar to
CALL , with the exception that the output of the execution determines the code of a newly
created contract.
Code Execution
The code in Ethereum contracts is written in a low-level, stack-based bytecode language,
referred to as "Ethereum virtual machine code" or "EVM code". The code consists of a
series of bytes, where each byte represents an operation. In general, code execution is an
infinite loop that consists of repeatedly carrying out the operation at the current program
counter (which begins at zero) and then incrementing the program counter by one, until
the end of the code is reached or an error or STOP or RETURN instruction is detected.
The operations have access to three types of space in which to store data:
The stack, a last-in-first-out container to which values can be pushed and popped
Memory, an infinitely expandable byte array