Bitcoin

Into the weeds...

Contents

Mining/Consensus

"Extraordinary claims require extraordinary evidence."

  - Carl Sagan

  • The process by which new blocks are added to the chain

  • Computing a hash of all the data in the block with certain requirements (proof-of-work)

  • Proving to everyone else that Bitcoin has been moved

What is Mining

Consensus

Proof of Work

  • Hashing Algorithm for Bitcoin is SHA-256
  • Difficulty increases as computing power increases
  • Reward is currently 6.25 BTC (The halvening...)
  • Mining Pools
    • Poolin
    • F2Pool
    • BTC.com
    • AntPool
    • More...
  • 51% Attack (CPU)

Block Proposed

Node works on Hash

Node Proposes Block

Other Nodes Confirm Block Hash

Block is Entered into the Ledger

Just a bit more detail...

  • Block Hash: 0x0000000000000000000a3c2821514f18f40fd85359a0fc330729f73f945085ec
  • Confirmations: 1
  • Timestamp: 2020-08-20 10:32
  • Height: 644579
  • Miner: F2Pool
  • Number of Transactions: 2,469
  • Difficulty: 16,947,802,333,946.61
  • Size: 1,251,876 bytes
  • Nonce: 4,247,817,874
  • Transaction Volume: 6157.65614867 BTC
  • Block Reward: 6.25000000 BTC
  • Fee Reward: 0.94561077 BTC

Bitcoin Scripting Language

"Good code is like a work of art; it makes you feel something. And if it makes you feel something, then it's doing its job. Sometimes it makes you want to drool. Sometimes it makes you want to punch the screen. But it always makes you feel something."

  - Unknown.

  • Stack-Based (LIFO)
  • Forth-like (similar to the Forth language)

How does it work?

1 2 OP_ADD 3 OP_EQUAL
2 3
1 1 3 3 TRUE (1)

The Stack

Common Opcodes

Opcode Description
OP_IF, OP_ELSE, OP_ENDIF Flow control. If-else statements
OP_RETURN End a script early - used to mark a UTXO as unspendable
OP_DUP Duplicates the top stack item.
OP_DROP Removes the top stack item.
OP_AND, OP_OR, OP_XOR Bitwise boolean operators
OP_EQUAL Puts True (1) on the stack if true, or False (0) otherwise
OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD Math operators
OP_RIPEMD160, OP_SHA256 Run hashing algorithms on inputs
OP_CHECKSIG The entire transaction's outputs, inputs, and script are hashed. Must be given a valid signature for this hash and public key.
OP_CHECKMULTISIG Compares each signature against each public key until it finds enough ECDSA matches or runs out.

Transactions (UTXO)

"I'm gonna make him an offer he can't refuse."

  - The Godfather

  • Unspent Transaction Output (UTXO)

  • Inputs to the transaction

  • Outputs from the transaction

  • Different types of Transactions

  • Locking Scripts & Unlocking Scripts

    • Locking scripts provide the script that needs to be solved

    • Unlocking scripts provide the correct input to make the locking script work

What is a Transaction?

// Locking Script
2 OP_ADD 3 OP_EQUAL
// Unlocking Script
1

Example Transaction

Example Transaction

{
  "txid": "0627052b6f28912f2703066a912ea577f2ce4da4caa5a5fbd8a57286c345c2f2",
  "hash": "0627052b6f28912f2703066a912ea577f2ce4da4caa5a5fbd8a57286c345c2f2",
  "version": 1,
  "size": 258,
  "vsize": 258,
  "weight": 1032,
  "locktime": 0,
  "vin": [
    {
      "txid": "7957a35fe64f80d234d76d83a2a8f1a0d8149a41d81de548f0a65a8a999f6f18",
      "vout": 0,
      "scriptSig": {
        "asm": "3045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e3813[ALL] 0484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adf",
        "hex": "483045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e381301410484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adf"
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.015,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 ab68025513c3dbd2f7b92a94e0581f5d50f654e7 OP_EQUALVERIFY OP_CHECKSIG",
        "desc": "addr(1GdK9UzpHBzqzX2A9JFP3Di4weBwqgmoQA)#ykrtxd0a",
        "hex": "76a914ab68025513c3dbd2f7b92a94e0581f5d50f654e788ac",
        "address": "1GdK9UzpHBzqzX2A9JFP3Di4weBwqgmoQA",
        "type": "pubkeyhash"
      }
    },
    {
      "value": 0.0845,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 7f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a8 OP_EQUALVERIFY OP_CHECKSIG",
        "desc": "addr(1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK)#e6ft626y",
        "hex": "76a9147f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a888ac",
        "address": "1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK",
        "type": "pubkeyhash"
      }
    }
  ],
  "hex": "0100000001186f9f998a5aa6f048e51dd8419a14d8a0f1a8a2836dd734d2804fe65fa35779000000008b483045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e381301410484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adfffffffff0260e31600000000001976a914ab68025513c3dbd2f7b92a94e0581f5d50f654e788acd0ef8000000000001976a9147f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a888ac00000000",
  "blockhash": "0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4",
  "confirmations": 502454,
  "time": 1388185914,
  "blocktime": 1388185914
}
  • Payment directly to a public Key

