Browse Source

Syncing fixes.

cl-refactor
Gav Wood 10 years ago
parent
commit
d7465f7b55
  1. 16
      libethereum/EthereumHost.cpp
  2. 6
      libethereum/EthereumHost.h
  3. 19
      libethereum/EthereumPeer.cpp
  4. 3
      libethereum/EthereumPeer.h

16
libethereum/EthereumHost.cpp

@ -55,7 +55,7 @@ EthereumHost::~EthereumHost()
i->cap<EthereumPeer>()->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)

6
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(); }

19
libethereum/EthereumPeer.cpp

@ -59,10 +59,6 @@ EthereumHost* EthereumPeer::host() const
return static_cast<EthereumHost*>(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

3
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);

Loading…
Cancel
Save