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

Chore(proxy)/openzeppelin v5 migration #1562

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from

Conversation

Anmol-Dhiman
Copy link
Contributor

@Anmol-Dhiman Anmol-Dhiman commented Apr 9, 2024

  • Updated the bytes calculation formula i.e. keccak256(abi.encode(uint256(keccak256(id)) - 1)) & ~bytes32(uint256(0xff))
  • Made changes for using Namespaced Storage Layout in proxy and mock proxy contracts.

Issue : #1270
Test Result :
Screenshot 2024-04-19 at 11 52 18 AM


PR-Codex overview

The focus of this PR is to update storage locations in various contracts for better organization and efficiency.

Detailed summary

  • Updated storage locations in Initializable.sol, UUPSProxiable.sol, UUPSProxy.sol, and mock contracts.
  • Added storage structs and constants for improved storage management.
  • Implemented functions to access and update storage variables efficiently.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Summary by CodeRabbit

  • New Features

    • Introduced structured storage patterns in several contracts to enhance state management and modularity.
    • Added new getter functions for state variables in upgraded contracts.
  • Bug Fixes

    • Updated storage slot constants for improved accuracy in contract interactions.
  • Refactor

    • Refactored state management in multiple contracts to utilize dedicated storage structs, improving maintainability and reducing risks of storage collisions during upgrades.
  • Documentation

    • Updated comments for constants to clarify new calculation methods for storage slots.

Copy link

netlify bot commented Apr 9, 2024

‼️ Deploy request for kleros-v2-testnet rejected.

Name Link
🔨 Latest commit 413cea4

Copy link

netlify bot commented Apr 9, 2024

👷 Deploy request for kleros-v2-university pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit c78167d

Copy link

netlify bot commented Apr 9, 2024

👷 Deploy request for kleros-v2-neo pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit c78167d

Copy link
Contributor

coderabbitai bot commented Jun 5, 2024

Walkthrough

The recent updates enhance the structure and maintainability of smart contracts by transitioning from direct state variable access to a defined storage pattern utilizing structs. This change promotes better encapsulation, aligns with best practices for proxy patterns, and improves gas efficiency. Key constants governing implementation slots were also updated, ensuring compatibility with current standards while maintaining the essential functionality of the contracts.

Changes

Files Change Summary
contracts/src/proxy/... Updated _INITIALIZABLE_STORAGE and IMPLEMENTATION_SLOT constants with new values; enhanced comments to reflect improved calculation methods.
contracts/src/proxy/mock/... Refactored state management in NonUpgradeableMock, UUPSUpgradeableMock, UpgradedByInheritance, and UpgradedByRewrite to use structured storage.
contracts/src/proxy/mock/by-rewrite/... Introduced UpgradedByRewriteV2 with an additional state variable; transitioned to structured storage while maintaining existing functionalities.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Contract
    participant Storage

    User->>Contract: Call function (e.g., increment)
    Contract->>Storage: Access storage struct
    Storage-->>Contract: Return state variable (e.g., counter)
    Contract-->>User: Return result
Loading

🐇 "In the code's bright spring, new patterns sway,
With structures and slots, they find their way.
A hop and a skip, through bytes we glide,
In a world of smart contracts, we take pride!
With each little change, the future's in sight,
Let’s dance through the code, oh what a delight!" ✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Anmol-Dhiman Anmol-Dhiman self-assigned this Jun 19, 2024
@Anmol-Dhiman Anmol-Dhiman linked an issue Jun 19, 2024 that may be closed by this pull request
Copy link

codeclimate bot commented Jul 31, 2024

Code Climate has analyzed commit c78167d and detected 0 issues on this pull request.

View more on Code Climate.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between db762de and c78167d.

Files selected for processing (7)
  • contracts/src/proxy/Initializable.sol (1 hunks)
  • contracts/src/proxy/UUPSProxiable.sol (1 hunks)
  • contracts/src/proxy/UUPSProxy.sol (1 hunks)
  • contracts/src/proxy/mock/UUPSUpgradeableMocks.sol (1 hunks)
  • contracts/src/proxy/mock/by-inheritance/UpgradedByInheritance.sol (1 hunks)
  • contracts/src/proxy/mock/by-rewrite/UpgradedByRewrite.sol (1 hunks)
  • contracts/src/proxy/mock/by-rewrite/UpgradedByRewriteV2.sol (1 hunks)
Additional comments not posted (34)
contracts/src/proxy/mock/by-rewrite/UpgradedByRewrite.sol (6)

10-14: LGTM!

The UpgradedByRewriteStorage struct encapsulates the governor and counter variables, aligning with best practices for upgradeable contracts.


24-26: LGTM!

