Track at which height a quorum commitment was mined
Also add upgrade code to allow smooth upgrades from v13 to v14.
This commit is contained in:
parent
806948f90e
commit
d5250a333a
@ -24,6 +24,9 @@ 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_FIRST_MINED_COMMITMENT = "q_fmc";
|
static const std::string DB_FIRST_MINED_COMMITMENT = "q_fmc";
|
||||||
|
static const std::string DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT = "q_mcih";
|
||||||
|
|
||||||
|
static const std::string DB_UPGRADED = "q_dbupgraded";
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@ -158,6 +161,13 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex*
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We store a mapping from minedHeight->quorumHeight in the DB
|
||||||
|
// minedHeight is inversed so that entries are traversable in reversed order
|
||||||
|
static std::tuple<std::string, uint8_t, int> BuildInversedHeightKey(Consensus::LLMQType llmqType, int nMinedHeight)
|
||||||
|
{
|
||||||
|
return std::make_tuple(DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT, (uint8_t)llmqType, std::numeric_limits<int>::max() - nMinedHeight);
|
||||||
|
}
|
||||||
|
|
||||||
bool CQuorumBlockProcessor::ProcessCommitment(const CBlockIndex* pindex, const CFinalCommitment& qc, CValidationState& state)
|
bool CQuorumBlockProcessor::ProcessCommitment(const CBlockIndex* pindex, const CFinalCommitment& qc, CValidationState& state)
|
||||||
{
|
{
|
||||||
auto& params = Params().GetConsensus().llmqs.at((Consensus::LLMQType)qc.llmqType);
|
auto& params = Params().GetConsensus().llmqs.at((Consensus::LLMQType)qc.llmqType);
|
||||||
@ -194,10 +204,9 @@ bool CQuorumBlockProcessor::ProcessCommitment(const CBlockIndex* pindex, const C
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Store commitment in DB
|
// Store commitment in DB
|
||||||
|
auto quorumIndex = mapBlockIndex.at(qc.quorumHash);
|
||||||
evoDb.Write(std::make_pair(DB_MINED_COMMITMENT, std::make_pair((uint8_t)params.type, quorumHash)), qc);
|
evoDb.Write(std::make_pair(DB_MINED_COMMITMENT, std::make_pair((uint8_t)params.type, quorumHash)), qc);
|
||||||
if (!evoDb.Exists(std::make_pair(DB_FIRST_MINED_COMMITMENT, (uint8_t)params.type))) {
|
evoDb.Write(BuildInversedHeightKey(params.type, pindex->nHeight), quorumIndex->nHeight);
|
||||||
evoDb.Write(std::make_pair(DB_FIRST_MINED_COMMITMENT, (uint8_t)params.type), quorumHash);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
LOCK(minableCommitmentsCs);
|
LOCK(minableCommitmentsCs);
|
||||||
@ -227,6 +236,7 @@ bool CQuorumBlockProcessor::UndoBlock(const CBlock& block, const CBlockIndex* pi
|
|||||||
}
|
}
|
||||||
|
|
||||||
evoDb.Erase(std::make_pair(DB_MINED_COMMITMENT, std::make_pair(qc.llmqType, qc.quorumHash)));
|
evoDb.Erase(std::make_pair(DB_MINED_COMMITMENT, std::make_pair(qc.llmqType, qc.quorumHash)));
|
||||||
|
evoDb.Erase(BuildInversedHeightKey((Consensus::LLMQType)qc.llmqType, pindex->nHeight));
|
||||||
{
|
{
|
||||||
LOCK(minableCommitmentsCs);
|
LOCK(minableCommitmentsCs);
|
||||||
hasMinedCommitmentCache.erase(std::make_pair((Consensus::LLMQType)qc.llmqType, qc.quorumHash));
|
hasMinedCommitmentCache.erase(std::make_pair((Consensus::LLMQType)qc.llmqType, qc.quorumHash));
|
||||||
@ -244,6 +254,47 @@ bool CQuorumBlockProcessor::UndoBlock(const CBlock& block, const CBlockIndex* pi
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO remove this with 0.15.0
|
||||||
|
void CQuorumBlockProcessor::UpgradeDB()
|
||||||
|
{
|
||||||
|
bool v = false;
|
||||||
|
if (evoDb.GetRawDB().Read(DB_UPGRADED, v) && v) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogPrintf("CQuorumBlockProcessor::%s -- Upgrading DB...\n", __func__);
|
||||||
|
|
||||||
|
{
|
||||||
|
LOCK(cs_main);
|
||||||
|
if (chainActive.Height() >= Params().GetConsensus().DIP0003EnforcementHeight) {
|
||||||
|
auto pindex = chainActive[Params().GetConsensus().DIP0003EnforcementHeight];
|
||||||
|
while (pindex) {
|
||||||
|
CBlock block;
|
||||||
|
bool r = ReadBlockFromDisk(block, pindex, Params().GetConsensus());
|
||||||
|
assert(r);
|
||||||
|
|
||||||
|
std::map<Consensus::LLMQType, CFinalCommitment> qcs;
|
||||||
|
CValidationState dummyState;
|
||||||
|
GetCommitmentsFromBlock(block, pindex->pprev, qcs, dummyState);
|
||||||
|
|
||||||
|
for (const auto& p : qcs) {
|
||||||
|
const auto& qc = p.second;
|
||||||
|
if (qc.IsNull()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto quorumIndex = mapBlockIndex.at(qc.quorumHash);
|
||||||
|
evoDb.GetRawDB().Write(BuildInversedHeightKey((Consensus::LLMQType)qc.llmqType, pindex->nHeight), quorumIndex->nHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
pindex = chainActive.Next(pindex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
evoDb.GetRawDB().Write(DB_UPGRADED, true);
|
||||||
|
LogPrintf("CQuorumBlockProcessor::%s -- Upgrade done...\n", __func__);
|
||||||
|
}
|
||||||
|
|
||||||
bool CQuorumBlockProcessor::GetCommitmentsFromBlock(const CBlock& block, const CBlockIndex* pindexPrev, std::map<Consensus::LLMQType, CFinalCommitment>& ret, CValidationState& state)
|
bool CQuorumBlockProcessor::GetCommitmentsFromBlock(const CBlock& block, const CBlockIndex* pindexPrev, std::map<Consensus::LLMQType, CFinalCommitment>& ret, CValidationState& state)
|
||||||
{
|
{
|
||||||
AssertLockHeld(cs_main);
|
AssertLockHeld(cs_main);
|
||||||
|
@ -37,6 +37,8 @@ private:
|
|||||||
public:
|
public:
|
||||||
CQuorumBlockProcessor(CEvoDB& _evoDb) : evoDb(_evoDb) {}
|
CQuorumBlockProcessor(CEvoDB& _evoDb) : evoDb(_evoDb) {}
|
||||||
|
|
||||||
|
void UpgradeDB();
|
||||||
|
|
||||||
void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
|
void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
|
||||||
|
|
||||||
bool ProcessBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state);
|
bool ProcessBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state);
|
||||||
|
@ -65,6 +65,8 @@ void DestroyLLMQSystem()
|
|||||||
|
|
||||||
void StartLLMQSystem()
|
void StartLLMQSystem()
|
||||||
{
|
{
|
||||||
|
quorumBlockProcessor->UpgradeDB();
|
||||||
|
|
||||||
if (blsWorker) {
|
if (blsWorker) {
|
||||||
blsWorker->Start();
|
blsWorker->Start();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user