Skip to content

Commit

Permalink
ability for cachedDebt to become negative
Browse files Browse the repository at this point in the history
to avoid subtraction overflow
  • Loading branch information
dbeal-eth committed Nov 7, 2024
1 parent ca82c8e commit b0e965a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
6 changes: 3 additions & 3 deletions contracts/BaseDebtCache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -316,7 +316,7 @@ contract BaseDebtCache is Owned, MixinSystemSettings, IDebtCache {
external
view
returns (
uint debt,
int debt,
uint timestamp,
bool isInvalid,
bool isStale
Expand Down
15 changes: 7 additions & 8 deletions contracts/DebtCache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
4 changes: 2 additions & 2 deletions contracts/interfaces/IDebtCache.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -37,7 +37,7 @@ interface IDebtCache {
external
view
returns (
uint debt,
int debt,
uint timestamp,
bool isInvalid,
bool isStale
Expand Down

0 comments on commit b0e965a

Please sign in to comment.