Compare commits
2 commits
b542236805
...
e0b3d03fdf
Author | SHA1 | Date | |
---|---|---|---|
e0b3d03fdf | |||
5de5d0bb3f |
9 changed files with 121 additions and 11 deletions
21
Block.cpp
21
Block.cpp
|
@ -5,8 +5,6 @@
|
||||||
|
|
||||||
#include "Block.h"
|
#include "Block.h"
|
||||||
|
|
||||||
size_t Block::max_height = 0;
|
|
||||||
|
|
||||||
void Block::calculateHash() {
|
void Block::calculateHash() {
|
||||||
memset(hash, 0, SHA256_DIGEST_LENGTH);
|
memset(hash, 0, SHA256_DIGEST_LENGTH);
|
||||||
SHA256_CTX sha;
|
SHA256_CTX sha;
|
||||||
|
@ -31,10 +29,27 @@ Block::Block(Transaction tr[], size_t count, const char *prev_hash) {
|
||||||
memset(this->prev_hash, 0, SHA256_CBLOCK+1);
|
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;
|
|
||||||
calculateHash();
|
calculateHash();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Block::mine(size_t diff) {
|
||||||
|
difficulty = diff;
|
||||||
|
std::string str;
|
||||||
|
for (int i = 0; i < diff; ++i) {
|
||||||
|
str += '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
while(strncmp(hash,str.c_str(),diff) != 0){
|
||||||
|
nonce++;
|
||||||
|
calculateHash();
|
||||||
|
std::cout << nonce << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Block::getDifficulty() const {
|
||||||
|
return difficulty;
|
||||||
|
}
|
||||||
|
|
||||||
Transaction &Transaction::operator=(Transaction &tr) {
|
Transaction &Transaction::operator=(Transaction &tr) {
|
||||||
if(this != &tr){
|
if(this != &tr){
|
||||||
strcpy(sender,tr.getSender());
|
strcpy(sender,tr.getSender());
|
||||||
|
|
8
Block.h
8
Block.h
|
@ -49,14 +49,14 @@ class Block {
|
||||||
char hash[SHA256_CBLOCK+1];
|
char hash[SHA256_CBLOCK+1];
|
||||||
char prev_hash[SHA256_CBLOCK+1];
|
char prev_hash[SHA256_CBLOCK+1];
|
||||||
Transaction transactions[MAX_BLOCK_TRANSACTION];
|
Transaction transactions[MAX_BLOCK_TRANSACTION];
|
||||||
size_t height;
|
size_t difficulty;
|
||||||
static size_t max_height;
|
int nonce = 0;
|
||||||
public:
|
public:
|
||||||
size_t getHeight() { return height; }
|
|
||||||
static size_t getMaxHeight() { return max_height; }
|
|
||||||
char* getHash() const { return (char*)hash;}
|
char* getHash() const { return (char*)hash;}
|
||||||
char* getPerviousHash() const { return (char*)prev_hash;}
|
char* getPerviousHash() const { return (char*)prev_hash;}
|
||||||
|
size_t getDifficulty() const;
|
||||||
Block(Transaction tr[],size_t count, const char *prev_hash = "");
|
Block(Transaction tr[],size_t count, const char *prev_hash = "");
|
||||||
|
void mine(size_t diff);
|
||||||
private:
|
private:
|
||||||
void calculateHash();
|
void calculateHash();
|
||||||
};
|
};
|
||||||
|
|
|
@ -34,3 +34,30 @@ bool Blockchain::addBlock(Block &b) {
|
||||||
blockList.push_back(b);
|
blockList.push_back(b);
|
||||||
return verifyBlocks();
|
return verifyBlocks();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Block Blockchain::operator[](size_t id) {
|
||||||
|
if(id >= blockList.size())
|
||||||
|
throw std::out_of_range("Out of range");
|
||||||
|
return blockList[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Blockchain::operator<(Blockchain &b) {
|
||||||
|
return blockList.size() < b.blockList.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Blockchain::getHeight() const {
|
||||||
|
return blockList.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Blockchain::operator>(Blockchain &b) {
|
||||||
|
return blockList.size() > b.getHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
Blockchain::Blockchain() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Blockchain::Blockchain(const Blockchain &b) {
|
||||||
|
for(Block bl : b.blockList)
|
||||||
|
blockList.push_back(bl);
|
||||||
|
}
|
||||||
|
|
|
@ -15,11 +15,17 @@ class Blockchain {
|
||||||
static int blockReward;
|
static int blockReward;
|
||||||
std::vector<Block> blockList;
|
std::vector<Block> blockList;
|
||||||
public:
|
public:
|
||||||
|
Blockchain();
|
||||||
|
Blockchain(const Blockchain &b);
|
||||||
const std::vector<Block> &getBlockList() const;
|
const std::vector<Block> &getBlockList() const;
|
||||||
static int getDifficulty();
|
static int getDifficulty();
|
||||||
static int getBlockReward();
|
static int getBlockReward();
|
||||||
bool verifyBlocks() const;
|
bool verifyBlocks() const;
|
||||||
|
size_t getHeight() const;
|
||||||
bool addBlock(Block& b);
|
bool addBlock(Block& b);
|
||||||
|
Block operator[](size_t id);
|
||||||
|
bool operator<(Blockchain& b);
|
||||||
|
bool operator>(Blockchain& b);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@ 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 Blockchain.cpp Blockchain.h)
|
add_executable(blockchain main.cpp Block.cpp Block.h config.h Blockchain.cpp Blockchain.h base58.cpp base58.h)
|
||||||
add_executable(test test/main.cpp Block.cpp Block.h)
|
add_executable(test test/main.cpp Block.cpp Block.h base58.h base58.cpp)
|
||||||
include_directories(${OPENSSL_INCLUDE_DIR})
|
include_directories(${OPENSSL_INCLUDE_DIR})
|
||||||
target_link_libraries(test ${OPENSSL_LIBRARIES})
|
target_link_libraries(test ${OPENSSL_LIBRARIES})
|
||||||
target_link_libraries(blockchain ${OPENSSL_LIBRARIES})
|
target_link_libraries(blockchain ${OPENSSL_LIBRARIES})
|
43
base58.cpp
Normal file
43
base58.cpp
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
//
|
||||||
|
// Készítette: Toldi Balázs Ádám
|
||||||
|
// Dátum: 2020. 03. 27.
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
#include "base58.h"
|
||||||
|
|
||||||
|
static const char* Base58arr = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
||||||
|
|
||||||
|
std::string base58::encode(const char *data, int len) {
|
||||||
|
int zero = 0;
|
||||||
|
int clen = 0;
|
||||||
|
for (int i = 0; i < len && data[i] == 0; ++i)
|
||||||
|
zero++;
|
||||||
|
int size = len*138/100+1;
|
||||||
|
std::vector<unsigned char> b58(size);
|
||||||
|
for (int j = 0; j < len; ++j) {
|
||||||
|
int carry = data[j];
|
||||||
|
int i = 0;
|
||||||
|
for (auto it = b58.rbegin(); (carry != 0 || i < clen) && (it != b58.rend()); it++, i++) {
|
||||||
|
carry += 256* (*it);
|
||||||
|
*it = carry % 58;
|
||||||
|
carry /= 58;
|
||||||
|
}
|
||||||
|
assert(carry == 0);
|
||||||
|
clen = i;
|
||||||
|
}
|
||||||
|
auto it = b58.begin() + (size - clen);
|
||||||
|
while (it != b58.end() && *it == 0)
|
||||||
|
it++;
|
||||||
|
// Translate the result into a string.
|
||||||
|
std::string str;
|
||||||
|
str.reserve(zero + (b58.end() - it));
|
||||||
|
str.assign(zero, '1');
|
||||||
|
while (it != b58.end())
|
||||||
|
str += Base58arr[*(it++)];
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string base58::encode(const std::string &data) {
|
||||||
|
return encode(data.c_str(),data.length());
|
||||||
|
}
|
17
base58.h
Normal file
17
base58.h
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
//
|
||||||
|
// Készítette: Toldi Balázs Ádám
|
||||||
|
// Dátum: 2020. 03. 27.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BLOCKCHAIN_BASE58_H
|
||||||
|
#define BLOCKCHAIN_BASE58_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <cstring>
|
||||||
|
#include <vector>
|
||||||
|
#include <cassert>
|
||||||
|
namespace base58 {
|
||||||
|
std::string encode(const char * data, int len);
|
||||||
|
std::string encode(const std::string& data);
|
||||||
|
}
|
||||||
|
#endif //BLOCKCHAIN_BASE58_H
|
3
main.cpp
3
main.cpp
|
@ -4,9 +4,10 @@
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello, World!" << std::endl;
|
std::cout << "Hello, World!" << std::endl;
|
||||||
Transaction tr("","Balazs",100);
|
Transaction tr("","Balazs1",100);
|
||||||
Transaction trs[] = {tr};
|
Transaction trs[] = {tr};
|
||||||
Block b(trs,1);
|
Block b(trs,1);
|
||||||
|
b.mine(3);
|
||||||
std::cout << b.getHash() << std::endl;
|
std::cout << b.getHash() << std::endl;
|
||||||
Block b2(nullptr,0,b.getHash());
|
Block b2(nullptr,0,b.getHash());
|
||||||
std::cout << b.getHash() << std::endl;
|
std::cout << b.getHash() << std::endl;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstring>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
#include "../base58.h"
|
||||||
|
|
||||||
class Entity{
|
class Entity{
|
||||||
float x,y;
|
float x,y;
|
||||||
|
@ -26,5 +26,6 @@ int main(){
|
||||||
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
|
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
|
||||||
ss << std::hex << (int)md[i];
|
ss << std::hex << (int)md[i];
|
||||||
std::cout << ss.str() << std::endl;
|
std::cout << ss.str() << std::endl;
|
||||||
|
std::cout << base58::encode("cat") << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue