Introduction – Blockchain Implementation
Blockchain Implementation is a decentralized and digital ledger that is used to record transaction across multiple computers so that the record cannot be altered retroactively without the alteration of all subsequent blocks and the consensus of the network. Implementing a basic blockchain in C++ involves understanding the fundamental components of blockchain such as blocks, hashing, and the chain itself.
Key Concepts
- Block
- Each block in a blockchain contains data (transactions in most cases), a timestamp, and a reference to the previous block.
- The block is uniquely identified by a hash generated using the SHA-256 cryptographic hash function.
- Hashing
- Hashing is a process of converting input data (in this case, block data) into a fixed-size string of characters using a cryptographic hash function.
- Any change in the input data results in a completely different hash output.
- Chain of Blocks
- Blocks in a blockchain are linked together in a sequential order.
- Each block contains the hash of the previous block, which creates a chain that ensures the integrity of the entire blockchain.
- Consensus Mechanism
- Blockchain networks typically use consensus algorithms (like Proof of Work or Proof of Stake) to agree on the validity of transactions and blocks added to the chain.
Code of the Project
#include <iostream> #include <ctime> #include <vector> #include <sstream> #include <iomanip> #include <openssl/sha.h> // Block structure struct Block { int index; std::time_t timestamp; std::string data; std::string previousHash; std::string hash; Block(int idx, std::string d, std::string prevHash) : index(idx), data(std::move(d)), previousHash(std::move(prevHash)) { timestamp = std::time(nullptr); hash = calculateHash(); } std::string calculateHash() const { std::stringstream ss; ss << index << timestamp << data << previousHash; std::string input = ss.str(); unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, input.c_str(), input.length()); SHA256_Final(hash, &sha256); std::stringstream hashedString; for (unsigned char i : hash) { hashedString << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(i); } return hashedString.str(); } }; // Blockchain class class Blockchain { private: std::vector<Block> chain; public: Blockchain() { // Create the genesis block (first block in the chain) chain.emplace_back(Block(0, "Genesis Block", "0")); } void addBlock(const std::string& data) { const Block& lastBlock = chain.back(); chain.emplace_back(Block(chain.size(), data, lastBlock.hash)); } bool isChainValid() const { for (size_t i = 1; i < chain.size(); ++i) { const Block& currentBlock = chain[i]; const Block& previousBlock = chain[i - 1]; // Check if the current block's hash is valid if (currentBlock.hash != currentBlock.calculateHash()) { return false; } // Check if the previousHash of the current block matches the hash of the previous block if (currentBlock.previousHash != previousBlock.hash) { return false; } } return true; } void printChain() const { for (const auto& block : chain) { std::cout << "Block #" << block.index << "\n"; std::cout << "Timestamp: " << std::ctime(&block.timestamp); std::cout << "Data: " << block.data << "\n"; std::cout << "Hash: " << block.hash << "\n"; std::cout << "Previous Hash: " << block.previousHash << "\n"; std::cout << "------------------\n"; } } }; int main() { Blockchain blockchain; blockchain.addBlock("Transaction Data 1"); blockchain.addBlock("Transaction Data 2"); std::cout << "Blockchain valid? " << (blockchain.isChainValid() ? "Yes" : "No") << "\n\n"; blockchain.printChain(); return 0; }