How bitcoin/libblkmaker works
Project Type and Purpose
• This is an open-source C library (libblkmaker) for constructing Bitcoin block templates according to BIP-23, consuming JSON-encoded template data (e.g., from a Bitcoin node’s RPC), rolling headers and time fields, and producing completed block headers/merkle roots for mining.
• Key use cases: embedding into mining software, CLI examples (example.c), test harnesses (test.c, test.sh).
File-Structure Analysis
• Top-level sources:
• blkmaker.c / blkmaker.h – core API for building block headers
• blktemplate.c / blktemplate.h – parsing/manipulating BIP-23 template objects
• blkmaker_jansson.c / blkmaker_jansson.h – Jansson-based JSON I/O glue
• base58.c – Base58 encoding (for block height in coinbase)
• hex.c – hex ⇄ binary conversions
• private.h – internal helpers / constants
• example.c, test.c, testinput.c – CLI/test apps showing usage
• test.sh – test runner
• Build/config: autogen.sh, configure.ac, Makefile.am, .travis.yml, pkgconfig template.
README Insights
• Dependencies: Jansson ≥2.0 (JSON parser), libgcrypt for SHA256.
• Usage: assign a SHA256 implementation via function pointer, call blkmk_get_mdata, respect ntime limits per BIP-23.
• No network client built in—expects JSON on stdin or via caller’s HTTP client.
a. Main Components
• Caller/CLI Layer (example.c, test.c)
• libblkmaker Core API
• JSON I/O Module (blkmaker_jansson)
• Template Parser/Manager (blktemplate)
• Hashing Module (abstracted via blkmk_sha256_impl → libgcrypt)
• Helpers: base58, hex
• External Dependencies: Jansson, libgcrypt, stdin or HTTP RPC input
b. Relationships & Data Flow
1. Caller (CLI or host software) fetches JSON template (stdin or RPC).
2. JSON I/O Module parses into in-memory template struct.
3. Template Manager validates BIP-23 limits, prepares coinbase, merkle branches.
4. Caller sets blkmk_sha256_impl to point at libgcrypt’s SHA256.
5. Core API executes header building and ntime rolling, calling SHA256 via the function pointer.
6. Base58 and hex helpers format outputs (e.g. block height in coinbase, hex-encoded header).
7. Caller receives finalized block header/blob for submission to miner or network.
c. Architectural Patterns & Principles
• Modular C library: separation of concerns by module (JSON, hashing, template logic, encoding).
• Use of function pointers for pluggable crypto implementations.
• Configuration via autoconf/automake for portability.
• Test-driven: example and test harnesses exercise public API.
d. Technologies, Frameworks, Libraries
• Language: C99 (assumed)
• JSON: Jansson 2.x
• Crypto: libgcrypt (or any custom SHA256)
• Build system: Autotools (autogen.sh, configure.ac, Makefile.am)
• CI: Travis CI (.travis.yml)