diff --git a/Block.cpp b/Block.cpp index bc87640..c97bf23 100644 --- a/Block.cpp +++ b/Block.cpp @@ -5,8 +5,6 @@ #include "Block.h" -size_t Block::max_height = 0; - void Block::calculateHash() { memset(hash, 0, SHA256_DIGEST_LENGTH); 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); else strcpy(this->prev_hash, prev_hash); - height = ++max_height; 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) { if(this != &tr){ strcpy(sender,tr.getSender()); diff --git a/Block.h b/Block.h index 0c6ee41..8a18eac 100644 --- a/Block.h +++ b/Block.h @@ -49,14 +49,14 @@ class Block { char hash[SHA256_CBLOCK+1]; char prev_hash[SHA256_CBLOCK+1]; Transaction transactions[MAX_BLOCK_TRANSACTION]; - size_t height; - static size_t max_height; + size_t difficulty; + int nonce = 0; public: - size_t getHeight() { return height; } - static size_t getMaxHeight() { return max_height; } char* getHash() const { return (char*)hash;} char* getPerviousHash() const { return (char*)prev_hash;} + size_t getDifficulty() const; Block(Transaction tr[],size_t count, const char *prev_hash = ""); + void mine(size_t diff); private: void calculateHash(); }; diff --git a/Blockchain.cpp b/Blockchain.cpp index af52b03..bd736bc 100644 --- a/Blockchain.cpp +++ b/Blockchain.cpp @@ -34,3 +34,30 @@ bool Blockchain::addBlock(Block &b) { blockList.push_back(b); 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); +} diff --git a/Blockchain.h b/Blockchain.h index bf0f47a..b86e2d2 100644 --- a/Blockchain.h +++ b/Blockchain.h @@ -15,11 +15,17 @@ class Blockchain { static int blockReward; std::vector blockList; public: + Blockchain(); + Blockchain(const Blockchain &b); const std::vector &getBlockList() const; static int getDifficulty(); static int getBlockReward(); bool verifyBlocks() const; + size_t getHeight() const; bool addBlock(Block& b); + Block operator[](size_t id); + bool operator<(Blockchain& b); + bool operator>(Blockchain& b); }; diff --git a/main.cpp b/main.cpp index 62eb1b6..8d4fc85 100644 --- a/main.cpp +++ b/main.cpp @@ -4,9 +4,10 @@ int main() { std::cout << "Hello, World!" << std::endl; - Transaction tr("","Balazs",100); + Transaction tr("","Balazs1",100); Transaction trs[] = {tr}; Block b(trs,1); + b.mine(3); std::cout << b.getHash() << std::endl; Block b2(nullptr,0,b.getHash()); std::cout << b.getHash() << std::endl;