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

Commit ded34f0

Browse files
committed
Minor code cleanup
Change capability background work intervals from constexpr in unnamed namespace to static constexpr class members since this makes more conceptual sense given that the values are exposed via a function (backgroundWorkInterval()), updated some license messages, removed unnecessary class names when creating CapDesc instances, changed Host run timer from deadline timer to steady timer so we can use std::chrono, and added some comments.
1 parent 75d941a commit ded34f0

File tree

11 files changed

+57
-57
lines changed

11 files changed

+57
-57
lines changed

libethereum/EthereumCapability.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ using namespace dev::eth;
2222
char const* const EthereumCapability::s_stateNames[static_cast<int>(SyncState::Size)] = {
2323
"NotSynced", "Idle", "Waiting", "Blocks", "State"};
2424

25-
constexpr chrono::milliseconds c_backgroundWorkInterval{1000};
25+
chrono::milliseconds constexpr EthereumCapability::s_backgroundWorkInterval;
2626

2727
namespace
2828
{
29-
constexpr unsigned c_maxSendTransactions = 256;
30-
constexpr unsigned c_maxHeadersToSend = 1024;
31-
constexpr unsigned c_maxIncomingNewHashes = 1024;
32-
constexpr unsigned c_peerTimeoutSeconds = 10;
33-
constexpr int c_minBlockBroadcastPeers = 4;
29+
static constexpr unsigned c_maxSendTransactions = 256;
30+
static constexpr unsigned c_maxHeadersToSend = 1024;
31+
static constexpr unsigned c_maxIncomingNewHashes = 1024;
32+
static constexpr unsigned c_peerTimeoutSeconds = 10;
33+
static constexpr int c_minBlockBroadcastPeers = 4;
3434

3535
string toString(Asking _a)
3636
{
@@ -402,14 +402,13 @@ EthereumCapability::EthereumCapability(shared_ptr<p2p::CapabilityHostFace> _host
402402

403403
chrono::milliseconds EthereumCapability::backgroundWorkInterval() const
404404
{
405-
return c_backgroundWorkInterval;
405+
return s_backgroundWorkInterval;
406406
}
407407

408408
void EthereumCapability::onStarting()
409409
{
410410
m_backgroundWorkEnabled = true;
411-
m_host->scheduleCapabilityBackgroundWork(
412-
p2p::CapDesc{name(), version()}, [this]() { doBackgroundWork(); });
411+
m_host->scheduleCapabilityBackgroundWork({name(), version()}, [this]() { doBackgroundWork(); });
413412
}
414413

415414
void EthereumCapability::onStopping()
@@ -437,7 +436,7 @@ void EthereumCapability::reset()
437436

438437
// reset() can be called from RPC handling thread,
439438
// but we access m_latestBlockSent and m_transactionsSent only from the network thread
440-
m_host->postCapabilityWork(p2p::CapDesc{name(), version()}, [this]() {
439+
m_host->postCapabilityWork({name(), version()}, [this]() {
441440
m_latestBlockSent = h256();
442441
m_transactionsSent.clear();
443442
});
@@ -482,7 +481,8 @@ void EthereumCapability::doBackgroundWork()
482481
}
483482

484483
if (m_backgroundWorkEnabled)
485-
m_host->scheduleCapabilityBackgroundWork(make_pair(name(), version()), [this]() { doBackgroundWork(); });
484+
m_host->scheduleCapabilityBackgroundWork(
485+
{name(), version()}, [this]() { doBackgroundWork(); });
486486
}
487487

488488
void EthereumCapability::maintainTransactions()
@@ -645,7 +645,7 @@ SyncStatus EthereumCapability::status() const
645645
void EthereumCapability::onTransactionImported(
646646
ImportResult _ir, h256 const& _h, h512 const& _nodeId)
647647
{
648-
m_host->postCapabilityWork(p2p::CapDesc{name(), version()}, [this, _ir, _h, _nodeId]() {
648+
m_host->postCapabilityWork({name(), version()}, [this, _ir, _h, _nodeId]() {
649649
auto itPeerStatus = m_peers.find(_nodeId);
650650
if (itPeerStatus == m_peers.end())
651651
return;

libethereum/EthereumCapability.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class EthereumCapability : public p2p::CapabilityFace
132132

133133
private:
134134
static char const* const s_stateNames[static_cast<int>(SyncState::Size)];
135+
static constexpr std::chrono::milliseconds s_backgroundWorkInterval{1000};
135136

136137
std::vector<NodeID> selectPeers(
137138
std::function<bool(EthereumPeer const&)> const& _predicate) const;

libethereum/WarpCapability.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ namespace dev
1616
{
1717
namespace eth
1818
{
19+
20+
chrono::milliseconds constexpr WarpCapability::s_backgroundWorkInterval;
21+
1922
namespace
2023
{
2124
static size_t const c_freePeerBufferSize = 32;
22-
constexpr chrono::milliseconds c_backgroundWorkInterval{1000};
2325

2426
bool validateManifest(RLP const& _manifestRlp)
2527
{
@@ -315,14 +317,13 @@ WarpCapability::WarpCapability(shared_ptr<p2p::CapabilityHostFace> _host,
315317

316318
chrono::milliseconds WarpCapability::backgroundWorkInterval() const
317319
{
318-
return c_backgroundWorkInterval;
320+
return s_backgroundWorkInterval;
319321
}
320322

321323
void WarpCapability::onStarting()
322324
{
323325
m_backgroundWorkEnabled = true;
324-
m_host->scheduleCapabilityBackgroundWork(
325-
p2p::CapDesc{name(), version()}, [this]() { doBackgroundWork(); });
326+
m_host->scheduleCapabilityBackgroundWork({name(), version()}, [this]() { doBackgroundWork(); });
326327
}
327328

328329
void WarpCapability::onStopping()
@@ -351,7 +352,7 @@ void WarpCapability::doBackgroundWork()
351352

352353
if (m_backgroundWorkEnabled)
353354
m_host->scheduleCapabilityBackgroundWork(
354-
p2p::CapDesc{name(), version()}, [this]() { doBackgroundWork(); });
355+
{name(), version()}, [this]() { doBackgroundWork(); });
355356
}
356357

357358
void WarpCapability::onConnect(NodeID const& _peerID, u256 const& /* _peerCapabilityVersion */)

libethereum/WarpCapability.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ class WarpCapability : public p2p::CapabilityFace
105105
void disablePeer(NodeID const& _peerID, std::string const& _problem);
106106

107107
private:
108+
static constexpr std::chrono::milliseconds s_backgroundWorkInterval{1000};
109+
108110
std::shared_ptr<WarpPeerObserverFace> createPeerObserver(
109111
boost::filesystem::path const& _snapshotDownloadPath);
110112

libp2p/Capability.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1-
/*
2-
This file is part of cpp-ethereum.
3-
4-
cpp-ethereum is free software: you can redistribute it and/or modify
5-
it under the terms of the GNU General Public License as published by
6-
the Free Software Foundation, either version 3 of the License, or
7-
(at your option) any later version.
8-
9-
cpp-ethereum is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
GNU General Public License for more details.
13-
14-
You should have received a copy of the GNU General Public License
15-
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16-
*/
1+
// Aleth: Ethereum C++ client, tools and libraries.
2+
// Copyright 2019 Aleth Authors.
3+
// Licensed under the GNU General Public License, Version 3.
174

185
#pragma once
196

@@ -23,8 +10,7 @@ namespace dev
2310
{
2411
namespace p2p
2512
{
26-
27-
DEV_SIMPLE_EXCEPTION(UnknownCapability);
13+
DEV_SIMPLE_EXCEPTION(CapabilityNotRegistered);
2814

2915
/**
3016
* @brief The Capability interface.

libp2p/Common.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ string reasonOf(DisconnectReason _r)
130130
}
131131
}
132132

133+
ostream& operator<<(ostream& _out, CapDesc const& _capDesc)
134+
{
135+
return _out << _capDesc.first << ": " << _capDesc.second;
136+
}
137+
133138
void NodeIPEndpoint::streamRLP(RLPStream& _s, RLPAppend _append) const
134139
{
135140
if (_append == StreamList)

libp2p/Common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ using CapDesc = std::pair<std::string, unsigned>;
114114
using CapDescSet = std::set<CapDesc>;
115115
using CapDescs = std::vector<CapDesc>;
116116

117+
std::ostream& operator<<(std::ostream& _out, CapDesc const& _capDesc);
118+
117119
/*
118120
* Used by Host to pass negotiated information about a connection to a
119121
* new Peer Session; PeerSessionInfo is then maintained by Session and can

libp2p/Host.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ using namespace dev::p2p;
2727
namespace
2828
{
2929
/// Interval at which Host::run will call keepAlivePeers to ping peers.
30-
constexpr chrono::seconds c_keepAliveInterval = chrono::seconds(30);
30+
static constexpr chrono::seconds c_keepAliveInterval{30};
3131

3232
/// Disconnect timeout after failure to respond to keepAlivePeers ping.
33-
constexpr chrono::seconds c_keepAliveTimeOut = chrono::seconds(1);
33+
static constexpr chrono::seconds c_keepAliveTimeOut{1};
3434

3535
/// Interval which m_runTimer is run when network is connected.
36-
constexpr unsigned int c_runTimerIntervalMs = 100;
36+
static constexpr chrono::milliseconds c_runTimerInterval{100};
3737
} // namespace
3838

3939
HostNodeTableHandler::HostNodeTableHandler(Host& _host): m_host(_host) {}
@@ -732,7 +732,7 @@ void Host::run(boost::system::error_code const& _ec)
732732
return;
733733

734734
auto runcb = [this](boost::system::error_code const& error) { run(error); };
735-
m_runTimer.expires_from_now(boost::posix_time::milliseconds(c_runTimerIntervalMs));
735+
m_runTimer.expires_from_now(c_runTimerInterval);
736736
m_runTimer.async_wait(runcb);
737737
}
738738

@@ -1029,10 +1029,13 @@ void Host::scheduleCapabilityBackgroundWork(CapDesc const& _capDesc, function<vo
10291029
{
10301030
auto cap = m_capabilities.find(_capDesc);
10311031
if (cap == m_capabilities.end())
1032-
BOOST_THROW_EXCEPTION(UnknownCapability());
1032+
{
1033+
LOG(m_logger) << "Capability not registered: " << _capDesc;
1034+
BOOST_THROW_EXCEPTION(CapabilityNotRegistered());
1035+
}
10331036

10341037
auto timer = cap->second.backgroundWorkTimer.get();
1035-
timer->expires_from_now(chrono::milliseconds(cap->second.capability->backgroundWorkInterval()));
1038+
timer->expires_from_now(cap->second.capability->backgroundWorkInterval());
10361039
timer->async_wait([_f](boost::system::error_code _ec) {
10371040
if (!_ec)
10381041
_f();
@@ -1042,7 +1045,10 @@ void Host::scheduleCapabilityBackgroundWork(CapDesc const& _capDesc, function<vo
10421045
void Host::postCapabilityWork(CapDesc const& _capDesc, function<void()> _f)
10431046
{
10441047
if (m_capabilities.find(_capDesc) == m_capabilities.end())
1045-
BOOST_THROW_EXCEPTION(UnknownCapability());
1046-
1048+
{
1049+
LOG(m_logger) << "Capability not registered" << _capDesc;
1050+
BOOST_THROW_EXCEPTION(CapabilityNotRegistered());
1051+
}
1052+
10471053
m_ioService.post(_f);
10481054
}

libp2p/Host.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,11 @@ class Host: public Worker
231231
void forEachPeer(
232232
std::string const& _capabilityName, std::function<bool(NodeID const&)> _f) const;
233233

234+
/// Schedule a capability's background work loop on the network thread
234235
void scheduleCapabilityBackgroundWork(CapDesc const& _capDesc, std::function<void()> _f);
235-
void postCapabilityWork(CapDesc const& _capdesc, std::function<void()> _f);
236+
237+
/// Execute capability work on the network thread
238+
void postCapabilityWork(CapDesc const& _capDesc, std::function<void()> _f);
236239

237240
std::shared_ptr<CapabilityHostFace> capabilityHost() const { return m_capabilityHost; }
238241

@@ -320,7 +323,7 @@ class Host: public Worker
320323
bi::tcp::acceptor m_tcp4Acceptor; ///< Listening acceptor.
321324

322325
/// Timer which, when network is running, calls run() every c_timerInterval ms.
323-
io::deadline_timer m_runTimer;
326+
ba::steady_timer m_runTimer;
324327

325328
std::set<Peer*> m_pendingPeerConns; /// Used only by connect(Peer&) to limit concurrently connecting to same node. See connect(shared_ptr<Peer>const&).
326329

test/unittests/libp2p/capability.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ using namespace std;
1515
using namespace dev;
1616
using namespace dev::p2p;
1717

18-
namespace
19-
{
20-
chrono::milliseconds constexpr c_backgroundWorkInterval{1000};
21-
}
22-
2318
class TestCapability : public CapabilityFace, public Worker
2419
{
2520
public:
@@ -30,7 +25,7 @@ class TestCapability : public CapabilityFace, public Worker
3025
unsigned messageCount() const override { return UserPacket + 1; }
3126
chrono::milliseconds backgroundWorkInterval() const override
3227
{
33-
return c_backgroundWorkInterval;
28+
return s_backgroundWorkInterval;
3429
}
3530

3631
void onStarting() override {}
@@ -75,6 +70,8 @@ class TestCapability : public CapabilityFace, public Worker
7570
return {cnt, checksum};
7671
}
7772

73+
static chrono::milliseconds constexpr s_backgroundWorkInterval{1000};
74+
7875
Host const& m_host;
7976
unordered_map<NodeID, int> m_cntReceivedMessages;
8077
unordered_map<NodeID, int> m_testSums;

0 commit comments

Comments
 (0)