From 5ce45164bd5ee9fbbec3a2f9fe7cbce37cb6e886 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 20 Sep 2014 13:54:55 -0500 Subject: [PATCH] Get rid of nasty old code. --- alethzero/MainWin.cpp | 2 +- libethereum/CommonNet.h | 1 + libethereum/EthereumHost.cpp | 59 +++++++++--------------------------- libethereum/EthereumHost.h | 6 +--- libethereum/EthereumPeer.cpp | 45 ++++++--------------------- libethereum/EthereumPeer.h | 8 ----- libp2p/Host.cpp | 7 +++-- 7 files changed, 32 insertions(+), 96 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 6602cd4cf..9b9ae69e9 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -125,7 +125,7 @@ Main::Main(QWidget *parent) : connect(ui->ourAccounts->model(), SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)), SLOT(ourAccountsRowsMoved())); - m_webThree.reset(new WebThreeDirect("AlethZero", getDataDir() + "/AlethZero", false, {"eth", "shh"})); + m_webThree.reset(new WebThreeDirect(string("AlethZero/v") + dev::Version + "/" DEV_QUOTED(ETH_BUILD_TYPE) "/" DEV_QUOTED(ETH_BUILD_PLATFORM), getDataDir() + "/AlethZero", false, {"eth", "shh"})); connect(ui->webView, &QWebView::loadStarted, [this]() { diff --git a/libethereum/CommonNet.h b/libethereum/CommonNet.h index 6774c02ae..4fdfa77a0 100644 --- a/libethereum/CommonNet.h +++ b/libethereum/CommonNet.h @@ -66,6 +66,7 @@ enum class Grabbing State, Hashes, Chain, + ChainHelper, Nothing }; diff --git a/libethereum/EthereumHost.cpp b/libethereum/EthereumHost.cpp index 6a621eab6..a2bac994e 100644 --- a/libethereum/EthereumHost.cpp +++ b/libethereum/EthereumHost.cpp @@ -55,30 +55,6 @@ EthereumHost::~EthereumHost() i->cap()->giveUpOnFetch(); } -h256Set EthereumHost::neededBlocks(h256Set const& _exclude) -{ - Guard l(x_blocksNeeded); - h256Set ret; - if (m_blocksNeeded.size()) - { - int s = m_blocksNeeded.size() - 1; - for (; ret.size() < c_maxBlocksAsk && s < (int)m_blocksNeeded.size() && s >= 0; --s) - if (!_exclude.count(m_blocksNeeded[s])) - { - auto it = m_blocksNeeded.begin() + s; - ret.insert(*it); - m_blocksOnWay.insert(*it); - m_blocksNeeded.erase(it); - } - } - if (ret.empty()) - for (auto i = m_blocksOnWay.begin(); ret.size() < c_maxBlocksAsk && i != m_blocksOnWay.end() && !_exclude.count(*i); ++i) - ret.insert(*i); - if (ret.size()) - clog(NetMessageSummary) << "Asking for" << ret.size() << "blocks that we don't yet have." << m_blocksNeeded.size() << "blocks still needed," << m_blocksOnWay.size() << "total blocks on way."; - return ret; -} - bool EthereumHost::ensureInitialised(TransactionQueue& _tq) { if (!m_latestBlockSent) @@ -102,7 +78,7 @@ void EthereumHost::noteHavePeerState(EthereumPeer* _who) if (m_grabbing != Grabbing::Nothing) { clog(NetAllDetail) << "Already downloading chain. Just set to help out."; - _who->restartGettingChain(); + _who->ensureGettingChain(); return; } @@ -117,7 +93,7 @@ void EthereumHost::updateGrabbing(Grabbing _g) readyForSync(); else if (_g == Grabbing::Chain) for (auto j: peers()) - j->cap()->restartGettingChain(); + j->cap()->ensureGettingChain(); } void EthereumHost::noteHaveChain(EthereumPeer* _from) @@ -145,15 +121,8 @@ void EthereumHost::noteHaveChain(EthereumPeer* _from) // Looks like it's the best yet for total difficulty. Set to download. m_man.resetToChain(_from->m_neededBlocks); - { - Guard l(x_blocksNeeded); - m_blocksNeeded.clear(); - for (auto i = _from->m_neededBlocks.rbegin(); i != _from->m_neededBlocks.rend(); ++i) - m_blocksNeeded.push_back(*i); - m_blocksOnWay.clear(); - m_totalDifficultyOfNeeded = td; - m_latestBlockSent = _from->m_latestHash; - } + m_totalDifficultyOfNeeded = td; + m_latestBlockSent = _from->m_latestHash; _from->m_grabbing = Grabbing::Chain; updateGrabbing(Grabbing::Chain); @@ -174,23 +143,25 @@ void EthereumHost::readyForSync() clog(NetNote) << "No more peers to sync with."; } -void EthereumHost::noteDoneBlocks() +void EthereumHost::noteDoneBlocks(EthereumPeer* _who) { if (m_man.isComplete()) { // Done our chain-get. - if (m_blocksNeeded.size()) - clog(NetNote) << "No more blocks coming. Missing" << m_blocksNeeded.size() << "blocks."; - else - clog(NetNote) << "No more blocks to get."; + clog(NetNote) << "Chain download complete."; + updateGrabbing(Grabbing::Nothing); + } + if (_who->m_grabbing == Grabbing::Chain) + { + // Done our chain-get. + clog(NetNote) << "Chain download failed. Peer with blocks didn't have them all. This peer is bad and should be punished."; + // TODO: note that peer is BADBADBAD! updateGrabbing(Grabbing::Nothing); } } bool EthereumHost::noteBlock(h256 _hash, bytesConstRef _data) { - Guard l(x_blocksNeeded); - m_blocksOnWay.erase(_hash); if (!m_chain.details(_hash)) { lock_guard l(m_incomingLock); @@ -259,12 +230,12 @@ void EthereumHost::reset() { m_grabbing = Grabbing::Nothing; + m_man.resetToChain(h256s()); + m_incomingTransactions.clear(); m_incomingBlocks.clear(); m_totalDifficultyOfNeeded = 0; - m_blocksNeeded.clear(); - m_blocksOnWay.clear(); m_latestBlockSent = h256(); m_transactionsSent.clear(); diff --git a/libethereum/EthereumHost.h b/libethereum/EthereumHost.h index 08fbd5d14..b735532b8 100644 --- a/libethereum/EthereumHost.h +++ b/libethereum/EthereumHost.h @@ -77,7 +77,7 @@ private: /// Session has finished getting the chain of hashes. void noteHaveChain(EthereumPeer* _who); /// Called when the peer can no longer provide us with any needed blocks. - void noteDoneBlocks(); + void noteDoneBlocks(EthereumPeer* _who); /// Sync with the BlockChain. It might contain one of our mined blocks, we might have new candidates from the network. void doWork(); @@ -117,11 +117,7 @@ private: std::vector m_incomingTransactions; std::vector m_incomingBlocks; - mutable std::mutex x_blocksNeeded; u256 m_totalDifficultyOfNeeded; - h256s m_blocksNeeded; - - h256Set m_blocksOnWay; DownloadMan m_man; diff --git a/libethereum/EthereumPeer.cpp b/libethereum/EthereumPeer.cpp index 672c5aa13..6f9ff226e 100644 --- a/libethereum/EthereumPeer.cpp +++ b/libethereum/EthereumPeer.cpp @@ -114,29 +114,17 @@ void EthereumPeer::tryGrabbingHashChain() void EthereumPeer::giveUpOnFetch() { - clogS(NetNote) << "GIVE UP FETCH; can't get" << toString(m_askedBlocks); + clogS(NetNote) << "GIVE UP FETCH"; // a bit overkill given that the other nodes may yet have the needed blocks, but better to be safe than sorry. if (m_grabbing == Grabbing::Chain) { + host()->noteDoneBlocks(this); m_grabbing = Grabbing::Nothing; - host()->updateGrabbing(Grabbing::Nothing); } + m_sub.doneFetch(); // NOTE: need to notify of giving up on chain-hashes, too, altering state as necessary. - - if (m_askedBlocks.size()) - { - Guard l (host()->x_blocksNeeded); - host()->m_blocksNeeded.reserve(host()->m_blocksNeeded.size() + m_askedBlocks.size()); - for (auto i: m_askedBlocks) - { - m_failedBlocks.insert(i); - host()->m_blocksOnWay.erase(i); - host()->m_blocksNeeded.push_back(i); - } - m_askedBlocks.clear(); - } } bool EthereumPeer::interpret(RLP const& _r) @@ -253,13 +241,12 @@ bool EthereumPeer::interpret(RLP const& _r) { clogS(NetMessageSummary) << "Blocks (" << dec << (_r.itemCount() - 1) << "entries)" << (_r.itemCount() - 1 ? "" : ": NoMoreBlocks"); - if (_r.itemCount() == 1 && !m_askedBlocksChanged) + if (_r.itemCount() == 1) { // Couldn't get any from last batch - probably got to this peer's latest block - just give up. m_sub.doneFetch(); giveUpOnFetch(); } - m_askedBlocksChanged = false; unsigned used = 0; for (unsigned i = 1; i < _r.itemCount(); ++i) @@ -268,7 +255,6 @@ bool EthereumPeer::interpret(RLP const& _r) m_sub.noteBlock(h); if (host()->noteBlock(h, _r[i].data())) used++; - m_askedBlocks.erase(h); Guard l(x_knownBlocks); m_knownBlocks.insert(h); } @@ -304,21 +290,9 @@ bool EthereumPeer::interpret(RLP const& _r) return true; } -void EthereumPeer::restartGettingChain() -{ - if (m_askedBlocks.size()) - { - m_askedBlocksChanged = true; // So that we continue even if the Ask's reply is empty. - m_askedBlocks.clear(); // So that we restart once we get the Ask's reply. - m_failedBlocks.clear(); - } - else - ensureGettingChain(); -} - void EthereumPeer::ensureGettingChain() { - if (m_askedBlocks.size()) + if (m_grabbing == Grabbing::ChainHelper) return; // Already asked & waiting for some. continueGettingChain(); @@ -326,6 +300,9 @@ void EthereumPeer::ensureGettingChain() void EthereumPeer::continueGettingChain() { + if (m_grabbing != Grabbing::Chain) + m_grabbing = Grabbing::ChainHelper; + auto blocks = m_sub.nextFetch(c_maxBlocksAsk); if (blocks.size()) @@ -338,9 +315,5 @@ void EthereumPeer::continueGettingChain() sealAndSend(s); } else - { - if (m_failedBlocks.size()) - clogS(NetMessageSummary) << "No blocks left to get. Peer doesn't seem to have" << m_failedBlocks.size() << "of our needed blocks."; - host()->noteDoneBlocks(); - } + giveUpOnFetch(); } diff --git a/libethereum/EthereumPeer.h b/libethereum/EthereumPeer.h index 960f1f482..f88a757d8 100644 --- a/libethereum/EthereumPeer.h +++ b/libethereum/EthereumPeer.h @@ -67,8 +67,6 @@ private: void ensureGettingChain(); /// Ensure that we are waiting for a bunch of blocks from our peer. void continueGettingChain(); - /// Now getting a different chain so we need to make sure we restart. - void restartGettingChain(); void giveUpOnFetch(); @@ -82,12 +80,6 @@ private: h256 m_latestHash; ///< Peer's latest block's hash. u256 m_totalDifficulty; ///< Peer's latest block's total difficulty. h256s m_neededBlocks; ///< The blocks that we should download from this peer. - h256Set m_failedBlocks; ///< Blocks that the peer doesn't seem to have. - - h256Set m_askedBlocks; ///< The blocks for which we sent the last GetBlocks for but haven't received a corresponding Blocks. - bool m_askedBlocksChanged = true; - - RangeMask m_blocksAsked; bool m_requireTransactions; diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index a0a595bce..cf76397bf 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -456,8 +456,11 @@ void Host::growPeers() auto x = time(0) % m_freePeers.size(); m_incomingPeers[m_freePeers[x]].second++; - connect(m_incomingPeers[m_freePeers[x]].first); - m_freePeers.erase(m_freePeers.begin() + x); + if (!m_peers.count(m_freePeers[x])) + { + connect(m_incomingPeers[m_freePeers[x]].first); + m_freePeers.erase(m_freePeers.begin() + x); + } } }