Understanding the Bitcoin Source Code (2024)

Bitcoin operates based on a decentralized ledger technology known as blockchain. The source code is open-source and written primarily in C++. The Bitcoin Core codebase embodies the principles and mechanisms that have kept the Bitcoin network secure and reliable for over a decade.

Developers and enthusiasts exploring the Bitcoin Core GitHub repository will find a treasure trove of engineering solutions related to distributed systems, cryptography, security, and more. Exploring its complexities and understandings its functionalities offers valuable insights into the world of blockchain and decentralized finance. Here is a high level overview of the key components of the Bitcoin source code:

Thanks for reading BitPublica Bitcoin Mining Newsletter! Subscribe for free to receive new posts and support my work.

Blockchain Management

  • Blocks: Each block comprises a block header, the block size, and the transactions. They are mined approximately every 10 minutes.

  • Chainstate: The UTXO set is stored in a LevelDB database and keeps track of the state of the blockchain, making it crucial for validating transactions.

Proof of Work

  • Mining: The mining process involves finding a hash that meets a specified condition. Miners continually modify the block header and hash it until they find a hash below a target value. This process is computationally intensive and is implemented in src/miner.cpp.

  • SHA-256: Bitcoin utilizes SHA-256 for its PoW algorithm. The crypto/sha256.cpp file contains the relevant implementation, ensuring cryptographic security and consistency in block hash calculations.

  • Target Value: The target value for the required hash adjusts approximately every two weeks (or every 2016 blocks) to ensure a 10-minute block interval. The adjustment logic can be found in src/arith_uint256.cpp and src/pow.cpp.

Transaction Management

  • Validation: The validation.cpp file contains numerous functions that are vital to validating transactions and blocks, ensuring they adhere to the consensus rules.

  • Mempool: The mempool holds transactions that are validated but not yet included in a block. Logic handling mempool operations can be found in txmempool.cpp.

Networking

  • P2P Communication: Found in net.cpp, this section manages peer-to-peer communications, handling connections, message sending, and receiving.

  • Address Management: The addrman.cpp file contains the logic for address management, maintaining a database of known peers in the network.

Wallet

  • Wallet Management: The wallet.cpp file handles operations regarding wallet management, including transaction creation, signature, and key management.

  • BIP32/BIP39: Bitcoin adheres to several Bitcoin Improvement Proposals (BIPs) for handling hierarchical deterministic wallets and mnemonic seed phrases. These implementations can be found across various files in the wallet/ directory.

Scripting

  • Script Interpreter: The script/interpreter.cpp file is tasked with handling Bitcoin's scripting language, enabling functionalities like multi-signature transactions and other complex transaction types.

  • Opcode Handling: Opcode functions in script/script.cpp handle execution of specific operations within the Bitcoin scripting language.

RPC Server

  • RPC Functions: The rpc/ directory includes a myriad of files, each providing functionalities for the Remote Procedure Call (RPC) server, enabling interfacing and interaction with the node.

GUI

  • Qt Framework: Bitcoin’s GUI is built using the Qt framework. The qt/ directory contains files that manage graphical components, user interactions, and wallet management in a graphical format.

Utility Functions

  • Utilities: The util/ directory includes various utility functions and classes that assist in managing system-level interactions, logging, data formatting, and other miscellaneous tasks.

Consensus Mechanism

Bitcoin's consensus mechanism is pivotal for maintaining agreement across its decentralized network, ensuring all nodes have a consistent view of the ledger. It primarily involves transaction validation and block verification based on Proof of Work (PoW). The source code handling the consensus mechanism is primarily written in C++ and is distributed among various files and modules within the Bitcoin Core codebase.

Block Verification:

  • Block Headers: Blocks must adhere to a specific structure, and their headers include metadata, such as the previous block’s hash, a timestamp, the Merkle tree root of the transactions, and a nonce. Header validation logic is found in src/consensus/params.h.

  • Blocksize Limit: There is a maximum block size (currently 1 MB) to prevent spam and DoS attacks. Logic that enforces this is within src/consensus/consensus.h.

