refactor: Refactored more logic to the GeneralLib.

This commit is contained in:
Peter Michael
2022-06-06 14:49:13 -04:00
parent bf83bf2160
commit 3061221a2c
4 changed files with 71 additions and 51 deletions

View File

@@ -11,14 +11,14 @@ import {DataTypes} from '../libraries/DataTypes.sol';
import {Errors} from '../libraries/Errors.sol';
import {GeneralLib} from '../libraries/GeneralLib.sol';
import {ProfileTokenURILogic} from '../libraries/ProfileTokenURILogic.sol';
import '../libraries/Constants.sol';
import {LensHubNFTBase} from './base/LensHubNFTBase.sol';
import {LensMultiState} from './base/LensMultiState.sol';
import {LensHubStorage} from './storage/LensHubStorage.sol';
import {VersionedInitializable} from '../upgradeability/VersionedInitializable.sol';
import {IERC721Enumerable} from '@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol';
import '../libraries/Constants.sol';
/**
* @title LensHub
* @author Lens Protocol
@@ -194,12 +194,7 @@ contract LensHub is
bytes calldata followModuleInitData
) external override whenNotPaused {
_validateCallerIsProfileOwner(profileId);
GeneralLib.setFollowModule(
profileId,
followModule,
followModuleInitData,
_profileById[profileId]
);
GeneralLib.setFollowModule(profileId, followModule, followModuleInitData);
}
/// @inheritdoc ILensHub
@@ -209,12 +204,6 @@ contract LensHub is
whenNotPaused
{
GeneralLib.setFollowModuleWithSig(vars);
GeneralLib.setFollowModule(
vars.profileId,
vars.followModule,
vars.followModuleInitData,
_profileById[vars.profileId]
);
}
/// @inheritdoc ILensHub
@@ -230,7 +219,6 @@ contract LensHub is
whenNotPaused
{
GeneralLib.setDispatcherWithSig(vars);
_setDispatcher(vars.profileId, vars.dispatcher);
}
/// @inheritdoc ILensHub

View File

@@ -13,19 +13,19 @@ import {DataTypes} from '../../libraries/DataTypes.sol';
* storage variables should be done solely at the bottom of this contract.
*/
abstract contract LensHubStorage {
mapping(address => bool) internal _profileCreatorWhitelisted; // Slot 13
mapping(address => bool) internal _followModuleWhitelisted; // Slot 14
mapping(address => bool) internal _collectModuleWhitelisted; // Slot 15
mapping(address => bool) internal _referenceModuleWhitelisted; // Slot 16
mapping(address => bool) internal _profileCreatorWhitelisted; // Slot 13
mapping(address => bool) internal _followModuleWhitelisted; // Slot 14
mapping(address => bool) internal _collectModuleWhitelisted; // Slot 15
mapping(address => bool) internal _referenceModuleWhitelisted; // Slot 16
mapping(uint256 => address) internal _dispatcherByProfile;
mapping(bytes32 => uint256) internal _profileIdByHandleHash;
mapping(uint256 => DataTypes.ProfileStruct) internal _profileById;
mapping(uint256 => mapping(uint256 => DataTypes.PublicationStruct)) internal _pubByIdByProfile;
mapping(uint256 => address) internal _dispatcherByProfile; // slot 17
mapping(bytes32 => uint256) internal _profileIdByHandleHash; // slot 18
mapping(uint256 => DataTypes.ProfileStruct) internal _profileById; // slot 19
mapping(uint256 => mapping(uint256 => DataTypes.PublicationStruct)) internal _pubByIdByProfile; // slot 20
mapping(address => uint256) internal _defaultProfileByAddress; // slot 21
mapping(address => uint256) internal _defaultProfileByAddress; // slot 21
uint256 internal _profileCounter;
address internal _governance; // slot 23
address internal _emergencyAdmin; // slot 24
uint256 internal _profileCounter; // slot 22
address internal _governance; // slot 23
address internal _emergencyAdmin; // slot 24
}

View File

