forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEthereumHost.cpp
737 lines (637 loc) · 21.2 KB
/
EthereumHost.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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
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 EthereumHost.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "EthereumHost.h"
#include <chrono>
#include <thread>
#include <libdevcore/Common.h>
#include <libp2p/Host.h>
#include <libp2p/Session.h>
#include <libethcore/Exceptions.h>
#include "BlockChain.h"
#include "TransactionQueue.h"
#include "BlockQueue.h"
#include "EthereumPeer.h"
#include "BlockChainSync.h"
#include "NodeConnParamsManagerApi.h"
#include <libdevcore/easylog.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
using namespace p2p;
unsigned const EthereumHost::c_oldProtocolVersion = 62;
static unsigned const c_maxSendTransactions = 256;
char const* const EthereumHost::s_stateNames[static_cast<int>(SyncState::Size)] = {"NotSynced", "Idle", "Waiting", "Blocks", "State", "NewBlocks" };
namespace
{
class EthereumPeerObserver: public EthereumPeerObserverFace
{
public:
EthereumPeerObserver(BlockChainSync& _sync, RecursiveMutex& _syncMutex, TransactionQueue& _tq): m_sync(_sync), m_syncMutex(_syncMutex), m_tq(_tq) {}
void onPeerStatus(std::shared_ptr<EthereumPeer> _peer) override
{
RecursiveGuard l(m_syncMutex);
try
{
m_sync.onPeerStatus(_peer);
}
catch (FailedInvariant const&)
{
// "fix" for https://github.com/ethereum/webthree-umbrella/issues/300
LOG(WARNING) << "Failed invariant during sync, restarting sync";
m_sync.restartSync();
}
}
void onPeerTransactions(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) override
{
unsigned itemCount = _r.itemCount();
LOG(TRACE) << "Transactions (" << dec << itemCount << "entries)";
m_tq.enqueue(_r, _peer->id());
}
void onPeerAborting() override
{
RecursiveGuard l(m_syncMutex);
try
{
m_sync.onPeerAborting();
}
catch (Exception&)
{
LOG(WARNING) << "Exception on peer destruciton: " << boost::current_exception_diagnostic_information();
}
}
void onPeerBlockHeaders(std::shared_ptr<EthereumPeer> _peer, RLP const& _headers) override
{
RecursiveGuard l(m_syncMutex);
try
{
m_sync.onPeerBlockHeaders(_peer, _headers);
}
catch (FailedInvariant const&)
{
// "fix" for https://github.com/ethereum/webthree-umbrella/issues/300
LOG(WARNING) << "Failed invariant during sync, restarting sync";
m_sync.restartSync();
}
}
void onPeerBlockBodies(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) override
{
RecursiveGuard l(m_syncMutex);
try
{
m_sync.onPeerBlockBodies(_peer, _r);
}
catch (FailedInvariant const&)
{
// "fix" for https://github.com/ethereum/webthree-umbrella/issues/300
LOG(WARNING) << "Failed invariant during sync, restarting sync";
m_sync.restartSync();
}
}
void onPeerNewHashes(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) override
{
RecursiveGuard l(m_syncMutex);
try
{
m_sync.onPeerNewHashes(_peer, _r);
}
catch (FailedInvariant const&)
{
// "fix" for https://github.com/ethereum/webthree-umbrella/issues/300
LOG(WARNING) << "Failed invariant during sync, restarting sync";
m_sync.restartSync();
}
}
void onPeerNewBlock(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) override
{
RecursiveGuard l(m_syncMutex);
try
{
m_sync.onPeerNewBlock(_peer, _r);
}
catch (FailedInvariant const&)
{
// "fix" for https://github.com/ethereum/webthree-umbrella/issues/300
LOG(WARNING) << "Failed invariant during sync, restarting sync";
m_sync.restartSync();
}
}
void onPeerNodeData(std::shared_ptr<EthereumPeer> /* _peer */, RLP const& _r) override
{
unsigned itemCount = _r.itemCount();
LOG(TRACE) << "Node Data (" << dec << itemCount << "entries)";
}
void onPeerReceipts(std::shared_ptr<EthereumPeer> /* _peer */, RLP const& _r) override
{
unsigned itemCount = _r.itemCount();
LOG(TRACE) << "Receipts (" << dec << itemCount << "entries)";
}
private:
BlockChainSync& m_sync;
RecursiveMutex& m_syncMutex;
TransactionQueue& m_tq;
};
class EthereumHostData: public EthereumHostDataFace
{
public:
EthereumHostData(BlockChain const& _chain, OverlayDB const& _db): m_chain(_chain), m_db(_db) {}
pair<bytes, unsigned> blockHeaders(RLP const& _blockId, unsigned _maxHeaders, u256 _skip, bool _reverse) const override
{
auto numHeadersToSend = _maxHeaders;
auto step = static_cast<unsigned>(_skip) + 1;
assert(step > 0 && "step must not be 0");
h256 blockHash;
if (_blockId.size() == 32) // block id is a hash
{
blockHash = _blockId.toHash<h256>();
//blockNumber = host()->chain().number(blockHash);
LOG(TRACE) << "GetBlockHeaders (block (hash): " << blockHash
<< ", maxHeaders: " << _maxHeaders
<< ", skip: " << _skip << ", reverse: " << _reverse << ")";
if (!_reverse)
{
auto n = m_chain.number(blockHash);
if (numHeadersToSend == 0)
blockHash = {};
else if (n != 0 || blockHash == m_chain.genesisHash())
{
auto top = n + uint64_t(step) * numHeadersToSend - 1;
auto lastBlock = m_chain.number();
if (top > lastBlock)
{
numHeadersToSend = (lastBlock - n) / step + 1;
top = n + step * (numHeadersToSend - 1);
}
assert(top <= lastBlock && "invalid top block calculated");
blockHash = m_chain.numberHash(static_cast<unsigned>(top)); // override start block hash with the hash of the top block we have
}
else
blockHash = {};
}
else if (!m_chain.isKnown(blockHash))
blockHash = {};
}
else // block id is a number
{
auto n = _blockId.toInt<bigint>();
LOG(TRACE) << "GetBlockHeaders (" << n
<< "max: " << _maxHeaders
<< "skip: " << _skip << (_reverse ? "reverse" : "") << ")";
if (!_reverse)
{
auto lastBlock = m_chain.number();
if (n > lastBlock || numHeadersToSend == 0)
blockHash = {};
else
{
bigint top = n + uint64_t(step) * (numHeadersToSend - 1);
if (top > lastBlock)
{
numHeadersToSend = (lastBlock - static_cast<unsigned>(n)) / step + 1;
top = n + step * (numHeadersToSend - 1);
}
assert(top <= lastBlock && "invalid top block calculated");
blockHash = m_chain.numberHash(static_cast<unsigned>(top)); // override start block hash with the hash of the top block we have
}
}
else if (n <= std::numeric_limits<unsigned>::max())
blockHash = m_chain.numberHash(static_cast<unsigned>(n));
else
blockHash = {};
}
auto nextHash = [this](h256 _h, unsigned _step)
{
static const unsigned c_blockNumberUsageLimit = 1000;
const auto lastBlock = m_chain.number();
const auto limitBlock = lastBlock > c_blockNumberUsageLimit ? lastBlock - c_blockNumberUsageLimit : 0; // find the number of the block below which we don't expect BC changes.
while (_step) // parent hash traversal
{
auto details = m_chain.details(_h);
if (details.number < limitBlock)
break; // stop using parent hash traversal, fallback to using block numbers
_h = details.parent;
--_step;
}
if (_step) // still need lower block
{
auto n = m_chain.number(_h);
if (n >= _step)
_h = m_chain.numberHash(n - _step);
else
_h = {};
}
return _h;
};
bytes rlp;
unsigned itemCount = 0;
vector<h256> hashes;
for (unsigned i = 0; i != numHeadersToSend; ++i)
{
if (!blockHash || !m_chain.isKnown(blockHash))
break;
hashes.push_back(blockHash);
++itemCount;
blockHash = nextHash(blockHash, step);
}
for (unsigned i = 0; i < hashes.size() && rlp.size() < c_maxPayload; ++i)
rlp += m_chain.headerData(hashes[_reverse ? i : hashes.size() - 1 - i]);
return make_pair(rlp, itemCount);
}
pair<bytes, unsigned> blockBodies(RLP const& _blockHashes) const override
{
unsigned const count = static_cast<unsigned>(_blockHashes.itemCount());
bytes rlp;
unsigned n = 0;
auto numBodiesToSend = std::min(count, c_maxBlocks);
for (unsigned i = 0; i < numBodiesToSend && rlp.size() < c_maxPayload; ++i)
{
auto h = _blockHashes[i].toHash<h256>();
if (m_chain.isKnown(h))
{
bytes blockBytes = m_chain.block(h);
RLP block{blockBytes};
RLPStream body;
body.appendList(4);
body.appendRaw(block[1].data()); // transactions
body.appendRaw(block[2].data()); // uncles
body.appendRaw(block[3].data()); // block hash
body.appendRaw(block[4].data()); // sign_list
auto bodyBytes = body.out();
rlp.insert(rlp.end(), bodyBytes.begin(), bodyBytes.end());
++n;
}
}
if (count > 20 && n == 0)
LOG(WARNING) << "all" << count << "unknown blocks requested; peer on different chain?";
else
LOG(TRACE) << n << "blocks known and returned;" << (numBodiesToSend - n) << "blocks unknown;" << (count > c_maxBlocks ? count - c_maxBlocks : 0) << "blocks ignored";
return make_pair(rlp, n);
}
strings nodeData(RLP const& _dataHashes) const override
{
unsigned const count = static_cast<unsigned>(_dataHashes.itemCount());
strings data;
size_t payloadSize = 0;
auto numItemsToSend = std::min(count, c_maxNodes);
for (unsigned i = 0; i < numItemsToSend && payloadSize < c_maxPayload; ++i)
{
auto h = _dataHashes[i].toHash<h256>();
auto node = m_db.lookup(h);
if (!node.empty())
{
payloadSize += node.length();
data.push_back(move(node));
}
}
LOG(TRACE) << data.size() << " nodes known and returned;" << (numItemsToSend - data.size()) << " unknown;" << (count > c_maxNodes ? count - c_maxNodes : 0) << " ignored";
return data;
}
pair<bytes, unsigned> receipts(RLP const& _blockHashes) const override
{
unsigned const count = static_cast<unsigned>(_blockHashes.itemCount());
bytes rlp;
unsigned n = 0;
auto numItemsToSend = std::min(count, c_maxReceipts);
for (unsigned i = 0; i < numItemsToSend && rlp.size() < c_maxPayload; ++i)
{
auto h = _blockHashes[i].toHash<h256>();
if (m_chain.isKnown(h))
{
auto const receipts = m_chain.receipts(h);
auto receiptsRlpList = receipts.rlp();
rlp.insert(rlp.end(), receiptsRlpList.begin(), receiptsRlpList.end());
++n;
}
}
LOG(TRACE) << n << " receipt lists known and returned;" << (numItemsToSend - n) << " unknown;" << (count > c_maxReceipts ? count - c_maxReceipts : 0) << " ignored";
return make_pair(rlp, n);
}
private:
BlockChain const& m_chain;
OverlayDB const& m_db;
};
}
EthereumHost::EthereumHost(BlockChain const& _ch, OverlayDB const& _db, TransactionQueue& _tq, BlockQueue& _bq, u256 _networkId):
HostCapability<EthereumPeer>(),
Worker ("ethsync"),
m_chain (_ch),
m_db(_db),
m_tq (_tq),
m_bq (_bq),
m_networkId (_networkId),
m_hostData(make_shared<EthereumHostData>(m_chain, m_db))
{
// TODO: Composition would be better. Left like that to avoid initialization
// issues as BlockChainSync accesses other EthereumHost members.
m_sync.reset(new BlockChainSync(*this));
m_peerObserver = make_shared<EthereumPeerObserver>(*m_sync, x_sync, m_tq);
NodeConnManagerSingleton::GetInstance().setEthereumHost(this);
m_latestBlockSent = _ch.currentHash();
m_tq.onImport([this](ImportResult _ir, h256 const & _h, h512 const & _nodeId) { onTransactionImported(_ir, _h, _nodeId); });
}
EthereumHost::~EthereumHost()
{
}
bool EthereumHost::ensureInitialised()
{
if (!m_latestBlockSent)
{
// First time - just initialise.
m_latestBlockSent = m_chain.currentHash();
LOG(TRACE) << "Initialising: latest=" << m_latestBlockSent;
Guard l(x_transactions);
m_transactionsSent = m_tq.knownTransactions();
return true;
}
return false;
}
void EthereumHost::reset()
{
RecursiveGuard l(x_sync);
m_sync->abortSync();
m_latestBlockSent = h256();
Guard tl(x_transactions);
m_transactionsSent.clear();
}
void EthereumHost::doWork()
{
bool netChange = ensureInitialised();
auto h = m_chain.currentHash();
// If we've finished our initial sync (including getting all the blocks into the chain so as to reduce invalid transactions), start trading transactions & blocks
if (!isSyncing() && m_chain.isKnown(m_latestBlockSent))
{
if (m_newTransactions)
{
m_newTransactions = false;
maintainTransactions();//广播交易
}
if (m_newBlocks)
{
m_newBlocks = false;
maintainBlocks(h);//广播块
}
}
time_t now = std::chrono::system_clock::to_time_t(chrono::system_clock::now());
if (now - m_lastTick >= 1)
{
m_lastTick = now;
foreachPeer([](std::shared_ptr<EthereumPeer> _p) { _p->tick(); return true; });
}
// return netChange;
// TODO: Figure out what to do with netChange.
(void)netChange;
}
void EthereumHost::maintainTransactions()
{
// Send any new transactions.
unordered_map<std::shared_ptr<EthereumPeer>, std::vector<size_t>> peerTransactions;
auto ts = m_tq.allTransactions();
{
Guard l(x_transactions);
unsigned count = 0;
for (size_t i = 0; count < c_maxSendTransactions && i < ts.size(); ++i) {
auto const& t = ts[i];
bool unsent = !m_transactionsSent.count(t.sha3());
auto peers = get<1>(randomSelection(0, [&](EthereumPeer * p) {
DEV_GUARDED(p->x_knownTransactions)
return p->m_requireTransactions || (unsent && !p->m_knownTransactions.count(t.sha3()));
return false;
}));
for (auto const& p : peers) {
peerTransactions[p].push_back(i);
}
if (peers.size() > 0) {
count++;
}
if (unsent) {
m_transactionsSent.insert(t.sha3());
}
}
}
foreachPeer([&](shared_ptr<EthereumPeer> _p)
{
bytes b;
unsigned n = 0;
DEV_GUARDED(_p->x_knownTransactions)
for (auto const& i : peerTransactions[_p])
{
if (_p->m_knownTransactions.size() > c_maxSendTransactions) {
_p->m_knownTransactions.pop();
}
_p->m_knownTransactions.insert(ts[i].sha3());
b += ts[i].rlp();
++n;
}
if (n || _p->m_requireTransactions)
{
RLPStream ts;
_p->prep(ts, TransactionsPacket, n).appendRaw(b, n);
_p->sealAndSend(ts);
LOG(TRACE) << "Sent" << n << "transactions to " << _p->session()->info().clientVersion;
}
_p->m_requireTransactions = false;
return true;
});
}
void EthereumHost::foreachPeer(std::function<bool(std::shared_ptr<EthereumPeer>)> const & _f) const
{
//order peers by protocol, rating, connection age
auto sessions = peerSessions();
auto sessionLess = [](std::pair<std::shared_ptr<SessionFace>, std::shared_ptr<Peer>> const & _left, std::pair<std::shared_ptr<SessionFace>, std::shared_ptr<Peer>> const & _right)
{ return _left.first->rating() == _right.first->rating() ? _left.first->connectionTime() < _right.first->connectionTime() : _left.first->rating() > _right.first->rating(); };
std::sort(sessions.begin(), sessions.end(), sessionLess);
for (auto s : sessions)
if (!_f(capabilityFromSession<EthereumPeer>(*s.first)))
return;
sessions = peerSessions(c_oldProtocolVersion); //TODO: remove once v61+ is common
std::sort(sessions.begin(), sessions.end(), sessionLess);
for (auto s : sessions)
if (!_f(capabilityFromSession<EthereumPeer>(*s.first, c_oldProtocolVersion)))
return;
}
tuple<vector<shared_ptr<EthereumPeer>>, vector<shared_ptr<EthereumPeer>>, vector<shared_ptr<SessionFace>>> EthereumHost::randomSelection(unsigned _percent, std::function<bool(EthereumPeer*)> const & _allow)
{
vector<shared_ptr<EthereumPeer>> chosen;
vector<shared_ptr<EthereumPeer>> allowed;
vector<shared_ptr<SessionFace>> sessions;
size_t peerCount = 0;
foreachPeer([&](std::shared_ptr<EthereumPeer> _p)
{
if (_allow(_p.get()))
{
allowed.push_back(_p);
sessions.push_back(_p->session());
}
++peerCount;
return true;
});
size_t chosenSize = (peerCount * _percent + 99) / 100;
chosen.reserve(chosenSize);
for (unsigned i = chosenSize; i && allowed.size(); i--)
{
unsigned n = rand() % allowed.size();
chosen.push_back(std::move(allowed[n]));
allowed.erase(allowed.begin() + n);
}
return make_tuple(move(chosen), move(allowed), move(sessions));
}
void EthereumHost::maintainBlocks(h256 const & _currentHash)
{
// Send any new blocks.
auto detailsFrom = m_chain.details(m_latestBlockSent);
auto detailsTo = m_chain.details(_currentHash);
if (detailsFrom.totalDifficulty < detailsTo.totalDifficulty)
{
if (diff(detailsFrom.number, detailsTo.number) < 20)
{
// don't be sending more than 20 "new" blocks. if there are any more we were probably waaaay behind.
LOG(TRACE) << "Sending a new block (current is" << _currentHash << ", was" << m_latestBlockSent << ")";
h256s blocks = get<0>(m_chain.treeRoute(m_latestBlockSent, _currentHash, false, false, true));
//先发送NewBlockHash
auto s = randomSelection(25, [&](EthereumPeer * p) {
DEV_GUARDED(p->x_knownBlocks)
return !p->m_knownBlocks.count(_currentHash);
return false;
});
for (shared_ptr<EthereumPeer> const& p : get<1>(s))
{
RLPStream ts;
p->prep(ts, NewBlockHashesPacket, blocks.size());
for (auto const& b : blocks)
{
ts.appendList(2);
ts.append(b);
ts.append(m_chain.number(b));
}
p->sealAndSend(ts);
}
std::this_thread::sleep_for(chrono::milliseconds(100));
auto s2 = randomSelection(25, [&](EthereumPeer * p) {
DEV_GUARDED(p->x_knownBlocks)
return !p->m_knownBlocks.count(_currentHash);
return false;
});
for (shared_ptr<EthereumPeer> const& p : get<0>(s2))
for (auto const& b : blocks)
{
RLPStream ts;
p->prep(ts, NewBlockPacket, 2).appendRaw(m_chain.block(b), 1).append(m_chain.details(b).totalDifficulty);
p->sealAndSend(ts);
DEV_GUARDED(p->x_knownBlocks)
{
if (p->m_knownBlocks.size() > EthereumPeer::kKnownBlockSize) {
p->m_knownBlocks.pop();
}
p->m_knownBlocks.insert(b);
}
}
}
m_latestBlockSent = _currentHash;
}
}
bool EthereumHost::isSyncing() const
{
return m_sync->isSyncing();
}
SyncStatus EthereumHost::status() const
{
RecursiveGuard l(x_sync);
return m_sync->status();
}
void EthereumHost::onTransactionImported(ImportResult _ir, h256 const & _h, h512 const & _nodeId)
{
auto session = host()->peerSession(_nodeId);
if (!session)
return;
std::shared_ptr<EthereumPeer> peer = capabilityFromSession<EthereumPeer>(*session);
if (!peer)
peer = capabilityFromSession<EthereumPeer>(*session, c_oldProtocolVersion);
if (!peer)
return;
Guard l(peer->x_knownTransactions);
peer->m_knownTransactions.insert(_h);
switch (_ir)
{
case ImportResult::Malformed:
peer->addRating(-100);
break;
case ImportResult::AlreadyKnown:
// if we already had the transaction, then don't bother sending it on.
DEV_GUARDED(x_transactions)
m_transactionsSent.insert(_h);
peer->addRating(0);
break;
case ImportResult::Success:
peer->addRating(100);
break;
default:;
}
}
shared_ptr<Capability> EthereumHost::newPeerCapability(shared_ptr<SessionFace> const & _s, unsigned _idOffset, p2p::CapDesc const & _cap, uint16_t _capID)
{
auto ret = HostCapability<EthereumPeer>::newPeerCapability(_s, _idOffset, _cap, _capID);
auto cap = capabilityFromSession<EthereumPeer>(*_s, _cap.second);
assert(cap);
LOG(TRACE) << "EthereumHost::newPeerCapability totalDifficulty=" << m_chain.details().totalDifficulty << ",number=" << m_chain.info().number();
cap->init(
protocolVersion(),
m_networkId,
m_chain.details().totalDifficulty,
m_chain.currentHash(),
m_chain.genesisHash(),
m_chain.info().number(),
m_hostData,
m_peerObserver
);
return ret;
}
void EthereumHost::addNodeConnParam(std::vector<NodeConnParams> const & vParams)
{
foreachPeer([&](std::shared_ptr<EthereumPeer> _p)
{
RLPStream ts;
_p->prep(ts, NodeInfoSync, vParams.size());
for (auto const& param : vParams)
{
ts.appendList(6);
ts.append(param._sNodeId);
ts.append(param._sAgencyInfo);
ts.append(param._sAgencyDesc);
ts.append(param._iIdentityType);
ts.append(param._sIP);
ts.append(param._iPort);
}
LOG(TRACE) << "EthereumHost::addNodeConnParam sealandsend " << vParams.size() << std::endl;
_p->sealAndSend(ts);
return true;
});
}
void EthereumHost::delNodeConnParam(std::string const & sParams)
{
if (sParams == "")
{
return;
}
foreachPeer([&](std::shared_ptr<EthereumPeer> _p)
{
RLPStream ts;
_p->prep(ts, DelNodeInfoSync, 1) << sParams;
LOG(TRACE) << "EthereumHost::delNodeConnParam sealandsend " << sParams << std::endl;
_p->sealAndSend(ts);
return true;
});
}