Skip to content
LumenaryAcademy
Practice 80 min

Ethereum and smart contracts

A world computer with accounts, a stack machine metered by gas, contracts that cannot be changed, and the patterns and compromises built around them.

Code tested · 4AI-draftedAwaiting review by a computer scientist

Your guides: Chloe · The Computer Scientist, Margaret · The Experimenter, Gideon · The Verifier

MotivationH

From a ledger of coins to a ledger of programs

Bitcoin's Script, as we saw in the last lesson, is deliberately small: a locking script can check signatures, hashes and times, and nothing else. In 2013 Vitalik Buterin proposed a different trade-off. Instead of a ledger of coins with small locks, keep a ledger of accounts, and let some accounts be programs that hold money and data and run whenever someone sends them a transaction. Ethereum launched on 30 July 2015 with that design, and the programs came to be called smart contracts: not smart, and not contracts in the legal sense, but code whose execution every validator repeats and agrees on.

The idea makes a public blockchain programmable. A token, an exchange, an auction or an escrow can be written as a contract, and anyone can use it without asking permission from the people who wrote it. It also brings old problems of computer science into a hostile setting. How do you stop someone from submitting a program that never halts, when thousands of machines must run it? How do you fix a bug in code that, by design, cannot be changed? How does a program that can see only the blockchain learn the price of rice or the result of a football match?

This lesson answers those questions by building a small Ethereum Virtual Machine with gas, reading a contract written in Solidity, decoding what a transaction actually sends to a contract, and looking at how upgrades, oracles and other chains' designs trade one kind of trust for another.

DefinitionsH

Accounts, state and transactions

Definition— account

An Ethereum account is identified by a 20-byte address and has four fields: a nonce, a balance in wei (10−1810^{-18} ether), a code hash, and a storage root, the root of a Merkle trie holding the account's storage (a map from 32-byte keys to 32-byte values). An externally owned account (EOA) is controlled by a private key and has no code; a contract account has code and is controlled only by that code.

Definition— world state and transaction

The world state maps every address to its account, and its Merkle root is committed in each block header. A transaction is signed by an EOA and names a recipient, a value in wei, a data field (called calldata when the recipient is a contract), a gas limit, fee parameters and the sender's nonce, which must equal the number of transactions the sender has sent before. Executing it moves the world state to a new state, deterministically.

Compare this with Bitcoin. A Bitcoin transaction names specific outputs; an Ethereum transaction says "from my account, nonce 41", and the nonce does the job that unique outpoints did: a replayed transaction has a stale nonce and is invalid. Every node that executes the same transactions in the same order reaches the same state root, which is what consensus (since September 2022, the proof of stake of Proof of stake and finality) agrees on.

Note

The line between the two kinds of account has blurred. ERC-4337 (2023) lets "smart accounts" (contracts) act as wallets with features such as social recovery and spending limits, and EIP-7702, part of the Pectra upgrade of May 2025, lets an EOA delegate to contract code. The four account fields above are unchanged.

IntuitionH

The EVM: a stack machine with three kinds of memory

Contract code is bytecode for the Ethereum Virtual Machine (EVM), a stack machine with 256-bit words. Each call to a contract gets a fresh machine:

coderead-only bytecodecalldataread-only input bytesEVM executionprogram countergas remainingstack256-bit words, max 1024discarded after the callmemorybyte array, discardedstoragepersistent 32-byte key → valuelogs (events)kept in receipts, unreadable by code
The state an EVM call works with. Code and calldata are read-only. The stack and memory are scratch space, discarded when the call ends. Storage belongs to the contract and persists between transactions. Every instruction draws on the gas counter.
  • The stack holds up to 1024 words; most instructions pop their operands and push a result.
  • Memory is a byte array that starts empty for each call and grows as it is touched; it is cheap at first and becomes quadratically expensive.
  • Storage is the contract's persistent key–value store, part of the world state. It is by far the most expensive to write, because every node must keep it.
  • Calldata is the read-only input of the call, and logs are records the contract emits for the outside world (events); contracts cannot read logs back.

Control flow uses JUMP and JUMPI (jump if non-zero) to positions marked JUMPDEST, so unlike Bitcoin's Script, the EVM can loop. That is exactly why it needs gas.

ComputationC

Gas: paying for every step

Suppose anyone could submit a program and every validator had to run it to completion. One program top: JUMP top would freeze the whole network. Could validators reject non-terminating programs in advance? No: deciding whether an arbitrary program halts is the halting problem, which no algorithm solves (see computability). Ethereum's answer is to sidestep the question. Every instruction has a price in gas; the sender sets a gas limit and pays for the gas used; if execution reaches the limit, it stops with an out-of-gas error, all its state changes are undone, and the gas is still paid for. Every computation halts, because every computation is bounded by the gas someone was willing to pay for.

The program below is a small EVM. It uses the real opcode numbers and 256-bit arithmetic, and a simplified version of the real gas schedule: 3 gas for most arithmetic and stack operations, 8 or 10 for jumps, 2,100 to read a storage slot and 20,000 to write a non-zero value into an empty slot (5,000 to overwrite), plus the EVM's actual formula for growing memory. The contract sums 1+2+⋯+n1 + 2 + \cdots + n, with nn taken from calldata, stores the result in storage slot 0, and returns it.

python
# A tiny EVM: real opcode numbers, 256-bit words, and a simplified version of the real gas schedule.
OPS = {  # name: (opcode, gas)
    "STOP": (0x00, 0), "ADD": (0x01, 3), "MUL": (0x02, 5), "SUB": (0x03, 3), "LT": (0x10, 3),
    "ISZERO": (0x15, 3), "CALLDATALOAD": (0x35, 3), "MSTORE": (0x52, 3), "SLOAD": (0x54, 2100),
    "SSTORE": (0x55, None), "JUMP": (0x56, 8), "JUMPI": (0x57, 10), "JUMPDEST": (0x5B, 1),
    "PUSH1": (0x60, 3), "DUP1": (0x80, 3), "DUP2": (0x81, 3), "SWAP1": (0x90, 3), "RETURN": (0xF3, 0),
}
NAME = {code: name for name, (code, _) in OPS.items()}
WORD = 2**256

def assemble(src):
    """Every token is one byte. 'name:' emits a JUMPDEST; 'PUSH1 name' pushes that label's address."""
    tokens = src.split()
    labels = {t[:-1]: i for i, t in enumerate(tokens) if t.endswith(":")}
    return bytes(OPS["JUMPDEST"][0] if t.endswith(":") else OPS[t][0] if t in OPS
                 else labels[t] if t in labels else int(t) for t in tokens)

def memory_cost(words):
    return 3 * words + words * words // 512            # the EVM's memory-expansion formula

