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

refactoring(cli && tests) - refactoring of the CLI package and integration tests #794

Merged
merged 50 commits into from
Dec 5, 2023

Conversation

ctrlc03
Copy link
Collaborator

@ctrlc03 ctrlc03 commented Oct 30, 2023

fix #789 and fix #790

What has been done in this PR:

  • rewrite cli code to be cleaner and compact
  • removed argparse in favour of commander
  • implemented interfaces for each of the command's arguments
  • allowing commands to return values -> this way we can use them in the integration tests and we have a blueprint for a possible SDK or just use this in a frontend
  • inline comments + typedoc across all of the code changes
  • documented usage
  • rewrote all of the tests to TS to improve readability as well as speed
  • removed redundant test cases
  • fixed wrong test cases (using stateIndex 0 while sending a message)
  • refactored integration tests
  • clean up of the integration tests
  • use the new cli in the integration tests and do not read output from stdout/stderr
  • removed several dependencies from these packages which are not needed
  • do not deploy verifier contract twice (on maci deployment and poll deployment)
  • ensure all recent changes are integrated
  • allow integration tests to run without starting up hardhat first in the contracts folder

@ctrlc03 ctrlc03 marked this pull request as draft October 30, 2023 17:59
@ctrlc03 ctrlc03 self-assigned this Oct 30, 2023
@ctrlc03 ctrlc03 added this to the Maci Refactoring milestone Oct 31, 2023
@ctrlc03 ctrlc03 linked an issue Oct 31, 2023 that may be closed by this pull request
7 tasks
@ctrlc03 ctrlc03 force-pushed the refactoring/cli branch 2 times, most recently from be37399 to 9435b4c Compare November 8, 2023 17:25
@ctrlc03 ctrlc03 marked this pull request as ready for review November 14, 2023 16:52
@ctrlc03 ctrlc03 requested a review from baumstern November 14, 2023 21:45
@ctrlc03 ctrlc03 changed the title Refactoring/cli refactoring(cli && tests) - refactoring of the CLI package and integration tests Nov 15, 2023
@ctrlc03 ctrlc03 linked an issue Nov 17, 2023 that may be closed by this pull request
import { readContractAddress } from "../utils/storage"
import { contractExists } from "../utils/contracts"
import { getDefaultSigner, parseArtifact } from "maci-contracts"
import { promptPwd } from "../utils/prompts"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctrlc03 , have you tried creating a hardhat task that imports any maci-cli command? I'm getting error (see below). I believe the hardhat import in the getDefaultSigner and parseArtifact is the issue. Hadhat scripts work fine, just the hardhat task gives errors

Error HH9: Error while loading Hardhat's configuration.

You probably tried to import the "hardhat" module from your config or a file imported from it.
This is not possible, as Hardhat can't be initialized while its config is being defined.

To learn more about how to access the Hardhat Runtime Environment from different contexts go to https://hardhat.org/hre

For more info go to https://hardhat.org/HH9 or run Hardhat with --show-stack-traces```

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not, I will give it a try as soon as I get a chance

@ctrlc03 ctrlc03 force-pushed the refactoring/cli branch 3 times, most recently from 451adc7 to 206dc1d Compare November 23, 2023 18:28
@baumstern
Copy link
Member

baumstern commented Nov 28, 2023

I encountered a SyntaxError: Cannot use import statement outside a module when running node build/index.js using commit c4de587:

% npm install
% npm run bootstrap
% npm run build

% cd cli
% node build/index.js
/Users/daehyun/workspace/pse/maci/cli/hardhat.config.ts:1
import dotenv from "dotenv";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at importCsjOrEsModule (/Users/daehyun/workspace/pse/maci/contracts/node_modules/hardhat/internal/core/config/config-loading.js:24:26)
    at loadConfigAndTasks (/Users/daehyun/workspace/pse/maci/contracts/node_modules/hardhat/internal/core/config/config-loading.js:66:22)

Node.js v18.17.0

Copy link
Member

@baumstern baumstern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on this PR! The reduction in test runtime from an hour to 30 minutes is a significant improvement and streamlining the testing process by eliminating the need for a separate hardhat network is highly beneficial. I've also provided some feedback to further refine our project's overall code quality and maintainability.