Transaction Verification:

  • Signature Verification: Transactions contain cryptographic signatures that must be verified to ensure authenticity and prevent double-spending. The code related to signature verification can be found in src/script/interpreter.cpp.

  • Input/Output Verification: Transactions' inputs and outputs are verified for validity and adherence to consensus rules within src/tx_verify.cpp.

UTXO Set:

  • UTXO Database: Bitcoin maintains the Unspent Transaction Output (UTXO) set, which is stored using LevelDB and managed with code in src/validation.cpp and src/txdb.cpp.

  • UTXO Set Validation: The UTXO set is crucial for validating new transactions. When a new transaction is validated, it’s checked against the UTXO set to ensure that the inputs are valid and unspent.

Merkle Tree:

  • Merkle Root: Each block header contains the Merkle root of the transactions within the block. The Merkle tree allows for efficient and secure verification of content. The relevant code is situated in src/merkleblock.cpp and src/merkleroot.cpp.

Chain Reorganization:

  • Reorg: Occasionally, two miners may solve a block nearly simultaneously, leading to a temporary fork. Bitcoin nodes always consider the longest valid chain (highest cumulative difficulty) to be the correct one. Logic managing reorgs is contained within src/validation.cpp.

Softforks:

Network Propagation:

  • P2P Communication: For the network to reach consensus, it's critical that blocks and transactions propagate through the network efficiently. The code managing P2P communication is located within src/net_processing.cpp and src/net.cpp.

Reward Mechanism:

The reward mechanism in Bitcoin, often referred to as "block rewards," plays a pivotal role in both the issuance of new coins and the security of the network. This process is embedded within the Bitcoin source code and is intrinsically linked to the 21 million Bitcoin supply limit.

  • Coinbase Transaction: The first transaction in a block (the coinbase transaction) awards miners with block rewards and transaction fees. Relevant logic is in src/validation.cpp.

  • Halving: Every 210,000 blocks, the block reward is halved. The code handling this can be located within src/validation.cpp. Initially, the block reward was 50 BTC. It then halved to 25 BTC, 12.5 BTC, 6.25 BTC, and so on. The hard cap of 21 million BTC is achieved by the repeated halving of the block reward. The geometric series converges to a sum of 21 million, ensuring no more bitcoins will be created after reaching that limit. Mathematically: 50+25+12.5+...=21million

Understanding the Bitcoin Source Code (1)

This capped supply and issuance schedule is not explicitly coded as “21 million,” but rather emerges from the block reward mechanism coded into Bitcoin Core. Once the block reward becomes zero (after the last halving, estimated to occur in the year 2140), miners will solely be incentivized by transaction fees. This shift is expected to be seamless since, as the network grows, transaction fees become a more significant portion of the mining reward.

Cryptographic Functions

Bitcoin heavily employs cryptographic functions to secure transactions, control the creation of new coins, and secure the transfer of assets. It utilizes various cryptographic algorithms to attain different goals within the system. The implementation of these cryptographic functionalities can be found in the Bitcoin Core source code, particularly within the src/crypto/ directory.

SHA-256:

  • Usage: SHA-256 (Secure Hash Algorithm 256-bit) is used in Bitcoin's proof-of-work (PoW) mechanism and to create Bitcoin addresses.

  • Location: The SHA-256 implementation can be found in src/crypto/sha256.cpp and src/crypto/sha256.h.

  • Notable Functions: SHA256(), SHA256AutoDetect(), Transform(), etc. facilitate hashing and transform functionalities within SHA-256.

RIPEMD-160:

  • Usage: RIPEMD-160 (RACE Integrity Primitives Evaluation Message Digest) is used alongside SHA-256 to create Bitcoin addresses.

  • Location: The implementation is located in src/crypto/ripemd160.cpp and src/crypto/ripemd160.h.

  • Notable Functions: CRIPEMD160(), Write(), and Finalize() facilitate hashing and message digest creation.

ECDSA:

  • Usage: Elliptic Curve Digital Signature Algorithm (ECDSA) is utilized for generating public and private key pairs and creating digital signatures.

  • Location: ECDSA is implemented using the OpenSSL library and Bitcoin-specific code can be found throughout src/pubkey.cpp and src/pubkey.h.

  • Notable Functions: GenerateNewKey(), Sign(), and Verify() manage key generation, signing, and verification respectively.

