Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC Onchain Trees #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions contracts/WakuRln.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ import {IPoseidonHasher} from "rln-contract/PoseidonHasher.sol";
import {RlnBase, DuplicateIdCommitment, FullTree, InvalidIdCommitment} from "rln-contract/RlnBase.sol";
import {Ownable} from "openzeppelin-contracts/contracts/access/Ownable.sol";

import {LazyIMT, LazyIMTData} from "@zk-kit/imt.sol/LazyIMT.sol";

error NotImplemented();

contract WakuRln is Ownable, RlnBase {
uint16 public immutable contractIndex;

LazyIMTData tree;
using LazyIMT for LazyIMTData;

constructor(
address _poseidonHasher,
uint16 _contractIndex
) Ownable() RlnBase(0, 20, _poseidonHasher, address(0)) {
contractIndex = _contractIndex;

// TODO: This errors
uint8 merkleTreeDepth = 32;
tree.init(merkleTreeDepth);
}

/// Registers a member
Expand All @@ -24,6 +33,7 @@ contract WakuRln is Ownable, RlnBase {

members[idCommitment] = idCommitmentIndex;
memberExists[idCommitment] = true;
tree.insert(idCommitment);

emit MemberRegistered(idCommitment, idCommitmentIndex);
idCommitmentIndex += 1;
Expand Down Expand Up @@ -71,4 +81,20 @@ contract WakuRln is Ownable, RlnBase {
function withdraw() external pure override {
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];
}
}
57 changes: 40 additions & 17 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ error NotImplemented()
uint16 contractIndex
```

### tree

```solidity
struct LazyIMTData tree
```

### constructor

```solidity
constructor(address _poseidonHasher, uint16 _contractIndex) public
```

### _register
### \_register

```solidity
function _register(uint256 idCommitment) internal
Expand All @@ -30,8 +36,8 @@ Registers a member

#### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| Name | Type | Description |
| ------------ | ------- | ------------------------------ |
| idCommitment | uint256 | The idCommitment of the member |

### register
Expand All @@ -50,8 +56,8 @@ Allows a user to register as a member

#### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| Name | Type | Description |
| ------------ | ------- | ------------------------------ |
| idCommitment | uint256 | The idCommitment of the member |

### slash
Expand All @@ -64,21 +70,21 @@ _Allows a user to slash a member_

#### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| idCommitment | uint256 | The idCommitment of the member |
| receiver | address payable | |
| proof | uint256[8] | |
| Name | Type | Description |
| ------------ | --------------- | ------------------------------ |
| idCommitment | uint256 | The idCommitment of the member |
| receiver | address payable | |
| proof | uint256[8] | |

### _validateRegistration
### \_validateRegistration

```solidity
function _validateRegistration(uint256 idCommitment) internal view
```

_Inheriting contracts MUST override this function_

### _validateSlash
### \_validateSlash

```solidity
function _validateSlash(uint256 idCommitment, address payable receiver, uint256[8] proof) internal pure
Expand All @@ -92,6 +98,24 @@ function withdraw() external pure

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

```solidity
Expand Down Expand Up @@ -160,26 +184,26 @@ modifier onlyUsableStorage()
function initialize(address _poseidonHasher) external
```

### _authorizeUpgrade
### \_authorizeUpgrade

```solidity
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}.

Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.

```solidity
````solidity
function _authorizeUpgrade(address) internal override onlyOwner {}
```_

### _insertIntoStorageMap

```solidity
function _insertIntoStorageMap(address storageAddress) internal
```
````

### registerStorage

Expand Down Expand Up @@ -216,4 +240,3 @@ function register(uint16 storageIndex, uint256 commitment) external
```solidity
function forceProgress() external
```

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"typescript": "^4.7.4"
},
"dependencies": {
"@zk-kit/imt.sol": "^2.0.0-beta",
"@zk-kit/incremental-merkle-tree.sol": "^1.7.0",
"dotenv": "^16.0.1"
},
"lint-staged": {
Expand Down
24 changes: 24 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,20 @@
resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31"
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:
version "3.0.0"
resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392"
Expand Down Expand Up @@ -6828,6 +6842,16 @@ pinkie@^2.0.0:
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"
integrity sha512-MnUuEycAemtSaeFSjXKW/aroV7akBbY+Sv+RkyqFjgAe73F+MR0TBWKBRDkmfWq/HiFmdavfZ1G7h4SPZXaCSg==

[email protected]:
version "0.0.4"
resolved "https://registry.yarnpkg.com/poseidon-solidity/-/poseidon-solidity-0.0.4.tgz#fb80181fb2b17756d98449d54e4cc2ecd8daa4d0"
integrity sha512-+S6jlCXKARCFbw0MMOjfR8gcKUVeB8F3Vu5S+1zDvb8N8cVjiW6WsP9kPqNaZCxjRKakt6LKVRPdA39YbpWfdg==

[email protected]:
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:
version "0.1.1"
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
Expand Down
Loading