From b0e965a5f770b19003c26595a1cf6d85c94ed762 Mon Sep 17 00:00:00 2001 From: Daniel Beal Date: Thu, 7 Nov 2024 12:04:41 +0900 Subject: [PATCH] ability for cachedDebt to become negative to avoid subtraction overflow --- contracts/BaseDebtCache.sol | 6 +++--- contracts/DebtCache.sol | 15 +++++++-------- contracts/interfaces/IDebtCache.sol | 4 ++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/contracts/BaseDebtCache.sol b/contracts/BaseDebtCache.sol index 7a5396083a..c1216b1def 100644 --- a/contracts/BaseDebtCache.sol +++ b/contracts/BaseDebtCache.sol @@ -26,7 +26,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache { using SafeMath for uint; using SafeDecimalMath for uint; - uint internal _cachedDebt; + int internal _cachedDebt; mapping(bytes32 => uint) internal _cachedSynthDebt; mapping(bytes32 => uint) internal _excludedIssuedDebt; uint internal _cacheTimestamp; @@ -111,7 +111,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache { return getDebtSnapshotStaleTime(); } - function cachedDebt() external view returns (uint) { + function cachedDebt() external view returns (int) { return _cachedDebt; } @@ -316,7 +316,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache { external view returns ( - uint debt, + int debt, uint timestamp, bool isInvalid, bool isStale diff --git a/contracts/DebtCache.sol b/contracts/DebtCache.sol index be465a6985..fbcc179bbd 100644 --- a/contracts/DebtCache.sol +++ b/contracts/DebtCache.sol @@ -41,10 +41,10 @@ contract DebtCache is BaseDebtCache { // Subtract out the excluded non-SNX backed debt from our total _cachedSynthDebt[EXCLUDED_DEBT_KEY] = excludedDebt; - uint newDebt = snxCollateralDebt.floorsub(excludedDebt); - _cachedDebt = newDebt; + uint newDebt = snxCollateralDebt - excludedDebt; + _cachedDebt = int(newDebt); _cacheTimestamp = block.timestamp; - emit DebtCacheUpdated(newDebt); + emit DebtCacheUpdated(int(newDebt)); emit DebtCacheSnapshotTaken(block.timestamp); // (in)validate the cache if necessary @@ -87,11 +87,10 @@ contract DebtCache is BaseDebtCache { uint delta = SafeDecimalMath.abs(amount); if (amount > 0) { _cachedSynthDebt[sUSD] = _cachedSynthDebt[sUSD].add(delta); - _cachedDebt = _cachedDebt.add(delta); } else { _cachedSynthDebt[sUSD] = _cachedSynthDebt[sUSD].sub(delta); - _cachedDebt = _cachedDebt.sub(delta); } + _cachedDebt = _cachedDebt + amount; emit DebtCacheUpdated(_cachedDebt); } @@ -131,10 +130,10 @@ contract DebtCache is BaseDebtCache { // Apply the debt update. if (cachedSum != currentSum) { - uint debt = _cachedDebt; + int debt = _cachedDebt; // apply the delta between the cachedSum and currentSum // add currentSum before sub cachedSum to prevent overflow as cachedSum > debt for large amount of excluded debt - debt = debt.add(currentSum).sub(cachedSum); + debt = debt + int(currentSum) - int(cachedSum); _cachedDebt = debt; emit DebtCacheUpdated(debt); } @@ -147,7 +146,7 @@ contract DebtCache is BaseDebtCache { /* ========== EVENTS ========== */ - event DebtCacheUpdated(uint cachedDebt); + event DebtCacheUpdated(int cachedDebt); event DebtCacheSnapshotTaken(uint timestamp); event DebtCacheValidityChanged(bool indexed isInvalid); } diff --git a/contracts/interfaces/IDebtCache.sol b/contracts/interfaces/IDebtCache.sol index 1c18056c88..faf0e5cc6f 100644 --- a/contracts/interfaces/IDebtCache.sol +++ b/contracts/interfaces/IDebtCache.sol @@ -5,7 +5,7 @@ import "./IIssuer.sol"; interface IDebtCache { // Views - function cachedDebt() external view returns (uint); + function cachedDebt() external view returns (int); function cachedSynthDebt(bytes32 currencyKey) external view returns (uint); @@ -37,7 +37,7 @@ interface IDebtCache { external view returns ( - uint debt, + int debt, uint timestamp, bool isInvalid, bool isStale