-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.hpp
134 lines (112 loc) · 3.11 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//author: tko
#ifndef BLOCK_H
#define BLOCK_H
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <stdexcept>
#include "json.hh"
#include "Transaction.hpp"
using json = nlohmann::json;
class Block {
public:
Block(int index, string prevHas, string hash, string nonce, vector<string> data,
vector<Transaction> t);
string getPreviousHash();
string getHash();
string getNonce();
int getIndex() const;
vector<string> getData();
bool isValidBlock();
void addTransaction(Transaction);
string miner();
void toString();
json toJSON();
vector<Transaction> transactions;
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,
vector<Transaction> t) {
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;
this -> transactions = t;
}
int Block::getIndex() const {
return this -> index;
}
string Block::getPreviousHash() {
return this -> previousHash;
}
string Block::getHash() {
return this -> blockHash;
}
string Block::getNonce() {
return this->nonce;
}
vector<string> Block::getData(){
return this -> data;
}
// Prints Block data
void Block::toString() {
string tr = "[\n";
for (auto& q: this->transactions) {
tr += q.asString() + "\n";
}
tr += "];\n";
string dataString;
for (auto & i : data)
dataString += i + ", ";
printf("\n-------------------------------\n");
printf("Block %d\nHash: %s\nPrevious Hash: %s\nContents: %s\nTransactions: %s",
index,this->blockHash.c_str(),this->previousHash.c_str(),dataString.c_str(),tr.c_str());
printf("\n-------------------------------\n");
}
json Block::toJSON() {
json j;
j["index"] = this->index;
j["hash"] = this->blockHash;
j["previousHash"] = this->previousHash;
j["nonce"] = this->nonce;
j["data"] = this->data;
string tr;
for (auto& q: this->transactions) {
string sen = q.getSender();
string rec = q.getReceiver();
string sig = q.getSign();
string amount = to_string(q.getAmount());
string isrew = to_string(q.isMinerReward());
tr += sen + ";" + rec + ";" + sig + ";" + amount + ";" + isrew + "|";
}
j["transactions"] = tr;
j["miner"] = miner();
return j;
}
bool Block::isValidBlock() {
for (auto& tran: this->transactions) {
if (!tran.isValidTransaction()) {
return false;
}
}
return true;
}
void Block::addTransaction(Transaction t) {
if (t.isValidTransaction()) {
transactions.push_back(t);
}
}
string Block::miner() {
return this->transactions[this->transactions.size() - 1].getReceiver();
}
#endif