Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 2eb39cb

Browse files
committed
Address PR feedback
Fix comment formatting, return bad block hashes in BlockChain::sync() via existing tuple rather than via out parameter, remove unused EthereumCapability::randomPartitionPeers, optimize EthereumCapability::randomPeers
1 parent adffa6c commit 2eb39cb

File tree

7 files changed

+47
-72
lines changed

7 files changed

+47
-72
lines changed

libdevcore/Common.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,4 @@ bool isFalse(std::string const& _m)
136136
return _m == "off" || _m == "no" || _m == "false" || _m == "0";
137137
}
138138

139-
mt19937_64 g_randomGenerator{random_device{}()};
140-
141-
int randomNumber(int _min, int _max)
142-
{
143-
return uniform_int_distribution<int>{_min, _max}(g_randomGenerator);
144-
}
145139
} // namespace dev

libdevcore/Common.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,4 @@ class ExitHandler
317317

318318
bool isTrue(std::string const& _m);
319319
bool isFalse(std::string const& _m);
320-
321-
int randomNumber(int _min = INT_MIN, int _max = INT_MAX);
322320
} // namespace dev

libethereum/BlockChain.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -427,19 +427,19 @@ tuple<ImportRoute, bool, unsigned> BlockChain::sync(
427427
VerifiedBlocks blocks;
428428
_bq.drain(blocks, _max);
429429

430-
h256s badBlockHashes;
431-
std::tuple<ImportRoute, unsigned> const importResult = sync(blocks, badBlockHashes, _stateDB);
432-
bool const moreBlocks = _bq.doneDrain(badBlockHashes);
433-
return {std::get<0>(importResult), moreBlocks, std::get<1>(importResult)};
430+
std::tuple<ImportRoute, h256s, unsigned> const importResult = sync(blocks, _stateDB);
431+
bool const moreBlocks = _bq.doneDrain(std::get<1>(importResult));
432+
return {std::get<0>(importResult), moreBlocks, std::get<2>(importResult)};
434433
}
435434

436-
tuple<ImportRoute, unsigned> BlockChain::sync(
437-
VerifiedBlocks const& _blocks, h256s& o_badBlockHashes, OverlayDB const& _stateDB)
435+
tuple<ImportRoute, h256s, unsigned> BlockChain::sync(
436+
VerifiedBlocks const& _blocks, OverlayDB const& _stateDB)
438437
{
439438
h256s fresh;
440439
h256s dead;
441440
Transactions goodTransactions;
442441
unsigned count = 0;
442+
h256s badBlockHashes;
443443
for (VerifiedBlock const& block : _blocks)
444444
{
445445
do {
@@ -465,7 +465,7 @@ tuple<ImportRoute, unsigned> BlockChain::sync(
465465
cwarn << "ODD: Import queue contains block with unknown parent.";// << LogTag::Error << boost::current_exception_diagnostic_information();
466466
// NOTE: don't reimport since the queue should guarantee everything in the right order.
467467
// Can't continue - chain bad.
468-
o_badBlockHashes.push_back(block.verified.info.hash());
468+
badBlockHashes.push_back(block.verified.info.hash());
469469
}
470470
catch (dev::eth::FutureTime const&)
471471
{
@@ -485,11 +485,11 @@ tuple<ImportRoute, unsigned> BlockChain::sync(
485485
m_onBad(ex);
486486
// NOTE: don't reimport since the queue should guarantee everything in the right order.
487487
// Can't continue - chain bad.
488-
o_badBlockHashes.push_back(block.verified.info.hash());
488+
badBlockHashes.push_back(block.verified.info.hash());
489489
}
490490
} while (false);
491491
}
492-
return {ImportRoute{dead, fresh, goodTransactions}, count};
492+
return {ImportRoute{dead, fresh, goodTransactions}, badBlockHashes, count};
493493
}
494494

495495
pair<ImportResult, ImportRoute> BlockChain::attemptImport(bytes const& _block, OverlayDB const& _stateDB, bool _mustBeNew) noexcept

libethereum/BlockChain.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ class BlockChain
108108
BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max);
109109

110110
/// Import the supplied blocks into the chain. Blocks should be processed in order.
111-
/// @returns a tuple with two members - the first (ImportRoute) contains fresh blocks, dead
112-
/// blocks and imported transactions. The second contains the imported block count.
113-
std::tuple<ImportRoute, unsigned> sync(
114-
VerifiedBlocks const& _blocks, h256s& o_badBlockHashes, OverlayDB const& _stateDB);
111+
/// @returns a tuple with three members - the first (ImportRoute) contains fresh blocks, dead
112+
/// blocks and imported transactions. The second contains hashes of bad blocks. The third
113+
/// contains the imported block count.
114+
std::tuple<ImportRoute, h256s, unsigned> sync(
115+
VerifiedBlocks const& _blocks, OverlayDB const& _stateDB);
115116

