2023-03-29 09:49:32 +00:00
|
|
|
// SPDX-License-Identifier: Unlicense
|
|
|
|
pragma solidity ^0.8.15;
|
|
|
|
|
|
|
|
import "../contracts/Rln.sol";
|
2023-05-26 08:01:05 +00:00
|
|
|
import "./Verifier.sol";
|
2023-03-29 09:49:32 +00:00
|
|
|
import "forge-std/Test.sol";
|
2023-03-29 11:48:00 +00:00
|
|
|
import "forge-std/StdCheats.sol";
|
2023-03-29 09:49:32 +00:00
|
|
|
import "forge-std/console.sol";
|
|
|
|
|
2023-07-26 10:42:37 +00:00
|
|
|
contract RlnTest is Test {
|
2023-03-29 11:48:00 +00:00
|
|
|
using stdStorage for StdStorage;
|
|
|
|
|
2023-11-30 17:29:56 +00:00
|
|
|
RLN public rln;
|
2023-05-26 08:01:05 +00:00
|
|
|
TrueVerifier public trueVerifier;
|
|
|
|
FalseVerifier public falseVerifier;
|
2023-03-29 09:49:32 +00:00
|
|
|
|
|
|
|
uint256 public constant MEMBERSHIP_DEPOSIT = 1000000000000000;
|
|
|
|
uint256 public constant DEPTH = 20;
|
|
|
|
uint256 public constant SET_SIZE = 1048576;
|
2023-05-26 08:01:05 +00:00
|
|
|
uint256[8] public zeroedProof = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
|
|
|
2023-03-29 09:49:32 +00:00
|
|
|
/// @dev Setup the testing environment.
|
|
|
|
function setUp() public {
|
2023-05-26 08:01:05 +00:00
|
|
|
trueVerifier = new TrueVerifier();
|
|
|
|
falseVerifier = new FalseVerifier();
|
2023-12-01 07:13:59 +00:00
|
|
|
rln = new RLN(MEMBERSHIP_DEPOSIT, DEPTH, address(trueVerifier));
|
2023-03-29 09:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @dev Ensure that you can hash a value.
|
|
|
|
function test__Constants() public {
|
|
|
|
assertEq(rln.MEMBERSHIP_DEPOSIT(), MEMBERSHIP_DEPOSIT);
|
|
|
|
assertEq(rln.DEPTH(), DEPTH);
|
|
|
|
assertEq(rln.SET_SIZE(), SET_SIZE);
|
2023-08-21 07:07:15 +00:00
|
|
|
assertEq(rln.deployedBlockNumber(), 1);
|
2023-03-29 09:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test__ValidRegistration(uint256 idCommitment) public {
|
2023-08-16 13:20:49 +00:00
|
|
|
vm.assume(rln.isValidCommitment(idCommitment));
|
2023-03-29 09:49:32 +00:00
|
|
|
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
|
2023-11-30 10:54:56 +00:00
|
|
|
// assertEq(rln.memberExists(idCommitment), true);
|
|
|
|
// assertEq(rln.members(idCommitment), 0);
|
2023-03-29 09:49:32 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 10:42:37 +00:00
|
|
|
function test__InvalidRegistration__DuplicateCommitment(uint256 idCommitment) public {
|
2023-08-16 13:20:49 +00:00
|
|
|
vm.assume(rln.isValidCommitment(idCommitment));
|
2023-03-29 09:49:32 +00:00
|
|
|
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
|
2023-08-30 16:47:07 +00:00
|
|
|
assertEq(rln.memberExists(idCommitment), true);
|
|
|
|
assertEq(rln.members(idCommitment), 0);
|
2023-03-30 12:00:54 +00:00
|
|
|
vm.expectRevert(DuplicateIdCommitment.selector);
|
2023-03-29 09:49:32 +00:00
|
|
|
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
}
|
|
|
|
|
2023-08-16 13:20:49 +00:00
|
|
|
function test__InvalidRegistration__InvalidIdCommitment(uint256 idCommitment) public {
|
|
|
|
vm.assume(!rln.isValidCommitment(idCommitment));
|
|
|
|
vm.expectRevert(abi.encodeWithSelector(InvalidIdCommitment.selector, idCommitment));
|
|
|
|
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
}
|
|
|
|
|
2023-07-26 10:42:37 +00:00
|
|
|
function test__InvalidRegistration__InsufficientDeposit(uint256 idCommitment) public {
|
2023-08-16 13:20:49 +00:00
|
|
|
vm.assume(rln.isValidCommitment(idCommitment));
|
2023-03-30 12:00:54 +00:00
|
|
|
uint256 badDepositAmount = MEMBERSHIP_DEPOSIT - 1;
|
2023-07-26 10:42:37 +00:00
|
|
|
vm.expectRevert(abi.encodeWithSelector(InsufficientDeposit.selector, MEMBERSHIP_DEPOSIT, badDepositAmount));
|
2023-03-30 12:00:54 +00:00
|
|
|
rln.register{value: badDepositAmount}(idCommitment);
|
2023-03-29 09:49:32 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 13:20:49 +00:00
|
|
|
function test__InvalidRegistration__FullSet() public {
|
2023-11-30 17:29:56 +00:00
|
|
|
RLN tempRln = new RLN(
|
2023-03-29 09:49:32 +00:00
|
|
|
MEMBERSHIP_DEPOSIT,
|
|
|
|
2,
|
2023-05-26 08:01:05 +00:00
|
|
|
address(rln.verifier())
|
2023-03-29 09:49:32 +00:00
|
|
|
);
|
2023-08-30 16:47:07 +00:00
|
|
|
uint256 setSize = tempRln.SET_SIZE();
|
2023-08-16 13:20:49 +00:00
|
|
|
for (uint256 i = 1; i <= setSize; i++) {
|
|
|
|
tempRln.register{value: MEMBERSHIP_DEPOSIT}(i);
|
2023-03-29 09:49:32 +00:00
|
|
|
}
|
|
|
|
assertEq(tempRln.idCommitmentIndex(), 4);
|
2023-05-23 06:54:58 +00:00
|
|
|
vm.expectRevert(FullTree.selector);
|
2023-08-16 13:20:49 +00:00
|
|
|
tempRln.register{value: MEMBERSHIP_DEPOSIT}(setSize + 1);
|
2023-03-29 09:49:32 +00:00
|
|
|
}
|
2023-03-29 11:48:00 +00:00
|
|
|
|
2023-05-26 08:01:05 +00:00
|
|
|
function test__ValidSlash(uint256 idCommitment, address payable to) public {
|
2023-03-29 11:48:00 +00:00
|
|
|
// avoid precompiles, etc
|
2023-03-29 12:21:48 +00:00
|
|
|
// TODO: wrap both of these in a single function
|
2023-03-29 11:48:00 +00:00
|
|
|
assumePayable(to);
|
2023-08-08 17:13:19 +00:00
|
|
|
assumeNotPrecompile(to);
|
2023-03-29 11:59:33 +00:00
|
|
|
vm.assume(to != address(0));
|
2023-08-16 13:20:49 +00:00
|
|
|
vm.assume(rln.isValidCommitment(idCommitment));
|
2023-03-29 11:48:00 +00:00
|
|
|
|
|
|
|
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
|
|
|
|
|
|
|
|
uint256 balanceBefore = to.balance;
|
2023-05-26 08:01:05 +00:00
|
|
|
rln.slash(idCommitment, to, zeroedProof);
|
2023-11-29 06:46:28 +00:00
|
|
|
assertEq(rln.withdrawalBalance(to), MEMBERSHIP_DEPOSIT);
|
2023-03-30 14:53:08 +00:00
|
|
|
vm.prank(to);
|
|
|
|
rln.withdraw();
|
2023-03-29 11:48:00 +00:00
|
|
|
assertEq(rln.stakedAmounts(idCommitment), 0);
|
2023-05-23 06:54:58 +00:00
|
|
|
assertEq(rln.members(idCommitment), 0);
|
2023-11-29 06:46:28 +00:00
|
|
|
assertEq(rln.withdrawalBalance(to), 0);
|
2023-03-29 11:48:00 +00:00
|
|
|
assertEq(to.balance, balanceBefore + MEMBERSHIP_DEPOSIT);
|
|
|
|
}
|
|
|
|
|
2023-03-30 14:53:08 +00:00
|
|
|
function test__InvalidSlash__ToZeroAddress() public {
|
2023-05-26 08:01:05 +00:00
|
|
|
uint256 idCommitment = 9014214495641488759237505126948346942972912379615652741039992445865937985820;
|
|
|
|
|
2023-03-29 11:48:00 +00:00
|
|
|
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
|
2023-07-26 10:42:37 +00:00
|
|
|
vm.expectRevert(abi.encodeWithSelector(InvalidReceiverAddress.selector, address(0)));
|
2023-05-26 08:01:05 +00:00
|
|
|
rln.slash(idCommitment, payable(address(0)), zeroedProof);
|
2023-03-29 11:48:00 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 14:53:08 +00:00
|
|
|
function test__InvalidSlash__ToRlnAddress() public {
|
2023-05-26 08:01:05 +00:00
|
|
|
uint256 idCommitment = 19014214495641488759237505126948346942972912379615652741039992445865937985820;
|
2023-03-29 11:48:00 +00:00
|
|
|
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
|
2023-07-26 10:42:37 +00:00
|
|
|
vm.expectRevert(abi.encodeWithSelector(InvalidReceiverAddress.selector, address(rln)));
|
2023-05-26 08:01:05 +00:00
|
|
|
rln.slash(idCommitment, payable(address(rln)), zeroedProof);
|
2023-03-29 11:48:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 13:20:49 +00:00
|
|
|
function test__InvalidSlash__MemberNotRegistered(uint256 idCommitment) public {
|
|
|
|
vm.assume(rln.isValidCommitment(idCommitment));
|
2023-07-26 10:42:37 +00:00
|
|
|
vm.expectRevert(abi.encodeWithSelector(MemberNotRegistered.selector, idCommitment));
|
2023-05-26 08:01:05 +00:00
|
|
|
rln.slash(idCommitment, payable(address(this)), zeroedProof);
|
2023-03-29 11:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// this shouldn't be possible, but just in case
|
2023-07-26 10:42:37 +00:00
|
|
|
function test__InvalidSlash__NoStake(uint256 idCommitment, address payable to) public {
|
2023-03-29 11:48:00 +00:00
|
|
|
// avoid precompiles, etc
|
|
|
|
assumePayable(to);
|
2023-08-08 17:13:19 +00:00
|
|
|
assumeNotPrecompile(to);
|
2023-03-29 11:59:33 +00:00
|
|
|
vm.assume(to != address(0));
|
2023-08-16 13:20:49 +00:00
|
|
|
vm.assume(rln.isValidCommitment(idCommitment));
|
2023-03-29 11:48:00 +00:00
|
|
|
|
|
|
|
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
|
|
|
|
|
2023-05-26 08:01:05 +00:00
|
|
|
rln.slash(idCommitment, to, zeroedProof);
|
2023-03-29 11:48:00 +00:00
|
|
|
assertEq(rln.stakedAmounts(idCommitment), 0);
|
2023-05-23 06:54:58 +00:00
|
|
|
assertEq(rln.members(idCommitment), 0);
|
2023-03-29 11:48:00 +00:00
|
|
|
|
|
|
|
// manually set members[idCommitment] to true using vm
|
2023-08-30 16:47:07 +00:00
|
|
|
stdstore.target(address(rln)).sig("memberExists(uint256)").with_key(idCommitment).depth(0).checked_write(true);
|
2023-07-26 10:42:37 +00:00
|
|
|
|
|
|
|
vm.expectRevert(abi.encodeWithSelector(MemberHasNoStake.selector, idCommitment));
|
2023-05-26 08:01:05 +00:00
|
|
|
rln.slash(idCommitment, to, zeroedProof);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test__InvalidSlash__InvalidProof() public {
|
|
|
|
uint256 idCommitment = 19014214495641488759237505126948346942972912379615652741039992445865937985820;
|
|
|
|
|
2023-11-30 17:29:56 +00:00
|
|
|
RLN tempRln = new RLN(
|
2023-05-26 08:01:05 +00:00
|
|
|
MEMBERSHIP_DEPOSIT,
|
|
|
|
2,
|
|
|
|
address(falseVerifier)
|
|
|
|
);
|
|
|
|
|
|
|
|
tempRln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
|
|
|
|
vm.expectRevert(InvalidProof.selector);
|
|
|
|
tempRln.slash(idCommitment, payable(address(this)), zeroedProof);
|
2023-03-29 11:48:00 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 14:53:08 +00:00
|
|
|
function test__InvalidWithdraw__InsufficientWithdrawalBalance() public {
|
|
|
|
vm.expectRevert(InsufficientWithdrawalBalance.selector);
|
|
|
|
rln.withdraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test__InvalidWithdraw__InsufficientContractBalance() public {
|
2023-05-26 08:01:05 +00:00
|
|
|
uint256 idCommitment = 19014214495641488759237505126948346942972912379615652741039992445865937985820;
|
2023-03-30 14:53:08 +00:00
|
|
|
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
|
2023-05-26 08:01:05 +00:00
|
|
|
rln.slash(idCommitment, payable(address(this)), zeroedProof);
|
2023-03-30 14:53:08 +00:00
|
|
|
assertEq(rln.stakedAmounts(idCommitment), 0);
|
2023-05-23 06:54:58 +00:00
|
|
|
assertEq(rln.members(idCommitment), 0);
|
2023-03-30 14:53:08 +00:00
|
|
|
|
|
|
|
vm.deal(address(rln), 0);
|
|
|
|
vm.expectRevert(InsufficientContractBalance.selector);
|
|
|
|
rln.withdraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test__ValidWithdraw(address payable to) public {
|
|
|
|
assumePayable(to);
|
2023-08-08 17:13:19 +00:00
|
|
|
assumeNotPrecompile(to);
|
2023-11-29 06:46:28 +00:00
|
|
|
vm.assume(to != address(0));
|
2023-03-30 14:53:08 +00:00
|
|
|
|
2023-05-26 08:01:05 +00:00
|
|
|
uint256 idCommitment = 19014214495641488759237505126948346942972912379615652741039992445865937985820;
|
2023-03-30 14:53:08 +00:00
|
|
|
|
|
|
|
rln.register{value: MEMBERSHIP_DEPOSIT}(idCommitment);
|
|
|
|
assertEq(rln.stakedAmounts(idCommitment), MEMBERSHIP_DEPOSIT);
|
2023-05-26 08:01:05 +00:00
|
|
|
rln.slash(idCommitment, to, zeroedProof);
|
2023-03-30 14:53:08 +00:00
|
|
|
assertEq(rln.stakedAmounts(idCommitment), 0);
|
2023-05-23 06:54:58 +00:00
|
|
|
assertEq(rln.members(idCommitment), 0);
|
2023-11-29 06:46:28 +00:00
|
|
|
assertEq(rln.memberExists(idCommitment), false);
|
2023-03-30 14:53:08 +00:00
|
|
|
|
|
|
|
vm.prank(to);
|
|
|
|
rln.withdraw();
|
|
|
|
assertEq(rln.withdrawalBalance(to), 0);
|
|
|
|
}
|
2023-11-29 06:46:28 +00:00
|
|
|
|
2023-11-30 17:27:41 +00:00
|
|
|
function test__root() 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();
|
2023-11-30 10:54:56 +00:00
|
|
|
|
2023-11-30 17:27:41 +00:00
|
|
|
assertEq(rln.root(), 5210724218081541877101688952118136930297124697603087561558225712176057209122);
|
2023-11-29 06:46:28 +00:00
|
|
|
}
|
2023-03-29 09:49:32 +00:00
|
|
|
}
|