Track best block to later know if a DB upgrade is needed

This commit is contained in:
Alexander Block 2019-04-04 17:58:51 +02:00
parent 1a25c2084c
commit 7d765a0fce

View File

@ -25,7 +25,7 @@ CQuorumBlockProcessor* quorumBlockProcessor;
static const std::string DB_MINED_COMMITMENT = "q_mc"; static const std::string DB_MINED_COMMITMENT = "q_mc";
static const std::string DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT = "q_mcih"; static const std::string DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT = "q_mcih";
static const std::string DB_UPGRADED = "q_dbupgraded"; static const std::string DB_BEST_BLOCK_UPGRADE = "q_bbu";
void CQuorumBlockProcessor::ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman) void CQuorumBlockProcessor::ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman)
{ {
@ -121,6 +121,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex*
bool fDIP0003Active = VersionBitsState(pindex->pprev, Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0003, versionbitscache) == THRESHOLD_ACTIVE; bool fDIP0003Active = VersionBitsState(pindex->pprev, Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0003, versionbitscache) == THRESHOLD_ACTIVE;
if (!fDIP0003Active) { if (!fDIP0003Active) {
evoDb.Write(DB_BEST_BLOCK_UPGRADE, block.GetHash());
return true; return true;
} }
@ -151,12 +152,17 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex*
} }
} }
auto blockHash = block.GetHash();
for (auto& p : qcs) { for (auto& p : qcs) {
auto& qc = p.second; auto& qc = p.second;
if (!ProcessCommitment(pindex->nHeight, block.GetHash(), qc, state)) { if (!ProcessCommitment(pindex->nHeight, blockHash, qc, state)) {
return false; return false;
} }
} }
evoDb.Write(DB_BEST_BLOCK_UPGRADE, blockHash);
return true; return true;
} }
@ -245,21 +251,22 @@ bool CQuorumBlockProcessor::UndoBlock(const CBlock& block, const CBlockIndex* pi
AddMinableCommitment(qc); AddMinableCommitment(qc);
} }
evoDb.Write(DB_BEST_BLOCK_UPGRADE, pindex->pprev->GetBlockHash());
return true; return true;
} }
// TODO remove this with 0.15.0 // TODO remove this with 0.15.0
void CQuorumBlockProcessor::UpgradeDB() void CQuorumBlockProcessor::UpgradeDB()
{ {
bool v = false; LOCK(cs_main);
if (evoDb.GetRawDB().Read(DB_UPGRADED, v) && v) { uint256 bestBlock;
if (evoDb.GetRawDB().Read(DB_BEST_BLOCK_UPGRADE, bestBlock) && bestBlock == chainActive.Tip()->GetBlockHash()) {
return; return;
} }
LogPrintf("CQuorumBlockProcessor::%s -- Upgrading DB...\n", __func__); LogPrintf("CQuorumBlockProcessor::%s -- Upgrading DB...\n", __func__);
{
LOCK(cs_main);
if (chainActive.Height() >= Params().GetConsensus().DIP0003EnforcementHeight) { if (chainActive.Height() >= Params().GetConsensus().DIP0003EnforcementHeight) {
auto pindex = chainActive[Params().GetConsensus().DIP0003EnforcementHeight]; auto pindex = chainActive[Params().GetConsensus().DIP0003EnforcementHeight];
while (pindex) { while (pindex) {
@ -281,12 +288,12 @@ void CQuorumBlockProcessor::UpgradeDB()
evoDb.GetRawDB().Write(BuildInversedHeightKey((Consensus::LLMQType)qc.llmqType, pindex->nHeight), quorumIndex->nHeight); evoDb.GetRawDB().Write(BuildInversedHeightKey((Consensus::LLMQType)qc.llmqType, pindex->nHeight), quorumIndex->nHeight);
} }
evoDb.GetRawDB().Write(DB_BEST_BLOCK_UPGRADE, pindex->GetBlockHash());
pindex = chainActive.Next(pindex); pindex = chainActive.Next(pindex);
} }
} }
}
evoDb.GetRawDB().Write(DB_UPGRADED, true);
LogPrintf("CQuorumBlockProcessor::%s -- Upgrade done...\n", __func__); LogPrintf("CQuorumBlockProcessor::%s -- Upgrade done...\n", __func__);
} }