feat: waku rln registry contract to enable autosharding

This commit is contained in:
rymnc 2023-08-03 11:30:43 +05:30
parent 7ebffc61a7
commit 7827e719c7
No known key found for this signature in database
GPG Key ID: AAA088D5C68ECD34
4 changed files with 150 additions and 3 deletions

View File

@ -8,7 +8,11 @@ import {Ownable} from "openzeppelin-contracts/contracts/access/Ownable.sol";
error NotImplemented(); error NotImplemented();
contract WakuRln is Ownable, RlnBase { contract WakuRln is Ownable, RlnBase {
constructor(address _poseidonHasher) Ownable() RlnBase(0, 20, _poseidonHasher, address(0)) {} uint16 public immutable contractIndex;
constructor(address _poseidonHasher, uint16 _contractIndex) Ownable() RlnBase(0, 20, _poseidonHasher, address(0)) {
contractIndex = _contractIndex;
}
/// Registers a member /// Registers a member
/// @param idCommitment The idCommitment of the member /// @param idCommitment The idCommitment of the member

View File

@ -0,0 +1,57 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import {WakuRln} from "./WakuRln.sol";
import {IPoseidonHasher} from "rln-contract/PoseidonHasher.sol";
import {Ownable} from "openzeppelin-contracts/contracts/access/Ownable.sol";
error StorageAlreadyExists(address storageAddress);
error NoStorageContractAvailable();
error FailedToRegister(string reason);
contract WakuRlnRegistry is Ownable {
uint16 public currentStorageIndex;
mapping(uint16 => address) public storages;
uint16 public usingStorageIndex = 0;
IPoseidonHasher public immutable poseidonHasher;
event NewStorageContract(uint16 index, address storageAddress);
constructor(address _poseidonHasher) Ownable() {
poseidonHasher = IPoseidonHasher(_poseidonHasher);
}
function _insertIntoStorageMap(address storageAddress) internal {
storages[currentStorageIndex] = storageAddress;
emit NewStorageContract(currentStorageIndex, storageAddress);
currentStorageIndex += 1;
}
function registerStorage(address storageAddress) external onlyOwner {
if (storages[currentStorageIndex] != address(0)) revert StorageAlreadyExists(storageAddress);
_insertIntoStorageMap(storageAddress);
}
function newStorage() external onlyOwner {
WakuRln newStorageContract = new WakuRln(address(poseidonHasher), currentStorageIndex);
_insertIntoStorageMap(address(newStorageContract));
}
function register(uint256 commitment) external payable {
if (usingStorageIndex >= currentStorageIndex) revert NoStorageContractAvailable();
// iteratively check if the storage contract is full, and increment the usingStorageIndex if it is
while (true) {
try WakuRln(storages[usingStorageIndex]).register{value: msg.value}(commitment) {
break;
} catch Error(string memory reason) {
if (keccak256(abi.encodePacked(reason)) != keccak256(abi.encodePacked("FullTree()"))) {
revert FailedToRegister(reason);
}
usingStorageIndex += 1;
}
}
}
}

View File

@ -8,10 +8,16 @@ error NotImplemented()
## WakuRln ## WakuRln
### contractIndex
```solidity
uint16 contractIndex
```
### constructor ### constructor
```solidity ```solidity
constructor(address _poseidonHasher) public constructor(address _poseidonHasher, uint16 _contractIndex) public
``` ```
### \_register ### \_register
@ -85,3 +91,83 @@ function withdraw() external pure
``` ```
Allows a user to withdraw funds allocated to them upon slashing a member Allows a user to withdraw funds allocated to them upon slashing a member
## StorageAlreadyExists
```solidity
error StorageAlreadyExists(address storageAddress)
```
## NoStorageContractAvailable
```solidity
error NoStorageContractAvailable()
```
## FailedToRegister
```solidity
error FailedToRegister(string reason)
```
## WakuRlnRegistry
### currentStorageIndex
```solidity
uint16 currentStorageIndex
```
### storages
```solidity
mapping(uint16 => address) storages
```
### usingStorageIndex
```solidity
uint16 usingStorageIndex
```
### poseidonHasher
```solidity
contract IPoseidonHasher poseidonHasher
```
### NewStorageContract
```solidity
event NewStorageContract(uint16 index, address storageAddress)
```
### constructor
```solidity
constructor(address _poseidonHasher) public
```
### \_insertIntoStorageMap
```solidity
function _insertIntoStorageMap(address storageAddress) internal
```
### registerStorage
```solidity
function registerStorage(address storageAddress) external
```
### newStorage
```solidity
function newStorage() external
```
### register
```solidity
function register(uint256 commitment) external payable
```

View File

@ -33,7 +33,7 @@ contract WakuRlnTest is Test {
/// @dev Setup the testing environment. /// @dev Setup the testing environment.
function setUp() public { function setUp() public {
poseidon = new PoseidonHasher(); poseidon = new PoseidonHasher();
wakuRln = new WakuRln(address(poseidon)); wakuRln = new WakuRln(address(poseidon), 0);
} }
/// @dev Ensure that you can hash a value. /// @dev Ensure that you can hash a value.