SECP256k1:

  • Usage: SECP256k1 refers to the parameters of the ECDSA curve used in Bitcoin and is vital for creating key pairs and signatures.

  • Location: Bitcoin uses the libsecp256k1 library, which can be found as a separate directory: src/secp256k1/.

  • Notable Functions: The library includes various functionalities for elliptic curve operations, such as secp256k1_context_create(), secp256k1_ec_pubkey_create(), and secp256k1_ecdsa_signature_parse_der().

AES:

  • Usage: Advanced Encryption Standard (AES) is used for wallet.dat file encryption.

  • Location: The relevant code can be found in src/crypto/aes.h and src/crypto/aes.cpp.

  • Notable Functions: AES128Encrypt(), AES128Decrypt(), AES256Encrypt(), and AES256Decrypt() handle encryption and decryption using AES.

Base58 Encoding:

  • Usage: Base58 encoding is used for creating human-readable Bitcoin addresses.

  • Location: Base58 functionality is implemented in src/base58.cpp and src/base58.h.

  • Notable Functions: EncodeBase58(), DecodeBase58(), EncodeBase58Check(), and DecodeBase58Check() manage encoding and decoding of Base58 and Base58Check formats.

BIP-38 (Bitcoin Improvement Proposal):

  • Usage: BIP-38 describes a method to encrypt and decrypt private keys using a passphrase.

  • Location: BIP-38 isn’t directly implemented in Bitcoin Core but is utilized in many wallet implementations.

  • Notable Functions: While not in Bitcoin Core, functions in wallet software that implement BIP-38 manage passphrase-based private key encryption and decryption.

BIP-32/39/44:

  • Usage: BIP-32, BIP-39, and BIP-44 define hierarchical deterministic wallets and mnemonic seed phrases for key generation and recovery.

  • Location: These BIPs are typically implemented in wallet software, with related functionality found in various files in src/wallet/.

  • Notable Functions: Functions for managing seed generation, key derivation, and wallet creation according to these BIPs can be found in wallet software source code.

Merkle Tree:

  • Usage: Merkle Trees are utilized to efficiently and securely verify the content of large data sets.

  • Location: The relevant implementation is located in src/merkleblock.cpp and src/merkleroot.cpp.

  • Notable Functions: ComputeMerkleRoot(), BlockWitnessMerkleRoot() and others help in computing the Merkle root and managing Merkle blocks.

The aforementioned cryptographic functions provide a foundation for the security, integrity, and robust functionality of Bitcoin. The implementations within the Bitcoin Core source code are critically audited and optimized for performance and reliability, given the high-stakes environment in which they operate.

Data Storage

LevelDB is an open-source library developed by Google for on-disk key-value storage. Within the Bitcoin Core software, LevelDB is used to manage various databases crucial to functionality, particularly in storing the block index and the chain state.

