Bitcoin Consensus and Ethereum Smart Contracts

Classified in Computers

Written on in English with a size of 106.85 KB

Bitcoin Consensus

The Problem: Decentralized Network

  • Bitcoin operates without a central authority; all users must agree on transaction history.
  • It faces the Byzantine Generals Problem; although unknown to each other, participants must establish trust.
  • Trust is established using consensus protocols, incentives, and cryptography.

How It Works

  • The Bitcoin algorithm operates on nodes.
  • Any device (computer) running the full Bitcoin software and connected to the internet acts as a connection point for creating blocks and verifying transactions.
  • When a block is created, nodes communicate to verify the transactions and ensure they follow the algorithm's rules.
  • Each node maintains a complete copy of the Bitcoin blockchain.

Participants in the Ecosystem

  • Full Nodes: Store the entire blockchain and validate all transactions.
  • Miners: Carry out Proof of Work to create new blocks and validate previous ones (often grouped in mining pools).
  • Wallets (Users): Create and store key pairs (public and private), manage keys, view balances, and request/send/receive transactions. (Wallets do not store Bitcoin, only the keys needed to access it).
  • Mempool: A pool of unconfirmed transactions waiting to be processed by miners and added to a block.

Bitcoin Consensus Mechanism

Problem: Sometimes two miners create blocks at the same time, causing a split in the blockchain.

Solution: Bitcoin follows the longest chain rule; the network accepts the chain with the most blocks and work performed.

Why Do Miners Work?

  • They are rewarded with Bitcoin for creating new blocks.
  • Each new block contains a coinbase transaction that generates new Bitcoin. Miners earn money in two ways: block rewards (new Bitcoin) and transaction fees (paid by users for faster processing).

Mining and Hashing

  • Miners use powerful computers to solve complex problems, a process called hashing.
  • Miners take transaction data and run it through hash functions, testing random numbers (nonces) until they find a valid hash.
  • The first miner to solve the puzzle adds the block to the blockchain and earns the reward.

Bitcoin Transactions

A Chain of Blocks

  • Blocks are immutable due to cryptographic hashes.
  • New blocks occur every 10 minutes, linked to the previous block via hashes.
  • Distributed hash trees (Merkle Trees) maintain data integrity.

Coinbase Transaction (Bitcoin Creation)

  • The only way new Bitcoins are created.
  • Miners receive 3.125 BTC per block as a reward.
  • This incentivizes miners to validate transactions and secure the network.
  • The reward halves every 210,000 blocks (~4 years) to control supply.

Miners and Proof of Work (PoW)

  • Miners validate transactions and create new blocks by solving complex hash calculations.
  • They repeatedly calculate hashes until they find a valid block with 19 leading zeros.
  • Once solved, the miner adds the block and claims the reward.

Transaction Format

  • Each transaction output becomes the input for the next transaction.
  • If Input > Output, the difference is the transaction fee paid to the miner.
  • Coinbase transaction outputs cannot be spent until 100 blocks are added, preventing issues like orphaned blocks.

Transaction Prioritization and Mempool

The mempool is a waiting area for validated but unconfirmed transactions. Users can use Replace-by-Fee (RBF) to increase fees for faster processing if a transaction is stuck.

UTXO (Unspent Transaction Output)

  • Represents active money in wallets waiting to be spent.
  • Wallets search the UTXO list for matching transactions to speed up validation.
  • To check a balance, all UTXOs linked to an address are added up.

Bitcoin Scripts

Each transaction output contains a script defining how it can be spent. Common types include P2PK, P2PKH, Multisig, and P2SH.

Blockchain Wallets

Wallets connect to the blockchain to check UTXOs. They use public keys to receive funds and private keys to sign transactions. BIP 39 allows for a 12-word seed phrase to regenerate keys if a wallet is lost.

Ethereum

Ethereum vs. Bitcoin

  • Bitcoin is a digital currency; Ethereum is a smart contract platform using an account-based ledger.
  • Ethereum reaches consensus via Proof of Stake (PoS) rather than energy-intensive Proof of Work.
  • Ethereum uses account-based state transitions rather than UTXOs.

Smart Contracts

Smart contracts are self-executing agreements where terms are written in code. They automate transactions when specific conditions are met, acting as a "trust machine" without intermediaries.

Decentralized Applications (dApps)

dApps run on blockchain networks instead of centralized servers, making them censorship-resistant. They typically consist of a smart contract, a frontend UI, and a wallet integration (e.g., MetaMask).

AqRqT2F1ZeQ7AAAAAElFTkSuQmCC

Technical Challenges

The Blockchain Trilemma

  • Decentralization: No central control.
  • Security: Protection against fraud.
  • Scalability: Ability to process transactions quickly.

Scalability Solutions

  • Layer 2: Increases speed and reduces costs (e.g., Rollups).
  • Layer 1: Modifies the blockchain protocol itself.
  • Layer 0: Improves cross-chain interoperability.

Types of Blockchain

  • Public: Fully decentralized.
  • Private: Controlled by one authority.
  • Consortium: Controlled by a group.
  • Hybrid: Centralized control with some decentralized processes.

Exam Questions

What is cryptography?
The practice of securing information using mathematical techniques to ensure confidentiality, integrity, and authentication.

How does symmetric encryption work?
It uses a single secret key for both encryption and decryption, making it fast but requiring secure key exchange.

How do you provide confidentiality with asymmetric encryption?
By encrypting a message with the recipient's public key, ensuring only their private key can decrypt it.

Why is blockchain considered unhackable?
Because blocks are linked via cryptographic hashes; altering one requires re-mining all subsequent blocks, which is computationally infeasible.

What are the pros and cons of CBDCs?
Pros: Faster transactions and better monetary policy control. Cons: Privacy concerns and risk of bank disintermediation.

Related entries: