A blockchain agrees on blocks. But what if the thing worth agreeing on isn't just "who sent what" — it's the outcome of actual logic, like an escrow that only pays out if a condition holds? Who runs that logic, and how does everyone trust the result without trusting whoever ran it?
Step through a deposit-then-release script. Every action is a pure function of the current state — no hidden variables, no server making a judgment call. Anyone who replays these same 3 actions against the same starting state lands on the exact same numbers, every time.
- A contract's state is just data
This escrow's state is
{ phase, balance, buyer, seller }— nothing more than that. It starts"created"with balance 0. - Actions transition state deterministically
is a pure function: deposit moves
created → funded, then release or refund movesfunded → releasedorfunded → refunded. Same state, same action, same result — always. - The state hash is what nodes actually agree on
. Two nodes that replay the same actions independently compute the same hash without ever talking to each other — the determinism of the code is the consensus mechanism, not a vote about it.
- This is what 'code is law' means operationally
There's no customer-support line to appeal to. Whatever
applyActioncomputes from the actions actually submitted is final — which is exactly why the code has to be gotten right before it runs, not fixed after.
- phase —
created,funded,released, orrefunded; the contract's only source of "what's happened so far." - action — one of deposit, release, refund; the only way the state is allowed to change.
Final state hash: 400 — anyone replaying these same 3 actions computes this exact number, no coordination required.
Fund the contract, then pick which way it resolves. Both branches run through the exact same
applyAction function — nothing about the code changes based on who's watching or what they'd prefer
the outcome to be.
Alice deposits 50 into escrow with Bob as the seller.
- Created
.
- Funded
Alice deposits 50: phase becomes
funded, balance becomes 50. — a completely different hash, because the state actually changed. - Branch: released
If the condition holds,
releasefires: . - Branch: refunded
If it doesn't,
refundfires instead: . Same starting state, same deposit — a completely different final hash, because the action taken from there was different.
alice deposits 50. Drive the contract to both resolutions — released (the branch where bob gets paid) and refunded (the branch where they don't) — to see that the same contract can end either way.
Seen so far: neither
A smart contract turns "what happened" into a deterministic state machine that every node computes identically — the code itself becomes the thing the network is reaching consensus on, block by block. The next chapters look at what happens when that computation has to scale across many machines at once (sharding), and at how identity and cost interact to keep the whole system honest (Sybil resistance).