116117
/// Attempt to import the given block directly into the BlockChain and sync with the state DB.
117118
/// @returns the block hashes of any blocks that came into/went out of the canonical block chain.

libethereum/Client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ void Client::syncBlockQueue()
386386
// cdebug << "syncBlockQueue()";
387387

388388
ImportRoute ir;
389+
h256s badBlockHashes;
389390
unsigned count;
390391
Timer t;
391392

@@ -398,8 +399,7 @@ void Client::syncBlockQueue()
398399
// Client's lifetime
399400
h->propagateNewBlocks(verifiedBlocks);
400401

401-
h256s badBlockHashes;
402-
std::tie(ir, count) = bc().sync(*verifiedBlocks, badBlockHashes, m_stateDB);
402+
std::tie(ir, badBlockHashes, count) = bc().sync(*verifiedBlocks, m_stateDB);
403403
m_syncBlockQueue = m_bq.doneDrain(badBlockHashes);
404404

405405
double elapsed = t.elapsed();

libethereum/EthereumCapability.cpp

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "BlockChainSync.h"
88
#include "BlockQueue.h"
99
#include "TransactionQueue.h"
10-
#include <libdevcore/Common.h>
1110
#include <libethcore/Exceptions.h>
1211
#include <libp2p/Common.h>
1312
#include <libp2p/Host.h>
@@ -212,14 +211,14 @@ class EthereumHostData : public EthereumHostDataFace
212211
top = n + step * (numHeadersToSend - 1);
213212
}
214213
assert(top <= lastBlock && "invalid top block calculated");
215-
blockHash = m_chain.numberHash(static_cast<unsigned>(
216-
top)); // override start block hash with the hash of the top block we have
214+
// override start block hash with the hash of the top block we have
215+
blockHash = m_chain.numberHash(static_cast<unsigned>(top));
217216
}
218217
else
219218
blockHash = {};
220219
}
221220
}
222-
else // block id is a number
221+
else // block id is a number
223222
{
224223
auto n = _blockId.toInt<bigint>();
225224
cnetlog << "GetBlockHeaders (" << n << " max: " << _maxHeaders << " skip: " << _skip
@@ -239,8 +238,8 @@ class EthereumHostData : public EthereumHostDataFace
239238
top = n + step * (numHeadersToSend - 1);
240239
}
241240
assert(top <= lastBlock && "invalid top block calculated");
242-
blockHash = m_chain.numberHash(static_cast<unsigned>(
243-
top)); // override start block hash with the hash of the top block we have
241+
// override start block hash with the hash of the top block we have
242+
blockHash = m_chain.numberHash(static_cast<unsigned>(top));
244243
}
245244
}
246245
else if (n <= std::numeric_limits<unsigned>::max())
@@ -253,21 +252,23 @@ class EthereumHostData : public EthereumHostDataFace
253252
constexpr unsigned c_blockNumberUsageLimit = 1000;
254253

255254
const auto lastBlock = m_chain.number();
255+
// find the number of the block below which we don't expect BC changes.
256256
const auto limitBlock =
257257
lastBlock > c_blockNumberUsageLimit ?
258258
lastBlock - c_blockNumberUsageLimit :
259-
0; // find the number of the block below which we don't expect BC changes.
259+
0;
260260

261-
while (_step) // parent hash traversal
261+
while (_step) // parent hash traversal
262262
{
263263
auto details = m_chain.details(_h);
264264
if (details.number < limitBlock)
265-
break; // stop using parent hash traversal, fallback to using block numbers
265+
// stop using parent hash traversal, fallback to using block numbers
266+
break;
266267
_h = details.parent;
267268
--_step;
268269
}
269270

270-
if (_step) // still need lower block
271+
if (_step) // still need lower block
271272
{
272273
auto n = m_chain.number(_h);
273274
if (n >= _step)
@@ -385,23 +386,6 @@ class EthereumHostData : public EthereumHostDataFace
385386
BlockChain const& m_chain;
386387
OverlayDB const& m_db;
387388
};
388-
389-
std::vector<NodeID> randomPeers(std::vector<NodeID> const& _peers, size_t _count)
390-
{
391-
if (_peers.size() <= _count || _peers.empty())
392-
return _peers;
393-
394-
std::vector<NodeID> peers{_peers};
395-
std::vector<NodeID> randomPeers;
396-
while (_count-- && !peers.empty())
397-
{
398-
auto const cIter = peers.begin() + randomNumber(0, peers.size() - 1);
399-
randomPeers.push_back(*cIter);
400-
peers.erase(cIter);
401-
}
402-
403-
return randomPeers;
404-
}
405389
} // namespace
406390

