Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

UTXOs and a transaction on regtest

After this lesson: I can work out a transaction's fee from its inputs and outputs.

60 min lesson3 commandsBitcoin Script lab: timelocks, multisig, Miniscript and Taproot on a regtest node

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

WordWhat it meansExample
Outputan amount locked by a scriptoutput 0: 10 coins to Bob
UTXOan unspent transaction output
Inputa pointer to the output a transaction spendstxid and output number
Feeinputs minus outputs, kept by the miner
Regtesta private test chain where you mine the blocks
Coinbasethe 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

Command
docker exec bitcoind bitcoin-cli getblockcount

Shown 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

Command
docker exec bitcoind sh steps/utxo.sh

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: what the node printed

OutputCoinsTypeLocked by
Block 1 rewardnone printedspentAlready used up
010witness_v0_keyhashBob's key
15witness_v0_scripthashA timelock, lesson 2
25witness_v0_scripthashTwo of three keys, lesson 3
35witness_v1_taprootTaproot, lesson 4
45witness_v0_scripthashA Miniscript policy, lesson 3
519.99999374witness_v0_keyhashAlice's change

The cells students fill in are left blank in this preview.

Try it: work out a fee

Code: python
# 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

Command
docker exec jupyter python3 steps/decode.py

Shown 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.

About this lab and its source: Bitcoin Script lab: timelocks, multisig, Miniscript and Taproot on a regtest node

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.