-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.cpp
61 lines (51 loc) · 1.24 KB
/
Block.cpp
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
61
#include "include/Block.h"
Block::Block(const uint8_t currentIndex, const std::vector<Transaction> &transactionVector, const uint64_t nonce, const std::string &previousHash)
: currentIndex{currentIndex}, transactionVector{transactionVector}, nonce{nonce}, previousHash{previousHash}
{
}
std::string generateTxVectorHash(const std::vector<Transaction> &transactionVector)
{
std::string hash;
for (uint i = 0; i < transactionVector.size(); i++)
{
hash += transactionVector[i].getTxHash();
}
return picosha2::hash256_hex_string(hash);
}
uint8_t Block::getCurrentIndex()
{
return currentIndex;
}
std::vector<Transaction> Block::getTransactionVector()
{
return transactionVector;
}
uint64_t Block::getNonce()
{
return nonce;
}
std::string Block::getHash()
{
std::string str = std::to_string(getCurrentIndex()) + generateTxVectorHash(transactionVector) + std::to_string(getNonce());
return picosha2::hash256_hex_string(str);
}
std::string Block::getPreviousHash()
{
return previousHash;
}
void Block::setNonce(uint64_t nonce)
{
this->nonce = nonce;
}
bool Block::validateNonce(uint8_t difficulty)
{
std::string selfHash = this->getHash();
for (uint64_t i = 0; i < difficulty; i++)
{
if (selfHash[i] != '0')
{
return false;
}
}
return true;
}