Skip to content

Commit

Permalink
Merge pull request #42 from Poseidon-ZKP/0xDigitalOil/scripted-blocka…
Browse files Browse the repository at this point in the history
…dvance

Creates script to advance blocks by N blocks and adjust README accordingly
  • Loading branch information
dtebbs authored Jun 7, 2023
2 parents 6791b89 + b5785bc commit debe1cd
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

strategy:
matrix:
node-version: [16.x]
node-version: [16.x, 18.12.0]

steps:
- name: check out git repo
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/yarn.lock') }}
key: ${{ runner.os }}-node-${{ matrix.node-version }}-modules-${{ hashFiles('**/yarn.lock') }}

- name: Install
run: |
Expand Down
72 changes: 41 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ zkVote demo
2. Run `yarn`

### Download ptau file
```console
```sh
curl -o circuits/ptau.16 https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_16.ptau
```

### Build

```console
```sh
yarn build
yarn tsc
```

### Run workflow test

```console
```sh
yarn test
```

### Unit tests

```console
```sh
yarn hardhat test
```

Expand All @@ -38,63 +38,73 @@ yarn hardhat test
own terminal).

Launch a development blockchain node:
```console
$ yarn hardhat node
```sh
yarn hardhat node
```

Deploy the contract and write the configuration to a file `nouns.config.json`.
This file is read by later commands to connect to the contract.

```console
$ yarn ts-node scripts/deploy.ts
```sh
yarn ts-node scripts/deploy.ts
```

Launch 3 committee daemons (each in it's own terminal, as the process will not
terminate until votes are tallied). For demo purposes, we set the tally to be
triggered when the total voting weight reaches 10.
triggered when the total voting weight reaches 10. Alternatively, it can be run with no `-v` flag and only committee member number parameter. In this case, tally will trigger when `endBlock` is reached.

```console
$ yarn ts-node scripts/committee.ts -v 10 1
```sh
yarn ts-node scripts/committee.ts -v 10 1
```
```console
$ yarn ts-node scripts/committee.ts -v 10 2
```sh
yarn ts-node scripts/committee.ts -v 10 2
```
```console
$ yarn ts-node scripts/committee.ts -v 10 3
```sh
yarn ts-node scripts/committee.ts -v 10 3
```

In a new terminal, setup a vote with proposal Id 1 and end block 12345678, register some dummy voters and cast votes up to a total voting weight above 10
In a new terminal, setup a vote with proposal Id 1 and end block 1234, register some dummy voters and cast votes up to a total voting weight above 10
(max total voting weight is 20). For example:
```console
$ yarn ts-node scripts/setup_vote.ts 1 12345678
```sh
yarn ts-node scripts/setup_vote.ts 1 1234
```

```console
$ yarn ts-node scripts/vote.ts 1 1 yay 6
```sh
yarn ts-node scripts/vote.ts 1 1 yay 6
```
```console
$ yarn ts-node scripts/vote.ts 1 2 nay 3
```sh
yarn ts-node scripts/vote.ts 1 2 nay 3
```
```console
$ yarn ts-node scripts/vote.ts 1 3 yay 5
```sh
yarn ts-node scripts/vote.ts 1 3 yay 5
```

When the committee commands notice that the total voting weight used is at
least 10, they begin the tallying, and will exit after the tallying process is
complete. To query the contract for the vote totals for proposal Id 1, run:
complete.

```console
$ yarn ts-node scripts/get_vote_tally.ts 1
If voting weight threshold isn't met or wasn't set upon committee daemon setup, force hardhat to mine `N` blocks by running:

```sh
yarn ts-node scripts/advanceBlocks.ts N
```

For the example above, run with `N = 1234` to advance `1234` blocks and ensure `endBlock` is reached. If no `N` is passed, defaults to 1 block advanced.

To query the contract for the vote totals for proposal Id 1, run:

```sh
yarn ts-node scripts/get_vote_tally.ts 1
```

## Development

Run syntax checkers and linters:
```console
$ yarn run check
```sh
yarn run check
```

Use `tsfmt` and `prettier-plugin-solidity` to format all code. Run these manually with:
```console
$ yarn run format
```sh
yarn run format
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"test": "yarn ts-node scripts/nouns/nouns.ts",
"postinstall": "patch-package",
"check": "yarn run solhint 'contracts/interfaces/*.sol' 'contracts/nouns/*.sol'",
"format": "./node_modules/.bin/prettier --write 'contracts/**/*.sol' && ./node_modules/.bin/tsfmt -r `git ls-files '*.ts'`"
"format": "./node_modules/.bin/prettier --write 'contracts/**/*.sol' && ./node_modules/.bin/tsfmt -r `git ls-files '*.ts'`",
"advance-time": "yarn ts-node scripts/advanceBlocks.ts"
},
"keywords": [],
"author": "",
Expand Down
9 changes: 9 additions & 0 deletions scripts/advanceBlocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { advanceBlocks } from "./utils/common";

async function advance() {
// process.argv[2] will be the first command-line argument
const numBlocks = process.argv[2] ? parseInt(process.argv[2]) : 1;
await advanceBlocks(numBlocks);
}

advance();
7 changes: 7 additions & 0 deletions scripts/logBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { logCurrentBlockNumber } from "./utils/common";

async function log() {
await logCurrentBlockNumber();
}

log();
22 changes: 22 additions & 0 deletions scripts/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,25 @@ export async function verify2(addr: string, args) {
}
}
}

export const advanceBlocks = async (blocks: number): Promise<void> => {
for (let i = 0; i < blocks; i++) {
await mineBlock();
}
};

export const mineBlock = async (): Promise<void> => {
try {
const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545/');
await provider.send('evm_mine', []);
//console.log('Block mined successfully');
} catch (error) {
console.error('Error mining block:', error);
}
};

export const logCurrentBlockNumber = async (): Promise<void> => {
const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545/');
const blockNumber = await provider.send('eth_blockNumber', []);
console.log('Current block number:', parseInt(blockNumber, 16));
};

0 comments on commit debe1cd

Please sign in to comment.