Skip to content

Commit

Permalink
chore(domainobjs): apply linter rules and fixes
Browse files Browse the repository at this point in the history
- [x] Minor types refactoring
- [x] Lint fixes
  • Loading branch information
0xmad committed Dec 7, 2023
1 parent a9e13e3 commit 6649ca6
Show file tree
Hide file tree
Showing 28 changed files with 618 additions and 392 deletions.
19 changes: 8 additions & 11 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ on:

jobs:
generate-proving-keys:
strategy:
fail-fast: false
matrix:
command: ["test:cli", "test:integration"]

runs-on: ubuntu-22.04

steps:
Expand Down Expand Up @@ -34,10 +39,9 @@ jobs:
npm run bootstrap
npm run build
- name: Compile Contracts
- name: Run hardhat fork
run: |
cd contracts
npm run compileSol
npm run hardhat &
- name: Download rapidsnark (1c137)
Expand All @@ -60,15 +64,8 @@ jobs:
npx zkey-manager compile -c ./zkeys.config.yml
npx zkey-manager genZkeys -c ./zkeys.config.yml
- name: e2e Tests
run: |
cd cli
npm run test
- name: Integration Tests
run: |
cd integrationTests
npm run test
- name: ${{ matrix.command }}
run: npm run ${{ matrix.command }}

- name: Stop Hardhat
run: kill $(lsof -t -i:8545)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
npm run bootstrap
npm run build
- name: Compile Contracts
- name: Run hardhat fork
run: |
cd contracts
npm run hardhat &
Expand Down
2 changes: 1 addition & 1 deletion core/ts/MaciState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class MaciState implements IMaciState {
const maciState = new MaciState(json.stateTreeDepth);

// assign the json values to the new instance
maciState.stateLeaves = json.stateLeaves.map((leaf: string) => StateLeaf.fromJSON(leaf));
maciState.stateLeaves = json.stateLeaves.map((leaf) => StateLeaf.fromJSON(leaf));
maciState.pollBeingProcessed = json.pollBeingProcessed;
maciState.currentPollBeingProcessed = json.currentPollBeingProcessed;
maciState.numSignUps = json.numSignUps;
Expand Down
30 changes: 15 additions & 15 deletions core/ts/Poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
stringifyBigInts,
genTreeCommitment,
} from "maci-crypto";
import { PubKey, Command, PCommand, TCommand, Message, Keypair, StateLeaf, Ballot, PrivKey } from "maci-domainobjs";
import { PubKey, ICommand, PCommand, TCommand, Message, Keypair, StateLeaf, Ballot, PrivKey } from "maci-domainobjs";

