Blockchain Prototype
main.cpp
#include <iostream>
#include <cstdlib>
#include <fstream>
#include "Node.h"
#include <ctime>
#include <iomanip>
#include <sstream>
using namespace std;
int main(int argc, char* argv[]){
//instantiating nodes
Node A = Node("A", "101010");
Node B = Node("B", "101010");
Node C = Node("C", "101010");
Node D = Node("D", "101010");
Node E = Node("E", "101010");
Node F = Node("F", "101010");
//instantiating pointers to each node
Node* toA = &A;
Node* toB = &B;
Node* toC = &C;
Node* toD = &D;
Node* toE = &E;
Node* toF = &F;
//for Node A:
A.setAdjNodeOne(toB);
A.setAdjNodeTwo(toC);
A.setAdjNodeThree(toD);
A.setAdjNodeFour(toE);
A.setAdjNodeFive(toF);
//for Node B:
B.setAdjNodeOne(toA);
node.h
#ifndef NODE_H
#define NODE_H
#include <vector>
#include <string>
#include <iostream>
class Node
{
public:
//Constructors
Node(std::string iden, std::string eKey, Node& N1, Node& N2, Node& N3, Node& N4, Node& N5);
Node(std::string iden, std::string eKey);
//Accessors
std::string getEncryptionKey();
Node* getAdjNodeOne();
Node* getAdjNodeTwo();
Node* getAdjNodeThree();
Node* getAdjNodeFour();
Node* getAdjNodeFive();
std::string getID();
std::string getDataCacheOut();
std::string getDataCacheOutEn();
std::string getDataCacheIn();
std::string getDataCacheInDe();
//Mutators
void setEncryptionKey(std::string newKey);
void setAdjNodeOne(Node* One);
void setAdjNodeTwo(Node* Two);
void setAdjNodeThree(Node* Three);
void setAdjNodeFour(Node* Four);
void setAdjNodeFive(Node* Five);
void setID(std::string newID);
void setDataCacheOut(std::string bin);
node.cpp
#include "Node.h"
//Constructors
Node::Node(std::string iden, std::string eKey, Node& N1, Node& N2, Node& N3, Node& N4, Node& N5){
ID = iden;
encryptionKey = eKey;
adjNodeOne = &N1;
adjNodeTwo = &N2;
adjNodeThree = &N3;
adjNodeFour = &N4;
adjNodeFive = &N5;
}
Node::Node(std::string iden, std::string eKey){
ID = iden;
encryptionKey = eKey;
}
//Accessors
std::string Node::getEncryptionKey(){return encryptionKey;}
Node* Node::getAdjNodeOne(){return adjNodeOne;}
Node* Node::getAdjNodeTwo(){return adjNodeTwo;}
Node* Node::getAdjNodeThree(){return adjNodeThree;}
Node* Node::getAdjNodeFour(){return adjNodeFour;}
Node* Node::getAdjNodeFive(){return adjNodeFive;}
std::string Node::getID(){return ID;}
std::string Node::getDataCacheOut(){return dataCacheOut;}
std::string Node::getDataCacheOutEn(){return dataCacheOutEn;}
std::string Node::getDataCacheIn(){return dataCacheIn;}
std::string Node::getDataCacheInDe(){return dataCacheInDe;}
//Mutators
void Node::setEncryptionKey(std::string newKey){encryptionKey = newKey;}
void Node::setAdjNodeOne(Node* One){adjNodeOne = One;}
Block.h
#ifndef BLOCK_H
#define BLOCK_H
#include "BlockHeader.h"
class Block
{
public:
//Constructor
Block();
//Accessors
//Mutators
private:
BlockHeader header;
};
#endif
Block.cpp
#include "Block.h"
#include "BlockHeader.h"
//Constructor
Block::Block(){
BlockHeader header = BlockHeader();
}
//Accessors
//Mutators
BlockHeader.h
#ifndef BLOCKHEADER_H
#define BLOCKHEADER_H
class BlockHeader
{
public:
//Constructors
BlockHeader();
BlockHeader(char par[64], char non[8]);
BlockHeader(char par[64], char tim[8], char non[8]);
//Accessors
char* getParentHash();
char* getTimeStamp();
char* getNonce();
//Mutators
void setParentHash(char ph[8]);
void setTimeStamp(char ts[8]);
void setNonce(char no[8]);
private:
char parentHash[64]; //32 bytes is 64 digits in hexidecimal
char timeStamp[8]; //4 bytes is 8 digits in hexidecimal
char nonce[8]; //4 bytes, 8 hex digits
};
#endif
BlockHeader.cpp
#include "BlockHeader.h"
#include <sstream>
#include <ctime>
#include <iomanip>
#include <cstdlib>
#include <string.h>
#include <cstring>
//Constructors
BlockHeader::BlockHeader(){
time_t curr = time(0);
std::stringstream ss;
ss << std::hex << curr;
std::string res = ss.str();
strcpy(timeStamp, res.c_str());
}
BlockHeader::BlockHeader(char par[64], char non[8]){
strcpy(parentHash, par);
strcpy(nonce, non);
time_t curr = time(0);
std::stringstream ss;
ss << std::hex << curr;
std::string res = ss.str();
strcpy(timeStamp, res.c_str());
}
BlockHeader::BlockHeader(char par[64], char tim[8], char non[8]){
strcpy(parentHash, par);
strcpy(timeStamp, tim);
strcpy(nonce, non);
}
//Accessors
char* BlockHeader::getParentHash(){return parentHash;}
char* BlockHeader::getTimeStamp(){return timeStamp;}
Transaction.h
#ifndef TRANSACTION_H
#define TRANSACTION_H
class Transaction
{
public:
//Constructors
Transaction(char ID[64], char from[64], char to[64]);
//Accessors
char* getHashID();
char* getFromWalletAdd();
char* getToWalletAdd();
char* getTime();
private:
char hashID[64];
char fromWalletAdd[64];
char toWalletAdd[64];
char timeStamp[8];
};
#endif
Transaction.cpp
#include "Transaction.h"
#include <sstream>
#include <ctime>
#include <iomanip>
#include <cstdlib>
#include <string.h>
#include <cstring>
//Constructor
Transaction::Transaction(char ID[64], char from[64], char to[64]){
strcpy(hashID, ID);
strcpy(fromWalletAdd, from);
strcpy(toWalletAdd, to);
time_t curr = time(0);
std::stringstream ss;
ss << std::hex << curr;
std::string res = ss.str();
strcpy(timeStamp, res.c_str());
}
//Accessors
char* Transaction::getHashID(){return hashID;}
char* Transaction::getFromWalletAdd(){return fromWalletAdd;}
char* Transaction::getToWalletAdd(){return toWalletAdd;}
char* Transaction::getTime(){return timeStamp;}
TransactionList.h
#ifndef TRANSACTIONLIST_H
#define TRANSACTIONLIST_H
#include <vector>
#include <cstdlib>
#include "Transaction.h"
class TransactionList
{
public:
//Constructor
TransactionList();
//Accessors
Transaction getTransactionAt(int index);
//Functions
void addTransaction(char ID[64], char from[64], char to[64]);
private:
std::vector<Transaction> ledger;
};
#endif
TransactionList.cpp
#include "TransactionList.h"
#include "Transaction.h"
//Constructor
TransactionList::TransactionList(){
}
//Accessor
Transaction TransactionList::getTransactionAt(int index){
return ledger[index];
}
//Functions
void TransactionList::addTransaction(char ID[64], char from[64], char to[64]){
Transaction add = Transaction(ID, from, to);
ledger.push_back(add);
}
Makefile
SOURCES = Main.cpp Node.cpp BlockHeader.cpp Block.cpp Transaction.cpp TransactionList.cpp
OBJECTS = Main.o Node.o BlockHeader.o Block.o Transaction.o TransactionList.o
blockchain: Main.cpp Node.cpp BlockHeader.cpp Block.cpp Transaction.cpp TransactionList.cpp
g++ -o blockchain Main.cpp Node.cpp BlockHeader.cpp Block.cpp Transaction.cpp TransactionList.cpp
CXXFLAGS = -std=c++11 -D_GNU_SOURCE -Wall