Browse Source

Diagnostics for tracking down source of apparently corrupt block

insertion.
cl-refactor
Gav Wood 10 years ago
parent
commit
d7fb14b95b
  1. 16
      libethereum/EthereumPeer.cpp
  2. 24
      libethereum/State.cpp

16
libethereum/EthereumPeer.cpp

@ -403,19 +403,25 @@ bool EthereumPeer::interpret(unsigned _id, RLP const& _r)
} }
case GetBlocksPacket: case GetBlocksPacket:
{ {
clog(NetMessageSummary) << "GetBlocks (" << dec << _r.itemCount() << "entries)"; unsigned count = _r.itemCount();
clog(NetMessageSummary) << "GetBlocks (" << dec << count << "entries)";
// return the requested blocks. // return the requested blocks.
bytes rlp; bytes rlp;
unsigned n = 0; unsigned n = 0;
for (unsigned i = 0; i < _r.itemCount() && i <= c_maxBlocks; ++i) for (unsigned i = 0; i < min(count, c_maxBlocks); ++i)
{ {
auto b = host()->m_chain.block(_r[i].toHash<h256>()); auto h = _r[i].toHash<h256>();
if (b.size()) if (host()->m_chain.isKnown(h))
{ {
rlp += b; rlp += host()->m_chain.block(_r[i].toHash<h256>());
++n; ++n;
} }
} }
if (count > 20 && n == 0)
clog(NetWarn) << "all" << count << "unknown blocks requested; peer on different chain?";
else
clog(NetMessageSummary) << n << "blocks known and returned;" << (min(count, c_maxBlocks) - n) << "blocks unknown;" << (c_maxBlocks > count ? c_maxBlocks - count : 0) << "blocks ignored";
addRating(0); addRating(0);
RLPStream s; RLPStream s;
prep(s, BlocksPacket, n).appendRaw(rlp, n); prep(s, BlocksPacket, n).appendRaw(rlp, n);

24
libethereum/State.cpp

@ -276,6 +276,7 @@ bool State::sync(BlockChain const& _bc)
bool State::sync(BlockChain const& _bc, h256 _block, BlockInfo const& _bi, ImportRequirements::value _ir) bool State::sync(BlockChain const& _bc, h256 _block, BlockInfo const& _bi, ImportRequirements::value _ir)
{ {
(void)_ir;
bool ret = false; bool ret = false;
// BLOCK // BLOCK
BlockInfo bi = _bi ? _bi : _bc.info(_block); BlockInfo bi = _bi ? _bi : _bc.info(_block);
@ -315,6 +316,24 @@ bool State::sync(BlockChain const& _bc, h256 _block, BlockInfo const& _bi, Impor
// No change since last sync. // No change since last sync.
// Carry on as we were. // Carry on as we were.
} }
else
{
// New blocks available, or we've switched to a different branch. All change.
// Find most recent state dump and replay what's left.
// (Most recent state dump might end up being genesis.)
if (m_db.lookup(bi.stateRoot).empty())
{
cwarn << "Unable to sync to" << bi.hash().abridged() << "; state root" << bi.stateRoot.abridged() << "not found in database.";
cwarn << "Database corrupt: contains block without stateRoot:" << bi;
cwarn << "Bailing.";
exit(-1);
}
m_previousBlock = bi;
resetCurrent();
ret = true;
}
#if ALLOW_REBUILD
else else
{ {
// New blocks available, or we've switched to a different branch. All change. // New blocks available, or we've switched to a different branch. All change.
@ -352,6 +371,7 @@ bool State::sync(BlockChain const& _bc, h256 _block, BlockInfo const& _bi, Impor
resetCurrent(); resetCurrent();
ret = true; ret = true;
} }
#endif
return ret; return ret;
} }
@ -692,9 +712,9 @@ void State::cleanup(bool _fullCommit)
paranoia("immediately before database commit", true); paranoia("immediately before database commit", true);
// Commit the new trie to disk. // Commit the new trie to disk.
cnote << "Commiting to disk..."; cnote << "Committing to disk: stateRoot" << m_currentBlock.stateRoot.abridged() << "=" << rootHash().abridged() << "=" << toHex(asBytes(m_db.lookup(rootHash())));
m_db.commit(); m_db.commit();
cnote << "Committed."; cnote << "Committed: stateRoot" << m_currentBlock.stateRoot.abridged() << "=" << rootHash().abridged() << "=" << toHex(asBytes(m_db.lookup(rootHash())));
paranoia("immediately after database commit", true); paranoia("immediately after database commit", true);
m_previousBlock = m_currentBlock; m_previousBlock = m_currentBlock;

Loading…
Cancel
Save