From a8a8fa5ced19a7f0f6f138e9af7508016c448500 Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 8 Jul 2015 19:35:04 +0200 Subject: [PATCH] start pv60 sync after 10sec delay --- libethereum/BlockChainSync.cpp | 4 +++- libethereum/BlockChainSync.h | 6 +++--- libethereum/EthereumHost.cpp | 25 ++++++++++++++++++++++++- libethereum/EthereumHost.h | 1 + 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/libethereum/BlockChainSync.cpp b/libethereum/BlockChainSync.cpp index b3b168cb7..5249c41e0 100644 --- a/libethereum/BlockChainSync.cpp +++ b/libethereum/BlockChainSync.cpp @@ -298,10 +298,12 @@ void BlockChainSync::onPeerNewBlock(std::shared_ptr _peer, RLP con case ImportResult::UnknownParent: { logNewBlock(h); - clog(NetMessageDetail) << "Received block with no known parent. Resyncing..."; u256 totalDifficulty = _r[1].toInt(); if (totalDifficulty > _peer->m_totalDifficulty) + { + clog(NetMessageDetail) << "Received block with no known parent. Resyncing..."; resetSyncFor(_peer, h, totalDifficulty); + } break; } default:; diff --git a/libethereum/BlockChainSync.h b/libethereum/BlockChainSync.h index 4fb7bae2c..6be17bc74 100644 --- a/libethereum/BlockChainSync.h +++ b/libethereum/BlockChainSync.h @@ -58,6 +58,9 @@ public: /// @returns true is Sync is in progress virtual bool isSyncing() const = 0; + /// Restart sync + virtual void restartSync() = 0; + /// Called by peer to report status virtual void onPeerStatus(std::shared_ptr _peer); @@ -92,9 +95,6 @@ protected: /// Resume downloading after witing state virtual void continueSync() = 0; - /// Restart sync - virtual void restartSync() = 0; - /// Called after all blocks have been donloaded virtual void completeSync() = 0; diff --git a/libethereum/EthereumHost.cpp b/libethereum/EthereumHost.cpp index f00ec7dc7..1c9a491a8 100644 --- a/libethereum/EthereumHost.cpp +++ b/libethereum/EthereumHost.cpp @@ -88,6 +88,7 @@ void EthereumHost::reset() if (m_sync) m_sync->abortSync(); m_sync.reset(); + m_syncStart = 0; m_latestBlockSent = h256(); Guard tl(x_transactions); @@ -115,6 +116,21 @@ void EthereumHost::doWork() foreachPeer([](std::shared_ptr _p) { _p->tick(); return true; }); + if (m_syncStart) + { + Guard l(x_sync); + if (!m_sync) + { + time_t now = std::chrono::system_clock::to_time_t(chrono::system_clock::now()); + if ((now - m_syncStart) > 10) + { + m_sync.reset(new PV60Sync(*this)); + m_syncStart = 0; + m_sync->restartSync(); + } + } + } + // return netChange; // TODO: Figure out what to do with netChange. (void)netChange; @@ -259,7 +275,14 @@ BlockChainSync* EthereumHost::sync() pv61 = true; return !pv61; }); - m_sync.reset(pv61 ? new PV61Sync(*this) : new PV60Sync(*this)); + if (pv61) + { + m_syncStart = 0; + m_sync.reset(new PV61Sync(*this)); + } + else if (!m_syncStart) + m_syncStart = std::chrono::system_clock::to_time_t(chrono::system_clock::now()); + return m_sync.get(); } diff --git a/libethereum/EthereumHost.h b/libethereum/EthereumHost.h index 6cb82ebb4..9afaa8413 100644 --- a/libethereum/EthereumHost.h +++ b/libethereum/EthereumHost.h @@ -138,6 +138,7 @@ private: mutable Mutex x_transactions; DownloadMan m_man; std::unique_ptr m_sync; + std::atomic m_syncStart = { 0 }; }; }