Overview
The consensus layer defines the rules that determine which blocks and transactions are valid in Bitcoin. These rules must be followed by all nodes to maintain network consensus. Bitcoin Core implements these rules in a modular fashion, with consensus-critical code isolated in specific libraries. Primary locations:src/consensus/- Core consensus validationsrc/script/interpreter.cpp- Script executionsrc/validation.cpp- Block and transaction validationlibbitcoin_consensus- Consensus library
Consensus Architecture
Consensus Parameters
Chain Parameters
Defined insrc/consensus/params.h:
Network-Specific Values
Mainnet:- Target block time: 600 seconds (10 minutes)
- Difficulty adjustment: Every 2016 blocks
- Subsidy halving: Every 210,000 blocks
- Initial subsidy: 50 BTC
- Maximum supply: ~21 million BTC
- Same rules as mainnet
- Minimum difficulty blocks allowed
- Different genesis block
- Instant mining (difficulty = 1)
- No peers required
- Customizable parameters
Block Consensus Rules
Block Header Validation
- Hash < Target: Block hash must be below difficulty target
- Timestamp: Not more than 2 hours in the future
- Version: Recognized version number
- Previous block: Must reference known block
Block Structure Rules
Size and weight limits:- Base size: Transaction size without witness data
- Total size: Complete transaction size including witness
Coinbase Transaction
Requirements:- First transaction in block must be coinbase
- Exactly one coinbase per block
- Coinbase input must have:
- Previous output: null hash (0x00…00)
- Output index: 0xFFFFFFFF
- Script sig: 2-100 bytes (includes height per BIP34)
Transaction Consensus Rules
Transaction Structure
Basic Transaction Rules
- Version: Currently 1 or 2 (version 3 for TRUC)
- Inputs: At least 1 input (except coinbase)
- Outputs: At least 1 output
- Output values: Each output ≥ 0, total < 21M BTC
- Size: ≤ 400,000 weight units
Input Validation
- Referenced output must exist in UTXO set
- Not already spent (no double-spends)
- Script validation passes
- Sequence/locktime constraints satisfied
Output Validation
- Value ≥ 0
- Value ≤ 21M BTC (consensus limit)
- Sum of outputs ≤ sum of inputs (for non-coinbase)
Script Validation
Script Types
Bitcoin supports multiple script types:1. Pay to Public Key Hash (P2PKH)
2. Pay to Script Hash (P2SH - BIP16)
- Verify scriptSig + scriptPubKey
- Extract redeemScript from scriptSig
- Verify redeemScript execution
3. Pay to Witness Public Key Hash (P2WPKH - BIP141)
- Fixes transaction malleability
- More efficient (lower fees)
- Enables Lightning Network
4. Pay to Witness Script Hash (P2WSH - BIP141)
5. Pay to Taproot (P2TR - BIP341)
- Schnorr signatures (BIP340)
- MAST (Merkelized Alternative Script Trees)
- Tapscript (BIP342)
- Better privacy and efficiency
Script Interpreter
Script execution insrc/script/interpreter.cpp:
- Execute scriptSig → produces stack
- Execute scriptPubKey with that stack
- Top stack element must be true
- For P2SH/P2WSH: Additional script execution
Signature Validation
ECDSA (Legacy and SegWit v0)
- DER encoding (BIP66)
- Low-S values (BIP146)
- Proper SIGHASH type
- Valid secp256k1 signature
Schnorr (Taproot/SegWit v1)
- 64-byte signatures (vs ~72 for ECDSA)
- Batch verification
- Key aggregation (MuSig2)
- Provable security
Soft Fork Mechanisms
Buried Deployments (BIP90)
Soft forks activated at specific heights:BIP9 Version Bits
Flexible soft fork deployment:- Bit: 2
- Start time: April 24, 2021
- Timeout: August 11, 2021
- Activation height: 709,632 (November 2021)
- Status: ACTIVE
Implemented BIPs
Bitcoin Core implements numerous BIPs (Bitcoin Improvement Proposals):Consensus Changes
| BIP | Description | Version | Status |
|---|---|---|---|
| BIP16 | Pay to Script Hash (P2SH) | v0.6.0 | Active since 2012 |
| BIP30 | Duplicate txid prevention | v0.6.0 | Active since 2012 |
| BIP34 | Block height in coinbase | v0.7.0 | Buried |
| BIP65 | OP_CHECKLOCKTIMEVERIFY | v0.12.0 | Buried |
| BIP66 | Strict DER signatures | v0.10.0 | Buried |
| BIP68 | Relative lock-time | v0.12.1 | Buried |
| BIP112 | OP_CHECKSEQUENCEVERIFY | v0.12.1 | Buried |
| BIP113 | Median time-past | v0.12.1 | Buried |
| BIP141 | Segregated Witness (consensus) | v0.13.0 | Buried |
| BIP143 | Transaction signature verification (SegWit) | v0.13.0 | Buried |
| BIP147 | NULLDUMMY | v0.13.1 | Buried |
| BIP340 | Schnorr signatures | v0.21.0 | Active |
| BIP341 | Taproot | v0.21.0 | Active |
| BIP342 | Tapscript | v0.21.0 | Active |
| BIP431 | TRUC (v3 transactions) | v28.0 | Active |
Network Protocol
| BIP | Description | Version |
|---|---|---|
| BIP14 | Protocol version and user agent | v0.6.0 |
| BIP31 | Pong message | v0.6.1 |
| BIP35 | Mempool message | v0.7.0 |
| BIP130 | Direct headers announcement | v0.12.0 |
| BIP133 | feefilter message | v0.13.0 |
| BIP152 | Compact block relay | v0.13.0 |
| BIP155 | addrv2 message | v0.21.0 |
| BIP324 | Version 2 P2P transport | v26.0 |
| BIP339 | Wtxid relay | v0.21.0 |
Wallet and Addresses
| BIP | Description | Version |
|---|---|---|
| BIP32 | Hierarchical Deterministic Wallets | v0.13.0 |
| BIP39 | Mnemonic code (external implementation) | - |
| BIP44 | Multi-account hierarchy | v0.21.0 |
| BIP49 | Derivation for P2WPKH-nested-in-P2SH | v0.21.0 |
| BIP84 | Derivation for P2WPKH | v0.21.0 |
| BIP86 | Derivation for P2TR | v23.0 |
| BIP173 | Bech32 address format | v0.16.0 |
| BIP174 | Partially Signed Bitcoin Transactions (PSBT) | v0.17.0 |
| BIP350 | Bech32m address format | v22.0 |
| BIP371 | Taproot fields for PSBT | v24.0 |
| BIP380-385 | Output script descriptors | v0.17.0 |
| BIP386 | tr() descriptors | v22.0 |
Consensus-Critical Code
Isolation Strategy
Consensus code is carefully isolated:- No external dependencies (except crypto)
- Deterministic behavior
- No floating-point arithmetic
- Careful integer overflow handling
- Identical behavior across platforms
libbitcoin_consensus
Extractable consensus library:- Alternative implementations can validate using Bitcoin Core’s consensus
- Third-party verification
- Cross-platform consensus checking
Validation Caching
Script Execution Cache
Caches script execution results:- Signature cache: Individual signature verifications
- Script cache: Full script execution results
- Avoid re-executing scripts during reorgs
- Faster block validation
- Reduced CPU usage
Assumevalid
Skip signature validation for old blocks:- Relies on proof-of-work accumulation
- All other consensus rules still enforced
- Significantly faster initial sync
- Configurable via
-assumevalid=<hash>
Time Locks
Absolute Time Locks
nLockTime (transaction-level):- Script-level absolute time lock
- Compares against nLockTime
- Enables refund transactions
Relative Time Locks
nSequence (input-level - BIP68):- Script-level relative time lock
- Enables payment channels
- Used in Lightning Network
Median Time Past (BIP113)
Time locks compare against median of last 11 blocks:- Prevents miner time manipulation
- More reliable than single block timestamp
Consensus Upgrades in Practice
Activation Process
- Proposal: BIP drafted and discussed
- Implementation: Code merged (inactive)
- Signaling: Miners signal readiness via version bits
- Lock-in: Threshold reached (95% of 2016 blocks)
- Activation: Rules enforced after lock-in period
Recent Example: Taproot
Future Considerations
Potential Soft Forks
Under discussion:- ANYPREVOUT (BIP118) - Signature flexibility for eltoo
- CHECKSIGFROMSTACK - Covenant functionality
- OP_CAT - Concatenation operation
- Cross-input signature aggregation
Consensus Evolution
Challenges:- Backward compatibility
- Decentralized coordination
- Testing and verification
- Long-term maintenance
- Better soft fork deployment mechanisms
- More comprehensive testing (fuzzing, formal verification)
- Clearer activation paths
Related Documentation
- Validation Engine - Validation implementation details
- Architecture Overview - System architecture
- BIPs Documentation - Bitcoin Improvement Proposals