Skip to content

Commit

Permalink
Create block.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 6, 2024
1 parent 25ab6a1 commit 523b438
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions blockchain_integration/pi_network/dapi/blockchain/core/block.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "block.h"
#include <openssl/sha.h>
#include <iostream>

Block::Block(uint32_t index, const std::string& previous_hash, const std::vector<std::string>& transactions)
: index_(index), previous_hash_(previous_hash), transactions_(transactions), nonce_(0), hash_("") {}

Block::~Block() {}

std::string Block::get_hash() const {
return hash_;
}

uint32_t Block::get_index() const {
return index_;
}

std::string Block::get_previous_hash() const {
return previous_hash_;
}

const std::vector<std::string>& Block::get_transactions() const {
return transactions_;
}

void Block::set_nonce(uint32_t nonce) {
nonce_ = nonce;
}

uint32_t Block::get_nonce() const {
return nonce_;
}

void Block::mine_block(uint32_t difficulty) {
char buffer[SHA256_DIGEST_LENGTH * 2 + 1];
uint32_t nonce = 0;
while (true) {
std::string header = std::to_string(index_) + previous_hash_ + std::to_string(nonce) + std::to_string(difficulty);
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256_ctx;
SHA256_Init(&sha256_ctx);
SHA256_Update(&sha256_ctx, header.c_str(), header.size());
SHA256_Final(hash, &sha256_ctx);
sprintf(buffer, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7],
hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15]);
std::string hash_str(buffer);
if (hash_str.substr(0, difficulty) == std::string(difficulty, '0')) {
hash_ = hash_str;
nonce_ = nonce;
break;
}
nonce++;
}
}

0 comments on commit 523b438

Please sign in to comment.