Sub topic 6
Ethereum: Accounts, the EVM, and Smart Contracts
Assume you know the basics of hashing, public and private keys, and how a blockchain stores transactions. This chapter focuses on Ethereum’s account model, its execution engine, and the programs called smart contracts.
Ethereum’s account model
Bitcoin records spendable outputs called UTXOs. A transaction consumes some UTXOs and creates new ones. Ethereum instead keeps a shared state containing accounts. Each account has an address, a balance, and a nonce. A nonce is a counter used to order transactions and prevent replay.
Ethereum has two account types:
- An externally owned account (EOA) is controlled by a private key. People and applications use EOAs to sign transactions.
- A contract account is controlled by code stored on the blockchain. It has no private key.
A contract account can store persistent data, called storage. For example, a token contract can store each address’s token balance. A contract cannot independently create a new transaction. An EOA or another contract must call it.
| Account type | Controlled by | Has code | Typical role |
|---|---|---|---|
| EOA | Private-key signature | No | User or application account |
| Contract account | Deployed program | Yes | Rules and persistent state |
A transaction from an EOA can transfer ether, deploy a contract, or call a function in an existing contract. Every validating node executes the same state change and checks that the result follows the protocol.
The EVM as a shared computer
The Ethereum Virtual Machine (EVM) is the execution environment for contract code. Developers usually write Solidity or another high-level language. A compiler converts that source into EVM bytecode, a sequence of instructions called opcodes.
The EVM is a stack machine. Most instructions read values from and write values to a stack of 256-bit words. It also provides memory for temporary data and storage for persistent contract state. Contract code is deterministic: given the same starting state and transaction input, every node must produce the same result.
This shared execution model makes contracts composable. One contract can call another contract, such as a trading application calling a token contract. It also means that a bug can affect every user who relies on the same code.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Counter {
uint256 public value;
function increment() external {
value += 1;
}
function set(uint256 newValue) external {
value = newValue;
}
}Deploying this contract creates a contract account. Calling increment changes its stored value. Calling the automatically generated value() view function reads that state without changing it.
State transitions and common errors
Ethereum can be viewed as a state-transition system:
- Start with the current account and contract state.
- Validate the transaction signature, nonce, and available funds.
- Execute the requested code.
- Commit successful storage and balance changes, or revert the changes if execution fails.
A revert does not erase the transaction from the chain. The failed transaction and its consumed execution resources remain visible.
Common errors include treating a contract like a person, assuming code can be changed without an upgrade mechanism, and trusting a contract because its source code looks simple. An upgradeable contract may delegate behavior to another address, so its effective rules can change. Calls between contracts can also create reentrancy risks when a contract sends control to outside code before updating its own state.
Exercise
Why can a contract account execute code but not initiate a transaction by itself?
Show answer
A transaction must begin with an authorization signature from an externally owned account. A contract can react during execution and call another contract, but it cannot create that initial signed transaction.