From d489ef3357d76c097aa8c736bdc8a67fac79c4c1 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 8 Oct 2014 11:46:12 +0200 Subject: [PATCH] Actually use NewBlock --- libethereum/DownloadMan.cpp | 4 ++- libethereum/DownloadMan.h | 4 +-- libethereum/EthereumHost.cpp | 2 +- libethereum/EthereumPeer.cpp | 62 +++++++++++++++++++----------------- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/libethereum/DownloadMan.cpp b/libethereum/DownloadMan.cpp index 3c8dae58e..e2c457404 100644 --- a/libethereum/DownloadMan.cpp +++ b/libethereum/DownloadMan.cpp @@ -66,10 +66,12 @@ h256Set DownloadSub::nextFetch(unsigned _n) return m_remaining; } -void DownloadSub::noteBlock(h256 _hash) +bool DownloadSub::noteBlock(h256 _hash) { Guard l(m_fetch); if (m_man && m_indices.count(_hash)) m_man->m_blocksGot += m_indices[_hash]; + bool ret = m_remaining.count(_hash); m_remaining.erase(_hash); + return ret; } diff --git a/libethereum/DownloadMan.h b/libethereum/DownloadMan.h index 6f4c4bafb..1902f3db1 100644 --- a/libethereum/DownloadMan.h +++ b/libethereum/DownloadMan.h @@ -48,8 +48,8 @@ public: /// Finished last fetch - grab the next bunch of block hashes to download. h256Set nextFetch(unsigned _n); - /// Note that we've received a particular block. - void noteBlock(h256 _hash); + /// Note that we've received a particular block. @returns true if we had asked for it but haven't received it yet. + bool noteBlock(h256 _hash); /// Nothing doing here. void doneFetch() { resetFetch(); } diff --git a/libethereum/EthereumHost.cpp b/libethereum/EthereumHost.cpp index bc5f11936..8e92fa9e6 100644 --- a/libethereum/EthereumHost.cpp +++ b/libethereum/EthereumHost.cpp @@ -202,7 +202,7 @@ void EthereumHost::maintainBlocks(h256 _currentHash) auto p = j->cap(); RLPStream ts; - p->prep(ts, BlocksPacket, hs.size()).appendRaw(bs, hs.size()); + p->prep(ts, NewBlockPacket, hs.size()).appendRaw(bs, hs.size()); Guard l(p->x_knownBlocks); if (!p->m_knownBlocks.count(_currentHash)) diff --git a/libethereum/EthereumPeer.cpp b/libethereum/EthereumPeer.cpp index 21fe5fbba..610050ad0 100644 --- a/libethereum/EthereumPeer.cpp +++ b/libethereum/EthereumPeer.cpp @@ -425,44 +425,46 @@ bool EthereumPeer::interpret(unsigned _id, RLP const& _r) unsigned future = 0; unsigned unknown = 0; unsigned got = 0; + unsigned repeated = 0; for (unsigned i = 1; i < _r.itemCount(); ++i) { auto h = BlockInfo::headerHash(_r[i].data()); - m_sub.noteBlock(h); - + if (m_sub.noteBlock(h)) { - Guard l(x_knownBlocks); - m_knownBlocks.insert(h); + addRating(10); + switch (host()->m_bq.import(_r[i].data(), host()->m_chain)) + { + case ImportResult::Success: + success++; + break; + + case ImportResult::Malformed: + disable("Malformed block received."); + return true; + + case ImportResult::FutureTime: + future++; + break; + + case ImportResult::AlreadyInChain: + case ImportResult::AlreadyKnown: + got++; + break; + + case ImportResult::UnknownParent: + unknown++; + break; + } } - - switch (host()->m_bq.import(_r[i].data(), host()->m_chain)) + else { - case ImportResult::Success: - addRating(1); - success++; - break; - - case ImportResult::Malformed: - disable("Malformed block received."); - return true; - - case ImportResult::FutureTime: - future++; - break; - - case ImportResult::AlreadyInChain: - case ImportResult::AlreadyKnown: - got++; - break; - - case ImportResult::UnknownParent: - unknown++; - break; + addRating(0); // -1? + repeated++; } } - clogS(NetMessageSummary) << dec << success << "imported OK," << unknown << "with unknown parents," << future << "with future timestamps," << got << " already known."; + clogS(NetMessageSummary) << dec << success << "imported OK," << unknown << "with unknown parents," << future << "with future timestamps," << got << " already known," << repeated << " repeats received."; if (m_asking == Asking::Blocks) transition(Asking::Blocks); @@ -480,8 +482,10 @@ bool EthereumPeer::interpret(unsigned _id, RLP const& _r) switch (host()->m_bq.import(_r[1].data(), host()->m_chain)) { case ImportResult::Success: + addRating(100); + break; case ImportResult::FutureTime: - addRating(1); + //TODO: Rating dependent on how far in future it is. break; case ImportResult::Malformed: