A lean, modular C++17 blockchain prototype focused on clarity, testability, and education.
Table of Contents
cpp-blockchain is a small, opinionated blockchain prototype written in modern C++ (C++17).
It prioritizes readable code, modular components, and easy experimentation with core concepts like blocks, transactions, chaining, and consensus primitives.
Use it to learn, prototype consensus ideas, or as a foundation for demos and tests.
- Minimal block and chain implementation with cryptographic hashing.
- Modular components:
Block— holds transactions, metadata, and previous hash.Chain— verifies and stores blocks.Miner— (toy) proof-of-work illustrative miner.Network— pluggable mock networking for tests/demos.
- Unit-tested core logic (fixtures + deterministic tests).
- Clean CMake-based build (Ninja generator supported).
A compact, modular layout:
src/— implementation (blocks, chain, miner, utils).include/— public headers.tests/— unit and integration tests.CMakeLists.txt— build orchestration.
Mermaid overview:
graph TD;
A[blocks] -->|ijson| B{chain};
B -->|m| C[wallet];
B -->|n| D[miner];
B -->|o| E[network];
D -->|extra| B;
Prerequisites:
- CMake >= 3.15 (project built with CMake 3.31 in CI).
- Ninja (recommended).
- A C++17 toolchain (MSVC, clang, or gcc).
Clone and build:
git clone https://github.com/gnatykdm/cpp-blockchain.git
cd cpp-blockchain
cmake -B build -S . -G Ninja -DCMAKE_BUILD_TYPE=Release
ninja -C buildRun tests (if present):
cd cpp-blockchain
cmake -B build -S . -G Ninja -DCMAKE_BUILD_TYPE=Debug
ninja -C build testRun a simple node (example CLI):
./build/nodeCreate/add a block (pseudo-CLI):
echo "Creating block..."Inspecting chain (programmatically):
#include "chain.h"
void inspect_chain(const Chain& chain) {
for (const auto& block : chain.get_blocks()) {
std::cout << "Block: " << block.get_hash() << std::endl;
}
}(See examples/ for runnable snippets.)
- CMake: prefer out-of-source builds via
-B build -S .. - Generator:
Ninjais recommended for fast incremental builds. - Compiler flags: use
-Wall -Wextra -Wpedanticand sanitize in CI for debug builds. - Formatting: follow a consistent style; run
clang-formator your configured formatter. - Tests: add unit tests under
tests/and integrate with CTest.
Helpful commands:
# Clean build artifacts
ninja -C build clean
# Re-run CMake configuration
cmake -B build -S .
# Build with specific target
ninja -C build <target_name>Contributions are welcome. Suggested workflow:
- Fork the repo.
- Create a feature branch:
git checkout -b feat/your-idea. - Add tests for new behavior.
- Open a PR with a clear description and context.
Please follow the existing module boundaries and keep functions small and testable.
This project is released under the MIT License. See LICENSE for details.
Enjoy exploring blockchain internals. If you'd like, add an example issue or feature request and start a conversation!