From 3459f4ed04ab6231e9dc5537d652d6edfff45f12 Mon Sep 17 00:00:00 2001 From: 0xmad <0xmad@users.noreply.github.com> Date: Fri, 12 Apr 2024 11:21:04 -0500 Subject: [PATCH] chore(contracts): get rid of bash scripts for prebuild command --- contracts/package.json | 2 +- contracts/scripts/compileSol.sh | 22 ------ contracts/scripts/compileSol.ts | 79 +++++++++++++++++++ .../scripts/writeMerkleZeroesContracts.sh | 41 ---------- contracts/ts/buildPoseidon.ts | 7 -- contracts/ts/genZerosContract.ts | 46 +++++------ contracts/tsconfig.build.json | 2 +- contracts/tsconfig.json | 2 +- 8 files changed, 102 insertions(+), 99 deletions(-) delete mode 100755 contracts/scripts/compileSol.sh create mode 100644 contracts/scripts/compileSol.ts delete mode 100755 contracts/scripts/writeMerkleZeroesContracts.sh diff --git a/contracts/package.json b/contracts/package.json index 367a1f1bd1..0aeef6bfe9 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -25,7 +25,7 @@ "scripts": { "watch": "tsc --watch", "hardhat": "hardhat node", - "compileSol": "./scripts/compileSol.sh $1", + "compileSol": "TS_NODE_TRANSPILE_ONLY=1 ts-node ./scripts/compileSol.ts", "moveIntegrationArtifacts": "cp -r artifacts/ ../integrationTests/artifacts", "prebuild": "pnpm run compileSol", "build": "tsc -p tsconfig.build.json", diff --git a/contracts/scripts/compileSol.sh b/contracts/scripts/compileSol.sh deleted file mode 100755 index 68f2c6ba30..0000000000 --- a/contracts/scripts/compileSol.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -set -o pipefail - -cd "$(dirname "$0")" -cd .. - -# Delete old files -rm -rf ./artifacts/* -rm -rf ./cache/* -rm -rf ./typechain-types/* - -echo 'Writing Merkle zeros contracts' -./scripts/writeMerkleZeroesContracts.sh - -echo 'Writing empty ballot tree root contract' -pnpm exec ts-node ts/genEmptyBallotRootsContract.ts - -echo 'Building contracts with Hardhat' -TS_NODE_TRANSPILE_ONLY=1 pnpm exec hardhat compile - -echo 'Building Poseidon libraries from bytecode' -pnpm exec ts-node ts/buildPoseidon.ts diff --git a/contracts/scripts/compileSol.ts b/contracts/scripts/compileSol.ts new file mode 100644 index 0000000000..d1cc9e7c2d --- /dev/null +++ b/contracts/scripts/compileSol.ts @@ -0,0 +1,79 @@ +#!/usr/bin/env node +import hre from "hardhat"; + +import fs from "fs"; +import path from "path"; + +import { buildPoseidonT3, buildPoseidonT4, buildPoseidonT5, buildPoseidonT6 } from "../ts/buildPoseidon"; +import { genEmptyBallotRootsContract } from "../ts/genEmptyBallotRootsContract"; +import { genZerosContract } from "../ts/genZerosContract"; + +const PATHS = [ + path.resolve(__dirname, "..", "artifacts"), + path.resolve(__dirname, "..", "cache"), + path.resolve(__dirname, "..", "typechain-types"), +]; + +const NOTHING_UP_MY_SLEEVE_MACI_NUMS = 8370432830353022751713833565135785980866757267633941821328460903436894336785n; +const BLANK_STATE_LEAF = 6769006970205099520508948723718471724660867171122235270773600567925038008762n; +const NUM_ZEROS = 33; + +const ZERO_TREES = [ + { + name: "MerkleBinary0", + zero: 0n, + hashLength: 2, + comment: "Binary tree zeros (0)", + }, + { + name: "MerkleBinaryMaci", + zero: NOTHING_UP_MY_SLEEVE_MACI_NUMS, + hashLength: 2, + comment: "Binary tree zeros (Keccak hash of 'Maci')", + }, + { + name: "MerkleQuinary0", + zero: 0n, + hashLength: 5, + comment: "Quinary tree zeros (0)", + }, + { + name: "MerkleQuinaryMaci", + zero: NOTHING_UP_MY_SLEEVE_MACI_NUMS, + hashLength: 5, + comment: "Quinary tree zeros (Keccak hash of 'Maci')", + }, + { + name: "MerkleQuinaryBlankSl", + zero: BLANK_STATE_LEAF, + hashLength: 5, + comment: "Quinary tree zeros (hash of a blank state leaf)", + }, +]; + +async function main(): Promise { + await Promise.all(PATHS.map((filepath) => fs.existsSync(filepath) && fs.promises.rm(filepath, { recursive: true }))); + + await Promise.all( + ZERO_TREES.map(({ name, zero, hashLength, comment }) => { + const text = genZerosContract({ + name, + zeroVal: zero, + hashLength, + numZeros: NUM_ZEROS, + comment, + useSha256: false, + subDepth: 0, + }); + return fs.promises.writeFile(path.resolve(__dirname, "..", "contracts/trees/zeros", `${name}.sol`), `${text}\n`); + }), + ); + + genEmptyBallotRootsContract(); + + await hre.run("compile"); + + await Promise.all([buildPoseidonT3(), buildPoseidonT4(), buildPoseidonT5(), buildPoseidonT6()]); +} + +main(); diff --git a/contracts/scripts/writeMerkleZeroesContracts.sh b/contracts/scripts/writeMerkleZeroesContracts.sh deleted file mode 100755 index bf51c76e54..0000000000 --- a/contracts/scripts/writeMerkleZeroesContracts.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash -set -e - -cd "$(dirname "$0")" -cd .. - -# The nothing-up-my-sleeve value -maciNums="8370432830353022751713833565135785980866757267633941821328460903436894336785" - -# The hash of a blank state leaf -blankSl="6769006970205099520508948723718471724660867171122235270773600567925038008762" - -# Binary tree with zero = 0 -pnpm exec ts-node ts/genZerosContract.ts \ - MerkleBinary0 0 2 33 "Binary tree zeros (0)" 0 0 \ - > contracts/trees/zeros/MerkleBinary0.sol - -# Binary tree with zero = maciNums -pnpm exec ts-node ts/genZerosContract.ts \ - MerkleBinaryMaci $maciNums 2 33 "Binary tree zeros (Keccak hash of 'Maci')" 0 0 \ - > contracts/trees/zeros/MerkleBinaryMaci.sol - -# Quinary tree with zero = 0 -pnpm exec ts-node ts/genZerosContract.ts \ - MerkleQuinary0 0 5 33 "Quinary tree zeros (0)" 0 0 \ - > contracts/trees/zeros/MerkleQuinary0.sol - -# Quinary tree with zero = maciNums -pnpm exec ts-node ts/genZerosContract.ts \ - MerkleQuinaryMaci $maciNums 5 33 "Quinary tree zeros (Keccak hash of 'Maci')" 0 0 \ - > contracts/trees/zeros/MerkleQuinaryMaci.sol - -# Quinary tree with zero = blank state leaf -pnpm exec ts-node ts/genZerosContract.ts \ - MerkleQuinaryBlankSl $blankSl 5 33 "Quinary tree zeros (hash of a blank state leaf)" 0 0 \ - > contracts/trees/zeros/MerkleQuinaryBlankSl.sol - -## Quinary tree with SHA256 for subtrees and zero = maciNums -#pnpm exec ts-node ts/genZerosContract.ts \ - #MerkleQuinaryMaciWithSha256 $maciNums 5 33 "Quinary tree (with SHA256) zeros (Keccak hash of 'Maci')" 1 2 \ - #> contracts/trees/zeros/MerkleQuinaryMaciWithSha256.sol diff --git a/contracts/ts/buildPoseidon.ts b/contracts/ts/buildPoseidon.ts index 7b71d1205c..6baafce970 100644 --- a/contracts/ts/buildPoseidon.ts +++ b/contracts/ts/buildPoseidon.ts @@ -11,10 +11,3 @@ export const buildPoseidonT3 = (): Promise => buildPoseidon(2); export const buildPoseidonT4 = (): Promise => buildPoseidon(3); export const buildPoseidonT5 = (): Promise => buildPoseidon(4); export const buildPoseidonT6 = (): Promise => buildPoseidon(5); - -if (require.main === module) { - buildPoseidonT3(); - buildPoseidonT4(); - buildPoseidonT5(); - buildPoseidonT6(); -} diff --git a/contracts/ts/genZerosContract.ts b/contracts/ts/genZerosContract.ts index 953be5da75..4e1912c12f 100644 --- a/contracts/ts/genZerosContract.ts +++ b/contracts/ts/genZerosContract.ts @@ -4,15 +4,25 @@ import assert from "assert"; import fs from "fs"; import path from "path"; -const genZerosContract = ( - contractName: string, - zeroVal: bigint, - hashLength: number, - numZeros: number, - comment: string, - useSha256: boolean, - subDepth: number, -): string => { +interface IGetZerosContractArgs { + name: string; + zeroVal: bigint; + hashLength: number; + numZeros: number; + comment: string; + useSha256: boolean; + subDepth: number; +} + +export const genZerosContract = ({ + name, + zeroVal, + hashLength, + numZeros, + comment, + useSha256, + subDepth, +}: IGetZerosContractArgs): string => { assert(hashLength === 2 || hashLength === 5); const template = fs.readFileSync(path.resolve(__dirname, "..", "templates", "MerkleZeros.sol.template")).toString(); @@ -43,26 +53,10 @@ const genZerosContract = ( } const generated = template - .replace("<% CONTRACT_NAME %>", contractName) + .replace("<% CONTRACT_NAME %>", name) .replace("<% NUM_ZEROS %>", numZeros.toString()) .replace("<% ZEROS %>", ` ${z.trim()}`) .replace("<% COMMENT %>", comment.trim()); return generated.trim(); }; - -if (require.main === module) { - const contractName = process.argv[2]; - const zero = BigInt(process.argv[3]); - const hashLength = Number(process.argv[4]); - const numZeros = Number(process.argv[5]); - const comment = process.argv[6]; - const useSha256 = process.argv[7] === "1"; - const subDepth = Number(process.argv[8]); - - const generated = genZerosContract(contractName, zero, hashLength, numZeros, comment, useSha256, subDepth); - // eslint-disable-next-line no-console - console.log(generated); -} - -export { genZerosContract }; diff --git a/contracts/tsconfig.build.json b/contracts/tsconfig.build.json index 06c13d0bbd..b3f129b66b 100644 --- a/contracts/tsconfig.build.json +++ b/contracts/tsconfig.build.json @@ -3,6 +3,6 @@ "compilerOptions": { "outDir": "./build" }, - "include": ["./ts", "./typechain-types", "./tasks"], + "include": ["./ts", "./scripts", "./typechain-types", "./tasks"], "files": ["./hardhat.config.ts"] } diff --git a/contracts/tsconfig.json b/contracts/tsconfig.json index 72d6a0c09c..e73bf3a396 100644 --- a/contracts/tsconfig.json +++ b/contracts/tsconfig.json @@ -3,6 +3,6 @@ "compilerOptions": { "outDir": "./build" }, - "include": ["./ts", "./tests", "./typechain-types", "./tasks"], + "include": ["./ts", "./scripts", "./tests", "./typechain-types", "./tasks"], "files": ["./hardhat.config.ts"] }