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): smart contracts optimizations #919

Merged
merged 1 commit into from
Jan 5, 2024

Conversation

ctrlc03
Copy link
Collaborator

@ctrlc03 ctrlc03 commented Dec 15, 2023

Description

  • use calldata vs memory for immutable function params
  • add payable to constructors to reduce deployment cost
  • unify TREE ARITY as we use quinary trees everywhere
  • ensure we do not accept max messages + 1 in top up

Related issue(s)

#843

Confirmation

Copy link

netlify bot commented Dec 15, 2023

Deploy Preview for maci-typedoc ready!

Name Link
🔨 Latest commit b10ff70
🔍 Latest deploy log https://app.netlify.com/sites/maci-typedoc/deploys/65983056b0e4610008022a8e
😎 Deploy Preview https://deploy-preview-919--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 added this to the MACI v1.1.1 refactor milestone Dec 15, 2023
@ctrlc03 ctrlc03 self-assigned this Dec 15, 2023
@ctrlc03 ctrlc03 added the smart contracts Related to the Solidity smart contracts label Dec 15, 2023
@ctrlc03 ctrlc03 force-pushed the refactor/contract-optimizations branch 3 times, most recently from cbef032 to 63b5180 Compare December 22, 2023 16:09
@ctrlc03 ctrlc03 force-pushed the refactor/contract-optimizations branch from 63b5180 to 195bdd6 Compare December 29, 2023 11:56
contracts/contracts/MACI.sol Show resolved Hide resolved
contracts/contracts/MACI.sol Show resolved Hide resolved
contracts/contracts/Poll.sol Show resolved Hide resolved
contracts/contracts/Poll.sol Show resolved Hide resolved
// The final commitment to the state and ballot roots
uint256 public sbCommitment;
uint256 public subsidyCommitment;

uint8 public constant treeArity = 5;
uint8 internal constant TREE_ARITY = 5;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thinking about that TREE_ARITY is called so many times in different contracts, and they should keep the same. Should we put it in somewhere and import it in these contracts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mm maybe we could put it inside another contract shared by all of them though I think that would bloat things up a little bit 🤔 was looking and couldn't find a contract that was already in use by all of them (MACI, Poll, MP, Tally, etc) where we could add this constant..

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is that a good idea to have a contract holding the constants? or we just keep status quo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mm we have SnarkConstants holding some constants, though I'm not sure if it's a widely used pattern.. to avoid bloating contracts even more I would just keep this one constant inside each contract, at least for now

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then when it comes to deployment, if a user wants to alter the TREE_ARITY, they currently need to navigate through every contract file to modify the value. What if we convert it into an environmental variable for the sake of convenience during deployment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fair point, though they would also need to change the type of AccQueue in use, which currently is hardcoded to a quinary tree everywhere, mm 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

useful discussion here, there's definitely some more simplifying we can do given we always use quinary trees

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

by converting to env variable, you mean that it should be a value passed in the constructor when deploying the contracts?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes... but not sure if this is a good idea, just wanna reduce the risk that if some users deploy it and miss to change one of them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

let's write this down and discuss together at the next team meeting? from next week we'll have a smart contract developer to ask all SC questions to 😈

@ctrlc03 ctrlc03 force-pushed the refactor/contract-optimizations branch 3 times, most recently from f409fe2 to f1514c4 Compare January 2, 2024 10:56
@ctrlc03 ctrlc03 requested a review from kittybest January 2, 2024 10:56
@ctrlc03 ctrlc03 force-pushed the refactor/contract-optimizations branch 8 times, most recently from c171e65 to 97a4589 Compare January 4, 2024 17:47
Unify usage of TREE_ARITY
Use calldata vs memory for function params which are immutable
Ensure we do not accept max messages + 1 in top up
Use payable for constructors to reduce deployment costs
@ctrlc03 ctrlc03 force-pushed the refactor/contract-optimizations branch from 97a4589 to b10ff70 Compare January 5, 2024 16:37
@ctrlc03 ctrlc03 merged commit c4949d7 into dev Jan 5, 2024
12 checks passed
@ctrlc03 ctrlc03 deleted the refactor/contract-optimizations branch January 5, 2024 17:15
@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
smart contracts Related to the Solidity smart contracts
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants