Browse Source

Blockchain syncing fixed.

cl-refactor
Gav Wood 10 years ago
parent
commit
ffe228b385
  1. 21
      libethereum/BlockChain.cpp
  2. 14
      libethereum/EthereumHost.cpp
  3. 12
      libethereum/EthereumSession.cpp

21
libethereum/BlockChain.cpp

@ -333,16 +333,23 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db)
h256s BlockChain::treeRoute(h256 _from, h256 _to, h256* o_common, bool _pre, bool _post) const h256s BlockChain::treeRoute(h256 _from, h256 _to, h256* o_common, bool _pre, bool _post) const
{ {
cdebug << "treeRoute" << _from.abridged() << "..." << _to.abridged();
if (!_from || !_to)
{
return h256s();
}
h256s ret; h256s ret;
h256s back; h256s back;
unsigned fn = details(_from).number; unsigned fn = details(_from).number;
unsigned tn = details(_to).number; unsigned tn = details(_to).number;
cdebug << "treeRoute" << fn << "..." << tn;
while (fn > tn) while (fn > tn)
{ {
if (_pre) if (_pre)
ret.push_back(_from); ret.push_back(_from);
_from = details(_from).parent; _from = details(_from).parent;
fn--; fn--;
cdebug << "from:" << fn << _from.abridged();
} }
while (fn < tn) while (fn < tn)
{ {
@ -350,15 +357,21 @@ h256s BlockChain::treeRoute(h256 _from, h256 _to, h256* o_common, bool _pre, boo
back.push_back(_to); back.push_back(_to);
_to = details(_to).parent; _to = details(_to).parent;
tn--; tn--;
cdebug << "to:" << tn << _to.abridged();
} }
while (_from != _to) while (_from != _to)
{ {
assert(_from);
assert(_to);
_from = details(_from).parent;
_to = details(_to).parent;
if (_pre) if (_pre)
_from = details(_from).parent; ret.push_back(_from);
if (_post) if (_post)
_to = details(_to).parent; back.push_back(_to);
ret.push_back(_from); fn--;
back.push_back(_to); tn--;
cdebug << "from:" << fn << _from.abridged() << "; to:" << tn << _to.abridged();
} }
if (o_common) if (o_common)
*o_common = _from; *o_common = _from;

14
libethereum/EthereumHost.cpp

@ -571,15 +571,27 @@ void EthereumHost::noteHaveChain(std::shared_ptr<EthereumSession> const& _from)
{ {
auto td = _from->m_totalDifficulty; auto td = _from->m_totalDifficulty;
if (_from->m_neededBlocks.empty())
return;
clog(NetNote) << "Hash-chain COMPLETE:" << log2((double)_from->m_totalDifficulty) << "vs" << log2((double)m_chain->details().totalDifficulty) << "," << log2((double)m_totalDifficultyOfNeeded) << ";" << _from->m_neededBlocks.size() << " blocks, ends" << _from->m_neededBlocks.back().abridged();
if ((m_totalDifficultyOfNeeded && td < m_totalDifficultyOfNeeded) || td < m_chain->details().totalDifficulty) if ((m_totalDifficultyOfNeeded && td < m_totalDifficultyOfNeeded) || td < m_chain->details().totalDifficulty)
{
clog(NetNote) << "Difficulty of hashchain LOWER. Ignoring.";
return; return;
}
clog(NetNote) << "Difficulty of hashchain HIGHER. Replacing fetch queue.";
// Looks like it's the best yet for total difficulty. Set to download.
{ {
Guard l(x_blocksNeeded); Guard l(x_blocksNeeded);
m_blocksNeeded = _from->m_neededBlocks; m_blocksNeeded = _from->m_neededBlocks;
m_blocksOnWay.clear();
m_totalDifficultyOfNeeded = td;
} }
// Looks like it's the best yet for total difficulty. Set to download.
{ {
Guard l(x_peers); Guard l(x_peers);
for (auto const& i: m_peers) for (auto const& i: m_peers)

12
libethereum/EthereumSession.cpp

@ -56,8 +56,19 @@ EthereumSession::~EthereumSession()
catch (...){} catch (...){}
} }
string toString(h256s const& _bs)
{
ostringstream out;
out << "[ ";
for (auto i: _bs)
out << i.abridged() << ", ";
out << "]";
return out.str();
}
void EthereumSession::giveUpOnFetch() void EthereumSession::giveUpOnFetch()
{ {
clogS(NetNote) << "GIVE UP FETCH; can't get " << toString(m_askedBlocks);
if (m_askedBlocks.size()) if (m_askedBlocks.size())
{ {
Guard l (m_server->x_blocksNeeded); Guard l (m_server->x_blocksNeeded);
@ -329,6 +340,7 @@ bool EthereumSession::interpret(RLP const& _r)
} }
clogS(NetMessageSummary) << dec << knownParents << " known parents, " << unknownParents << "unknown, " << used << "used."; clogS(NetMessageSummary) << dec << knownParents << " known parents, " << unknownParents << "unknown, " << used << "used.";
ensureGettingChain(); ensureGettingChain();
break;
} }
case GetTransactionsPacket: case GetTransactionsPacket:
{ {

Loading…
Cancel
Save