fix: refactor, reuse function for memberExists

This commit is contained in:
rymnc 2024-05-24 14:42:05 +05:30
parent 9042b8be35
commit dda2affdbe
No known key found for this signature in database
GPG Key ID: AAA088D5C68ECD34
2 changed files with 39 additions and 32 deletions

View File

@ -82,12 +82,41 @@ contract WakuRlnV2 {
LazyIMT.init(imtData, DEPTH);
}
/// @notice Checks if a commitment is valid
/// @param idCommitment The idCommitment of the member
/// @return true if the commitment is valid, false otherwise
function isValidCommitment(uint256 idCommitment) public pure returns (bool) {
return idCommitment != 0 && idCommitment < Q;
}
/// @notice Checks if a user message limit is valid
/// @param userMessageLimit The user message limit
/// @return true if the user message limit is valid, false otherwise
function isValidUserMessageLimit(uint32 userMessageLimit) public view returns (bool) {
return userMessageLimit > 0 && userMessageLimit <= MAX_MESSAGE_LIMIT;
}
/// @notice Returns the rateCommitment of a member
/// @param index The index of the member
/// @return The rateCommitment of the member
function indexToCommitment(uint32 index) internal view returns (uint256) {
return imtData.elements[LazyIMT.indexForElement(0, index)];
}
/// @notice Returns the metadata of a member
/// @param idCommitment The idCommitment of the member
/// @return The metadata of the member (userMessageLimit, index, rateCommitment)
function idCommitmentToMetadata(uint256 idCommitment) public view returns (uint32, uint32, uint256) {
MembershipInfo memory member = memberInfo[idCommitment];
return (member.userMessageLimit, member.index, indexToCommitment(member.index));
}
/// @notice Checks if a member exists
/// @param idCommitment The idCommitment of the member
/// @return true if the member exists, false otherwise
function memberExists(uint256 idCommitment) public view returns (bool) {
MembershipInfo memory member = memberInfo[idCommitment];
return member.userMessageLimit > 0 && member.index >= 0;
(uint32 userMessageLimit, uint32 index, uint256 rateCommitment) = idCommitmentToMetadata(idCommitment);
return userMessageLimit > 0 && index >= 0 && rateCommitment != 0;
}
/// Allows a user to register as a member
@ -120,35 +149,6 @@ contract WakuRlnV2 {
idCommitmentIndex += 1;
}
/// @notice Checks if a commitment is valid
/// @param idCommitment The idCommitment of the member
/// @return true if the commitment is valid, false otherwise
function isValidCommitment(uint256 idCommitment) public pure returns (bool) {
return idCommitment != 0 && idCommitment < Q;
}
/// @notice Checks if a user message limit is valid
/// @param userMessageLimit The user message limit
/// @return true if the user message limit is valid, false otherwise
function isValidUserMessageLimit(uint32 userMessageLimit) public view returns (bool) {
return userMessageLimit > 0 && userMessageLimit <= MAX_MESSAGE_LIMIT;
}
/// @notice Returns the rateCommitment of a member
/// @param index The index of the member
/// @return The rateCommitment of the member
function indexToCommitment(uint32 index) public view returns (uint256) {
return imtData.elements[LazyIMT.indexForElement(0, index)];
}
/// @notice Returns the metadata of a member
/// @param idCommitment The idCommitment of the member
/// @return The metadata of the member (userMessageLimit, index, rateCommitment)
function idCommitmentToMetadata(uint256 idCommitment) public view returns (uint32, uint32, uint256) {
MembershipInfo memory member = memberInfo[idCommitment];
return (member.userMessageLimit, member.index, indexToCommitment(member.index));
}
/// @notice Returns the commitments of a range of members
/// @param startIndex The start index of the range
/// @param endIndex The end index of the range

View File

@ -38,7 +38,6 @@ contract WakuRlnV2Test is Test {
// kats from zerokit
uint256 rateCommitment =
4_699_387_056_273_519_054_140_667_386_511_343_037_709_699_938_246_587_880_795_929_666_834_307_503_001;
assertEq(w.indexToCommitment(0), rateCommitment);
uint256[] memory commitments = w.getCommitments(0, 1);
assertEq(commitments.length, 1);
assertEq(commitments[index], rateCommitment);
@ -71,6 +70,14 @@ contract WakuRlnV2Test is Test {
assertEq(fetchedRateCommitment, rateCommitment);
}
function test__IdCommitmentToMetadata__DoesntExist() external {
uint256 idCommitment = 2;
(uint32 userMessageLimit, uint32 index, uint256 rateCommitment) = w.idCommitmentToMetadata(idCommitment);
assertEq(userMessageLimit, 0);
assertEq(index, 0);
assertEq(rateCommitment, 0);
}
function test__InvalidRegistration__InvalidIdCommitment__Zero() external {
uint256 idCommitment = 0;
uint32 userMessageLimit = 2;