Role of LevelDB in Bitcoin Core:

  1. Block Index: The block index stores metadata about all known blocks (whether they're part of the main chain or not). This includes the block header, the height, and the transactions included in the block.

  2. Chain State: The chain state (or UTXO set) holds the state of the entire blockchain and is fundamental for validating new blocks and transactions. It contains all unspent transaction outputs (UTXOs), along with some metadata like the block height and block hash.

  3. Mempool: The mempool (memory pool) stores unconfirmed transactions before they are included in a block. Although the mempool itself is stored in memory, a node may utilize LevelDB to persist mempool data across restarts.

  4. Peers Data: Peer information might also be stored using LevelDB to keep track of known nodes in the Bitcoin network, facilitating quicker peer discovery and network reconnection upon node restarts.

  5. Wallet: Although the wallet data in older Bitcoin Core versions was managed using Berkeley DB, newer versions allow wallet data (like keys, transactions, and scripts) to be managed using SQLite or remain in-memory.

Implementation in Source Code:

The Bitcoin Core source code manages and interacts with LevelDB databases through several crucial files in the src/ directory.

  1. Validation: Validation of new blocks and transactions and the management of the UTXO set are done in:

    • src/validation.cpp: This file contains most of the logic related to block validation and management of the UTXO set.

  2. Database Interaction: Interaction with LevelDB for storing and retrieving block data is implemented in:

    • src/index/blockfilterindex.cpp: Implements the logic for creating, updating, and accessing the block filter index using LevelDB.

    • src/index/base.cpp: Defines base functionalities for different types of block tree databases.

  3. Chain State Management: The management and persistence of chain state through LevelDB are handled in:

    • src/validation.cpp: Contains functions for interacting with the chain state, ensuring it's kept in sync with the blockchain's state.

  4. LevelDB Wrappers: The Bitcoin Core codebase also includes wrapper classes to interact with LevelDB:

    • src/dbwrapper.cpp and src/dbwrapper.h: These files define the CDBWrapper class, which provides an interface to interact with LevelDB, abstracting away some complexities and offering a set of helper functions.

  5. Peer Management: Functions that manage peer data in LevelDB reside in:

    • src/addrdb.cpp: This file contains functions to manage the storage and retrieval of peer data.

  6. Mempool: Logic related to the management of the mempool can be found in:

    • src/txmempool.cpp: Although mempool data resides in memory, this file contains logic for managing transactions awaiting confirmation.

  7. Wallet: While LevelDB isn't directly used for wallet management in recent Bitcoin Core versions, it's worth noting that wallet interactions are handled in:

    • src/wallet/sqlite.cpp: Demonstrates interaction with SQLite for wallet data management.

    • src/wallet/db.cpp: Provides a database environment wrapper, supporting different DB implementations including LevelDB.

LevelDB serves a fundamental role in Bitcoin Core by persistently storing crucial data such as the block index and chain state. The database ensures that the node operates smoothly and retains essential data across restarts, enhancing the robustness and reliability of a node.

Version Management

Version management in Bitcoin primarily relates to managing protocol versions and ensuring nodes are compatible with each other. Understanding and managing version differences is crucial for maintaining network coherence and ensuring nodes can communicate effectively.

Protocol Versions:

  • The protocol version indicates the features and rules supported by a node.

  • Nodes exchange version messages when they connect, enabling them to understand each other's capabilities.

  • You can find the protocol version management in the src/version.h file where PROTOCOL_VERSION is defined.

Soft Forks and BIP9:

  • Soft forks introduce backward-compatible rule changes. Nodes must manage version bits to understand and enforce new consensus rules.

  • BIP9 introduces a method for signaling readiness for new rules using version bits in block headers.

  • Check src/consensus/params.h and src/versionbits.cpp for the implementation details related to version bits and soft forks.

Wallet Versions:

  • Wallet versions ensure that wallet files (wallet.dat) are compatible with the software.

  • Check the src/wallet/wallet.cpp and src/wallet/wallet.h files for wallet version management, where wallet versions are defined and checked.

Peer-to-Peer Message Versioning:

  • P2P messages (like version, verack, getdata, etc.) might change format between different Bitcoin Core versions.

  • src/protocol.h and src/protocol.cpp manage P2P message formats and versioning.

Network Management

Network management encompasses functionalities related to how nodes discover each other, connect, synchronize data, and propagate transactions and blocks across the Bitcoin network.

Node Discovery:

  • New nodes discover peers via DNS seeds, hardcoded seed addresses, and from the previously known peers.

  • Peer discovery is primarily managed in src/net.cpp.

Peer Connections:

  • Nodes connect to peers, exchange data, and validate against consensus rules to stay synchronized.

  • src/net_processing.cpp and src/net.cpp handle peer connection, message sending/receiving, and data validation.

Block and Transaction Propagation:

  • Blocks and transactions are propagated across the network using the P2P protocol.

  • Nodes validate and relay transactions and blocks to ensure network consensus.

  • Check src/net_processing.cpp for block and transaction validation and propagation logic.

P2P Messages:

  • Bitcoin nodes communicate using a binary P2P protocol, exchanging various message types.

  • You can find P2P message management in src/protocol.cpp and src/net_processing.cpp.

Address Manager (addrman):

  • The address manager keeps track of known nodes on the Bitcoin network, managing addresses for establishing new connections.

  • Address management logic is encapsulated within src/addrman.cpp and src/addrman.h.

Ban Manager:

  • Nodes may ban or disconnect from peers that send invalid data or violate protocol rules to maintain network health.

  • Refer to src/banman.cpp and src/banman.h for managing banned peers.

Blockchain Synchronization:

  • Nodes synchronize the blockchain by requesting and validating blocks from peers.

  • Chain synchronization, block validation, and storage logic reside in src/validation.cpp and src/init.cpp.

Fee Management

Transaction fees play a critical role in Bitcoin's incentive structure. Miners prioritize transactions based on fees, and higher fees generally lead to quicker confirmations. Here's how fee management is handled within the Bitcoin Core source code:

Transaction Selection and Mining:

Transactions waiting to be confirmed are stored in the mempool (memory pool). When a miner or a node with mining capabilities prepares a new block, they select transactions from the mempool. While they have the freedom to choose any transaction, in practice, they prioritize transactions offering higher fees because it's more profitable. This transaction selection logic is typically influenced by fee rates.

Files and Functions:

  • src/validation.cpp: Contains functions related to transaction validation and mempool management.

  • src/miner.cpp: Implements the creation of new blocks, where the transaction selection based on fees happens.

Fee Rate Calculation:

Bitcoin Core provides an estimation of the fee rate, given recent blocks and mempool state. This helps users set appropriate fees for their transactions based on their desired confirmation times.

Files and Functions:

  • src/policy/fees.cpp: Implements the logic of fee rate estimation based on past blocks and current mempool state.

Fee Policies:

There are predefined rules in Bitcoin Core to ensure that the network doesn’t get spammed with extremely low-fee transactions, which would burden and bloat the mempool.

Files and Functions:

  • src/validation.cpp: Contains the CheckFeeRate() function that checks if the transaction fee meets the minimum threshold.

  • src/policy/policy.cpp: Contains functions like GetMinRelayTxFee() that set the minimum transaction fee required for a transaction to be relayed by nodes.

Wallet Fee Setting:

When creating a transaction using Bitcoin Core's built-in wallet, users can set custom fees, or they can rely on the software's fee estimation. The software considers various factors like current mempool congestion and recent confirmation fees.

Files and Functions:

  • src/wallet/wallet.cpp: Contains wallet-related functions and has logic related to setting transaction fees when sending bitcoins.

  • src/wallet/coincontrol.h: Defines the CoinControl class, which has attributes that influence fee settings when creating transactions.

Replace-by-Fee (RBF):

RBF is a mechanism that allows users to "replace" a transaction (which hasn’t been confirmed yet) with a new one that offers a higher fee. This is useful if the original transaction fee was set too low and is not getting confirmed in a timely manner.

Files and Functions:

  • src/wallet/wallet.cpp: Functions like CreateTransaction() handle transaction creation with considerations for RBF.

  • src/validation.cpp: Contains logic that checks if a new incoming transaction can replace an existing transaction in the mempool based on the fee rate.

Block Propagation and Fee Filters:

Nodes can set minimum fee filters when propagating blocks to peers. This allows nodes to filter out and not request transactions below a certain fee threshold, optimizing bandwidth and processing.

Files and Functions:

  • src/net_processing.cpp: Contains logic for setting and checking fee filter messages between nodes.

Fee management in Bitcoin is a balance between user priorities (wanting quick confirmations) and miner incentives (maximizing profitability). It's also a protective measure against potential spam attacks. The Bitcoin Core codebase implements a range of functionalities to handle fees, offer fee estimations, and set policies to ensure the network remains efficient and resilient.

This outline is still quite high-level as each of the mentioned aspects encapsulates complex functionalities and logic. If you're inclined to delve even deeper, exploring the Bitcoin source code on GitHub and the developer documentation on the Bitcoin Core developer reference page will yield deeper understanding of how Bitcoin works.

Thanks for reading BitPublica Bitcoin Mining Newsletter! Subscribe for free to receive new posts and support my work.

Understanding the Bitcoin Source Code (2024)
Top Articles
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 5962

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.