From d7465f7b5521cea7aa0a410b6312f71eba015b40 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 7 Oct 2014 16:01:38 +0200 Subject: [PATCH] Syncing fixes. --- libethereum/EthereumHost.cpp | 16 ++++++++-------- libethereum/EthereumHost.h | 6 +++--- libethereum/EthereumPeer.cpp | 19 +++++++++++++------ libethereum/EthereumPeer.h | 3 --- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/libethereum/EthereumHost.cpp b/libethereum/EthereumHost.cpp index 3cdc442bf..4bd34f201 100644 --- a/libethereum/EthereumHost.cpp +++ b/libethereum/EthereumHost.cpp @@ -55,7 +55,7 @@ EthereumHost::~EthereumHost() i->cap()->abortSync(); } -bool EthereumHost::ensureInitialised(TransactionQueue& _tq) +bool EthereumHost::ensureInitialised() { if (!m_latestBlockSent) { @@ -63,7 +63,7 @@ bool EthereumHost::ensureInitialised(TransactionQueue& _tq) m_latestBlockSent = m_chain.currentHash(); clog(NetNote) << "Initialising: latest=" << m_latestBlockSent.abridged(); - for (auto const& i: _tq.transactions()) + for (auto const& i: m_tq.transactions()) m_transactionsSent.insert(i.first); return true; } @@ -144,16 +144,16 @@ void EthereumHost::reset() void EthereumHost::doWork() { - bool netChange = ensureInitialised(m_tq); + bool netChange = ensureInitialised(); auto h = m_chain.currentHash(); - maintainTransactions(m_tq, h); - maintainBlocks(m_bq, h); + maintainTransactions(h); + maintainBlocks(h); // return netChange; // TODO: Figure out what to do with netChange. (void)netChange; } -void EthereumHost::maintainTransactions(TransactionQueue& _tq, h256 _currentHash) +void EthereumHost::maintainTransactions(h256 _currentHash) { bool resendAll = (!isSyncing() && m_chain.isKnown(m_latestBlockSent) && _currentHash != m_latestBlockSent); @@ -163,7 +163,7 @@ void EthereumHost::maintainTransactions(TransactionQueue& _tq, h256 _currentHash { bytes b; unsigned n = 0; - for (auto const& i: _tq.transactions()) + for (auto const& i: m_tq.transactions()) if ((!m_transactionsSent.count(i.first) && !ep->m_knownTransactions.count(i.first)) || ep->m_requireTransactions || resendAll) { b += i.second; @@ -185,7 +185,7 @@ void EthereumHost::maintainTransactions(TransactionQueue& _tq, h256 _currentHash } } -void EthereumHost::maintainBlocks(BlockQueue& _bq, h256 _currentHash) +void EthereumHost::maintainBlocks(h256 _currentHash) { // If we've finished our initial sync send any new blocks. if (!isSyncing() && m_chain.isKnown(m_latestBlockSent) && m_chain.details(m_latestBlockSent).totalDifficulty < m_chain.details(_currentHash).totalDifficulty) diff --git a/libethereum/EthereumHost.h b/libethereum/EthereumHost.h index 92e2c8c32..2ce5b9971 100644 --- a/libethereum/EthereumHost.h +++ b/libethereum/EthereumHost.h @@ -84,8 +84,8 @@ private: /// Sync with the BlockChain. It might contain one of our mined blocks, we might have new candidates from the network. void doWork(); - void maintainTransactions(TransactionQueue& _tq, h256 _currentBlock); - void maintainBlocks(BlockQueue& _bq, h256 _currentBlock); + void maintainTransactions(h256 _currentBlock); + void maintainBlocks(h256 _currentBlock); /// Get a bunch of needed blocks. /// Removes them from our list of needed blocks. @@ -96,7 +96,7 @@ private: bool isInitialised() const { return m_latestBlockSent; } /// Initialises the network peer-state, doing the stuff that needs to be once-only. @returns true if it really was first. - bool ensureInitialised(TransactionQueue& _tq); + bool ensureInitialised(); virtual void onStarting() { startWorking(); } virtual void onStopping() { stopWorking(); } diff --git a/libethereum/EthereumPeer.cpp b/libethereum/EthereumPeer.cpp index 175c0bbae..c80912c30 100644 --- a/libethereum/EthereumPeer.cpp +++ b/libethereum/EthereumPeer.cpp @@ -59,10 +59,6 @@ EthereumHost* EthereumPeer::host() const return static_cast(Capability::hostCapability()); } -void EthereumPeer::sendStatus() -{ -} - /* * Possible asking/syncing states for two peers: */ @@ -132,17 +128,21 @@ void EthereumPeer::transition(Asking _a, bool _force) { if (m_asking == Asking::Hashes) { + if (!isSyncing()) + clogS(NetWarn) << "Bad state: asking for Hashes yet not syncing!"; if (shouldGrabBlocks()) { clog(NetNote) << "Difficulty of hashchain HIGHER. Grabbing" << m_syncingNeededBlocks.size() << "blocks [latest now" << m_syncingLatestHash.abridged() << ", was" << host()->m_latestBlockSent.abridged() << "]"; + m_syncingLatestHash = h256(); + host()->m_man.resetToChain(m_syncingNeededBlocks); host()->m_latestBlockSent = m_syncingLatestHash; } else { clog(NetNote) << "Difficulty of hashchain not HIGHER. Ignoring."; - + m_latestHash = h256(); setAsking(Asking::Nothing, false); return; } @@ -169,7 +169,7 @@ void EthereumPeer::transition(Asking _a, bool _force) { if (m_asking == Asking::Blocks) { - clogS(NetNote) << "Finishing block fetch..."; + clogS(NetNote) << "Finishing blocks fetch..."; // a bit overkill given that the other nodes may yet have the needed blocks, but better to be safe than sorry. if (isSyncing()) @@ -205,6 +205,12 @@ void EthereumPeer::setAsking(Asking _a, bool _isSyncing) m_asking = _a; if (_isSyncing != (host()->m_syncer == this)) host()->updateSyncer(_isSyncing ? this : nullptr); + if (!_isSyncing) + { + m_syncingLatestHash = h256(); + m_syncingTotalDifficulty = 0; + m_syncingNeededBlocks.clear(); + } session()->addNote("ask", _a == Asking::Nothing ? "nothing" : _a == Asking::State ? "state" : _a == Asking::Hashes ? "hashes" : _a == Asking::Blocks ? "blocks" : "?"); session()->addNote("sync", string(isSyncing() ? "ongoing" : "holding") + (needsSyncing() ? " & needed" : "")); @@ -263,6 +269,7 @@ void EthereumPeer::attemptSync() if (td >= m_totalDifficulty) { clogS(NetAllDetail) << "No. Our chain is better."; + m_latestHash = h256(); transition(Asking::Nothing); } else diff --git a/libethereum/EthereumPeer.h b/libethereum/EthereumPeer.h index a736516c2..3a625f369 100644 --- a/libethereum/EthereumPeer.h +++ b/libethereum/EthereumPeer.h @@ -65,9 +65,6 @@ private: /// Interpret an incoming message. virtual bool interpret(RLP const& _r); - /// Send our status to peer. - void sendStatus(); - /// Transition state in a particular direction. void transition(Asking _wantState, bool _force = false);