Pay to Public Key (P2PK)

// Locking Script
04ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84c OP_CHECKSIG
// Unlocking Script
30440220576497b7e6f9b553c0aba0d8929432550e092db9c130aae37b84b545e7f4a36c022066cb982ed80608372c139d7bb9af335423d5280350fe3e06bd510e695480914f01
// Locking Script
< public_key > OP_CHECKSIG
// Unlocking Script
< signature >
  • Payment to a public key hash (A legacy address!)

Pay to Public Key Hash (P2PKH)

// Locking Script
OP_DUP OP_HASH160 12ab8dc588ca9d5787dde7eb29569da63c3a238c OP_EQUALVERIFY OP_CHECKSIG
// Unlocking Script
304502203f004eeed0cef2715643e2f25a27a28f3c578e94c7f0f6a4df104e7d163f7f8f022100b8b248c1cfd8f77a0365107a9511d759b7544d979dd152a955c867afac0ef78601 044d05240cfbd8a2786eda9dadd520c1609b8593ff8641018d57703d02ba687cf2f187f0cee2221c3afb1b5ff7888caced2423916b61444666ca1216f26181398c
// Locking Script
OP_DUP OP_HASH160 < public_key_hash > OP_EQUALVERIFY OP_CHECKSIG
// Unlocking Script
< signature > < public_key >
  • Payment to a multisig (rarely used)

Pay To Multisig (P2M)

// Locking Script
OP_1 04cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaff7d8a473e7e2e6d317b87bafe8bde97e3cf8f065dec022b51d11fcdd0d348ac4 0461cbdcc5409fb4b4d42b51d33381354d80e550078cb532a34bfa2fcfdeb7d76519aecc62770f5b0e4ef8551946d8a540911abe3e7854a26f39f58b25c15342af OP_2 OP_CHECKMULTISIG
// Unlocking Script
OP_0 304402203f16c6f40162ab686621ef3000b04e75418a0c0cb2d8aebeac894ae360ac1e780220ddc15ecdfc3507ac48e1681a33eb60996631bf6bf5bc0a0682c4db743ce7ca2b01
// Locking Script
< M > < ...public_keys > < N > OP_CHECKMULTISIG
// Unlocking Script
OP_0 < ...signatures >
  • Payment to a script hash (Address starts with 3)

  • Allows someone to define a script for unlocking (like a multisig) and let people send money to it as an address instead of needing to use that script directly

Pay To Script Hash (P2SH)

// Locking Script
OP_HASH160 748284390f9e263a4b766a75d0633c50426eb875 OP_EQUAL
// Unlocking Script
OP_0 3046022100a07b2821f96658c938fa9c68950af0e69f3b2ce5f8258b3a6ad254d4bc73e11e022100e82fab8df3f7e7a28e91b3609f91e8ebf663af3a4dc2fd2abd954301a5da67e701 5121022afc20bf379bc96a2f4e9e63ffceb8652b2b6a097f63fbee6ecec2a49a48010e2103a767c7221e9f15f870f1ad9311f5ab937d79fcaeee15bb2c722bca515581b4c052ae
// Locking Script
OP_HASH160 < script_hash > OP_EQUAL
// Unlocking Script
OP_0 < signature > < redeem_script >
  • Unspendable outputs

  • Store data on the chain

  • Burn Bitcoin

Nulldata

// Locking Script
OP_RETURN 68656c6c6f20776f726c64
// Unlocking Script
... UNSPENDABLE ...
// Locking Script
OP_RETURN < data >
// Unlocking Script
... UNSPENDABLE ...
  • Segwit keyhash (address is 42 characters and starts with bc1p) BIP173

  • Segwit scripthash (address is 62 characters and starts with bc1p) BIP173

  • Taproot transaction (address is 62 characters and starts with bc1q) BIP341 and BIP350

  • Nonstandard - anything that doesn't match a known pattern

Other Types

Bitcoin P2P Network

"I'm not one to gossip, but I heard something that might interest you..."

  - Unknown.

  • Finds a node to connect to (known or using DNS Seeds)

  • Transmits `version` message and receives `verack` message

  • Sends `addr` message

  • Asks for other `addr` messages of more nodes (`getaddr`) - for reliability

  • Full Nodes sync the full blockchain from other full nodes and verify each block

Node enters the Network...

  • Transaction is formed and hashed - the locking/unlocking scripts are set

  • Transaction is broadcast between nodes to their neighbors, and the to other neighbors, and so on

  • Unconfirmed transactions live in the "mempool"

  • After transactions are confirmed, all* the UTXOs (outputs from the transaction) end up in the UTXO pool to be spent later

Transactions

Thanks!

Bitcoin is pretty cool

References

Bitcoin

By Ethan Brouwer

Bitcoin

  • 93