Skip to content

Commit

Permalink
Merge pull request #725 from bgd-labs/feat/723-optimization
Browse files Browse the repository at this point in the history
Improving skip of updateState() by time, and caching dynamics for indexes
  • Loading branch information
miguelmtzinf authored Nov 14, 2022
2 parents cac49b4 + bab0eab commit 72e0425
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions contracts/protocol/libraries/logic/ReserveLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,17 @@ library ReserveLogic {
DataTypes.ReserveData storage reserve,
DataTypes.ReserveCache memory reserveCache
) internal {
// If time didn't pass since last stored timestamp, skip state update
//solium-disable-next-line
if (reserve.lastUpdateTimestamp == uint40(block.timestamp)) {
return;
}

_updateIndexes(reserve, reserveCache);
_accrueToTreasury(reserve, reserveCache);

//solium-disable-next-line
reserve.lastUpdateTimestamp = uint40(block.timestamp);
}

/**
Expand Down Expand Up @@ -283,9 +292,6 @@ library ReserveLogic {
DataTypes.ReserveData storage reserve,
DataTypes.ReserveCache memory reserveCache
) internal {
reserveCache.nextLiquidityIndex = reserveCache.currLiquidityIndex;
reserveCache.nextVariableBorrowIndex = reserveCache.currVariableBorrowIndex;

// Only cumulating on the supply side if there is any income being produced
// The case of Reserve Factor 100% is not a problem (currentLiquidityRate == 0),
// as liquidity index should not be updated
Expand Down Expand Up @@ -314,9 +320,6 @@ library ReserveLogic {
);
reserve.variableBorrowIndex = reserveCache.nextVariableBorrowIndex.toUint128();
}

//solium-disable-next-line
reserve.lastUpdateTimestamp = uint40(block.timestamp);
}

/**
Expand All @@ -334,8 +337,9 @@ library ReserveLogic {

reserveCache.reserveConfiguration = reserve.configuration;
reserveCache.reserveFactor = reserveCache.reserveConfiguration.getReserveFactor();
reserveCache.currLiquidityIndex = reserve.liquidityIndex;
reserveCache.currVariableBorrowIndex = reserve.variableBorrowIndex;
reserveCache.currLiquidityIndex = reserveCache.nextLiquidityIndex = reserve.liquidityIndex;
reserveCache.currVariableBorrowIndex = reserveCache.nextVariableBorrowIndex = reserve
.variableBorrowIndex;
reserveCache.currLiquidityRate = reserve.currentLiquidityRate;
reserveCache.currVariableBorrowRate = reserve.currentVariableBorrowRate;

Expand Down

0 comments on commit 72e0425

Please sign in to comment.