Last active
February 1, 2024 08:54
-
-
Save ankushKun/73129ffe070d8f1b752d9f28f8b78d0f to your computer and use it in GitHub Desktop.
Smol project we were forced to make during class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////// | |
// WORTHLESS BLOCKCHAIN PROJECT // | |
//////////////////////////////////// | |
// The most basic imitation/implementation of how a Blockchain would work | |
// Still has lots of flaws (╯°□°)╯︵ ┻━┻ | |
#include <ctime> | |
#include <cstring> | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
class Block { | |
public: | |
string previousHash; | |
string currentHash; | |
string data; | |
int index; | |
time_t timestamp; | |
Block(string data, string previousHash, int index) { | |
this->data = data; | |
this->previousHash = previousHash; | |
this->index = index; | |
this->timestamp = time(nullptr); | |
this->currentHash = calculateHash(); | |
} | |
string calculateHash() { | |
string hashStr = | |
previousHash + to_string(timestamp) + data + to_string(index); | |
hash<string> str_hash; | |
return to_string(str_hash(hashStr)); | |
} | |
}; | |
class Blockchain { | |
public: | |
int blockHeight; | |
vector<Block> blocks; | |
Blockchain() { | |
blockHeight = 0; | |
blocks.push_back(generateGenesisBlock()); | |
} | |
Block generateGenesisBlock() { return Block("Genesis Block", "0", 0); } | |
void addBlock(string data) { | |
string previousHash = blocks[blockHeight].currentHash; | |
Block blockToAdd = Block(data, previousHash, blockHeight + 1); | |
blockHeight++; | |
blocks.push_back(blockToAdd); | |
} | |
void printBlocks() { | |
for (int i = 0; i < blocks.size(); i++) { | |
cout << "Block " << blocks[i].index << endl; | |
cout << "Data\t\t" << blocks[i].data << endl; | |
cout << "Timestamp\t\t" << blocks[i].timestamp << endl; | |
cout << "Previous Hash\t\t" << blocks[i].previousHash << endl; | |
cout << "Current Hash\t\t" << blocks[i].currentHash << endl; | |
cout << endl; | |
} | |
} | |
void validateBlockchain() { | |
for (int i = blocks.size() - 1; i > 1; i--) { | |
if (strcmp(blocks[i].previousHash.c_str(), | |
blocks[i - 1].calculateHash().c_str()) != 0) { | |
cout << "Blockchain is invalid" << endl; | |
return; | |
} | |
} | |
cout << "Blockchain is valid" << endl; | |
} | |
}; | |
int main() { | |
Blockchain blockchain = Blockchain(); | |
cout << "\nAdding BLocks\n"; | |
int blocksToAdd = 5; | |
for (int i = 0; i < blocksToAdd; i++) { | |
blockchain.addBlock("Block Data " + to_string(i)); | |
} | |
blockchain.printBlocks(); | |
blockchain.validateBlockchain(); | |
cout << "\nModifying Blockchain\n"; | |
cout << "\nold hash: " + blockchain.blocks[2].calculateHash() << endl; | |
blockchain.blocks[2].data = "Modified Block Data 69"; | |
cout << "hash after change: " + blockchain.blocks[2].calculateHash() << endl | |
<< endl; | |
blockchain.printBlocks(); | |
cout << "\nValidating Blockchain\n"; | |
blockchain.validateBlockchain(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment