From f192234ae6608404723f1e07479baa027511d201 Mon Sep 17 00:00:00 2001 From: Peter Bushnell Date: Fri, 27 Jan 2023 11:34:07 +0000 Subject: [PATCH 1/2] Remove positive amount on close vault --- src/init.cpp | 19 +++++++++++++++++++ src/masternodes/loan.cpp | 4 ++++ src/masternodes/loan.h | 1 + src/masternodes/mn_checks.cpp | 5 +++++ 4 files changed, 29 insertions(+) diff --git a/src/init.cpp b/src/init.cpp index 8117c158542..c965d4e7183 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1395,6 +1395,23 @@ void SetupInterrupts() { fStopOrInterrupt = isSet; } +static void EraseLoanAmountsInClosedVaults(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) { + mnview.EraseVaultLoanTokens(vaultId); + } + } +} + bool AppInitMain(InitInterfaces& interfaces) { const CChainParams& chainparams = Params(); @@ -1756,6 +1773,8 @@ bool AppInitMain(InitInterfaces& interfaces) // Ensure we are on latest DB version pcustomcsview->SetDbVersion(CCustomCSView::DbVersion); + EraseLoanAmountsInClosedVaults(*pcustomcsview); + // make account history db paccountHistoryDB.reset(); if (gArgs.GetBoolArg("-acindex", DEFAULT_ACINDEX)) { diff --git a/src/masternodes/loan.cpp b/src/masternodes/loan.cpp index beca57c2b22..452f5b6de1c 100644 --- a/src/masternodes/loan.cpp +++ b/src/masternodes/loan.cpp @@ -552,6 +552,10 @@ Res CLoanView::SubLoanToken(const CVaultId &vaultId, CTokenAmount amount) { return Res::Ok(); } +void CLoanView::EraseVaultLoanTokens(const CVaultId &vaultId) { + EraseBy(vaultId); +} + std::optional CLoanView::GetLoanTokens(const CVaultId &vaultId) { return ReadBy(vaultId); } diff --git a/src/masternodes/loan.h b/src/masternodes/loan.h index 0aa17cae445..ea495ef71b6 100644 --- a/src/masternodes/loan.h +++ b/src/masternodes/loan.h @@ -470,6 +470,7 @@ class CLoanView : public virtual CStorageView { Res AddLoanToken(const CVaultId &vaultId, CTokenAmount amount); Res SubLoanToken(const CVaultId &vaultId, CTokenAmount amount); + void EraseVaultLoanTokens(const CVaultId &vaultId); std::optional GetLoanTokens(const CVaultId &vaultId); void ForEachLoanTokenAmount(std::function callback); diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index 0108510642b..61a82e436f9 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -2839,6 +2839,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}); From b1b99c53c1a3c7fb6624154781e125c28d37094d Mon Sep 17 00:00:00 2001 From: Bushstar Date: Tue, 7 Feb 2023 06:49:17 +0000 Subject: [PATCH 2/2] Throw error on invalid state --- src/init.cpp | 10 +++++++--- src/masternodes/loan.cpp | 4 ---- src/masternodes/loan.h | 1 - 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index c15a81f4fbf..098fa939169 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1396,7 +1396,7 @@ void SetupInterrupts() { fStopOrInterrupt = isSet; } -static void EraseLoanAmountsInClosedVaults(CCustomCSView &mnview) { +static bool LoanAmountsInClosedVaults(CCustomCSView &mnview) { LOCK(cs_main); std::set vaults; @@ -1408,9 +1408,10 @@ static void EraseLoanAmountsInClosedVaults(CCustomCSView &mnview) { for (const auto &vaultId : vaults) { const auto vault = mnview.GetVault(vaultId); if (!vault) { - mnview.EraseVaultLoanTokens(vaultId); + return true; } } + return false; } bool AppInitMain(InitInterfaces& interfaces) @@ -1774,7 +1775,10 @@ bool AppInitMain(InitInterfaces& interfaces) // Ensure we are on latest DB version pcustomcsview->SetDbVersion(CCustomCSView::DbVersion); - EraseLoanAmountsInClosedVaults(*pcustomcsview); + 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(); diff --git a/src/masternodes/loan.cpp b/src/masternodes/loan.cpp index 452f5b6de1c..beca57c2b22 100644 --- a/src/masternodes/loan.cpp +++ b/src/masternodes/loan.cpp @@ -552,10 +552,6 @@ Res CLoanView::SubLoanToken(const CVaultId &vaultId, CTokenAmount amount) { return Res::Ok(); } -void CLoanView::EraseVaultLoanTokens(const CVaultId &vaultId) { - EraseBy(vaultId); -} - std::optional CLoanView::GetLoanTokens(const CVaultId &vaultId) { return ReadBy(vaultId); } diff --git a/src/masternodes/loan.h b/src/masternodes/loan.h index ea495ef71b6..0aa17cae445 100644 --- a/src/masternodes/loan.h +++ b/src/masternodes/loan.h @@ -470,7 +470,6 @@ class CLoanView : public virtual CStorageView { Res AddLoanToken(const CVaultId &vaultId, CTokenAmount amount); Res SubLoanToken(const CVaultId &vaultId, CTokenAmount amount); - void EraseVaultLoanTokens(const CVaultId &vaultId); std::optional GetLoanTokens(const CVaultId &vaultId); void ForEachLoanTokenAmount(std::function callback);