Skip to content

Commit

Permalink
fix(rln): use zero based indexing for commitments (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Aug 30, 2023
1 parent bd8403a commit 35f2182
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 7 additions & 3 deletions contracts/RlnBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract contract RlnBase {
uint256 public immutable SET_SIZE;

/// @notice The index of the next member to be registered
uint256 public idCommitmentIndex = 1;
uint256 public idCommitmentIndex = 0;

/// @notice The amount of eth staked by each member
/// maps from idCommitment to the amount staked
Expand All @@ -61,6 +61,8 @@ abstract contract RlnBase {
/// maps from idCommitment to their index in the set
mapping(uint256 => uint256) public members;

mapping(uint256 => bool) public memberExists;

/// @notice The balance of each user that can be withdrawn
mapping(address => uint256) public withdrawalBalance;

Expand Down Expand Up @@ -111,10 +113,11 @@ abstract contract RlnBase {
/// @param idCommitment The idCommitment of the member
/// @param stake The amount of eth staked by the member
function _register(uint256 idCommitment, uint256 stake) internal virtual {
if (members[idCommitment] != 0) revert DuplicateIdCommitment();
if (memberExists[idCommitment]) revert DuplicateIdCommitment();
if (idCommitmentIndex >= SET_SIZE) revert FullTree();

members[idCommitment] = idCommitmentIndex;
memberExists[idCommitment] = true;
stakedAmounts[idCommitment] = stake;

emit MemberRegistered(idCommitment, idCommitmentIndex);
Expand Down Expand Up @@ -144,7 +147,7 @@ abstract contract RlnBase {
revert InvalidReceiverAddress(receiver);
}

if (members[idCommitment] == 0) revert MemberNotRegistered(idCommitment);
if (memberExists[idCommitment] == false) revert MemberNotRegistered(idCommitment);
// check if member is registered
if (stakedAmounts[idCommitment] == 0) {
revert MemberHasNoStake(idCommitment);
Expand All @@ -159,6 +162,7 @@ abstract contract RlnBase {
// delete member
uint256 index = members[idCommitment];
members[idCommitment] = 0;
memberExists[idCommitment] = false;
stakedAmounts[idCommitment] = 0;

// refund deposit
Expand Down
10 changes: 6 additions & 4 deletions test/Rln.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ contract RlnTest is Test {
vm.assume(rln.isValidCommitment(idCommitment));
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
assertEq(rln.members(idCommitment), 1);
assertEq(rln.memberExists(idCommitment), true);
assertEq(rln.members(idCommitment), 0);
}

function test__InvalidRegistration__DuplicateCommitment(uint256 idCommitment) public {
vm.assume(rln.isValidCommitment(idCommitment));
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
assertEq(rln.members(idCommitment), 1);
assertEq(rln.memberExists(idCommitment), true);
assertEq(rln.members(idCommitment), 0);
vm.expectRevert(DuplicateIdCommitment.selector);
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
}
Expand All @@ -73,7 +75,7 @@ contract RlnTest is Test {
address(rln.poseidonHasher()),
address(rln.verifier())
);
uint256 setSize = tempRln.SET_SIZE() - 1;
uint256 setSize = tempRln.SET_SIZE();
for (uint256 i = 1; i <= setSize; i++) {
tempRln.register{value: MEMBERSHIP_DEPOSIT}(i);
}
Expand Down Expand Up @@ -141,7 +143,7 @@ contract RlnTest is Test {
assertEq(rln.members(idCommitment), 0);

// manually set members[idCommitment] to true using vm
stdstore.target(address(rln)).sig("members(uint256)").with_key(idCommitment).depth(0).checked_write(true);
stdstore.target(address(rln)).sig("memberExists(uint256)").with_key(idCommitment).depth(0).checked_write(true);

vm.expectRevert(abi.encodeWithSelector(MemberHasNoStake.selector, idCommitment));
rln.slash(idCommitment, to, zeroedProof);
Expand Down

0 comments on commit 35f2182

Please sign in to comment.