openzeppelin-upgrades icon indicating copy to clipboard operation
openzeppelin-upgrades copied to clipboard

gap resizing using solc v0.5.x

Open fruiz08 opened this issue 2 years ago • 1 comments

Related to #698

pragma solidity 0.5.8;

contract V1 {
    address public a;

    /**
     * @dev This empty reserved space is put in place to allow future versions to add new
     * variables without shifting down storage in the inheritance chain.
     * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
     */
    uint256[50] private __gap;
}
pragma solidity 0.5.8;

contract V2 {
    address public a;
    address public b;
    /**
     * @dev This empty reserved space is put in place to allow future versions to add new
     * variables without shifting down storage in the inheritance chain.
     * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
     */
    uint256[49] private __gap;
}

Test:

       const v1 = await deployTransparentProxy("V1", deployer, V1__factory);
       const V1 =  await ethers.getContractFactory("V1");
       const V2 =  await ethers.getContractFactory("V2");
        // forces the import of an existing proxy to be used with this plugin
        await upgrades.forceImport(v1.address, V1);
        // validates an implementation
        await upgrades.validateImplementation(V2);
        // compares the current implementation to the new implementation to check for storage layout compatibility errors
        await upgrades.validateUpgrade(v1.address, V2);

Error: `Error: New storage layout is incompatible

contracts/V2.sol:5: Inserted b

New variables should be placed after all existing inherited variables

contracts/V2.sol:11: Upgraded __gap to an incompatible type

  • Bad storage gap resize from 50 to 49 Size decrease must match with corresponding variable inserts`

For newer solc versions, at leat > v0.7.6, works fine

fruiz08 avatar Mar 01 '23 21:03 fruiz08

It is not expectd to work with 0.5.8. This feature requires the storageLayout compiler output, which was introduced in 0.5.13. If you need 0.5, that version might work. But note that both v1 and v2 should be compiled using that compiler version.

frangio avatar Mar 03 '23 18:03 frangio