Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate collateral ratio when there is actual price update #1007

Merged
merged 1 commit into from
Apr 15, 2022
Merged
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
36 changes: 29 additions & 7 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3064,10 +3064,16 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
viewCache.Flush();
}

if (pindex->nHeight % chainparams.GetConsensus().blocksCollateralizationRatioCalculation() == 0) {
static const auto calculationBlock = chainparams.GetConsensus().blocksCollateralizationRatioCalculation();

// non consensus enforced
static bool firstRun = true;
static std::vector<uint256> liquidatedVaults;

if (pindex->nHeight % calculationBlock == 0) {
bool useNextPrice = false, requireLivePrice = true;

cache.ForEachVaultCollateral([&](const CVaultId& vaultId, const CBalances& collaterals) {
auto processVault = [&](const CVaultId& vaultId, const CBalances& collaterals) {
auto collateral = cache.GetLoanCollaterals(vaultId, collaterals, pindex->nHeight, pindex->nTime, useNextPrice, requireLivePrice);
if (!collateral) {
return true;
Expand Down Expand Up @@ -3145,7 +3151,20 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
}

return true;
});
};

auto lastPriceUpdate = pindex->nHeight - (pindex->nHeight % cache.GetIntervalBlock());
if (firstRun || lastPriceUpdate > pindex->nHeight - calculationBlock) {
cache.ForEachVaultCollateral(processVault);
} else {
for (const auto& vaultId : liquidatedVaults) {
if (auto collaterals = cache.GetVaultCollaterals(vaultId)) {
processVault(vaultId, *collaterals);
}
}
}
firstRun = false;
liquidatedVaults.clear();
}

CHistoryWriters writers{nullptr, pburnHistoryDB.get(), pvaultHistoryDB.get()};
Expand Down Expand Up @@ -3222,6 +3241,8 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
view.StoreVault(vaultId, *vault);
view.EraseAuction(vaultId, pindex->nHeight);

liquidatedVaults.push_back(vaultId);

// Store state in vault DB
if (pvaultHistoryDB) {
pvaultHistoryDB->WriteVaultState(view, *pindex, vaultId);
Expand Down Expand Up @@ -3414,20 +3435,21 @@ void CChainState::ProcessOracleEvents(const CBlockIndex* pindex, CCustomCSView&

// Furthermore, the time stamp is always indicative of the
// last price time.
auto nextPrice = fixedIntervalPrice.priceRecord[1];
auto& nextPrice = fixedIntervalPrice.priceRecord[1];
if (nextPrice > 0) {
fixedIntervalPrice.priceRecord[0] = fixedIntervalPrice.priceRecord[1];
auto& currentPrice = fixedIntervalPrice.priceRecord[0];
currentPrice = nextPrice;
}
// keep timestamp updated
fixedIntervalPrice.timestamp = pindex->nTime;
// Use -1 to indicate empty price
fixedIntervalPrice.priceRecord[1] = -1;
nextPrice = -1;
auto aggregatePrice = GetAggregatePrice(cache,
fixedIntervalPrice.priceFeedId.first,
fixedIntervalPrice.priceFeedId.second,
pindex->nTime);
if (aggregatePrice) {
fixedIntervalPrice.priceRecord[1] = aggregatePrice;
nextPrice = aggregatePrice;
} else {
LogPrint(BCLog::ORACLE,"ProcessOracleEvents(): No aggregate price available: %s\n", aggregatePrice.msg);
}
Expand Down