-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.cpp
53 lines (40 loc) · 1.09 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
#include <stdio.h>
#include <string>
#include "Block.h"
#include "TransactionData.h"
// Constructor with parameters
Block::Block(int idx, TransactionData d, size_t prevHash)
{
index = idx;
data = d;
previousHash = prevHash;
blockHash = generateHash();
}
// Private Functions
int Block::getIndex()
{
return index;
}
size_t Block::generateHash()
{
// creating string of transaction data
std::string toHashS = std::to_string(data.amount) + data.receiverKey + data.senderKey + std::to_string(data.timeStamp);
// 2 hashes to combine
std::hash<std::string> tDataHash; // hashes transaction data string
std::hash<std::string> prevHash; // re-hases previous hash (for combination)
// combine hashes and get size_t for block hash
return tDataHash(toHashS) ^ (prevHash(std::to_string(previousHash)) << 1);
}
// Public Functions
size_t Block::getHash()
{
return blockHash;
}
size_t Block::getPreviousHash()
{
return previousHash;
}
bool Block::isHashValid()
{
return generateHash() == getHash();
}