Skip to content

Commit

Permalink
refactor: Wrapped for loop iterators in unchecked blocks, also format…
Browse files Browse the repository at this point in the history
…ted with prettier.
  • Loading branch information
Zer0dot committed Mar 24, 2022
1 parent 56d6b28 commit 37ab8ce
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 11 deletions.
5 changes: 4 additions & 1 deletion contracts/core/LensHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,11 @@ contract LensHub is ILensHub, LensNFTBase, VersionedInitializable, LensMultiStat
whenNotPaused
{
bytes32[] memory dataHashes = new bytes32[](vars.datas.length);
for (uint256 i = 0; i < vars.datas.length; ++i) {
for (uint256 i = 0; i < vars.datas.length; ) {
dataHashes[i] = keccak256(vars.datas[i]);
unchecked {
++i;
}
}
_validateRecoveredAddress(
_calculateDigest(
Expand Down
15 changes: 12 additions & 3 deletions contracts/core/modules/follow/ApprovalFollowModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ contract ApprovalFollowModule is IFollowModule, FollowValidatorFollowModuleBase
address owner = IERC721(HUB).ownerOf(profileId);
if (msg.sender != owner) revert Errors.NotProfileOwner();

for (uint256 i = 0; i < addresses.length; ++i) {
for (uint256 i = 0; i < addresses.length; ) {
_approvedByProfileByOwner[owner][profileId][addresses[i]] = toApprove[i];
unchecked {
++i;
}
}

emit Events.FollowsApproved(owner, profileId, addresses, toApprove, block.timestamp);
Expand All @@ -63,8 +66,11 @@ contract ApprovalFollowModule is IFollowModule, FollowValidatorFollowModuleBase

if (data.length > 0) {
address[] memory addresses = abi.decode(data, (address[]));
for (uint256 i = 0; i < addresses.length; ++i) {
for (uint256 i = 0; i < addresses.length; ) {
_approvedByProfileByOwner[owner][profileId][addresses[i]] = true;
unchecked {
++i;
}
}
}
return data;
Expand Down Expand Up @@ -125,8 +131,11 @@ contract ApprovalFollowModule is IFollowModule, FollowValidatorFollowModuleBase
address[] calldata toCheck
) external view returns (bool[] memory) {
bool[] memory approved = new bool[](toCheck.length);
for (uint256 i = 0; i < toCheck.length; ++i) {
for (uint256 i = 0; i < toCheck.length; ) {
approved[i] = _approvedByProfileByOwner[profileOwner][profileId][toCheck[i]];
unchecked {
++i;
}
}
return approved;
}
Expand Down
1 change: 0 additions & 1 deletion contracts/interfaces/IFollowNFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,4 @@ interface IFollowNFT {
* @param blockNumber The block number to query the delegated supply at.
*/
function getDelegatedSupplyByBlockNumber(uint256 blockNumber) external returns (uint256);

}
1 change: 0 additions & 1 deletion contracts/libraries/Events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -489,5 +489,4 @@ library Events {
bool[] enabled,
uint256 timestamp
);

}
5 changes: 4 additions & 1 deletion contracts/libraries/InteractionLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ library InteractionLogic {
mapping(bytes32 => uint256) storage _profileIdByHandleHash
) external {
if (profileIds.length != followModuleDatas.length) revert Errors.ArrayMismatch();
for (uint256 i = 0; i < profileIds.length; ++i) {
for (uint256 i = 0; i < profileIds.length; ) {
string memory handle = _profileById[profileIds[i]].handle;
if (_profileIdByHandleHash[keccak256(bytes(handle))] != profileIds[i])
revert Errors.TokenDoesNotExist();
Expand Down Expand Up @@ -80,6 +80,9 @@ library InteractionLogic {
followModuleDatas[i]
);
}
unchecked {
++i;
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion contracts/libraries/ProfileTokenURILogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,14 @@ library ProfileTokenURILogic {
if (imageURIBytes.length == 0) {
return false;
}
for (uint256 i = 0; i < imageURIBytes.length; i++) {
for (uint256 i = 0; i < imageURIBytes.length; ) {
if (imageURIBytes[i] == '"') {
// Avoids embedding a user provided imageURI containing double-quotes to prevent injection attacks
return false;
}
unchecked {
++i;
}
}
return true;
}
Expand Down
5 changes: 4 additions & 1 deletion contracts/libraries/PublishingLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,15 @@ library PublishingLogic {
if (byteHandle.length == 0 || byteHandle.length > Constants.MAX_HANDLE_LENGTH)
revert Errors.HandleLengthInvalid();

for (uint256 i = 0; i < byteHandle.length; ++i) {
for (uint256 i = 0; i < byteHandle.length; ) {
if (
(byteHandle[i] < '0' ||
byteHandle[i] > 'z' ||
(byteHandle[i] > '9' && byteHandle[i] < 'a')) && byteHandle[i] != '.'
) revert Errors.HandleContainsInvalidCharacters();
unchecked {
++i;
}
}
}
}
7 changes: 5 additions & 2 deletions contracts/misc/LensPeripheryDataProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {Errors} from '../libraries/Errors.sol';
*
* @dev This is useful because it allows clients to filter out follow NFTs that were transferred to
* a recipient by another user (i.e. Not a mint) and not register them as "following" unless
* the recipient explicitly toggles the follow here.
* the recipient explicitly toggles the follow here.
*/
contract LensPeripheryDataProvider {
string public constant NAME = 'LensPeripheryDataProvider';
Expand Down Expand Up @@ -79,11 +79,14 @@ contract LensPeripheryDataProvider {
bool[] calldata enables
) internal {
if (profileIds.length != enables.length) revert Errors.ArrayMismatch();
for (uint256 i = 0; i < profileIds.length; ++i) {
for (uint256 i = 0; i < profileIds.length; ) {
address followNFT = HUB.getFollowNFT(profileIds[i]);
if (followNFT == address(0)) revert Errors.FollowInvalid();
if (!IERC721Time(address(HUB)).exists(profileIds[i])) revert Errors.TokenDoesNotExist();
if (IERC721Time(followNFT).balanceOf(follower) == 0) revert Errors.FollowInvalid();
unchecked {
++i;
}
}
emit Events.FollowsToggled(follower, profileIds, enables, block.timestamp);
}
Expand Down

0 comments on commit 37ab8ce

Please sign in to comment.