From 2a18aa2d8e6cb3de1560a7e5dd30a37fba555c8a Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 28 Aug 2014 22:53:35 +0200 Subject: [PATCH] Avoid sending chain back to peer once initial sync complete. --- libethereum/EthereumHost.cpp | 24 +++++++++++++++++++++--- libethereum/EthereumHost.h | 2 ++ libethereum/EthereumSession.cpp | 3 +++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/libethereum/EthereumHost.cpp b/libethereum/EthereumHost.cpp index 176c7465c..2c2e327d5 100644 --- a/libethereum/EthereumHost.cpp +++ b/libethereum/EthereumHost.cpp @@ -421,6 +421,17 @@ bool EthereumHost::ensureInitialised(TransactionQueue& _tq) return false; } +void EthereumHost::noteDoneBlocks() +{ + clog(NetNote) << "Peer given up on blocks fetch."; + if (m_blocksOnWay.empty()) + { + // Done our chain-get. + clog(NetNote) << "No more blocks coming. Missing" << m_blocksNeeded.size() << "blocks."; + m_latestBlockSent = m_chain->currentHash(); + } +} + bool EthereumHost::noteBlock(h256 _hash, bytesConstRef _data) { Guard l(x_blocksNeeded); @@ -509,8 +520,14 @@ void EthereumHost::maintainBlocks(BlockQueue& _bq, h256 _currentHash) m_incomingBlocks.clear(); } - // Send any new blocks. - if (_currentHash != m_latestBlockSent) + // If we've finished our initial sync... + { + Guard l(x_blocksNeeded); + if (m_blocksOnWay.size()) + return; + } + // ...send any new blocks. + if (m_latestBlockSent != _currentHash) { RLPStream ts; EthereumSession::prep(ts); @@ -521,6 +538,7 @@ void EthereumHost::maintainBlocks(BlockQueue& _bq, h256 _currentHash) bs += m_chain->block(h); ++c; } + clog(NetNote) << "Sending" << c << "new blocks (current is" << _currentHash << ", was" << m_latestBlockSent << ")"; ts.appendList(1 + c).append(BlocksPacket).appendRaw(bs, c); bytes b; ts.swapOut(b); @@ -534,8 +552,8 @@ void EthereumHost::maintainBlocks(BlockQueue& _bq, h256 _currentHash) p->send(&b); p->m_knownBlocks.clear(); } + m_latestBlockSent = _currentHash; } - m_latestBlockSent = _currentHash; } void EthereumHost::growPeers() diff --git a/libethereum/EthereumHost.h b/libethereum/EthereumHost.h index 9487e40f6..9a9f83b1b 100644 --- a/libethereum/EthereumHost.h +++ b/libethereum/EthereumHost.h @@ -113,6 +113,8 @@ private: bool noteBlock(h256 _hash, bytesConstRef _data); /// Session has finished getting the chain of hashes. void noteHaveChain(std::shared_ptr const& _who); + /// Called when the peer can no longer provide us with any needed blocks. + void noteDoneBlocks(); /// Called when the session has provided us with a new peer we can connect to. void noteNewPeers() {} diff --git a/libethereum/EthereumSession.cpp b/libethereum/EthereumSession.cpp index db25441e5..5f9ee76fc 100644 --- a/libethereum/EthereumSession.cpp +++ b/libethereum/EthereumSession.cpp @@ -398,7 +398,10 @@ void EthereumSession::continueGettingChain() sealAndSend(s); } else + { clogS(NetMessageSummary) << "No blocks left to get. Peer doesn't seem to have " << m_failedBlocks.size() << "of our needed blocks."; + m_server->noteDoneBlocks(); + } } void EthereumSession::ping()