Added Simple blockchain class

This commit is contained in:
Bazsalanszky 2020-03-16 00:10:23 +01:00
parent 33b7a33961
commit b542236805
Signed by: Bazsalanszky
GPG key ID: 3A1E008A6F682DA0
8 changed files with 117 additions and 25 deletions

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View file

@ -7,7 +7,7 @@
size_t Block::max_height = 0;
std::string Block::calculateHash() {
void Block::calculateHash() {
memset(hash, 0, SHA256_DIGEST_LENGTH);
SHA256_CTX sha;
SHA256_Init(&sha);
@ -15,9 +15,9 @@ std::string Block::calculateHash() {
unsigned char md[SHA256_DIGEST_LENGTH];
SHA256_Final(md, &sha);
std::stringstream ss;
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
ss << std::hex << (int) md[i];
return ss.str();
for (unsigned char i : md)
ss << std::hex << (int) i;
strcpy(hash,ss.str().c_str());
}
Block::Block(Transaction tr[], size_t count, const char *prev_hash) {
@ -28,18 +28,32 @@ Block::Block(Transaction tr[], size_t count, const char *prev_hash) {
}
if (strcmp(prev_hash, "") == 0)
memset(this->prev_hash, 0, SHA256_DIGEST_LENGTH);
memset(this->prev_hash, 0, SHA256_CBLOCK+1);
else
strcpy(this->prev_hash, prev_hash);
height = ++max_height;
strcpy(hash, calculateHash().c_str());
calculateHash();
}
Transaction &Transaction::operator=(Transaction &tr) {
if(this != &tr){
strcpy(sender,tr.getSender());
strcpy(reciver,tr.getReciever());
strcpy(hash,tr.getHash());
amount = tr.getAmount();
}
return *this;
}
}
void Transaction::calculateHash() {
memset(hash, 0, SHA256_DIGEST_LENGTH);
SHA256_CTX sha;
SHA256_Init(&sha);
SHA256_Update(&sha, this, sizeof(Transaction));
unsigned char md[SHA256_DIGEST_LENGTH];
SHA256_Final(md, &sha);
std::stringstream ss;
for (unsigned char i : md)
ss << std::hex << (int) i;
strcpy(hash,ss.str().c_str());
}

31
Block.h
View file

@ -11,34 +11,43 @@
#include <iostream>
#include <vector>
#include <openssl/sha.h>
#include <openssl/ripemd.h>
#include "config.h"
class Transaction{
char sender[KEY_LENGTH];
char reciver[KEY_LENGTH];
char sender[RIPEMD160_DIGEST_LENGTH];
char reciver[RIPEMD160_DIGEST_LENGTH];
char hash[SHA256_CBLOCK+1];
int amount;
public:
Transaction(){
amount = 0;
memset(sender,0,KEY_LENGTH);
memset(reciver,0,KEY_LENGTH);
memset(sender,0,RIPEMD160_DIGEST_LENGTH);
memset(reciver,0,RIPEMD160_DIGEST_LENGTH);
memset(hash,0,SHA256_CBLOCK+1);
}
Transaction(const char* sender,const char* to,int amount):amount(amount){
memset(this->reciver,0,KEY_LENGTH);
memset(this->sender,0,KEY_LENGTH);
strncpy(this->sender,sender,KEY_LENGTH);
strncpy(this->reciver,to,KEY_LENGTH);
memset(this->reciver,0,RIPEMD160_DIGEST_LENGTH);
memset(this->sender,0,RIPEMD160_DIGEST_LENGTH);
strncpy(this->sender,sender,RIPEMD160_DIGEST_LENGTH);
strncpy(this->reciver,to,RIPEMD160_DIGEST_LENGTH);
calculateHash();
}
char* getSender() const { return (char*)sender; }
char* getReciever() const { return (char*)reciver; }
char* getHash() const { return (char*)hash; }
int getAmount() const { return amount; }
Transaction& operator=(Transaction& tr);
private:
void calculateHash();
};
class Block {
char hash[SHA256_DIGEST_LENGTH];
char prev_hash[SHA256_DIGEST_LENGTH];
char hash[SHA256_CBLOCK+1];
char prev_hash[SHA256_CBLOCK+1];
Transaction transactions[MAX_BLOCK_TRANSACTION];
size_t height;
static size_t max_height;
@ -49,7 +58,7 @@ public:
char* getPerviousHash() const { return (char*)prev_hash;}
Block(Transaction tr[],size_t count, const char *prev_hash = "");
private:
std::string calculateHash();
void calculateHash();
};

36
Blockchain.cpp Normal file
View file

@ -0,0 +1,36 @@
//
// Keszitette: Toldi Balázs Ádám
// Datum: 2020. 03. 15.
//
#include "Blockchain.h"
int Blockchain::difficulty = 2;
int Blockchain::blockReward = 50;
int Blockchain::getBlockReward() {
return blockReward;
}
int Blockchain::getDifficulty() {
return difficulty;
}
const std::vector<Block> &Blockchain::getBlockList() const {
return blockList;
}
bool Blockchain::verifyBlocks() const{
std::string prev_hash = blockList.rbegin()->getPerviousHash();
for (auto it = blockList.rbegin()+1;it < blockList.rend();++it) {
if(it->getHash() != prev_hash)
return false;
}
return true;
}
bool Blockchain::addBlock(Block &b) {
blockList.push_back(b);
return verifyBlocks();
}

26
Blockchain.h Normal file
View file

@ -0,0 +1,26 @@
//
// Keszitette: Toldi Balázs Ádám
// Datum: 2020. 03. 15.
//
#ifndef BLOCKCHAIN_BLOCKCHAIN_H
#define BLOCKCHAIN_BLOCKCHAIN_H
#include <vector>
#include <string>
#include "Block.h"
class Blockchain {
static int difficulty;
static int blockReward;
std::vector<Block> blockList;
public:
const std::vector<Block> &getBlockList() const;
static int getDifficulty();
static int getBlockReward();
bool verifyBlocks() const;
bool addBlock(Block& b);
};
#endif //BLOCKCHAIN_BLOCKCHAIN_H

View file

@ -3,7 +3,7 @@ project(blockchain)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenSSL REQUIRED)
add_executable(blockchain main.cpp Block.cpp Block.h config.h)
add_executable(blockchain main.cpp Block.cpp Block.h config.h Blockchain.cpp Blockchain.h)
add_executable(test test/main.cpp Block.cpp Block.h)
include_directories(${OPENSSL_INCLUDE_DIR})
target_link_libraries(test ${OPENSSL_LIBRARIES})

View file

@ -7,6 +7,5 @@
#define BLOCKCHAIN_CONFIG_H
#define MAX_BLOCK_TRANSACTION 60
#define KEY_LENGTH 33
#endif //BLOCKCHAIN_CONFIG_H

View file

@ -1,14 +1,16 @@
#include <iostream>
#include "Block.h"
#include "Blockchain.h"
int main() {
std::cout << "Hello, World!" << std::endl;
Transaction tr("","Balazs",100);
Transaction tr2("","Balazs",100);
Transaction tr3("","Balazs",100);
Transaction trs[] = {tr,tr2,tr3};
Block b(trs,3);
Transaction trs[] = {tr};
Block b(trs,1);
std::cout << b.getHash() << std::endl;
Block b2(nullptr,0,b.getHash());
std::cout << b2.getHash() << std::endl;
std::cout << b.getHash() << std::endl;
std::cout << b2.getPerviousHash() << std::endl;
Blockchain bc;
return 0;
}