forked from tko22/simple-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.hpp
80 lines (68 loc) · 1.89 KB
/
Block.hpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//author: tko
#ifndef BLOCK_H
#define BLOCK_H
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <stdexcept>
#include "json.hh"
using json = nlohmann::json;
class Block {
public:
Block(int index, string prevHas, string hash, string nonce, vector<string> data);
string getPreviousHash(void);
string getHash(void);
int getIndex(void);
vector<string> getData(void);
void toString(void);
json toJSON(void);
private:
int index;
string previousHash;
string blockHash;
string nonce;
vector<string> data;
// string getMerkleRoot(const vector<string> &merkle);
};
// Constructor
Block::Block(int index, string prevHash, string hash, string nonce, vector<string> data ) {
printf("\nInitializing Block: %d ---- Hash: %s \n", index,hash.c_str());
this -> previousHash = prevHash;
this -> data = data;
this -> index = index;
this -> nonce = nonce;
this -> blockHash = hash;
}
int Block::getIndex(void) {
return this -> index;
}
string Block::getPreviousHash(void) {
return this -> previousHash;
}
string Block::getHash(void) {
return this -> blockHash;
}
vector<string> Block::getData(void){
return this -> data;
}
// Prints Block data
void Block::toString(void) {
string dataString;
for (int i=0; i < data.size(); i++)
dataString += data[i] + ", ";
printf("\n-------------------------------\n");
printf("Block %d\nHash: %s\nPrevious Hash: %s\nContents: %s",
index,this->blockHash.c_str(),this->previousHash.c_str(),dataString.c_str());
printf("\n-------------------------------\n");
}
json Block::toJSON(void) {
json j;
j["index"] = this->index;
j["hash"] = this->blockHash;
j["previousHash"] = this->previousHash;
j["nonce"] = this->nonce;
j["data"] = this->data;
return j;
}
#endif