Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 41 ยท Building a Toy Blockchain

UTXO vs Account Model

Compare Bitcoin-style UTXOs with Ethereum-style account balances and nonces, including change, double-spend checks and trade-offs, with two live Python ledgers.

A ledger must answer one question reliably: who owns what right now? Blockchains have settled on two answers. Bitcoin and its relatives keep a set of unspent coins (unspent transaction outputs, or UTXOs). Ethereum and most smart-contract chains keep accounts with a balance and a counter, much like a bank.

Both models prevent the same thing, the double spend, but they do it in different ways and make different trade-offs for privacy, parallelism and programmability. In this lesson you will build a tiny ledger of each kind, run the same attack on both, and compare. We leave out signatures here (you built them in Transactions and the Mempool) and use a plain signer field so the ownership rules are easy to see.

The UTXO model: coins that are spent whole

Think of cash in a wallet. If you hold a 20 and a 5 and want to pay 12, you cannot tear a banknote: you hand over the 20 and receive 8 back as change. The UTXO model works the same way.

  • The ledger state is a set of unspent outputs, each with an owner and an amount.
  • A transaction consumes one or more existing outputs completely (its inputs) and creates new outputs.
  • The total of the inputs must be at least the total of the outputs. The difference is the fee, which goes to the miner.
  • Any value you do not send to someone else must go back to yourself in a change output. If you forget it, the leftover becomes fee.
flowchart LR
    I["Input: alice coin<br/>50"] --> T["Transaction"]
    T --> O1["Output: bob<br/>30"]
    T --> O2["Output: alice change<br/>19"]
    T --> F["Fee: 1"]

One 50-coin output is consumed whole. Bob receives 30, Alice gets 19 back as change and 1 goes to the miner.

A UTXO transaction is valid when: every input exists in the current unspent set, no input appears twice, the signer owns every input, all outputs are positive, and inputs cover outputs.

Python A UTXO ledger and a double-spend attempt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output is (the coin id differs only if you change the code):

TEXT
pay Bob 30 with change:
  accepted 354e79 (fee 1)
   354e79:1  alice  19
   354e79:0  bob    30
try to spend the same coin again:
  rejected: input 05862d:0 missing or already spent
Bob tries to spend Alice's change:
  rejected: input belongs to alice, not bob
balances: {'alice': 19, 'bob': 30, 'carol': 0}

The double spend is stopped by absence. Once the coin is consumed it no longer exists in the unspent set, so a second transaction that refers to it has nothing to consume. To check a transaction a node needs only the UTXO set, not the whole history.

The account model: balances and nonces

Now think of a bank. The state is a table of accounts, and each account has a balance. Spending subtracts from the sender and adds to the recipient. There are no coins, no change and no inputs.

But then what stops a double spend? Two things:

  • The balance check: a transaction fails if the sender's balance cannot cover it at the moment it is applied. Two payments of 30 from a balance of 50 cannot both succeed.
  • The nonce: each account has a counter, and a transaction must carry the next number. The same signed transaction cannot be applied twice (replay), and two different transactions with the same nonce cannot both be valid, because only one can be next.
Python An account ledger: balances and nonces

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output is:

TEXT
pay Bob 30:
  accepted (fee 1)
replay the same transaction:
  rejected: nonce 0, expected 1
a new payment of 30 to Carol (valid nonce, too little balance):
  rejected: balance 19 cannot cover 31
a payment of 10 to Carol:
  accepted (fee 1)
balances: {'alice': 8, 'bob': 30, 'carol': 10}
nonces:   {'alice': 2}

In an account ledger the double spend is stopped by order: transactions from one account are applied one after another, and each one sees the result of the one before it.

Side by side

UTXO model Account model
State Set of unspent outputs Table of balances and nonces
A payment Consumes whole outputs, makes new ones Subtracts and adds to two balances
Double-spend check The input must exist in the set Balance and nonce check
Order of one owner's payments Independent unless they share coins Strictly sequential by nonce
Parallel validation Easy: different coins do not interact Harder: same account must be ordered
Privacy New addresses per payment are natural One address tends to be reused
Smart contracts Awkward; state is spread over coins Natural; a contract is an account with storage
Balance lookup Sum all outputs of an owner Read one number
Typical users Bitcoin, Litecoin, Cardano (extended) Ethereum and most EVM chains

Neither is better in every way. UTXOs make validation simple and parallel, because two transactions that spend different coins can never conflict, and they make it easy to prove a payment was valid. Accounts make programmable state and balance queries simple, but need nonces and sequential handling per account, and are more exposed to ordering games such as front-running (see Smart Contract Security Pitfalls).

Coin selection and change

In the UTXO world a wallet does not hold a balance. It holds a collection of coins and must decide which ones to use for each payment. This is called coin selection, and it affects fees (more inputs mean a bigger transaction) and privacy (combining coins links them to the same owner).

Python Choosing coins and making change

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output is:

TEXT
wallet: [5, 10, 20, 40] balance: 75
pay  33: spend [40], change 7
pay  40: spend [40], change 0
pay  70: spend [40, 20, 10], change 0
pay  75: spend [40, 20, 10, 5], change 0
pay 100: not enough funds

To pay 33 the wallet spends the 40 and gets 7 back. To pay 70 it needs the 40, the 20 and the 10, and gets nothing back. A wallet that always picks the largest coins spends its big coins first and leaves lots of small ones, which are costly to spend later (dust). Real wallets use smarter strategies, but the principle is the same.

Which model do you build on?

If you build your own chain, ask:

  • Is your main goal simple, parallel validation of payments? UTXOs fit well.
  • Do you need programmable shared state, such as tokens and contracts? Accounts make that much easier, as the lessons on smart contracts will show.
  • Do you care about privacy by default? UTXOs help a bit, though chain analysis still links coins (see Privacy and Zero-Knowledge Proofs).

Some designs mix both. Cardano's extended UTXO model attaches data and scripts to outputs, and some chains let accounts hold UTXO-like notes. The two ideas are more a spectrum than a fork in the road.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a UTXO transaction the inputs total 50 and the outputs total 30. What happens to the missing 20?

    Inputs may exceed outputs. The difference is the fee. A wallet adds a change output so that it does not overpay.
  2. 2. How does an account ledger stop the same signed transaction from being applied twice?

    After the first application the account's nonce moves on, so the replay carries an old nonce and is rejected.
  3. 3. Why can UTXO transactions that spend different coins be validated in parallel?

    Two transactions only conflict if they try to consume the same output, so independent coins can be checked at the same time.
  4. 4. Which model more naturally holds long-lived shared state like a token contract's storage?

    An account can own persistent storage and code, while a UTXO is spent and replaced on each use.
  5. 5. What is coin selection?

    A UTXO wallet holds many coins and must pick a set that covers the payment, creating change if needed.

Summary

  • The UTXO model keeps a set of unspent outputs; a transaction consumes whole outputs and creates new ones, with change returned to the sender.
  • The account model keeps balances and a nonce per account; a transaction subtracts and adds.
  • The UTXO model blocks double spends because a consumed output is gone; the account model blocks them with balance checks and ordered nonces.
  • UTXOs favour simple parallel validation and fresh addresses; accounts favour programmable state and easy balance queries.
  • Wallets in a UTXO system perform coin selection; leftover value must go back as change or it becomes fee.

Next: The Double-Spend Problem and Byzantine Faults, where the question becomes: what if the network itself cannot agree which spend came first?