No accounts, only outputs
A bank keeps one balance for each customer. Bitcoin keeps no balances at all. It keeps a list of unspent outputs, and a transaction uses some up and makes new ones.
In this module you read that list on regtest, a private chain inside your lab. Its coins have no value, and you can read every byte.
By the end of this lesson
Note
I can work out a transaction's fee from its inputs and outputs.
Key words
| Word | What it means | Example |
|---|---|---|
| Output | an amount locked by a script | output 0: 10 coins to Bob |
| UTXO | an unspent transaction output | |
| Input | a pointer to the output a transaction spends | txid and output number |
| Fee | inputs minus outputs, kept by the miner | |
| Regtest | a private test chain where you mine the blocks | |
| Coinbase | the first transaction in a block, paying the reward |
The cells students fill in are left blank in this preview.
Regtest is a chain you control
When your lab started, its node mined 101 blocks and paid every reward to Alice. A new reward cannot be spent until 100 more blocks are on top, so 101 blocks make the first one spendable.
The node then sent one transaction that split block 1's reward six ways, and mined block 102. The clock in the lab is fixed, so you see the same blocks every time.
Ask the node how long the chain is
docker exec bitcoind bitcoin-cli getblockcountShown here, not run. Press Start lab to type it into your own lab’s terminal.
Coins are outputs, not balances
Every output has an amount and a locking script that says who may spend it. A transaction spends whole outputs, never part of one. So Alice spent block 1's whole reward of 50 coins, and paid her change back to herself as a new output.
The node keeps the set of outputs nobody has spent yet. Once an output is spent, the node forgets it.
List the outputs of the lab's transaction
docker exec bitcoind sh steps/utxo.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: what the node printed
| Output | Coins | Type | Locked by |
|---|---|---|---|
| Block 1 reward | none printed | spent | Already used up |
| 0 | 10 | witness_v0_keyhash | Bob's key |
| 1 | 5 | witness_v0_scripthash | A timelock, lesson 2 |
| 2 | 5 | witness_v0_scripthash | Two of three keys, lesson 3 |
| 3 | 5 | witness_v1_taproot | Taproot, lesson 4 |
| 4 | 5 | witness_v0_scripthash | A Miniscript policy, lesson 3 |
| 5 | 19.99999374 | witness_v0_keyhash | Alice's change |
The cells students fill in are left blank in this preview.
Try it: work out a fee
# One input and two outputs, in sats (100,000,000 sats make one coin).
spent = [20000]
paid = [12000, 7000]
fee = sum(spent) - sum(paid)
print("fee:", fee, "sats")
vsize = 141 # the size the network counts, in virtual bytes
print("fee rate:", fee / vsize, "sat/vB")
# Add a third output of 500 sats and run again. What happens to the fee?
A transaction is bytes you can read
A transaction has a version, its inputs, its outputs, a locktime and, for newer outputs, a witness. Each input names an earlier transaction by its txid and an output number.
The txid is a double SHA-256 hash of the bytes without the witness. The lab's Python reads the same bytes the node sent and works the txid out again.
Read the transaction in Python
docker exec jupyter python3 steps/decode.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Size is counted in virtual bytes
Blocks have a limit on weight. A witness byte weighs 1 and every other byte weighs 4, and the virtual size is the weight divided by 4.
The lab's transaction has a weight of 1249, so 313 virtual bytes. It paid 50 coins in and 4,999,999,374 sats out, which leaves a fee of 626 sats. That is 2.0 sats per virtual byte.
A common mistake
Warning
Many people think the chain stores a balance for each address. It stores outputs only. A balance is a sum that a wallet works out by adding the unspent outputs it can unlock.