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

Fix library reference substitution and regular expression matching for both contracts and libraries #16

Merged
merged 8 commits into from
Sep 9, 2024

Conversation

juliopavila
Copy link
Contributor

@juliopavila juliopavila commented Sep 4, 2024

Description:

This PR addresses two related issues in the zodiac-core package.

  1. Library Reference Replacement in Bytecode:
    There was a critical issue where the OZGovernor contract, which depends on the MultisendEncoder library, would fail to deploy because the contract bytecode saved a reference to the library but not its address. This caused deployment failures since the library's placeholder in the bytecode was not being correctly replaced by the actual deployed address.

  2. Regular Expression Mismatch in sourcePathFromSourceCode:
    Additionally, the sourcePathFromSourceCode function was returning null when attempting to resolve the MultisendEncoder contract. Upon investigation, it was discovered that MultisendEncoder is actually defined as a library, not a contract, and the function's regular expression was only matching contract definitions. This mismatch caused the function to fail when looking for libraries.

Solutions:

  1. Library Reference Replacement:

    • Added the replaceLibraryReferences function to handle replacing placeholders in the contract bytecode with the actual deployed addresses of libraries.
    • Ensured that library addresses are properly validated (formatted as 40-character hexadecimal strings) and padded if necessary before replacement.
    • This solution ensures that the OZGovernor contract can now correctly link to the MultisendEncoder library during deployment.
  2. Regular Expression Update in sourcePathFromSourceCode:

    • Modified the regular expression in the sourcePathFromSourceCode function to match both contract and library definitions. This change allows the function to successfully resolve libraries as well as contracts.

    Old RegExp:

    const contractPattern = new RegExp(`contract\\s+${contractName}\\s+`, "g");

    New RegExp:

    const contractPattern = new RegExp(`(contract|library)\\s+${contractName}\\s+`, "g");

Changes Introduced:

  1. New replaceLibraryReferences Function:

    • Introduced a function to replace library references in the bytecode with the actual deployed addresses. The function ensures proper formatting and validation of the library addresses and replaces any placeholders in the bytecode with these addresses.
  2. Integration into getBuildArtifact:

    • The replaceLibraryReferences function was integrated into getBuildArtifact to ensure that any contract bytecode with library references is correctly resolved and linked before deployment.
  3. Regular Expression Fix:

    • Updated the regular expression in sourcePathFromSourceCode to match both contract and library keywords, ensuring libraries like MultisendEncoder are properly detected in the source code.
  4. Library Address Resolution:

    • Utilized getCreate2Address to calculate and retrieve the deployed address for libraries, ensuring that the OZGovernor contract now correctly links with MultisendEncoder.

Testing:

  • Verified the deployment of the OZGovernor contract using the updated bytecode, ensuring library references were correctly replaced and the contract no longer fails during deployment.
  • Confirmed that the sourcePathFromSourceCode function now resolves both contracts and libraries.

…libraries

Updated the regular expression in the sourcePathFromSourceCode function to account for both `contract` and `library` declarations. This resolves an issue where library definitions were not being matched, causing the function to return `null`. The new RegExp now looks for both `contract` and `library` keywords.
@juliopavila juliopavila added the bug Something isn't working label Sep 4, 2024
@juliopavila juliopavila changed the title Fix: Update sourcePathFromSourceCode to match both contracts and libraries Fix library reference substitution and regular expression matching for both contracts and libraries Sep 4, 2024
@coveralls
Copy link

coveralls commented Sep 4, 2024

Pull Request Test Coverage Report for Build 10773316312

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 99.512%

Totals Coverage Status
Change from base Build 10621322757: 0.0%
Covered Lines: 122
Relevant Lines: 123

💛 - Coveralls

@samepant samepant merged commit 1b99ccd into master Sep 9, 2024
1 of 2 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants