-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBlock.cpp
200 lines (151 loc) · 4.23 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "Block.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QCryptographicHash>
#include "Miner.h"
unsigned int Block::getTarget(int index)
{
return index / 1024 + 20;
//155
//500
//2.49
//42
//429
}
Block::Block(int index)
:m_index(index),
m_preHash(),
m_transactions(),
m_solution(0)
{
}
Block::Block(int index, const QHash<QByteArray, Transaction> &transactions, const QByteArray &preHash)
:m_index(index),
m_transactions(transactions),
m_preHash(preHash),
m_solution(0)
{
}
QByteArray Block::toJson() const
{
QJsonObject jobj;
jobj["prehash"] = QString::fromLocal8Bit(m_preHash.toBase64());
jobj["index"] = m_index;
QJsonArray jarr;
foreach(Transaction t, m_transactions)
{
QJsonObject tobj = QJsonDocument::fromJson( t.getMessageJson()).object();
jarr.append(tobj);
}
jobj["transactions"] = jarr;
QByteArray solutionArray;
solutionArray.append((char*)&m_solution, sizeof(m_solution));
QJsonDocument jdoc(jobj);
return jdoc.toJson();
}
QByteArray Block::toMessageJson() const
{
QJsonObject jobj;
jobj["prehash"] = QString::fromLocal8Bit(m_preHash.toBase64());
jobj["index"] = m_index;
QJsonArray jarr;
foreach(Transaction t, m_transactions)
{
QJsonObject tobj = QJsonDocument::fromJson( t.getMessageJson()).object();
jarr.append(tobj);
}
jobj["transactions"] = jarr;
QJsonObject messageJsonObj;
messageJsonObj["message"] = "Block";
messageJsonObj["block"] = jobj;
messageJsonObj["solution"] = QString::fromLocal8Bit(m_solution.getData().toBase64());
QJsonDocument jdoc(messageJsonObj);
return jdoc.toJson();
}
void Block::addTransaction(const Transaction &t)
{
m_transactions.insert(t.getSignature(), t);
}
void Block::removeTransaction(const Transaction &t)
{
m_transactions.remove(t.getSignature());
}
int Block::getIndex() const
{
return m_index;
}
void Block::setSolution(BigInt solution)
{
m_solution = solution;
}
bool Block::isValid()
{
//verify solution
QByteArray blockJson = toJson();
QByteArray data;
data.push_back(m_solution.getData());
data.push_back(blockJson);
QCryptographicHash sha512(QCryptographicHash::Sha3_512);
sha512.addData(data);
QByteArray result = sha512.result();
int zeroCount = Miner::countLeadingZeros(result);
unsigned int target = Block::getTarget(m_index);
if (zeroCount < target)
{
return false;
}
//check if there is only one reward transaction;
int rewardTransactionCount = 0;
foreach(Transaction t, m_transactions)
{
if (t.getType() == Transaction::REWARD)
{
++ rewardTransactionCount;
}
}
if (rewardTransactionCount != 1)
{
return false;
}
}
const QByteArray &Block::getPreHash() const
{
return m_preHash;
}
void Block::refreshBlockHash()
{
QByteArray messageJson = toMessageJson();
QCryptographicHash sha512(QCryptographicHash::Sha3_512);
sha512.addData(messageJson);
m_cachedBlockHash = sha512.result();
}
const QByteArray &Block::getCacheBlockHash()
{
if (m_cachedBlockHash.isEmpty())
{
refreshBlockHash();
}
return m_cachedBlockHash;
}
const QHash<QByteArray, Transaction> &Block::getTransactions() const
{
return m_transactions;
}
bool Block::parseFromQJsonObject(const QJsonObject &obj)
{
QJsonObject jobj = obj["block"].toObject();
m_preHash = QByteArray::fromBase64(jobj["prehash"].toString().toLocal8Bit());
m_index = jobj["index"].toInt();
QJsonArray jarr = jobj["transactions"].toArray();
foreach(QJsonValue t, jarr)
{
QJsonObject tobj = t.toObject();
Transaction transaction;
transaction.parseFromJsonObject(tobj);
m_transactions.insert(transaction.getSignature(), transaction);
}
QByteArray solution = QByteArray::fromBase64(obj["solution"].toString().toLocal8Bit());
m_solution.setData(solution);
return true;
}