@@ -15,18 +15,23 @@ uint16 constant MAX_PROFILE_IMAGE_URI_LENGTH = 6000;
// is equivalent to keccak256(NAME_SLOT) and is where the name string is stored
// if the length is greater than 31 bytes.
uint256 constant NAME_SLOT = 0;
uint256 constant TOKEN_DATA_MAPPING_SLOT = 2;
uint256 constant SIG_NONCES_MAPPING_SLOT = 10;
uint256 constant PROTOCOL_STATE_SLOT = 12;
uint256 constant PROFILE_CREATOR_WHITELIST_MAPPING_SLOT = 13;
uint256 constant FOLLOW_MODULE_WHITELIST_MAPPING_SLOT = 14;
uint256 constant COLLECT_MODULE_WHITELIST_MAPPING_SLOT = 15;
uint256 constant REFERENCE_MODULE_WHITELIST_MAPPING_SLOT = 16;
uint256 constant DISPATCHER_BY_PROFILE_MAPPING_SLOT = 17;
uint256 constant PROFILE_BY_ID_MAPPING_SLOT = 19;
uint256 constant DEFAULT_PROFILE_MAPPING_SLOT = 21;
uint256 constant GOVERNANCE_SLOT = 23;
uint256 constant EMERGENCY_ADMIN_SLOT = 24;
uint256 constant TOKEN_DATA_MAPPING_SLOT = 2;
uint256 constant NAME_SLOT_GT_31 = 0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563;
// Profile struct offsets
uint256 constant FOLLOW_MODULE_PROFILE_OFFSET = 1;
// We also store typehashes here
bytes32 constant EIP712_REVISION_HASH = keccak256('1');
bytes32 constant PERMIT_TYPEHASH = keccak256(
@@ -71,4 +76,3 @@ bytes32 constant FOLLOW_WITH_SIG_TYPEHASH = keccak256(
bytes32 constant COLLECT_WITH_SIG_TYPEHASH = keccak256(
'CollectWithSig(uint256 profileId,uint256 pubId,bytes data,uint256 nonce,uint256 deadline)'
);
// }

View File

@@ -28,6 +28,9 @@ import {InteractionHelpers} from './InteractionHelpers.sol';
* @dev The functions are external, so they are called from the hub via `delegateCall` under
* the hood. Furthermore, expected events are emitted from this library instead of from the
* hub to alleviate code size concerns.
*
* Note: The setDispatcher non-signature function was not migrated as it was more space-efficient
* to leave it in the hub.
*/
library GeneralLib {
/**
@@ -164,31 +167,13 @@ library GeneralLib {
* @param profileId The profile ID to set the follow module for.
* @param followModule The follow module to set for the given profile, if any.
* @param followModuleInitData The data to pass to the follow module for profile initialization.
* @param _profile The storage reference to the profile struct associated with the given profile ID.
*/
function setFollowModule(
uint256 profileId,
address followModule,
bytes calldata followModuleInitData,
DataTypes.ProfileStruct storage _profile
bytes calldata followModuleInitData
) external {
if (followModule != _profile.followModule) {
_profile.followModule = followModule;
}
bytes memory followModuleReturnData;
if (followModule != address(0))
followModuleReturnData = _initFollowModule(
profileId,
followModule,
followModuleInitData
);
emit Events.FollowModuleSet(
profileId,
followModule,
followModuleReturnData,
block.timestamp
);
_setFollowModule(profileId, followModule, followModuleInitData);
}
/**
@@ -491,7 +476,7 @@ library GeneralLib {
*/
function setFollowModuleWithSig(DataTypes.SetFollowModuleWithSigData calldata vars) external {
MetaTxHelpers.baseSetFollowModuleWithSig(vars);
// set follow module
_setFollowModule(vars.profileId, vars.followModule, vars.followModuleInitData);
}
/**
@@ -502,7 +487,15 @@ library GeneralLib {
*/
function setDispatcherWithSig(DataTypes.SetDispatcherWithSigData calldata vars) external {
MetaTxHelpers.baseSetDispatcherWithSig(vars);
// set dispatcher
uint256 profileId = vars.profileId;
address dispatcher = vars.dispatcher;
assembly {
mstore(0, profileId)
mstore(32, DISPATCHER_BY_PROFILE_MAPPING_SLOT)
let slot := keccak256(0, 64)
sstore(slot, dispatcher)
}
emit Events.DispatcherSet(profileId, dispatcher, block.timestamp);
}
/**
@@ -643,6 +636,41 @@ library GeneralLib {
emit Events.DefaultProfileSet(wallet, profileId, block.timestamp);
}
function _setFollowModule(
uint256 profileId,
address followModule,
bytes calldata followModuleInitData
) private {
address currentFollowModule;
uint256 slot;
assembly {
mstore(0, profileId)
mstore(32, PROFILE_BY_ID_MAPPING_SLOT)
slot := add(keccak256(0, 64), FOLLOW_MODULE_PROFILE_OFFSET)
currentFollowModule := sload(slot)
}
if (followModule != currentFollowModule) {
assembly {
sstore(slot, followModule)
}
}
bytes memory followModuleReturnData;
if (followModule != address(0))
followModuleReturnData = _initFollowModule(
profileId,
followModule,
followModuleInitData
);
emit Events.FollowModuleSet(
profileId,
followModule,
followModuleReturnData,
block.timestamp
);
}
function _initFollowModule(
uint256 profileId,
address followModule,