waku-rlnv1-contract/test/WakuRlnRegistry.t.sol

118 lines
4.5 KiB
Solidity
Raw Normal View History

2023-08-03 18:25:13 +05:30
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import "../contracts/WakuRlnRegistry.sol";
2023-08-03 18:27:46 +05:30
import {PoseidonHasher} from "rln-contract/PoseidonHasher.sol";
2023-08-08 13:32:07 +05:30
import {DuplicateIdCommitment, FullTree} from "rln-contract/RlnBase.sol";
2023-08-22 10:44:43 +05:30
import {noDuplicate, noInvalidCommitment, isValidCommitment} from "./utils.sol";
2023-08-03 18:25:13 +05:30
import "forge-std/Test.sol";
import "forge-std/StdCheats.sol";
contract WakuRlnRegistryTest is Test {
2023-08-08 13:32:07 +05:30
using stdStorage for StdStorage;
2023-08-03 18:25:13 +05:30
WakuRlnRegistry public wakuRlnRegistry;
PoseidonHasher public poseidonHasher;
function setUp() public {
poseidonHasher = new PoseidonHasher();
wakuRlnRegistry = new WakuRlnRegistry(address(poseidonHasher));
}
function test__NewStorage() public {
wakuRlnRegistry.newStorage();
}
function test__RegisterStorage_BadIndex() public {
wakuRlnRegistry.registerStorage(address(new WakuRln(address(poseidonHasher), 0)));
address newStorage = address(new WakuRln(address(poseidonHasher), 0));
vm.expectRevert(IncompatibleStorageIndex.selector);
wakuRlnRegistry.registerStorage(newStorage);
}
function test__RegisterStorage_BadImpl() public {
address newStorage = address(new WakuRln(address(new PoseidonHasher()), 0));
vm.expectRevert(IncompatibleStorage.selector);
wakuRlnRegistry.registerStorage(newStorage);
}
function test__Register(uint256[] calldata commitments) public {
2023-08-22 10:44:43 +05:30
vm.assume(noInvalidCommitment(commitments, poseidonHasher.Q()));
2023-08-03 18:25:13 +05:30
vm.assume(noDuplicate(commitments));
wakuRlnRegistry.newStorage();
wakuRlnRegistry.register(commitments);
}
2023-08-08 13:32:07 +05:30
function test__InvalidRegistration_Duplicate(uint256[] calldata commitments) public {
2023-08-22 10:44:43 +05:30
vm.assume(noInvalidCommitment(commitments, poseidonHasher.Q()));
2023-08-03 18:25:13 +05:30
vm.assume(!noDuplicate(commitments));
wakuRlnRegistry.newStorage();
vm.expectRevert(DuplicateIdCommitment.selector);
wakuRlnRegistry.register(commitments);
}
function test__forceProgression() public {
wakuRlnRegistry.newStorage();
2023-08-03 18:25:13 +05:30
wakuRlnRegistry.newStorage();
wakuRlnRegistry.forceProgress();
assertEq(wakuRlnRegistry.usingStorageIndex(), 1);
assertEq(wakuRlnRegistry.nextStorageIndex(), 2);
2023-08-03 18:25:13 +05:30
}
2023-08-08 13:32:07 +05:30
function test__SingleRegistration(uint256 commitment) public {
2023-08-22 10:44:43 +05:30
vm.assume(isValidCommitment(commitment, poseidonHasher.Q()));
2023-08-08 13:32:07 +05:30
wakuRlnRegistry.newStorage();
wakuRlnRegistry.register(0, commitment);
}
function test__InvalidSingleRegistration__NoStorageContract(uint256 commitment) public {
wakuRlnRegistry.newStorage();
2023-08-22 10:44:43 +05:30
vm.assume(isValidCommitment(commitment, poseidonHasher.Q()));
2023-08-08 13:32:07 +05:30
vm.expectRevert(NoStorageContractAvailable.selector);
wakuRlnRegistry.register(1, commitment);
}
function test__InvalidSingleRegistration__Duplicate(uint256 commitment) public {
2023-08-22 10:44:43 +05:30
vm.assume(isValidCommitment(commitment, poseidonHasher.Q()));
2023-08-08 13:32:07 +05:30
wakuRlnRegistry.newStorage();
wakuRlnRegistry.register(0, commitment);
vm.expectRevert(DuplicateIdCommitment.selector);
wakuRlnRegistry.register(0, commitment);
}
function test__InvalidSingleRegistration__FullTree() public {
wakuRlnRegistry.newStorage();
WakuRln wakuRln = WakuRln(wakuRlnRegistry.storages(0));
2023-08-22 10:44:43 +05:30
uint256[] memory commitments = new uint256[](1);
vm.mockCallRevert(
address(wakuRln),
abi.encodeWithSignature("register(uint256[])", commitments),
abi.encodeWithSelector(FullTree.selector)
);
2023-08-08 13:32:07 +05:30
vm.expectRevert(FullTree.selector);
2023-08-22 10:44:43 +05:30
wakuRlnRegistry.register(0, commitments[0]);
2023-08-08 13:32:07 +05:30
}
function test__InvalidRegistration__NoStorageContract() public {
wakuRlnRegistry.newStorage();
2023-08-22 10:44:43 +05:30
WakuRln wakuRln = WakuRln(wakuRlnRegistry.storages(0));
2023-08-08 13:32:07 +05:30
2023-08-22 10:44:43 +05:30
uint256[] memory commitments = new uint256[](1);
vm.mockCallRevert(
address(wakuRln),
abi.encodeWithSignature("register(uint256[])", commitments),
abi.encodeWithSelector(FullTree.selector)
);
2023-08-08 13:32:07 +05:30
vm.expectRevert(NoStorageContractAvailable.selector);
wakuRlnRegistry.register(commitments);
}
function test__forceProgression__NoStorageContract() public {
vm.expectRevert(NoStorageContractAvailable.selector);
wakuRlnRegistry.forceProgress();
assertEq(wakuRlnRegistry.usingStorageIndex(), 0);
assertEq(wakuRlnRegistry.nextStorageIndex(), 0);
}
2023-08-03 18:25:13 +05:30
}