fix: Adapt code to support >128 reserves

This commit is contained in:
Lasse Herskind 2021-11-24 10:18:22 +00:00
parent c1ada1cb68
commit 2b883e4f43
3 changed files with 40 additions and 17 deletions

View File

@ -87,7 +87,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
_addressesProvider = provider; _addressesProvider = provider;
_maxStableRateBorrowSizePercent = 2500; _maxStableRateBorrowSizePercent = 2500;
_flashLoanPremiumTotal = 9; _flashLoanPremiumTotal = 9;
_maxNumberOfReserves = 128; _maxNumberOfReserves = UserConfiguration._maxReserves;
} }
/** /**

View File

@ -13,6 +13,9 @@ library UserConfiguration {
uint256 internal constant BORROWING_MASK = uint256 internal constant BORROWING_MASK =
0x5555555555555555555555555555555555555555555555555555555555555555; 0x5555555555555555555555555555555555555555555555555555555555555555;
uint256 internal constant _maxReserves = 256;
uint256 internal constant _indexCount = _maxReserves / 128 + ((_maxReserves % 128 > 0) ? 1 : 0);
/** /**
* @dev Sets if the user is borrowing the reserve identified by reserveIndex * @dev Sets if the user is borrowing the reserve identified by reserveIndex
* @param self The configuration object * @param self The configuration object
@ -24,9 +27,11 @@ library UserConfiguration {
uint256 reserveIndex, uint256 reserveIndex,
bool borrowing bool borrowing
) internal { ) internal {
require(reserveIndex < 128, Errors.UL_INVALID_INDEX); require(reserveIndex < _maxReserves, Errors.UL_INVALID_INDEX);
self.data = uint256 index = reserveIndex / 128;
(self.data & ~(1 << (reserveIndex * 2))) | reserveIndex = reserveIndex % 128;
self.data[index] =
(self.data[index] & ~(1 << (reserveIndex * 2))) |
(uint256(borrowing ? 1 : 0) << (reserveIndex * 2)); (uint256(borrowing ? 1 : 0) << (reserveIndex * 2));
} }
@ -41,9 +46,11 @@ library UserConfiguration {
uint256 reserveIndex, uint256 reserveIndex,
bool usingAsCollateral bool usingAsCollateral
) internal { ) internal {
require(reserveIndex < 128, Errors.UL_INVALID_INDEX); require(reserveIndex < _maxReserves, Errors.UL_INVALID_INDEX);
self.data = uint256 index = reserveIndex / 128;
(self.data & ~(1 << (reserveIndex * 2 + 1))) | reserveIndex = reserveIndex % 128;
self.data[index] =
(self.data[index] & ~(1 << (reserveIndex * 2 + 1))) |
(uint256(usingAsCollateral ? 1 : 0) << (reserveIndex * 2 + 1)); (uint256(usingAsCollateral ? 1 : 0) << (reserveIndex * 2 + 1));
} }
@ -57,8 +64,10 @@ library UserConfiguration {
DataTypes.UserConfigurationMap memory self, DataTypes.UserConfigurationMap memory self,
uint256 reserveIndex uint256 reserveIndex
) internal pure returns (bool) { ) internal pure returns (bool) {
require(reserveIndex < 128, Errors.UL_INVALID_INDEX); require(reserveIndex < _maxReserves, Errors.UL_INVALID_INDEX);
return (self.data >> (reserveIndex * 2)) & 3 != 0; uint256 index = reserveIndex / 128;
reserveIndex = reserveIndex % 128;
return (self.data[index] >> (reserveIndex * 2)) & 3 != 0;
} }
/** /**
@ -72,8 +81,10 @@ library UserConfiguration {
pure pure
returns (bool) returns (bool)
{ {
require(reserveIndex < 128, Errors.UL_INVALID_INDEX); require(reserveIndex < _maxReserves, Errors.UL_INVALID_INDEX);
return (self.data >> (reserveIndex * 2)) & 1 != 0; uint256 index = reserveIndex / 128;
reserveIndex = reserveIndex % 128;
return (self.data[index] >> (reserveIndex * 2)) & 1 != 0;
} }
/** /**
@ -87,8 +98,10 @@ library UserConfiguration {
pure pure
returns (bool) returns (bool)
{ {
require(reserveIndex < 128, Errors.UL_INVALID_INDEX); require(reserveIndex < _maxReserves, Errors.UL_INVALID_INDEX);
return (self.data >> (reserveIndex * 2 + 1)) & 1 != 0; uint256 index = reserveIndex / 128;
reserveIndex = reserveIndex % 128;
return (self.data[index] >> (reserveIndex * 2 + 1)) & 1 != 0;
} }
/** /**
@ -97,7 +110,12 @@ library UserConfiguration {
* @return True if the user has been borrowing any reserve, false otherwise * @return True if the user has been borrowing any reserve, false otherwise
**/ **/
function isBorrowingAny(DataTypes.UserConfigurationMap memory self) internal pure returns (bool) { function isBorrowingAny(DataTypes.UserConfigurationMap memory self) internal pure returns (bool) {
return self.data & BORROWING_MASK != 0; for (uint8 i = 0; i < _indexCount; i++) {
if (self.data[i] & BORROWING_MASK != 0) {
return true;
}
}
return false;
} }
/** /**
@ -106,6 +124,11 @@ library UserConfiguration {
* @return True if the user has been borrowing any reserve, false otherwise * @return True if the user has been borrowing any reserve, false otherwise
**/ **/
function isEmpty(DataTypes.UserConfigurationMap memory self) internal pure returns (bool) { function isEmpty(DataTypes.UserConfigurationMap memory self) internal pure returns (bool) {
return self.data == 0; for (uint8 i = 0; i < _indexCount; i++) {
if (self.data[i] != 0) {
return false;
}
}
return true;
} }
} }

View File

@ -42,7 +42,7 @@ library DataTypes {
} }
struct UserConfigurationMap { struct UserConfigurationMap {
uint256 data; uint256[2] data; // size is _maxReserves / 128 + ((_maxReserves % 128 > 0) ? 1 : 0), but need to be literal
} }
enum InterestRateMode {NONE, STABLE, VARIABLE} enum InterestRateMode {NONE, STABLE, VARIABLE}