Design Ethereum

## **Overview**
Ethereum is a peer-to-peer network that maintains a database containing the storage values of all Ethereum accounts and processes state-altering transactions. Approximately every 12 seconds, a batch of new transactions, known as a "block", is processed by the network. Each block contains a cryptographic hash identifying the series of blocks that must precede it if the block is to be considered valid. This series of blocks is known as the blockchain.
Each "node" (network participant) connects with a relatively small subset of the network to offer blocks and unvalidated transactions (i.e. transactions not yet in the blockchain) to its peers for download, and it downloads any of these from its peers that it doesn't already have. Each node usually has a unique set of peers, so that offering an item to its peers results in the propagation of that item throughout the entire network within seconds. A node's collection of unvalidated transactions is known as its "mempool".
A node may choose to create a copy of the state for itself. It does this by starting with the genesis state and executing every transaction in the blockchain, in the proper order of blocks and in the order they are listed within each block.
Any Ethereum account may "stake" (deposit) 32 ETH to become a validator. At the end of each "epoch" (32 block slots, each slot lasting 12 seconds), each validator is pseudorandomly assigned to one of the slots of the epoch after the next, either as the block proposer or as an attester. During a slot, the block proposer uses their mempool to create a block that is intended to become the new "head" (latest block) of the blockchain, and the attesters attest to which block is at the head of the chain. If a validator makes self-contradicting proposals or attestations, or if it is inactive, it loses a portion of its stake. It may add to its stake at any time. A validator's attestation is given a weight equal to its stake or 32, whichever is less. According to the Ethereum protocol, the blockchain with the highest accumulated weight of attestations at any given time is to be regarded as the canonical chain. Validators are rewarded for making valid proposals and attestations. A validator's rewards are paid via transactions within the same chain that contains their proposal or attestation, and therefore would have little or no market value unless that chain becomes the canonical chain. This incentivizes validators to support the chain that they think other validators view as the canonical chain, which results in a high degree of consensus.
## **Ether**
Ether (ETH) is the cryptocurrency generated in accordance with the Ethereum protocol as a reward to validators in a proof-of-stake system for adding blocks to the blockchain. Ether is represented in the state as an unsigned integer associated with each account, this being the account's ETH balance denominated in wei (1018 wei = 1 ether). At the end of each epoch, new ETH is generated by the addition of protocol-specified amounts to the balances of all validators for that epoch, with the block proposers receiving the largest portion. Additionally, ether is the only currency accepted by the protocol as payment for the transaction fee. The transaction fee is composed of two parts: the base fee and the tip. The base fee is "burned" (deleted from existence) and the tip goes to the block proposer. The validator reward together with the tips provide the incentive to validators to keep the blockchain growing (i.e. to keep processing new transactions). Therefore, ETH is fundamental to the operation of the network. Ether may be "sent" from one account to another via a transaction, the execution of which simply entails subtracting the amount to be sent from the sender's balance and adding the same amount to the recipient's balance.
Ether is often erroneously referred to as "Ethereum".
## **Accounts**
There are two types of accounts on Ethereum: user accounts (also known as externally-owned accounts), and contract accounts. Both types have an ETH balance, may transfer ETH to any account, may execute the code of another contract, or create a new contract, and are identified on the blockchain and in the state by an account address.
Contracts are the only type of account that has associated bytecode and storage (to store contract-specific state). The code of a contract is evaluated when a transaction is sent to it. The code of the contract may read user-specified data from the transaction, and may have a return value. In addition to control flow statements, the bytecode may include instructions to send ETH, read from and write to the contract's storage, create temporary storage (memory) that vanishes at the end of code evaluation, perform arithmetic and hashing operations, send transactions to other contracts (thus executing their code), create new contracts, and query information about the current transaction or the blockchain.
## **Addresses**
Ethereum addresses are composed of the prefix "0x" (a common identifier for hexadecimal) concatenated with the rightmost 20 bytes of the Keccak-256 hash of the ECDSA public key (the curve used is the so-called secp256k1). In hexadecimal, two digits represent a byte, and so addresses contain 40 hexadecimal digits after the "0x", e.g. 0xb794f5ea0ba39494ce839613fffba74279579268. Contract addresses are in the same format, however, they are determined by sender and creation transaction nonce.
## **Virtual machine**
The Ethereum Virtual Machine (EVM) is the runtime environment for transaction execution in Ethereum. The EVM is a stack based virtual machine with an instruction set specifically designed for Ethereum. The instruction set includes, among other things, stack operations, memory operations, and operations to inspect the current execution context, such as remaining gas, information about the current block, and the current transaction. The EVM is designed to be deterministic on a wide variety of hardware and operating systems, so that given a pre-transaction state and a transaction, each node produces the same post-transaction state, thereby enabling network consensus. The formal definition of the EVM is specified in the Ethereum Yellow Paper. EVMs have been implemented in C++, C#, Go, Haskell, Java, JavaScript, Python, Ruby, Rust, Elixir, Erlang, and soon WebAssembly.
## **Gas**
Gas is a unit of account within the EVM used in the calculation of the transaction fee, which is the amount of ETH a transaction's sender must pay to the network to have the transaction included in the blockchain. Each type of operation which may be performed by the EVM is hardcoded with a certain gas cost, which is intended to be roughly proportional to the monetary value of the resources (e.g. computation and storage) a node must expend or dedicate to perform that operation.
When a sender is creating a transaction, the sender must specify a gas limit and gas price. The gas limit is the maximum amount of gas the sender is willing to use in the transaction, and the gas price is the amount of ETH the sender wishes to pay to the network per unit of gas used. A transaction may only be included in the blockchain at a block slot that has a base gas price less than or equal to the transaction's gas price. The portion of the gas price that is in excess of the base gas price is known as the tip and goes to the block proposer; the higher the tip, the more incentive a block proposer has to include the transaction in their block, and thus the quicker the transaction will be included in the blockchain. The sender buys the full amount of gas (i.e. their ETH balance is debited gas limit × gas price and their gas balance is set to gas limit) up-front, at the start of the execution of the transaction, and is refunded at the end for any unused gas. If at any point the transaction does not have enough gas to perform the next operation, the transaction is reverted but the sender is still only refunded for the unused gas. In user interfaces, gas prices are typically denominated in gigawei (Gwei), a subunit of ETH equal to 10−9 ETH.