Skip to content

Commit

Permalink
refactor: Removed old evo/deterministicmns parts (#5113)
Browse files Browse the repository at this point in the history
<!--
*** Please remove the following help text before submitting: ***

Provide a general summary of your changes in the Title above

Pull requests without a rationale and clear improvement may be closed
immediately.

Please provide clear motivation for your patch and explain how it
improves
Dash Core user experience or Dash Core developer experience
significantly:

* Any test improvements or new tests that improve coverage are always
welcome.
* All other changes should have accompanying unit tests (see
`src/test/`) or
functional tests (see `test/`). Contributors should note which tests
cover
modified code. If no tests exist for a region of modified code, new
tests
  should accompany the change.
* Bug fixes are most welcome when they come with steps to reproduce or
an
explanation of the potential issue as well as reasoning for the way the
bug
  was fixed.
* Features are welcome, but might be rejected due to design or scope
issues.
If a feature is based on a lot of dependencies, contributors should
first
  consider building the system outside of Dash Core, if possible.
-->

## Issue being fixed or feature implemented
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->


## What was done?
<!--- Describe your changes in detail -->
Removed code related to the upgrade for old `CDeterministicMNListDiff`
format to new format.
This was implemented in #3017.
I believe we can safely remove this now

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->


## Breaking Changes
<!--- Please describe any breaking changes your code introduces -->


## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have added or updated relevant unit/integration/functional/e2e
tests
- [ ] I have made corresponding changes to the documentation

**For repository code-owners and collaborators only**
- [x] I have assigned this pull request to a milestone
  • Loading branch information
ogabrielides authored Dec 27, 2022
1 parent 07b5a45 commit c87ff11
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 151 deletions.
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

0 comments on commit c87ff11

Please sign in to comment.