The initialize function correctly updates the state variables through the new storage structure.


30-31: LGTM!

The _authorizeUpgrade function maintains the access control logic while adhering to the updated state management approach.


35-36: LGTM!

The increment function maintains its functionality while improving the underlying implementation.


42-46: LGTM!

The _getUpgradedByRewriteStorage function correctly retrieves the storage location using inline assembly, ensuring efficient access to the state variables.


48-53: LGTM!

The governor and counter functions correctly encapsulate state management within the dedicated structure, improving readability and maintainability.

contracts/src/proxy/mock/by-rewrite/UpgradedByRewriteV2.sol (6)

10-15: LGTM!

The UpgradedByRewriteV2 struct encapsulates the governor, counter, and newVariable variables, aligning with best practices for upgradeable contracts.


25-26: LGTM!

The initialize function correctly updates the newVariable state variable through the new storage structure and maintains functionality.


31-32: LGTM!

The _authorizeUpgrade function maintains the access control logic while adhering to the updated state management approach.


36-37: LGTM!

The increment function maintains its functionality while improving the underlying implementation.


44-47: LGTM!

The _getStorageUpgradedByRewrite function correctly retrieves the storage location using inline assembly, ensuring efficient access to the state variables.


49-56: LGTM!

The governor, counter, and newVariable functions correctly encapsulate state management within the dedicated structure, improving readability and maintainability.

contracts/src/proxy/mock/by-inheritance/UpgradedByInheritance.sol (9)

10-14: LGTM!

The UpgradedByInheritanceV1Storage struct encapsulates the governor and counter variables, aligning with best practices for upgradeable contracts.


25-27: LGTM!

The initialize function correctly updates the state variables through the new storage structure.


31-32: LGTM!

The _authorizeUpgrade function maintains the access control logic while adhering to the updated state management approach.


36-37: LGTM!

The increment function maintains its functionality while improving the underlying implementation.


43-46: LGTM!

The _getInheritanceV1Storage function correctly retrieves the storage location using inline assembly, ensuring efficient access to the state variables.


57-62: LGTM!

The UpgradedByInheritanceV2Storage struct encapsulates the governor, counter, and newVariable variables, aligning with best practices for upgradeable contracts.


68-69: LGTM!

The initializeV2 function correctly updates the newVariable state variable through the new storage structure and maintains functionality.


76-79: LGTM!

The _getInheritanceV2Storage function correctly retrieves the storage location using inline assembly, ensuring efficient access to the state variables.


81-88: LGTM!

The governor, counter, and newVariable functions correctly encapsulate state management within the dedicated structure, improving readability and maintainability.

contracts/src/proxy/mock/UUPSUpgradeableMocks.sol (9)

10-15: Adopt structured storage for better state management.

The introduction of the NonUpgradeableMockStorage struct and the use of a specific storage slot enhance state management. This approach aligns with best practices for proxy patterns and improves gas efficiency.


18-19: Ensure correct access to structured storage.

The counter function correctly accesses the counter variable through the storage struct. This approach ensures consistent state management.


23-24: Ensure correct state modification through structured storage.

The increment function correctly modifies the counter variable through the storage struct. This approach ensures consistent state management.


30-38: Correctly retrieve storage pointer using inline assembly.

The _getNonUpgradeableMockStorage function correctly retrieves the storage pointer using inline assembly. This approach ensures that the storage slot is correctly accessed.


42-49: Adopt structured storage for better state management.

The introduction of the UUPSUpgradeableMockStorage struct and the use of a specific storage slot enhance state management. This approach aligns with best practices for proxy patterns and improves gas efficiency.


52-53: Ensure correct initialization of structured storage.

The constructor correctly initializes the initialized variable through the storage struct. This approach ensures consistent state management.


57-60: Ensure correct initialization logic.

The initialize function correctly sets the governor and initialized variables through the storage struct. The require statement ensures the contract is not re-initialized.


64-65: Ensure correct authorization logic.

The _authorizeUpgrade function correctly checks the governor variable through the storage struct. This approach ensures that only the authorized address can upgrade the contract.


71-79: Correctly retrieve storage pointer using inline assembly.

The _getInitializableStorage function correctly retrieves the storage pointer using inline assembly. This approach ensures that the storage slot is correctly accessed.

contracts/src/proxy/UUPSProxy.sol (1)

25-27: Update storage slot constant for implementation address.

The IMPLEMENTATION_SLOT constant has been updated to a new value. This change ensures compatibility with the latest standards or practices in proxy contract design. Verify that this new value is correctly used throughout the contract.

Verification successful

The new IMPLEMENTATION_SLOT value is correctly used throughout the contract.

