diff --git a/libethereum/BlockChainSync.cpp b/libethereum/BlockChainSync.cpp index b3b168cb7..6aede0c16 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:; @@ -1168,9 +1170,9 @@ SyncStatus PV61Sync::status() const { RecursiveGuard l(x_sync); SyncStatus res = PV60Sync::status(); + res.protocolVersion = 61; if (m_state == SyncState::Hashes && isPV61Syncing()) { - res.protocolVersion = 61; res.hashesReceived = 0; for (auto const& d : m_readyChainMap) res.hashesReceived += d.second.size(); 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..88f95f64f 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) + { + DEV_GUARDED(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 }; }; }