Skip to content

Add __gap properties to allow base contract variable adding #33

Description

@samuelmanzanera

On Solidity we cannot add new variable to a parent class, if the child class have some custom properties, as the index of the variable must be consistent.

A known solution is to use __gap properties.

Storage gaps are a convention for reserving storage slots in a base contract, allowing future versions of that contract to use up those slots without affecting the storage layout of child contracts.

To create a storage gap, declare a fixed-size array in the base contract with an initial number of slots. This can be an array of uint256 so that each element reserves a 32 byte slot. Use the name __gap or a name starting with _gap for the array so that OpenZeppelin Upgrades will recognize the gap:

ontract Base {
    uint256 base1;
    uint256[49] __gap;
}

contract Child is Base {
    uint256 child;
}

with a new version reducing the __gap property

contract Base {
    uint256 base1;
    uint256 base2; // 32 bytes
    uint256[48] __gap;
}

or

contract Base {
    uint256 base1;
    uint128 base2a; // 16 bytes
    uint128 base2b; // 16 bytes - continues from the same slot as above
    uint256[48] __gap;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions