1. 21 Million Supply Limit

Bitcoin’s total supply is capped at 21 million BTC. This comes from the block reward halving schedule:

Initial block reward: 50 BTC

Halving every 210,000 blocks (~4 years)

Total BTC = sum of geometric series:

total_btc = 0

block_reward = 50

halving_interval = 210000

while block_reward > 0:

total_btc += block_reward * halving_interval

block_reward /= 2

print(total_btc) # Should output 21000000

2. Block Size Limit

Originally, Bitcoin had a 1 MB block size limit (set by Satoshi). Transactions in a block cannot exceed this limit.

MAX_BLOCK_SIZE = 1_000_000 # bytes

def is_block_valid(block):

return block.size <= MAX_BLOCK_SIZE

3. Difficulty Adjustment

Bitcoin adjusts the mining difficulty every 2016 blocks (~2 weeks) to keep the block interval ~10 minutes.

Formula:

\text{New Difficulty} = \text{Old Difficulty} \times \frac{\text{Actual Time of Last 2016 Blocks}}{20160 \text{ minutes}}

If blocks came too fast → difficulty ↑

If blocks came too slow → difficulty ↓

def adjust_difficulty(prev_difficulty, actual_time_seconds):

target_time = 2016 * 10 * 60 # 2016 blocks * 10 minutes per block

new_difficulty = prev_difficulty * (actual_time_seconds / target_time)

return new_difficulty

4. Block Interval (~10 minutes)

Bitcoin targets a 10-minute interval between blocks.

This is enforced indirectly via mining difficulty: miners must solve Proof-of-Work (PoW) hashes under the target.

Formula for target hash:

\text{hash} \leq \text{target}

import hashlib

def mine_block(data, target):

nonce = 0

while True:

hash_result = hashlib.sha256((data + str(nonce)).encode()).hexdigest()

if int(hash_result, 16) <= target:

return nonce, hash_result

nonce += 1

5. Consensus Rules for Valid Transactions

A transaction is valid if it meets Bitcoin’s rules:

1. Inputs reference unspent outputs (UTXOs)

2. Input amounts ≥ output amounts

3. Signatures are valid

4. ScriptPubKey conditions satisfied

5. No double-spending

def is_tx_valid(tx, utxos):

input_sum = 0

output_sum = 0

for inp in tx.inputs:

if inp.utxo not in utxos:

return False # input does not exist

if not verify_signature(inp.signature, inp.utxo.pubkey):

return False # invalid signature

input_sum += inp.utxo.amount

for out in tx.outputs:

output_sum += out.amount

if input_sum < output_sum:

return False # trying to create coins out of nothing

return True

Reply to this note

Please Login to reply.

Discussion

No replies yet.