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

refactor(contracts): cleanup unused scripts and organize ts code #909

Merged
merged 1 commit into from
Dec 20, 2023

Conversation

ctrlc03
Copy link
Collaborator

@ctrlc03 ctrlc03 commented Dec 12, 2023

  • Remove unused scripts in the contracts folder
  • refactor and add typedoc comments to the typescript code
  • ensure we can pass custom signers to the deployment functions.

Related to #843

@ctrlc03 ctrlc03 self-assigned this Dec 12, 2023
Copy link

netlify bot commented Dec 12, 2023

Deploy Preview for maci-typedoc ready!

Name Link
🔨 Latest commit 458bc6b
🔍 Latest deploy log https://app.netlify.com/sites/maci-typedoc/deploys/658160b7c6fc250008f7772a
😎 Deploy Preview https://deploy-preview-909--maci-typedoc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@ctrlc03 ctrlc03 force-pushed the refactoring/contracts-scripts branch 2 times, most recently from 1e69f50 to 4eb8e72 Compare December 12, 2023 13:02
@samajammin samajammin added this to the MACI v1.1.1 refactor milestone Dec 12, 2023
@ctrlc03 ctrlc03 force-pushed the refactoring/contracts-scripts branch 2 times, most recently from 549a95e to efe8d5f Compare December 14, 2023 11:01
@kittybest
Copy link
Collaborator

