Skip to content

hashhog/camlcoin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

375 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

camlcoin

A Bitcoin full node implementation in OCaml.

What is it?

Maybe you've wondered what it takes to validate a Bitcoin transaction from scratch. camlcoin is a from-scratch Bitcoin full node written in OCaml that does exactly that, using algebraic data types for protocol structures, pattern matching for opcode dispatch, and Lwt for async I/O.

Current status

  • Project scaffold and dune build system
  • Core types (transactions, blocks, headers) with algebraic data types
  • Binary serialization (CompactSize, little-endian, segwit)
  • Cryptographic primitives (SHA256d, RIPEMD160, secp256k1 ECDSA)
  • Merkle root computation
  • Address encoding (Base58Check, Bech32/Bech32m, WIF)
  • Script interpreter (opcodes, stack machine, P2PKH/P2SH/P2WPKH/P2WSH/P2TR/P2A, legacy sighash with FindAndDelete/OP_CODESEPARATOR, witness cleanstack, P2SH push-only, MINIMALIF, NULLFAIL)
  • Consensus parameters (difficulty adjustment, testnet walk-back, BIP94, rewards)
  • BIP9 version bits (soft fork activation state machine, signal counting)
  • Storage layer (blocks, UTXOs, chain state, batch writes, flat files)
  • Block and transaction validation (weight, BIP-141 weighted sigops with witness discount, Merkle, coinbase, BIP68 sequence locks)
  • P2P message serialization (version, inv, getdata, blocks, headers, tx)
  • Peer connections and handshake (TCP/Lwt, version/verack, ping/pong)
  • Peer manager and discovery (DNS seeds, connection pool, addr relay)
  • Header synchronization (BIP-130, block locators, proof-of-work tracking, anti-DoS)
  • Header-sync anti-DoS (PRESYNC/REDOWNLOAD strategy, constant memory per peer)
  • Block synchronization (IBD, parallel downloads, chain reorganization)
  • UTXO set with cache (block connect/disconnect, maturity checks, layered cache with batch flushing)
  • Undo data for chain reorganizations (tx_undo, block_undo, checksums)
  • Mempool (fee-rate prioritization, eviction, dependency tracking, full RBF, ancestor/descendant limits 25/25/101kvB with cached counts, v3/TRUC policy, cluster mempool with linearization, P2A anchor outputs)
  • Fee estimation (bucket-based tracking, confirmation time analysis)
  • Block template construction (getblocktemplate, coinbase, witness commitment)
  • CPU miner for regtest (proof-of-work search, nonce iteration)
  • JSON-RPC interface (batch requests, parallel processing)
  • Wallet (BIP-39 mnemonic, BIP-32/44/84/86 derivation, coin selection with BnB+SRD, passphrase encryption with PBKDF2-SHA512, multi-wallet support)
  • Command-line interface
  • Test suite (Alcotest unit tests, QCheck property-based tests)
  • Performance optimization (LRU cache, compact headers, benchmarks, parallel validation)
  • Hardware-accelerated cryptography (libsecp256k1 FFI, batch Schnorr verification, ECDSA fast path)
  • Misbehavior scoring and peer banning (100-point threshold, 24h bans)
  • Pre-handshake message rejection (60s timeout, self-connection detection)
  • Inventory trickling (Poisson-scheduled tx relay, 5s inbound, 2s outbound)
  • Eclipse attack protections (bucketing, multi-criteria eviction, netgroup diversity, anchors)
  • Stale peer eviction (headers timeout, block stalling, ping timeout, 45s check)
  • Checkpoint verification (hardcoded checkpoints, assume_valid, minimum_chain_work)
  • sendrawtransaction broadcast (mempool validation, maxfeerate/maxburnamount, peer relay)
  • getrawtransaction RPC (mempool lookup, txindex, blockhash param, verbose JSON)
  • Flat file block storage (blk/rev files, block index, 128MB file rotation)
  • Block pruning (-prune=N, 550MB minimum, txindex incompatible, 288 block safety margin)
  • Coinbase maturity (100-block delay for coinbase spends, enforced in block validation and mempool)
  • Wallet encryption (encryptwallet, walletpassphrase, walletlock, passphrase change, timeout-based auto-lock)
  • Block indexes (hash index, height index, BIP-157/158 compact block filters with GCS)
  • Compact block relay (BIP 152, SipHash, short IDs, block reconstruction, high-bandwidth mode)
  • Package relay (BIP 331, 1p1c topology, CPFP fee-bumping, topological sort, ephemeral anchors)
  • PSBT (BIP-174, creator/updater/signer/combiner/finalizer/extractor roles, taproot support, RPC interface)
  • Output descriptors (BIP 380-386, checksum, parsing, script generation, range expansion, RPC interface)
  • Miniscript (type system, script generation/decompilation, optimal satisfaction with DP, wsh(miniscript) descriptors)
  • BIP-133 feefilter (Poisson timing, noise rounding, block-relay-only exclusion)
  • AssumeUTXO (BIP 199, snapshot loading, dual chainstate, background validation, UTXO hash verification)
  • REST API (block, tx, headers, chaininfo, mempool, blockhashbyheight, .json/.hex/.bin formats)
  • ZMQ notifications (hashblock, hashtx, rawblock, rawtx, sequence topics, 4-byte LE sequence numbers)
  • Regtest mode (generate, generatetoaddress, generateblock RPCs, instant mining, 150-block halving)
  • Block invalidation (invalidateblock, reconsiderblock RPCs, descendant tracking, chain reorg)
  • BIP324 v2 encrypted transport (ElligatorSwift key exchange, ChaCha20-Poly1305 AEAD, short message IDs)
  • BIP155 addrv2 message support (Tor v3, I2P, CJDNS network addresses)
  • Tor and I2P proxy support (SOCKS5 client, I2P SAM protocol, stream isolation, -proxy/-onion/-i2psam)
  • Bloom filters (BIP 37)

