In the previous chapter we talked about Bitcoin, the existing concepts and the potential applications of blockchain. We also discussed the challenges that the blockchain world was facing. You can find the article HERE.
In this chapter we will talk more about Ethereum itself. So lets start!
What is Ethereum
Ethereum is a decentralized platform for building applications. It provides a different set of trade-offs compared to other protocols and is designed for rapid development, security for small and infrequently used applications, and efficient interaction between different applications.
Ethereum achieves this by utilizing a blockchain with a built-in Turing-complete programming language, which enables developers to write smart contracts and decentralized applications with custom ownership, transaction formats, and state transition functions. With the added capabilities of Turing-completeness, value-awareness, blockchain-awareness, and state, Ethereum offers more power for building smart contracts and cryptographic “boxes” compared to what is offered by Bitcoin scripting.
This makes it possible to create applications such as currencies, reputation systems, and a bare-bones version of Namecoin in a relatively short amount of code.
The Ethereum Accounts
Ethereum is a decentralized platform that uses accounts to keep track of the state of the system. An account in Ethereum has a 20-byte address and is composed of four fields:
An Ethereum account contains four fields:
- The nonce, a counter used to make sure each transaction can only be processed once
- The account’s current ether balance
- The account’s contract code, if present
- The account’s storage (empty by default)
Ether is the main internal cryptocurrency in Ethereum and is used to pay transaction fees.
There are two types of accounts in Ethereum: externally owned accounts, controlled by private keys, and contract accounts, controlled by their contract code. Externally owned accounts have no code and can only send transactions, while contract accounts have code that is executed every time they receive a message. Contracts in Ethereum are similar to “autonomous agents” that live inside the Ethereum environment and execute specific code when “poked” by a message or transaction.
The Ethereum Transactions
In Ethereum, the term “transaction” refers to a signed data package sent from an externally owned account. Transactions contain the recipient of the message, the sender’s signature, the amount of ether to transfer, an optional data field, a STARTGAS value, and a GASPRICE value. The data field has no function by default, but contracts can access it through the virtual machine’s opcode. The STARTGAS and GASPRICE values are important for Ethereum’s anti-denial of service model and set limits on the computational steps and fees for each transaction.
The Ethereum Messages
Contracts also have the ability to send “messages” to other contracts. A message is like a transaction, but it is produced by a contract and not an external actor. It contains the sender (implicit), the recipient, the amount of ether to transfer, and an optional data field. A message is produced when a contract executes the CALL opcode, which leads to the recipient’s code being executed. The gas allowance assigned by a transaction or contract applies to the total gas consumed by that transaction and all sub-executions.
The Ethereum State Transition Function
The Ethereum state transition function, APPLY(S, TX) -> S’, is the process by which the Ethereum Virtual Machine (EVM) updates the global state of the Ethereum network, based on a transaction or message.
The process of the state transition function can be broken down into the following steps:
- Validation: The transaction is checked to see if it is well-formed, with a valid signature and matching nonce, and if not, an error is returned.
- Transaction fee calculation: The transaction fee is calculated as STARTGAS * GASPRICE, and the sending address is determined from the signature. The fee is subtracted from the sender’s account balance, and the sender’s nonce is incremented. If there is not enough balance to spend, an error is returned.
- Gas allocation: The GAS is initialized to the value of STARTGAS, and a certain quantity of gas per byte is taken off to pay for the bytes in the transaction.
- Transfer of value: The transaction value is transferred from the sender’s account to the receiving account. If the receiving account does not yet exist, it is created. If the receiving account is a contract, its code is executed either to completion or until the execution runs out of gas.
- Reversion or success: If the value transfer failed or the code execution ran out of gas, the state changes are reverted except for the payment of the fees, and the fees are added to the miner’s account. If the value transfer was successful, the remaining gas fees are refunded to the sender and the fees paid for gas consumed are sent to the miner.
- Contract code execution: If the receiving account is a contract, its code is executed, which in the example provided, checks if a certain value is stored in the contract’s storage and sets it if it is not.
Note that the CREATE opcode is used to create a contract, and its execution mechanics are similar to CALL, except that the output of the execution determines the code of a newly created contract.
Ethereum Code Execution
Code execution in Ethereum contracts is done through the Ethereum Virtual Machine (EVM) which is a low-level, stack-based bytecode language. The code consists of a series of bytes where each byte represents an operation. The operations are executed in an infinite loop, where the current operation is carried out based on the program counter (which starts at zero) and then the program counter is incremented by one until the end of the code is reached or an error or STOP or RETURN instruction is encountered.
The EVM code has access to three data storage spaces:
- Stack: A last-in-first-out container where values can be pushed and popped.
- Memory: An infinitely expandable byte array.
- Long-term storage: A key-value store which is used to store data for the long-term. Unlike stack and memory, which reset after computation ends, storage persists for the long term.
The code can also access the value, sender, and data of the incoming message, block header data, and can return a byte array of data as an output.
The execution model of EVM code is defined by the tuple (block_state, transaction, message, code, memory, stack, pc, gas). The block_state contains the global state that includes all accounts, balances, and storage. At the start of every execution round, the current instruction is found by taking the pcth byte of the code, and each instruction affects the tuple in its own way. For instance, the ADD operation pops two items off the stack, pushes their sum, reduces gas by 1, and increments pc by 1. The SSTORE operation pushes the top two items off the stack and inserts the second item into the contract’s storage at the index specified by the first item. Although just-in-time compilation can be used to optimize the Ethereum virtual machine’s execution, a basic implementation of Ethereum can be done in a few hundred lines of code.
Blockchain and Mining in Ethereum
Ethereum is a blockchain platform that operates similarly to Bitcoin. However, it does have some differences, the most notable being that Ethereum blocks contain both a copy of the transaction list and the most recent state.
The process of validating a block in Ethereum involves several steps:
- Checking if the previous block exists and is valid.
- Verifying that the block’s timestamp is accurate and within 15 minutes of the previous block.
- Checking the block number, difficulty, transaction root, uncle root, and gas limit for validity.
- Validating the proof-of-work on the block.
- Applying the block’s transactions to the state to get the final state.
- Checking if the final state root is equal to the one provided in the block header.
The Ethereum blockchain uses a tree structure to store the state, allowing for efficient storage and referencing of data. The execution of contract code is part of the state transition function, which occurs during the block validation process.
Note: The state is stored in a special tree called a Patricia tree, which allows for efficient insertion and deletion of nodes.
To be continued …