Browse Source

Fixes for reimporting, make block availability atomic.

cl-refactor
Gav Wood 10 years ago
parent
commit
8328220471
  1. 2
      alethzero/MainWin.cpp
  2. 6
      libethcore/EthashAux.cpp
  3. 38
      libethereum/BlockChain.cpp
  4. 3
      libethereum/BlockChain.h

2
alethzero/MainWin.cpp

@ -164,7 +164,7 @@ Main::Main(QWidget *parent) :
statusBar()->addPermanentWidget(ui->chainStatus);
statusBar()->addPermanentWidget(ui->blockCount);
ui->blockCount->setText(QString("PV%2 D%3 %4-%5 v%6").arg(eth::c_protocolVersion).arg(c_databaseVersion).arg(QString::fromStdString(ProofOfWork::name())).arg(ProofOfWork::revision()).arg(dev::Version));
ui->blockCount->setText(QString("PV%1.%2 D%3 %4-%5 v%6").arg(eth::c_protocolVersion).arg(eth::c_minorProtocolVersion).arg(c_databaseVersion).arg(QString::fromStdString(ProofOfWork::name())).arg(ProofOfWork::revision()).arg(dev::Version));
connect(ui->ourAccounts->model(), SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)), SLOT(ourAccountsRowsMoved()));

6
libethcore/EthashAux.cpp

@ -75,11 +75,11 @@ h256 EthashAux::seedHash(unsigned _number)
n = get()->m_seedHashes.size() - 1;
}
get()->m_seedHashes.resize(epoch + 1);
cdebug << "Searching for seedHash of epoch " << epoch;
// cdebug << "Searching for seedHash of epoch " << epoch;
for (; n <= epoch; ++n, ret = sha3(ret))
{
get()->m_seedHashes[n] = ret;
cdebug << "Epoch" << n << "is" << ret.abridged();
// cdebug << "Epoch" << n << "is" << ret.abridged();
}
}
return get()->m_seedHashes[epoch];
@ -95,7 +95,7 @@ ethash_params EthashAux::params(h256 const& _seedHash)
}
catch (...)
{
cdebug << "Searching for seedHash " << _seedHash.abridged();
// cdebug << "Searching for seedHash " << _seedHash.abridged();
for (h256 h; h != _seedHash && epoch < 2048; ++epoch, h = sha3(h), get()->m_epochs[h] = epoch) {}
if (epoch == 2048)
{

38
libethereum/BlockChain.cpp

@ -167,6 +167,7 @@ void BlockChain::open(std::string const& _path, WithExisting _we)
std::string l;
m_extrasDB->Get(m_readOptions, ldb::Slice("best"), &l);
m_lastBlockHash = l.empty() ? m_genesisHash : *(h256*)l.data();
m_lastBlockNumber = number(m_lastBlockHash);
cnote << "Opened blockchain DB. Latest: " << currentHash();
}
@ -177,6 +178,7 @@ void BlockChain::close()
delete m_extrasDB;
delete m_blocksDB;
m_lastBlockHash = m_genesisHash;
m_lastBlockNumber = 0;
m_details.clear();
m_blocks.clear();
}
@ -191,8 +193,7 @@ void BlockChain::rebuild(std::string const& _path, std::function<void(unsigned,
ProfilerStart("BlockChain_rebuild.log");
#endif
// unsigned originalNumber = (unsigned)BlockInfo(oldBlock(m_lastBlockHash)).number;
unsigned originalNumber = number();
unsigned originalNumber = m_lastBlockNumber;
// Keep extras DB around, but under a temp name
delete m_extrasDB;
@ -217,6 +218,7 @@ void BlockChain::rebuild(std::string const& _path, std::function<void(unsigned,
m_blocksBlooms.clear();
m_lastLastHashes.clear();
m_lastBlockHash = genesisHash();
m_lastBlockNumber = 0;
m_details[m_lastBlockHash].totalDifficulty = c_genesisDifficulty;
@ -535,12 +537,6 @@ ImportRoute BlockChain::import(bytes const& _block, OverlayDB const& _db, Import
{
unsigned commonIndex;
tie(route, common, commonIndex) = treeRoute(last, bi.hash());
{
WriteGuard l(x_lastBlockHash);
m_lastBlockHash = bi.hash();
}
m_extrasDB->Put(m_writeOptions, ldb::Slice("best"), ldb::Slice((char const*)&(bi.hash()), 32));
// Most of the time these two will be equal - only when we're doing a chain revert will they not be
if (common != last)
@ -601,6 +597,14 @@ ImportRoute BlockChain::import(bytes const& _block, OverlayDB const& _db, Import
m_extrasDB->Put(m_writeOptions, toSlice(h, ExtraTransactionAddress), (ldb::Slice)dev::ref(m_transactionAddresses[h].rlp()));
}
// FINALLY! change our best hash.
{
WriteGuard l(x_lastBlockHash);
m_lastBlockHash = bi.hash();
m_lastBlockNumber = (unsigned)bi.number;
m_extrasDB->Put(m_writeOptions, ldb::Slice("best"), ldb::Slice((char const*)&(bi.hash()), 32));
}
clog(BlockChainNote) << " Imported and best" << td << " (#" << bi.number << "). Has" << (details(bi.parentHash).children.size() - 1) << "siblings. Route:" << toString(route);
noteCanonChanged();
@ -960,14 +964,26 @@ bool BlockChain::isKnown(h256 const& _hash) const
{
if (_hash == m_genesisHash)
return true;
{
ReadGuard l(x_blocks);
if (m_blocks.count(_hash))
return true;
auto it = m_blocks.find(_hash);
if (it != m_blocks.end())
{
noteUsed(_hash);
BlockInfo bi(it->second, CheckNothing, _hash);
return bi.number <= m_lastBlockNumber; // TODO: m_lastBlockNumber
}
}
string d;
m_blocksDB->Get(m_readOptions, toSlice(_hash), &d);
return !!d.size();
if (!d.size())
return false;
BlockInfo bi(bytesConstRef(&d), CheckNothing, _hash);
return bi.number <= m_lastBlockNumber; // TODO: m_lastBlockNumber
}
bytes BlockChain::block(h256 const& _hash) const

3
libethereum/BlockChain.h

@ -185,7 +185,7 @@ public:
/// Get a number for the given hash (or the most recent mined if none given). Thread-safe.
unsigned number(h256 const& _hash) const { return details(_hash).number; }
unsigned number() const { return number(currentHash()); }
unsigned number() const { return m_lastBlockNumber; }
/// Get a given block (RLP format). Thread-safe.
h256 currentHash() const { ReadGuard l(x_lastBlockHash); return m_lastBlockHash; }
@ -315,6 +315,7 @@ private:
/// Hash of the last (valid) block on the longest chain.
mutable boost::shared_mutex x_lastBlockHash;
h256 m_lastBlockHash;
unsigned m_lastBlockNumber = 0;
/// Genesis block info.
h256 m_genesisHash;

Loading…
Cancel
Save