About EVM, Opcode, Gas, Ethereum Accounts
What is EVM?
EVM is an execution environment for the Ethereum blockchain. It allows you to run the smart contract code by compiling it into EVM bytecode.
Basics: Solidity → Byte Code → Opcode
As you know, your Solidity code must be compiled into bytecode before being deployed on the Ethereum network. This bytecode corresponds to a series of opcode instructions that the EVM interprets.
Source code: A file written in a programming language such as Java, Solidity.
Bytecode: Compiled from source code and run on a virtual machine such as JVM, EVM.
Machine code: Code that only the operating system can read. The bytecode is converted to machine code and finally executed.
Byte code
In order to store opcodes efficiently, they are encoded into a byte code. Each operation code is allocated a byte (for example, STOP — 0x00). Let’s look at the following byte code: 0x6001600101
During execution, the byte code is split into bytes (1 byte equals 2 hexadecimal characters). Bytes in the range 0x60–0x7f (PUSH1-PUSH32) are handled differently because they include push data (which needs to be attached to the opcode rather than treated as a separate opcode).
The first instruction is 0x60, which translates to PUSH1. Therefore, we know that the length of the push data is 1 byte, and we add the next byte to the stack. Now there is 1 element in the stack, and we can move on to the next instruction. Since we know that 0x01 is part of the PUSH instruction, the next instruction we need to execute is another 0x60 instruction (PUSH1) along with the same data. Now there are 2 identical items in the stack. The last instruction is 0x01, which translates to ADD. This instruction takes 2 elements from the stack and puts the sum of these elements on the stack. Now there is one element in the stack: 0x02.
Operation Codes
You can divide all opcodes into the following categories:
1) Stack management operation codes (POP, PUSH, UP, SWAP);
2) Arithmetic operations / comparison / bitwise operation codes (ADD, SUB, GT, LT, AND, OR);
3) Environmental operation codes (CALLER, CALL VALUE, NUMBER);
4) Operation codes that control memory (LOAD, STORE, MSSTORE 8, M SIZE);
5) Memory management operation codes (LOAD, STORE);
6) Operation codes related to the program counter (JUMP, JUMP, PC, JUMPDEST);
7) Stop operation codes (STOP, RETURN, REVERT, INVALID, SELF DESTRUCT).
EVM Architecture
EVM uses a stack-based architecture. The word size (that is, the size of the data element in the stack) is 256 bits (32 bytes). This is done to facilitate the execution of 256-bit calculations of the Keccak hash and elliptic curves. The maximum stack depth is 1024. EVM also has an independent storage model; it is similar to memory, but is not an array of bytes, but an array of words based on word addressing. Storage is a permanent key and value store that is maintained as part of the system state (permanent storage in the Merkle tree). . EVM is not a standard von Neumann framework. The program code is stored in an independent virtual ROM, which can only interact with certain instructions, and not in public memory or storage.
Gas
Gas is a unit of measurement that measures the amount of computational effort required to perform certain operations on the Ethereum network.
Since each Ethereum transaction requires computing resources to complete, each transaction requires a commission. Gas is the commission required for a successful transaction in Ethereum.
The payment for gas takes place in the Ethereum currency, ether (ETH). Gas prices are indicated in gwei, which in itself is the nominal value of ETH — each gwei is equal to 0.000000001 ETH (10–9 ETH). For example, instead of saying that your gas costs 0.000000001 ether, you can say that your gas costs 1 gwei. The word “gwei” itself means “giga-wei” and is equal to 1,000,000,000 wei. Wei (named after Wei Dai, the creator of b-money) is the smallest unit of ETH.
Get Grohotov Aleksei’s stories in your inbox
Join Medium for free to get updates from this writer.
About Wei Dai, the creator of the world’s first cryptocurrency b-money: https://habr.com/ru/company/ruvds/blog/558298/
Why do we need gas
The gas fee helps maintain the security of the Ethereum network. By demanding a fee for each calculation performed on the network, we do not allow attackers to send spam on the network. To avoid random or hostile infinite loops or other computational losses in the code, each transaction should set a limit on the number of computational steps of code execution that it can use. The main unit of calculation is “gas”.
Although the transaction includes a limit, any gas not used in the transaction is returned to the user (i.e. max fee — (base fee + tip) is returned).
Types of Ethereum Accounts
Both types of accounts have the ability to:
Receive, store and send ETH and tokens;
Interact with deployed smart contracts.
Key differences
Externally-owned:
Creating an account costs nothing;
Can initiate transactions;
Transactions between Externally-owned can only be transfers of ETH/tokens.
Contract:
Creating a contract requires costs because network storage is used;
Can only send transactions in response to received transactions;
Transactions from an Externally-owned account to a Contract account can run code that can perform many different actions, such as transferring tokens or even creating a new contract.
Creating an account
EVM processes 160-bit addresses.
The account consists of a cryptographic key pair: public and private. The public key is generated from the private key using the ECDSA algorithm.
The public address of an Externally-owned account is formed as follows — the last 20 bytes from Keccak-256(public key) are taken and 0x is added to the beginning.
The Contract address is usually specified when deploying a contract in the Ethereum blockchain. The address is formed from the Externally-owned address of the creator and the number of transactions sent from this address (“nonce”). The last 20 bytes from Keccak are 256(RLP(Externall-owned; nonce)).
Screenshots of formulas from Ethereum Yellow Paper: https://ethereum .github.io/yellowpaper/paper.pdf
What is included in each account type
Each account consists of balance, nonce, bytecode and stored data (storage). However, there are some differences between the two types of accounts. For example, the External-owned fields bytecode and storage are empty, while Contract stores its bytecode and the root hash of the Merkle of the entire state tree. Moreover, while External-owned has the corresponding private key, Contract does not. The actions of Contract accounts are controlled by the code.







