Skip to content

Commit

Permalink
chore(contracts): get rid of bash scripts for prebuild command
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmad committed Apr 12, 2024
1 parent b51d2bc commit 3459f4e
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 99 deletions.
2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 0 additions & 22 deletions contracts/scripts/compileSol.sh

This file was deleted.

79 changes: 79 additions & 0 deletions contracts/scripts/compileSol.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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();
41 changes: 0 additions & 41 deletions contracts/scripts/writeMerkleZeroesContracts.sh

This file was deleted.

7 changes: 0 additions & 7 deletions contracts/ts/buildPoseidon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,3 @@ export const buildPoseidonT3 = (): Promise<void> => buildPoseidon(2);
export const buildPoseidonT4 = (): Promise<void> => buildPoseidon(3);
export const buildPoseidonT5 = (): Promise<void> => buildPoseidon(4);
export const buildPoseidonT6 = (): Promise<void> => buildPoseidon(5);

if (require.main === module) {
buildPoseidonT3();
buildPoseidonT4();
buildPoseidonT5();
buildPoseidonT6();
}
46 changes: 20 additions & 26 deletions contracts/ts/genZerosContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 };
2 changes: 1 addition & 1 deletion contracts/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"compilerOptions": {
"outDir": "./build"
},
"include": ["./ts", "./typechain-types", "./tasks"],
"include": ["./ts", "./scripts", "./typechain-types", "./tasks"],
"files": ["./hardhat.config.ts"]
}
2 changes: 1 addition & 1 deletion contracts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"compilerOptions": {
"outDir": "./build"
},
"include": ["./ts", "./tests", "./typechain-types", "./tasks"],
"include": ["./ts", "./scripts", "./tests", "./typechain-types", "./tasks"],
"files": ["./hardhat.config.ts"]
}

0 comments on commit 3459f4e

Please sign in to comment.