Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 0 additions & 115 deletions src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1069,121 +1069,6 @@ void CDeterministicMNManager::CleanupCache(int nHeight)
}
}

void CDeterministicMNManager::UpgradeDiff(CDBBatch& batch, const CBlockIndex* pindexNext, const CDeterministicMNList& curMNList, CDeterministicMNList& newMNList)
{
CDataStream oldDiffData(SER_DISK, CLIENT_VERSION);
if (!m_evoDb.GetRawDB().ReadDataStream(std::make_pair(DB_LIST_DIFF, pindexNext->GetBlockHash()), oldDiffData)) {
LogPrintf("CDeterministicMNManager::%s -- no diff found for %s\n", __func__, pindexNext->GetBlockHash().ToString());
newMNList = curMNList;
newMNList.SetBlockHash(pindexNext->GetBlockHash());
newMNList.SetHeight(pindexNext->nHeight);
return;
}

CDeterministicMNListDiff_OldFormat oldDiff;
oldDiffData >> oldDiff;

CDeterministicMNListDiff newDiff;
size_t addedCount = 0;
for (const auto& p : oldDiff.addedMNs) {
auto dmn = std::make_shared<CDeterministicMN>(*p.second, curMNList.GetTotalRegisteredCount() + addedCount);
newDiff.addedMNs.emplace_back(dmn);
addedCount++;
}
for (const auto& p : oldDiff.removedMns) {
auto dmn = curMNList.GetMN(p);
newDiff.removedMns.emplace(dmn->GetInternalId());
}

// applies added/removed MNs
newMNList = curMNList.ApplyDiff(pindexNext, newDiff);

// manually apply updated MNs and calc new state diffs
for (const auto& p : oldDiff.updatedMNs) {
auto oldMN = newMNList.GetMN(p.first);
if (!oldMN) {
throw(std::runtime_error(strprintf("%s: Can't find an old masternode with proTxHash=%s", __func__, p.first.ToString())));
}
newMNList.UpdateMN(p.first, p.second);
auto newMN = newMNList.GetMN(p.first);
if (!newMN) {
throw(std::runtime_error(strprintf("%s: Can't find a new masternode with proTxHash=%s", __func__, p.first.ToString())));
}

newDiff.updatedMNs.emplace(std::piecewise_construct,
std::forward_as_tuple(oldMN->GetInternalId()),
std::forward_as_tuple(*oldMN->pdmnState, *newMN->pdmnState));
}

batch.Write(std::make_pair(DB_LIST_DIFF, pindexNext->GetBlockHash()), newDiff);
}

// TODO this can be completely removed in a future version
bool CDeterministicMNManager::UpgradeDBIfNeeded()
{
LOCK(cs_main);

if (::ChainActive().Tip() == nullptr) {
// should have no records
return m_evoDb.IsEmpty();
}

if (m_evoDb.GetRawDB().Exists(EVODB_BEST_BLOCK)) {
return true;
}

// Removing the old EVODB_BEST_BLOCK value early results in older version to crash immediately, even if the upgrade
// process is cancelled in-between. But if the new version sees that the old EVODB_BEST_BLOCK is already removed,
// then we must assume that the upgrade process was already running before but was interrupted.
if (::ChainActive().Height() > 1 && !m_evoDb.GetRawDB().Exists(std::string("b_b"))) {
return false;
}
m_evoDb.GetRawDB().Erase(std::string("b_b"));

if (::ChainActive().Height() < Params().GetConsensus().DIP0003Height) {
// not reached DIP3 height yet, so no upgrade needed
auto dbTx = m_evoDb.BeginTransaction();
m_evoDb.WriteBestBlock(::ChainActive().Tip()->GetBlockHash());
dbTx->Commit();
return true;
}

LogPrintf("CDeterministicMNManager::%s -- upgrading DB to use compact diffs\n", __func__);

CDBBatch batch(m_evoDb.GetRawDB());

CDeterministicMNList curMNList;
curMNList.SetHeight(Params().GetConsensus().DIP0003Height - 1);
curMNList.SetBlockHash(::ChainActive()[Params().GetConsensus().DIP0003Height - 1]->GetBlockHash());

for (int nHeight = Params().GetConsensus().DIP0003Height; nHeight <= ::ChainActive().Height(); nHeight++) {
auto pindex = ::ChainActive()[nHeight];

CDeterministicMNList newMNList;
UpgradeDiff(batch, pindex, curMNList, newMNList);

if ((nHeight % DISK_SNAPSHOT_PERIOD) == 0) {
batch.Write(std::make_pair(DB_LIST_SNAPSHOT, pindex->GetBlockHash()), newMNList);
m_evoDb.GetRawDB().WriteBatch(batch);
batch.Clear();
}

curMNList = newMNList;
}

m_evoDb.GetRawDB().WriteBatch(batch);

LogPrintf("CDeterministicMNManager::%s -- done upgrading\n", __func__);

// Writing EVODB_BEST_BLOCK (which is b_b2 now) marks the DB as upgraded
auto dbTx = m_evoDb.BeginTransaction();
m_evoDb.WriteBestBlock(::ChainActive().Tip()->GetBlockHash());
dbTx->Commit();

m_evoDb.GetRawDB().CompactFull();

return true;
}

template <typename ProTx>
static bool CheckService(const ProTx& proTx, CValidationState& state)
Expand Down
35 changes: 0 additions & 35 deletions src/evo/deterministicmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,38 +477,6 @@ class CDeterministicMNListDiff
}
};

// TODO can be removed in a future version
class CDeterministicMNListDiff_OldFormat
{
public:
uint256 prevBlockHash;
uint256 blockHash;
int nHeight{-1};
std::map<uint256, CDeterministicMNCPtr> addedMNs;
std::map<uint256, std::shared_ptr<const CDeterministicMNState>> updatedMNs;
std::set<uint256> removedMns;

template<typename Stream>
void Unserialize(Stream& s) {
addedMNs.clear();
s >> prevBlockHash;
s >> blockHash;
s >> nHeight;
size_t cnt = ReadCompactSize(s);
for (size_t i = 0; i < cnt; i++) {
uint256 proTxHash;
// NOTE: This is a hack and "0" is just a dummy id. The actual internalId is assigned to a copy
// of this dmn via corresponding ctor when we convert the diff format to a new one in UpgradeDiff
// thus the logic that we must set internalId before dmn is used in any meaningful way is preserved.
auto dmn = std::make_shared<CDeterministicMN>(0);
s >> proTxHash;
dmn->Unserialize(s, true);
addedMNs.emplace(proTxHash, dmn);
}
s >> updatedMNs;
s >> removedMns;
}
};

class CDeterministicMNManager
{
Expand Down Expand Up @@ -559,9 +527,6 @@ class CDeterministicMNManager

bool IsDIP3Enforced(int nHeight = -1);

// TODO these can all be removed in a future version
void UpgradeDiff(CDBBatch& batch, const CBlockIndex* pindexNext, const CDeterministicMNList& curMNList, CDeterministicMNList& newMNList);
bool UpgradeDBIfNeeded();

void DoMaintenance();

Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2168,7 +2168,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
break; // out of the chainstate activation do-while
}

if (!deterministicMNManager->UpgradeDBIfNeeded() || !llmq::quorumBlockProcessor->UpgradeDB()) {
if (!llmq::quorumBlockProcessor->UpgradeDB()) {
strLoadError = _("Error upgrading evo database");
break;
}
Expand Down