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

Add docstring for the gap variable #47

Closed
come-maiz opened this issue Feb 12, 2019 · 10 comments
Closed

Add docstring for the gap variable #47

come-maiz opened this issue Feb 12, 2019 · 10 comments

Comments

@come-maiz
Copy link

Contracts have an undocumented uint256[50] private ______gap;

This variable should be documented with a very clear explanation, because it's weird.

@nneverlander
Copy link

What is this variable used for?

@nventuro
Copy link
Contributor

@nevverlander Since OpenZeppelin contracts are used by inheritance, user-defined variables will be placed by the compiler after OpenZeppelin's ones. If, in a newer version, new variables are added by the library, the storage layouts would be incompatible, and an upgrade would not be possible.

The gap is a workaround to that issue: by leaving a 50-slot gap, we're able to increase the contract's storage by that amount (provided we also remove the same slots from the gap) with no clashing issues.

@ppoliani
Copy link

ppoliani commented Jan 28, 2021

May I ask what would happen is some of the base contracts modify the layout e.g. adding a new field. Should we still keep the gap of 50 slot or should it be 50 -

For example, imagine the following hierarchy]

abstract contract Base1 {
  uint256 val1;

  uint256[50] private ______gap;
}

abstract contract Base2 {
  uint256 val2;
  uint256[50] private ______gap;
}

contract Child is Base1, Base2 {

}

Then at some point we decide to update the Base1 by adding a new fields val3

abstract contract Base1V2 {
  uint256 val1;
  uint256 val3;

  uint256[50] private ______gap;
}

abstract contract Base2 {
  uint256 val2;
  uint256[50] private ______gap;
}

contract Child is Base1V2, Base2 {}

Should we still keep uint256[50] private ______gap; in Base1V2 or should it become uint256[49] private ______gap;? Basically, will the val3 override val2 from Base2 if we keep the gaps of 50 slots?

@abcoathup
Copy link
Contributor

Hi @ppoliani,

If you add a new state variable to Base1V2, then you should reduce the reserved storage gap to prevent the "shifting down" of all of the state variables below in the inheritance chain.

Please see the documentation for how storage gaps are used in OpenZeppelin Contracts: https://docs.openzeppelin.com/contracts/3.x/upgradeable#storage_gaps

If you have further questions, you can ask in the community forum: https://forum.openzeppelin.com/

@alexon1234
Copy link

alexon1234 commented May 11, 2021

Hi,

I am also wondering why it has to be an array of 50? Why not 40 or 30?

Thanks

@ppoliani
Copy link

@alexon1234 I'm not sure either maybe @abcoathup can correct me, but since the number of slots does not cause any overhead then something safe as 50 might not be a bad figure to use

@frangio
Copy link
Contributor

frangio commented May 12, 2021

The number 50 seemed enough. There is a very small overhead in code size because slot numbers will be slightly larger.

@GhasemiReza21
Copy link

Hi @abcoathup
when I added a new state variable to Base contracts and reduce the reserved storage gap and try to upgrade I get this error :
Bad array resize from 47 to 46 ,
New variables should be placed after all existing inherited variables

@frangio
Copy link
Contributor

frangio commented Jan 31, 2022

Fixed in OpenZeppelin/openzeppelin-transpiler#73.


@GhasemiReza21 Reducing gaps is not yet supported in the Upgrades Plugins but we're adding this in the next few weeks. For now you need to resolve this issue manually, if you need help please post in the OpenZeppelin Forum.

@itdream-dev

This comment was marked as off-topic.

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

No branches or pull requests

9 participants