mirror of
https://github.com/vacp2p/rln-contract.git
synced 2025-02-27 05:40:36 +00:00
chore: added pagination for commitments
This commit is contained in:
parent
caa3d3b2cc
commit
9d32b4444a
@ -40,6 +40,9 @@ error InsufficientContractBalance();
|
||||
/// Invalid proof
|
||||
error InvalidProof();
|
||||
|
||||
/// Invalid pagination query
|
||||
error InvalidPaginationQuery(uint256 startIndex, uint256 endIndex);
|
||||
|
||||
abstract contract RlnBase {
|
||||
/// @notice The Field
|
||||
uint256 public constant Q = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
|
||||
@ -64,6 +67,9 @@ abstract contract RlnBase {
|
||||
/// maps from idCommitment to their index in the set
|
||||
mapping(uint256 => uint256) public members;
|
||||
|
||||
/// @notice the index to commitment mapping
|
||||
mapping(uint256 => uint256) public indexToCommitment;
|
||||
|
||||
/// @notice The membership status of each member
|
||||
mapping(uint256 => bool) public memberExists;
|
||||
|
||||
@ -121,6 +127,7 @@ abstract contract RlnBase {
|
||||
if (idCommitmentIndex >= SET_SIZE) revert FullTree();
|
||||
|
||||
members[idCommitment] = idCommitmentIndex;
|
||||
indexToCommitment[idCommitmentIndex] = idCommitment;
|
||||
memberExists[idCommitment] = true;
|
||||
BinaryIMT.insert(imtData, idCommitment);
|
||||
stakedAmounts[idCommitment] = stake;
|
||||
@ -167,6 +174,7 @@ abstract contract RlnBase {
|
||||
// delete member
|
||||
uint256 index = members[idCommitment];
|
||||
members[idCommitment] = 0;
|
||||
indexToCommitment[index] = 0;
|
||||
memberExists[idCommitment] = false;
|
||||
stakedAmounts[idCommitment] = 0;
|
||||
// TODO: remove from IMT
|
||||
@ -218,4 +226,15 @@ abstract contract RlnBase {
|
||||
function root() external view returns (uint256) {
|
||||
return imtData.root;
|
||||
}
|
||||
|
||||
function getCommitments(uint256 startIndex, uint256 endIndex) public view returns (uint256[] memory) {
|
||||
if (startIndex >= endIndex) revert InvalidPaginationQuery(startIndex, endIndex);
|
||||
if (endIndex > idCommitmentIndex) revert InvalidPaginationQuery(startIndex, endIndex);
|
||||
|
||||
uint256[] memory commitments = new uint256[](endIndex - startIndex);
|
||||
for (uint256 i = startIndex; i < endIndex; i++) {
|
||||
commitments[i - startIndex] = indexToCommitment[i];
|
||||
}
|
||||
return commitments;
|
||||
}
|
||||
}
|
||||
|
@ -125,6 +125,14 @@ error InvalidProof()
|
||||
|
||||
Invalid proof
|
||||
|
||||
## InvalidPaginationQuery
|
||||
|
||||
```solidity
|
||||
error InvalidPaginationQuery(uint256 startIndex, uint256 endIndex)
|
||||
```
|
||||
|
||||
Invalid pagination query
|
||||
|
||||
## RlnBase
|
||||
|
||||
### Q
|
||||
@ -185,6 +193,14 @@ mapping(uint256 => uint256) members
|
||||
The membership status of each member
|
||||
maps from idCommitment to their index in the set
|
||||
|
||||
### indexToCommitment
|
||||
|
||||
```solidity
|
||||
mapping(uint256 => uint256) indexToCommitment
|
||||
```
|
||||
|
||||
the index to commitment mapping
|
||||
|
||||
### memberExists
|
||||
|
||||
```solidity
|
||||
@ -371,6 +387,12 @@ _Groth16 proof verification_
|
||||
function root() external view returns (uint256)
|
||||
```
|
||||
|
||||
### getCommitments
|
||||
|
||||
```solidity
|
||||
function getCommitments(uint256 startIndex, uint256 endIndex) public view returns (uint256[])
|
||||
```
|
||||
|
||||
## Pairing
|
||||
|
||||
### G1Point
|
||||
|
@ -38,8 +38,8 @@ contract RlnTest is Test {
|
||||
vm.assume(rln.isValidCommitment(idCommitment));
|
||||
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
||||
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
|
||||
// assertEq(rln.memberExists(idCommitment), true);
|
||||
// assertEq(rln.members(idCommitment), 0);
|
||||
assertEq(rln.memberExists(idCommitment), true);
|
||||
assertEq(rln.members(idCommitment), 0);
|
||||
}
|
||||
|
||||
function test__InvalidRegistration__DuplicateCommitment(uint256 idCommitment) public {
|
||||
@ -220,4 +220,30 @@ contract RlnTest is Test {
|
||||
|
||||
assertEq(rln.root(), 5210724218081541877101688952118136930297124697603087561558225712176057209122);
|
||||
}
|
||||
|
||||
function test__paginationCommitments() public {
|
||||
uint256[] memory idCommitments = new uint256[](10);
|
||||
idCommitments[0] = 19143711682366759980911001457853255795836264632723844153354310748778748156460;
|
||||
idCommitments[1] = 16984765328852711772291441487727981184905800779020079168989152080434188364678;
|
||||
idCommitments[2] = 10972315136095845343447418815139813428649316683283020632475608655814722712541;
|
||||
idCommitments[3] = 2709631781045191277266130708832884002577134582503944059038971337978087532997;
|
||||
idCommitments[4] = 8255654132980945447086418574686169461187805238257784695584517016324877809505;
|
||||
idCommitments[5] = 20291701150251695209910387548168084091751201746043024067531503187703236470983;
|
||||
idCommitments[6] = 11817872986033932471261438074921403500882957864164537515599299873089437746577;
|
||||
idCommitments[7] = 18475838919635792169148272767721284591038756730004222133003018558598315558783;
|
||||
idCommitments[8] = 10612118277928165031660389522171737855229037400929675201853245490188277695983;
|
||||
idCommitments[9] = 17318633845296358766427229711888486415250435256643711009388405482885762601797;
|
||||
|
||||
vm.pauseGasMetering();
|
||||
for (uint256 i = 0; i < idCommitments.length; i++) {
|
||||
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitments[i]);
|
||||
}
|
||||
vm.resumeGasMetering();
|
||||
|
||||
uint256[] memory commitments = rln.getCommitments(0, 10);
|
||||
assertEq(commitments.length, 10);
|
||||
for (uint256 i = 0; i < commitments.length; i++) {
|
||||
assertEq(commitments[i], idCommitments[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user