Added Simple blockchain class
This commit is contained in:
parent
33b7a33961
commit
b542236805
8 changed files with 117 additions and 25 deletions
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal 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>
|
28
Block.cpp
28
Block.cpp
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
size_t Block::max_height = 0;
|
size_t Block::max_height = 0;
|
||||||
|
|
||||||
std::string Block::calculateHash() {
|
void Block::calculateHash() {
|
||||||
memset(hash, 0, SHA256_DIGEST_LENGTH);
|
memset(hash, 0, SHA256_DIGEST_LENGTH);
|
||||||
SHA256_CTX sha;
|
SHA256_CTX sha;
|
||||||
SHA256_Init(&sha);
|
SHA256_Init(&sha);
|
||||||
|
@ -15,9 +15,9 @@ std::string Block::calculateHash() {
|
||||||
unsigned char md[SHA256_DIGEST_LENGTH];
|
unsigned char md[SHA256_DIGEST_LENGTH];
|
||||||
SHA256_Final(md, &sha);
|
SHA256_Final(md, &sha);
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
|
for (unsigned char i : md)
|
||||||
ss << std::hex << (int) md[i];
|
ss << std::hex << (int) i;
|
||||||
return ss.str();
|
strcpy(hash,ss.str().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
Block::Block(Transaction tr[], size_t count, const char *prev_hash) {
|
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)
|
if (strcmp(prev_hash, "") == 0)
|
||||||
memset(this->prev_hash, 0, SHA256_DIGEST_LENGTH);
|
memset(this->prev_hash, 0, SHA256_CBLOCK+1);
|
||||||
else
|
else
|
||||||
strcpy(this->prev_hash, prev_hash);
|
strcpy(this->prev_hash, prev_hash);
|
||||||
height = ++max_height;
|
height = ++max_height;
|
||||||
strcpy(hash, calculateHash().c_str());
|
calculateHash();
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction &Transaction::operator=(Transaction &tr) {
|
Transaction &Transaction::operator=(Transaction &tr) {
|
||||||
if(this != &tr){
|
if(this != &tr){
|
||||||
strcpy(sender,tr.getSender());
|
strcpy(sender,tr.getSender());
|
||||||
strcpy(reciver,tr.getReciever());
|
strcpy(reciver,tr.getReciever());
|
||||||
|
strcpy(hash,tr.getHash());
|
||||||
amount = tr.getAmount();
|
amount = tr.getAmount();
|
||||||
}
|
}
|
||||||
return *this;
|
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
31
Block.h
|
@ -11,34 +11,43 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
#include <openssl/ripemd.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
class Transaction{
|
class Transaction{
|
||||||
char sender[KEY_LENGTH];
|
char sender[RIPEMD160_DIGEST_LENGTH];
|
||||||
char reciver[KEY_LENGTH];
|
char reciver[RIPEMD160_DIGEST_LENGTH];
|
||||||
|
char hash[SHA256_CBLOCK+1];
|
||||||
int amount;
|
int amount;
|
||||||
public:
|
public:
|
||||||
Transaction(){
|
Transaction(){
|
||||||
amount = 0;
|
amount = 0;
|
||||||
memset(sender,0,KEY_LENGTH);
|
memset(sender,0,RIPEMD160_DIGEST_LENGTH);
|
||||||
memset(reciver,0,KEY_LENGTH);
|
memset(reciver,0,RIPEMD160_DIGEST_LENGTH);
|
||||||
|
memset(hash,0,SHA256_CBLOCK+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction(const char* sender,const char* to,int amount):amount(amount){
|
Transaction(const char* sender,const char* to,int amount):amount(amount){
|
||||||
memset(this->reciver,0,KEY_LENGTH);
|
memset(this->reciver,0,RIPEMD160_DIGEST_LENGTH);
|
||||||
memset(this->sender,0,KEY_LENGTH);
|
memset(this->sender,0,RIPEMD160_DIGEST_LENGTH);
|
||||||
strncpy(this->sender,sender,KEY_LENGTH);
|
strncpy(this->sender,sender,RIPEMD160_DIGEST_LENGTH);
|
||||||
strncpy(this->reciver,to,KEY_LENGTH);
|
strncpy(this->reciver,to,RIPEMD160_DIGEST_LENGTH);
|
||||||
|
calculateHash();
|
||||||
}
|
}
|
||||||
char* getSender() const { return (char*)sender; }
|
char* getSender() const { return (char*)sender; }
|
||||||
char* getReciever() const { return (char*)reciver; }
|
char* getReciever() const { return (char*)reciver; }
|
||||||
|
char* getHash() const { return (char*)hash; }
|
||||||
int getAmount() const { return amount; }
|
int getAmount() const { return amount; }
|
||||||
Transaction& operator=(Transaction& tr);
|
Transaction& operator=(Transaction& tr);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void calculateHash();
|
||||||
};
|
};
|
||||||
|
|
||||||
class Block {
|
class Block {
|
||||||
char hash[SHA256_DIGEST_LENGTH];
|
char hash[SHA256_CBLOCK+1];
|
||||||
char prev_hash[SHA256_DIGEST_LENGTH];
|
char prev_hash[SHA256_CBLOCK+1];
|
||||||
Transaction transactions[MAX_BLOCK_TRANSACTION];
|
Transaction transactions[MAX_BLOCK_TRANSACTION];
|
||||||
size_t height;
|
size_t height;
|
||||||
static size_t max_height;
|
static size_t max_height;
|
||||||
|
@ -49,7 +58,7 @@ public:
|
||||||
char* getPerviousHash() const { return (char*)prev_hash;}
|
char* getPerviousHash() const { return (char*)prev_hash;}
|
||||||
Block(Transaction tr[],size_t count, const char *prev_hash = "");
|
Block(Transaction tr[],size_t count, const char *prev_hash = "");
|
||||||
private:
|
private:
|
||||||
std::string calculateHash();
|
void calculateHash();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
36
Blockchain.cpp
Normal file
36
Blockchain.cpp
Normal 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
26
Blockchain.h
Normal 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
|
|
@ -3,7 +3,7 @@ project(blockchain)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
find_package(OpenSSL REQUIRED)
|
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)
|
add_executable(test test/main.cpp Block.cpp Block.h)
|
||||||
include_directories(${OPENSSL_INCLUDE_DIR})
|
include_directories(${OPENSSL_INCLUDE_DIR})
|
||||||
target_link_libraries(test ${OPENSSL_LIBRARIES})
|
target_link_libraries(test ${OPENSSL_LIBRARIES})
|
||||||
|
|
1
config.h
1
config.h
|
@ -7,6 +7,5 @@
|
||||||
#define BLOCKCHAIN_CONFIG_H
|
#define BLOCKCHAIN_CONFIG_H
|
||||||
|
|
||||||
#define MAX_BLOCK_TRANSACTION 60
|
#define MAX_BLOCK_TRANSACTION 60
|
||||||
#define KEY_LENGTH 33
|
|
||||||
|
|
||||||
#endif //BLOCKCHAIN_CONFIG_H
|
#endif //BLOCKCHAIN_CONFIG_H
|
||||||
|
|
12
main.cpp
12
main.cpp
|
@ -1,14 +1,16 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Block.h"
|
#include "Block.h"
|
||||||
|
#include "Blockchain.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello, World!" << std::endl;
|
std::cout << "Hello, World!" << std::endl;
|
||||||
Transaction tr("","Balazs",100);
|
Transaction tr("","Balazs",100);
|
||||||
Transaction tr2("","Balazs",100);
|
Transaction trs[] = {tr};
|
||||||
Transaction tr3("","Balazs",100);
|
Block b(trs,1);
|
||||||
Transaction trs[] = {tr,tr2,tr3};
|
std::cout << b.getHash() << std::endl;
|
||||||
Block b(trs,3);
|
|
||||||
Block b2(nullptr,0,b.getHash());
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue