# Storage gaps in libraries and structs

**URL:** <https://forum.openzeppelin.com/t/storage-gaps-in-libraries-and-structs/28171>\
**Category:** Smart Contracts\
**Tags:** proxies, upgrades\
**Created:** [April 29, 2022, 9:09am UTC](https://forum.openzeppelin.com/t/storage-gaps-in-libraries-and-structs/28171 "2022-04-29T09:09:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nkuba](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/nkuba/32/14670_2.png) [@nkuba](https://forum.openzeppelin.com/u/nkuba)\
**Post date:** [April 29, 2022, 9:09am UTC](https://forum.openzeppelin.com/t/storage-gaps-in-libraries-and-structs/28171/1 "2022-04-29T09:09:33Z")

</div>

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](https://github.com/keep-network/keep-core/pull/2926/commits/1b67c56bbdfe0b9d957e21919de2e16e2499be94).

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](https://github.com/keep-network/keep-core/pull/2926/commits/7abe797a86134770e48cee1a35f82e2e92e3c907).  
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):  
[documentation]: docs.openzeppelin\_.com/contracts/4.x/upgradeable#storage\_gaps

---

<div class="post-metadata">

**Author:** ![ericglau](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/ericglau/32/15149_2.png) [@ericglau](https://forum.openzeppelin.com/u/ericglau)\
**Post date:** [April 29, 2022, 4:16pm UTC](https://forum.openzeppelin.com/t/storage-gaps-in-libraries-and-structs/28171/2 "2022-04-29T16:16:34Z")

</div>

According to Solidity [storage layout](https://docs.soliditylang.org/en/v0.8.13/internals/layout_in_storage.html), "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](https://docs.soliditylang.org/en/v0.8.13/internals/layout_in_storage.html#mappings-and-dynamic-arrays) 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](https://forum.openzeppelin.com/t/upgrades-with-peace-of-mind-structs-edition/5526) for an example).