export const testIncompleteSubtree = async (aq: AccQueue, contract: AccQueueContract): Promise<void> => {

Should the contract here be changed to aqContract to keep consistence with other testFill functions?

@kittybest
Copy link
Collaborator

await expect(aqContract.getSubRoot(0)).to.be.revertedWithCustomError(aqContract, "InvalidIndex");

This has been tested in testEmptyUponDeployment above, not sure should it be tested again?
If we just wanna test the contract couldn't get an non-existing index, maybe choose another index instead of 0?

@ctrlc03 ctrlc03 force-pushed the refactoring/contracts-scripts branch from efe8d5f to abb90dd Compare December 15, 2023 14:44
@kittybest
Copy link
Collaborator

export const testEnqueue = async (

For me, I would expect this function testing if enqueueing leaves works well. Not sure why we need to insert another subtree? Isn't it testing two things? (The leaves could be successfully inserted, and if the tree is full, there will be another subtree) or actually they are the same thing? (please tell me if I understand wrong)

@ctrlc03 ctrlc03 force-pushed the refactoring/contracts-scripts branch 2 times, most recently from 3907102 to e183411 Compare December 15, 2023 15:18
@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 15, 2023

export const testIncompleteSubtree = async (aq: AccQueue, contract: AccQueueContract): Promise<void> => {

Should the contract here be changed to aqContract to keep consistence with other testFill functions?

Sure, changed that for all accQueue test functions

@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 15, 2023

await expect(aqContract.getSubRoot(0)).to.be.revertedWithCustomError(aqContract, "InvalidIndex");

This has been tested in testEmptyUponDeployment above, not sure should it be tested again? If we just wanna test the contract couldn't get an non-existing index, maybe choose another index instead of 0?

Hold you thoughts on the tests, let's write everything that is not clear down so we can fix in another PR 🙂
Here tbh I just moved these test cases around, did not check them nor wrote them myself so would have to take some time to answer your questions.
Happy for you to work on improving these tests once we done with the other priorities if you'd like @kittybest

@ctrlc03 ctrlc03 requested a review from kittybest December 15, 2023 15:22
@ctrlc03 ctrlc03 force-pushed the refactoring/contracts-scripts branch from e183411 to 282ff28 Compare December 15, 2023 17:09
@kittybest
Copy link
Collaborator

it("mergeSubRoots() should fail on an empty AccQueue", async () => {

The test here is actually being tested also in the edge tests

it("Should not be possible to merge if empty", async () => {

@ctrlc03 ctrlc03 force-pushed the refactoring/contracts-scripts branch from 282ff28 to 724f18e Compare December 18, 2023 15:24
@kittybest
Copy link
Collaborator

Some typos:

it("tallyVotes() shuold revert when votes have already been tallied", async () => {

should*
it("shold not publish a message after the voting period", async () => {

should* again

@kittybest
Copy link
Collaborator

const onChainPackedVals = BigInt(await tallyContract.genTallyVotesPackedVals(users.length, 0, tallyBatchSize));

Just curious that should we make arguments in packTallyVotesSmallVals function same as the tallyContract.genTallyVotesPackedVals?

@kittybest
Copy link
Collaborator

kittybest commented Dec 18, 2023

expect(packedVals.toString(16)).to.eq(onChainPackedVals.toString(16));

why we use toString(16) here while using just toString() elsewhere?

@kittybest
Copy link
Collaborator

it("SignUps - should not overflow", async () => {

what kind of overflow are we testing here?

@kittybest
Copy link
Collaborator

maxMessages: 3125,
maxVoteOptions: 25,

should we add how the numbers calculated in the comments?

@kittybest
Copy link
Collaborator

"test-maci": "hardhat test ./tests/MACI.test.ts",
"test-poll": "hardhat test ./tests/Poll.test.ts",
"test-messageProcessor": "hardhat test ./tests/MessageProcessor.test.ts",
"test-tally": "hardhat test ./tests/Tally.test.ts",
"test-hasher": "hardhat test ./tests/Hasher.test.ts",
"test-utilities": "hardhat test ./tests/Utilities.test.ts",
"test-signupGatekeeper": "hardhat test ./tests/SignUpGatekeeper.test.ts",
"test-verifier": "hardhat test ./tests/Verifier.test.ts",
"test-accQueue": "hardhat test ./tests/AccQueue.test.ts",
"test-accQueueBenchmark": "hardhat test ./tests/AccQueueBenchmark.test.ts"

I think we miss the test-hasherBenchmarks and test-maciOverflow here?

@kittybest
Copy link
Collaborator

kittybest commented Dec 18, 2023

users[0].pubKey.asContractParam() as { x: BigNumberish; y: BigNumberish },

user.pubKey.asContractParam() as { x: BigNumberish; y: BigNumberish },

user.pubKey.asContractParam() as { x: BigNumberish; y: BigNumberish },

I tried to remove the as { x: BigNumberish; y: BigNumberish } part to make it clearer, and it still works, so I think we could remove this?

@kittybest
Copy link
Collaborator

const messageData = [NOTHING_UP_MY_SLEEVE, BigInt(0)];
for (let i = 2; i < 10; i += 1) {
messageData.push(BigInt(0));
}

Do you know why put the first BigInt(0) in the array, while adding other BigInt(0) in the for loop?
Same as the below parts:
const messageData = [NOTHING_UP_MY_SLEEVE, BigInt(0)];
for (let i = 2; i < 10; i += 1) {
messageData.push(BigInt(0));
}

const messageData = [NOTHING_UP_MY_SLEEVE, BigInt(0)];
for (let i = 2; i < 10; i += 1) {
messageData.push(BigInt(0));
}

const messageData = [NOTHING_UP_MY_SLEEVE, BigInt(0)];
for (let i = 2; i < 10; i += 1) {
messageData.push(BigInt(0));
}

@kittybest
Copy link
Collaborator

Do we test to prevent accidentally input negative numbers somewhere?
And I found most of the gasLimit are different, how are they decided?

@kittybest
Copy link
Collaborator

await expect(pollContract.init()).to.be.reverted;

another question, why we need the init() function public for poll contract? it seems to be inited once the maci contract call deployPoll().

@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 19, 2023

Do we test to prevent accidentally input negative numbers somewhere? And I found most of the gasLimit are different, how are they decided?

All numeric variables we use in the smart contracts are uint so no negative values can be accepted.
Honestly I don't know how they decided about the gas limits for the tests, probably just put what worked at the time to get the test through (some being more expensive computations than others and requiring more gas to complete). I'll work today on refactoring the actual tests and address some of the points you brought up about the tests

@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 19, 2023

await expect(pollContract.init()).to.be.reverted;

another question, why we need the init() function public for poll contract? it seems to be inited once the maci contract call deployPoll().

So you suggest we make it external instead? that would definitely work

@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 19, 2023

const messageData = [NOTHING_UP_MY_SLEEVE, BigInt(0)];
for (let i = 2; i < 10; i += 1) {
messageData.push(BigInt(0));
}

Do you know why put the first BigInt(0) in the array, while adding other BigInt(0) in the for loop?
Same as the below parts:

const messageData = [NOTHING_UP_MY_SLEEVE, BigInt(0)];
for (let i = 2; i < 10; i += 1) {
messageData.push(BigInt(0));
}

const messageData = [NOTHING_UP_MY_SLEEVE, BigInt(0)];
for (let i = 2; i < 10; i += 1) {
messageData.push(BigInt(0));
}

const messageData = [NOTHING_UP_MY_SLEEVE, BigInt(0)];
for (let i = 2; i < 10; i += 1) {
messageData.push(BigInt(0));
}

Not sure, seems unnecessary, I'll address this when I work on the test suite

@ctrlc03 ctrlc03 force-pushed the refactoring/contracts-scripts branch from 724f18e to d2d13a9 Compare December 19, 2023 09:09
@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 19, 2023

maxMessages: 3125,
maxVoteOptions: 25,

should we add how the numbers calculated in the comments?

definitely!

@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 19, 2023

it("SignUps - should not overflow", async () => {

what kind of overflow are we testing here?

mm agree this test case is a bit vague, will refactor it when working on the test suite

@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 19, 2023

n

Good shout, definitely we should be as consistent as possible

Remove unused scripts in the contracts folder, refactor and add typedoc comments to the typescript
code as well as ensure we can pass custom signers to the deployment functions.
@ctrlc03 ctrlc03 force-pushed the refactoring/contracts-scripts branch from d2d13a9 to 458bc6b Compare December 19, 2023 09:21
@kittybest
Copy link
Collaborator

await expect(pollContract.init()).to.be.reverted;

another question, why we need the init() function public for poll contract? it seems to be inited once the maci contract call deployPoll().

So you suggest we make it external instead? that would definitely work

I'm not sure whether make it as an external is better or not.
Just wonder why we need the init function for Poll.sol since it looks like just setting up isInit to true? and if it's called more than once, the call would be reverted, but currently I think the only one who will call this is the PollFactory? so why not just put the command of set isInit to true in the constructor, removing the possibility of user accidentally calling poll.init()?

@samajammin
Copy link
Member

  • ensure we can pass custom signers to the deployment functions.

Curious, why do we want this?

@kittybest
Copy link
Collaborator

maci/contracts/ts/deploy.ts

Lines 350 to 360 in 458bc6b

export const deployMaci = async (
signUpTokenGatekeeperContractAddress: string,
initialVoiceCreditBalanceAddress: string,
verifierContractAddress: string,
topupCreditContractAddress: string,
signer?: Signer,
stateTreeDepth = 10,
quiet = false,
): Promise<IDeployedMaci> => {
const { PoseidonT3Contract, PoseidonT4Contract, PoseidonT5Contract, PoseidonT6Contract } =
await deployPoseidonContracts(signer, quiet);

Just curious about why we assign the Poseidon contracts in the deploy function arguments for other contracts deploying, such as deploySubsidy, deployTally, etc, while we deploy Poseidon and link the libraries independently in the deployMaci function?

@kittybest
Copy link
Collaborator

for (let i = 1; i < 6; i += 1) {

I think we could add description here explaining why running for loop from 1 to 6?

@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 19, 2023

  • ensure we can pass custom signers to the deployment functions.

Curious, why do we want this?

This opens up more testing scenarios, where you can deploy smart contracts from different addresses rather than all from the same. Also you could use the signer of this custom deployer class

this.signer = new Wallet(privateKey, this.provider);
which could fit other use cases

@ctrlc03
Copy link
Collaborator Author

ctrlc03 commented Dec 20, 2023

  • ensure we can pass custom signers to the deployment functions.

Curious, why do we want this?

await expect(pollContract.init()).to.be.reverted;

another question, why we need the init() function public for poll contract? it seems to be inited once the maci contract call deployPoll().

So you suggest we make it external instead? that would definitely work

I'm not sure whether make it as an external is better or not. Just wonder why we need the init function for Poll.sol since it looks like just setting up isInit to true? and if it's called more than once, the call would be reverted, but currently I think the only one who will call this is the PollFactory? so why not just put the command of set isInit to true in the constructor, removing the possibility of user accidentally calling poll.init()?

So the problem with that is that we need an init function inside Poll because there is a initialization step we must do before it is usable, but cannot be done inside the constructor (at this time).
Basically we first deploy a Poll contract inside the PollFactory contract. Then we deploy an AccQueue instance, and then we transfer ownership of the AccQueue instance to the Poll contract. For that, we need the address of the Poll contract which we only have after deployment (of course there are other patterns in Solidity which can be used to deploy smart contracts and for instance with create2 it's possible to know the address before the deployment, though this is not something we have implemented atm and within the scope of this refactoring). As you can see, inside the init function of the Poll we enqueue a message into the messageAq instance, which we can only do when the Poll is the owner of messageAq.

@ctrlc03 ctrlc03 merged commit 7d05e2c into dev Dec 20, 2023
12 checks passed
@ctrlc03 ctrlc03 deleted the refactoring/contracts-scripts branch December 20, 2023 16:28
@ctrlc03 ctrlc03 mentioned this pull request Jan 5, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants