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

Commit

Permalink
Revert "using namespace std" changes
Browse files Browse the repository at this point in the history
Also update license message in CapabilityHost files
  • Loading branch information
halfalicious committed Mar 19, 2019
1 parent 2e44d09 commit 18b5273
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 107 deletions.
109 changes: 54 additions & 55 deletions libethereum/EthereumCapability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
#include <chrono>
#include <thread>

using namespace std;
using namespace dev;
using namespace dev::eth;

char const* const EthereumCapability::c_stateNames[static_cast<int>(SyncState::Size)] = {
"NotSynced", "Idle", "Waiting", "Blocks", "State"};

chrono::milliseconds constexpr EthereumCapability::c_backgroundWorkInterval;
std::chrono::milliseconds constexpr EthereumCapability::c_backgroundWorkInterval;

namespace
{
Expand All @@ -32,7 +31,7 @@ constexpr unsigned c_maxIncomingNewHashes = 1024;
constexpr unsigned c_peerTimeoutSeconds = 10;
constexpr int c_minBlockBroadcastPeers = 4;

string toString(Asking _a)
std::string toString(Asking _a)
{
switch (_a)
{
Expand All @@ -59,7 +58,7 @@ string toString(Asking _a)
class EthereumPeerObserver: public EthereumPeerObserverFace
{
public:
EthereumPeerObserver(shared_ptr<BlockChainSync> _sync, TransactionQueue& _tq): m_sync(_sync), m_tq(_tq) {}
EthereumPeerObserver(std::shared_ptr<BlockChainSync> _sync, TransactionQueue& _tq): m_sync(_sync), m_tq(_tq) {}

void onPeerStatus(EthereumPeer const& _peer) override
{
Expand All @@ -78,7 +77,7 @@ class EthereumPeerObserver: public EthereumPeerObserverFace
void onPeerTransactions(NodeID const& _peerID, RLP const& _r) override
{
unsigned itemCount = _r.itemCount();
LOG(m_logger) << "Transactions (" << dec << itemCount << " entries)";
LOG(m_logger) << "Transactions (" << std::dec << itemCount << " entries)";
m_tq.enqueue(_r, _peerID);
}

Expand Down Expand Up @@ -123,7 +122,7 @@ class EthereumPeerObserver: public EthereumPeerObserverFace
}

void onPeerNewHashes(
NodeID const& _peerID, vector<pair<h256, u256>> const& _hashes) override
NodeID const& _peerID, std::vector<std::pair<h256, u256>> const& _hashes) override
{
try
{
Expand Down Expand Up @@ -154,17 +153,17 @@ class EthereumPeerObserver: public EthereumPeerObserverFace
void onPeerNodeData(NodeID const& /* _peerID */, RLP const& _r) override
{
unsigned itemCount = _r.itemCount();
LOG(m_logger) << "Node Data (" << dec << itemCount << " entries)";
LOG(m_logger) << "Node Data (" << std::dec << itemCount << " entries)";
}

void onPeerReceipts(NodeID const& /* _peerID */, RLP const& _r) override
{
unsigned itemCount = _r.itemCount();
LOG(m_logger) << "Receipts (" << dec << itemCount << " entries)";
LOG(m_logger) << "Receipts (" << std::dec << itemCount << " entries)";
}

private:
shared_ptr<BlockChainSync> m_sync;
std::shared_ptr<BlockChainSync> m_sync;
TransactionQueue& m_tq;

Logger m_logger{createLogger(VerbosityDebug, "host")};
Expand All @@ -175,7 +174,7 @@ 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
std::pair<bytes, unsigned> blockHeaders(RLP const& _blockId, unsigned _maxHeaders, u256 _skip, bool _reverse) const override
{
auto numHeadersToSend = _maxHeaders;

Expand Down Expand Up @@ -236,7 +235,7 @@ class EthereumHostData: public EthereumHostDataFace
blockHash = m_chain.numberHash(static_cast<unsigned>(top)); // override start block hash with the hash of the top block we have
}
}
else if (n <= numeric_limits<unsigned>::max())
else if (n <= std::numeric_limits<unsigned>::max())
blockHash = m_chain.numberHash(static_cast<unsigned>(n));
else
blockHash = {};
Expand Down Expand Up @@ -273,7 +272,7 @@ class EthereumHostData: public EthereumHostDataFace

bytes rlp;
unsigned itemCount = 0;
vector<h256> hashes;
std::vector<h256> hashes;
for (unsigned i = 0; i != numHeadersToSend; ++i)
{
if (!blockHash || !m_chain.isKnown(blockHash))
Expand All @@ -288,16 +287,16 @@ class EthereumHostData: public EthereumHostDataFace
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);
return std::make_pair(rlp, itemCount);
}

pair<bytes, unsigned> blockBodies(RLP const& _blockHashes) const override
std::pair<bytes, unsigned> blockBodies(RLP const& _blockHashes) const override
{
unsigned const count = static_cast<unsigned>(_blockHashes.itemCount());

bytes rlp;
unsigned n = 0;
auto numBodiesToSend = min(count, c_maxBlocks);
auto numBodiesToSend = std::min(count, c_maxBlocks);
for (unsigned i = 0; i < numBodiesToSend && rlp.size() < c_maxPayload; ++i)
{
auto h = _blockHashes[i].toHash<h256>();
Expand All @@ -321,7 +320,7 @@ class EthereumHostData: public EthereumHostDataFace
<< " blocks unknown; " << (count > c_maxBlocks ? count - c_maxBlocks : 0)
<< " blocks ignored";

return make_pair(rlp, n);
return std::make_pair(rlp, n);
}

strings nodeData(RLP const& _dataHashes) const override
Expand All @@ -330,7 +329,7 @@ class EthereumHostData: public EthereumHostDataFace

strings data;
size_t payloadSize = 0;
auto numItemsToSend = min(count, c_maxNodes);
auto numItemsToSend = std::min(count, c_maxNodes);
for (unsigned i = 0; i < numItemsToSend && payloadSize < c_maxPayload; ++i)
{
auto h = _dataHashes[i].toHash<h256>();
Expand All @@ -347,13 +346,13 @@ class EthereumHostData: public EthereumHostDataFace
return data;
}

pair<bytes, unsigned> receipts(RLP const& _blockHashes) const override
std::pair<bytes, unsigned> receipts(RLP const& _blockHashes) const override
{
unsigned const count = static_cast<unsigned>(_blockHashes.itemCount());

bytes rlp;
unsigned n = 0;
auto numItemsToSend = min(count, c_maxReceipts);
auto numItemsToSend = std::min(count, c_maxReceipts);
for (unsigned i = 0; i < numItemsToSend && rlp.size() < c_maxPayload; ++i)
{
auto h = _blockHashes[i].toHash<h256>();
Expand All @@ -369,7 +368,7 @@ class EthereumHostData: public EthereumHostDataFace
<< " unknown; " << (count > c_maxReceipts ? count - c_maxReceipts : 0)
<< " ignored";

return make_pair(rlp, n);
return std::make_pair(rlp, n);
}

private:
Expand All @@ -379,7 +378,7 @@ class EthereumHostData: public EthereumHostDataFace

}

EthereumCapability::EthereumCapability(shared_ptr<p2p::CapabilityHostFace> _host,
EthereumCapability::EthereumCapability(std::shared_ptr<p2p::CapabilityHostFace> _host,
BlockChain const& _ch, OverlayDB const& _db, TransactionQueue& _tq, BlockQueue& _bq,
u256 _networkId)
: m_host(move(_host)),
Expand All @@ -396,11 +395,11 @@ EthereumCapability::EthereumCapability(shared_ptr<p2p::CapabilityHostFace> _host
m_peerObserver.reset(new EthereumPeerObserver(m_sync, m_tq));
m_latestBlockSent = _ch.currentHash();
m_tq.onImport([this](ImportResult _ir, h256 const& _h, h512 const& _nodeId) { onTransactionImported(_ir, _h, _nodeId); });
random_device seed;
m_urng = mt19937_64(seed());
std::random_device seed;
m_urng = std::mt19937_64(seed());
}

chrono::milliseconds EthereumCapability::backgroundWorkInterval() const
std::chrono::milliseconds EthereumCapability::backgroundWorkInterval() const
{
return c_backgroundWorkInterval;
}
Expand Down Expand Up @@ -439,7 +438,7 @@ void EthereumCapability::completeSync()
void EthereumCapability::maintainTransactions()
{
// Send any new transactions.
unordered_map<NodeID, vector<size_t>> peerTransactions;
std::unordered_map<NodeID, std::vector<size_t>> peerTransactions;
auto ts = m_tq.topTransactions(c_maxSendTransactions);
{
for (size_t i = 0; i < ts.size(); ++i)
Expand Down Expand Up @@ -482,10 +481,10 @@ void EthereumCapability::maintainTransactions()
}
}

vector<NodeID> EthereumCapability::selectPeers(
function<bool(EthereumPeer const&)> const& _predicate) const
std::vector<NodeID> EthereumCapability::selectPeers(
std::function<bool(EthereumPeer const&)> const& _predicate) const
{
vector<NodeID> allowed;
std::vector<NodeID> allowed;
for (auto const& peer : m_peers)
{
if (_predicate(peer.second))
Expand All @@ -494,21 +493,21 @@ vector<NodeID> EthereumCapability::selectPeers(
return allowed;
}

pair<vector<NodeID>, vector<NodeID>> EthereumCapability::randomPartitionPeers(
vector<NodeID> const& _peers, size_t _number) const
std::pair<std::vector<NodeID>, std::vector<NodeID>> EthereumCapability::randomPartitionPeers(
std::vector<NodeID> const& _peers, size_t _number) const
{
vector<NodeID> part1(_peers);
vector<NodeID> part2;
std::vector<NodeID> part1(_peers);
std::vector<NodeID> part2;

if (_number >= _peers.size())
return make_pair(part1, part2);
return std::make_pair(part1, part2);

shuffle(part1.begin(), part1.end(), m_urng);

// Remove elements from the end of the shuffled part1 vector and move them to part2.
move(part1.begin() + _number, part1.end(), back_inserter(part2));
part1.erase(part1.begin() + _number, part1.end());
return make_pair(move(part1), move(part2));
return std::make_pair(move(part1), move(part2));
}

void EthereumCapability::maintainBlocks(h256 const& _currentHash)
Expand All @@ -524,17 +523,17 @@ void EthereumCapability::maintainBlocks(h256 const& _currentHash)
LOG(m_logger) << "Sending new blocks (current is " << _currentHash << ", was "
<< m_latestBlockSent << ")";

h256s blocks = get<0>(m_chain.treeRoute(m_latestBlockSent, _currentHash, false, false, true));
h256s blocks = std::get<0>(m_chain.treeRoute(m_latestBlockSent, _currentHash, false, false, true));


auto const peersWithoutBlock = selectPeers(
[&](EthereumPeer const& _peer) { return !_peer.isBlockKnown(_currentHash); });

auto const peersToSendNumber =
max<size_t>(c_minBlockBroadcastPeers, sqrt(m_peers.size()));
std::max<size_t>(c_minBlockBroadcastPeers, sqrt(m_peers.size()));

vector<NodeID> peersToSend;
vector<NodeID> peersToAnnounce;
std::vector<NodeID> peersToSend;
std::vector<NodeID> peersToAnnounce;
tie(peersToSend, peersToAnnounce) =
randomPartitionPeers(peersWithoutBlock, peersToSendNumber);

Expand Down Expand Up @@ -644,7 +643,7 @@ bool EthereumCapability::interpretCapabilityPacket(
NodeID const& _peerID, unsigned _id, RLP const& _r)
{
auto& peer = m_peers[_peerID];
peer.setLastAsk(chrono::system_clock::to_time_t(chrono::system_clock::now()));
peer.setLastAsk(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()));

try
{
Expand Down Expand Up @@ -685,13 +684,13 @@ bool EthereumCapability::interpretCapabilityPacket(
static_cast<unsigned>(maxHeaders) :
c_maxHeadersToSend;

if (skip > numeric_limits<unsigned>::max() - 1)
if (skip > std::numeric_limits<unsigned>::max() - 1)
{
cnetdetails << "Requested block skip is too big: " << skip;
break;
}

pair<bytes, unsigned> const rlpAndItemCount =
std::pair<bytes, unsigned> const rlpAndItemCount =
m_hostData->blockHeaders(blockId, numHeadersToSend, skip, reverse);

RLPStream s;
Expand All @@ -716,7 +715,7 @@ bool EthereumCapability::interpretCapabilityPacket(
case GetBlockBodiesPacket:
{
unsigned count = static_cast<unsigned>(_r.itemCount());
cnetlog << "GetBlockBodies (" << dec << count << " entries)";
cnetlog << "GetBlockBodies (" << std::dec << count << " entries)";

if (!count)
{
Expand All @@ -725,7 +724,7 @@ bool EthereumCapability::interpretCapabilityPacket(
break;
}

pair<bytes, unsigned> const rlpAndItemCount = m_hostData->blockBodies(_r);
std::pair<bytes, unsigned> const rlpAndItemCount = m_hostData->blockBodies(_r);

m_host->updateRating(_peerID, 0);
RLPStream s;
Expand Down Expand Up @@ -754,7 +753,7 @@ bool EthereumCapability::interpretCapabilityPacket(
{
unsigned itemCount = _r.itemCount();

cnetlog << "BlockHashes (" << dec << itemCount << " entries) "
cnetlog << "BlockHashes (" << std::dec << itemCount << " entries) "
<< (itemCount ? "" : " : NoMoreHashes");

if (itemCount > c_maxIncomingNewHashes)
Expand All @@ -763,9 +762,9 @@ bool EthereumCapability::interpretCapabilityPacket(
break;
}

vector<pair<h256, u256>> hashes(itemCount);
std::vector<std::pair<h256, u256>> hashes(itemCount);
for (unsigned i = 0; i < itemCount; ++i)
hashes[i] = make_pair(_r[i][0].toHash<h256>(), _r[i][1].toInt<u256>());
hashes[i] = std::make_pair(_r[i][0].toHash<h256>(), _r[i][1].toInt<u256>());

m_peerObserver->onPeerNewHashes(_peerID, hashes);
break;
Expand All @@ -779,7 +778,7 @@ bool EthereumCapability::interpretCapabilityPacket(
m_host->updateRating(_peerID, -10);
break;
}
cnetlog << "GetNodeData (" << dec << count << " entries)";
cnetlog << "GetNodeData (" << std::dec << count << " entries)";

strings const data = m_hostData->nodeData(_r);

Expand All @@ -800,9 +799,9 @@ bool EthereumCapability::interpretCapabilityPacket(
m_host->updateRating(_peerID, -10);
break;
}
cnetlog << "GetReceipts (" << dec << count << " entries)";
cnetlog << "GetReceipts (" << std::dec << count << " entries)";

pair<bytes, unsigned> const rlpAndItemCount = m_hostData->receipts(_r);
std::pair<bytes, unsigned> const rlpAndItemCount = m_hostData->receipts(_r);

m_host->updateRating(_peerID, 0);
RLPStream s;
Expand Down Expand Up @@ -842,7 +841,7 @@ bool EthereumCapability::interpretCapabilityPacket(
cnetlog << "Peer causing an Exception: "
<< boost::current_exception_diagnostic_information() << " " << _r;
}
catch (exception const& _e)
catch (std::exception const& _e)
{
cnetlog << "Peer causing an exception: " << _e.what() << " " << _r;
}
Expand Down Expand Up @@ -870,13 +869,13 @@ void EthereumCapability::doBackgroundWork()
}
}

time_t now = chrono::system_clock::to_time_t(chrono::system_clock::now());
time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
if (now - m_lastTick >= 1)
{
m_lastTick = now;
for (auto const& peer : m_peers)
{
time_t now = chrono::system_clock::to_time_t(chrono::system_clock::now());
time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

if (now - peer.second.lastAsk() > c_peerTimeoutSeconds && peer.second.isConversing())
// timeout
Expand All @@ -899,11 +898,11 @@ void EthereumCapability::setAsking(NodeID const& _peerID, Asking _a)
auto& peerStatus = itPeerStatus->second;

peerStatus.setAsking(_a);
peerStatus.setLastAsk(chrono::system_clock::to_time_t(chrono::system_clock::now()));
peerStatus.setLastAsk(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()));

m_host->addNote(_peerID, "ask", ::toString(_a));
m_host->addNote(_peerID, "sync",
string(isCriticalSyncing(_peerID) ? "ONGOING" : "holding") +
std::string(isCriticalSyncing(_peerID) ? "ONGOING" : "holding") +
(needsSyncing(_peerID) ? " & needed" : ""));
}

Expand All @@ -928,7 +927,7 @@ bool EthereumCapability::needsSyncing(NodeID const& _peerID) const
return (peerStatus != m_peers.end() && peerStatus->second.latestHash());
}

void EthereumCapability::disablePeer(NodeID const& _peerID, string const& _problem)
void EthereumCapability::disablePeer(NodeID const& _peerID, std::string const& _problem)
{
m_host->disableCapability(_peerID, name(), _problem);
}
Expand Down
Loading

0 comments on commit 18b5273

Please sign in to comment.