407391
EthereumCapability::EthereumCapability(shared_ptr<p2p::CapabilityHostFace> _host,
@@ -523,32 +507,29 @@ vector<NodeID> EthereumCapability::selectPeers(
523507
return allowed;
524508
}
525509

526-
std::pair<std::vector<NodeID>, std::vector<NodeID>> EthereumCapability::randomPartitionPeers(
527-
std::vector<NodeID> const& _peers, std::size_t _number) const
510+
std::vector<NodeID> EthereumCapability::randomPeers(
511+
std::vector<NodeID> const& _peers, size_t _count) const
528512
{
529-
vector<NodeID> part1(_peers);
530-
vector<NodeID> part2;
531-
532-
if (_number >= _peers.size())
533-
return std::make_pair(part1, part2);
534-
535-
std::shuffle(part1.begin(), part1.end(), m_urng);
536-
537-
// Remove elements from the end of the shuffled part1 vector and move them to part2.
538-
std::move(part1.begin() + _number, part1.end(), std::back_inserter(part2));
539-
part1.erase(part1.begin() + _number, part1.end());
540-
return std::make_pair(move(part1), move(part2));
513+
std::vector<NodeID> peers{_peers};
514+
if (_count > peers.size())
515+
_count = peers.size();
516+
auto itEnd = peers.begin() + _count;
517+
std::shuffle(peers.begin(), itEnd, m_urng);
518+
return {peers.begin(), itEnd};
541519
}
542520

543521
void EthereumCapability::maintainBlockHashes(h256 const& _currentHash)
544522
{
545523
// Send any new block hashes
546524
auto const detailsFrom = m_chain.details(m_latestBlockHashSent);
547525
auto const detailsTo = m_chain.details(_currentHash);
548-
if (detailsFrom.totalDifficulty >= detailsTo.totalDifficulty ||
549-
// don't be sending more than c_maxSendNewBlocksCount "new" block hashes. if there are any
550-
// more we were probably waaaay behind.
551-
diff(detailsFrom.number, detailsTo.number) > c_maxSendNewBlocksCount)
526+
527+
if (detailsFrom.totalDifficulty >= detailsTo.totalDifficulty)
528+
return;
529+
530+
// don't be sending more than c_maxSendNewBlocksCount "new" block hashes. if there are any
531+
// more we were probably waaaay behind.
532+
if (diff(detailsFrom.number, detailsTo.number) > c_maxSendNewBlocksCount)
552533
return;
553534

554535
LOG(m_logger) << "Sending new block hashes (current is " << _currentHash << ", was "
@@ -970,8 +951,10 @@ void EthereumCapability::propagateNewBlocks(std::shared_ptr<VerifiedBlocks const
970951
auto const latestHash = _newBlocks->back().verified.info.hash();
971952
auto const detailsFrom = m_chain.details(m_latestBlockSent);
972953
auto const detailsTo = m_chain.details(latestHash);
973-
if (detailsFrom.totalDifficulty >= detailsTo.totalDifficulty ||
974-
diff(detailsFrom.number, detailsTo.number) > c_maxSendNewBlocksCount)
954+
if (detailsFrom.totalDifficulty >= detailsTo.totalDifficulty)
955+
return;
956+
957+
if (diff(detailsFrom.number, detailsTo.number) > c_maxSendNewBlocksCount)
975958
return;
976959

977960
auto const peersWithoutBlock = selectPeers(

libethereum/EthereumCapability.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ class EthereumCapability : public p2p::CapabilityFace
155155
std::vector<NodeID> selectPeers(
156156
std::function<bool(EthereumPeer const&)> const& _predicate) const;
157157

158-
std::pair<std::vector<NodeID>, std::vector<NodeID>> randomPartitionPeers(
159-
std::vector<NodeID> const& _peers, std::size_t _number) const;
158+
std::vector<NodeID> randomPeers(std::vector<NodeID> const& _peers, size_t _count) const;
160159

161160
/// Send top transactions (by nonce and gas price) to available peers
162161
void maintainTransactions();

0 commit comments

Comments
 (0)