This commit is contained in:
alrevuelta 2023-11-27 18:35:47 +01:00
parent 19fded82bc
commit d136499243
No known key found for this signature in database
GPG Key ID: F345C9F3CCDB886E
4 changed files with 92 additions and 17 deletions

View File

@ -5,16 +5,25 @@ import {IPoseidonHasher} from "rln-contract/PoseidonHasher.sol";
import {RlnBase, DuplicateIdCommitment, FullTree, InvalidIdCommitment} from "rln-contract/RlnBase.sol"; import {RlnBase, DuplicateIdCommitment, FullTree, InvalidIdCommitment} from "rln-contract/RlnBase.sol";
import {Ownable} from "openzeppelin-contracts/contracts/access/Ownable.sol"; import {Ownable} from "openzeppelin-contracts/contracts/access/Ownable.sol";
import {LazyIMT, LazyIMTData} from "@zk-kit/imt.sol/LazyIMT.sol";
error NotImplemented(); error NotImplemented();
contract WakuRln is Ownable, RlnBase { contract WakuRln is Ownable, RlnBase {
uint16 public immutable contractIndex; uint16 public immutable contractIndex;
LazyIMTData tree;
using LazyIMT for LazyIMTData;
constructor( constructor(
address _poseidonHasher, address _poseidonHasher,
uint16 _contractIndex uint16 _contractIndex
) Ownable() RlnBase(0, 20, _poseidonHasher, address(0)) { ) Ownable() RlnBase(0, 20, _poseidonHasher, address(0)) {
contractIndex = _contractIndex; contractIndex = _contractIndex;
// TODO: This errors
uint8 merkleTreeDepth = 32;
tree.init(merkleTreeDepth);
} }
/// Registers a member /// Registers a member
@ -24,6 +33,7 @@ contract WakuRln is Ownable, RlnBase {
members[idCommitment] = idCommitmentIndex; members[idCommitment] = idCommitmentIndex;
memberExists[idCommitment] = true; memberExists[idCommitment] = true;
tree.insert(idCommitment);
emit MemberRegistered(idCommitment, idCommitmentIndex); emit MemberRegistered(idCommitment, idCommitmentIndex);
idCommitmentIndex += 1; idCommitmentIndex += 1;
@ -71,4 +81,20 @@ contract WakuRln is Ownable, RlnBase {
function withdraw() external pure override { function withdraw() external pure override {
revert NotImplemented(); revert NotImplemented();
} }
// TODO Some quick getter functions, unsure if needed
function merkleRoot() public view returns(uint256) {
return tree.root();
}
function numOfLeaves() public view returns(uint40) {
return tree.numberOfLeaves;
}
// Unsure how viable is to fetch elements one by one.
// Perhaps we just want the leafs and the client can reconstruct
// the hashes.
function getElement(uint256 elementIndex) public view returns(uint256) {
return tree.elements[elementIndex];
}
} }

View File

@ -14,13 +14,19 @@ error NotImplemented()
uint16 contractIndex uint16 contractIndex
``` ```
### tree
```solidity
struct LazyIMTData tree
```
### constructor ### constructor
```solidity ```solidity
constructor(address _poseidonHasher, uint16 _contractIndex) public constructor(address _poseidonHasher, uint16 _contractIndex) public
``` ```
### _register ### \_register
```solidity ```solidity
function _register(uint256 idCommitment) internal function _register(uint256 idCommitment) internal
@ -30,8 +36,8 @@ Registers a member
#### Parameters #### Parameters
| Name | Type | Description | | Name | Type | Description |
| ---- | ---- | ----------- | | ------------ | ------- | ------------------------------ |
| idCommitment | uint256 | The idCommitment of the member | | idCommitment | uint256 | The idCommitment of the member |
### register ### register
@ -50,8 +56,8 @@ Allows a user to register as a member
#### Parameters #### Parameters
| Name | Type | Description | | Name | Type | Description |
| ---- | ---- | ----------- | | ------------ | ------- | ------------------------------ |
| idCommitment | uint256 | The idCommitment of the member | | idCommitment | uint256 | The idCommitment of the member |
### slash ### slash
@ -64,13 +70,13 @@ _Allows a user to slash a member_
#### Parameters #### Parameters
| Name | Type | Description | | Name | Type | Description |
| ---- | ---- | ----------- | | ------------ | --------------- | ------------------------------ |
| idCommitment | uint256 | The idCommitment of the member | | idCommitment | uint256 | The idCommitment of the member |
| receiver | address payable | | | receiver | address payable | |
| proof | uint256[8] | | | proof | uint256[8] | |
### _validateRegistration ### \_validateRegistration
```solidity ```solidity
function _validateRegistration(uint256 idCommitment) internal view function _validateRegistration(uint256 idCommitment) internal view
@ -78,7 +84,7 @@ function _validateRegistration(uint256 idCommitment) internal view
_Inheriting contracts MUST override this function_ _Inheriting contracts MUST override this function_
### _validateSlash ### \_validateSlash
```solidity ```solidity
function _validateSlash(uint256 idCommitment, address payable receiver, uint256[8] proof) internal pure function _validateSlash(uint256 idCommitment, address payable receiver, uint256[8] proof) internal pure
@ -92,6 +98,24 @@ 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
### merkleRoot
```solidity
function merkleRoot() public view returns (uint256)
```
### numOfLeaves
```solidity
function numOfLeaves() public view returns (uint40)
```
### getElement
```solidity
function getElement(uint256 elementIndex) public view returns (uint256)
```
## StorageAlreadyExists ## StorageAlreadyExists
```solidity ```solidity
@ -160,18 +184,18 @@ modifier onlyUsableStorage()
function initialize(address _poseidonHasher) external function initialize(address _poseidonHasher) external
``` ```
### _authorizeUpgrade ### \_authorizeUpgrade
```solidity ```solidity
function _authorizeUpgrade(address newImplementation) internal function _authorizeUpgrade(address newImplementation) internal
``` ```
_Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by \_Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
{upgradeTo} and {upgradeToAndCall}. {upgradeTo} and {upgradeToAndCall}.
Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}. Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
```solidity ````solidity
function _authorizeUpgrade(address) internal override onlyOwner {} function _authorizeUpgrade(address) internal override onlyOwner {}
```_ ```_
@ -179,7 +203,7 @@ function _authorizeUpgrade(address) internal override onlyOwner {}
```solidity ```solidity
function _insertIntoStorageMap(address storageAddress) internal function _insertIntoStorageMap(address storageAddress) internal
``` ````
### registerStorage ### registerStorage
@ -216,4 +240,3 @@ function register(uint16 storageIndex, uint256 commitment) external
```solidity ```solidity
function forceProgress() external function forceProgress() external
``` ```

View File

@ -41,6 +41,8 @@
"typescript": "^4.7.4" "typescript": "^4.7.4"
}, },
"dependencies": { "dependencies": {
"@zk-kit/imt.sol": "^2.0.0-beta",
"@zk-kit/incremental-merkle-tree.sol": "^1.7.0",
"dotenv": "^16.0.1" "dotenv": "^16.0.1"
}, },
"lint-staged": { "lint-staged": {

View File

@ -1043,6 +1043,20 @@
resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31" resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31"
integrity sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ== integrity sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ==
"@zk-kit/imt.sol@^2.0.0-beta":
version "2.0.0-beta"
resolved "https://registry.yarnpkg.com/@zk-kit/imt.sol/-/imt.sol-2.0.0-beta.tgz#102f88a52bd6848783fddc0db8219de2f163e684"
integrity sha512-bH7RvI5WHAEswUwPspUY582O2+71xbYv5aL+DM4xkaA0GdMyMLUwf5c1yJ4wrt46hp07iXCXJsLXdtLNsTnvZw==
dependencies:
poseidon-solidity "0.0.5"
"@zk-kit/incremental-merkle-tree.sol@^1.7.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@zk-kit/incremental-merkle-tree.sol/-/incremental-merkle-tree.sol-1.7.0.tgz#9d6166c2d84b8bb4fae709c7d729aae6c8c6474f"
integrity sha512-1HF5HEQ2GVKYBAKeNlhiQ7Fb7g2x6iBQ/bBovxBQYx8xuJlD8jPJU/fn2foMVI42+BQtz2V/V4tVtn+0uNL1Rg==
dependencies:
poseidon-solidity "0.0.4"
abort-controller@^3.0.0: abort-controller@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392" resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392"
@ -6828,6 +6842,16 @@ pinkie@^2.0.0:
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870" resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"
integrity sha512-MnUuEycAemtSaeFSjXKW/aroV7akBbY+Sv+RkyqFjgAe73F+MR0TBWKBRDkmfWq/HiFmdavfZ1G7h4SPZXaCSg== integrity sha512-MnUuEycAemtSaeFSjXKW/aroV7akBbY+Sv+RkyqFjgAe73F+MR0TBWKBRDkmfWq/HiFmdavfZ1G7h4SPZXaCSg==
poseidon-solidity@0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/poseidon-solidity/-/poseidon-solidity-0.0.4.tgz#fb80181fb2b17756d98449d54e4cc2ecd8daa4d0"
integrity sha512-+S6jlCXKARCFbw0MMOjfR8gcKUVeB8F3Vu5S+1zDvb8N8cVjiW6WsP9kPqNaZCxjRKakt6LKVRPdA39YbpWfdg==
poseidon-solidity@0.0.5:
version "0.0.5"
resolved "https://registry.yarnpkg.com/poseidon-solidity/-/poseidon-solidity-0.0.5.tgz#3f93e01cfe25f6d2f2fac49734fbb00961b84655"
integrity sha512-NzrvSwHzvZgT4hvg2GyGqeR+UOU/eLSEt4wAoXEua+VaR7NTKKwx1X9bPlh1VMBEVEno+IWvkRBbidFGzTeAqQ==
posix-character-classes@^0.1.0: posix-character-classes@^0.1.0:
version "0.1.1" version "0.1.1"
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab" resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"