From 885b03fef4ff5c17c329285cdb3b6013f864c5d1 Mon Sep 17 00:00:00 2001 From: eboado Date: Tue, 1 Nov 2022 14:56:04 +0100 Subject: [PATCH 1/2] - Added optimization to skip updateState() on same timestamp. - Improved caching flow on next_ indexes --- .../protocol/libraries/logic/ReserveLogic.sol | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/contracts/protocol/libraries/logic/ReserveLogic.sol b/contracts/protocol/libraries/logic/ReserveLogic.sol index a946c3813..4ca939199 100644 --- a/contracts/protocol/libraries/logic/ReserveLogic.sol +++ b/contracts/protocol/libraries/logic/ReserveLogic.sol @@ -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); } /** @@ -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 if there is any income being produced if (reserveCache.currLiquidityRate != 0) { uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest( @@ -310,9 +316,6 @@ library ReserveLogic { reserve.variableBorrowIndex = reserveCache.nextVariableBorrowIndex.toUint128(); } } - - //solium-disable-next-line - reserve.lastUpdateTimestamp = uint40(block.timestamp); } /** @@ -330,8 +333,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; @@ -342,8 +346,10 @@ library ReserveLogic { reserveCache.reserveLastUpdateTimestamp = reserve.lastUpdateTimestamp; reserveCache.currScaledVariableDebt = reserveCache.nextScaledVariableDebt = IVariableDebtToken( - reserveCache.variableDebtTokenAddress - ).scaledTotalSupply(); + reserveCache + .variableDebtTokenAddress + ) + .scaledTotalSupply(); ( reserveCache.currPrincipalStableDebt, From bb8e2acd3af721994931d3512e5812b8e9055a8b Mon Sep 17 00:00:00 2001 From: eboado Date: Wed, 2 Nov 2022 08:43:16 +0100 Subject: [PATCH 2/2] Formatting fix