35
Study Notes on Ethereum Virtual Machine (EVM) - The Basics
Ethereum Accounts §
-
External Accounts
- Users
- Address = public key
-
Contract Accounts
- smart contracts (code)
- Address generated when the contract is created (it is derived from the creator address and the number of transactions sent from that address, the so-called “nonce”).
Ethereum Transactions §
- the address of that contract is not the zero address, but an address derived from the sender and its number of transactions sent (the “nonce”).
- The payload of such a contract creation transaction is taken to be EVM bytecode and executed.
- The output data of this execution is permanently stored as the code of the contract.
- This means that in order to create a contract, you do not send the actual code of the contract, but in fact code that returns that code when executed.
- ⚠ While a contract is being created, its code is still empty. Because of that, you should not call back into the contract under construction until its constructor has finished executing.
Gas §
- They have to pay
gas_price * gas
upfront from the sending account.
Storage, Memory, and the Stack §
- Non-Volatile: persistent between function calls & transactions.
- It’s a key-value store that maps 256-bit words to 256-bit words.
- not possible to enumerate storage from within a contract.
- is comparatively costly to read , and even more to initialize and modify storage.
- Because of this cost, you should minimize what you store in persistent storage.
- Store data like derived calculations, caching, and aggregates outside of the contract.
- A contract can neither read nor write to any storage apart from its own.
- Volatile: fresh instance for each message call.
- it is linear and can be addressed at the byte level.
- reads are limited to a width of 256 bits.
- writes can be either 8 bits or 256 bits wide.
- Memory is expanded by a word (256-bit) when accessing (either reading or writing) a previously untouched memory word (i.e. any offset within a word).
- At the time of expansion, the cost in gas must be paid.
- Memory is more costly the larger it grows (it scales quadratically).
- all computations are performed on the stack.
- maximum size of 1024 elements and contains words of 256 bits.
- It is possible to copy one of the topmost sixteen elements to the top of the stack or swap the topmost element with one of the sixteen elements below it.
- All other operations take the topmost two (or one, or more, depending on the operation) elements from the stack and push the result onto the stack.
- it is possible to move stack elements to storage or memory to get deeper access to the stack.
Message Calls §
Delegatecall / Callcode and Libraries §
msg.sender
and msg.value
do not change their values.
Logs §
events
.
Create §
Deactivate and Self-destruct §
selfdestruct
operation.35