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
407391EthereumCapability::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
543521void 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 (
0 commit comments