diff --git a/src/init.cpp b/src/init.cpp index 57a1b14988..098fa93916 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1396,6 +1396,24 @@ void SetupInterrupts() { fStopOrInterrupt = isSet; } +static bool LoanAmountsInClosedVaults(CCustomCSView &mnview) { + LOCK(cs_main); + + std::set vaults; + mnview.ForEachLoanTokenAmount([&](const CVaultId &vaultId, const CBalances &balances) { + vaults.insert(vaultId); + return true; + }); + + for (const auto &vaultId : vaults) { + const auto vault = mnview.GetVault(vaultId); + if (!vault) { + return true; + } + } + return false; +} + bool AppInitMain(InitInterfaces& interfaces) { const CChainParams& chainparams = Params(); @@ -1757,6 +1775,11 @@ bool AppInitMain(InitInterfaces& interfaces) // Ensure we are on latest DB version pcustomcsview->SetDbVersion(CCustomCSView::DbVersion); + if (LoanAmountsInClosedVaults(*pcustomcsview)) { + strLoadError = "Corrupted block database detected. You will need to rebuild the database using -reindex-chainstate."; + break; + } + // make account history db paccountHistoryDB.reset(); if (gArgs.GetBoolArg("-acindex", DEFAULT_ACINDEX)) { diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index 3d3e885dcc..44b32412f5 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -2840,6 +2840,11 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor { Require(amount + totalInterest <= 0, "Vault <%s> has loans", obj.vaultId.GetHex()); + // If there is an amount negated by interested remove it from loan tokens. + if (amount > 0) { + mnview.SubLoanToken(obj.vaultId, {tokenId, amount}); + } + if (totalInterest < 0) { TrackNegativeInterest( mnview, {tokenId, amount > std::abs(totalInterest) ? std::abs(totalInterest) : amount});