blockchain-test/Block.cpp
2020-03-15 14:04:03 +01:00

45 lines
No EOL
1.2 KiB
C++

//
// Keszitette: Toldi Balázs Ádám
// Datum: 2020. 03. 14.
//
#include "Block.h"
size_t Block::max_height = 0;
std::string Block::calculateHash() {
memset(hash, 0, SHA256_DIGEST_LENGTH);
SHA256_CTX sha;
SHA256_Init(&sha);
SHA256_Update(&sha, this, sizeof(Block));
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();
}
Block::Block(Transaction tr[], size_t count, const char *prev_hash) {
if(count > MAX_BLOCK_TRANSACTION) throw "Too much!";
memset(transactions,0, sizeof(Transaction)*MAX_BLOCK_TRANSACTION);
for (int i = 0; i < count; ++i) {
transactions[i] = tr[i];
}
if (strcmp(prev_hash, "") == 0)
memset(this->prev_hash, 0, SHA256_DIGEST_LENGTH);
else
strcpy(this->prev_hash, prev_hash);
height = ++max_height;
strcpy(hash, calculateHash().c_str());
}
Transaction &Transaction::operator=(Transaction &tr) {
if(this != &tr){
strcpy(sender,tr.getSender());
strcpy(reciver,tr.getReciever());
amount = tr.getAmount();
}
return *this;
}