def execute(code, storage, gas_limit, calldata=b""):
    """On success return ('ok', output, gas used) and commit storage writes.
    On failure return the error, leave storage unchanged and charge all the gas."""
    stack, memory, pc, gas = [], bytearray(), 0, 0
    writes = dict(storage)
    jumpdests, i = set(), 0
    while i < len(code):                               # valid targets: JUMPDESTs outside push data
        if code[i] == 0x5B:
            jumpdests.add(i)
        i += 2 if code[i] == 0x60 else 1
    try:
        while True:
            if pc < len(code) and code[pc] not in NAME:
                raise RuntimeError("invalid opcode")
            name = NAME[code[pc]] if pc < len(code) else "STOP"
            cost = OPS[name][1]
            if name == "SSTORE":                       # writing a fresh slot is the expensive case
                cost = 20_000 if writes.get(stack[-1], 0) == 0 and stack[-2] != 0 else 5_000
            if name in ("MSTORE", "RETURN"):           # pay for newly touched memory
                end = stack[-1] + (32 if name == "MSTORE" else stack[-2])
                words = max(len(memory) // 32, (end + 31) // 32)
                cost += memory_cost(words) - memory_cost(len(memory) // 32)
                memory.extend(bytes(32 * words - len(memory)))
            gas += cost
            if gas > gas_limit:
                raise RuntimeError("out of gas")
            pc += 1
            if name == "STOP":
                output = b""
                break
            if name == "PUSH1":
                stack.append(code[pc])
                pc += 1
            elif name in ("ADD", "MUL", "SUB", "LT"):
                a, b = stack.pop(), stack.pop()        # a was on top of the stack
                stack.append({"ADD": a + b, "MUL": a * b, "SUB": a - b, "LT": int(a < b)}[name] % WORD)
            elif name == "ISZERO":
                stack.append(int(stack.pop() == 0))
            elif name == "CALLDATALOAD":
                i = stack.pop()
                stack.append(int.from_bytes(calldata[i:i + 32].ljust(32, b"\0"), "big"))
            elif name in ("DUP1", "DUP2"):
                stack.append(stack[-1 if name == "DUP1" else -2])
            elif name == "SWAP1":
                stack[-1], stack[-2] = stack[-2], stack[-1]
            elif name == "MSTORE":
                offset, value = stack.pop(), stack.pop()
                memory[offset:offset + 32] = value.to_bytes(32, "big")
            elif name == "SLOAD":
                stack.append(writes.get(stack.pop(), 0))
            elif name == "SSTORE":
                key, value = stack.pop(), stack.pop()
                writes[key] = value
            elif name in ("JUMP", "JUMPI"):
                dest = stack.pop()
                if name == "JUMP" or stack.pop() != 0:
                    if dest not in jumpdests:
                        raise RuntimeError("bad jump destination")
                    pc = dest
            elif name == "RETURN":
                offset, size = stack.pop(), stack.pop()
                output = bytes(memory[offset:offset + size])
                break
            if len(stack) > 1024:
                raise RuntimeError("stack overflow")
    except (RuntimeError, IndexError) as err:
        return (str(err) or "stack underflow"), b"", gas_limit
    storage.clear()
    storage.update(writes)
    return "ok", output, gas

# Sum 1 + 2 + ... + n, with n read from the call data; store the result in slot 0 and return it.
SUM = """
    PUSH1 0 CALLDATALOAD PUSH1 0
  loop:
    DUP2 ISZERO PUSH1 done JUMPI
    DUP2 ADD
    SWAP1 PUSH1 1 SWAP1 SUB SWAP1
    PUSH1 loop JUMP
  done:
    DUP1 PUSH1 0 SSTORE
    PUSH1 0 MSTORE PUSH1 32 PUSH1 0 RETURN
"""
code = assemble(SUM)
print("bytecode:", code.hex())
arg = lambda n: n.to_bytes(32, "big")

storage = {}
for n, limit in [(10, 100_000), (100, 100_000), (1000, 20_000)]:
    status, out, used = execute(code, storage, limit, arg(n))
    result = int.from_bytes(out, "big") if out else "-"
    print(f"n={n:<5} gas limit {limit:>7}: {status:10} returns {result!s:>5}  gas {used:>6}  storage {storage}")

forever = assemble("top: PUSH1 top JUMP")              # a loop with no way out
status, _, used = execute(forever, {}, 1_000_000)
print(f"endless loop, gas limit 1000000: {status} after {used // 12} iterations of 12 gas")
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

Read the output carefully. With n=10n = 10 the call uses 20,571 gas, of which 20,000 is the single storage write. With n=100n = 100 the loop runs ten times as often, yet the call is cheaper, 10,251 gas, because the slot already holds a value and overwriting costs 5,000. With n=1000n = 1000 and a 20,000 gas limit, the loop runs out of gas: the status is an error, the stored value is still 5050, and all 20,000 gas is charged. The endless loop stops too, after 83,333 iterations, because it can only buy that many.

Gas prices are not arbitrary. They approximate the resources each operation consumes on every node: computation, memory, disk reads, and above all the permanent growth of the state. When prices drift from reality, attackers notice. In September and October 2016, transactions that repeatedly called underpriced operations (ones that read other accounts' data from disk) slowed nodes to a crawl, and the Tangerine Whistle upgrade raised those prices (EIP-150). Repricing has continued since, for example making the first access to a storage slot in a transaction ("cold") cost more than later ones ("warm"). A transaction also pays a base cost of 21,000 gas, and since the London upgrade of 2021 its fee per gas is a protocol-set base fee, which is burned, plus a priority fee to the block proposer (EIP-1559). A block has a total gas limit, raised several times by validators since 2025, which caps the work every node must do per block.

Worked examplesH

A contract in Solidity, line by line

Nobody writes contracts in bytecode. The dominant language is Solidity, statically typed with a syntax reminiscent of JavaScript and C++, compiled to EVM bytecode; Vyper is a smaller, Python-like alternative. Here is an escrow: a buyer deposits ether for a seller, and either the buyer releases it or an arbiter both trust decides. The code is shown, not run; it is for learning and has not been audited.

solidity
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.24;

/// A one-off escrow between a buyer and a seller, with an arbiter.
contract Escrow {
    enum State { AwaitingDelivery, Released, Refunded }

    address public immutable buyer;
    address public immutable seller;
    address public immutable arbiter;
    uint256 public immutable amount;
    State public state;

    event Released(address indexed to, uint256 amount);
    event Refunded(address indexed to, uint256 amount);

    error NotAllowed();
    error AlreadySettled();
    error PaymentFailed();

    constructor(address seller_, address arbiter_) payable {
        require(msg.value > 0, "deposit required");
        buyer = msg.sender;
        seller = seller_;
        arbiter = arbiter_;
        amount = msg.value;
    }

    function release() external {
        if (msg.sender != buyer && msg.sender != arbiter) revert NotAllowed();
        _settle(State.Released, seller);
        emit Released(seller, amount);
    }

    function refund() external {
        if (msg.sender != seller && msg.sender != arbiter) revert NotAllowed();
        _settle(State.Refunded, buyer);
        emit Refunded(buyer, amount);
    }

    function _settle(State outcome, address to) private {
        if (state != State.AwaitingDelivery) revert AlreadySettled();
        state = outcome;                                // record the outcome first...
        (bool ok, ) = to.call{value: amount}("");       // ...then send the ether
        if (!ok) revert PaymentFailed();
    }
}

Line by line:

  • The SPDX comment states the licence, and pragma pins the compiler versions the code was written for. Since version 0.8, arithmetic that overflows reverts instead of wrapping silently, a change whose history we will meet in Smart-contract security.
  • enum State names the three phases. state is a state variable, kept in storage; its default is the first value, AwaitingDelivery.
  • immutable variables are fixed in the constructor and written into the deployed code itself, so reading them costs no storage access. public makes the compiler generate a getter function for each.
  • event declarations define the logs the contract emits; indexed parameters become searchable topics. Custom errors give cheap, descriptive reasons when a call reverts.
  • The constructor runs once, at deployment. payable lets it receive ether, msg.value is the amount sent and msg.sender is the caller, here the buyer. require reverts the whole deployment if no deposit is made.
  • release and refund are external functions that anyone can call, so each first checks who is calling. Forgetting such a check is one of the commonest real bugs.
  • _settle is private. It checks the phase, updates the state before sending ether, and only then makes the external call. to.call{value: amount}("") sends ether and returns whether the recipient accepted it. The order of these lines, checks, then effects, then interactions, is the defence against the reentrancy attack of the next lesson.

Tokens: ERC-20 and ERC-721

Much of Ethereum's activity is tokens, and tokens are just contracts that keep a table of balances and agree on a common interface. ERC-20 (proposed in 2015) standardises fungible tokens, where every unit is interchangeable. Its interface, in Solidity (shown, not run):

solidity
interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 value) external returns (bool);
    function approve(address spender, uint256 value) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function transferFrom(address from, address to, uint256 value) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

transfer moves your own tokens. approve and transferFrom let you authorise another contract, such as an exchange, to move up to a set amount on your behalf; this two-step dance exists because a contract cannot see that tokens arrived unless it moves them itself. Approvals are also the root of much wallet-draining fraud, as the next lesson explains. ERC-721 (2018) standardises non-fungible tokens (NFTs), where each token has a unique identifier and owner (ownerOf(tokenId)) and usually points to metadata stored elsewhere. A token is only as meaningful as what its issuer promises: an ERC-721 token records that an address controls an identifier, not that anyone owns a painting.

ComputationC

Calldata, storage layout and events

What does a wallet actually send when you call transfer(to, 2500)? The Application Binary Interface (ABI) says: the first 4 bytes of calldata are a function selector, the first 4 bytes of the Keccak-256 hash of the function's signature, written without spaces or parameter names; then each argument is encoded, a static type taking one 32-byte word. Events work similarly: the first topic of a log is the hash of the event's signature. And storage has a layout too. State variables are assigned consecutive 32-byte slots from slot 0, with small variables packed together into one slot when they fit; a mapping occupies one slot pp, and its entry for key kk lives at slot keccak256⁡(k ∥ p)\operatorname{keccak256}(k \,\|\, p), each padded to 32 bytes.

Ethereum's hash is Keccak-256, the function that won the SHA-3 competition, but in the version submitted before NIST changed its padding. Python's hashlib.sha3_256 is the standardised version and gives different answers, so the program implements Keccak itself, from the permutation (an original, unoptimised implementation for learning).

python
import hashlib

# Keccak-256 as Ethereum uses it, written out from the Keccak-f[1600] permutation.
MASK = 2**64 - 1
def rol(v, n):
    return ((v << n) | (v >> (64 - n))) & MASK

def lfsr_bit(t):                         # round-constant bits come from a small LFSR
    r = 1
    for _ in range(t % 255):
        r <<= 1
        if r & 0x100:
            r ^= 0x171
    return r & 1

RC = [sum(lfsr_bit(j + 7 * i) << (2**j - 1) for j in range(7)) for i in range(24)]
ROT, (x, y) = [[0] * 5 for _ in range(5)], (1, 0)
for t in range(24):                      # rotation offsets, walked out along (x, y) -> (y, 2x + 3y)
    ROT[x][y] = (t + 1) * (t + 2) // 2 % 64
    x, y = y, (2 * x + 3 * y) % 5

def keccak_f(A):
    for rc in RC:
        C = [A[x][0] ^ A[x][1] ^ A[x][2] ^ A[x][3] ^ A[x][4] for x in range(5)]
        D = [C[(x - 1) % 5] ^ rol(C[(x + 1) % 5], 1) for x in range(5)]
        B = [[0] * 5 for _ in range(5)]
        for x in range(5):
            for y in range(5):
                B[y][(2 * x + 3 * y) % 5] = rol(A[x][y] ^ D[x], ROT[x][y])
        A = [[B[x][y] ^ (~B[(x + 1) % 5][y] & B[(x + 2) % 5][y]) for y in range(5)] for x in range(5)]
        A[0][0] ^= rc
    return A

def keccak256(data: bytes) -> bytes:
    rate = 136                                               # bytes absorbed per permutation
    padded = bytearray(data) + b"\x01" + bytes((-len(data) - 1) % rate)
    padded[-1] |= 0x80                                       # Keccak padding; NIST SHA-3 uses 0x06
    A = [[0] * 5 for _ in range(5)]
    for block in range(0, len(padded), rate):
        for i in range(rate // 8):
            A[i % 5][i // 5] ^= int.from_bytes(padded[block + 8 * i: block + 8 * i + 8], "little")
        A = keccak_f(A)
    return b"".join(A[i % 5][i // 5].to_bytes(8, "little") for i in range(4))

print("keccak256('')  ", keccak256(b"").hex())
print("NIST sha3_256('')", hashlib.sha3_256(b"").hexdigest())

def selector(signature):                 # first 4 bytes of the hash of the function's signature
    return keccak256(signature.encode())[:4]

def word(n):                             # every ABI argument of a static type fills one 32-byte word
    return n.to_bytes(32, "big")

print("transfer(address,uint256)  ->", selector("transfer(address,uint256)").hex())
print("balanceOf(address)         ->", selector("balanceOf(address)").hex())
print("Transfer event topic       ->", keccak256(b"Transfer(address,address,uint256)").hex())

to = int("5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c", 16)       # a made-up address
calldata = selector("transfer(address,uint256)") + word(to) + word(2_500)
print("calldata:", calldata.hex()[:8], calldata.hex()[8:72], calldata.hex()[72:], sep="\n  ")

# Where does a token keep balances[to], if `balances` is the first state variable (slot 0)?
slot = keccak256(word(to) + word(0))
print("storage slot of balances[to]:", slot.hex())
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

The hash of the empty string, c5d24601…, is the familiar Keccak-256 value, and it differs from the NIST SHA3-256 hash on the next line. The selector a9059cbb is the one you will find at the start of every ERC-20 transfer on any block explorer, and ddf252ad… is the first topic of every ERC-20 Transfer event. The calldata is 68 bytes: the selector, the address padded to 32 bytes, and 2,500 (hexadecimal 9c4) padded to 32 bytes. Anyone can compute where an account's balance lives, which is how explorers and analytics read contract state directly.

ExplanationH

Deployment, immutability and upgradeable proxies

To deploy a contract you send a transaction with no recipient whose data is init code: bytecode that runs once (the constructor) and returns the runtime code to be stored. The new contract's address is derived from the deployer's address and nonce, or, with CREATE2, from the deployer, a chosen salt and the hash of the init code, so the address can be known before deployment. Once stored, the code cannot be edited. Since the Dencun upgrade (March 2024), even the old SELFDESTRUCT instruction no longer deletes a contract's code except within the transaction that created it.

Immutability is a feature, since users can read exactly what will run, and a liability, since bugs cannot be patched. Projects therefore often deploy an upgradeable proxy.

usersproxyholds storage and balanceimplementation = v2admin = a multisigimplementation v1no longer usedimplementation v2code onlyadministratorDELEGATECALLupgrade: change pointer
An upgradeable proxy. Users always call the proxy, which keeps the storage and the balance. It forwards each call with DELEGATECALL to the current implementation, whose code runs against the proxy's storage. An administrator can point the proxy at a new implementation.

DELEGATECALL runs another contract's code in the caller's context: same storage, same balance, same msg.sender. A proxy stores the address of its implementation in a fixed, unlikely storage slot (standardised by EIP-1967) and forwards every call to it; upgrading means writing a new address into that slot. Patterns differ in where the upgrade logic lives (the "transparent" proxy and UUPS, both implemented in OpenZeppelin Contracts), but the trade-off is the same: whoever can upgrade can change every rule, including who owns the funds. A proxy turns "the code is the law" into "the code is the law until the administrator says otherwise". Responsible projects narrow that power with multi-signature administrators, timelocks that delay upgrades so users can leave first, and governance votes, and they publish all of it. Upgradeable code also brings its own bugs: a new implementation must keep the old storage layout exactly, and an implementation contract left uninitialised can sometimes be taken over directly.

ExplanationH

Oracles: how contracts learn about the world

A contract can read only the blockchain. Every node must reach the same result, so a contract cannot fetch a web page: two nodes fetching it a second apart might get different answers. Yet lending, insurance, stablecoins and derivatives all need outside facts such as exchange rates. An oracle is a mechanism that posts such facts on chain.

The oracle problem is that the contract is now only as trustworthy as its oracle. Designs differ in how they spread that trust: networks of independent node operators that aggregate many sources and post a median (Chainlink is the largest such network); feeds signed by the market participants who produce the prices (Pyth Network); and "optimistic" oracles, where anyone may propose an answer and anyone may dispute it within a window, with disputes settled by a vote (UMA). A contract that reads the price of a thinly traded asset directly from a single on-chain exchange is the weakest design of all, as the oracle-manipulation attacks of the next lesson show.

ExplanationH

Other designs

Ethereum's design is not the only one, and the alternatives are best understood by what they change.

  • EVM-compatible chains. Many chains run the EVM with different consensus, validator sets or fees, among them BNB Smart Chain, Avalanche's C-Chain and Polygon PoS, as do most of the rollups in Scaling: rollups and data availability. The code and tools carry over; the trust assumptions do not.
  • Solana (mainnet beta since 2020) separates code from data more sharply. Programs are stateless; all data lives in accounts that programs own, and every transaction must list in advance the accounts it will read and write. That list lets the runtime execute transactions that touch disjoint accounts in parallel across many cores, and Solana pairs this with fast blocks and demanding validator hardware. The network suffered several multi-hour outages in 2021 and 2022, and its developers have since worked on reliability, including a second, independently written validator client (Firedancer).
  • Move-based chains. Move was designed at Facebook (later Meta) for the Diem project, which was abandoned in 2022, and lives on in chains such as Aptos and Sui. Its central idea is the resource type: a value that the type system forbids from being copied or silently discarded, so a token cannot be duplicated or lost by a programming error, only moved. Sui also organises state as objects with owners, which lets transactions on independent objects run in parallel.
  • Cardano uses the extended UTXO model: like Bitcoin, value sits in outputs, but an output can carry a datum (data) and be locked by a validator script that sees the whole spending transaction. Because a transaction's effect depends only on its inputs, its validity and its fee can be checked off-chain before it is submitted. The cost is concurrency: an output can be spent only once, so an application with shared state has to be designed so that many users do not race for the same output. Smart contracts arrived on Cardano with the Alonzo upgrade in September 2021.

None of these choices is free. Parallelism needs declared or detectable conflicts; resource types need a new language; EUTXO's determinism costs shared state. Throughput gained by raising hardware requirements is paid for with fewer people able to run a validator.

Common misconceptionsH

Common misconceptions

Misconception

"A smart contract is a legal contract." It is a program. It may implement part of an agreement, but it knows nothing of intent, fairness or law, and courts in many countries are still working out how code-based arrangements relate to legal ones. "Code is law" is a slogan, not a legal principle.

Misconception

"Gas limits make contracts slow; Ethereum just has a slow CPU." Gas measures the cost to every node of storing and executing a transaction. The binding constraint is the state that all nodes must keep forever and the time that the slowest acceptable node needs to verify a block, not any single machine's speed.

Misconception

"Deployed contracts can never change." The bytecode at an address cannot change, but a proxy can point to new code, an administrator key can pause a contract or move funds if the code allows it, and the platform itself can change through hard forks. Always ask who holds which keys.

Misconception

"If it's on the blockchain, it's true." A contract faithfully records what it was told. An NFT's metadata, an oracle's price or a token's claim to represent a dollar are all only as good as whoever stands behind them off the chain.

Exercises

Exercises

1. Ethereum and smart contracts

Warm-upHuman

Using the lesson's gas schedule (PUSH1, ADD: 3 gas each; SSTORE of a non-zero value into an empty slot: 20,000; STOP: 0), how much gas does this program use when storage is empty?

PUSH1 2 PUSH1 3 ADD PUSH1 0 SSTORE STOP

2. Ethereum and smart contracts

CoreHuman

Solidity assigns state variables to 32-byte storage slots in order of declaration, starting at slot 0. A variable goes into the current slot if it still fits; otherwise it starts the next slot. A mapping always takes a whole slot of its own (its entries live at hashed slots elsewhere), and the variable after it starts a new slot.

contract Shop {
    uint128 sold;                         // 16 bytes
    uint128 refunded;                     // 16 bytes
    address owner;                        // 20 bytes
    bool paused;                          // 1 byte
    uint256 total;                        // 32 bytes
    mapping(address => uint256) credit;
    uint64 openedAt;                      // 8 bytes
}

In which slot is openedAt stored?

3. Ethereum and smart contracts

CoreHuman

Which of these are genuine reasons Ethereum charges gas for each instruction? Select all that apply.

Choose all that apply

4. Ethereum and smart contracts

CoreComputational

Implement the core of an ERC-20 token in Python. Complete transfer, approve and transfer_from in the class below.

  • transfer(sender, to, amount) moves amount from sender to to and records ("Transfer", sender, to, amount) in events.
  • approve(owner, spender, amount) sets (does not add to) the allowance of spender over owner's tokens and records ("Approval", owner, spender, amount).
  • transfer_from(spender, owner, to, amount) moves owner's tokens to to if spender is allowed at least amount, reduces the allowance by amount, and records a Transfer event.
  • All three return True. Like a Solidity revert, any failure (negative or non-integer amount, insufficient balance or allowance) raises ValueError and changes nothing, not even the event list.
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

5. Ethereum and smart contracts

CoreComputational

Write encode_transfer(to, amount), which returns the calldata for the ERC-20 call transfer(to, amount) as a lowercase hex string without 0x: the 4-byte selector a9059cbb, then the address left-padded with zeros to 32 bytes, then the amount as a 32-byte big-endian integer.

to is a 20-byte address in hex, with or without a 0x prefix, in either case. Raise ValueError if the address is not 40 hex digits, or if amount is not an integer in the range of uint256 (0≤0 \le amount <2256< 2^{256}).

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

6. Ethereum and smart contracts

CoreHuman

A lending protocol is deployed behind an upgradeable proxy whose administrator is a 3-of-5 multisig, with a 48-hour timelock on upgrades. Which statement best describes the trust a depositor places in it?

Choose one

7. Ethereum and smart contracts

StretchHuman

Which statements about other smart-contract platforms are correct? Select all that apply.

Choose all that apply

Further reading and resources