Browse Source

Synchronisation safety for details/blocks in BlockChain. Avoid situation

where block isKnown before details available.
cl-refactor
Gav Wood 10 years ago
parent
commit
a02aab8c34
  1. 41
      libethereum/BlockChain.cpp

41
libethereum/BlockChain.cpp

@ -485,14 +485,15 @@ ImportRoute BlockChain::import(bytes const& _block, OverlayDB const& _db, Import
#endif #endif
{ {
ReadGuard l1(x_blocks);
ReadGuard l2(x_details); ReadGuard l2(x_details);
ReadGuard l4(x_receipts); ReadGuard l4(x_receipts);
ReadGuard l5(x_logBlooms); ReadGuard l5(x_logBlooms);
m_blocksDB->Put(m_writeOptions, toSlice(bi.hash()), (ldb::Slice)ref(_block));
m_extrasDB->Put(m_writeOptions, toSlice(bi.hash(), ExtraDetails), (ldb::Slice)dev::ref(m_details[bi.hash()].rlp())); m_extrasDB->Put(m_writeOptions, toSlice(bi.hash(), ExtraDetails), (ldb::Slice)dev::ref(m_details[bi.hash()].rlp()));
m_extrasDB->Put(m_writeOptions, toSlice(bi.parentHash, ExtraDetails), (ldb::Slice)dev::ref(m_details[bi.parentHash].rlp())); m_extrasDB->Put(m_writeOptions, toSlice(bi.parentHash, ExtraDetails), (ldb::Slice)dev::ref(m_details[bi.parentHash].rlp()));
m_extrasDB->Put(m_writeOptions, toSlice(bi.hash(), ExtraLogBlooms), (ldb::Slice)dev::ref(m_logBlooms[bi.hash()].rlp())); m_extrasDB->Put(m_writeOptions, toSlice(bi.hash(), ExtraLogBlooms), (ldb::Slice)dev::ref(m_logBlooms[bi.hash()].rlp()));
m_extrasDB->Put(m_writeOptions, toSlice(bi.hash(), ExtraReceipts), (ldb::Slice)dev::ref(m_receipts[bi.hash()].rlp())); m_extrasDB->Put(m_writeOptions, toSlice(bi.hash(), ExtraReceipts), (ldb::Slice)dev::ref(m_receipts[bi.hash()].rlp()));
m_blocksDB->Put(m_writeOptions, toSlice(bi.hash()), (ldb::Slice)ref(_block));
} }
#if ETH_TIMED_IMPORTS #if ETH_TIMED_IMPORTS
@ -968,25 +969,23 @@ bool BlockChain::isKnown(h256 const& _hash) const
if (_hash == m_genesisHash) if (_hash == m_genesisHash)
return true; return true;
BlockInfo bi; ETH_READ_GUARDED(x_blocks)
if (!m_blocks.count(_hash))
{ {
ReadGuard l(x_blocks); string d;
auto it = m_blocks.find(_hash); m_blocksDB->Get(m_readOptions, toSlice(_hash), &d);
if (it != m_blocks.end()) if (d.empty())
bi = BlockInfo(it->second, CheckNothing, _hash); return false;
} }
ETH_READ_GUARDED(x_details)
if (!bi) if (!m_details.count(_hash))
{ {
string d; string d;
m_blocksDB->Get(m_readOptions, toSlice(_hash), &d); m_extrasDB->Get(m_readOptions, toSlice(_hash, ExtraDetails), &d);
if (!d.size()) if (d.empty())
return false; return false;
bi = BlockInfo(bytesConstRef(&d), CheckNothing, _hash); }
} return true;
return bi.number <= m_lastBlockNumber; // TODO: m_lastBlockNumber
} }
bytes BlockChain::block(h256 const& _hash) const bytes BlockChain::block(h256 const& _hash) const
@ -1004,7 +1003,7 @@ bytes BlockChain::block(h256 const& _hash) const
string d; string d;
m_blocksDB->Get(m_readOptions, toSlice(_hash), &d); m_blocksDB->Get(m_readOptions, toSlice(_hash), &d);
if (!d.size()) if (d.empty())
{ {
cwarn << "Couldn't find requested block:" << _hash.abridged(); cwarn << "Couldn't find requested block:" << _hash.abridged();
return bytes(); return bytes();

Loading…
Cancel
Save