Quick start

opam switch create . 4.14.2 --deps-only -y
eval $(opam env)
opam install . --deps-only --with-test -y
dune build
dune exec camlcoin -- --help
dune exec camlcoin -- --network regtest --debug
dune exec camlcoin -- --benchmark

Project structure

camlcoin/
  bin/main.ml         entry point
  lib/
    types.ml          protocol data types
    serialize.ml      binary serialization
    crypto.ml         hash functions, signatures
    address.ml        address encoding
    script.ml         script interpreter
    consensus.ml      consensus parameters, BIP9 versionbits
    validation.ml     block/tx validation
    storage.ml        block/utxo storage, flat files
    utxo.ml           UTXO set, layered cache with batch flushing
    p2p.ml            network protocol, BIP324 v2 transport, SOCKS5/I2P proxy
    peer.ml           peer connections
    peer_manager.ml   connection pool and discovery
    sync.ml           chain synchronization
    mempool.ml        transaction pool, cluster mempool, linearization
    fee_estimation.ml fee rate estimation
    mining.ml         block template and miner
    rpc.ml            JSON-RPC server
    rest.ml           REST API server
    wallet.ml         HD wallet, BIP-39/44/84/86, coin selection, encrypted storage, multi-wallet
    bip39.ml          mnemonic generation and seed derivation
    block_index.ml    block indexes, BIP-157/158 filters, height index
    psbt.ml           PSBT (BIP-174) multi-party signing
    descriptor.ml     output descriptors (BIP 380-386)
    miniscript.ml     miniscript (type system, compilation/decompilation, satisfaction)
    assume_utxo.ml    assumeUTXO (BIP 199, snapshot loading, background validation)
    zmq_notify.ml     ZeroMQ notification message formatting and queueing
    zmq_socket.ml     ZeroMQ socket publisher/subscriber implementation
    perf.ml           performance utilities and benchmarks
    cli.ml            command line interface
    camlcoin.ml       library interface
  test/               unit tests
  resources/          BIP39 wordlist

Running tests

dune test

About

Bitcoin full node in OCaml

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages