|
|
@ -41,9 +41,9 @@ bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const
|
|
|
|
return success;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BaseIndex::DB::WriteBestBlock(const CBlockLocator& locator)
|
|
|
|
void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return Write(DB_BEST_BLOCK, locator);
|
|
|
|
batch.Write(DB_BEST_BLOCK, locator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BaseIndex::~BaseIndex()
|
|
|
|
BaseIndex::~BaseIndex()
|
|
|
@ -95,7 +95,11 @@ void BaseIndex::ThreadSync()
|
|
|
|
int64_t last_locator_write_time = 0;
|
|
|
|
int64_t last_locator_write_time = 0;
|
|
|
|
while (true) {
|
|
|
|
while (true) {
|
|
|
|
if (m_interrupt) {
|
|
|
|
if (m_interrupt) {
|
|
|
|
WriteBestBlock(pindex);
|
|
|
|
m_best_block_index = pindex;
|
|
|
|
|
|
|
|
// No need to handle errors in Commit. If it fails, the error will be already be
|
|
|
|
|
|
|
|
// logged. The best way to recover is to continue, as index cannot be corrupted by
|
|
|
|
|
|
|
|
// a missed commit to disk for an advanced index state.
|
|
|
|
|
|
|
|
Commit();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -103,9 +107,10 @@ void BaseIndex::ThreadSync()
|
|
|
|
LOCK(cs_main);
|
|
|
|
LOCK(cs_main);
|
|
|
|
const CBlockIndex* pindex_next = NextSyncBlock(pindex);
|
|
|
|
const CBlockIndex* pindex_next = NextSyncBlock(pindex);
|
|
|
|
if (!pindex_next) {
|
|
|
|
if (!pindex_next) {
|
|
|
|
WriteBestBlock(pindex);
|
|
|
|
|
|
|
|
m_best_block_index = pindex;
|
|
|
|
m_best_block_index = pindex;
|
|
|
|
m_synced = true;
|
|
|
|
m_synced = true;
|
|
|
|
|
|
|
|
// No need to handle errors in Commit. See rationale above.
|
|
|
|
|
|
|
|
Commit();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pindex = pindex_next;
|
|
|
|
pindex = pindex_next;
|
|
|
@ -119,8 +124,10 @@ void BaseIndex::ThreadSync()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
|
|
|
|
if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
|
|
|
|
WriteBestBlock(pindex);
|
|
|
|
m_best_block_index = pindex;
|
|
|
|
last_locator_write_time = current_time;
|
|
|
|
last_locator_write_time = current_time;
|
|
|
|
|
|
|
|
// No need to handle errors in Commit. See rationale above.
|
|
|
|
|
|
|
|
Commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CBlock block;
|
|
|
|
CBlock block;
|
|
|
@ -144,15 +151,22 @@ void BaseIndex::ThreadSync()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BaseIndex::WriteBestBlock(const CBlockIndex* block_index)
|
|
|
|
bool BaseIndex::Commit()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
CDBBatch batch(GetDB());
|
|
|
|
if (!GetDB().WriteBestBlock(chainActive.GetLocator(block_index))) {
|
|
|
|
if (!CommitInternal(batch) || !GetDB().WriteBatch(batch)) {
|
|
|
|
return error("%s: Failed to write locator to disk", __func__);
|
|
|
|
return error("%s: Failed to commit latest %s state", __func__, GetName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool BaseIndex::CommitInternal(CDBBatch& batch)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
LOCK(cs_main);
|
|
|
|
|
|
|
|
GetDB().WriteBestBlock(batch, chainActive.GetLocator(m_best_block_index));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex,
|
|
|
|
void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex,
|
|
|
|
const std::vector<CTransactionRef>& txn_conflicted)
|
|
|
|
const std::vector<CTransactionRef>& txn_conflicted)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -224,9 +238,10 @@ void BaseIndex::ChainStateFlushed(const CBlockLocator& locator)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!GetDB().WriteBestBlock(locator)) {
|
|
|
|
// No need to handle errors in Commit. If it fails, the error will be already be logged. The
|
|
|
|
error("%s: Failed to write locator to disk", __func__);
|
|
|
|
// best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
|
|
|
|
}
|
|
|
|
// for an advanced index state.
|
|
|
|
|
|
|
|
Commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BaseIndex::BlockUntilSyncedToCurrentChain()
|
|
|
|
bool BaseIndex::BlockUntilSyncedToCurrentChain()
|
|
|
|