Giter VIP home page Giter VIP logo

Comments (10)

abcoathup avatar abcoathup commented on August 30, 2024 3

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/

from openzeppelin-contracts-upgradeable.

frangio avatar frangio commented on August 30, 2024 2

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

from openzeppelin-contracts-upgradeable.

nneverlander avatar nneverlander commented on August 30, 2024

What is this variable used for?

from openzeppelin-contracts-upgradeable.

nventuro avatar nventuro commented on August 30, 2024

@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.

from openzeppelin-contracts-upgradeable.

ppoliani avatar ppoliani commented on August 30, 2024

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?

from openzeppelin-contracts-upgradeable.

alexon1234 avatar alexon1234 commented on August 30, 2024

Hi,

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

Thanks

from openzeppelin-contracts-upgradeable.

ppoliani avatar ppoliani commented on August 30, 2024

@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

from openzeppelin-contracts-upgradeable.

GhasemiReza21 avatar GhasemiReza21 commented on August 30, 2024

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

from openzeppelin-contracts-upgradeable.

frangio avatar frangio commented on August 30, 2024

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.

from openzeppelin-contracts-upgradeable.

itdream-dev avatar itdream-dev commented on August 30, 2024

@frangio, it's updated in the plugin now? because I'm getting the issue now.

  • New variables should be placed after all existing inherited variables
  • Bad storage gap resize from 46 to 45
    Size decrease must match with corresponding variable inserts
    if not updated, what's the manual way?

from openzeppelin-contracts-upgradeable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.