import { MaciState } from "./MaciState";
import { TreeDepths, MaxValues, BatchSizes, packTallyVotesSmallVals, packSubsidySmallVals } from "./utils/utils";
Expand Down Expand Up @@ -73,7 +73,7 @@ class Poll implements IPoll {

public messages: Message[] = [];
public messageTree: IncrementalQuinTree;
public commands: Command[] = [];
public commands: ICommand[] = [];

public encPubKeys: PubKey[] = [];

Expand Down Expand Up @@ -312,7 +312,7 @@ class Poll implements IPoll {
this.messageTree.insert(messageLeaf);

const command = new TCommand(_message.data[0], _message.data[1], BigInt(this.pollId));
this.commands.push(command);
this.commands.push(command as ICommand);
};

/*
Expand All @@ -338,12 +338,12 @@ class Poll implements IPoll {
const sharedKey = Keypair.genEcdhSharedKey(this.coordinatorKeypair.privKey, _encPubKey);
try {
const { command } = PCommand.decrypt(_message, sharedKey);
this.commands.push(command);
this.commands.push(command as ICommand);
} catch (e) {
//console.log(`error cannot decrypt: ${e.message}`)
const keyPair = new Keypair();
const command = new PCommand(BigInt(0), keyPair.pubKey, BigInt(0), BigInt(0), BigInt(0), BigInt(0), BigInt(0));
this.commands.push(command);
this.commands.push(command as ICommand);
}
};

Expand Down Expand Up @@ -1054,19 +1054,19 @@ class Poll implements IPoll {
this.stateTreeDepth,
);

copied.stateLeaves = this.stateLeaves.map((x: StateLeaf) => x.copy());
copied.messages = this.messages.map((x: Message) => x.copy());
copied.commands = this.commands.map((x: Command) => x.copy());
copied.ballots = this.ballots.map((x: Ballot) => x.copy());
copied.encPubKeys = this.encPubKeys.map((x: PubKey) => x.copy());
copied.stateLeaves = this.stateLeaves.map((x) => x.copy());
copied.messages = this.messages.map((x) => x.copy());
copied.commands = this.commands.map((x) => x.copy());
copied.ballots = this.ballots.map((x) => x.copy());
copied.encPubKeys = this.encPubKeys.map((x) => x.copy());
if (this.ballotTree) {
copied.ballotTree = this.ballotTree.copy();
}
copied.currentMessageBatchIndex = this.currentMessageBatchIndex;
copied.maciStateRef = this.maciStateRef;
copied.messageTree = this.messageTree.copy();
copied.results = this.results.map((x: bigint) => BigInt(x.toString()));
copied.perVOSpentVoiceCredits = this.perVOSpentVoiceCredits.map((x: bigint) => BigInt(x.toString()));
copied.results = this.results.map((x) => BigInt(x.toString()));
copied.perVOSpentVoiceCredits = this.perVOSpentVoiceCredits.map((x) => BigInt(x.toString()));

copied.numBatchesProcessed = Number(this.numBatchesProcessed.toString());
copied.numBatchesTallied = Number(this.numBatchesTallied.toString());
Expand Down Expand Up @@ -1177,8 +1177,8 @@ class Poll implements IPoll {
// set all properties
poll.ballots = json.ballots.map((ballot: Ballot) => Ballot.fromJSON(ballot));
poll.encPubKeys = json.encPubKeys.map((key: string) => PubKey.deserialize(key));
poll.messages = json.messages.map((message: Message) => Message.fromJSON(message));
poll.commands = json.commands.map((command: any) => {
poll.messages = json.messages.map((message) => Message.fromJSON(message));
poll.commands = json.commands.map((command) => {
switch (command.cmdType) {
case "1": {
return PCommand.fromJSON(command);
Expand All @@ -1187,7 +1187,7 @@ class Poll implements IPoll {
return TCommand.fromJSON(command);
}
default: {
return Command.fromJSON(command);
return { cmdType: command.cmdType };
}
}
});
Expand Down
107 changes: 107 additions & 0 deletions domainobjs/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const fs = require("fs");
const path = require("path");

const prettierConfig = fs.readFileSync(path.resolve(__dirname, "../.prettierrc"), "utf8");
const prettierOptions = JSON.parse(prettierConfig);
const isProduction = process.env.NODE_ENV === "production";

module.exports = {
root: true,
extends: [
"airbnb",
"prettier",
"plugin:import/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:@typescript-eslint/strict",
"plugin:@typescript-eslint/strict-type-checked",
"plugin:@typescript-eslint/stylistic",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:import/typescript",
],
plugins: ["json", "prettier", "unused-imports", "import", "@typescript-eslint"],
parser: "@typescript-eslint/parser",
env: {
node: true,
mocha: true,
es2022: true,
},
settings: {
react: {
version: "999.999.999",
},
"import/resolver": {
typescript: {},
node: {
extensions: [".ts", ".js"],
moduleDirectory: ["node_modules", "ts", "src"],
},
},
},
parserOptions: {
project: path.resolve(__dirname, "./tsconfig.json"),
sourceType: "module",
typescript: true,
ecmaVersion: 2022,
experimentalDecorators: true,
requireConfigFile: false,
ecmaFeatures: {
classes: true,
impliedStrict: true,
},
warnOnUnsupportedTypeScriptVersion: true,
},
reportUnusedDisableDirectives: isProduction,
rules: {
"import/no-cycle": ["error"],
"unused-imports/no-unused-imports": "error",
"import/no-extraneous-dependencies": [
"error",
{
devDependencies: ["**/*.test.ts"],
},
],
"no-debugger": isProduction ? "error" : "off",
"no-console": "error",
"no-underscore-dangle": "error",
"no-redeclare": ["error", { builtinGlobals: true }],
"import/order": [
"error",
{
groups: ["external", "builtin", "internal", "type", "parent", "sibling", "index", "object"],
alphabetize: {
order: "asc",
caseInsensitive: true,
},
warnOnUnassignedImports: true,
"newlines-between": "always",
},
],
"prettier/prettier": ["error", prettierOptions],
"import/prefer-default-export": "off",
"import/extensions": ["error", { json: "always" }],
"class-methods-use-this": "off",
"prefer-promise-reject-errors": "off",
"max-classes-per-file": "off",
"no-use-before-define": ["off"],
"no-shadow": "off",
curly: ["error", "all"],

