A simple blockchain implementation in C++ that demonstrates the fundamental concepts of blockchain technology.
This project implements a basic blockchain with the following components:
- Block structure with hash linking
- Data storage capabilities
- Blockchain management
- File persistence (text and JSON formats)
- Block Structure: Each block contains index, timestamp, data, nonce, hash, and previous hash
- Chain Management: Creating and managing a continuous chain of blocks
- Hashing: SHA-256 hash function to secure block data
- Persistence: Save blockchain to text and JSON files
- Testing: Basic testing functionality
graph TD
A[Start] --> B[Create Genesis Block]
B --> C[Initialize Blockchain]
C --> D{Add New Block?}
D -->|Yes| E[Get Last Block]
E --> F[Create New Block with Previous Hash]
F --> G[Calculate Hash for New Block]
G --> H[Add Block to Chain]
H --> I[Save to File System]
I --> D
D -->|No| J[Display Blockchain]
J --> K[End]
subgraph "Block Structure"
L[index] --> M[Block]
N[timestamp] --> M
O[data] --> M
P[nonce] --> M
Q[hash] --> M
R[prevHash] --> M
end
subgraph "Hash Generation"
S[Combine Block Data] --> T[Apply SHA-256]
T --> U[Convert to Hex String]
U --> V[Return 16-char Hash]
end
- C++ compiler (g++ recommended)
- OpenSSL library
- nlohmann/json library
# Navigate to project directory
cd d:\projectB
# Compile with OpenSSL and JSON include paths
g++ blockChain.cpp -o blockChain -IC:/OpenSSL-Win64/include -IC:/path/to/project/include -LC:/OpenSSL-Win64/lib -lssl -lcrypto
# Run the compiled program
.\blockChain- Run the program
- Enter the number of blocks to create
- For each block, provide the data to store
- The program will display the blockchain after adding all blocks
- Blockchain data is saved to
blockChain.txtandblockchain.json
info: Class to store data/transactionsblock: Class representing a single unit in the blockchainblockChain: Class for managing the chain of blocks- Utility functions for hashing, file I/O, and testing
- Add validation, security mechanisms, and digital signatures
- Implement Proof of Work (PoW) mining with nonce and difficulty level
- Add error handling and testing features
- P2P network implementation
Mansvi Kumar