forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PBFTClient.cpp
300 lines (244 loc) · 8.35 KB
/
PBFTClient.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libethereum/EthereumHost.h>
#include <libethereum/NodeConnParamsManagerApi.h>
#include <libp2p/Host.h>
#include "PBFTClient.h"
#include "PBFT.h"
#include "PBFTHost.h"
#include <libdevcore/easylog.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
using namespace p2p;
PBFTClient& dev::eth::asPBFTClient(Interface& _c)
{
if (dynamic_cast<PBFT*>(_c.sealEngine()))
return dynamic_cast<PBFTClient&>(_c);
throw NotPBFTSealEngine();
}
PBFTClient* dev::eth::asPBFTClient(Interface* _c)
{
if (dynamic_cast<PBFT*>(_c->sealEngine()))
return &dynamic_cast<PBFTClient&>(*_c);
throw NotPBFTSealEngine();
}
PBFTClient::PBFTClient(
ChainParams const& _params,
int _networkID,
p2p::Host* _host,
std::shared_ptr<GasPricer> _gpForAdoption,
std::string const& _dbPath,
WithExisting _forceAction,
TransactionQueue::Limits const& _limits
):
Client(_params, _networkID, _host, _gpForAdoption, _dbPath, _forceAction, _limits)
{
// will throw if we're not an PBFT seal engine.
asPBFTClient(*this);
init(_params, _host);
}
PBFTClient::~PBFTClient() {
pbft()->cancelGeneration();
stopWorking();
}
void PBFTClient::init(ChainParams const& _params, p2p::Host *_host) {
m_params = _params;
m_working.setEvmCoverLog(m_params.evmCoverLog);
m_working.setEvmEventLog(m_params.evmEventLog);
// register PBFTHost
auto pbft_host = _host->registerCapability(make_shared<PBFTHost>([this](unsigned _id, std::shared_ptr<Capability> _peer, RLP const & _r) {
pbft()->onPBFTMsg(_id, _peer, _r);
}));
pbft()->initEnv(pbft_host, &m_bc, &m_stateDB, &m_bq, _host->keyPair(), static_cast<unsigned>(sealEngine()->getIntervalBlockTime()) * 3);
pbft()->reportBlock(bc().info(), bc().details().totalDifficulty);
pbft()->onSealGenerated([ this ](bytes const & _block, bool _isOurs) {
if (!submitSealed(_block, _isOurs))
LOG(ERROR) << "Submitting block failed...";
});
pbft()->onViewChange([this]() {
DEV_WRITE_GUARDED(x_working)
{
if (m_working.isSealed()) {
m_working.resetCurrent();
}
}
});
LOG(INFO) << "Init PBFTClient success";
}
PBFT* PBFTClient::pbft() const
{
return dynamic_cast<PBFT*>(Client::sealEngine());
}
void PBFTClient::startSealing() {
setName("Client");
pbft()->reportBlock(bc().info(), bc().details().totalDifficulty);
pbft()->startGeneration();
Client::startSealing();
}
void PBFTClient::stopSealing() {
Client::stopSealing();
pbft()->cancelGeneration();
}
void PBFTClient::syncBlockQueue() {
Client::syncBlockQueue();
pbft()->reportBlock(bc().info(), bc().details().totalDifficulty);
DEV_WRITE_GUARDED(x_working)
{
if (m_working.isSealed() && m_working.info().number() <= bc().info().number()) {
m_working.resetCurrent();
}
}
}
void PBFTClient::onTransactionQueueReady() {
m_syncTransactionQueue = true;
m_signalled.notify_all();
// 通知EthereumHost去广播交易
if (auto h = m_host.lock()) {
h->noteNewTransactions();
}
}
void PBFTClient::syncTransactionQueue()
{
Timer timer;
h256Hash changeds;
TransactionReceipts newPendingReceipts;
DEV_WRITE_GUARDED(x_working)
{
if (m_working.isSealed())
{
LOG(TRACE) << "Skipping txq sync for a sealed block.";
return;
}
if ( m_working.pending().size() >= m_maxBlockTranscations )
{
LOG(TRACE) << "Skipping txq sync for Full block .";
return;
}
tie(newPendingReceipts, m_syncTransactionQueue) = m_working.sync(bc(), m_tq, *m_gp);
}
if (newPendingReceipts.empty())
{
auto s = m_tq.status();
LOG(TRACE) << "No transactions to process. " << m_working.pending().size() << " pending, " << s.current << " queued, " << s.future << " future, " << s.unverified << " unverified";
return;
}
DEV_READ_GUARDED(x_working)
DEV_WRITE_GUARDED(x_postSeal)
m_postSeal = m_working;
DEV_READ_GUARDED(x_postSeal)
for (size_t i = 0; i < newPendingReceipts.size(); i++)
appendFromNewPending(newPendingReceipts[i], changeds, m_postSeal.pending()[i].sha3());
// Tell farm about new transaction (i.e. restart mining).
onPostStateChanged();
// Tell watches about the new transactions.
noteChanged(changeds);
LOG(TRACE) << "Processed " << newPendingReceipts.size() << " transactions in" << (timer.elapsed() * 1000) << "(" << (bool)m_syncTransactionQueue << ")";
cblockstat << m_working.info().number() << ",trans=" << newPendingReceipts.size() << " 交易处理速度=" << ((1000 * newPendingReceipts.size()) / ((timer.elapsed() * 1000000) / 1000)) << "/S";
}
void PBFTClient::doWork(bool _doWait)
{
bool t = true;
if (m_syncBlockQueue.compare_exchange_strong(t, false))
syncBlockQueue();
if (m_needStateReset)
{
resetState();
m_needStateReset = false;
}
tick();
rejigSealing();
callQueuedFunctions();
if (!m_syncBlockQueue && _doWait)
{
std::unique_lock<std::mutex> l(x_signalled);
m_signalled.wait_for(l, chrono::milliseconds(10));
}
}
void PBFTClient::rejigSealing() {
bool would_seal = m_wouldSeal && (pbft()->accountType() == EN_ACCOUNT_TYPE_MINER);
bool is_major_syncing = isMajorSyncing();
if (would_seal && !is_major_syncing)
{
if (pbft()->shouldSeal(this)) // 自己是不是leader?
{
DEV_READ_GUARDED(x_working)
{
if (m_working.isSealed()) {
LOG(TRACE) << "Tried to seal sealed block...";
return;
}
}
// 被选为leader之后才开始打包
bool t = true;
if (!isSyncing() && !m_remoteWorking && m_syncTransactionQueue.compare_exchange_strong(t, false)) {
syncTransactionQueue();
}
DEV_READ_GUARDED(x_working)
{
// 判断是否已够
size_t tx_num = m_working.pending().size();
//auto last_consensus_time = pbft()->lastConsensusTime();
auto last_exec_finish_time = pbft()->lastExecFinishTime();
auto now_time = utcTime();
//auto proc_time = last_consensus_time - static_cast<uint64_t>(bc().info().timestamp());
auto max_interval = static_cast<uint64_t>(sealEngine()->getIntervalBlockTime());
/*if (proc_time > max_interval) {
max_interval = 0;
} else {
max_interval -= proc_time;
}*/
if (tx_num < m_maxBlockTranscations && now_time - last_exec_finish_time < max_interval) {
//LOG(TRACE) << "Wait for next interval, tx:" << tx_num;
return;
}
}
DEV_WRITE_GUARDED(x_working)
{
// 出块
DEV_BLOCK_STAT_LOG("", m_working.info().number(), utcTime(), "commitToSeal");
//m_working.resetCurrentTime();
m_working.setIndex(pbft()->nodeIdx());
m_working.setNodeList(pbft()->getMinerNodeList());
m_working.commitToSeal(bc(), m_extraData);
m_sealingInfo = m_working.info();
RLPStream ts;
m_sealingInfo.streamRLP(ts, WithoutSeal);
m_working.setEvmCoverLog(m_params.evmCoverLog);
m_working.setEvmEventLog(m_params.evmEventLog);
//增加cache
bc().addBlockCache(m_working, m_working.info().difficulty());
if (!m_working.sealBlock(m_sealingInfo, &ts.out())) {
LOG(INFO) << "Error: sealBlock failed";
return;
}
}
DEV_READ_GUARDED(x_working)
{
DEV_WRITE_GUARDED(x_postSeal)
m_postSeal = m_working;
if (would_seal)
{
LOG(INFO) << "+++++++++++++++++++++++++++ Generating seal on " << m_sealingInfo.hash(WithoutSeal).abridged() << "#" << m_sealingInfo.number() << "tx:" << m_working.pending().size();
pbft()->generateSeal(m_sealingInfo, m_working.blockData());
}
}
}
}
}
bool PBFTClient::submitSealed(bytes const & _block, bool _isOurs) {
auto ret = m_bq.import(&_block, _isOurs);
LOG(TRACE) << "PBFTClient::submitSealed m_bq.import return " << (unsigned)ret;
return ret == ImportResult::Success;
}