fix: Tests fixed. In StorageLib: ownerOf moved to ProfileLib, getPublicationType and getContentURI moved to PublicationLib, unsafeOwner removed.

Co-authored-by: Alan <donosonaumczuk@gmail.com>
This commit is contained in:
vicnaum
2023-02-23 17:27:25 +00:00
parent 219df94247
commit 41f4b00ec9
6 changed files with 56 additions and 51 deletions

View File

@@ -28,6 +28,7 @@ contract FollowNFT is HubRestricted, LensNFTBase, ERC2981CollectionRoyalties, IF
bool private _initialized;
// Introduced in v2
mapping(uint256 => FollowData) internal _followDataByFollowTokenId;
mapping(uint256 => uint256) internal _followTokenIdByFollowerProfileId;
mapping(uint256 => uint256) internal _followApprovalByFollowTokenId;

View File

@@ -694,7 +694,7 @@ contract LensHub is LensNFTBase, VersionedInitializable, LensMultiState, LensHub
/// @inheritdoc ILensHub
function getContentURI(uint256 profileId, uint256 pubId) external view override returns (string memory) {
// This function is used by the Collect NFTs' tokenURI function.
return StorageLib.getContentURI(profileId, pubId);
return PublicationLib.getContentURI(profileId, pubId);
}
/// @inheritdoc ILensHub
@@ -714,7 +714,7 @@ contract LensHub is LensNFTBase, VersionedInitializable, LensMultiState, LensHub
override
returns (Types.PublicationType)
{
return StorageLib.getPublicationType(profileId, pubId);
return PublicationLib.getPublicationType(profileId, pubId);
}
/// @inheritdoc ILensHub

View File

