-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.cpp
More file actions
64 lines (51 loc) · 1.99 KB
/
Block.cpp
File metadata and controls
64 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "Block.h"
#include "SHA256.h"
#include <sstream>
#include <iomanip>
#include <iostream>
// Constructor initializes block data and calculates the initial hash
Block::Block(std::string previousHash, std::string sender, std::string receiver, double amount,std::string hsh,int non)
: previousHash(previousHash), sender(sender), receiver(receiver), amount(amount), nonce(non), hash(hsh),timestamp(time(nullptr)) {
}
Block::Block(std::string previousHash, std::string sender, std::string receiver, double amount)
: previousHash(previousHash), sender(sender), receiver(receiver), amount(amount), nonce(0), timestamp(std::time(nullptr)) {
hash = calculateHash();
}
// Calculate the hash based on block content
std::string Block::calculateHash() {
std::stringstream ss;
ss << previousHash << sender << receiver <<std::fixed << std::setprecision(2) << amount << nonce;
return SHA256::hash(ss.str());
}
// Get the current hash of the block
std::string Block::getHash() const {
return hash;
}
int Block::getNonce() const{
return nonce;
}
// Get the previous block's hash
std::string Block::getPreviousHash() const {
return previousHash;
}
// Return the transaction data as a formatted string
std::string Block::getTransactionData() const {
std::stringstream ss;
ss << sender <<" "<< receiver <<" "<< amount;
return ss.str();
}
// Mine the block by adjusting the nonce until the hash meets the difficulty
void Block::mineBlock(int difficulty) {
std::string target(difficulty, '0'); // Target string with 'difficulty' leading zeros
// Loop to find a nonce that generates a hash matching the difficulty level
while (hash.substr(hash.size() - difficulty, difficulty) != target) {
nonce++;
hash = calculateHash();
std::cout<<"Hash "<<hash<<std::endl;
}
std::cout << "Block mined: " << hash << std::endl;
}
// Recalculate the hash of the current block, for example if data changes
void Block::recalculateHash() {
hash = calculateHash();
}