-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlock.cpp
More file actions
53 lines (44 loc) · 1.15 KB
/
Block.cpp
File metadata and controls
53 lines (44 loc) · 1.15 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
//
// Created by salad on 9/23/18.
//
#include "Block.h"
#include "sha256.h"
Block::Block(uint32_t indexIn, const string &dataIn) :
index(indexIn), data(dataIn)
{
nonce = -1;
//set to current time
currentTime = time(nullptr);
}
string Block::GetHash()
{
return hash;
}
// lets make this rewarding for miners
void Block::MineBlock(uint32_t nDifficulty)
{
// create array of characters with length greater than nDifficulty
char cArray[nDifficulty + 1];
// fill array with 0's
for (uint32_t i = 0; i < nDifficulty; i++)
{
cArray[i] = '0';
}
// attach on a terminator at the end of the array
cArray[nDifficulty] = '\0';
string str(cArray);
// keep looping until a match is found when the hash is calculated
do
{
nonce++;
hash = CalculateHash();
} while (hash.substr(0, nDifficulty) != str);
// send message to output buffer that block has been mined
cout << "Block mined: " << hash << endl;
}
inline string Block::CalculateHash() const
{
stringstream sStream;
sStream << index << prevHash << currentTime << data << nonce;
return sha256(sStream.str());
}