Skip to content

Commit

Permalink
Merge branch 'master' into fix/191-gas-optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Nov 30, 2021
2 parents fe77bbc + c72277f commit be23b11
Show file tree
Hide file tree
Showing 30 changed files with 345 additions and 117 deletions.
47 changes: 47 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
# Changelog

## [1.6.0](https://github.com/aave/aave-v3-core/compare/v1.5.2...v1.6.0) (2021-11-27)


### Features

* reduced the number of optimizer runs ([8562a91](https://github.com/aave/aave-v3-core/commit/8562a911d04ede756a703be60f985d1916805d46))
* reorganized PoolStorage for gas savings ([87776f7](https://github.com/aave/aave-v3-core/commit/87776f757d58ddf20b99cdce752e068602f38389))


### Bug Fixes

* Add `useATokens` param to `Repay` event ([beec3f8](https://github.com/aave/aave-v3-core/commit/beec3f86bf1e4024dc74583bb386851b3d212963))
* Add additional constraint to use `eModeAssetPrice` ([ec42295](https://github.com/aave/aave-v3-core/commit/ec422953ca60110d98aa3c7c8930f70d88d5b294))
* Add check to `Pool::initializer()` ([8f2b426](https://github.com/aave/aave-v3-core/commit/8f2b426962a11c3e289301a341bcdf170b3e763f))
* Add clean ups to VariableDebtToken test ([bc4f314](https://github.com/aave/aave-v3-core/commit/bc4f314c85ac59ea84244108e6fc6f7c22d3fb74))
* Add cleanup to StableDebtToken test ([d1eeaa5](https://github.com/aave/aave-v3-core/commit/d1eeaa51d19b1e45bfa564fb51e5bd69623450db))
* Add cleanups in eMode tests ([fbf80a8](https://github.com/aave/aave-v3-core/commit/fbf80a8092c5807a281ab95b6e54f85790dda9b4))
* Add extra input to MockPoolInherited test deployment ([e7944dc](https://github.com/aave/aave-v3-core/commit/e7944dc1f92ea6fada5a31031d3628c7682da0d7))
* Add minor gas optimization for executeFlashLoan ([0d737f5](https://github.com/aave/aave-v3-core/commit/0d737f5041649a27a2d97148e625e1d3b211d0e3))
* Add natspec comments to IncentivizedERC20 ([722a8e7](https://github.com/aave/aave-v3-core/commit/722a8e7b5c3a2e14f639e6491cb264b8d305508a))
* Add precision to debt ceiling comment ([3d43c02](https://github.com/aave/aave-v3-core/commit/3d43c02a8d014e33619ad5d375d1baeac4448cb8))
* Add test exploiting pricing issue ([3457fb8](https://github.com/aave/aave-v3-core/commit/3457fb829d094a45f6d056c9893ad0c3b1bef465))
* Add test for incorrect init of pool ([b3cebaf](https://github.com/aave/aave-v3-core/commit/b3cebaf6180340378e37257c6ac4e4adab444e2f))
* Bumped Node JS version of Dockerfile to 16 stable version. Update package-lock. ([f0c8787](https://github.com/aave/aave-v3-core/commit/f0c8787725ff3fc77b02cf92928f6e79207a93ff))
* Change visibility of `name()` in IncentivizedERC20 ([467a5c1](https://github.com/aave/aave-v3-core/commit/467a5c110552bf4ec71449cccc2bb8fd9201806b))
* Do multiplication before devision for `currentStableBorrowRate` ([ca177fb](https://github.com/aave/aave-v3-core/commit/ca177fbe5860ae360e4ebf8e93712fa84b1ab2e4))
* Fix typo on credit delegation test case ([0b6a65b](https://github.com/aave/aave-v3-core/commit/0b6a65bb109665b1d74bcdc5c5fd04c38dfa4721))
* Handle minor merge issue ([36488c9](https://github.com/aave/aave-v3-core/commit/36488c9e86a15bc66047ddf09505dbcf29f79324))
* Make `_addressesProvider` immutable in `Pool` ([b41feab](https://github.com/aave/aave-v3-core/commit/b41feabd504c8da527db7fc1e3867519b2c7334e))
* Make `_nonces` internal and add `nonces` getter ([f3d1817](https://github.com/aave/aave-v3-core/commit/f3d18176f6250ac255287acaf86aae8f04c22e77))
* Make `MAX_RESERVES_COUNT` constant ([04ced7f](https://github.com/aave/aave-v3-core/commit/04ced7fd5582af588ebdcf4d2f755db8998d1b75))
* MockPoolInherited wrong return value on `MAX_NUMBER_RESERVES` ([b724a73](https://github.com/aave/aave-v3-core/commit/b724a73790b8fcad67e33df10217da448a4cf953))
* Move `_nonces` to IncentivizedERC20 ([54eb024](https://github.com/aave/aave-v3-core/commit/54eb024bef45a8d77a5f856cf1866024d282da7e))
* Move list length check to front of `validateFlashloan` function ([f485be5](https://github.com/aave/aave-v3-core/commit/f485be5d9f87d85cef65d816e708741437586280))
* Pair `_avgStableBorrowRate` and `_totalSupplyTimestamp` ([b6c9372](https://github.com/aave/aave-v3-core/commit/b6c937292dd7bb2c91eec05940170ac902701a09))
* Refactor tokens, move domain separator function to IncentivizedERC20 ([c033f9d](https://github.com/aave/aave-v3-core/commit/c033f9d4d83c08f75b63b972ca02fca27d6afc45))
* Removal of unneeded struct ([262dc7a](https://github.com/aave/aave-v3-core/commit/262dc7a525b3e6794b96b904189696773e3787c5))
* Remove unneeded fields from `AvailableCollateralToLiquidateLocalVars` ([f9088b6](https://github.com/aave/aave-v3-core/commit/f9088b65a6b3a5c6875f0176f32bda99caf63ee9))
* setup npm registry without file ([91fdc99](https://github.com/aave/aave-v3-core/commit/91fdc99b02dffb4924c5078d894f2ca180d8e23c))
* Simplify `_getFirstAssetAsCollateralId()` ([dffc2f6](https://github.com/aave/aave-v3-core/commit/dffc2f63275c6d4be780dc4199adace662afb585))
* source setup env for coverage ([fd7de34](https://github.com/aave/aave-v3-core/commit/fd7de34862f678aeac426634ba48b1c0afd65f7d))
* Update `user` to `onBehalfOf` for VariabelDebtToken ([7ff840f](https://github.com/aave/aave-v3-core/commit/7ff840f36a12ae19372c1f83b9d2ee01ae30de5b))
* Update package.json ([49a8c39](https://github.com/aave/aave-v3-core/commit/49a8c39e6137f31aa7a4e05a6ca2556532ddab20))
* update v3 deploy dev dependency to latest deployment scripts ([2aa8f5c](https://github.com/aave/aave-v3-core/commit/2aa8f5c3364c518890a12366f4ed6c8747dfe4bc))
* update v3 dev dependencies ([9cb6a47](https://github.com/aave/aave-v3-core/commit/9cb6a47393d676d16bd928e8cce5f90db14892ad))
* Use cached value for asset unit instead of recomputation ([335927c](https://github.com/aave/aave-v3-core/commit/335927c0493772f68968feb0477d0fad348e5b59))

### [1.5.2](https://github.com/aave/aave-v3-core/compare/v1.5.1...v1.5.2) (2021-11-12)


Expand Down
4 changes: 3 additions & 1 deletion contracts/interfaces/IPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ interface IPool {
* @param user The beneficiary of the repayment, getting his debt reduced
* @param repayer The address of the user initiating the repay(), providing the funds
* @param amount The amount repaid
* @param useATokens True if the repayment is done using aTokens, `false` if done with underlying asset directly
**/
event Repay(
address indexed reserve,
address indexed user,
address indexed repayer,
uint256 amount
uint256 amount,
bool useATokens
);

/**
Expand Down
27 changes: 27 additions & 0 deletions contracts/mocks/helpers/MockPool.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.8.10;

import {Errors} from '../../protocol/libraries/helpers/Errors.sol';
import {IPoolAddressesProvider} from '../../interfaces/IPoolAddressesProvider.sol';

contract MockPool {
// Reserved storage space to avoid layout collisions.
uint256[100] private ______gap;
Expand Down Expand Up @@ -28,11 +31,35 @@ contract MockPool {
import {Pool} from '../../protocol/pool/Pool.sol';

contract MockPoolInherited is Pool {
uint256 internal _maxNumberOfReserves = 128;

function getRevision() internal pure override returns (uint256) {
return 0x3;
}

constructor(IPoolAddressesProvider provider) Pool(provider) {}

function setMaxNumberOfReserves(uint256 newMaxNumberOfReserves) public {
_maxNumberOfReserves = newMaxNumberOfReserves;
}

function MAX_NUMBER_RESERVES() public view override returns (uint256) {
return _maxNumberOfReserves;
}

function _addReserveToList(address asset) internal override {
uint256 reservesCount = _reservesCount;
require(reservesCount < _maxNumberOfReserves, Errors.P_NO_MORE_RESERVES_ALLOWED);
bool reserveAlreadyAdded = _reserves[asset].id != 0 || _reservesList[0] == asset;

if (!reserveAlreadyAdded) {
for (uint8 i = 0; i <= reservesCount; i++) {
if (_reservesList[i] == address(0)) {
_reserves[asset].id = i;
_reservesList[i] = asset;
_reservesCount = uint16(reservesCount + 1);
}
}
}
}
}
6 changes: 5 additions & 1 deletion contracts/mocks/tokens/MintableERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract MintableERC20 is IERC20WithPermit, ERC20 {
bytes32 public constant PERMIT_TYPEHASH =
keccak256('Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)');

mapping(address => uint256) public _nonces;
mapping(address => uint256) internal _nonces;

bytes32 public DOMAIN_SEPARATOR;

Expand Down Expand Up @@ -83,4 +83,8 @@ contract MintableERC20 is IERC20WithPermit, ERC20 {
_mint(account, value);
return true;
}

function nonces(address owner) public view virtual returns (uint256) {
return _nonces[owner];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ library ReserveConfiguration {
uint256 constant MAX_VALID_DEBT_CEILING = 1099511627775;

uint256 public constant DEBT_CEILING_DECIMALS = 2;
uint256 public constant MAX_RESERVES_COUNT = 128;

/**
* @notice Sets the Loan to Value of the reserve
Expand Down
14 changes: 7 additions & 7 deletions contracts/protocol/libraries/configuration/UserConfiguration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ library UserConfiguration {
bool borrowing
) internal {
unchecked {
require(reserveIndex < 128, Errors.UL_INVALID_INDEX);
require(reserveIndex < ReserveConfiguration.MAX_RESERVES_COUNT, Errors.UL_INVALID_INDEX);
self.data =
(self.data & ~(1 << (reserveIndex * 2))) |
(uint256(borrowing ? 1 : 0) << (reserveIndex * 2));
Expand All @@ -49,7 +49,7 @@ library UserConfiguration {
bool usingAsCollateral
) internal {
unchecked {
require(reserveIndex < 128, Errors.UL_INVALID_INDEX);
require(reserveIndex < ReserveConfiguration.MAX_RESERVES_COUNT, Errors.UL_INVALID_INDEX);
self.data =
(self.data & ~(1 << (reserveIndex * 2 + 1))) |
(uint256(usingAsCollateral ? 1 : 0) << (reserveIndex * 2 + 1));
Expand All @@ -67,7 +67,7 @@ library UserConfiguration {
uint256 reserveIndex
) internal pure returns (bool) {
unchecked {
require(reserveIndex < 128, Errors.UL_INVALID_INDEX);
require(reserveIndex < ReserveConfiguration.MAX_RESERVES_COUNT, Errors.UL_INVALID_INDEX);
return (self.data >> (reserveIndex * 2)) & 3 != 0;
}
}
Expand All @@ -84,7 +84,7 @@ library UserConfiguration {
returns (bool)
{
unchecked {
require(reserveIndex < 128, Errors.UL_INVALID_INDEX);
require(reserveIndex < ReserveConfiguration.MAX_RESERVES_COUNT, Errors.UL_INVALID_INDEX);
return (self.data >> (reserveIndex * 2)) & 1 != 0;
}
}
Expand All @@ -101,7 +101,7 @@ library UserConfiguration {
returns (bool)
{
unchecked {
require(reserveIndex < 128, Errors.UL_INVALID_INDEX);
require(reserveIndex < ReserveConfiguration.MAX_RESERVES_COUNT, Errors.UL_INVALID_INDEX);
return (self.data >> (reserveIndex * 2 + 1)) & 1 != 0;
}
}
Expand Down Expand Up @@ -205,9 +205,9 @@ library UserConfiguration {
uint256 id;

while ((firstCollateralPosition >>= 2) > 0) {
id += 2;
id += 1;
}
return id / 2;
return id;
}
}
}
8 changes: 4 additions & 4 deletions contracts/protocol/libraries/logic/BorrowLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ library BorrowLogic {
address indexed reserve,
address indexed user,
address indexed repayer,
uint256 amount
uint256 amount,
bool useATokens
);

event RebalanceStableBorrowRate(address indexed reserve, address indexed user);
Expand Down Expand Up @@ -230,7 +231,7 @@ library BorrowLogic {
IAToken(reserveCache.aTokenAddress).handleRepayment(msg.sender, paybackAmount);
}

emit Repay(params.asset, params.onBehalfOf, msg.sender, paybackAmount);
emit Repay(params.asset, params.onBehalfOf, msg.sender, paybackAmount, params.useATokens);

return paybackAmount;
}
Expand All @@ -241,6 +242,7 @@ library BorrowLogic {
address user
) external {
DataTypes.ReserveCache memory reserveCache = reserve.cache();
reserve.updateState(reserveCache);

IERC20 stableDebtToken = IERC20(reserveCache.stableDebtTokenAddress);
IERC20 variableDebtToken = IERC20(reserveCache.variableDebtTokenAddress);
Expand All @@ -255,8 +257,6 @@ library BorrowLogic {
reserveCache.aTokenAddress
);

reserve.updateState(reserveCache);

IStableDebtToken(address(stableDebtToken)).burn(user, stableDebt);

(, reserveCache.nextTotalStableDebt, reserveCache.nextAvgStableBorrowRate) = IStableDebtToken(
Expand Down
13 changes: 7 additions & 6 deletions contracts/protocol/libraries/logic/FlashLoanLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ library FlashLoanLogic {
for (vars.i = 0; vars.i < params.assets.length; vars.i++) {
vars.currentAsset = params.assets[vars.i];
vars.currentAmount = params.amounts[vars.i];
vars.currentATokenAddress = vars.aTokenAddresses[vars.i];
vars.currentAmountPlusPremium = vars.currentAmount + vars.totalPremiums[vars.i];
vars.currentPremiumToProtocol = params.amounts[vars.i].percentMul(
vars.flashloanPremiumToProtocol
);
vars.currentPremiumToLP = vars.totalPremiums[vars.i] - vars.currentPremiumToProtocol;

if (DataTypes.InterestRateMode(params.modes[vars.i]) == DataTypes.InterestRateMode.NONE) {
vars.currentATokenAddress = vars.aTokenAddresses[vars.i];
vars.currentAmountPlusPremium = vars.currentAmount + vars.totalPremiums[vars.i];
vars.currentPremiumToProtocol = vars.currentAmount.percentMul(
vars.flashloanPremiumToProtocol
);
vars.currentPremiumToLP = vars.totalPremiums[vars.i] - vars.currentPremiumToProtocol;

DataTypes.ReserveData storage reserve = reserves[vars.currentAsset];
DataTypes.ReserveCache memory reserveCache = reserve.cache();

Expand Down
3 changes: 2 additions & 1 deletion contracts/protocol/libraries/logic/GenericLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ library GenericLogic {
unchecked {
vars.assetUnit = 10**vars.decimals;
}
vars.assetPrice = vars.eModeAssetPrice > 0
vars.assetPrice = vars.eModeAssetPrice > 0 &&
params.userEModeCategory == vars.eModeAssetCategory
? vars.eModeAssetPrice
: IPriceOracleGetter(params.oracle).getAssetPrice(vars.currentReserveAddress);

Expand Down
2 changes: 0 additions & 2 deletions contracts/protocol/libraries/logic/LiquidationLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ library LiquidationLogic {
}

struct AvailableCollateralToLiquidateLocalVars {
uint256 userCompoundedBorrowBalance;
uint256 liquidationBonus;
uint256 collateralPrice;
uint256 debtAssetPrice;
uint256 maxCollateralToLiquidate;
Expand Down
4 changes: 2 additions & 2 deletions contracts/protocol/libraries/logic/ValidationLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ library ValidationLogic {
) *
params.amount;
unchecked {
vars.amountInBaseCurrency /= 10**vars.reserveDecimals;
vars.amountInBaseCurrency /= vars.assetUnit;
}

//add the current already borrowed amount to the amount requested to calculate the total collateral needed.
Expand Down Expand Up @@ -456,13 +456,13 @@ library ValidationLogic {
uint256[] memory amounts,
mapping(address => DataTypes.ReserveData) storage reservesData
) internal view {
require(assets.length == amounts.length, Errors.VL_INCONSISTENT_FLASHLOAN_PARAMS);
for (uint256 i = 0; i < assets.length; i++) {
DataTypes.ReserveConfigurationMap memory configuration = reservesData[assets[i]]
.configuration;
require(!configuration.getPaused(), Errors.VL_RESERVE_PAUSED);
require(configuration.getActive(), Errors.VL_NO_ACTIVE_RESERVE);
}
require(assets.length == amounts.length, Errors.VL_INCONSISTENT_FLASHLOAN_PARAMS);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions contracts/protocol/libraries/types/DataTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ library DataTypes {
//bit 58: borrowing is enabled
//bit 59: stable rate borrowing enabled
//bit 60: asset is paused
//bit 61-63: reserved
//bit 61: borrowing in isolation mode is enabled
//bit 62-63: reserved
//bit 64-79: reserve factor
//bit 80-115 borrow cap, borrowCap == 0 => disabled
//bit 116-151 supply cap, supplyCap == 0 => disabled
//bit 80-115 borrow cap, borrowCap == 0 => disabled
//bit 116-151 supply cap, supplyCap == 0 => disabled
//bit 80-115 borrow cap in whole tokens, borrowCap == 0 => no cap
//bit 116-151 supply cap in whole tokens, supplyCap == 0 => no cap
//bit 152-167 liquidation protocol fee
//bit 168-175 eMode category
//bit 176-211 unbacked mint cap, unbackedMintCap == 0 => disabled
//bit 212-251 debt ceiling
//bit 176-211 unbacked mint cap in whole tokens, unbackedMintCap == 0 => minting disabled
//bit 212-251 debt ceiling for isolation mode with (ReserveConfiguration::DEBT_CEILING_DECIMALS) decimals
//bit 252-255 unused

uint256 data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ contract DefaultReserveInterestRateStrategy is IReserveInterestRateStrategy {
} else {
vars.currentStableBorrowRate =
vars.currentStableBorrowRate +
_stableRateSlope1.rayMul(vars.borrowUtilizationRate.rayDiv(OPTIMAL_UTILIZATION_RATE));
_stableRateSlope1.rayMul(vars.borrowUtilizationRate).rayDiv(OPTIMAL_UTILIZATION_RATE);

vars.currentVariableBorrowRate =
_baseVariableBorrowRate +
Expand Down
20 changes: 13 additions & 7 deletions contracts/protocol/pool/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ contract Pool is VersionedInitializable, IPool, PoolStorage {
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;

uint256 public constant POOL_REVISION = 0x2;
IPoolAddressesProvider internal immutable _addressesProvider;

modifier onlyPoolConfigurator() {
_onlyPoolConfigurator();
Expand Down Expand Up @@ -79,19 +80,21 @@ contract Pool is VersionedInitializable, IPool, PoolStorage {
return POOL_REVISION;
}

constructor(IPoolAddressesProvider provider) {
_addressesProvider = provider;
}

/**
* @notice Initializes the Pool.
* @dev Function is invoked by the proxy contract when the Pool contract is added to the
* PoolAddressesProvider of the market.
* @dev Caching the address of the PoolAddressesProvider in order to reduce gas consumption
* on subsequent operations
* @param provider The address of the PoolAddressesProvider
**/
function initialize(IPoolAddressesProvider provider) external initializer {
_addressesProvider = provider;
require(provider == _addressesProvider, Errors.PC_INVALID_CONFIGURATION);
_maxStableRateBorrowSizePercent = 2500;
_flashLoanPremiumTotal = 9;
_maxNumberOfReserves = 128;
_flashLoanPremiumToProtocol = 0;
}

Expand Down Expand Up @@ -567,8 +570,8 @@ contract Pool is VersionedInitializable, IPool, PoolStorage {
}

/// @inheritdoc IPool
function MAX_NUMBER_RESERVES() public view override returns (uint256) {
return _maxNumberOfReserves;
function MAX_NUMBER_RESERVES() public view virtual override returns (uint256) {
return ReserveConfiguration.MAX_RESERVES_COUNT;
}

/// @inheritdoc IPool
Expand Down Expand Up @@ -701,10 +704,13 @@ contract Pool is VersionedInitializable, IPool, PoolStorage {
return _usersEModeCategory[user];
}

function _addReserveToList(address asset) internal {
function _addReserveToList(address asset) internal virtual {
uint256 reservesCount = _reservesCount;

require(reservesCount < _maxNumberOfReserves, Errors.P_NO_MORE_RESERVES_ALLOWED);
require(
reservesCount < MAX_NUMBER_RESERVES(),
Errors.P_NO_MORE_RESERVES_ALLOWED
);

bool reserveAlreadyAdded = _reserves[asset].id != 0 || _reservesList[0] == asset;

Expand Down
Loading

0 comments on commit be23b11

Please sign in to comment.