Study notesWeek 3: Bitcoin
Week 3

Bitcoin

Outcomes

What You Will Be Able To Do

Origin

The 2008 Whitepaper

UTXO model

Coins As Unspent Outputs

Inputs and outputs

Anatomy Of A Transaction

Inputs:   UTXO(7 BTC), signed by Alice
Outputs:  5.0   -> Bob
          1.999 -> Alice (change)
Fee:      0.001 BTC
Double spend check

Spending An Output Once

Header and body

Anatomy Of A Block

Header { version, prev_hash, merkle_root,
         time, target, nonce }
Body   { coinbase, tx1, tx2, ... }
Proof of work

Mining: Find A Lucky Nonce

Live code

Demo: Toy Miner

import hashlib, itertools

def mine(data, difficulty):
    prefix = '0' * difficulty
    for nonce in itertools.count():
        text = f'{data}{nonce}'.encode()
        digest = hashlib.sha256(text).hexdigest()
        if digest.startswith(prefix):
            return nonce, digest

print(mine('block 1', 4))
Cost of rewriting

Why Proof Of Work Secures History

Stable rhythm

Difficulty Adjusts Itself

ParameterValue
Target block timeAbout 10 minutes
Adjusts every2016 blocks
RoughlyTwo weeks
Issuance

Block Reward And Halving

014284256502512.56.253.12520092012201620202024
Supply cap

Why 21 Million

reward, total = 50, 0
while reward >= 1e-8:
    total += 210000 * reward
    reward = int(reward * 1e8 / 2) / 1e8
print(total)
Roles

Nodes, Miners And Wallets

Week 3 in five lines

Recap

Week 4

Next Week: Consensus

Hands-on

Questions And Exercise