forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientBase.cpp
622 lines (531 loc) · 16.9 KB
/
ClientBase.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
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/>.
*/
/** @file ClientBase.cpp
* @author Gav Wood <i@gavwood.com>
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
#include "ClientBase.h"
#include <algorithm>
#include "BlockChain.h"
#include "Executive.h"
#include "State.h"
#include "SystemContractApi.h"
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace dev { namespace eth { const u256 c_maxGasEstimate = 50000000; } }
bool ClientBase::isNonceOk(Transaction const&_ts) const
{
return bc().isNonceOk(_ts);
}
bool ClientBase::isBlockLimitOk(Transaction const&_ts) const
{
return bc().isBlockLimitOk(_ts);
}
pair<h256, Address> ClientBase::submitTransaction(TransactionSkeleton const& _t, Secret const& _secret)
{
prepareForTransaction();
TransactionSkeleton ts(_t);
ts.from = toAddress(_secret);
Transaction t(ts, _secret);
LOG(TRACE) << "ClientBase::submitTransaction " << t.sha3();
ImportResult ir = m_tq.import(t.rlp());
if ( ir == ImportResult::NonceCheckFail )
{
return make_pair(t.sha3(), Address(1) );
}
else if ( ImportResult::BlockLimitCheckFail == ir )
{
return make_pair(t.sha3(), Address(2) );
}
else if ( ImportResult::NoTxPermission == ir )
{
return make_pair(t.sha3(), Address(3));
}
else if ( ImportResult::NoDeployPermission == ir )
{
return make_pair(t.sha3(), Address(4) );
}
return make_pair(t.sha3(), toAddress(ts.from, ts.randomid));
}
// TODO: remove try/catch, allow exceptions
ExecutionResult ClientBase::call(Address const& _from, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff)
{
ExecutionResult ret;
try
{
Block temp = block(_blockNumber);
temp.setEvmEventLog(bc().chainParams().evmEventLog);
u256 nonce = max<u256>(temp.transactionsFrom(_from), m_tq.maxNonce(_from));
u256 gas = _gas == Invalid256 ? gasLimitRemaining() : _gas;
u256 gasPrice = _gasPrice == Invalid256 ? gasBidPrice() : _gasPrice;
Transaction t(_value, gasPrice, gas, _dest, _data, nonce);
t.forceSender(_from);
if (_ff == FudgeFactor::Lenient)
temp.mutableState().addBalance(_from, (u256)(t.gas() * t.gasPrice() + t.value()));
u256 check = bc().filterCheck(t, FilterCheckScene::CheckCall);
if ( (u256)SystemContractCode::Ok != check )
{
BOOST_THROW_EXCEPTION(NoCallPermission());
}
ret = temp.execute(bc().lastHashes(), t, Permanence::Reverted);
}
catch (...)
{
LOG(ERROR) << boost::current_exception_diagnostic_information() << endl;
throw;
// TODO: Some sort of notification of failure.
}
return ret;
}
ExecutionResult ClientBase::create(Address const& _from, u256 _value, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff)
{
ExecutionResult ret;
try
{
Block temp = block(_blockNumber);
u256 n = temp.transactionsFrom(_from);
Transaction t(_value, _gasPrice, _gas, _data, n);
t.forceSender(_from);
if (_ff == FudgeFactor::Lenient)
temp.mutableState().addBalance(_from, (u256)(t.gas() * t.gasPrice() + t.value()));
ret = temp.execute(bc().lastHashes(), t, Permanence::Reverted);
}
catch (...)
{
// TODO: Some sort of notification of failure.
}
return ret;
}
std::pair<u256, ExecutionResult> ClientBase::estimateGas(Address const& _from, u256 _value, Address _dest, bytes const& _data, u256 _maxGas, u256 _gasPrice, BlockNumber _blockNumber, GasEstimationCallback const& _callback)
{
try
{
u256 upperBound = _maxGas;
if (upperBound == Invalid256 || upperBound > c_maxGasEstimate)
upperBound = c_maxGasEstimate;
u256 lowerBound = (u256)Transaction::gasRequired(!_dest, &_data, EVMSchedule(), 0);
Block bk = block(_blockNumber);
u256 gasPrice = _gasPrice == Invalid256 ? gasBidPrice() : _gasPrice;
ExecutionResult er;
ExecutionResult lastGood;
bool good = false;
while (upperBound != lowerBound)
{
u256 mid = (lowerBound + upperBound) / 2;
u256 n = bk.transactionsFrom(_from);
Transaction t;
if (_dest)
t = Transaction(_value, gasPrice, mid, _dest, _data, n);
else
t = Transaction(_value, gasPrice, mid, _data, n);
t.forceSender(_from);
EnvInfo env(bk.info(), bc().lastHashes(), 0);
env.setGasLimit(mid.convert_to<int64_t>());
State tempState(bk.state());
tempState.addBalance(_from, (u256)(t.gas() * t.gasPrice() + t.value()));
er = tempState.execute(env, *bc().sealEngine(), t, Permanence::Reverted).first;
if (er.excepted == TransactionException::OutOfGas ||
er.excepted == TransactionException::OutOfGasBase ||
er.excepted == TransactionException::OutOfGasIntrinsic ||
er.codeDeposit == CodeDeposit::Failed ||
er.excepted == TransactionException::BadJumpDestination)
lowerBound = lowerBound == mid ? upperBound : mid;
else
{
lastGood = er;
upperBound = upperBound == mid ? lowerBound : mid;
good = true;
}
if (_callback)
_callback(GasEstimationProgress { lowerBound, upperBound });
}
if (_callback)
_callback(GasEstimationProgress { lowerBound, upperBound });
return make_pair(upperBound, good ? lastGood : er);
}
catch (...)
{
// TODO: Some sort of notification of failure.
return make_pair(u256(), ExecutionResult());
}
}
ImportResult ClientBase::injectBlock(bytes const& _block)
{
return bc().attemptImport(_block, preSeal().db()).first;
}
u256 ClientBase::balanceAt(Address _a, BlockNumber _block) const
{
return block(_block).balance(_a);
}
u256 ClientBase::countAt(Address _a, BlockNumber _block) const
{
return block(_block).transactionsFrom(_a);
}
u256 ClientBase::stateAt(Address _a, u256 _l, BlockNumber _block) const
{
return block(_block).storage(_a, _l);
}
h256 ClientBase::stateRootAt(Address _a, BlockNumber _block) const
{
return block(_block).storageRoot(_a);
}
bytes ClientBase::codeAt(Address _a, BlockNumber _block) const
{
return block(_block).code(_a);
}
h256 ClientBase::codeHashAt(Address _a, BlockNumber _block) const
{
return block(_block).codeHash(_a);
}
map<h256, pair<u256, u256>> ClientBase::storageAt(Address _a, BlockNumber _block) const
{
return block(_block).storage(_a);
}
LocalisedLogEntries ClientBase::logs(unsigned _watchId) const
{
LogFilter f;
try
{
Guard l(x_filtersWatches);
f = m_filters.at(m_watches.at(_watchId).id).filter;
}
catch (...)
{
return LocalisedLogEntries();
}
return logs(f);
}
LocalisedLogEntries ClientBase::logs(LogFilter const& _f) const
{
LocalisedLogEntries ret;
unsigned begin = min(bc().number() + 1, (unsigned)numberFromHash(_f.latest()));
unsigned end = min(bc().number(), min(begin, (unsigned)numberFromHash(_f.earliest())));
// Handle pending transactions differently as they're not on the block chain.
if (begin > bc().number())
{
Block temp = postSeal();
for (unsigned i = 0; i < temp.pending().size(); ++i)
{
// Might have a transaction that contains a matching log.
TransactionReceipt const& tr = temp.receipt(i);
LogEntries le = _f.matches(tr);
for (unsigned j = 0; j < le.size(); ++j)
ret.insert(ret.begin(), LocalisedLogEntry(le[j]));
}
begin = bc().number();
}
// Handle reverted blocks
// There are not so many, so let's iterate over them
h256s blocks;
h256 ancestor;
unsigned ancestorIndex;
tie(blocks, ancestor, ancestorIndex) = bc().treeRoute(_f.earliest(), _f.latest(), false);
for (size_t i = 0; i < ancestorIndex; i++)
prependLogsFromBlock(_f, blocks[i], BlockPolarity::Dead, ret);
// cause end is our earliest block, let's compare it with our ancestor
// if ancestor is smaller let's move our end to it
// example:
//
// 3b -> 2b -> 1b
// -> g
// 3a -> 2a -> 1a
//
// if earliest is at 2a and latest is a 3b, coverting them to numbers
// will give us pair (2, 3)
// and we want to get all logs from 1 (ancestor + 1) to 3
// so we have to move 2a to g + 1
end = min(end, (unsigned)numberFromHash(ancestor) + 1);
// Handle blocks from main chain
set<unsigned> matchingBlocks;
if (!_f.isRangeFilter())
for (auto const& i : _f.bloomPossibilities())
for (auto u : bc().withBlockBloom(i, end, begin))
matchingBlocks.insert(u);
else
// if it is a range filter, we want to get all logs from all blocks in given range
for (unsigned i = end; i <= begin; i++)
matchingBlocks.insert(i);
for (auto n : matchingBlocks)
prependLogsFromBlock(_f, bc().numberHash(n), BlockPolarity::Live, ret);
reverse(ret.begin(), ret.end());
return ret;
}
void ClientBase::prependLogsFromBlock(LogFilter const& _f, h256 const& _blockHash, BlockPolarity _polarity, LocalisedLogEntries& io_logs) const
{
auto receipts = bc().receipts(_blockHash).receipts;
for (size_t i = 0; i < receipts.size(); i++)
{
TransactionReceipt receipt = receipts[i];
auto th = transaction(_blockHash, i).sha3();
LogEntries le = _f.matches(receipt);
for (unsigned j = 0; j < le.size(); ++j)
io_logs.insert(io_logs.begin(), LocalisedLogEntry(le[j], _blockHash, (BlockNumber)bc().number(_blockHash), th, i, 0, _polarity));
}
}
unsigned ClientBase::installWatch(LogFilter const& _f, Reaping _r)
{
h256 h = _f.sha3();
{
Guard l(x_filtersWatches);
if (!m_filters.count(h))
{
LOG(TRACE) << "FFF" << _f << h;
m_filters.insert(make_pair(h, _f));
}
}
return installWatch(h, _r);
}
unsigned ClientBase::installWatch(h256 _h, Reaping _r)
{
unsigned ret;
{
Guard l(x_filtersWatches);
ret = m_watches.size() ? m_watches.rbegin()->first + 1 : 0;
m_watches[ret] = ClientWatch(_h, _r);
LOG(TRACE) << "+++" << ret << _h;
}
#if INITIAL_STATE_AS_CHANGES
auto ch = logs(ret);
if (ch.empty())
ch.push_back(InitialChange);
{
Guard l(x_filtersWatches);
swap(m_watches[ret].changes, ch);
}
#endif
return ret;
}
bool ClientBase::uninstallWatch(unsigned _i)
{
LOG(TRACE) << "XXX" << _i;
Guard l(x_filtersWatches);
auto it = m_watches.find(_i);
if (it == m_watches.end())
return false;
auto id = it->second.id;
m_watches.erase(it);
auto fit = m_filters.find(id);
if (fit != m_filters.end())
if (!--fit->second.refCount)
{
LOG(TRACE) << "*X*" << fit->first << ":" << fit->second.filter;
m_filters.erase(fit);
}
return true;
}
LocalisedLogEntries ClientBase::peekWatch(unsigned _watchId) const
{
Guard l(x_filtersWatches);
auto& w = m_watches.at(_watchId);
if (w.lastPoll != chrono::system_clock::time_point::max())
w.lastPoll = chrono::system_clock::now();
return w.changes;
}
LocalisedLogEntries ClientBase::checkWatch(unsigned _watchId)
{
Guard l(x_filtersWatches);
LocalisedLogEntries ret;
auto& w = m_watches.at(_watchId);
std::swap(ret, w.changes);
if (w.lastPoll != chrono::system_clock::time_point::max())
w.lastPoll = chrono::system_clock::now();
return ret;
}
BlockHeader ClientBase::blockInfo(h256 _hash) const
{
if (_hash == PendingBlockHash)
return preSeal().info();
return BlockHeader(bc().block(_hash));
}
BlockDetails ClientBase::blockDetails(h256 _hash) const
{
return bc().details(_hash);
}
Transaction ClientBase::transaction(h256 _transactionHash) const
{
return Transaction(bc().transaction(_transactionHash), CheckTransaction::Cheap);
}
LocalisedTransaction ClientBase::localisedTransaction(h256 const& _transactionHash) const
{
std::pair<h256, unsigned> tl = bc().transactionLocation(_transactionHash);
return localisedTransaction(tl.first, tl.second);
}
Transaction ClientBase::transaction(h256 _blockHash, unsigned _i) const
{
auto bl = bc().block(_blockHash);
RLP b(bl);
if (_i < b[1].itemCount())
return Transaction(b[1][_i].data(), CheckTransaction::Cheap);
else
return Transaction();
}
LocalisedTransaction ClientBase::localisedTransaction(h256 const& _blockHash, unsigned _i) const
{
Transaction t = Transaction(bc().transaction(_blockHash, _i), CheckTransaction::Cheap);
return LocalisedTransaction(t, _blockHash, _i, numberFromHash(_blockHash));
}
TransactionReceipt ClientBase::transactionReceipt(h256 const& _transactionHash) const
{
return bc().transactionReceipt(_transactionHash);
}
LocalisedTransactionReceipt ClientBase::localisedTransactionReceipt(h256 const& _transactionHash) const
{
std::pair<h256, unsigned> tl = bc().transactionLocation(_transactionHash);
Transaction t = Transaction(bc().transaction(tl.first, tl.second), CheckTransaction::Cheap);
TransactionReceipt tr = bc().transactionReceipt(tl.first, tl.second);
return LocalisedTransactionReceipt(
tr,
t.sha3(),
tl.first,
numberFromHash(tl.first),
tl.second);
}
pair<h256, unsigned> ClientBase::transactionLocation(h256 const& _transactionHash) const
{
return bc().transactionLocation(_transactionHash);
}
Transactions ClientBase::transactions(h256 _blockHash) const
{
auto bl = bc().block(_blockHash);
RLP b(bl);
Transactions res;
for (unsigned i = 0; i < b[1].itemCount(); i++)
res.emplace_back(b[1][i].data(), CheckTransaction::Cheap);
return res;
}
TransactionHashes ClientBase::transactionHashes(h256 _blockHash) const
{
return bc().transactionHashes(_blockHash);
}
BlockHeader ClientBase::uncle(h256 _blockHash, unsigned _i) const
{
auto bl = bc().block(_blockHash);
RLP b(bl);
if (_i < b[2].itemCount())
return BlockHeader(b[2][_i].data(), HeaderData);
else
return BlockHeader();
}
UncleHashes ClientBase::uncleHashes(h256 _blockHash) const
{
return bc().uncleHashes(_blockHash);
}
unsigned ClientBase::transactionCount(h256 _blockHash) const
{
auto bl = bc().block(_blockHash);
RLP b(bl);
return b[1].itemCount();
}
unsigned ClientBase::uncleCount(h256 _blockHash) const
{
auto bl = bc().block(_blockHash);
RLP b(bl);
return b[2].itemCount();
}
unsigned ClientBase::number() const
{
return bc().number();
}
Transactions ClientBase::pending() const
{
return postSeal().pending();
}
h256s ClientBase::pendingHashes() const
{
return h256s() + postSeal().pendingHashes();
}
BlockHeader ClientBase::pendingInfo() const
{
return postSeal().info();
}
BlockDetails ClientBase::pendingDetails() const
{
auto pm = postSeal().info();
auto li = Interface::blockDetails(LatestBlock);
return BlockDetails((unsigned)pm.number(), li.totalDifficulty + pm.difficulty(), pm.parentHash(), h256s{});
}
Addresses ClientBase::addresses(BlockNumber _block) const
{
Addresses ret;
for (auto const& i : block(_block).addresses())
ret.push_back(i.first);
return ret;
}
u256 ClientBase::gasLimitRemaining() const
{
return postSeal().gasLimitRemaining();
}
Address ClientBase::author() const
{
return preSeal().author();
}
h256 ClientBase::hashFromNumber(BlockNumber _number) const
{
if (_number == PendingBlock)
return h256();
if (_number == LatestBlock)
return bc().currentHash();
return bc().numberHash(_number);
}
BlockNumber ClientBase::numberFromHash(h256 _blockHash) const
{
if (_blockHash == PendingBlockHash)
return bc().number() + 1;
else if (_blockHash == LatestBlockHash)
return bc().number();
else if (_blockHash == EarliestBlockHash)
return 0;
return bc().number(_blockHash);
}
int ClientBase::compareBlockHashes(h256 _h1, h256 _h2) const
{
BlockNumber n1 = numberFromHash(_h1);
BlockNumber n2 = numberFromHash(_h2);
if (n1 > n2)
return 1;
else if (n1 == n2)
return 0;
return -1;
}
bool ClientBase::isKnown(h256 const& _hash) const
{
return _hash == PendingBlockHash ||
_hash == LatestBlockHash ||
_hash == EarliestBlockHash ||
bc().isKnown(_hash);
}
bool ClientBase::isKnown(BlockNumber _block) const
{
return _block == PendingBlock ||
_block == LatestBlock ||
bc().numberHash(_block) != h256();
}
bool ClientBase::isKnownTransaction(h256 const& _transactionHash) const
{
return bc().isKnownTransaction(_transactionHash);
}
bool ClientBase::isKnownTransaction(h256 const& _blockHash, unsigned _i) const
{
return isKnown(_blockHash) && block(_blockHash).pending().size() > _i;
}
Block ClientBase::block(BlockNumber _h) const
{
if (_h == PendingBlock)
return postSeal();
else if (_h == LatestBlock)
return preSeal();
return block(bc().numberHash(_h));
}