integrationTests/scripts/runTestsInCi.sh Outdated Show resolved Hide resolved
.github/workflows/reusable-e2e.yml Show resolved Hide resolved
cli/package.json Outdated
Comment on lines 16 to 17
"compileSol": "./node_modules/maci-contracts/scripts/compileSol.sh $1",
"pretest": "./node_modules/maci-contracts/scripts/compileSol.sh",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: here the CLI package references scripts directly from node_modules/maci-contracts/. Given that our codebase is a monorepo using Lerna, which sets up symlinks via lerna bootstrap, this setup could lead to issues. If the version specified in dependencies differs from the local maci-contracts version, Lerna might pull the package from npm instead of using the local symlink. This discrepancy could cause problems during development and testing, especially if there are version-specific dependencies or configurations. It's a potential area for tricky troubleshooting scenarios. We might want to consider revising this approach to ensure consistency and avoid such conflicts.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you suggest here? I was thinking that users might install the cli package via npm and not have the full repo locally, so this way the package could work on its own

cli/tests/utilities/constants.ts Outdated Show resolved Hide resolved
cli/ts/commands/genProofs.ts Outdated Show resolved Hide resolved
cli/ts/index.ts Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I noticed that our Mocha tests are throwing raw errors without using assertions. This results in unstructured and cluttered error logs, making it difficult to pinpoint the exact cause of test failures. For better clarity and maintainability, I suggest incorporating assertions (e.g., using assert or a similar library) in our test cases. This way, if a test fails, we get a clear, concise message about what was expected versus what was actually observed. It will make debugging more straightforward and improve the overall quality of our test suite:

    multiplePolls2
[✗] Could not find ./zkeys/ProcessMessages_10-2-1-2_test.wasm.
      9) should run the first poll
      ✔ should deploy two more polls (452ms)
      ✔ should publish messages to the second poll (736ms)
      ✔ should publish messages to the third poll (756ms)
[✗] Could not find ./zkeys/ProcessMessages_10-2-1-2_test.wasm.
      10) should complete the second poll
[✗] Could not find ./zkeys/ProcessMessages_10-2-1-2_test.wasm.
      11) should complete the third poll


  18 passing (1m)
  11 failing

  1) e2e tests
       test1
         should generate zk-SNARK proofs and verify them:
     
  Error: 
      at logError (ts/utils/theme.ts:46:11)
      at /Users/daehyun/workspace/pse/maci/cli/ts/commands/genProofs.ts:71:26
      at step (ts/commands/genProofs.ts:33:23)
      at Object.next (ts/commands/genProofs.ts:14:53)
      at /Users/daehyun/workspace/pse/maci/cli/ts/commands/genProofs.ts:8:71
      at new Promise (<anonymous>)
      at __awaiter (ts/commands/genProofs.ts:4:12)
      at genProofs (ts/commands/genProofs.ts:51:20)
      at Suite.<anonymous> (tests/e2e.test.ts:110:28)
      at step (tests/e2e.test.ts:33:23)
      at Object.next (tests/e2e.test.ts:14:53)
      at fulfilled (tests/e2e.test.ts:5:58)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at runNextTicks (node:internal/process/task_queues:64:3)
      at listOnTimeout (node:internal/timers:538:9)
      at processTimers (node:internal/timers:512:7)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[✗] Could not find ./zkeys/ProcessMessages_10-2-1-2_test.wasm. isn't that clear enough?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am throwing directly with the error text rather than an empty and confusing stack trace too (as suggested in a previous comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests should explicitly assert the conditions they're checking for clarity and precision. If you intend to address this in a follow-up PR, please create an issue to track it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea tests should actually be properly reviewed in a separate piece of work imo (this PR goal was not that), there's loads we can improve and perhaps some of them are not so correct

cli/hardhat.config.ts Outdated Show resolved Hide resolved
cli/ts/commands/deployPoll.ts Outdated Show resolved Hide resolved
cli/ts/utils/prompts.ts Outdated Show resolved Hide resolved
@baumstern
Copy link
Member

(+) I've observed that our current logging strategy frequently checks the quiet variable to decide whether to print log messages. This approach can be improved by adopting a more conventional logging level system. Using logging levels (like debug, info, warn, error) allows for more flexible and granular control over what gets logged.