"@typescript-eslint/explicit-member-accessibility": ["error", { accessibility: "no-public" }],
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/prefer-nullish-coalescing": "off",
"@typescript-eslint/no-floating-promises": "off",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/explicit-module-boundary-types": "error",
"@typescript-eslint/no-use-before-define": ["error", { functions: false, classes: false }],
"@typescript-eslint/no-misused-promises": ["error", { checksVoidReturn: false }],
"@typescript-eslint/no-shadow": [
"error",
{
builtinGlobals: true,
allow: ["location", "event", "history", "name", "status", "Option", "test", "expect"],
},
],
},
};
2 changes: 1 addition & 1 deletion domainobjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "maci-domainobjs",
"version": "1.1.2",
"description": "",
"main": "build/index.js",
"main": "build/ts/index.js",
"scripts": {
"watch": "tsc --watch",
"build": "tsc",
Expand Down
7 changes: 4 additions & 3 deletions domainobjs/ts/__tests__/ballot.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Ballot } from "..";
import { expect } from "chai";

import { Ballot } from "..";

describe("Ballot", () => {
it("should create a new ballot and hash it", () => {
const b = new Ballot(0, 2);
const h = b.hash();
expect(h).to.not.be.null;
expect(h).to.not.eq(null);
});

it("copy should produce a deep copy", () => {
const b1 = Ballot.genRandomBallot(2, 2);
const b2 = b1.copy();
expect(b1.equals(b2)).to.be.true;
expect(b1.equals(b2)).to.eq(true);
});

it("asCircuitInputs should produce an array", () => {
Expand Down
22 changes: 11 additions & 11 deletions domainobjs/ts/__tests__/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Keypair, Message, PCommand, TCommand } from "../";
import { genRandomSalt } from "maci-crypto";
import { expect } from "chai";
import { genRandomSalt } from "maci-crypto";

import { Keypair, Message, PCommand, TCommand } from "..";

describe("Commands & Messages", () => {
const { privKey, pubKey } = new Keypair();
Expand All @@ -11,36 +12,35 @@ describe("Commands & Messages", () => {
const newPubKey = k.pubKey;

const ecdhSharedKey = Keypair.genEcdhSharedKey(privKey, pubKey1);
const random50bitBigInt = (): bigint => {
return ((BigInt(1) << BigInt(50)) - BigInt(1)) & BigInt(genRandomSalt().toString());
};
// eslint-disable-next-line no-bitwise
const random50bitBigInt = (): bigint => ((BigInt(1) << BigInt(50)) - BigInt(1)) & BigInt(genRandomSalt().toString());
const command: PCommand = new PCommand(
random50bitBigInt(),
newPubKey,
random50bitBigInt(),
random50bitBigInt(),
random50bitBigInt(),
random50bitBigInt(),
genRandomSalt() as bigint,
genRandomSalt(),
);
const signature = command.sign(privKey);
const message = command.encrypt(signature, ecdhSharedKey);
const decrypted = PCommand.decrypt(message, ecdhSharedKey);

it("command.sign() should produce a valid signature", () => {
expect(command.verifySignature(signature, pubKey)).to.be.true;
expect(command.verifySignature(signature, pubKey)).to.eq(true);
});

it("decrypted message should match the original command", () => {
expect(decrypted.command.equals(command)).to.be.true;
expect(decrypted.command.equals(command)).to.eq(true);
expect(decrypted.signature.R8[0].toString()).to.eq(signature.R8[0].toString());
expect(decrypted.signature.R8[1].toString()).to.eq(signature.R8[1].toString());
expect(decrypted.signature.S.toString()).to.eq(signature.S.toString());
});

it("decrypted message should have a valid signature", () => {
const isValid = decrypted.command.verifySignature(decrypted.signature, pubKey);
expect(isValid).to.be.true;
expect(isValid).to.eq(true);
});

it("Command.copy() should produce a deep copy", () => {
Expand Down Expand Up @@ -73,7 +73,7 @@ describe("Commands & Messages", () => {
]);

const m2 = m1.copy();
expect(m2.equals(m1)).to.be.true;
expect(m2.equals(m1)).to.eq(true);
});

it("message.asCircuitInputs() should return a array", () => {
Expand Down Expand Up @@ -114,7 +114,7 @@ describe("Commands & Messages", () => {

it("copy should produce a deep copy", () => {
const c = tCommand.copy();
expect(c.equals(tCommand)).to.be.true;
expect(c.equals(tCommand)).to.eq(true);
});
});
});
Loading

0 comments on commit 6649ca6

Please sign in to comment.