Skip to content

Commit

Permalink
Restart: Check collateral valid before usage (#3047)
Browse files Browse the repository at this point in the history
* Restart: Check collateral valid before usage

* Add log for no collaterals
  • Loading branch information
Bushstar authored Sep 16, 2024
1 parent ac6dfea commit 1c6c5ed
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/dfi/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2887,8 +2887,8 @@ static Res PaybackWithSwappedCollateral(const DCT_ID &collId,
std::vector<CollToLoan> collToLoans;
// collect all loanValues (in USD) of vaults which contain this collateral
cache.ForEachLoanTokenAmount([&](const CVaultId &vaultId, const CBalances &balances) {
auto colls = cache.GetVaultCollaterals(vaultId);
if (colls->balances.count(collId)) {
const auto colls = cache.GetVaultCollaterals(vaultId);
if (colls && colls->balances.count(collId)) {
collToLoans.emplace_back(CollToLoan{vaultId, {}, 0});
collToLoans.back().useableCollateralAmount = colls->balances.at(collId);
for (const auto &[tokenId, amount] : balances.balances) {
Expand Down Expand Up @@ -3304,6 +3304,9 @@ static Res ForceCloseAllLoans(const CBlockIndex *pindex, CCustomCSView &cache, B
std::set<DCT_ID> allUsedCollaterals;
cache.ForEachLoanTokenAmount([&](const CVaultId &vaultId, const CBalances &balances) {
auto colls = cache.GetVaultCollaterals(vaultId);
if (!colls) {
return true;
}
for (const auto &[collId, collAmount] : colls->balances) {
allUsedCollaterals.insert(collId);
}
Expand Down Expand Up @@ -3389,9 +3392,10 @@ static Res ForceCloseAllLoans(const CBlockIndex *pindex, CCustomCSView &cache, B
if (!gotLoan) {
return true;
}
auto colls = cache.GetVaultCollaterals(vaultId);
for (const auto &[collId, collAmount] : colls->balances) {
allUsedCollaterals.insert(collId);
if (const auto colls = cache.GetVaultCollaterals(vaultId)) {
for (const auto &[collId, collAmount] : colls->balances) {
allUsedCollaterals.insert(collId);
}
}
return true;
});
Expand All @@ -3416,10 +3420,13 @@ static Res ForceCloseAllLoans(const CBlockIndex *pindex, CCustomCSView &cache, B
for (const auto &loan : loanAmounts->balances) {
LogPrintf(" %s@%d\n", GetDecimalString(loan.second), loan.first.v);
}
const auto collAmounts = cache.GetVaultCollaterals(vaultId);
LogPrintf("%d collaterals: \n", collAmounts->balances.size());
for (const auto &loan : collAmounts->balances) {
LogPrintf(" %s@%d\n", GetDecimalString(loan.second), loan.first.v);
if (const auto collAmounts = cache.GetVaultCollaterals(vaultId)) {
LogPrintf("%d collaterals: \n", collAmounts->balances.size());
for (const auto &loan : collAmounts->balances) {
LogPrintf(" %s@%d\n", GetDecimalString(loan.second), loan.first.v);
}
} else {
LogPrintf("no collaterals: vault %s\n", vaultId.ToString());
}
}
return true;
Expand Down

0 comments on commit 1c6c5ed

Please sign in to comment.