feat: update some asserts related to CreditPool in consensus code to exceptions

This commit is contained in:
Konstantin Akimov 2024-10-28 22:20:31 +07:00
parent 877aa08144
commit c97f5f5ca5
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524

View File

@ -134,9 +134,13 @@ CCreditPool CCreditPoolManager::ConstructCreditPool(const CBlockIndex* const blo
if (!block) {
// If reading of previous block is not successfully, but
// prev contains credit pool related data, something strange happened
assert(prev.locked == 0);
assert(prev.indexes.IsEmpty());
if (prev.locked != 0) {
throw std::runtime_error(strprintf("Failed to create CreditPool but previous block has value"));
}
if (!prev.indexes.IsEmpty()) {
throw std::runtime_error(
strprintf("Failed to create CreditPool but asset unlock transactions already mined"));
}
CCreditPool emptyPool;
AddToCache(block_index->GetBlockHash(), block_index->nHeight, emptyPool);
return emptyPool;
@ -184,9 +188,7 @@ CCreditPool CCreditPoolManager::ConstructCreditPool(const CBlockIndex* const blo
currentLimit = std::min(currentLimit, LimitAmountHigh - latelyUnlocked);
}
assert(currentLimit >= 0);
if (currentLimit > 0 || latelyUnlocked > 0 || locked > 0) {
if (currentLimit != 0 || latelyUnlocked > 0 || locked > 0) {
LogPrint(BCLog::CREDITPOOL, /* Continued */
"CCreditPoolManager: asset unlock limits on height: %d locked: %d.%08d limit: %d.%08d "
"unlocked-in-window: %d.%08d\n",
@ -194,6 +196,11 @@ CCreditPool CCreditPoolManager::ConstructCreditPool(const CBlockIndex* const blo
latelyUnlocked / COIN, latelyUnlocked % COIN);
}
if (currentLimit < 0) {
throw std::runtime_error(
strprintf("Negative limit for CreditPool: %d.%08d\n", currentLimit / COIN, currentLimit % COIN));
}
CCreditPool pool{locked, currentLimit, latelyUnlocked, indexes};
AddToCache(block_index->GetBlockHash(), block_index->nHeight, pool);
return pool;