There are several libraries that can help implement this, such as winston, bunyan, or pino. These packages provide robust logging capabilities with customizable levels, formats, and outputs, enhancing the overall logging strategy of our application.

@baumstern
Copy link
Member

As I've gone through this PR, I've observed several areas that could benefit from future attention and refinement. While these don't necessarily need to be addressed within the scope of this current PR, they're important for the ongoing improvement of our project:

  • Error Handling Techniques: From a CLI design perspective, we should consider how error messages are displayed to the end user. This includes refining error handling in the originating functions, how these errors are caught and processed, and ensuring that CLI commands present user-friendly error messages instead of raw output or stack traces.

Though not pressing for this PR, this point would be valuable to keep in mind for future development to elevate the quality and usability of our CLI.

@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Nov 28, 2023

(+) I've observed that our current logging strategy frequently checks the quiet variable to decide whether to print log messages. This approach can be improved by adopting a more conventional logging level system. Using logging levels (like debug, info, warn, error) allows for more flexible and granular control over what gets logged.

There are several libraries that can help implement this, such as winston, bunyan, or pino. These packages provide robust logging capabilities with customizable levels, formats, and outputs, enhancing the overall logging strategy of our application.

I wanted to keep the production package a bit slimmer and avoid using third party libraries for that, what you think?

@ctrlc03 ctrlc03 force-pushed the refactoring/cli branch 3 times, most recently from f693acb to 5716bfb Compare November 28, 2023 13:55
@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Nov 28, 2023

I encountered a SyntaxError: Cannot use import statement outside a module when running node build/index.js using commit c4de587:

% npm install
% npm run bootstrap
% npm run build

% cd cli
% node build/index.js
/Users/daehyun/workspace/pse/maci/cli/hardhat.config.ts:1
import dotenv from "dotenv";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at importCsjOrEsModule (/Users/daehyun/workspace/pse/maci/contracts/node_modules/hardhat/internal/core/config/config-loading.js:24:26)
    at loadConfigAndTasks (/Users/daehyun/workspace/pse/maci/contracts/node_modules/hardhat/internal/core/config/config-loading.js:66:22)

Node.js v18.17.0

could you try to run it with ts-node instead please? this is due to hardhat.config.ts unfortunately. We need to think how this would work on a published npm package (how do users get access to the contracts ABI, how do they setup the config file locally, and how do they run the cli)

@baumstern
Copy link
Member

(+) I've observed that our current logging strategy frequently checks the quiet variable to decide whether to print log messages. This approach can be improved by adopting a more conventional logging level system. Using logging levels (like debug, info, warn, error) allows for more flexible and granular control over what gets logged.
There are several libraries that can help implement this, such as winston, bunyan, or pino. These packages provide robust logging capabilities with customizable levels, formats, and outputs, enhancing the overall logging strategy of our application.

I wanted to keep the production package a bit slimmer and avoid using third party libraries for that, what you think?

It looks weird for me that we have to use if(!quiet) statement whenever we want manage log verbosity tbh. But let's keep it as it is for now since it gonna take our bandwidth to assess logging framework and implementing it. And at least we have a working custom logging system for now. If it turns out not working well later, then we could improve it from that point

…PR comments, revert to using hardhat.config.js and running a local node first for cli local usage
@baumstern
Copy link
Member

CI failed due to corrupted lock file in contract package:


> [email protected] compileSol
> ./scripts/compileSol.sh $1

Writing Merkle zeros contracts
Writing empty ballot tree root contract
Building contracts with Hardhat
Error HH18: You installed Hardhat with a corrupted lockfile due to the NPM bug #4828.

Please delete your node_modules, package-lock.json, reinstall your project, and try again.

baumstern
baumstern previously approved these changes Dec 5, 2023
Copy link
Member

@baumstern baumstern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job on this PR! Really appreciate all the effort we put into this.

@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 5, 2023

Great job on this PR! Really appreciate all the effort we put into this.

thanks for the time taken to review this big one!

@ctrlc03 ctrlc03 merged commit c20bf33 into dev Dec 5, 2023
10 checks passed
@ctrlc03 ctrlc03 deleted the refactoring/cli branch December 5, 2023 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integration tests refactoring CLI refactoring Installing npm package [email protected] gives warning
4 participants