A Bitcoin full node implementation in Nim.
Maybe you've wondered what it takes to validate a Bitcoin transaction from scratch. nimrod is a from-scratch Bitcoin full node written in Nim that aims to do exactly that. It leverages Nim's Python-like syntax with C-level performance.
- Project scaffold and module structure
- Primitive types (TxId, BlockHash, Satoshi)
- Binary serialization (BinaryWriter/BinaryReader, CompactSize)
- SegWit transaction serialization (witness data, marker/flag)
- txid and wtxid computation
- Cryptographic hashing (SHA-256d, RIPEMD-160, HASH160)
- secp256k1 bindings (ECDSA/Schnorr via libsecp256k1 FFI)
- Base58Check encoding (legacy P2PKH, P2SH addresses)
- Bech32/Bech32m encoding (segwit P2WPKH, P2WSH, P2TR addresses)
- Address encoding/decoding for all Bitcoin address types
- Script interpreter (P2PKH, P2SH, P2WPKH, P2WSH, P2TR)
- BIP-16 P2SH push-only scriptSig enforcement (unconditional consensus rule)
- BIP-141 witness pubkey type enforcement (compressed keys only in witness v0)
- BIP-146 NULLFAIL enforcement (failed sig checks require empty signatures)
- BIP-147 NULLDUMMY enforcement (CHECKMULTISIG dummy must be empty)
- Witness cleanstack enforcement (exactly one true element after execution)
- MINIMALIF enforcement (OP_IF/NOTIF argument must be empty or @[0x01])
- Sighash computation (legacy with FindAndDelete/OP_CODESEPARATOR, segwit v0, taproot)
- Consensus parameters (mainnet, testnet3, testnet4, regtest, signet)
- Difficulty adjustment (mainnet retarget, testnet 20-min rule, BIP94 time-warp fix)
- Genesis block construction and verification
- Block/transaction validation
- Sigop cost counting with witness discount (BIP-141: 80K limit, legacy 4x, witness 1x)
- BIP-68 sequence locks (relative lock-time enforcement in block validation)
- RocksDB storage (column families via FFI, UTXO set, block index)
- Chainstate management (atomic block connect/disconnect, write batches)
- UTXO set manager (cache, coinbase maturity, reorg support)
- Undo data storage (flat file rev*.dat, checksums, block disconnect)
- Flat file block storage (blk*.dat files, 128 MiB max, 16 MiB pre-alloc)
- P2P message serialization (typed case object, all message types)
- Peer connection (TCP, message framing, version handshake, ping/pong)
- Peer manager (DNS discovery, connection limits, banning, message routing)
- Block synchronization
- Headers-first sync (256-bit work calculation, most-work chain selection)
- Full initial block download (parallel download, sliding window, adaptive timeouts)
- Header sync anti-DoS (PRESYNC/REDOWNLOAD two-phase sync, commitment verification)
- Transaction mempool (fee/size policy, CPFP tracking, eviction, ancestor/descendant limits)
- Fee estimation (histogram-based, 85% confirmation threshold)
- Block template generation (BIP-34 coinbase, witness commitment, sigops limit, locktime finality, anti-fee-sniping)
- JSON-RPC server (getblockchaininfo, getnetworkinfo, getpeerinfo, getmempoolinfo, getrawmempool, estimatesmartfee, validateaddress, getblocktemplate, batch requests)
- HD Wallet (BIP-32/39/44/49/84/86 key derivation, all address types)
- Coin selection (Branch-and-Bound exact match, Knapsack fallback)
- SQLite wallet storage (keys, UTXOs, transactions)
- Wallet RPC (getnewaddress, getbalance, listunspent, getwalletinfo)
- Coinbase maturity enforcement (100-block delay before spending)
- Wallet encryption (AES-256-CBC, passphrase-based key derivation)
- Address labels (setlabel, getaddressesbylabel, listlabels)
- Unified CLI (subcommands for node, RPC, wallet; config file; signal handlers)
- Comprehensive test suite (unit tests, integration tests, Bitcoin Core vectors)
- Performance optimization (parallel sig verification, UTXO cache, RocksDB tuning)
- BIP-9 version bits (soft fork signaling state machine, deployment tracking)
- Misbehavior scoring (100-point threshold, persistent ban list, RPC commands)
- Pre-handshake message rejection (VERSION/VERACK enforcement, protocol version check, self-connection detection, 60s timeout)
- Inventory trickling (Poisson-distributed tx relay, per-peer queues, immediate block relay)
- Stale peer eviction (chain sync timeout, ping timeout, headers timeout, extra outbound eviction)
- Checkpoint verification (minimum chain work, assume-valid, fork rejection below checkpoints)
- sendrawtransaction RPC (maxfeerate validation, idempotent mempool handling, inv broadcast)
- getrawtransaction RPC (mempool lookup, txindex lookup, blockhash parameter, verbose output with asm/address)
- Multi-layer UTXO cache (CoinsView hierarchy, dirty/fresh tracking, memory-aware flushing)
- Block pruning (auto/manual deletion of old blk/rev files, 550 MiB minimum, 288-block safety margin)
- Compact block relay (BIP-152 short ID, cmpctblock/getblocktxn/blocktxn, mempool reconstruction)
- Cluster mempool (connected component clustering, greedy linearization, chunk-based mining scores, cluster size limits)
- Feerate diagram RBF validation (diagram construction, interpolation, strict improvement comparison)
- Pay-to-Anchor (P2A) script detection (BIP-TBD: OP_1 <0x4e73> anyone-can-spend for CPFP fee bumping)
- REST API (read-only endpoints: block, headers, tx, getutxos, mempool in JSON/binary/hex formats)
nimble build
./nimrod --help
./nimrod --network=regtest start
./nimrod --network=regtest getinfonimrod/
├── src/
│ ├── nimrod.nim # Unified CLI entry point
│ ├── primitives/ # Core types and serialization
│ ├── crypto/ # Hashing, secp256k1, address encoding
│ ├── script/ # Script interpreter
│ ├── consensus/ # Params and validation
│ ├── storage/ # RocksDB, chainstate, undo files
│ ├── network/ # P2P messaging and sync
│ ├── mempool/ # Transaction pool
│ ├── mining/ # Fees and block templates
│ ├── rpc/ # JSON-RPC server
│ ├── wallet/ # Full wallet: BnB/Knapsack coin selection, SQLite
│ └── perf/ # Benchmarking and parallel verification
├── tests/ # Test suites with Bitcoin Core vectors
│ └── data/ # Test vectors (script, BIP-32, addresses)
└── resources/ # BIP39 wordlist
nimble test