@@ -13,6 +13,14 @@ import {IFollowNFT} from 'contracts/interfaces/IFollowNFT.sol';
library ProfileLib {
uint16 constant MAX_PROFILE_IMAGE_URI_LENGTH = 6000;
function ownerOf(uint256 profileId) internal view returns (address) {
address profileOwner = StorageLib.getTokenData(profileId).owner;
if (profileOwner == address(0)) {
revert Errors.TokenDoesNotExist();
}
return profileOwner;
}
/**
* @notice Creates a profile with the given parameters to the given address. Minting happens
* in the hub.

View File

@@ -168,6 +168,41 @@ library PublicationLib {
return pubIdAssigned;
}
function getPublicationType(uint256 profileId, uint256 pubId) internal view returns (Types.PublicationType) {
Types.Publication storage _publication = StorageLib.getPublication(profileId, pubId);
Types.PublicationType pubType = _publication.pubType;
if (uint8(pubType) == 0) {
// If publication type is 0, we check using the legacy rules.
if (_publication.pointedProfileId != 0) {
// It is pointing to a publication, so it can be either a comment or a mirror, depending on if it has a
// collect module or not.
if (_publication.collectModule == address(0)) {
return Types.PublicationType.Mirror;
} else {
return Types.PublicationType.Comment;
}
} else if (_publication.collectModule != address(0)) {
return Types.PublicationType.Post;
}
}
return pubType;
}
function getContentURI(uint256 profileId, uint256 pubId) internal view returns (string memory) {
Types.Publication storage _publication = StorageLib.getPublication(profileId, pubId);
Types.PublicationType pubType = _publication.pubType;
if (pubType == Types.PublicationType.Nonexistent) {
pubType = getPublicationType(profileId, pubId);
}
if (pubType == Types.PublicationType.Mirror) {
uint256 rootProfileId = _publication.pointedProfileId;
uint256 rootPubId = _publication.pointedPubId;
return StorageLib.getPublication(rootProfileId, rootPubId).contentURI;
} else {
return StorageLib.getPublication(profileId, pubId).contentURI;
}
}
function _emitQuoteEvent(
Types.QuoteParams calldata quoteParams,
uint256 pubIdAssigned,
@@ -327,7 +362,7 @@ library PublicationLib {
revert(add(err, 32), length)
}
}
if (transactionExecutor != StorageLib.unsafeOwnerOf(commentParams.profileId)) {
if (transactionExecutor != StorageLib.getTokenData(commentParams.profileId).owner) {
// TODO: WTF is this?
revert Errors.ExecutorInvalid();
}
@@ -370,7 +405,7 @@ library PublicationLib {
revert(add(err, 32), length)
}
}
if (transactionExecutor != StorageLib.unsafeOwnerOf(quoteParams.profileId)) {
if (transactionExecutor != StorageLib.getTokenData(quoteParams.profileId).owner) {
// TODO: WTF is this?
revert Errors.ExecutorInvalid();
}
@@ -413,7 +448,7 @@ library PublicationLib {
revert(add(err, 32), length)
}
}
if (transactionExecutor != StorageLib.unsafeOwnerOf(mirrorParams.profileId)) {
if (transactionExecutor != StorageLib.getTokenData(mirrorParams.profileId).owner) {
// TODO: WTF is this?
revert Errors.ExecutorInvalid();
}

View File

@@ -43,41 +43,6 @@ library StorageLib {
}
}
function getPublicationType(uint256 profileId, uint256 pubId) internal view returns (Types.PublicationType) {
Types.Publication storage _publication = getPublication(profileId, pubId);
Types.PublicationType pubType = _publication.pubType;
if (uint8(pubType) == 0) {
// If publication type is 0, we check using the legacy rules.
if (_publication.pointedProfileId != 0) {
// It is pointing to a publication, so it can be either a comment or a mirror, depending on if it has a
// collect module or not.
if (_publication.collectModule == address(0)) {
return Types.PublicationType.Mirror;
} else {
return Types.PublicationType.Comment;
}
} else if (_publication.collectModule != address(0)) {
return Types.PublicationType.Post;
}
}
return pubType;
}
function getContentURI(uint256 profileId, uint256 pubId) internal view returns (string memory) {
Types.Publication storage _publication = getPublication(profileId, pubId);
Types.PublicationType pubType = _publication.pubType;
if (pubType == Types.PublicationType.Nonexistent) {
pubType = getPublicationType(profileId, pubId);
}
if (pubType == Types.PublicationType.Mirror) {
uint256 rootProfileId = _publication.pointedProfileId;
uint256 rootPubId = _publication.pointedPubId;
return getPublication(rootProfileId, rootPubId).contentURI;
} else {
return getPublication(profileId, pubId).contentURI;
}
}
function getProfile(uint256 profileId) internal pure returns (Types.Profile storage _profile) {
assembly {
mstore(0, profileId)
@@ -206,9 +171,4 @@ library StorageLib {
sstore(PROTOCOL_STATE_SLOT, newState)
}
}
// TODO: Try to get rid of this function.
function unsafeOwnerOf(uint256 tokenId) internal view returns (address) {
return getTokenData(tokenId).owner;
}
}

View File

@@ -6,6 +6,7 @@ import {Types} from 'contracts/libraries/constants/Types.sol';
import {Errors} from 'contracts/libraries/constants/Errors.sol';
import {StorageLib} from 'contracts/libraries/StorageLib.sol';
import {ProfileLib} from 'contracts/libraries/ProfileLib.sol';
import {PublicationLib} from 'contracts/libraries/PublicationLib.sol';
/**
* @title ValidationLib
@@ -14,14 +15,14 @@ import {ProfileLib} from 'contracts/libraries/ProfileLib.sol';
library ValidationLib {
function validatePointedPub(uint256 profileId, uint256 pubId) internal view {
// If it is pointing to itself it will fail because it will return non-existent type.
Types.PublicationType pointedPubType = StorageLib.getPublicationType(profileId, pubId);
Types.PublicationType pointedPubType = PublicationLib.getPublicationType(profileId, pubId);
if (pointedPubType == Types.PublicationType.Nonexistent || pointedPubType == Types.PublicationType.Mirror) {
revert Errors.InvalidPointedPub();
}
}
function validateAddressIsProfileOwner(address expectedProfileOwner, uint256 profileId) internal view {
if (expectedProfileOwner != StorageLib.unsafeOwnerOf(profileId)) {
if (expectedProfileOwner != ProfileLib.ownerOf(profileId)) {
revert Errors.NotProfileOwner();
}
}
@@ -30,7 +31,7 @@ library ValidationLib {
address expectedOwnerOrDelegatedExecutor,
uint256 profileId
) internal view {
if (expectedOwnerOrDelegatedExecutor != StorageLib.unsafeOwnerOf(profileId)) {
if (expectedOwnerOrDelegatedExecutor != ProfileLib.ownerOf(profileId)) {
validateAddressIsDelegatedExecutor({
expectedDelegatedExecutor: expectedOwnerOrDelegatedExecutor,
delegatorProfileId: profileId
@@ -78,7 +79,7 @@ library ValidationLib {
}
function validateProfileExists(uint256 profileId) internal view {
if (StorageLib.unsafeOwnerOf(profileId) == address(0)) {
if (StorageLib.getTokenData(profileId).owner == address(0)) {
revert Errors.TokenDoesNotExist();
}
}
@@ -101,7 +102,7 @@ library ValidationLib {
revert Errors.InvalidReferrer();
}
Types.PublicationType referrerPubType = StorageLib.getPublicationType(referrerProfileId, referrerPubId);
Types.PublicationType referrerPubType = PublicationLib.getPublicationType(referrerProfileId, referrerPubId);
if (referrerPubType == Types.PublicationType.Mirror) {
_validateReferrerAsMirror(referrerProfileId, referrerPubId, profileId, pubId);
@@ -145,7 +146,7 @@ library ValidationLib {
uint256 pubId
) private view {
Types.Publication storage _referrerPub = StorageLib.getPublication(referrerProfileId, referrerPubId);
Types.PublicationType typeOfPubPointedByReferrer = StorageLib.getPublicationType(profileId, pubId);
Types.PublicationType typeOfPubPointedByReferrer = PublicationLib.getPublicationType(profileId, pubId);
// We already know that the publication being collected/referenced is not a mirror nor a non-existent one.
if (typeOfPubPointedByReferrer == Types.PublicationType.Post) {
// If the publication collected/referenced is a post, the referrer comment/quote must have it as root.