From ba814939763b817172e162f89fc49f2a3a414f7a Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Sat, 28 Feb 2015 23:51:27 +0100 Subject: [PATCH] StructuredLogger propagated as a const reference --- libdevcore/StructuredLogger.h | 1 + libethereum/BlockChain.cpp | 33 +++++++++++++++------------------ libethereum/BlockChain.h | 8 ++++---- libethereum/Client.cpp | 8 +++----- libethereum/Client.h | 7 +++---- libethereum/EthereumHost.cpp | 6 ++---- libethereum/EthereumHost.h | 3 +-- libp2p/Host.cpp | 8 +++----- libp2p/Host.h | 7 +++---- libwebthree/WebThree.cpp | 2 +- libwebthree/WebThree.h | 2 +- 11 files changed, 37 insertions(+), 48 deletions(-) diff --git a/libdevcore/StructuredLogger.h b/libdevcore/StructuredLogger.h index 8d807d9d4..de838da6f 100644 --- a/libdevcore/StructuredLogger.h +++ b/libdevcore/StructuredLogger.h @@ -35,6 +35,7 @@ namespace dev class StructuredLogger { public: + StructuredLogger(): m_enabled(false) {} StructuredLogger(bool _enabled): m_enabled(_enabled) {} void logStarting(std::string const& _clientImpl, const char* _ethVersion); diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 0ac9445d7..6cce7b51b 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -25,7 +25,6 @@ #include #include #include -#include #include #include #include @@ -153,7 +152,7 @@ inline string toString(h256s const& _bs) return out.str(); } -h256s BlockChain::sync(BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max, StructuredLogger const* _logger) +h256s BlockChain::sync(BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max, StructuredLogger const& _logger) { _bq.tick(*this); @@ -188,7 +187,7 @@ h256s BlockChain::sync(BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max return ret; } -h256s BlockChain::attemptImport(bytes const& _block, OverlayDB const& _stateDB, StructuredLogger const* _logger) noexcept +h256s BlockChain::attemptImport(bytes const& _block, OverlayDB const& _stateDB, StructuredLogger const& _logger) noexcept { try { @@ -201,7 +200,7 @@ h256s BlockChain::attemptImport(bytes const& _block, OverlayDB const& _stateDB, } } -h256s BlockChain::import(bytes const& _block, OverlayDB const& _db, StructuredLogger const* _logger) +h256s BlockChain::import(bytes const& _block, OverlayDB const& _db, StructuredLogger const& _logger) { // VERIFY: populates from the block and checks the block is internally coherent. BlockInfo bi; @@ -318,13 +317,12 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db, StructuredLo } #endif - if (_logger) - _logger->logChainReceivedNewBlock( - bi.headerHash(WithoutNonce).abridged(), - bi.nonce.abridged(), - currentHash().abridged(), - "", // TODO: remote id ?? - bi.parentHash.abridged()); + _logger.logChainReceivedNewBlock( + bi.headerHash(WithoutNonce).abridged(), + bi.nonce.abridged(), + currentHash().abridged(), + "", // TODO: remote id ?? + bi.parentHash.abridged()); // cnote << "Parent " << bi.parentHash << " has " << details(bi.parentHash).children.size() << " children."; h256s ret; @@ -339,13 +337,12 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db, StructuredLo } m_extrasDB->Put(m_writeOptions, ldb::Slice("best"), ldb::Slice((char const*)&newHash, 32)); clog(BlockChainNote) << " Imported and best" << td << ". Has" << (details(bi.parentHash).children.size() - 1) << "siblings. Route:" << toString(ret); - if (_logger) - _logger->logChainNewHead( - bi.headerHash(WithoutNonce).abridged(), - bi.nonce.abridged(), - currentHash().abridged(), - bi.parentHash.abridged() - ); + _logger.logChainNewHead( + bi.headerHash(WithoutNonce).abridged(), + bi.nonce.abridged(), + currentHash().abridged(), + bi.parentHash.abridged() + ); } else { diff --git a/libethereum/BlockChain.h b/libethereum/BlockChain.h index dc0cd76b2..ffdbccee4 100644 --- a/libethereum/BlockChain.h +++ b/libethereum/BlockChain.h @@ -32,6 +32,7 @@ #include #include #include +#include #include "BlockDetails.h" #include "Account.h" #include "BlockQueue.h" @@ -40,7 +41,6 @@ namespace ldb = leveldb; namespace dev { -class StructuredLogger; class OverlayDB; namespace eth @@ -80,15 +80,15 @@ public: void process(); /// Sync the chain with any incoming blocks. All blocks should, if processed in order - h256s sync(BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max, StructuredLogger const* _logger); + h256s sync(BlockQueue& _bq, OverlayDB const& _stateDB, unsigned _max, StructuredLogger const& _logger); /// Attempt to import the given block directly into the CanonBlockChain and sync with the state DB. /// @returns the block hashes of any blocks that came into/went out of the canonical block chain. - h256s attemptImport(bytes const& _block, OverlayDB const& _stateDB, StructuredLogger const* _logger = nullptr) noexcept; + h256s attemptImport(bytes const& _block, OverlayDB const& _stateDB, StructuredLogger const& _logger = StructuredLogger()) noexcept; /// Import block into disk-backed DB /// @returns the block hashes of any blocks that came into/went out of the canonical block chain. - h256s import(bytes const& _block, OverlayDB const& _stateDB, StructuredLogger const* _logger = nullptr); + h256s import(bytes const& _block, OverlayDB const& _stateDB, StructuredLogger const& _logger = StructuredLogger()); /// Returns true if the given block is known (though not necessarily a part of the canon chain). bool isKnown(h256 _hash) const; diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 6c9c1c44e..a4e9c9a66 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -25,7 +25,6 @@ #include #include #include -#include #include #include "Defaults.h" #include "Executive.h" @@ -61,7 +60,7 @@ void VersionChecker::setOk() } Client::Client(p2p::Host* _extNet, std::string const& _dbPath, bool _forceClean, - u256 _networkId, int _miners, StructuredLogger const* _structuredLogger): + u256 _networkId, int _miners, StructuredLogger const& _structuredLogger): Worker("eth"), m_vc(_dbPath), m_bc(_dbPath, !m_vc.ok() || _forceClean), @@ -70,7 +69,7 @@ Client::Client(p2p::Host* _extNet, std::string const& _dbPath, bool _forceClean, m_postMine(Address(), m_stateDB), m_structuredLogger(_structuredLogger) { - m_host = _extNet->registerCapability(new EthereumHost(m_bc, m_tq, m_bq, _networkId, _structuredLogger)); + m_host = _extNet->registerCapability(new EthereumHost(m_bc, m_tq, m_bq, _networkId)); if (_miners > -1) setMiningThreads(_miners); @@ -418,8 +417,7 @@ void Client::transact(Secret _secret, u256 _value, Address _dest, bytes const& _ } Transaction t(_value, _gasPrice, _gas, _dest, _data, n, _secret); // cdebug << "Nonce at " << toAddress(_secret) << " pre:" << m_preMine.transactionsFrom(toAddress(_secret)) << " post:" << m_postMine.transactionsFrom(toAddress(_secret)); - if (m_structuredLogger) - m_structuredLogger->logTransactionReceived(t.sha3().abridged(), t.sender().abridged()); + m_structuredLogger.logTransactionReceived(t.sha3().abridged(), t.sender().abridged()); cnote << "New transaction " << t; m_tq.attemptImport(t.rlp()); } diff --git a/libethereum/Client.h b/libethereum/Client.h index 644a6ed6c..33b0080f2 100644 --- a/libethereum/Client.h +++ b/libethereum/Client.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include "CanonBlockChain.h" @@ -45,8 +46,6 @@ namespace dev { -class StructuredLogger; - namespace eth { @@ -170,7 +169,7 @@ class Client: public MinerHost, public Interface, Worker public: /// New-style Constructor. explicit Client(p2p::Host* _host, std::string const& _dbPath = std::string(), bool _forceClean = false, - u256 _networkId = 0, int _miners = -1, StructuredLogger const* _structuredLogger = nullptr); + u256 _networkId = 0, int _miners = -1, StructuredLogger const& _structuredLogger = StructuredLogger()); /// Destructor. virtual ~Client(); @@ -368,7 +367,7 @@ private: std::map m_filters; std::map m_watches; - StructuredLogger const* m_structuredLogger; + StructuredLogger const& m_structuredLogger; mutable std::chrono::system_clock::time_point m_lastGarbageCollection; }; diff --git a/libethereum/EthereumHost.cpp b/libethereum/EthereumHost.cpp index 4d605a209..7dfc51b47 100644 --- a/libethereum/EthereumHost.cpp +++ b/libethereum/EthereumHost.cpp @@ -38,15 +38,13 @@ using namespace dev; using namespace dev::eth; using namespace p2p; -EthereumHost::EthereumHost(BlockChain const& _ch, TransactionQueue& _tq, BlockQueue& _bq, - u256 _networkId, bool _structuredLogging): +EthereumHost::EthereumHost(BlockChain const& _ch, TransactionQueue& _tq, BlockQueue& _bq, u256 _networkId): HostCapability(), Worker ("ethsync"), m_chain (_ch), m_tq (_tq), m_bq (_bq), - m_networkId (_networkId), - m_structuredLogging (_structuredLogging) + m_networkId (_networkId) { m_latestBlockSent = _ch.currentHash(); } diff --git a/libethereum/EthereumHost.h b/libethereum/EthereumHost.h index c9f92d1e0..dfa928675 100644 --- a/libethereum/EthereumHost.h +++ b/libethereum/EthereumHost.h @@ -59,7 +59,7 @@ class EthereumHost: public p2p::HostCapability, Worker public: /// Start server, but don't listen. - EthereumHost(BlockChain const& _ch, TransactionQueue& _tq, BlockQueue& _bq, u256 _networkId, bool _structuredLogging); + EthereumHost(BlockChain const& _ch, TransactionQueue& _tq, BlockQueue& _bq, u256 _networkId); /// Will block on network process events. virtual ~EthereumHost(); @@ -111,7 +111,6 @@ private: u256 m_networkId; EthereumPeer* m_syncer = nullptr; // TODO: switch to weak_ptr - bool m_structuredLogging; DownloadMan m_man; diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 97d785dc6..9c73f0173 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include "Session.h" @@ -46,7 +45,7 @@ void HostNodeTableHandler::processEvent(NodeId const& _n, NodeTableEventType con m_host.onNodeTableEvent(_n, _e); } -Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bytesConstRef _restoreNetwork, StructuredLogger const* _structuredLogger): +Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bytesConstRef _restoreNetwork, StructuredLogger const& _structuredLogger): Worker("p2p", 0), m_restoreNetwork(_restoreNetwork.toBytes()), m_clientVersion(_clientVersion), @@ -477,9 +476,8 @@ void Host::connect(std::shared_ptr const& _p) _p->m_lastConnected = std::chrono::system_clock::now(); _p->m_failedAttempts = 0; - if (m_structuredLogger) - m_structuredLogger->logP2PConnected(_p->id.abridged(), _p->peerEndpoint(), _p->m_lastConnected, - 0);// TODO: num_connections + m_structuredLogger.logP2PConnected(_p->id.abridged(), _p->peerEndpoint(), _p->m_lastConnected, + 0);// TODO: num_connections auto ps = make_shared(this, std::move(*s), _p); ps->start(); diff --git a/libp2p/Host.h b/libp2p/Host.h index b93a64556..de984ca59 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include "NodeTable.h" #include "HostCapability.h" @@ -45,8 +46,6 @@ namespace bi = ba::ip; namespace dev { -class StructuredLogger; - namespace p2p { @@ -88,7 +87,7 @@ class Host: public Worker public: /// Start server, listening for connections on the given port. Host(std::string const& _clientVersion, NetworkPreferences const& _n = NetworkPreferences(), - bytesConstRef _restoreNetwork = bytesConstRef(), StructuredLogger const* _structuredLogger = nullptr); + bytesConstRef _restoreNetwork = bytesConstRef(), StructuredLogger const& _structuredLogger = StructuredLogger()); /// Will block on network process events. virtual ~Host(); @@ -235,7 +234,7 @@ private: std::map> m_capabilities; ///< Each of the capabilities we support. std::chrono::steady_clock::time_point m_lastPing; ///< Time we sent the last ping to all peers. - StructuredLogger const* m_structuredLogger; + StructuredLogger const& m_structuredLogger; bool m_accepting = false; }; diff --git a/libwebthree/WebThree.cpp b/libwebthree/WebThree.cpp index 1025aad55..50e988932 100644 --- a/libwebthree/WebThree.cpp +++ b/libwebthree/WebThree.cpp @@ -37,7 +37,7 @@ using namespace dev::shh; WebThreeDirect::WebThreeDirect(std::string const& _clientVersion, std::string const& _dbPath, bool _forceClean, std::set const& _interfaces, NetworkPreferences const& _n, - bytesConstRef _network, int _miners, StructuredLogger const* _structuredLogger): + bytesConstRef _network, int _miners, StructuredLogger const& _structuredLogger): m_clientVersion(_clientVersion), m_net(_clientVersion, _n, _network, _structuredLogger) { diff --git a/libwebthree/WebThree.h b/libwebthree/WebThree.h index 840e91b2b..197181335 100644 --- a/libwebthree/WebThree.h +++ b/libwebthree/WebThree.h @@ -106,7 +106,7 @@ public: WebThreeDirect(std::string const& _clientVersion, std::string const& _dbPath, bool _forceClean = false, std::set const& _interfaces = {"eth", "shh"}, p2p::NetworkPreferences const& _n = p2p::NetworkPreferences(), - bytesConstRef _network = bytesConstRef(), int _miners = -1, StructuredLogger const* _structuredLogger = nullptr); + bytesConstRef _network = bytesConstRef(), int _miners = -1, StructuredLogger const& _structuredLogger = StructuredLogger()); /// Destructor. ~WebThreeDirect();