-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblock.cpp
More file actions
40 lines (34 loc) · 1.03 KB
/
Copy pathblock.cpp
File metadata and controls
40 lines (34 loc) · 1.03 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
#include "block.h"
#include "sha256.h"
Block::Block(const int32_t (&data)[DATA_SIZE]){
nonce = 0;
timestamp = time(nullptr);
for(int32_t i=0; i<sizeof(data)/sizeof(int32_t); i++)
this->data[i] = data[i];
hash = calculateHash();
}
void Block::mineBlock(const uint32_t &difficulty){
char cstr[difficulty + 1];
for (uint32_t i = 0; i < difficulty; ++i)
cstr[i] = '0';
cstr[difficulty] = '\0';
std::string str(cstr);
//std::cout << "Mining block... " << std::flush;
do{
nonce++;
hash = calculateHash();
}while (hash.substr(0, difficulty) != str);
//std::cout << hash << std::endl;
}
bool Block::isValid(const uint32_t &difficulty) const{
for(int32_t i=0; i<difficulty; i++)
if (hash[i] != '0') return false;
return hash == calculateHash();
}
inline std::string Block::calculateHash() const{
std::stringstream ss;
ss << prev_hash << timestamp << nonce;
for(uint32_t i=0; i<DATA_SIZE; i++)
ss << data[i];
return sha256(ss.str());
}