Storage gaps in libraries and structs

Hey y'all!
We're implementing upgradeability in our contracts based on TransparentUpgradeableProxy.

The [documentation] mentions storage gaps that should be added to base contracts. We did so in commit 1b67c56.

Another thing I noticed is that we use libraries' storage where we store other structs. My understanding is that we should also add gaps there. Please see implementation in commit 7abe797.
Is this approach correct?

P.S. I'm a new user and the form didn't let me post too many links, so leaving it here for reference (remove the underscore):

According to Solidity storage layout, "The elements of structs and arrays are stored after each other, just as if they were given as individual values."

So if the struct is used as a state variable, then you should use gaps.

If the struct is stored in a mapping, mapping values are stored in different slots so there is no need for gaps (because they are not contiguous with other values). But you do need to ensure that new struct elements (for an upgrade) come after previous struct elements (see Upgrades with Peace of Mind: "Structs" Edition for an example).

1 Like