The constant IMPLEMENTATION_SLOT with the updated value is consistently used in assignments, reads, and checks within the contract.

  • Lines where IMPLEMENTATION_SLOT is assigned:
    • sstore(IMPLEMENTATION_SLOT, _implementation)
    • sstore(IMPLEMENTATION_SLOT, newImplementation)
  • Lines where IMPLEMENTATION_SLOT is read:
    • implementation := sload(IMPLEMENTATION_SLOT)
    • return IMPLEMENTATION_SLOT
  • Line where IMPLEMENTATION_SLOT is checked:
    • if (slot != IMPLEMENTATION_SLOT)
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the new IMPLEMENTATION_SLOT value.

# Test: Search for the IMPLEMENTATION_SLOT usage. Expect: Only occurrences of the new value.
rg --type solidity "0x75b20eef8615de99c108b05f0dbda081c91897128caa336d75dffb97c4132b00"

Length of output: 120


Script:

#!/bin/bash
# Description: Verify the usage of the new IMPLEMENTATION_SLOT value.

# Find all Solidity files and search for the IMPLEMENTATION_SLOT value.
fd -e sol -x rg "0x75b20eef8615de99c108b05f0dbda081c91897128caa336d75dffb97c4132b00" {}

Length of output: 327


Script:

#!/bin/bash
# Description: Verify the usage of the IMPLEMENTATION_SLOT constant.

# Find all Solidity files and search for all occurrences of IMPLEMENTATION_SLOT.
fd -e sol -x rg -A 5 "IMPLEMENTATION_SLOT" {}

Length of output: 1852

contracts/src/proxy/UUPSProxiable.sol (1)

67-69: Update storage slot constant for implementation address.

The IMPLEMENTATION_SLOT constant has been updated to a new value. This change ensures compatibility with the latest standards or practices in proxy contract design. Verify that this new value is correctly used throughout the contract.

Verification successful

Update storage slot constant for implementation address.

The IMPLEMENTATION_SLOT constant has been updated to a new value and is correctly used throughout the contract's logic. The new value is consistently applied in conditional checks, storage operations, and return statements.

  • Lines with IMPLEMENTATION_SLOT usage:
    • Conditional checks
    • Storage operations (sstore, sload)
    • Return statements
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the new IMPLEMENTATION_SLOT value.

# Test: Search for the IMPLEMENTATION_SLOT usage. Expect: Only occurrences of the new value.
rg --type solidity "0x75b20eef8615de99c108b05f0dbda081c91897128caa336d75dffb97c4132b00"

Length of output: 120


Script:

#!/bin/bash
# Description: Verify the usage of the new IMPLEMENTATION_SLOT value.

# Find all Solidity files and search for the IMPLEMENTATION_SLOT value within them.
fd -e sol -x rg "0x75b20eef8615de99c108b05f0dbda081c91897128caa336d75dffb97c4132b00" {}

Length of output: 327


Script:

#!/bin/bash
# Description: Verify the usage of the new IMPLEMENTATION_SLOT value with context.

# Find all Solidity files and search for the IMPLEMENTATION_SLOT value within them, including context lines.
fd -e sol -x rg -A 5 -B 5 "0x75b20eef8615de99c108b05f0dbda081c91897128caa336d75dffb97c4132b00" {}

Length of output: 1298


Script:

#!/bin/bash
# Description: Verify the usage of the IMPLEMENTATION_SLOT constant throughout the Solidity files.

# Find all Solidity files and search for the usage of the IMPLEMENTATION_SLOT constant within them.
fd -e sol -x rg "IMPLEMENTATION_SLOT" {}

Length of output: 597

contracts/src/proxy/Initializable.sol (2)

73-75: LGTM! The constant value update is correct.

The comment update correctly describes the new calculation.


73-75: Verify the correctness of the constant value change.

The _INITIALIZABLE_STORAGE constant value has been updated. Ensure that this change aligns with the new storage layout as per EIP-7201.

Verification successful

The _INITIALIZABLE_STORAGE constant value change is correct and properly utilized within the codebase.

  • The _INITIALIZABLE_STORAGE constant is used in the contracts/src/proxy/Initializable.sol file, and the new value is correctly propagated.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of _INITIALIZABLE_STORAGE constant in the codebase.

# Test: Search for the constant usage. Expect: Only occurrences of the new value.
rg --type solidity -A 5 $'_INITIALIZABLE_STORAGE'

Length of output: 81


Script:

#!/bin/bash
# Description: Verify the usage of _INITIALIZABLE_STORAGE constant in the codebase.

# Test: Search for the constant usage in Solidity files.
rg '_INITIALIZABLE_STORAGE' --glob '*.sol' -A 5

Length of output: 689

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate to OZ Contracts v5
1 participant