Skip to content

Instantly share code, notes, and snippets.

@viveknavadia
Created February 7, 2018 11:13
Show Gist options
  • Save viveknavadia/c748165079b506b471a1482bc73d98b5 to your computer and use it in GitHub Desktop.
Save viveknavadia/c748165079b506b471a1482bc73d98b5 to your computer and use it in GitHub Desktop.
Building Own Blockchain using C++
#include <iostream>
#include <string>
#include "SHA256.h"
using namespace std;
class block
{
public :
int index; //to store block number
string timestamp; //to store date & time
string data; // to staore data
string previoushash; //to store hash of previous block
string hash; //to store has of current block
//Constructor - will be called automatically when object of block created
block(int index, string timestamp, string data,string previoushash)
{
this->index = index;
this->timestamp = timestamp;
this->data = data;
this->previoushash = previoushash;
this->hash = calculateHash();
}
//function to calculate hash of given string using library pocosha2
string calculateHash()
{
string source = to_string(index) + timestamp + data + previoushash;
string hash;
picosha2::hash256_hex_string(source, hash);
return hash;
}
};
class blockchain
{
public:
vector <block> chain;
blockchain() //Constructor
{
chain.push_back(createGenesisBlock());
}
block createGenesisBlock() //Function to create Genesis block
{
block b(0,"01/01/2018","Genesis Block","0");
return b;
}
void addBlock(block b) //Method to add new block to blockchain
{
b.previoushash = getLatestBlock().hash;
b.hash = b.calculateHash();
chain.push_back(b);
}
block getLatestBlock() //Function to get last block from blockchain
{
return chain [this -> chain.size()-1];
}
string isChainValid() //validation of blockchain
{
for (int i=1; i< chain.size()- 1; i++)
{
block currentBlock = chain[i];
block previousBlock = chain[i-1];
if (currentBlock.hash != currentBlock.calculateHash())
{
return "Data on block is altered, so invalid blockchain";
}
if (currentBlock.previoushash != previousBlock.hash)
{
return "Data on block is altered, so invalid blockchain";
}
}
return "No data tempered. This is valid blockchain";
}
};
int main(int argc, const char * argv[]) {
blockchain coin; //object of blockchain
coin.addBlock(block(1,"02/01/2018","10 Coins","0")); //New block added
coin.addBlock(block(2,"03/01/2018","20 Coins","0")); //New block added
coin.addBlock(block(3,"04/01/2018","30 Coins","0")); //New block added
//Uncomment following lines to check invalid blockchain use case
//coin.chain[1].data = "1000 Coins";
//coin.chain[1].hash = coin.chain[1].calculateHash();
for (int i=0; i<=coin.chain.size()- 1; i++)
{
cout << "Index : " + to_string(coin.chain[i].index);
cout << "\n";
cout << "Timestamp : " + (coin.chain[i].timestamp);
cout << "\n";
cout << "Data : " + (coin.chain[i].data);
cout << "\n";
cout << "Previous Hash : " + (coin.chain[i].previoushash);
cout << "\n";
cout << "Hash : " + (coin.chain[i].hash);
cout << "\n\n";
}
cout << coin.isChainValid();
cout << "\n\n\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment