separate eraseMembership functions to user-focused (lazy) and admin-focused (tree cleanup)

This commit is contained in:
Sergei Tikhomirov 2024-10-03 12:35:25 +02:00
parent 018ba1c9d2
commit 96364ac87a
No known key found for this signature in database
GPG Key ID: 6A1F8ED9D6538027
2 changed files with 20 additions and 13 deletions

View File

@ -231,27 +231,34 @@ contract WakuRlnV2 is Initializable, Ownable2StepUpgradeable, UUPSUpgradeable, M
}
/// @notice Erase expired memberships or owned grace-period memberships
/// The user can select expired memberships offchain,
/// and proceed to erase them.
/// This function is also used by the holder to erase their own grace-period memberships.
/// The user can select expired memberships offchain, and proceed to erase them.
/// The holder can use this function to erase their own grace-period memberships.
/// The holder can then withdraw the deposited tokens.
/// @param idCommitments The list of idCommitments of the memberships to erase
/// @param eraseFromMembershipSet Indicates whether to erase the membership's rate commitment from the membership
/// set
function eraseMemberships(uint256[] calldata idCommitments) external {
_eraseMemberships(idCommitments, false);
}
/// @notice Erase expired memberships or owned grace-period memberships
/// Optionally, also erase rate commitment data from the membership set (clean-up).
/// Compared to eraseMemberships(idCommitments),
/// this fucntion decreases Merkle tree size and spends more gas (if eraseFromMembershipSet == true).
/// @param idCommitments The list of idCommitments of the memberships to erase
/// @param eraseFromMembershipSet Indicates whether to erase membership data from the membership set
function eraseMemberships(uint256[] calldata idCommitments, bool eraseFromMembershipSet) external {
_eraseMemberships(idCommitments, eraseFromMembershipSet);
}
/// @dev Erase memberships from the list of idCommitments
/// @param idCommitmentsToErase The idCommitments of memberships to erase from storage
/// @param eraseFromMembershipSet Indicates whether to erase membership data from the set
/// @param eraseFromMembershipSet Indicates whether to erase membership data from the membership set
function _eraseMemberships(uint256[] calldata idCommitmentsToErase, bool eraseFromMembershipSet) internal {
// eraseFromMembershipSet == true means full clean-up.
// Erase memberships from memberships array (free up the rate limit and index),
// and also erase the rate limit from the Merkle tree that represents the membership set (reduce the tree
// size).
// and erase the rate commitment from the membership set (reduce the Merkle tree size).
// eraseFromMembershipSet == false means lazy erasure.
// Only erase memberships from the membership array (consume less gas).
// Only erase memberships from the memberships array (consume less gas).
// Merkle tree data will be overwritten when the correspondind index is reused.
for (uint256 i = 0; i < idCommitmentsToErase.length; i++) {
// Erase the membership from the memberships array in contract storage

View File

@ -436,7 +436,7 @@ contract WakuRlnV2Test is Test {
// time travel to the moment we can erase all expired memberships
uint256 membershipExpirationTimestamp = w.membershipExpirationTimestamp(idCommitmentsLength);
vm.warp(membershipExpirationTimestamp);
w.eraseMemberships(commitmentsToErase, false);
w.eraseMemberships(commitmentsToErase);
// Verify that expired indices match what we expect
for (uint32 i = 0; i < idCommitmentsLength; i++) {
@ -508,7 +508,7 @@ contract WakuRlnV2Test is Test {
emit MembershipUpgradeable.MembershipExpired(commitmentsToErase[0], 0, 0);
vm.expectEmit(true, false, false, false); // only check the first parameter of the event (the idCommitment)
emit MembershipUpgradeable.MembershipExpired(commitmentsToErase[0], 0, 0);
w.eraseMemberships(commitmentsToErase, false);
w.eraseMemberships(commitmentsToErase);
address holder;
@ -525,7 +525,7 @@ contract WakuRlnV2Test is Test {
commitmentsToErase[0] = idCommitment;
commitmentsToErase[1] = idCommitment + 4;
vm.expectRevert(abi.encodeWithSelector(CannotEraseMembership.selector, idCommitment + 4));
w.eraseMemberships(commitmentsToErase, false);
w.eraseMemberships(commitmentsToErase);
}
function test__RemoveAllExpiredMemberships(uint32 idCommitmentsLength) external {
@ -556,7 +556,7 @@ contract WakuRlnV2Test is Test {
emit MembershipUpgradeable.MembershipExpired(i + 1, 0, 0);
}
w.eraseMemberships(commitmentsToErase, false);
w.eraseMemberships(commitmentsToErase);
// Erased memberships are gone!
for (uint256 i = 0; i < commitmentsToErase.length; i++) {
@ -588,7 +588,7 @@ contract WakuRlnV2Test is Test {
uint256[] memory commitmentsToErase = new uint256[](1);
commitmentsToErase[0] = idCommitment;
w.eraseMemberships(commitmentsToErase, false);
w.eraseMemberships(commitmentsToErase);
uint256 availableBalance = w.depositsToWithdraw(address(this), address(token));