1

I'm working on an upgradable contract using Openzeppelin upgrade.

I'm aware that upgrading the contract must take the storage layout into consideration.

However, I'm still not so confident about the safeness when I want to upgrade the mapping of struct OR array of the struct.

For example, Is it safe to change from

struct User {
   bytes32 name;
}

contract Sample {
   mapping(uint32 => User) userById;
   User[] users;
}

To this

struct User {
   bytes32 name;
   uint256 age;
   uint256[] friends;
}

contract Sample {
   mapping(uint32 => User) userById;
   User[] users;
}
1

In this case it is not safe because of the User array isn't compatible with the previous storage layout.

User[] users;

Solidity stores array continuously User[0] | User[1] | ... with each user using the same amount of storage slots.

The old User occupies a single storage slot while the new uses three. So items the new version will be overlapping.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.